FIFO compliance closing trades

To add comments, please log in or register
Point Zero
798
Point Zero  

Good afternoon,

I hope you are fine. I would like to ask wether closing all trades with the following function is FIFO compliant, just to check.

Is it enough to iterate trades backwards? For some reason it does not close all trades at the same time.

for(int i = 0; i <= OrdersTotal(); i++)

Thanks in advance!

bool CloseOrder(int Type)
{
        for(int i = OrdersTotal()-1; i >= 0; i--)
        {
                if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) &&
                   OrderSymbol() == Symbol() && 
                   OrderMagicNumber() == MagicNumber && 
                   Type == OrderType())
                { 
                 
              if(Type == OP_BUY || Type == OP_SELL)  
              {
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Gold))
            {
               Print(ShortName +" (OrderClose Error) "+ ErrorDescription(GetLastError()));
               return(false);
            }
         }
          if(Type == OP_BUYSTOP || Type == OP_SELLSTOP || Type == OP_BUYLIMIT || Type == OP_SELLLIMIT)  
              {
            if(!OrderDelete(OrderTicket(), Gold))
            {
               Print(ShortName +" (OrderDelete Error) "+ ErrorDescription(GetLastError()));
               return(false);
            }
         }
      }
   }
   return(true);
}
qjol
3196
qjol  
flaab:

Is it enough to iterate trades backwards?

no

For some reason it does not close all trades at the same time.

there is no way to close multiple trades at the exact same time (you can try whit SL / TP)

William Roeder
20061
William Roeder  
flaab: I would like to ask wether closing all trades with the following function is FIFO compliant, just to check.

Fifo requires closing oldest first. Your loop down is reverse ticket number (for non-pending orders is newest first.)

Counting up won't work Loops and Closing or Deleting Orders - MQL4 forum

Ticket number order won't work in general because pending orders open based on price, not ticket number.

Fifo also breaks mq4 model with stop loss/TP. If you move the oldest order SL up, all newer trades will move their SL down so they won't close before the older.

  1. Get ticket numbers ordered by open time GetSortedOrders() 
  2. Use a broker that does the fifo accounting on the backside and don't worry about it.
Point Zero
798
Point Zero  
WHRoeder:

Fifo requires closing oldest first. Your loop down is reverse ticket number (for non-pending orders is newest first.)

Counting up won't work Loops and Closing or Deleting Orders - MQL4 forum

Ticket number order won't work in general because pending orders open based on price, not ticket number.

Fifo also breaks mq4 model with stop loss/TP. If you move the oldest order SL up, all newer trades will move their SL down so they won't close before the older.

  1. Get ticket numbers ordered by open time GetSortedOrders() 
  2. Use a broker that does the fifo accounting on the backside and don't worry about it.




Thanks a lot, like always :)
To add comments, please log in or register