In this video we are going to create a Bollinger Bands standalone Expert Advisor that is going to actually trade the Bollinger Band signals. We have already made sell trade here and now we want to find out how we can do that in MQL5.
To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue“, I will call this one: “SimpleBollingerBandsStandalone”, click on “Continue”,
“Continue“ and “Finish”, and now you can delete everything that is above the “OnTick” function and we will also remove the two command lines here.
First we need to include the Trade.mqh file; it will enable us to create a “CTrade” instance that will be called: “trade” and we are using it later to open a position.
In the “OnTick“ function we first need to create an empty string, we will call it: “entry” and we don’t assign a value because we are going to calculate that now.
…but before we do that we need to get the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol and we use: “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price.
The “NormalizeDouble” function in combination with “_Digits” will help us to get the number of digits for the current symbol of the current chart because we have currency pairs with only 3 digits and other ones with 5 digits behind the dot.
We also need to create an array for the prices that will be called: “PriceInfo”, we use “MqlRates” format and cure rate stores the information about prices like open, high, low and close.
Now we sort the price array (PriceInfo) from the current candidate downwards by using “ArraySetAsSeries“ for our “PriceInfo” array that we have created here and after we have done that we use “CopyRates” for our “PriceInfo” array to fill the array with price data for the current symbol and the currently selected period on the chart from the current candle 0 (zero) for 3 candles and we store the result in our “PriceInfo” array.
We also need to create an array for several prices for the upper and lower band of the Bollinger Bands; this is the upper band, this is the lower band and whenever the price breaks out here and re-enters into the Bollinger Bands that would be a signal. When it does it from below that’s a buy signal and when it does it from above like here that would be a sell signal.
Let’s also use “ArraySetAsSeries” to sort the upper band array (UpperBandArray) and the lower band array (LowerBandArray) and now we actually define the Bollinger Bands by using the built-in “iBands” definition for the current symbol on the chart and the currently selected period. We will use 20 candles and if you click on “Insert/ Indicators/ Trend/ Bollinger Bands” you will see that we use a period of 20 candles, we have a shift value of 0 (zero) and the deviation value is 2, also what we use here and the calculation will be based on the close price.
Let’s copy the “PriceInfo” into that array by using “CopyBuffer” for the Bollinger Bands definition (BollingerBandsDefinition) that we have created here.
Buffer 1 and buffer 2 are used to calculate the upper and the lower band from the current candle 0 (zero) for 3 candles and the result will be stored in the upper band array (UpperBandArray) or the lower band array (LowerBandArray).
Let’s calculate the Expert Advisor for the current candle; my upper band value (myUpperBandValue) will be the value for candle 0 (zero) in the upper band array (UpperBandArray), the lower band value will be the value for candle 0 (zero) in the lower band array (LowerBandArray) and to calculate a crossover we would also need to calculate the Expert Advisor for the candle before – that is candle 1 – and we store the results in the variable: my last upper band value (myLastUpperBandValue) and my last lower band value (myLastLowerBandValue).
Now we want to check if we have a re-entry from below; that would be true if the close price for candle 0 (zero) – that’s the current price – is above my lower band value (myLowerBandValue) and if the close price for the candle before was below my last lower band value (myLastLowerBandValue) and in that case we would have a buy entry so we assign the word: ”buy” to our entry.
In the other case; if we have a re-entry from above that would happen if the current price is below my upper band value (myUpperBandValue) and if the price for candle 1 was above my last upper band value (myLastUpperBandValue) and in that case we assign the word: “sell” to our entry.
If the entry equals sell and “PositionsTotal” is less than 1 – that would be the case when we have no open positions – we want to sell 10 micro lot.
In the other case if the entry equals buy and we have no open positions we want to buy 10 micro lot. Last but not least we want to use the “Comment” function to output the words: ”Entry Signal:“ followed by our calculated entry directly on the chart.
So that’s it, please click on the “Compile” button or press F7 on your keyboard, this worked without any errors and without any warnings so now I can click here or press F4 to go back to Metatrader and
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R and in the Strategy Tester please make sure to pick the “SimpleBollingerBandsStandalone.ex5” file, mark the visualization option here and start your test.
…and here is our little Expert Advisor at work, it has already opened a trade here and now you know how to automate the Bollinger Bands signal with MQL5 and create a simple standalone Expert Advisor that is actually going to trade and you did it with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE BOLLINGER BANDS STANDALONE EXPERT ADVISOR”SimpleBollingerBandsStandAloneEA.txt – Downloaded 575 times – 2.35 KB