MQL4 TUTORIAL BASICS – 83 SIMPLE BOLLINGER BANDS BUY TRAILING STOP

video

In this video, we are going to create an expert advisor that is able to move a buy trailing stop for the bollinger bands indicator, so let’s find out how to do that with mql4.
To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple bollinger bands buy trailing stop, click on continue, continue and finish.
Now you can remove everything above the ontick function and we also delete the two comment lines here.
First, we want to create a static variable, it’s a double variable and it is called last stop bollinger value, and to have something that we can modify we need a position, so let’s check if we have no open positions – that would be the case when orders total equals zero – and in that case, we will open a test position by using order send. I would like to open a buy position for ten micro lot, and whenever we do that we want to set the last stop bollinger value to zero because this is a new position so we need a new stop value.
Let’s continue and calculate the lower bollinger band and the upper bollinger band that is done by using the ibands function that comes with mql4, for the current symbol on the chart and the currently selected period, we want to calculate the result based on twenty candles, the deviation value is two, we don’t need any shift values here, the calculation should be done based on the close price and we use either mode lower or mode upper to calculate the lower or the upper bollinger band for candle one – that’s the last finished candle on the chart – and afterwards we want to try to move the stop loss.
We try it because it will not be changed every time we want to do that, that depends on a few things so you might see these order modify errors here.
For example, if the current price is too close to the lower bollinger band we cannot move the stop loss upwards because of the spread and whenever we want to try to change the stop loss we want to call a function called check bollinger buy trailing stop and we pass the value for the lower bollinger band as a parameter here.
Afterwards, we want to assign the value for the lower bollinger band to the last stop bollinger value because the current value will be the last value the next time we call the function so let’s actually create that function.
We use void because we don’t have a return type here.
The name of the function is check bollinger buy trailing stop and we pass the parameter for the lower bollinger bands.
Afterwards, we use a for loop to go through all the open orders. Now we want to use order select to select each of those orders.
In the next step, we want to find out if the if the current order symbol is equal to the symbol on the chart. We check if the order type equals op buy for a buy order and if all those conditions apply we want to find out if the current order stop loss is below the lower bollinger band and if this is true we use order modify for the current order ticket, we don’t change the order open price but we will try to change the stop loss to the value for the lower bollinger band, the order take profit, the expiration and the color values are not modified here.
Finally, we need to close the loops and the function and that’s about it.
If you have no idea what all the code here does or if this was too fast for you or if maybe you want to watch one of the other videos in the basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, you shouldn’t get any errors here and if that is the case you can press the little button here or press F4 on your keyboard to go back to Metatrader.
And in Metatrader we want to open a blank chart and click on insert, indicators, trend, bollinger bands, please select a period for twenty candles, the deviation value is two, applied to the close price, and please pick a color here and click on okay. Now you should see the bollinger bands indicator and here we want to right-click on the chart, select template, save template and save it as tester dot tpl because that is the chart that we are going to see in the next strategy test. You can overwrite the old value here and if you don’t see the strategy tester please click on view, strategy tester or press ctrl and r, please pick the new file, simple bollinger bands buy trailing stop dot ex4, mark the option for the visual mode here and start your test.
Here we are!
And we should see that the stop loss is rising here so our little expert advisor works as designed and in this little video you have learned how to create an expert advisor that is able to move a buy trailing stop for the bollinger bands indicator and you have coded it yourself with a few lines of mql4 code.