MT4 Order Pool for Open Orders

 

I hope you find this as amusing ..

If you are looping order pool for open orders, if you count down you are reading orders most recent to oldest?

Okay

If you want to close all orders opened most oldest to most recent you would be counting orders up?

Visually

void CloseAllOrders() 
 {
 bool result=false; int iRetries; 
 for(int OrdersOT=OrdersTotal()-1; OrdersOT>=0; OrdersOT--)
  {
  if(!OrderSelect(OrdersOT,SELECT_BY_POS,MODE_TRADES) || OrderMagicNumber()!=newMagic){continue;} 
  if(OrderType()==OP_SELL || OrderType()==OP_BUY )
   {
   result=false; iRetries=1;
   while( iRetries<=Retries && !result)
    {
    while(IsTradeContextBusy()){Sleep(10);} iRetries++; result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrNONE);
    }
   if(!result){Print("Failed To Close Order#"+IntegerToString(OrderTicket())+". Error: "+strError(GetLastError())+"." ); } 
   }
  if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT )
   {
   result=false; iRetries=1;
   while( iRetries<=Retries && !result)
    { 
    while(IsTradeContextBusy()){Sleep(10);} iRetries++; result=OrderDelete(OrderTicket());
    }
   if(!result){Print("Failed To Close Order#"+IntegerToString(OrderTicket())+". Error: "+strError(GetLastError())+"." ); } 
   }
  }
 }

This would need to be reversed to close oldest created orders first?

 
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 

Thank you for your help! I didn't think to take into perspective FIFO brokers but when are there going to be more US brokers?? lol

So for Non-FIFO I can simply loop OrderSelect(0,SELECT_BY_POS,MODE_TRADES) OrdersTotal()-1 number of times and delete/close all orders taken and arranged by MT4.

But for FIFO and Non-FIFO all orders will need to be stored and sorted for use such as for closing.

Reason: