orders opened and closed during last bar

 

hi all, I need to check the opened and closed orders after the start of the current bar.

Is this code correct? I'm not sure.

 

  
  total = OrdersTotal();
  Num_Order_Long_5  = 0;
  Num_Order_Short_5 = 0;
  
  for(cnt=0;cnt<=total;cnt++)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
     if (OrderSymbol()==Symbol() && OrderOpenTime()>=Time[0])
        {  
        if (OrderType()==OP_BUY)
           {
           Num_Order_Long_5++;
           }
        if (OrderType()==OP_SELL)
           {
           Num_Order_Short_5++;

           }  
        }  
     }

 

Thank you! 

 

  total = OrdersTotal();
  
  for(cnt=0;cnt<=total;cnt++)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);

You need OrdersHistoryTotal() if looping through closed orders

OrdersTotal() is for open orders.

 

In addition to the above, the orders start at index 0 (not 1) so really it should be: 

// for(cnt=0;cnt<=total;cnt++)
for(cnt=0;cnt<total;cnt++)

As you have it, the final order select is going to fail, but it won't cause any major issue because the OrderSymbol check will also fail.

In any event, it is best to count down, rather than up when dealing with orders - see here

You will need 2 loops, one for the open orders and one for the closed orders. 

I'd also recommend adding

#property strict

to your code. It would have flagged up at least one warning with the code you have posted (to check the return value of OrderSelect). I'm sure it will help with other sections of your code.

 
Thank you very much!
 

For orders (and objects) one better should make it as a personal habit to count down - in case you want to delete/close them and you are re-using (copy & paste) your own code!

int o = = OrdersTotal();
while(o-->0) { ...
 
Carl Schreiber:

For orders (and objects) one better should make it as a personal habit to count down - in case you want to delete/close them and you are re-using (copy & paste) your own code!

int o = = OrdersTotal();
while(o-->0) { ...
Although I have never used anything like this, I feel that Order with the index 0 will not be checked.
 
Keith Watford:
Although I have never used anything like this, I feel that Order with the index 0 will not be checked.

It will check 0, but if he had written: 

   while(--o>0) { ...

then it would not check 0.

I enjoy reading Carl's posts - he often has a slightly different way of looking at things (PS typo on the first line = not ==)

 
honest_knave:

It will check 0, but if he had written: 

   while(--o>0) { ...

then it would not check 0.

I enjoy reading Carl's posts - he often has a slightly different way of looking at things (PS typo on the first line = not ==)

Of course. I am an idiot. I always get confused with that :)

That's why I never use anything like that.

 
Keith Watford:

Of course. I am an idiot. I always get confused with that :)

That's why I never use anything like that.

Don't worry, you are not alone. I personally never use such confusing syntax, there is 0 benefit.
Reason: