Modifying Limit and stop order's SL and TP while still pending

 

totally stuck on this one.

scenario: EA for MT5

the EA is supposed to add SL and TP as soon as a new order is entered.

orders can be all possible types from market to stop/limit order.

IT#d working fine with the market orders but not at all with the limit and sop orders.

I permanently(since 2 days) get "Invalid request" or sl/tp erors. sl and tp calculation is seemingly correct as it works for the

market orders and when I log the actual values SL and TP would sit exactly where expectd, soit must be some issue with the request I'm sending.

I already tried the following:

    // trans is of type MqlTradeTransaction and is passed by the EA's OnTradeTransaction() method
    orderTicket = trans.order;
    if (OrderSelect(orderTicket) && tradeUtils.IsPendingOrder((ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE)) &&
        OrderGetString(ORDER_SYMBOL)==Symbol() && OrderGetDouble(ORDER_SL)==0) {
          // this prints correctly the ticket id of the limit/stop order
  Comment("next ticket:  "  +OrderGetInteger(ORDER_TICKET));
      ulong ticket = OrderGetInteger(ORDER_TICKET);
      string ticketAsString = IntegerToString(ticket);

      // get stuff
      double volume = OrderGetDouble(ORDER_VOLUME_INITIAL);
      double orderOpenPrice = NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), Digits());
      ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
      ENUM_ORDER_TYPE_TIME orderTypeTime = (ENUM_ORDER_TYPE_TIME)OrderGetInteger(ORDER_TYPE_TIME);
      double orderPriceStopLimit = OrderGetDouble(ORDER_PRICE_STOPLIMIT);
      double sl=0, tp=0;
      // custom sl/tp calculation
      tradeUtils.CalculateSLTP(orderOpenPrice, volume, 
            Symbol(), calcType, sl, tp, tpSlFactor, balancePercentage, slPoints, riskLineName, fixedRiskAmount);
      if (sl!=0) {
        // order type checking
        if( tradeUtils.WhatOrderType(orderType)==ORDER_TYPE_BUY) {
          sl = NormalizeDouble(orderOpenPrice - sl, Digits());
          tp = NormalizeDouble(orderOpenPrice + tp, Digits());
        }
        if (tradeUtils.WhatOrderType(orderType)==ORDER_TYPE_SELL) {
          sl = NormalizeDouble(orderOpenPrice + sl, Digits());
          tp = NormalizeDouble(orderOpenPrice - tp, Digits());
        }
        ticketsProcessedSuccessfully += ticketAsString + ";";
        ResetLastError();
        // mmodify
        if (trade.OrderModify(ticket,
          orderOpenPrice,
          sl,
          tp,
          orderTypeTime,
          0,
          0 //orderPriceStopLimit 
          )) {
            return;
        } else {
            // I always end up here
            badSLTPOrders += ticketAsString + ";";
            errorTicket = ticketAsString;
            errorTitle = "Invalid SL and/or TP ";
            errorMsg = "Ticket " + ticketAsString +
                        "\n@ " +DoubleToString(posi.PriceOpen(), symb.Digits()) + 
                        "\nSL "+DoubleToString(sl, symb.Digits()) + 
                        "\nTP "+DoubleToString(tp, symb.Digits()) + "\n\n";
            errorUtils.BuildErrorMessage(errorMsg, errorTitle, calcType);             
          
        }

the error shows following values for a sell limit order which are correct and make sense but still  get the error

Ask: 48358.12

sell limit price: 101001.13

SL: 120060.03

TP: 628833,33

I entered the values manually and they are accepted, so it's not a matter of wrong  SL/TP calculation, neither rounding issues.

I treid as well with order send and Request and response with the same values .. but still no success


         request.action = TRADE_ACTION_MODIFY;                           
         request.order = ticket;    // as above                        
         request.symbol = Symbol();                                   
         request.deviation = 5;
         request.tp = tp; // value as above
         request.sl = sl; // value as above
         request.price = OrderGetDouble(ORDER_PRICE_OPEN); // -> tried as well OrderGetDouble(ORDER_PRICE_STOPLIMIT)
         if(!OrderSend(request,result)) {
           // ...
         }                                            

This is all according to documentationand I'm totally stuck.

Can anybody help, please?

Thanks in advance