How to calculate total Profits made in a day

Help needed. I am trying to calculate the total profits or loss made in a day in MQL5. Some should Kindly help me with code example. Thank you.

I use MT4 currently, so to answer your question I went to:

claude (dot) ai
which is similar to chatGPT but seems to work better for me.

This is the prompt I used:
In Metatrader 5, show me code to calculate and put on the chart the total daily profit (in green) or the total daily loss (in red and with a minus in front).

SP ============================

Here is some MQL5 code to calculate and display the total daily profit or loss on the chart:


Copy code

#property strict

datetime prevTime = 0;
double prevBalance = 0;
double dailyProfit = 0; 

int OnInit()
  prevTime = TimeCurrent();
  prevBalance = AccountInfoDouble(ACCOUNT_BALANCE);

void OnTick()
  datetime currTime = TimeCurrent();
  if(currTime >= prevTime + PeriodSeconds(PERIOD_D1)) 
    double currBalance = AccountInfoDouble(ACCOUNT_BALANCE);
    dailyProfit = currBalance - prevBalance;
    if(dailyProfit >= 0) 
      Comment("\n Total Daily Profit: ", DoubleToStr(dailyProfit, 2), "   ");
      Comment("\n Total Daily Loss: ", DoubleToStr(dailyProfit, 2), "   ");
    prevTime = currTime;
    prevBalance = currBalance;

This code keeps track of the previous daily balance and time. On each new tick, it checks if a new trading day has started. If so, it calculates the difference between the current and previous balance to get the total daily profit/loss.

It then displays the daily profit in green or the daily loss (with a minus sign) in red on the chart using the Comment function. The prevTime and prevBalance are updated to the current values, ready for the next day’s calculation.

So this will show a running display of the total profit or loss for each day on the Metatrader 5 chart. You can modify the code as needed, for example to reset the dailyProfit to 0 at the start of each new day.