TRADE_ACTION_SLT error -> Invalid request

 

My Trailing SL function no longer works. Maybe it is because of my NormalizeDouble routine.

Normalize Double:

//NormalizeDdouble(double,par);
double ND(double val, string par) {
//   return(NormalizeDouble(val,StringLen(SymbolInfoDouble(par,SYMBOL_POINT))));
   return(NormalizeDouble(val,SymbolInfoInteger(par,SYMBOL_DIGITS)));
}

Why I think this might be the issue:

halfway in my Trailing SL funcion I have this debug monitor:

if(par_temp==Symbol()) printf("ENTROU NA SL -> "+ND(New_TSL,par_temp);

Which returns:

2022.07.25 12:04:14.073 NMC_Proj (EURAUD,M15) ENTROU NA SL -> 1.4728000164031982

Where I expected it to say  ENTROU NA SL -> 1.47280

My Trailing stop is not being moved and I get the following error:

2022.07.25 12:05:29.553 Trades '5004787288': failed modify #0 buy 0  sl: 0.00000, tp: 0.00000 -> sl: 136.32700, tp: 136.80100 [Invalid request]


My TSL request was not changed and looks like this:


      MqlTradeRequest request= {};
      MqlTradeResult result= {};
      ZeroMemory(request);
      ZeroMemory(result);
      
      request.action  =TRADE_ACTION_SLTP; // type of trade operation
      request.position=ticket;   // ticket of the position
      request.symbol=par_temp; // symbol
      request.sl     =ND(New_TSL,par_temp);                // Stop Loss of the position
      request.tp     =PositionGetDouble(POSITION_TP);                // Take Profit of the position
      request.magic  =PositionGetInteger(POSITION_MAGIC);         // MagicNumber of the position
      if(request.sl>0) OrderSend(request,result);



I trying changing the ND funcion around, and I tried using the NormalizeDouble(New_TSL,5) and neither worked.

What else can I look at? I am picking up the position tickets correctly but somewhere along the way my double format doesn't get normalized.



Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum (2013)

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
              On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

    And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

  4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
              Trailing Bar Entry EA - MQL4 programming forum (2013)
              Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

  5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
              (MT4 2013)) (MT5 2022))

  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum (2017)
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

  7. Prices you get from the terminal are already correct (normalized).

  8. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)

 

Thanks, this will give me plenty to work on.

Thanks for teaching us how to fish, it is way better than giving us free fish!

 

It works after the following changes:

Normalize function:

//NormalizeDdouble(double,par);
double ND(double value, string par) {
   double TickSize=SymbolInfoDouble(par,SYMBOL_TRADE_TICK_SIZE);
   return(MathRound(value/TickSize)*TickSize);
}


on the Trailing function, replaced the request method from:

      MqlTradeRequest request= {};
      MqlTradeResult result= {};
      ZeroMemory(request);
      ZeroMemory(result);
      
      request.action  =TRADE_ACTION_SLTP; // type of trade operation
      request.position=ticket;   // ticket of the position
      request.symbol=par_temp; // symbol
      request.sl     =ND(New_TSL,par_temp);                // Stop Loss of the position
      request.tp     =PositionGetDouble(POSITION_TP);                // Take Profit of the position
      request.magic  =PositionGetInteger(POSITION_MAGIC);         // MagicNumber of the position
      if(request.sl>0) OrderSend(request,result); // Original SL moving method

to:

      m_trade.PositionModify(m_position.Ticket(), ND(New_TSL,par_temp), PositionGetDouble(POSITION_TP)); // New SL moving method

Thank you for your input, @William Roeder