Just an word of caution to other MT4 users

 
Hi everyone,

I came across a little issue that everyone has to be careful of when they are using multiple open positions.

I was using something like this
for(trade=0;trade< OrdersTotal();trade++){
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
Closetrades...
etc.

But, for some reason, a trade was not being closed.

However, when I did the following it worked perfectly.

for(trade=OrdersTotal()-1;trade>=0;trade--){
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
Closetrades...
etc.


Not sure why, maybe one of the MT experts can explain. Just wanted to advise other users.
 
See example. You have 5 orders
0 - 110001
1 - 110002
2 - 110003
3 - 110004
4 - 110005

You select first order (index 0) and close it. Your list changed
0 - 110002
1 - 110003
2 - 110004
3 - 110005

Next index is 1 (trade++). You select second order and close it
0 - 110002
1 - 110004
2 - 110005

Next index is 2. You select and close
0 - 110002
1 - 110004

Next index is 3. But You have not order with index 3.
 
See example. You have 5 orders
0 - 110001
1 - 110002
2 - 110003
3 - 110004
4 - 110005

You select first order (index 0) and close it. Your list changed
0 - 110002
1 - 110003
2 - 110004
3 - 110005

Next index is 1 (trade++). You select second order and close it
0 - 110002
1 - 110004
2 - 110005

Next index is 2. You select and close
0 - 110002
1 - 110004

Next index is 3. But You have not order with index 3.


Thank you for the explanation. That is what I suspected. It is a slight nuance that can become a major problem if not paid attention to. Maybe you all should encourage the users to write the code using the "for(trade=OrdersTotal()-1;trade>=0;trade--){" style.

In your code base examples, it uses the other count style to find trades and close it. It is okay for single trades, but will falter during multiple trades. I am requesting that you change the examples in the codebase samples to incorporate this style of counting. Otherwise, it will confuse many users.

Thanks.