Implementing prop firm rules within my EA

 

Hello, I am trying to implement the main prop firm rules within my EA: 

- stop trading when 8% profit reached

- stop trading when 10% loss reached

- stop trading for the day when 5% drop from the equity at the opening of the day.

I have the following code to save the equity at the opening of the day, this is within at timer which run every 30 seconds. I also have the following conditions to determine if the equity meet certain criteria:

void OnTimer(){
   currentEquity = AccountInfoDouble(ACCOUNT_EQUITY);
   currentBalance = AccountInfoDouble(ACCOUNT_BALANCE);
   if(EquityOpenDay == 0) EquityOpenDay = currentEquity;
   
   datetime time = TimeCurrent();
   CurrentTime = TimeToString(time, TIME_MINUTES); 

   if(CurrentTime == "00:00"){
      EquityOpenDay = AccountInfoDouble(ACCOUNT_EQUITY);       //This is working, equity at 00:00 will get saved into EquityOpenDay
 } 
 
   double DailyDrawdown = ((EquityOpenDay - currentEquity) / EquityOpenDay) * 100;   
   double AccountMaxLoss = AccountSize * (1 - (MaxDD / 100));   
   double AccountBalanceTarget = AccountSize * (1 + (target/100) + 0.001);

   if (DailyDrawdown >= MaxDailyDD) DailyDDReached = true;
   if ( (currentBalance <= AccountMaxLoss) || (currentEquity <= AccountMaxLoss) ) MaxDDReached = true;
   if ((currentBalance >= AccountBalanceTarget) || (currentEquity >= AccountBalanceTarget)) TargetReached = true;

}  

AccountSize, MaxDD, target and MaxDailyDD are input and set in percentage, except for AccountSize. 

Then in the OnTick I have the following conditions to open trades:

   if (TargetReached ){
close all trades
}
         
   else if (DailyDDReached ){
close all trades
}         


      if((TargetReached == false) && (MaxDDReached == false)){
         if (DailyDDReached == false){
Trade executions
}}

However it is working most of the time but sometimes the profit goes a few % above the wanted target. Anyone has an idea of why. 

Also, is it valid to collect the daily open equity that way? It seems a bit too simple that way. 

Thanks!