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.
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:
For non-FIFO (US brokers,)(or the EA only opens one order per symbol,) you can simply count down when closing/deleting/modifying in a
position loop, and you don't miss orders. Get in the habit of always counting
down.
For
FIFO (US brokers,)and if you (potentially) process multiple orders, you must count up and on a successful
operation, reprocess all positions (set index to -1 before continuing.)
and if you (potentially) process multiple orders, must call RefreshRates() after server calls if
you want to use the
Predefined Variables(Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.
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
For FIFO (US brokers,) and if you (potentially) process multiple orders, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
Which is the most recent position's index.
This topic is about mql4. There is no "PositionTotal" function.