my LockInProfit() and CloseAll() fail to close everything.

 
void LockInProfit()
{
   double Balance = AccountBalance();
   double Equity = AccountEquity();   
   
   if ((Equity) > (Balance*1.01))
   {          
         CloseAll();                
   }    return; 
}

void CloseAll() 
{
  // for (int i=OrdersTotal()-1;i>=0; i--)
   for (int i = 0; i < OrdersTotal(); i++)    // FIFO rule
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      { 
         RefreshRates();            
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrNONE))
         Print("Error closing (CloseAll) #",IntegerToString(OrderTicket())," Error code ",GetLastError());
      }  
   }
}

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?

 

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
   }
 
johnnybegoode: 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()?

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).

 
GumRai:

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?

 
FMIC:

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? 

 
johnnybegoode:

   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

 
johnnybegoode: I assigned magic number 12345 on all the orders. How do I close all the orders with magic number 12345? 
After the OrderSelect() is successful, then check the OrderMagicNumber() and only if it is the same as you own magic number, do you then proceed to close it.
 
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
Reason: