In this video we are going to create an expert advisor that is able to move a sell trailing stop for the upper bollinger band, 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 sell trailing stop, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, this one will give us some simple trading functions, it contains the class ctrade and we are going to create an instance of ctrade that will be called trade.
Inside of the ontick function, we want to create a static double variable, called last stop bollinger bands value. We are going to use it later to move our trailing stop and compare the current value with the last value. We also need to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol ask or symbol underscore bid and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot depending on the currency pair that might be three digits or five digits.
And to move our sell trailing stop we need to have a test position so if positions total is less than one we use trade dot sell to sell ten micro lot and now I will set the last stop bollinger bands value to an unrealistic high value of one thousand, we also could set it to zero here but we would need to add another condition later on and for this simple example it is good enough.
Now let’s create an upper band array and a lower band array. Actually, we have a middle band but we don’t need it, so let’s use array set as series for the upper and the lower band to sort both arrays from the current candle downwards.
Now we can define the bollinger bands by using the included ibands function for the current symbol on the chart and the currently selected period on that chart, we want to calculate it for twenty candles, don’t use a shift value, the deviation value is two and we are going to calculate the results based on the close price.
With copy buffer we can now fill our array according to the bollinger bands definition that we have created here, we will do it for buffer one and buffer two. Buffer one is for the upper bend array, we start with candle zero and we want to copy the values for three candles and store them in the upper band array or in this case store them in the lower band array and that makes it possible to calculate the last upper band value and the last lower band value by just looking at candle one in the upper and in the lower band array.
Let’s continue with the conditions because if my last upper band value is above the ask price and if my last upper band value is below the last stop bollinger bands value that is when it’s time to check the bollinger sell trailing stop, we are passing the parameter my last upper bands value here and afterwards, we assign the last upper band value to the variable last stop bollinger bands value because the next time we call the function the current value will be the last one.
Now let’s continue with the user-defined function, we are using void because we don’t need a return value, the name is check bollinger sell trailing stop, this is the parameter that we have passed and now we use a for loop to go through all the open positions. Position get symbol will give us the currency pair for the current position.
Now we need to check if the current symbol on the chart is the same symbol as the position symbol and maybe we should close the ontick function here, so let’s continue with the position ticket, we will get that by using position get integer and the parameter is position underscore ticket. Let’s repeat that for the current stop loss! To get the current stop loss we use position get double and the parameter is position underscore sl and if the current stop loss is bigger than the my last upper bands value or if we have no stop loss at all – in that case, the current stop loss would be zero – that is when we want to use trade dot position modify for the current position ticket and set the stop loss to the my last upper band value, the take profit value will not be changed and please don’t forget this or condition here because when you don’t set a stop loss it can never be bigger than this value and if you forget to check this condition the stop loss will never be moved.
Now let’s close the loops here and close the function, and that’s it.
If you have no idea what all the code here does or this was too fast for you you might want to watch 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 on your keyboard…
I messed up the parentheses, we have closed one if loop here but there is another one! So let’s fix that! Here we are! Let’s recompile the code and this time it worked without 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 please click on view, strategy tester or press control and r, now let’s pick the file, simple bollinger bands sell trailing stop dot ex5, mark the option for the visual mode here and start a test.
Here we are! This is our sell trailing stop and its trailing, so our little expert advisor works as designed and in this little video you have learned how to create a sell trailing stop for the bollinger band indicator and you have coded it yourself with a few lines of mql5 code.