Problem with trailing stop

 

Hi,

I've this error

2023.09.24 08:02:56.564 2021.07.19 10:05:54   take profit triggered #39 sell 0.01 GBPUSD 1.37469 sl: 1.37339 tp: 1.37269 [#40 buy 0.01 GBPUSD at 1.37269]

2023.09.24 08:02:56.564 2021.07.19 10:05:54   deal #27 buy 0.01 GBPUSD at 1.37269 done (based on order #40)

2023.09.24 08:02:56.564 2021.07.19 10:05:54   deal performed [#27 buy 0.01 GBPUSD at 1.37269]

2023.09.24 08:02:56.564 2021.07.19 10:05:54   order performed buy 0.01 at 1.37269 [#40 buy 0.01 GBPUSD at 1.37269]

2023.09.24 08:02:56.564 2021.07.19 10:05:55   failed modify #0 buy 0  sl: 0.00000, tp: 0.00000 -> sl: 1.37332, tp: 1.37269 [Invalid parameters]

2023.09.24 08:02:56.564 2021.07.19 10:05:55   CTrade::OrderSend: modify position #39 GBPUSD (sl: 1.37332, tp: 1.37269) [unknown retcode 3]


I believe that the order is closed and the EA is still trying to modify the SL and TP

   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if(position.SelectByIndex(i) &&
         position.Symbol() == _Symbol &&
         position.Magic() == MagicNum)
         {
            if(position.PositionType() == POSITION_TYPE_BUY)
            {
               //Trail Profit
               if(position.PriceCurrent() - position.PriceOpen() >= TrailStart * adjustpt &&
                  position.PriceCurrent() - position.StopLoss() >= (TrailSize + 1) * adjustpt &&
                  position.PriceCurrent() - position.StopLoss() >= (TrailSize + TrailStep) * adjustpt)
                  {
                     trade.PositionModify(position.Ticket(), position.PriceCurrent() - (TrailSize * adjustpt), position.TakeProfit());
                  }
            }

            if(position.PositionType() == POSITION_TYPE_SELL)
            {
               //Trail Profit
               if(position.PriceOpen() - position.PriceCurrent() >= TrailStart * adjustpt &&
                  position.StopLoss() - position.PriceCurrent() >= (TrailSize + 1) * adjustpt &&
                  position.StopLoss() - position.PriceCurrent() >= (TrailSize + TrailStep) * adjustpt)
                  {
                     trade.PositionModify(position.Ticket(), position.PriceCurrent() + (TrailSize * adjustpt), position.TakeProfit());
                  }
            }
         }
   }

How do I resolve this error?

 
2023.10.02 09:52:46.868 Trades '7735881': failed cancel order #340847473 buy 0.01 GBPUSD at 1.21632 [Invalid request]


I think the ticket was closed by trailing stop and the code is still trying to modify the ticket

Reason: