Hi,
I'm using this code to count open and pending orders.
It seems that occasionally MT4 will return a value of 0, even though a pending or market order is present.
Is there a way to double check my orders or a better way to code that is more reliable than what I'm using?
Thanks in advance!
I don't recall seeing evidence of this myself
Does it happen when the order has only just been sent?
If so, you might try something like this:
if (!OrderSelect(i,SELECT_BY_POS)) { Sleep(5000); OrderSelect(i,SELECT_BY_POS); }
Paul
Hi Phantom. Thanks for your reply.
Yes it will only happen after the order has been sent. It makes sense to only be at that moment.
When it does count an order is usually shows it until the order is closed or deleted.
I'm not sure why a sleep function would correct this problem.
I'm sure there must be a way to add in a while loop or some onther method that will call MT4 to check and/or double check positions.
I'll give the sleep function a try. Just unsure of it's reliability.
Hi Phantom. Thanks for your reply.
Yes it will only happen after the order has been sent. It makes sense to only be at that moment.
When it does count an order is usually shows it until the order is closed or deleted.
I'm not sure why a sleep function would correct this problem.
I'm sure there must be a way to add in a while loop or some onther method that will call MT4 to check and/or double check positions.
I'll give the sleep function a try. Just unsure of it's reliability.
You're probably right JPS, it is only an assumption on my part that OrderSelect would fail until the orders have fully completed. On second thoughts a robust OrderSend may be needed which checks that the intended order has fully completed. I've never seen this occur in MT4, but in MT5 it is quite common, see my comments here.
Broadly, the approach would be ...
Goal: robust issue of order of type xxx
- Check count of open orders of type xxx
- OrderSend(...xxx...)
- while no change in xxx count, and repeatcount less than max
- Sleep
The count orders function would look something like this
int OpenOrders(int nOrderType, string strSymbol, int nMagic) { int nOrderCount=0; for (int i=OrdersTotal()-1 ; i>=0 ; i--) { if (!OrderSelect(i,SELECT_BY_POS)) continue; if (OrderType() == nOrderType) if (OrderMagicNumber() == nMagic) if (OrderSymbol() == strSymbol) nOrderCount++; } return(nOrderCount); }
As a matter of fact, I'm going to add that to my generic MT4 EA module - better safe than sorry.
Paul
You must always refreshRates() after doing any orderSend/orderModify/orderClose before doing any other operation including ordersTotal() or do your counting on the next tick. You need to to the counting on the next tick anyway as the terminal/ea might have been just restarted and you need to manage any open orders.
There is only need to use RefreshRates() if u use the predefined variables (Bid, Ask, Digits, etc...). Personally I access all info via MarketInfo() function, hence no need for RefreshRates(). Also an order that was just opened can appear in the pool in the same tick and not necessarily in the next one... I have seen this happen many times.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I'm using this code to count open and pending orders.
It seems that occasionally MT4 will return a value of 0, even though a pending or market order is present.
Is there a way to double check my orders or a better way to code that is more reliable than what I'm using?
Thanks in advance!