Close all open positions by profit

 

hi i made this function to close trades when the amount of profit in the open positions crosses a certain threshold the bot closes all open positions cause i noticed in the back test that EA was had alot of equity it was holding onto but was not closing the trades.

bool CloseAll () {

 bool result = true;
 
 int count  = PositionsTotal();
 for (int i=count-1; i>=0; i--){
  ulong ticket = (int) PositionGetTicket(i);
   if (ticket>0) {
    if ( (InpThisSymbol==false || PositionGetString(POSITION_SYMBOL)==Symbol()) && ( PositionGetInteger(POSITION_MAGIC)==MagicNum) ) {
      result &= Trade.PositionClose(ticket);
    }
   } else {
     result = false;
   }
 }
 return(result);

}

void CloseByEquity () {

 if (ACCOUNT_PROFIT >= ACCOUNT_BALANCE* 10/100){
 return;
 }

 int retries = 10;
 for (int i=0; i<retries ; i++){
  if (CloseAll()){
   return;
  }
 
 }
 return;
}

but it does not seem to be working properly cause its still not closing the trades in the backtest when the threshold is passed.

 
Mihlali Dlulane: but it does not seem to be working properly
 if (ACCOUNT_PROFIT >= ACCOUNT_BALANCE* 10/100){
Why do you expect this to work correctly?
 if (       2       >=        0       * 10/100){

Use the constants to get the actual values.

 
William Roeder #:
Mihlali Dlulane: but it does not seem to be working properly
Why do you expect this to work correctly?

Use the constants to get the actual values.

I thought that using accountbalance and accountprofit would give me the amounts
void CloseByEquity () {
 const double Balance = ACCOUNT_BALANCE;
 const double Profit  = ACCOUNT_PROFIT;
 if ( Profit >= Balance * 10/100){
 return;
 }

 int retries = 10;
 for (int i=0; i<retries ; i++){
  if (CloseAll()){
   return;
  }
 
 }
 return;
}

so i need to set them like this instead ?

 
Mihlali Dlulane #: so i need to set them like this instead ?
 const double Balance = ACCOUNT_BALANCE;
 const double Profit  = ACCOUNT_PROFIT;
Same as the previous post.
 const double Balance = 0;
 const double Profit  = 2;

What part of “Use the constants to get the actual values.” was unclear?

For the function AccountInfoDouble()

ENUM_ACCOUNT_INFO_DOUBLE

Identifier

Description

Type

ACCOUNT_BALANCE

Account balance in the deposit currency

double

ACCOUNT_PROFIT

Current profit of an account in the deposit currency

double

 
William Roeder #:
Mihlali Dlulane #: so i need to set them like this instead ?
Same as the previous post.

What part of “Use the constants to get the actual values.” was unclear?

sorry i didnt read the documentation,
Thanks for the help
void CloseByEquity () {
 const double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
 const double Profit  = AccountInfoDouble(ACCOUNT_PROFIT);
 if ( Profit >= Balance * 10/100){
 return;
 }

 int retries = 10;
 for (int i=0; i<retries ; i++){
  if (CloseAll()){
   return;
  }
 
 }
 return;
}

This is how is should be used now ?