MQL4 TUTORIAL – SIMPLE STANDALONE BOLLINGER BANDS EA

video

In this video we want to create an automated Expert Advisor that is actually able to trade this Indicator; it’s the Bollinger Bands Indicator for 20 candles, actually it’s producing buy or sell signals directly on the chart.
Whenever the candle close below the lower Bollinger Band that would be a buy signal and when we have a breakout on the other side that would be a sell signal.
Now let’s find out how to create an Expert Advisor in MQL4 that is actually going to trade.
To do that please click on a little button here or press F4 on your keyboard, 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 file: “SimpleBollingerBandsStandAloneEA”, click on: “Continue”, “Continue” and “Finish”.
Now you can remove everything that is above the “OnTick” function and the two comment lines here. First we need to create an empty string for the signal, it will be called: “signal” and we don’t assign a value because we are going to calculate that now.
The Bollinger Bands Indicator has 3 different bands, we are interested in the upper band and the lower band, so we use the included “iBands” function for the current symbol and the currently on the chart selected period for 20 candles.
If you look at the Bollinger Bands properties you will see that we use 20 candles, a shift value of 0, and the deviation’s value is 2.
These are the default values that we are going to use here, the result will be calculated based on the close price. We use “MODE_LOWER” for the lower Bollinger Band (LowerBB) and “MODE_UPPER” for the upper Bollinger Band (UpperBB).
The first one will be calculated based on candle 1 and now we do it again for the previous lower Bollinger Band (PrevLowerBB) and the previous upper Bollinger Band (PrevUpperBB) and this time we are going to use the values for candle 2.

If the close price of candle 2 was below the lower Bollinger Band, so if close for candle 2 is less than the value for previous lower BB (PrevLowerBB) and if the close price for candle 1 is above the Bollinger Band – that would be the case when close 1 is bigger than lower BB (LowerBB) – that would be a buy signal and we would assign the word: “buy” to our signal.
In the other case if the close price for candle 2 was above the upper Bollinger Band, so if close 2 is bigger than previous upper BB (PrevUpperBB) and if the close price for candle 1 is now below the upper Bollinger Band, so if close 1 is less than upper BB (UpperBB) that would be a sell signal and now we would assign the word: “sell” to our signal.
And whenever the signal equals buy and ”OrdersTotal” equals 0 (zero) – that would be the case when we have no open trades – we want to send a buy order so we use the “OrderSend” statement and “OP_BUY” to buy 10 micro lot.
In the other case if the signal equal sell and we have no open orders we use “OrderSend” to open a sell trade for 10 micro lot and last but not least we create a chart output that is done by using the “Comment” statement, it will output the word: “Signal:” followed by our calculated signal value.
Okay. That’s it.
Now you can click on the “Compile” button or press F7 on your keyboard, we don’t have any errors here, two minor warnings and if that is the case you can click on the button here or press F4 to go back to Metatrader.
In Metatrader we use a little trick, we click on: “Insert/ Indicators/ Trend/ Bollinger Bands” here are our default values, we click on “OK” and now we save the template by right-clicking into the chart and selecting “Template/ Save Template” and please save it as: “tester.tpl” because this is the template that our back tester is going to use.
After you have saved the template please click on “View/ Strategy Tester” or press CTRL and R and here you want to select the file: “SimpleBollingerBandsStandAloneEA.ex4”, enable the visual mode here and start a test.
…and here is our little Expert Advisor at work, it already opened a trade and now you know how to automate the Bollinger Bands Indicator for Metatrader 4 and you have done it yourself with a few lines of MQL4 code.