Problem with trailing stoploss.

 

I wrote a function for a trailing stoploss and the logid seems air tight. however while testing, the stoploss is stuck an interval away from the current price no matter which way it moves. what could be the problem?

heres the code (MQL5)


Help would be much appreciated..

void SLmodify()
   {
//      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--)
         {
            ulong PositionTicket = PositionGetTicket(i);   
            long trade_type = PositionGetInteger(POSITION_TYPE);            
            
            if (trade_type == 0)
               {
               
               double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
               
               if (ask - meanEntryPriceBuy() >  (2*Grid*_Point))
                  {       
                     trade.PositionModify(PositionTicket,NormalizeDouble(ask - Grid*_Point,_Digits),PositionGetDouble(POSITION_TP));
                  }
                  
                  if (PositionGetDouble(POSITION_SL) < NormalizeDouble(ask - Grid*_Point, _Digits))
                     {
                        trade.PositionModify(PositionTicket,NormalizeDouble(ask - Grid*_Point,_Digits),PositionGetDouble(POSITION_TP));
                     }   
               }
            if (trade_type == 1)
               {
      
               double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
               
               if ( meanEntryPriceSell() - bid > (2*Grid*_Point))
                  {
                     trade.PositionModify(PositionTicket,NormalizeDouble(bid + Grid*_Point, _Digits) ,PositionGetDouble(POSITION_TP));
                  }
                  
                  if (PositionGetDouble(POSITION_SL) > NormalizeDouble(bid + Grid*_Point, _Digits))
                     {                 
                        trade.PositionModify(PositionTicket,NormalizeDouble(bid + Grid*_Point, _Digits) ,PositionGetDouble(POSITION_TP));

                     }
                  
               }
         }
   }
 
You should search CodeBase. Study different peoples' codes and you will be able to fix it.
Reason: