MQL5 TUTORIAL BASICS – 27 HOW TO CODE A SIMPLE SELL TRAILING STOP

video

In this video we are going to create a trailing stop for sell trades, you can see when the price falls that the trailing stop is adjusted automatically and now we want to find out how to do that in mql5.
To start please click on the 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 sell trailing stop, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
First, we include the file trade dot mqh that one will give us simplified trading functions, and it includes a class called c trade, we will create an instance of that class called trade that is what we are going to use later on to open a new trade.
To do that we first need to find out the bid price that is done by using symbol info double, for the current symbol on the chart and we use symbol underscore bid – all in capital letters.
I also like to use normalize double and underscore digits because that will automatically calculate the number of digits behind the dot depending on the currency pair that could be 3 or 5 digits.
So let’s first find out if we have any open positions, and if positions total equals zero that would mean that we don’t have any positions. So on a demo account, we would now use trade dot sell, to sell 10 micro lot because we need a position otherwise, we cannot find out if the trailing stop works and afterwards we want to call the function check the trailing stop and pass the bid price.
Well, this function does not exist, so we now need to create it.
I use void because I don’t need to return any values, I just want to check and adjust the trailing stop.
Our function will take the bid price as a parameter and I would like to have the stop loss 150 points above the bid price. So let’s go through all the positions that is done with a for loop, positions total will give us the number of open positions and we want to count down until we have no open positions left.
First we need to get the position symbol for the current position because we only want to continue if the current symbol on the chart and the position symbol are equal and we also want to check if the position type is order type sell – all in capital letters – because if you mark that and press F1 you will see that this kind of order type is for a market sell order and that’s what we need.
If this is the case we want to get the position ticket – that’s kind of a license plate that will identify one particular position – and we get it by using position get integer.
Please use position underscore ticket – once again all in capital letters – afterwards we want to calculate the current stop loss that is done by using position get double and position underscore sl – also in capital letters – and if the current stop loss is above the stop loss that we want to have we use trade dot position modify for the current position ticket to adjust the current stop loss by 10 points.
Finally, we need to close the loops and that’s about it.
Once again if this is too fast for you or if you don’t understand what the code does you maybe want to watch the other videos in this basic series or maybe even you want to take part in the premium course.
But for now please click on the compile button, you should not get any errors here and if this is the case please click on the little symbol here or press F4 to go back to Metatrader.
Inside of Metatrader I take an empty chart, right-click, select templates, save template and I save it as tester dot tpl, replace the current one because this is what we want to see in the backtest, we don’t need any Indicators.
When you are done please click on view, strategy tester or press ctrl and r, here we want to pick the new file simple sell trailing stop ex5, please mark the option for the visualization here and start your test.
And here is our expert advisor at work, we should be able to see the trailing stop moving down as the price goes down and in this video you have learned how to create a trailing stop for sell trades and you have coded it with a few lines of mql5 code.