Dont enter anymore trades for the day if profit target reached

 

Hi There


I have an EA and would like to not enter any more trades if the profit target has been reached for the day.

If I have a 5% target for the day 

I have put this code in the ontick function but it does not seem to be doing anything.

 if(m_account.Equity()>=m_account.Balance()*5) return;

Any help would be appreciated.

Thanks

 
Stanton Roux:

Hi There


I have an EA and would like to not enter any more trades if the profit target has been reached for the day.

If I have a 5% target for the day 

I have put this code in the ontick function but it does not seem to be doing anything.

 if(m_account.Equity()>=m_account.Balance()*5) return;

Any help would be appreciated.

Thanks

Compiled, not tested. 
You need to code the close positions and delete pending orders part.
#include <Trade\AccountInfo.mqh>
CAccountInfo m_account;
int day;
double day_balance;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   day=DayOfYear();
   day_balance=m_account.Balance();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- new day
   if(day!=DayOfYear())
     {
      day=DayOfYear();
      day_balance=m_account.Balance();
     }
   if(m_account.Equity()>=day_balance*1.05)
     {
      CloseAllPositions();
      DeleteAllPending();
      return;
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int DayOfYear()
  {
   MqlDateTime cur_time;
   TimeToStruct(TimeCurrent(),cur_time);
   return cur_time.day_of_year;
  }
//+------------------------------------------------------------------+
//| Here code to close open positions                                |
//+------------------------------------------------------------------+
void CloseAllPositions()
  {
  }
//+------------------------------------------------------------------+
//| Here code to delete pending orders                               |
//+------------------------------------------------------------------+
void DeleteAllPending()
  {
  }
 
Amir Yacoby:
Compiled, not tested. 
You need to code the close positions and delete pending orders part.

it seems to be working.

Only change I made was to replace.

 if(m_account.Equity()>=m_account.Balance()*1.05)
     {
      CloseAllPositions();
      DeleteAllPending();
      return;
     }
  }

with

 if(m_account.Equity()>=day_balance*1.05)
     {
      CloseAllPositions();
      DeleteAllPending();
      return;
     }
  }
 
Stanton Roux:

it seems to be working.

Only change I made was to replace.

Yes, I replaced it shortly after first publish.
 
Amir Yacoby:
Yes, I replaced it shortly after first publish.

Thanks for this will investigate further and do some back tests.

Much appreciated

Reason: