Help creating a LOOP for Counting Open Orders

 

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!

int total = OrdersTotal();

double OpenLongOrders = 0, OpenShortOrders = 0, PendLongs =0, PendShorts =0;
 for( i=0;i<total;i++)       
   {
     OrderSelect(i, SELECT_BY_POS );
    if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
           int type = OrderType();

if (type == OP_BUY )       {OpenLongOrders=OpenLongOrders+1;}
if (type == OP_SELL )      {OpenShortOrders=OpenShortOrders+1;}
if (type == OP_BUYSTOP )   {PendLongs=PendLongs+1;}
if (type == OP_SELLSTOP )  {PendShorts=PendShorts+1;}
 
JPS wrote >>

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

http://paulsfxrandomwalk.blogspot.com/

 

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.

 
This might happen if your connection to the server is really bad. From my experience in this kind of situation, a short sleep and retry might solve the problem. Specifically I have seen cases of orders not appearing in the order pool till a few minutes AFTER they were opened... This had to do with bad connection to the server. The solution was indeed a short sleep and retry.
 
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.
 
JPS wrote >>

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

  1. Check count of open orders of type xxx
  2. OrderSend(...xxx...)
  3. while no change in xxx count, and repeatcount less than max
  4. 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

http://paulsfxrandomwalk.blogspot.com/

 
WHRoeder:
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.

 
Thanks everyone for your help and comments.
Reason: