CloseOrders by FIFO Rules - page 2

 

I would like to see an example where the code I posted would fail.

 
Donald Gibson: I would like to see an example where the code I posted would fail.
   for(i=0; i<OrdersTotal(); i++) if(
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   && OrderSymbol()==Symbol()
   ){
               Close_Result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrNONE);
               if(Close_Result) i--;
I gave you several links which explain it. Here's a graphic FIFO example.
  1. Start with orders:
    0
    1
    2
    3
    4
    5
    SomeChart
    SomeChart
    CurrentChart1
    CurrentChart2AnotherChart
    AnotherChart
  2. Loop goes 0,1, finds an order at position 2, and calls OrderClose.

  3. SomeChart orders are closed by SL, before position 2 can send network request, have it processed in brokers queue, and received server reply. On next select, position list looks like:
    0
    1
    2
    3
    CurrentChart1
    CurrentChart2AnotherChart
    AnotherChart
  4. OrderClose completes, On next select, position list looks like:
    0
    1
    2
    CurrentChart2AnotherChart
    AnotherChart
  5. Your code decremented i to 1. For loop increments i back to 2, then 3 and exits. Order at position zero is not closed.
 
William Roeder:
I gave you several links which explain it. Here's a graphic FIFO example.
  1. Start with orders:
    0
    1
    2
    3
    4
    5
    SomeChart
    SomeChart
    CurrentChart1
    CurrentChart2AnotherChart
    AnotherChart
  2. Loop goes 0,1, finds an order at position 2, and calls OrderClose.

  3. SomeChart orders are closed by SL, before position 2 can send network request, have it processed in brokers queue, and received server reply. On next select, position list looks like:
    0
    1
    2
    3
    CurrentChart1
    CurrentChart2AnotherChart
    AnotherChart
  4. OrderClose completes, On next select, position list looks like:
    0
    1
    2
    CurrentChart2AnotherChart
    AnotherChart
  5. Your code decremented i to 1. For loop increments i back to 2, then 3 and exits. Order at position zero is not closed.

LMAO

Typical Rhoeder

In the 3 years I have been using this function, in every situation in which I could see the need to close multiple positions on multiple charts or just 1, this function has never failed to close all the orders.

OP can use this function or not doesn't matter to me. I am still waiting on something better to come along, haven't seen anything but links and graphics so far.

 

Donald Gibson:

LMAO

Typical Rhoeder

In the 3 years I have been using this function, in every situation in which I could see the need to close multiple positions on multiple charts or just 1, this function has never failed to close all the orders.

  1. LMAO as you apparently like bugs in your code. Three years and your bug remains.
  2. Typical? Apparently you can't think beyond your own pride. Can't type correctly either.
  3. What part of FIFO and multiple orders was unclear? What part of "non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down" was unclear?
  4. I don't help rude, arrogant, or ignorant persons. You are apparently all three. Added to my do not help list.
 
Donald Gibson:

LMAO

Typical Rhoeder

In the 3 years I have been using this function, in every situation in which I could see the need to close multiple positions on multiple charts or just 1, this function has never failed to close all the orders.

OP can use this function or not doesn't matter to me.

I'm going to side with whordor on this one. No matter what programming language you use you should never mutate an iterable while you are iterating it. With MQL you are forced against your will to iterate a mutating iterable and need to adjust your code accordingly. First, iterating index forward over the iterable (while mutating it explicitly) is an absolute recipe for failure. I also don't agree that reversing the index is the best fix, but it's better than forward since it won't skip over elements when they are removed. No matter what, working with FIFO and the MQL order-pool is always a race-condition. Therefore it is best to find the (F)irst ticket in the pool, deal with it, then recursively call back to the function so that you can limit the race-condition to just the nanosecond it takes to iterate over the pool (w/o trade ops). 

 I am still waiting on something better to come along, haven't seen anything but links and graphics so far.

This is better.

int closeFifoTrades(string symbol, int order_type) {
   int ticket = WRONG_VALUE;
   for (int i=OrdersTotal()-1; i>=0; --i) 
      if (OrderSelect(i, SELECT_BY_POS)
         && OrderSymbol() == symbol
         && OrderType() == order_type 
         && (ticket == WRONG_VALUE || OrderTicket() < ticket)
      )
         ticket = OrderTicket();
   if (OrderSelect(ticket, SELECT_BY_TICKET)
      && OrderClose(ticket, OrderLots(), OrderClosePrice(), 10)
   )
      return closeFifoTrades(symbol, order_type) + 1;
   return 0;
}
 
nicholi shen: never mutate an iterable while you are iterating it.

And shouldn't use iteration when a loop will do. Perhaps this, for both of us:

int closeFifoTrades(string symbol, int order_type) {
   int count=0;
   for(int i=0; i < OrdersTotal(); ){
      if(OrderSelect(i, SELECT_BY_POS)
      && OrderType()   == order_type 
      && OrderSymbol() == symbol
      && OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10)
      ){ RefreshRates(); // Update OrderClosePrice for next select.
         i=0;            // Reprocess all orders, possible race condition.
         ++count;
      }
      else ++i;
   }
   return count;
}
 
William Roeder:

And shouldn't use iteration when a loop will do. Perhaps this, for both of us:

Iteration is looping. I think you meant recursion? Your function would only work on non-FIFO brokers, and even still, recursion is much more explicit than mutating the index. 

 
Wasn't yet awake, yes I meant recursion. Function works on both.
 
William Roeder:
Wasn't yet awake, yes I meant recursion. Function works on both.

Wasn't it you who said, "don't assume the order is ordered"? ...don't assume the order pool is ordered. 

 
nicholi shen: Wasn't it you who said, "don't assume the order is ordered"? ...don't assume the order pool is ordered. 
Reason: