Error 4108 appears after code was working fine before.

 

I have been running the EA and did not notice the 4108 error before - but mow the code refuses to close my pending orders and give 4108 - invalid ticket errors.

Here is t

//Close Pending Orders when no trades open 

   Count = 0;
   
   if(Sell == 0)
   {

   for(Count = OrdersTotal()-1; Count >= 0; Count--)
     {
      if(OrderSelect(Count, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())

        {
         if(OrderType() == OP_SELLLIMIT)
         OrderDelete(OrderTicket(),clrNONE);
         }
      } 
   } 
   if(Buy == 0)
   {

   for(Count = OrdersTotal()-1; Count >= 0; Count--)
     {
      if(OrderSelect(Count, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())

        {
         if(OrderType()== OP_BUYLIMIT)
         OrderDelete(OrderTicket(),clrNONE);
         }
      } 
   }           
           


he code. See if you can find the mistake please!

 
No problem with your posted code; your problem is elsewhere.
 

Thank you so much W.H. Roeder! 

I have discovered that the error is caused by this folllowing routine (which I have posted before and then made some changes):

(I cannot get my head around how the sequence in this code would actually be executed. So it may be that the code does not make clear which tickets should be closed??) 

Please help!!

    Count = 0;

         for(Count = OrdersTotal()-1; Count >= 0; Count--)
           {
             if(OrderSelect(Count, SELECT_BY_POS,MODE_TRADES)
             && OrderMagicNumber() == MagicNumber
             && OrderSymbol() == Symbol())
               {
               if(OrderType() == OP_BUY)
               BuyProfit += OrderProfit() +  OrderSwap() + OrderCommission();
               }
                          
            if(BuyProfit >= ProfitDollarValue || BuyProfit <= -LossDollarValue)
             
            Result = OrderClose(OrderTicket(), OrderLots(), Bid, UseSlippage, Blue);
            if(Result = -1)
                {
                  ErrorCode = GetLastError();
                  ErrDesc = ErrorDescription(ErrorCode);

                  ErrAlert = StringConcatenate("Close Profit Buy Orders - Error ",ErrorCode,": ",ErrDesc);
                  Alert(ErrAlert);
                }               
             }
 
Finish your loop. Then check BuyProfit.