Closing Muliple Positions

 

Hi everybody

I have been written a sample EA to open & close multiple positions as follows:

Opening short or long if price crosses moving averages (diMA1, diMA2 & diMA3).

That is to say, each time the price crosses any of them a position should be open (any direction).

For exit I use one criteria to close all Long positions and another to close all Short's at the same time

Though it is a very simple EA (attached for convenience) it is not working (ie: not opening or closing when required).

Kindly advice.

Tamir58

 

Advice:

//--- Closing Positions ---//
total=OrdersTotal();
if (total > 0)
{
for(cnt=0; cnt < total; cnt++)

If you are closing multiple orders, ALWAYS count from high to low:

for(cnt=total-1; cnt >= 0; cnt--)
 
phy:

Advice:

If you are closing multiple orders, ALWAYS count from high to low:

Can i know the logic behind?

 

Orders:

A B C D

0 1 2 3

Loop starting with 0, count to 3 to close all orders.

Select and Close 0

Orders After Close:

B C D

0 1 2

Select and Close 1

Orders After Close:

B D

0 1

Select and Close 2

There is no 2 .

Select and Close 3

There is no 3 .

Loop end, orders B and D remain open.

 
phy:

Orders:

A B C D

0 1 2 3

Loop starting with 0, count to 3 to close all orders.

Select and Close 0

Orders After Close:

B C D

0 1 2

Select and Close 1

Orders After Close:

B D

0 1

Select and Close 2

There is no 2 .

Select and Close 3

There is no 3 .

Loop end, orders B and D remain open.

Hi phy,


Is it applicable to single orders?


Thanks

 
Tested, works with single orders also.
Reason: