Sort orders based on OrderOpenTime

 

Hi,

  At times, when I try to close multiple orders, I am ending up with "prohibited by FIFO" error. I would like to know is there a simple way to sort the orders by OrderOpenTime and NOT the ticket number?

Added the code logic in the comment

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
if(OrdersTotal()>0)
   {
      // close opened orders first
      total = OrdersTotal();
      for (cnt = 0; cnt < total ; cnt++)
      {
         if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol() == symb) 
         {
           int type   = OrderType();  
          if(type == OP_BUY)
          {
              OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
              cnt = -1;               
           }                    
         }
      }
   }
 
Sergey Golubev:
Thank you. Corrected the post
 
dhirajm: I am ending up with "prohibited by FIFO" error.
  1. 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 (non-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 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - 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, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  2.           if(type == OP_BUY){ … MarketInfo(OrderSymbol(), MODE_BID)
    You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect. № 1.3
 
   int as=0;
   int ordersArray[][2];
   ArrayResize(ordersArray,as);
   for(int x=OrdersTotal()-1; x>=0; x--)
     {
      if(OrderSelect(x,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         as++;
         ArrayResize(ordersArray,as);
         ordersArray[as-1][0]=(int)OrderOpenTime();
         ordersArray[as-1][1]=(int)OrderTicket();
        }
     }
   ArraySort(ordersArray);

You now have the open orders in the array ordered by the open time.

 
Keith Watford:

You now have the open orders in the array ordered by the open time.

Position ticket numbers always increment positively with each new position. Two positions can have the same timestamp (think two positions entered milliseconds apart) but no two can share a ticket number. Knowing this can simplify your code because you don't need to do anything fancy. All you have to do it fill an array with ticket numbers and sort. 

void OnStart() {
   int sorted_orders[];
   int size = fifoTickets(sorted_orders);
   for (int i=0; i<size; i++) {
      if (OrderSelect(sorted_orders[i], SELECT_BY_TICKET)){
         printf("%s - %s", OrderSymbol(), string(OrderOpenTime()));
      }
   }
      
}
//+------------------------------------------------------------------+


int fifoTickets(int &sorted_order_tickets[]) {
   int size = ArrayResize(sorted_order_tickets, OrdersTotal());
   for (int i=0; i<size; i++)
      if (OrderSelect(i, SELECT_BY_POS))
         sorted_order_tickets[i] = OrderTicket();
   return ArraySort(sorted_order_tickets);
}
 

An sort by open time or order number takes the same, lower order number is lower open time

 
nicholi shen:

Position ticket numbers always increment positively with each new position. Two positions can have the same timestamp (think two positions entered milliseconds apart) but no two can share a ticket number. Knowing this can simplify your code because you don't need to do anything fancy. All you have to do it fill an array with ticket numbers and sort. 

I'm not sure if you have a point or not, if more than 1 trades have the same timestamp, does it matter which one is closed first?

When using pending orders, they are not guaranteed to trigger in the same order as they are placed, so the ticket numbers may not be in chronological order.

Reason: