MQL4 – ADVANCED BOLLINGER BANDS ENTRY

video
play-sharp-fill

Here is the new video in which we shall code an Expert Advisor that is able to trade the Bollinger Bands Indicator. Some few years before this was the first Indicator that generated profit for me so it is one of my favorite.
Now, how do we get a sell signal from the Bollinger Bands for example?
Once the last candle goes outside the upper band and closing outside the band we wait for the current candle to reenter inside the Bollinger Bands and actually close below the upper band. Accordingly we get the buy signal when the current candle returns inside the lower band from below.
Now let’s see how to build an Expert Advisor which shall generate the signals and will function as an entry module (CheckEntry_Bollinger) that is working together with a main module and opening positions by using the Bollinger Bands Indicator.
For to start please hit the button here in your Metatrader or use the F4 key for the shortcut and wait for the Metaeditor to come up. Then let’s create a new Expert Advisor by clicking on: “File/ New/ Expert Advisor (template)” from a template, click on “Next”. I name the new file as: “CheckEntry_Bollinger” and it will be called by the main module.
Let’s continue by clicking on: “Next”, “Next” again then “Finish”. Here please delete all the things above the “OnTick” function even the two comment lines.
Now we don’t need the “OnTick” function but instead we shall use a string function (string CheckEntry). Our main module shall run this function to check if there is a signal.
Let’s code the string variable to start with and let’s leave it empty – that is we are not adding any value. We name it as: “signal”.
Next is to do the calculations for the Bollinger Bands Indicator. We shall calculate the values for the upper and the lower bands with the “iBands” function which comes with MQL4 for the same symbol and the period which are sat on the chart.
We are using the values of the last 20 candles based on the close price and the deviation of 2 for calculating the signal. This is done by “MODE_LOWER” for the lower Bollinger Band and “MODE_UPPER” for the upper Bollinger Band. Now let’s see the calculation for the buy signal. Here is the calculation for candle 1 – candle 1 is always the last candle before the one which is currently building, so this is candle 0 here and candle 1 is this one.
We are having 3 conditions to be confirmed to say we have a buy signal. First we want to know if a crossover has happened. So how do we do that? For that we also need to calculate the values of the candle before candle 1, so that is for candle 2. Basically we do that the exact same way as we did for candle 1. Secondly we also check if the price is going up and thirdly if the close of candle 1 is now above the lower band from outside. If all 3 conditions are true we render the word: “buy” to the signal.

Logically with we have to repeat the same for the sell signal. So we want to confirm if the price bar (candle) has returned inside the Bollinger Band after crossing the upper band a second time. So if price is moving down (close 1 is lower than close 2) and if candle 2 closed above the upper Bollinger Band and candle 1 closed inside the band (below the upper band) we render the word: “sell” to the signal.
After confirming a signal we need to return its values (buy or sell) to the main module. We do that with the “return” function.
My main module is the Forex Trading Framework for MQL4/ version of April 2018.
What my framework is doing; it contains several functions which are opening and closing trades based on predefined criteria’s in MQL4.
Now let’s save the file we just coded (CheckEntry_Bollinger.mq4) then by the use of the “include” function we are going to import it into the main module.
So next I am opening my main module (Forex Trading Framework), you could do the same and open yours or if you don’t have one just download my demo version from my website.
Next we can compile the framework by hitting the F7 key or use the “Compile” button. If we get no errors or warnings we can get back to Metatrader so please hit the F4 key or use the button here.
When in Metatrader we shall make a simple trick; click on “Insert/ Indicators/ Trend/ Bollinger Bands”, let’s just use the standard values (period 20 and deviation 2). We don’t need the shift. Please click “OK” and now you can see the Indicator below the candles.
Okay, now let’s do a right mouse click in the template here, we select “Template/ Save Template”, let’s save it with a name: “tester.tpl” so the back-tester will display the template with the Bollinger Bands when running in visual mode.
After these we can run a back-test on our entry module Expert Advisor. Let’s click on: “View/ Strategy Tester”, you can also use “CTRL + R”, here please open the main module, turn on the visual mode by ticking the check box and start the test.
The Expert Advisor is already trading here, even made some profit, so now you just learned how to code an Expert Advisor entry module which is opening positions based on the signals from the Bollinger Band entry module and you have built it yourself in MQL4 with the use of a few lines of code only.

Download “MQL4 - ADVANCED BOLLINGER BANDS ENTRY” AdvancedBollingerBands.txt – Downloaded 1468 times – 1.10 KB