You must count down in your loop when closing trades.
That is because if you close the trade at position 0, the trade at position 1 is moved to 0, but the loop moves on to position 1 (which is the previous position 2) and the trade that was originally at position 1 is missed. etc.
If this conflicts with FIFO, then either save the ticket numbers in an array first and then close them.
Or
Use a while loop ? (never tried this myself)
while(OrdersTotal()>0) { //close the order at position 0 }
As GumRai stated, always count down. However, don't just close all orders blindly! Do proper management of Magic Number Identity!
In the future, you never know if you will be using other EAs and/or manual trading simultaneously, and you don't want your EA messing up those trades.
So, define a Magic Number ID in your EA, and only close trades with that Magic Number (and not the rest).
You must count down in your loop when closing trades.
That is because if you close the trade at position 0, the trade at position 1 is moved to 0, but the loop moves on to position 1 (which is the previous position 2) and the trade that was originally at position 1 is missed. etc.
If this conflicts with FIFO, then either save the ticket numbers in an array first and then close them.
Or
Use a while loop ? (never tried this myself)
1) How do I get the OrderSelect index i if using the while loop?
while(OrdersTotal()>0) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
2) How save the ticket numbers in an array first and then close them?
As GumRai stated, always count down. However, don't just close all orders blindly! Do proper management of Magic Number Identity!
In the future, you never know if you will be using other EAs and/or manual trading simultaneously, and you don't want your EA messing up those trades.
So, define a Magic Number ID in your EA, and only close trades with that Magic Number (and not the rest).
I assigned magic number 12345 on all the orders.
How do I close all the orders with magic number 12345?
1) How do I get the OrderSelect index i if using the while loop?
From my previous post
while(OrdersTotal()>0) { //close the order at position 0 }
johnnybegoode:
2) How save the ticket numbers in an array first and then close them?
Loop through the open orders and save the ticket numbers in an array
Loop through the array and use the saved ticket numbers to close the trades
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have multiple opened positions in multiple currencies. When LockInProfit() is triggered, it does not close everything.
Is the problem with LockInProfit() or/and CloseAll()?
I suspect that
1) The functions only close the currency pair that the EA is attached to and not all the other currency pairs.
2) At some point, it stop closing when (Equity) is no longer > (Balance*1.01).
How do I close everything? (All opened positions in multiple currencies )
How to close everything without attaching the EA to multiple charts of different pairs.
How do I close everything (including currencies of different pairs) when attaching the EA to only one chart?