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++)
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
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.
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!
while(o-->0) { ...
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!
while(o-->0) { ...
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:
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 ==)
It will check 0, but if he had written:
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.
Of course. I am an idiot. I always get confused with that :)
That's why I never use anything like that.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- 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 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!