MQL4 TUTORIAL – SIMPLE BOLLINGER BANDS EXPERT ADVISOR

video
play-sharp-fill

In this video, we are going to talk about an Expert Advisor that can tell you if you should buy or sell based on the Bollinger Bands indicator.
The Bollinger Bands indicator has three bands.
This is the lower band. This is the middle band, and this is the upper band. Whenever a candle closes above the upper band and reenters into the bands this is a signal for a sell trade.
In the other case when the candle closes below the lower band and you see a reentry from below this is a buy signal.
Usually you won’t see the Bollinger Bands in your back-testing and to change this you can insert the Bollinger Bands indicator, we are going to use the period of 20 candles and deviation value of 2 because that is the standard when you click reset.
Now click on ok.
I will change the properties here because I like the bands to have the red color.
No I right click on the chart, click on Template/Save Template. I will call this one: Bollinger Bands.tpl. Tpl stands for Template. Now let’s save this and when you start a new back-test you simply need to right click the chart, select template, Bollinger Bands and here we are.
Now we are going to create an Expert Advisor that can calculate Bollinger Bands entry signals.
To do that please click on the little button here or press F4 in your Metatrader and now you should see Metaeditor and here you want to click on File/New/Expert Advisor (template) from template. Continue (Next).
I’ll call this one Simple Bollinger Bands. Continue (Next), Continue (Next), and Finish, and now you can delete everything above the OnTick() function and the two comment lines here.
Now let’s create an empty string for the signal.
I’ll call it signal and it has no value here, and to calculate the lower Bollinger Band we use the function iBands. iBands is a built-in function of MQL4 and it takes a few parameters. The first one is _Symbol; that’s the symbol on the chart, the second parameter is _Period; that’s the currently selected period on the chart, like the minute or the hour chart.
We will use 20 candles and the deviation of 2 because that is what you see when you click on the Bollinger Bands properties.
Period 20 candles, deviation 2, we have no shift and we apply it on the close price so the parameter for the shift is 0, and we use: PRICE_CLOSE for the close price and because this is the lower Bollinger Band we use MODE_LOWER and it will be calculated for candle 1.
To calculate the upper Bollinger Band, you basically do the same but this time we use MODE_UPPER, everything else is like in this line.
Candle 1 is the candle before the current candle, now when this candle isn’t finished this would be candle 1, and this would be candle 2.
To calculate the value for candle 2 we repeat those two lines here and now the only thing we need to change in the two new lines is the last parameter for candle 2.
Let’s calculate a buy signal. Now if the Ask price is above the close price of the last candle; that’s candle[1] and if the close price for candle[2] was below the lower band.
That’s close 2 is below the value of the previous lower Bollinger Band and if the close price for candle[1] is above the lower band so that’s close 1 is greater than lower Bollinger Band that would be a signal to buy.
In the other case if the price is below the last close so if Bid is below close 1 and if close 2 was above the upper band so close 2 is above previous upper Bollinger Band and close 1 is below the upper band so the close price for candle[1] is now below the upper Bollinger Band that would be a crossing like here and that would be a sell signal so we assign the word: sell to our signal.
In the last step, we want to create a chart output so we use the Comment function from MQL4 to output the word: Signal, followed by the signal in the upper left corner of the chart. So, whenever we see a reentry here it will tell us that we have to buy and when the price was above the upper band and we have a reentry it will tell us that we have a signal to sell.
Okay, if you have done everything right you now can click on the Compile button or press F7 on your keyboard and you should see no errors and no warnings here, and if that is the case please click on the little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on View/Startegy Tester or press CTRL and R and that will show the Strategy Tester panel here and there you want to select the SimpleBollingetBands.ex4 file.
Please make sure to enable the visual mode here and click on start.
Now you should see something like this, let’s do a right click and load the Bollinger Bands template we created. We can also switch to the candle view and here is our little Expert Advisor at work and it will create buy or sell signal right on your chart.
Now you know how to create an Expert Advisor for Bollinger Band signals and you have done it yourself with a few lines of MQL4 code.