CloseOrder error 4108 unknown ticket

 

Hi all

I am not sure but seems like the code is trying to OrderSelect an already closed order.
I am struggling with creating codes I started this simplest code from here so I can understand whats going on a little better.

Getting 4108 unknown ticket errors

See code below

//+------------------------------------------------------------------+
//|                                                    Dirty_Rat.mq4 |
//|                               Agent86's Dirty Rat learning Trade |
//|                                    
//+------------------------------------------------------------------+
#property copyright "Agent86"

//---- input parameters
extern double    TakeProfit=300.0;
extern double    Lots=0.1;
extern double    StopLoss=40.0;
//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; 
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 

   int ticket,i,total,result;  
     
   double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL

//I'm going to have to create some loop


   total  = OrdersTotal(); 
   if(total < 1){ //I'll change this later too so it will work with other EA's and other symbols
      if(faster > slower)
      OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green);
      }


for (i = OrdersTotal()-1; i >= 0; i--) 
//many have recommmended deincriment due to error 4108, and increment may miss orders 
//deincrement had no effect with error 4108
//for(i = 0; i < OrdersTotal(); i++) //many say this will miss orders.
//anyhow moving on
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //I'll add return codes later, it opens trades currently
      if(OrderType() == OP_BUY && Symbol() == OrderSymbol()){
         if(faster < slower)
         result = OrderClose( OrderTicket(), OrderLots(), Bid,3, Red);
         if(result == false){
            Print("Order", OrderTicket()," failed to close Error ",GetLastError());
            return(0);
            }
         }   
      }   
     
            


//+------------------------------------------------------------------+
 

Replace

for (i = OrdersTotal()-1; i >= 0; i--)

to

for (i = OrdersTotal()-1; i >= 0; i--) {

and count brackets

 
HO HO HO, thanks

Surprised it let me close the trades at all really.
I can't believe I missed this. NOOB Alert !

AND it only took me 2 weeks to figure this out - OUCH
OK I'll put my dunce cap on for a couple week now and go stand in the corner

Oh well, back to documents and back to the drawing board

Thanks again
 
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    ){
Reason: