How to create Trailing Stoploss in Expert Advisor in MT5

Sunil Kumar Yadav  

What is a Trailing Stop Loss ?


A trailing stop is a modification of a typical stop order that can be set at a defined percentage or dollar amount away from a security's current market price. For a long position, an investor places a trailing stop loss below the current market price. For a short position, an investor places the trailing stop above the current market price.


I have search online in almost every possible ways to find the code of trailing stop loss but I got nothing. So after making lots of effort and hard work I finally crack it. Now i am going to share with you all guys so that you also can use it. If you are reading this article that means you already knows how trailing works, so I am not going to explain its working.


I have created a trailing function in which I am calculating point and digit before using it because some times it doesn't work with JPY pairs, so I have calculate them separately and use variable later.


double TrailingStop  = 250; // Setting Traling to be triger at 25 pips.

int OnInit()
  {
   Trade.SetExpertMagicNumber(444444);
   return(INIT_SUCCEEDED);
  }

void Trailing()
{
      if(PositionGetInteger(POSITION_MAGIC) == 444444)//adding magic number, if you want you can delete this if you don't want to use magic number
      {
      
      for(int i=PositionsTotal()-1; i>=0; i--)
      {
            string symbol = PositionGetSymbol(i);
            ulong PositionTicket = PositionGetTicket(i);   
            long trade_type = PositionGetInteger(POSITION_TYPE);
            
            double POINT  =       SymbolInfoDouble(  symbol, SYMBOL_POINT  );
            int    DIGIT  =       (int) SymbolInfoInteger( symbol, SYMBOL_DIGITS );
   
            
            if(trade_type == 0)
            {    
               double Bid = NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),DIGIT);
                                  
               if(Bid-PositionGetDouble(POSITION_PRICE_OPEN) > NormalizeDouble(POINT * TrailingStop,DIGIT))
                 {
                  if(PositionGetDouble(POSITION_SL) < NormalizeDouble(Bid - POINT * TrailingStop,DIGIT))
                    {
                     Trade.PositionModify(PositionTicket,NormalizeDouble(Bid - POINT * TrailingStop,DIGIT),PositionGetDouble(POSITION_TP));
                    }
                 }
            
            }
            
            if(trade_type == 1)
            {  
               double Ask = NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),DIGIT);
               
               if((PositionGetDouble(POSITION_PRICE_OPEN) - Ask) > NormalizeDouble( POINT * TrailingStop,DIGIT))
                 {
                  if((PositionGetDouble(POSITION_SL) > NormalizeDouble(Ask + POINT * TrailingStop,DIGIT)) || (PositionGetDouble(POSITION_SL)==0))
                    {
                     Trade.PositionModify(PositionTicket,NormalizeDouble(Ask + POINT * TrailingStop,DIGIT),PositionGetDouble(POSITION_TP));
                    }
                 }
            
            }
      }

      }      
     
}
Reason: