ATR Trailing not work in sell order

 

in this coding ATR Trailing for buy order is work but sell order not apply

help me to solve error

void TrailStops()
  {
   double ATR = iATR(NULL,0,14,0);
   double TrailingStop = ATR * 2 ;
   
   double   minstoplevel   =  MarketInfo(_Symbol,MODE_STOPLEVEL)*_Point;
   double   buyStopLoss    =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID)-minstoplevel-TrailingStop,_Digits);
   double   sellStopLoss   =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK)+minstoplevel+TrailingStop,_Digits);
   

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==ORDER_TYPE_BUY && buyStopLoss>OrderOpenPrice() && buyStopLoss>OrderStopLoss())
           {
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),buyStopLoss,OrderTakeProfit(),0,clrBlue))
              {
               Print("Buy OrderModify error ",GetLastError(), " SL ",OrderStopLoss()," New SL ", buyStopLoss," Ticket Number : ",OrderTicket());
              }
           }
         else
            if(OrderType()==ORDER_TYPE_SELL && sellStopLoss<OrderOpenPrice() && sellStopLoss<OrderStopLoss())
              {
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),sellStopLoss,OrderTakeProfit(),0,clrWhite))
                 {
                  Print("Sell OrderModify error ",GetLastError(), " SL ",OrderStopLoss()," New SL ", sellStopLoss," Ticket Number : ",OrderTicket());
                 }
              }
        }
     }
  }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Trade Orders in DOM - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Rahul Ramani:

  1.         if(OrderType()==ORDER_TYPE_BUY 

    There is no such type in MT4.

  2.    double   minstoplevel   =  MarketInfo(_Symbol,MODE_STOPLEVEL)*_Point;
    

    You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers, the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

    The checks a trading robot must pass before publication in the Market - MQL5 Articles (2016)

 

after change to OP_BUY and OP_SELL 

not work for sell


void TrailStops()
  {
   double ATR = iATR(NULL,0,14,0);
   double TrailingStop = ATR * 2 ;


   double   buyStopLoss    =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID)-TrailingStop,_Digits);
   double   sellStopLoss   =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK)+TrailingStop,_Digits);

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUY && buyStopLoss>OrderOpenPrice() && buyStopLoss>OrderStopLoss())
           {
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),buyStopLoss,OrderTakeProfit(),0,clrBlue))
              {
               Print("Buy OrderModify error ",GetLastError(), " SL ",OrderStopLoss()," New SL ", buyStopLoss," Ticket Number : ",OrderTicket());
              }
           }
         else
            if(OrderType()==OP_SELL && sellStopLoss<OrderOpenPrice() && sellStopLoss<OrderStopLoss())
              {
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),sellStopLoss,OrderTakeProfit(),0,clrWhite))
                 {
                  Print("Sell OrderModify error ",GetLastError(), " SL ",OrderStopLoss()," New SL ", sellStopLoss," Ticket Number : ",OrderTicket());
                 }
              }
        }
     }
  }
 
Rahul Ramani #:

after change to OP_BUY and OP_SELL 

not work for sell


Probably because the current SL is 0.

Reason: