Failed To Modify Order

 

Hello I am getting this error in validation pass, can someone help me out with this thing ? Attaching the code and the error screenshot. What should I add into this to avoid this error ?

int myOrderModifyRel(ENUM_ORDER_TYPE type, ulong ticket, double SL, double TP) //works for positions and orders, modify SL and TP (relative to open price), zero targets do not modify, ticket is irrelevant for open positions
  {
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) || !MQLInfoInteger(MQL_TRADE_ALLOWED)) return(-1);
   bool netting = AccountInfoInteger(ACCOUNT_MARGIN_MODE) != ACCOUNT_MARGIN_MODE_RETAIL_HEDGING;
   int retries = 0;
   int err = 0;
   SL = NormalizeDouble(SL, Digits());
   TP = NormalizeDouble(TP, Digits());
   if(SL < 0) SL = 0;
   if(TP < 0) TP = 0;
   //prepare to select order
   Sleep(10);
   if((type <= 1 && ((netting && !PositionSelect(Symbol())) || (!netting && !PositionSelectByTicket(ticket)))) || (type > 1 && !OrderSelect(ticket)))
     {
      err = GetLastError();
      myAlert("error", "PositionSelect / OrderSelect failed; error #"+IntegerToString(err));
      return(-1);
     }
   //ignore open positions other than "type"
   if (type <= 1 && PositionGetInteger(POSITION_TYPE) != type) return(0);
   //prepare to modify order, convert relative to absolute
   double openprice = (type <= 1) ? PositionGetDouble(POSITION_PRICE_OPEN) : OrderGetDouble(ORDER_PRICE_OPEN);
   if(((type <= 1) ? PositionGetInteger(POSITION_TYPE) : OrderGetInteger(ORDER_TYPE)) % 2 == 0) //buy
     {
      if(NormalizeDouble(SL, Digits()) != 0)
         SL = openprice - SL;
      if(NormalizeDouble(TP, Digits()) != 0)
         TP = openprice + TP;
     }
   else //sell
     {
      if(NormalizeDouble(SL, Digits()) != 0)
         SL = openprice + SL;
      if(NormalizeDouble(TP, Digits()) != 0)
         TP = openprice - TP;
     }
   //adjust targets for market order if too close to the market price
   double MinDistance = PriceTooClose * Point();
   MqlTick last_tick;
   SymbolInfoTick(Symbol(), last_tick);
   if(type == ORDER_TYPE_BUY)
     {
      if(NormalizeDouble(SL, Digits()) != 0 && last_tick.ask - SL < MinDistance)
         SL = last_tick.ask - MinDistance;
      if(NormalizeDouble(TP, Digits()) != 0 && TP - last_tick.ask < MinDistance)
         TP = last_tick.ask + MinDistance;
     }
   else if(type == ORDER_TYPE_SELL)
     {
      if(NormalizeDouble(SL, Digits()) != 0 && SL - last_tick.bid < MinDistance)
         SL = last_tick.bid + MinDistance;
      if(NormalizeDouble(TP, Digits()) != 0 && last_tick.bid - TP < MinDistance)
         TP = last_tick.bid - MinDistance;
     }
   double currentSL = (type <= 1) ? PositionGetDouble(POSITION_SL) : OrderGetDouble(ORDER_SL);
   double currentTP = (type <= 1) ? PositionGetDouble(POSITION_TP) : OrderGetDouble(ORDER_TP);
   if(NormalizeDouble(SL, Digits()) == 0) SL = currentSL; //not to modify
   if(NormalizeDouble(TP, Digits()) == 0) TP = currentTP; //not to modify
   if(NormalizeDouble(SL - currentSL, Digits()) == 0
   && NormalizeDouble(TP - currentTP, Digits()) == 0)
      return(0); //nothing to do
   MqlTradeRequest request;
   ZeroMemory(request);
   request.action = (type <= 1) ? TRADE_ACTION_SLTP : TRADE_ACTION_MODIFY;
   if (type > 1)
      request.order = ticket;
   else
      request.position = PositionGetInteger(POSITION_TICKET);
   request.symbol = Symbol();
   request.price = (type <= 1) ? PositionGetDouble(POSITION_PRICE_OPEN) : OrderGetDouble(ORDER_PRICE_OPEN);
   request.sl = NormalizeDouble(SL, Digits());
   request.tp = NormalizeDouble(TP, Digits());
   request.deviation = MaxSlippage_;
   MqlTradeResult result;
   ZeroMemory(result);
   while(!OrderSuccess(result.retcode) && retries < OrderRetry+1)
     {
      if(!OrderSend(request, result) || !OrderSuccess(result.retcode))
        {
         err = GetLastError();
         myAlert("print", "OrderModify error #"+IntegerToString(err));
         Sleep(OrderWait*1000);
        }
      retries++;
     }
   if(!OrderSuccess(result.retcode))
     {
      myAlert("error", "OrderModify failed "+IntegerToString(OrderRetry+1)+" times; error #"+IntegerToString(err));
      return(-1);
     }
   string alertstr = "Order modify: ticket="+IntegerToString(ticket);
   if(NormalizeDouble(SL, Digits()) != 0) alertstr = alertstr+" SL="+DoubleToString(SL);
   if(NormalizeDouble(TP, Digits()) != 0) alertstr = alertstr+" TP="+DoubleToString(TP);
   myAlert("modify", alertstr);
   return(0);
  }

Full error :

test on EURUSD,H1 (netting) strategy tester report 5 total trades test on XAUUSD,D1 (netting) 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] 2021.02.01 02:15:55 failed modify order #2 sell stop 0.2 XAUUSD at 1849.96 sl: 0.00 tp: 0.00 -> 1849.96, sl: 1884.96 tp: 0.00 [Modification failed due to order or position being close to market] strategy tester report 234 total trades

Files:
error.png  59 kb
 
Abhimanyu Hans:

Hello I am getting this error in validation pass, can someone help me out with this thing ? Attaching the code and the error screenshot. What should I add into this to avoid this error ?

Surprised over a month and no response from MQ5!!!

I have also started getting this error, on previously working well code. Seems MQL5 update has messed up somewhere something.

 

@Abhimanyu Hans and @Anil Varma, please do some research on the contract specifications called "Freeze Level" and "Stops Level".

Here is a starting point regarding how to implement the checks in your code ...

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.