I want make my EA to stop trading when my target profit for the day has been hit.

 

Hello guys, please I need help in making my bot to stop trading when my target profit for the day has been reached. I have written down a few codes but it is bringing up an alert of target profit for each currency pair( It is in the picture attached ). All I want is for the bot to alert me when I have reached my target profit of the day maybe stop algo trading when the profit is hit. Please assist me with this guys🙏🏼. Thank you. Here is my code below:

CTrade trade;
ulong posTicket;
bool dayprof;
static datetime recommence_trading = 0;

void DayProfit()
  {
   dayprof= 0.0;
   datetime end = TimeCurrent();
   string sdate = TimeToString (TimeCurrent(), TIME_DATE);
   datetime start = StringToTime(sdate);

   HistorySelect(start,end);
   int TotalDeals = HistoryDealsTotal();

   for(int i = 0; i < TotalDeals; i++)
     {
      ulong Ticket = HistoryDealGetTicket(i);

      if(HistoryDealGetInteger(Ticket,DEAL_ENTRY) == DEAL_ENTRY_OUT)
        {
         double LatestProfit = HistoryDealGetDouble(Ticket, DEAL_PROFIT);
         dayprof += LatestProfit;
        }
     }
   Print("DAY PROFIT: ", dayprof);
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      DayProfit();
//---
   return(INIT_SUCCEEDED);
  }

void OnTick {

if(dayprof = 20.00)
     {
     Alert("Close trading for today, target profit reached");
     }
     else if(dayprof = -20.00)
     {
     Alert("Close trading for today, too much loss made today");
     }
}
Files:
bb.png  92 kb
 
David Udoh: stop trading when my target profit for the day has been reached.
if(dayprof = 20.00)
  1. That is not a comparison ("==") it is an assignment ("=").

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  3. Your code stop only when profit/loss exactly equals 20. What happens if it is one cent past that?

 

There are a few things wrong there. I recommend not using global variable for dayprof (and it cannot be bool – but double) – just return value of daily profit by your DayProfit() function. Then call this function on every tick and if result is more than assumed daily profit - simply send alert (but then you need to block other alerts for this day) and send return (to block further trading for this day) like:
if(dayprof>=20){

Alert(“dayprof reached”);
return;

}else {

//normal trading

}
And also in the Dayprofit function you need to choose only deals that are interesting for you:
so add a few ifs:
if(HistoryDealGetInteger(Ticket,DEAL_TIME) <sdate) continue; //omit trades closed earlier than today
if(HistoryDealGetInteger(Ticket,DEAL_SYMBOL) !=_Symbol) continue; //omit trades from other symbols
etc…
 
Marzena Maria Szmit #: There are a few things wrong there. I

Starting with not using the code button for code.

 
Thanks guys it work. I really appreciate the help
Files:
work.png  50 kb
 
David Udoh #:
Thanks guys it work. I really appreciate the help

Can I please have a copy of this EA

Reason: