Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
jon
424
jon 2016.09.14 02:57 
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?

Keith Watford
Moderator
7958
Keith Watford 2016.09.14 03:46  

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
   }
Fernando Carreiro
1498
Fernando Carreiro 2016.09.14 03:58  
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).

jon
424
jon 2016.09.14 08:36  
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?

jon
424
jon 2016.09.14 08:37  
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? 

Keith Watford
Moderator
7958
Keith Watford 2016.09.14 09:32  
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

Fernando Carreiro
1498
Fernando Carreiro 2016.09.14 09:54  
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.
whroeder1
13627
whroeder1 2016.09.14 13:42  
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
/
To add comments, please log in or register