Error 136 : Off quotes with Orders and Trailing

 

Hello,


I am frequently getting Error 136, specially during news hours. I searched and founded this happens due to Price change faster than EA placing trade. In this forum i saw some post where peoples saying using of  RefreshRates(); will resolve the error. 


I changed to code with RefreshRates();

//Direct Orders

if(Trade_Mode_Status == "Active")
     {
      RefreshRates();
      double TakeProfit_BuyEntry = (TP != 0) ? (Ask + TP * Pips) : 0;
      double TakeProfit_SellEntry = (TP != 0) ? (Bid - TP * Pips) : 0;
      double StopLoss_BuyEntry = (SL != 0) ? (Ask - SL * Pips) : 0;
      double StopLoss_SellEntry = (SL != 0) ? (Bid + SL * Pips) : 0;
      if((!Close_Buy_Limit) && Buy_Limit && trades_count(BuyLimitMagic) == 0 && istochastic_16 > up_level)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_BUY, Lot_Size, Ask, Slippage, StopLoss_BuyEntry, TakeProfit_BuyEntry, (BuyLimitTradeComment + trades_count(BuyLimitMagic)), BuyLimitMagic, 0, White);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Buy Limit Order Error Code : " + GetLastError());
        }
      if((!Close_Sell_Limit) && Sell_Limit && trades_count(SellLimitMagic) == 0 && istochastic_16 < lo_level)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, Slippage, StopLoss_SellEntry, TakeProfit_SellEntry, (SellLimitTradeComment + trades_count(SellLimitMagic)), SellLimitMagic, 0, Aqua);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Sell Limit Order Error Code : " + GetLastError());
        }

      if((!Close_Buy_Stop) && Buy_Stop && trades_count(BuyStopMagic) == 0 && istochastic_16 > up_level)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_BUY, Lot_Size, Ask, Slippage, StopLoss_BuyEntry, TakeProfit_BuyEntry, (BuyStopTradeComment + trades_count(BuyStopMagic)), BuyStopMagic, 0, Blue);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Buy Stop Order Error Code : " + GetLastError());
        }
      if((!Close_Sell_Stop) && Sell_Stop && trades_count(SellStopMagic) == 0 && istochastic_16 < lo_level)
        {
         ResetLastError();
         gTicket = OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, Slippage, StopLoss_SellEntry, TakeProfit_SellEntry, (SellStopTradeComment + trades_count(SellStopMagic)), SellStopMagic, 0, Red);
         if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
            Print("Initial Sell Stop Order Error Code : " + GetLastError());
        }

     }

//Pending Orders 

void BuyLimitPendingOrder()
  {
   int cmd_0;
   double order_open_price_4;
   double order_lots_12;
   double price_24;
   double Ld_32;

   if(BuyLimitCommentCheck == "False")
     {
      if(trades_count(BuyLimitMagic) > 0 && trades_count(BuyLimitMagic) < Level_Max)
        {
         for(int pos_20 = 0; pos_20 < OrdersTotal(); pos_20++)
           {
            OrderSelect(pos_20, SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol() != Symbol() || OrderMagicNumber() != BuyLimitMagic)
               continue;
            cmd_0 = OrderType();
            RefreshRates();
            order_open_price_4 = OrderOpenPrice();
            order_lots_12 = OrderLots();
           }
         price_24 = order_open_price_4 - Range * Pips;
         Ld_32 = order_open_price_4 + Range * Pips;
         if(cmd_0 == OP_BUY && trade_count_ordertype(OP_BUYLIMIT, BuyLimitMagic) == 0)
           {
            double TakeProfit_BuyLimitPendingOrder = (TP != 0) ? (price_24 + TP * Pips) : 0;
            double StopLoss_BuyLimitPendingOrder = (SL != 0) ? (price_24 - SL * Pips) : 0;
            double BuyLimitLot = (Increase_Type == Multiplication) ?  NormalizeDouble(order_lots_12 * DiMarti, LotDigit) : NormalizeDouble(order_lots_12 + DiMarti, LotDigit);
            ResetLastError();
            gTicket = OrderSend(Symbol(), OP_BUYLIMIT, BuyLimitLot, price_24, Slippage, StopLoss_BuyLimitPendingOrder, TakeProfit_BuyLimitPendingOrder, (BuyLimitTradeComment + trades_count(BuyLimitMagic)), BuyLimitMagic, 0, White);
            if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
               Print(__FUNCTION__ + " Error Code : " + GetLastError());
           }
        }
     }
  }

//Trailing Stop

void TrailingStopLoss(int TrailingStopLoss_magic)
  {
   double stopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         double TrailingStopLoss_entryPrice = OrderOpenPrice();
         double stopLoss = OrderStopLoss();
         double stopAdjustment = stopLevel > 0 ? stopLevel * Point : 0.0;

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic)
           {
            if(OrderType() == OP_BUY)
              {
               if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * Pips) > Pips * Trailing_Gap + stopAdjustment)
                 {
                 RefreshRates();
                  double TrailingStopLoss_Buy_newStopLoss = NormalizeDouble(Bid - Trailing_TP * Pips - stopAdjustment, Digits);
                  if(stopLoss < TrailingStopLoss_Buy_newStopLoss || stopLoss == 0.0)
                    {
                     ResetLastError();
                     OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, TrailingStopLoss_Buy_newStopLoss, OrderTakeProfit(), 0, clrNONE);
                     if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
                        Print(__FUNCTION__ + " => Buy Trailing Error Code : " + GetLastError());
                    }
                 }
              }
            else
               if(OrderType() == OP_SELL)
                 {
                  if((TrailingStopLoss_entryPrice - Trailing_TP * Pips) - Ask > Pips * Trailing_Gap + stopAdjustment)
                    {
                     RefreshRates();
                     double TrailingStopLoss_Sell_newStopLoss = NormalizeDouble(Ask + Trailing_TP * Pips + stopAdjustment, Digits);
                     if(stopLoss > TrailingStopLoss_Sell_newStopLoss || stopLoss == 0.0)
                       {
                        ResetLastError();
                        OrderModify(OrderTicket(), TrailingStopLoss_entryPrice - Trailing_TP * Pips, TrailingStopLoss_Sell_newStopLoss, OrderTakeProfit(), 0, clrNONE);
                        if(_LastError != ERR_NO_ERROR && _LastError != ERR_NO_MQLERROR)
                           Print(__FUNCTION__ + " => Sell Trailing Error Code : " + GetLastError());
                       }
                    }
                 }
           }
        }
     }
  }

I am doing correct?. And RefreshRates(); is only way to resolve Error 136 or i have to work in other sect of code too?

 
anuj71:

Hello,


I am frequently getting Error 136, specially during news hours. I searched and founded this happens due to Price change faster than EA placing trade. In this forum i saw some post where peoples saying using of  RefreshRates(); will resolve the error. 


I changed to code with RefreshRates();

I am doing correct?. And RefreshRates(); is only way to resolve Error 136 or i have to work in other sect of code too?

1. Check GetLastError.
2. If it is 136 call RefreshRate and do everything from start.
Or you can just ignore the error untill it can do it in upcoming ticks.