Does looping backwards solve problem of OrderDelete changing the list size?

 

A common problem: Find all open pending orders for an EA and delete them.

I am wondering if the following code, which loops backwards, solves the problem of OrderDelete which causes "holes" and/or changes the list size?

//Loop backwards, finding each order for this EA
for (int i = ordersTotal - 1; i >=0; i--)
{
//Handle any position indexing error (this could happen looping forwards, as well. just being cautious)

if (!OrderSelect (i, SELECT_BY_POS))
{
LogError("OrderSelect SELECT_BY_POS failed.");

break; //Go to the next iteration
}

//Is this a pending order for this EA?
if ( (OrderMagicNumber() == myMagicNumber) && (OrderType() != OP_BUY) && (OrderType() != OP_SELL) )
{

result = OrderDelete(ticket);

}

}

QUESTION: Will this code successfully delete all pending positions for this EA? Are there any gotchas?

 

This is more correct:

//Loop backwards, finding each order for this EA
for (int i = OrdersTotal() - 1; i >=0; i--)
   {
   //Handle any position indexing error (this could happen looping forwards, as well. just being cautious)
 
   if (!OrderSelect (i, SELECT_BY_POS))
      {
      LogError("OrderSelect SELECT_BY_POS failed.");
      break; //Go to the next iteration
      }
 
   //Is this a pending order for this EA?
   if ((OrderMagicNumber() == myMagicNumber) && (OrderType() != OP_BUY) && (OrderType() != OP_SELL) )
      {
      int ticket = OrderTicket();
      result = OrderDelete(ticket);
      if (!result) Print("Deleting tiket #",ticket, " is failed with error = ",GetLastError());
      }
 
   }
 
Rosh wrote >>

This is more correct:

So, is that a "Yes", it solves the "looping with delete" problem? Thank you for your comments.

 
chaffinsjc wrote >>

So, is that a "Yes", it solves the "looping with delete" problem? Thank you for your comments.

Also, when looping thru OrderSelect, I assume each OrderSelect goes across the internet -- that is, the data is not on the local machine.

QUESTION: Is there any issue with too much server access?

QUESTION: Is it "common" and "reasonable" to do such a loop every minute?

I reaize the ticket numbers etc. could be stored in a local memory array, but a local array would only be a partial truth -- only the server has the real data.

So, I would appreciate any comments on this coding issue. Thank you.

 

"Also, when looping thru OrderSelect, I assume each OrderSelect goes across the internet"

No, it uses local copy.

 
Rosh wrote >>

This is more correct:

Yes, your code is better. Knowing the ticket number was sorta implied and not explicitly annotated. Thank you for adding the code.

Now, in spite of responses by Rosh and Phy, THE FUNDAMENTAL QUESTION HAS NEVER BEEN ANSWERED!!!

===> Does looping backwards solve the "looping with delete" problem? <===

 
It solves the problems that you get when you loop forward and delete orders, yes.