OrderModify Error 4108 from Trailing Stop + Scaling-In Trade System

 

Hi coders,

I need help on fixing OrderModify Error 4108.

I am trying to develop EA that buys incrementally (Scaling In) with trailing stop on each order. Error happens when particular order hit the stoploss and later EA tries to modify the same order for trailing stop but, of course, EA cannot find that order and spits out OrderModify Error 4108. 

Here is my code:

//Trailing Stop
   for(int position=OrdersTotal()-1; position >= 0;position--)
     {
      if(!OrderSelect(position, SELECT_BY_POS,MODE_TRADES)) continue;
         
      if(OrderCloseTime() == 0 && OrderType() == OP_BUY && Bid-OrderOpenPrice()>TSPoint && OrderStopLoss()<Bid-TSPoint)
        {
         OrderModify(Ticket,OrderOpenPrice(),Bid-TSPoint,OrderTakeProfit(),0,Blue);
        };
     
      if(OrderCloseTime() == 0 && OrderType() == OP_SELL && OrderOpenPrice()-Ask>TSPoint && OrderStopLoss()>Ask+TSPoint)
        {
         OrderModify(Ticket,OrderOpenPrice(),Ask+TSPoint,OrderTakeProfit(),0,Blue);
        };  
     };

 //Close Buy Position Signal
   if(BuySignal)
     {
         if(OrdersTotal()<Scaling_Size) //Scaling_Size is set to 3
         {
            Ticket = OrderSend(_Symbol, OP_SELL, Lots, Bid, 0, Bid+SLPoint,0);
         }

Here is the excerpt of log:

2018.09.07 15:04:00.605 2016.01.15 20:00:00  EA USDJPY,H4: open #24 sell 0.33 USDJPY at 116.885 sl: 117.085 ok
2018.09.07 15:04:00.605 2016.01.16 00:00:00  Tester: stop loss #24 at 117.085 (117.014 / 117.094)
2018.09.07 15:04:00.605 2016.01.18 00:00:00  EA USDJPY,H4: unknown ticket 24 for OrderModify function
2018.09.07 15:04:00.605 2016.01.18 00:00:00  EA USDJPY,H4: OrderModify error 4108

If you could advice me how to avoid the error, I am very grateful. Thank you in advance!

 
 if(OrderCloseTime() == 0 && OrderType() == OP_BUY && Bid-OrderOpenPrice()>TSPoint && OrderStopLoss()<Bid-TSPoint)
        {
         OrderModify(Ticket,OrderOpenPrice(),Bid-TSPoint,OrderTakeProfit(),0,Blue);
        };

You should be modifying OrderTicket()

Although not causing a problem here, using unnecessary  ; are probably not good practice, especially if you place them after if()

 
Keith Watford:

You should be modifying OrderTicket()

Although not causing a problem here, using unnecessary  ; are probably not good practice, especially if you place them after if()

Awesome! OrderTicket() did the job. My EA is error-free now. Thank you! Also thanks for the coding tip. I will keep that in mind.