My Manage equity loop dosen't work

 

Hi!!

One more time I'm here to learn from you ;)

I've writen a loop to manage profit. I want to close all open orders from all pairs open by an EA if equity is more or less than a percent of balance......but dosen't work. The loop is into the EA.

Thanks for your help.

This is the code:

void Manage ()
{
bool result;
Arko = 0;
for (int i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == 0) continue;
   if (OrderComment()==Version){
   if ((AccountEquity()>=(((AccountBalance()/100)*ProfitPercent)+AccountBalance()) && ProfitPercent>0) || (AccountEquity()<=(AccountBalance()-((AccountBalance()/100)*LossPercent) && LossPercent>0))){
   if (OrderType() == OP_BUY)
      {
      result = 0;
      Arko = 0;
      if (Kumo == 1) Print("Trying to close trade.");
      while (!result && (Arko < Number))
         {
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         result = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Gold);
         if (!result)
            {
            Print("Error closing BUY order try #" + Arko + " " + ErrorDescription(GetLastError()));
            Arko++;
            }
         }
      break;
      }
   if (OrderType() == OP_SELL)
      {
      result = 0;
      Arko = 0;
      if (Kumo == 1) Print("Trying to close trade.");
      while (!result && (Arko < Number))
         {
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         result = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Gold);
         if (!result)
            {
            Print("Error closing SELL order try #" + Arko + " " + ErrorDescription(GetLastError()));
            Arko++;
            }
         }
      break;
      }
   }
}
}
}
 

I've made a change in the code and now works.....but the EA only closes orders form the symbol where the function is activated, not all the trades from all symbols that was open by the EA.....

The changes in the code:

start ()
{
if (ProfitManage == true){
if (ProfitPercent>0 && (AccountEquity()>(AccountBalance()+((AccountBalance()/100)*ProfitPercent))))
{
Manage();
}
if (LossPercent>0 && (AccountEquity()<(AccountBalance()-((AccountBalance()/100)*LossPercent))))
{
Manage();
}
}
.
.
.
.
}
void Manage ()
{
bool result;
Arko = 0;
for (int i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) > 0){
   if (OrderComment()==Version){
   if (OrderType() == OP_BUY)
      {
      result = 0;
      Arko = 0;
      if (Kumo == 1) Print("Trying to close trade.");
      while (!result && (Arko < Number))
         {
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         result = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Gold);
         if (!result)
            {
            Print("Error closing BUY order try #" + Arko + " " + ErrorDescription(GetLastError()));
            Arko++;
            }
         }
      }
   if (OrderType() == OP_SELL)
      {
      result = 0;
      Arko = 0;
      if (Kumo == 1) Print("Trying to close trade.");
      while (!result && (Arko < Number))
         {
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         result = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Gold);
         if (!result)
            {
            Print("Error closing SELL order try #" + Arko + " " + ErrorDescription(GetLastError()));
            Arko++;
            }
         }
      }
   }
   }
   }
   }
 

I've been reading this post on the forum: 'Need help on CloseOrder at SAR cross'

After read it I have the conclusion that I need change

for (int i = 0; i < OrdersTotal(); i++)

to

for(int i = OrdersTotal()-1; i >= 0; i--)

But I don´t understand the reason, can anyone explain me?

Now I know what is the problem in my loop or I think so.

My new code is working, I only need introduce de ordercomment() to select position.

I'd like your comments about the next code:

void start()
{
if (ProfitManage == true){
if (ProfitPercent>0 && (AccountEquity()>(AccountBalance()+((AccountBalance()/100)*ProfitPercent))))
{
Manage();
}
if (LossPercent>0 && (AccountEquity()<(AccountBalance()-((AccountBalance()/100)*LossPercent))))
{
Manage();
}
}
.
.
.
.
.
}
void Manage ()
  {
//----
   for (int i=0;i<150;i++)//150 or other number....
      {
         int total=OrdersTotal();
   
         if (total==0) 
           {
            Print("Manage OK");
            return(0);
           }
   
         for(int j=0;j<total;j++)
            {
              if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) //Maybe here I can introduce OrderComment() to select orders??
               {
                  int type=OrderType();
                  double openprice=OrderOpenPrice();
                  int ticket=OrderTicket();
                  double volume=OrderLots();
                  string symbol=OrderSymbol();
                  
                  if (type>=OP_BUYLIMIT){
                  RefreshRates();
                  OrderDelete(ticket,CLR_NONE);
                  }
                  if (type==OP_SELL){
                  RefreshRates();
                   OrderClose(ticket,volume,MarketInfo(symbol,MODE_ASK),Slippage,CLR_NONE);
}
                  if (type==OP_BUY){
                  RefreshRates();
                  OrderClose(ticket,volume,MarketInfo(symbol,MODE_BID),Slippage,CLR_NONE);
               }
               }
            }
      }
   total=OrdersTotal();
   
   if (total>0)
      Print("Manage wrong, repeating loop");
      Manage(); //Is good the idea of call manage() again here???
//----
   return(0);
  }
 
Did you ever get this to work? I am trying for the same thing!!! Please advise......DK
Reason: