MQL5 TUTORIAL BASICS – 26 HOW TO CODE A SIMPLE BUY TRAILING STOP

video

In this video we want to create a trailing stop loss like this one, the stop loss is trailing whenever the price is going up so let’s find out how to do that.
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/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleBuyTrailingStop”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and please also remove the two comment lines here.
We start by including “Trade.mqh”, this file will give us simplified trading functions, and we start by creating an instance of “CTrade” that will be called: “trade” and we are using it to open a test function in our case.
To do that we need to get the Ask price, that is done by using “SymbolInfoDouble”, for the current symbol (_Symbol), we use “SYMBOL_ASK” – all in capital letters – and with “NormalizeDouble” and “_Digits” we will get either 3 or 5 digits behind the dot depending on the currency pair we have on the chart.
So let’s find out if we have no open positions, that would be true when “PositionsTotal” equals 0 (zero) and in that case, we are going to open a buy position by using “trade.Buy”, we will buy 10 micro lot.
This is just a test position for this case because we need a position to find out if the trailing stop really works, you wouldn’t do that on a real account without any entry signal and finally, we want to check the trailing stop for our position.
In this case we are using the Ask price as a parameter and now we need to create the check trailing stop function (CheckTrailingStop), this one will take the Ask price and now we use it to calculate the stop loss, we want it to be 150 points below the current Ask price and as it is possible to have more than one position on the chart we use “PositionsTotal” and a “for” loop to go through all the open positions.
First we want to find out if the position belongs to the right symbol, so we use “PositionGetSymbol” for the current position number to get the symbol and if the symbol of the current currency pair (_Symbol) equals the symbol of the position (symbol), we have a position that needs to be modified.
And now we get the position ticket (PositionTicket) by using “PositionGetInteger”.
A position ticket will identify our position; it’s like a license plate.
We also want to calculate the current stop loss for the position that is done by using “PositionGetDouble”, “POSITION_SL” will give us the stop loss, if you mark that and press F1 you will see that it’s also possible to get the position volume (POSITION_VOLUME) or the take profit value (POSITION_TP) and other position properties but we just want to check if the current stop loss is below the desired stop loss and if this is the case we use “trade.PositionModify” for the position ticket (PositionTicket) that we have calculated here and now we rise the current stop loss by 10 points.
Finally we want to close the “if” loop, the “for” loop and the function and if you don’t understand what all the code does or if this was too fast for you maybe you want to watch the other videos in this basic series or maybe you want to even become a customer for the premium course.
Otherwise please click on the “Compile” button now, you shouldn’t get any errors here and in that case you can click on a little button here or press F4 to go back to Metatrader.
In Metatrader we use an empty chart without any Indicators, we click on: “Templates/ Save Template” and save it as: “tester.tpl” because this is the template that is going to be used in the backtest, so let’s replace the current one, click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleBuyTrailingStop.ex5” and start a test.
Here we are!
Our trailing stop is actually working and in this little video you have learned how to code a buy trailing stop and you have done it yourself with a few lines of MQL5 code.