Closing Position via FIFO in a fast moving market

 
I'm having trouble enforcing FIFO closing of positions in a fast moving market in MQL4.  Based on other forum questions I keep a class array of my open orders sorted by OrderOpenTime.  I use Limit Orders exclusively to open these positions.  The order in which these limit orders can be triggered cannot be guaranteed.  My problem is that in a fast moving market open orders can have the same time.  OrderTime is only on a seconds basis even though order management systems are operating in microseconds.  MQL4 doesn't have an OnTrade event so I've coded an event, but the event can't differentiate which limit order was executed first. I've coded a work-around where if I get a FIFO error the script tries the next open order if it has the same time.  This work-around is not elegant and consumes cycles.  Anyone have a more elegant solution to handle FIFO when open order times are the same?  Again, I use Limit orders that can be executed randomly.
 

void Close_AllOrders()
  {
//Close Orders according to FIFO Rule
bool Result_Close;

   for(i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            switch(OrderType())
              {
               case OP_BUY:
                  Result_Close=OrderClose(OrderTicket(),OrderLots(),Bid,0);
                  if(Result_Close) i--;
                  break;
               case OP_SELL:
                  Result_Close=OrderClose(OrderTicket(),OrderLots(),Ask,0);
                  if(Result_Close) i--;
                  break;
              }
           }
     }
/*-------------------------------------------------------------------------------------------------*/
  }

This what I use.

You may have to change it to suit your needs. 

 

Thanks for your response Donald but my situation is a bit more complex.  If there is a big gap up in the cross rate (due to a Fed statement, etc.) I can have 3 or 4 limit orders executed at the same price and time.  I can't embed a value in the Comment field either since the limit orders may not have been created sequentially.  I can code around this but it requires multiple array passes and array resizes which consumes cycles and is not desirable in a fast moving market.

Initial State (Cross Rate 1.0795): 

Limit 4 - 1.0815

Limit 3 - 1.0810

Limit 2 - 1.0805 

Limit 1 - 1.08  

State 2 (Cross Rate 1.080):

Limit 4 - 1.0815

Limit 3 - 1.0810

Limit 2 - 1.0805 

Open Order 1 - 1.080

State 3 (Cross Rate 1.0790 - TP Order 1):

Limit 4 - 1.0815

Limit 3 - 1.0810

Limit 2 - 1.0805 

Close Order 1 - 1.0790

Limit 5 - 1.08 (Replaces original Limit 1)

State 4 (Gaps up to 1.0812 & all open orders have same OpenTime):

Limit 4 - 1.0815

Open Order 3 - 1.08120

Open Order 2 - 1.08120

Open Order 5 - 1.08120

Programatically how do I determine FIFO?

 
  1. You already answered your own question, and provided a solution. You can't because the order opened when you have two identical prices means they open in the same second.
  2. Either make sure you don't have identical prices so the timestamps differ.
  3. Or, don't use pending orders. Humans can't watch the screen 24/7 so they use pending orders; EA's can, so no need for pending orders, have it wait until the market reaches the trigger price and open an order. Thus the order is ascending ticket numbers.
 
whroeder1:
whroeder1:
  1. You already answered your own question, and provided a solution. You can't because the order opened when you have two identical prices means they open in the same second.
  2. Either make sure you don't have identical prices so the timestamps differ.
  3. Or, don't use pending orders. Humans can't watch the screen 24/7 so they use pending orders; EA's can, so no need for pending orders, have it wait until the market reaches the trigger price and open an order. Thus the order is ascending ticket numbers.
Thanks whroeder.  Makes sense.  I'm trying to replicate my manual trading.  I figured since the limit orders reside server side there would be less latency, but this comes at a big expense with the lack of control.
  1. You already answered your own question, and provided a solution. You can't because the order opened when you have two identical prices means they open in the same second.
  2. Either make sure you don't have identical prices so the timestamps differ.
  3. Or, don't use pending orders. Humans can't watch the screen 24/7 so they use pending orders; EA's can, so no need for pending orders, have it wait until the market reaches the trigger price and open an order. Thus the order is ascending ticket number

mz1zcd:

  1. Thanks for your response Donald but my situation is a bit more complex.  If there is a big gap up in the cross rate (due to a Fed statement, etc.) I can have 3 or 4 limit orders executed at the same price and time.  I can't embed a value in the Comment field either since the limit orders may not have been created sequentially.  I can code around this but it requires multiple array passes and array resizes which consumes cycles and is not desirable in a fast moving market.

    Initial State (Cross Rate 1.0795): 

    Limit 4 - 1.0815

    Limit 3 - 1.0810

    Limit 2 - 1.0805 

    Limit 1 - 1.08  

    State 2 (Cross Rate 1.080):

    Limit 4 - 1.0815

    Limit 3 - 1.0810

    Limit 2 - 1.0805 

    Open Order 1 - 1.080

    State 3 (Cross Rate 1.0790 - TP Order 1):

    Limit 4 - 1.0815

    Limit 3 - 1.0810

    Limit 2 - 1.0805 

    Close Order 1 - 1.0790

    Limit 5 - 1.08 (Replaces original Limit 1)

    State 4 (Gaps up to 1.0812 & all open orders have same OpenTime):

    Limit 4 - 1.0815

    Open Order 3 - 1.08120

    Open Order 2 - 1.08120

    Open Order 5 - 1.08120

    Programatically how do I determine FIFO?