failing to modify stop order

 

Hello board,


I'm troubleshooting a problem for some days now, but did not found a solution for it.


My EA sometimes can't perform a stop loss modification order giving this error:


2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   order performed sell 1 at 104975 [#94 sell 1 WINQ22 at 104975]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   CTrade::OrderSend: exchange sell 1.00 WINQ22 [done]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   position modified [#94 sell 1 WINQ22 104975 sl: 105065 tp: 104885]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   CTrade::OrderSend: modify position #94 WINQ22 (sl: 105065, tp: 104885) [done]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   failed modify #94 sell 1 WINQ22 sl: 105065, tp: 104885 -> sl: 104975, tp: 104885 [Invalid stops]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   CTrade::OrderSend: modify position #94 WINQ22 (sl: 104975, tp: 104885) [invalid stops]
2022.06.22 07:53:21.837 Core 1  2022.06.15 14:58:00   failed modify #94 sell 1 WINQ22 sl: 105065, tp: 104885 -> sl: 50, tp: 104885 [Invalid stops]


my code in MQL5 is:


void SLTP()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      P.SelectByIndex(i);
      if(P.Symbol() == MySymbol)
        {
         if(P.PositionType()==POSITION_TYPE_BUY)
           {
            double sl = 0;
            double tp = 0;
            if(SL > 0)
               sl = P.PriceOpen()-SL*MyPoint;
            if(TP > 0)
               tp = P.PriceOpen()+TP*MyPoint;
            if(P.StopLoss() > 0)
               sl = P.StopLoss();
            sl = NormalizeDouble(sl, MyDigits);
            tp = NormalizeDouble(tp, MyDigits);
            if(tp != P.TakeProfit() || sl != P.StopLoss())
               T.PositionModify(P.Ticket(), sl, tp);
           }
         if(P.PositionType()==POSITION_TYPE_SELL)
           {
            double sl = 0;
            double tp = 0;
            if(SL > 0)
               sl = P.PriceOpen()+SL*MyPoint;
            if(TP > 0)
               tp = P.PriceOpen()-TP*MyPoint;
            if(P.StopLoss() > 0)
               sl = P.StopLoss();
            sl = NormalizeDouble(sl, MyDigits);
            tp = NormalizeDouble(tp, MyDigits);
            if(tp != P.TakeProfit() || sl != P.StopLoss())
               T.PositionModify(P.Ticket(), sl, tp);
           }
        }
     }
  }
 
Stop Loss and Take Profit must be calculated based on CPositionInfo -> PriceCurrent
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PriceCurrent
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PriceCurrent
  • www.mql5.com
PriceCurrent - CPositionInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
            if(SL > 0)
               sl = P.PriceOpen()+SL*MyPoint;
            if(TP > 0)
               tp = P.PriceOpen()-TP*MyPoint;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: