MQL5 TUTORIAL BASICS – 83 SIMPLE BOLLINGER BANDS TRAILING STOP

video

In this video we are going to create an expert advisor that is able to calculate a buy trailing stop for the bollinger bands indicator, so let’s find out how to do that with mql5.
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 delete everything above the ontick function and the two comment lines here.
First, we want to include the file trade dot mqh, this one comes with mql5 and it contains the class ctrade and we are going to create an instance from ctrade that is called trade.
Inside of the ontick function, we want to create a static double variable, called last stop bollinger bands value.
Afterwards, we want to calculate the ask price and the bid price by using symbol info double for the current symbol on the chart, we use symbol ask or symbol bid and we use normalize double and underscore digits to automatically calculate the right number of digits behind the dot.
For a trailing stop we need to have an open position, so let’s create a test position, whenever positions total is below one we use trade dot buy to buy ten micro lot and we set the value for the last stop bollinger bands value to zero.
Now let’s create an array for the upper band array and for the lower band array. Actually, the bollinger bands have three arrays for the upper, the middle, and the lower band but in this case, we only need the two extremes. Let’s use array set as series to sort both arrays from the current candle downwards.
Now we can define the bollinger bands by using the integrated ibands function for the current symbol on the chart and the currently selected period, we want to calculate the result for twenty candles, we use no shift value, the deviation value is two and we want to calculate the results based on the close price.
Now we use copy buffer to copy the data according to the bollinger bands definition that we have created here, we use buffer one for the upper bend array and buffer two for the lower band array starting from the current candle zero and we want to copy the values for three candles and store them in the array.
And now we can calculate the last candle by looking into candle one of our upper band array or our lower band array, this really depends on if you are looking for a buy or a sell trade.
For a buy trade, we want to check if the last lower band value was below the current bid price and we also want to know if the current value is above the last bollinger bands value, and if this is true we want to call a function called, check bollinger buy trailing stop and we pass the value my last lower band value.
Afterwards, we are going to assign the last lower band value to the last stop bollinger band value.
So far so good but this function doesn’t exist so far, so let’s create that now!
We are using void because we have no return value, the name of the function is check bollinger buy trailing stop and we pass the last lower band value here.
Now we use a for loop to go through all the open positions, with position get symbol for the current counter value we will get the position symbol and that makes it possible to check if the current symbol on the chart equals the position symbol, if that is true, we use position get integer, position underscore ticket to get the current position ticket number and with position get double for the current stop loss – the parameter is called position underscore sl – we get the current stop loss and only if the current stop loss is below the last lower band value we use trade dot position modify for the current position ticket to move the current stop loss to the last lower band value, this parameter is for the take profit and we will leave that one unchanged.
Finally, we want to close the if loop, the for loop and the function, and that’s about it.
If this was too fast for you or if you have no idea what all the code here does 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, let’s enable the toolbar here and click on compile or press F7, I have no errors here and if this is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader you want to click on view, strategy tester or press control and r, please pick the new file, simple bollinger bands buy trailing stop dot ex5, mark the option for the visual mode here and start a test.
Here we are! We have a first position and a stop loss and when the lower bollinger band is rising, the stop loss is adjusted, so our little expert advisor works as designed and in this little video you have learned how to create a buy trailing stop for the lower bollinger band and you have coded it yourself with a few lines of mql5 code.