How to separate (and close) (Market) Sell orders and Buy orders separately? - page 2

 
FourX:

But the bottom of the stack is at zero rather than one isn't it? Hence having to subtract one from the number of orders returned when queried about this. Thus when the open order pool is empty, then it isn't going to return zero as this is a valid number for an order and there would still be one order in the pool. So do I test for 'NumberOfOrdersProcessed' < 0' to find out when i have gone through all of the stack including the last one at index position zero?

The processing is even more complicated than what you're thinking. Suppose you have open orders at position: 0) another chart order (different pair or EA,) 1) your buy, 2) your sell and you want to close the sell.

At this point You start the loop with OrdersTotal = 3 and iPos = 2. You select the sell and call OrderClose. While the order is closing, the other EA on the other chart closes its order at position 0.

When the OrderClose completes, OrdersTotal will now be 1 and your loop will decrement iPos to 1.

On next loop cycle OrderSelect will fail, there is no order at position 1. This is why you must count down AND test the OrderSelect status. What happens next is the iPos decrements again you select the order at 0 (Your buy) and ignore it since you're closing sells. and the loop exits.

You don't need to count anything. Once you get to iPos < 0, the loop exits and you are done.

 
RaptorUK:
When there are no open orders and no pending orders OrdersTotal() will return 0 . . . . this value is the number of orders, it has nothing to do with with any positions of any orders in the pool . . . there are no orders in the pool. When you have orders in the pool and you reference them by position valid positions are >= 0 the last position is OrdersTotal() - 1 if OrdersTotal() is 0 then 0 - 1 = -1 and this is not a valid position.

Got it,

Thanks Raptor

 
Standard post updated for FIFO (since 2009)NFA Enforces FIFO Rule, Bans Forex Hedging in US Forex Accounts - Trading Heroes
FAQ: FIFO in the Forex Market - BabyPips.com
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because 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:
 
strategy_tester:
Which is the most recent position's index. 
Is it 0 or PositionsTotal()-1 ?
This topic is about mql4. There is no "PositionTotal" function.
 
Alain Verleyen:
This topic is about mql4. There is no "PositionTotal" function.
Sorry I meant orders.
OrdersTotal()-1 or 0?