ArraySort() my love!!!

 
Excuse my bad English.
I have a problem with ArraySort(). I used it to classify a basket of orders before closing. Rarely, but from time to time I take a "Invalid ticket for OrderClose function". If I do not class them, I is never this error. I cannot find or is my error!
If someone can tell me.

Thank you

void closeall(int clostyp)
  {
   int clostotal=-1;
   total=OrdersTotal();
   ArrayResize(clostab,total);
   disposerv("Close ");
   for(int i=total-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         {
         message =StringConcatenate(idordre,"Selection Fermeture Erreur: ",errordescript(GetLastError()));
         Print(message);
         Alert(message);
         if(mail) SendMail(message,"");
         break;
         }
      if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol() || OrderType()!=clostyp) continue;
      clostotal++;
      clostab[clostotal][0] = OrderProfit()+OrderCommission()+OrderSwap();//critére de tri!!!
      clostab[clostotal][1] = OrderTicket();
      clostab[clostotal][2] = OrderLots();
     }
  if(ArraySort(clostab,WHOLE_ARRAY,0,MODE_ASCEND)==false)//Grrrrrrrrrrrrr......
  //if(ArraySort(clostab)==false)
      {
      message =StringConcatenate(idordre,"Trie Fermeture Erreur: ",errordescript(GetLastError()));
      Print(message);
      Alert(message);
      if(mail) SendMail(message,"");
      }
   for(int j=0; j<=clostotal; j++)
     {
      for(int try=4; try>=0; try--)//tentes de fermer l'ordre 5 fois
        {
         Sleep(500);
         RefreshRates();
         if(clostyp==OP_BUY)
           {
            if(OrderClose(clostab[j][1],clostab[j][2],Bid,slippage,Blue)==true)break;
           }
         if(clostyp==OP_SELL)
           {
            if(OrderClose(clostab[j][1],clostab[j][2],Ask,slippage,Red)==true)break;
           }
         if(try==0)
           {
            message = StringConcatenate(idordre,"Fermeture ",clostab[j][1]," Erreur: ",errordescript(GetLastError()));
            Print(message);
            Alert(message);
            if(mail==true)SendMail(message,"");
           }
        }
     }
  }

 

 

 

 

 

 

 
stanislass: Rarely, but from time to time I take a "Invalid ticket for OrderClose function".
   ArrayResize(clostab,total);
:
      if(OrderMagicNumber()!=magic || OrderSymbol()!=Symbol() || OrderType()!=clostyp) continue;
      clostotal++;
      clostab[clostotal][0] = OrderProfit()+OrderCommission()+OrderSwap();//critére de tri!!!
      clostab[clostotal][1] = OrderTicket();
      clostab[clostotal][2] = OrderLots();
     }
  if(ArraySort(clostab,WHOLE_ARRAY,0,MODE_ASCEND)==false)//Grrrrrrrrrrrrr......
  1. You resize the array for the maximum (ALL orders)
  2. You only fill clostotal + 1 to the array. (use nOrders=0; ... clostab[nOrders]...; nOrders=1;)
  3. You sort ALL the array (use nOrders)
  4. Result, unfilled elements to the front. Invalid tickets.
 

Hello WHRoeder

 Thanks for your reply.

But... 

At the beginning "clostotal = - 1".

Before first use, "clostotal ++", therefore it well started was 0.

Or there is something that escapes me?

I said that the error is not systematic, it is even rare, but well presented.


 

Can it be that your order was closed by a stop-loss or a take-profit after your first OrdersTotal() and before try to close the orders?

Check the journal-tab for the time the order has been closed and compare time of close and the time of the error (Expert-tab).

 

Hello WHRoeder

 Thanks for your reply.

But... 

At the beginning "clostotal = - 1".

Before first use, "clostotal ++", therefore it well started was 0.

Or there is something that escapes me?

I said that the error is not systematic, it is even rare, but well presented.


 

Hello gooly

 After careful study of the maps, it seems that the error will occur when there are open at the same time buy and sell orders.

That is to say when total <>clostotal?

And ultimately, the EA recomence a cycle and closes orders without problem.

???

 
stanislass:

At the beginning "clostotal = - 1".

Before first use, "clostotal ++", therefore it well started was 0.

Or there is something that escapes me?

  1. ArraySort(clostab,WHOLE_ARRAY,0,MODE_ASCEND)
    You are sorting ALL elements in the array but you only have filled [0 .. clostotal]. Sort only what you HAVE (clostotal+1.)
  2. Your variable is clostotal, but it is not a total, it is one less (an index)
     clostotal =-1; // A total of minus one is meaningless.
     {
        clostotal++
        clostab[clostotal]=..
    I also suggested, you change to self-documenting variable names that mean what it says:
     nOrders = 0;            // Don't have any yet
     for(...){
        clostab[nOrders]=..  // Add one.
        ++nOrders;           // Count it.
    }

 

I am...

 

 Thanks for your reply.

Reason: