1. Introduction to Creating a Buy Trailing Stop Expert Advisor in MQL5 (00:00 – 00:14)
    • Introduction to the tutorial on creating a trailing stop loss for buy positions using MQL5.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:14 – 00:35)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleBuyTrailingStop”.
  3. Setting Up the Code Structure and Including Trade.mqh (00:35 – 00:42)
    • Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
  4. Creating an Instance of CTrade and Getting the Ask Price (00:42 – 01:06)
    • Creating an instance of the CTrade class and using ‘SymbolInfoDouble’ to get the Ask price.
  5. Opening a Test Buy Position (01:06 – 01:42)
    • Opening a test buy position if there are no open positions using ‘trade.Buy’.
  6. Checking and Implementing the Trailing Stop for Buy Position (01:42 – 02:07)
    • Implementing the trailing stop logic for the buy position based on the Ask price.
  7. Creating the Check Trailing Stop Function (02:07 – 03:07)
    • Defining the function to check and adjust the trailing stop for each position.
  8. Modifying Positions Based on Trailing Stop Conditions (03:07 – 03:45)
    • Adjusting the stop loss of positions based on the trailing stop conditions.
  9. Compilation and Testing in MetaTrader (03:45 – 04:19)
    • Compiling the code and instructions on testing the Expert Advisor in MetaTrader.
  10. Setting Up the Tester Template and Running the Strategy Tester (04:19 – 04:56)
  • Saving the tester template and running the Expert Advisor in the Strategy Tester.
  1. Observing the Trailing Stop in Action (04:56 – 05:04)
  • Watching the trailing stop function in action during the test.


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.