FIFO Safe Closure

To add comments, please log in or register
Frederick Langemark
2273
Frederick Langemark  

I do not understand why this code is causing an error about a FIFO violation.

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

Is the order at index 0 not always the first opened position? If not, what is the right way to go about this?

Mohamad Zulhairi Baba
16397
Mohamad Zulhairi Baba  
Frederick Langemark:

I do not understand why this code is causing an error about a FIFO violation.

Is the order at index 0 not always the first opened position? If not, what is the right way to go about this?

void CloseAll() {
   bool resultClose = false;
   
   for(int i = 0; i <= OrdersTotal()-1; i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            switch(OrderType()) {
                RefreshRates();
                case OP_BUY:
                  resultClose = OrderClose(OrderTicket(), OrderLots(), Bid, 0);
                  if(resultClose) i--;
                  break;
                case OP_SELL:
                  resultClose = OrderClose(OrderTicket(), OrderLots(), Ask, 0);
                  if(resultClose) i--;
                  break;
            }
         }
      }
   }
}





Trinh Dat
46797
Trinh Dat  

Hi

Get list of order ticket in array, then sort ASCEND.

Select order by ticket then close.

William Roeder
20207
William Roeder  
  1. Frederick Langemark: Is the order at index 0 not always the first opened position? If not, what is the right way to go about this?

    Yes but after closing the first position, the second position could be at zero which you skip.

    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, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  2. Mohamad Zulhairi Baba:
    Decrementing i works if and only if there are orders on one symbol. See highlighted portion above.


To add comments, please log in or register