MQL4 TUTORIAL BASICS – 84 SIMPLE BOLLINGER BANDS SELL TRAILING STOP

video

In this video we are going to create a sell trailing stop for the upper bollinger band, whenever the bollinger bands are going down we will see the trailing stop is moved, 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 band sell trailing stop, click on continue, continue and finish.
Now you can delete everything above the ontick function and let’s also remove the two comment lines.
We start by creating a static double variable and we will call it last stop bollinger value, this is going to hold the last value so that we can compare it with the current one.
And to move a stop loss we need to open a test position, we use order send for the current symbol on the chart, we use op sell to open a sell position for ten micro lot and we also want to reset the last stop bollinger value to zero here.
Now we can use the included ibands function that comes with mql4 for the current symbol on the chart and the currently selected period, we want to calculate the result for twenty candles, the deviation is two, we don’t use a shift value and we want to calculate the prices based on the close price once for the lower and once for the upper bollinger band, we do that for candle one.
Now that we have the value we pass the value for the upper bollinger band to a user-defined function called check bollinger sell trailing stop. This function doesn’t exist so far, so we need to create it in a few seconds but before we do that we take the value for the upper bollinger band and save it as the last stop bollinger value because this is the value that we are going to use the next time we call the function.
So let’s continue with the user-defined function, we use void because we don’t need a return value, the name of the function is check bollinger sell trailing stop and we pass the value for the upper bollinger band as a parameter.
Inside of the function we use a for loop to go through all the open positions, we want to select each order that is done by using order select for the current order number, that’s the counter of the for loop here. Now let’s check if the current order symbol is the same as the symbol on our chart, symbol equals currency pair in our case.
In the next step, we want to find out if the order type is op sell. That would mean we have a sell trade and let’s find out if the current order stop loss is bigger than the value for the upper bollinger band because that’s when we want to adjust the sell trailing stop.
And if that is true we use order modify for the current order ticket, we don’t change the order open price but we do change the stop loss and assign the value for the upper bollinger band, the order take profit is also unchanged and that is true for the expiration date and for the color.
Finally, we want to close the if loop, the for loop and the function and that’s about it.
Well, 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 this 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, I don’t have any errors and if that 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 pick an empty chart, click on insert, indicators, trend, bollinger bands, we want to select a period for twenty candles, the deviation value is two, applied to the close price, no shift value, now pick a color and click on okay and here are the bollinger bands, now let’s right-click select template, save template and save it as tester dot tpl because this is the template that we are going to see in the next strategy test.
So let’s start one and click on view, strategy tester or press ctrl and r, please pick the new file, simple bollinger bands sell trailing stop dot ex4, mark the option for the visual mode here and start a test.
Here we are! This is our first sell position and now we should see that the trailing stop is changed that happened right now so our little expert advisor is working as expected and in this little video you have learned how to create a sell trailing stop for the upper bollinger band and you have coded it yourself with a few lines of mql4 code.