Experts: Daily Profit Function

 

Daily Profit Function:

A simple and easy to understand function that calculates todays profit, written to the Expert Journal.

Author: Meta_Work

 
Meta_Work #:

Here is the function without needing to download the EA.


Thanks,

Why have you assigned this to the variable start?

   datetime end = TimeCurrent();
   datetime start = end-PERIOD_D1;
 

Here is the function without needing to download the EA.


void DayProfit()
  {
   double 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);
  }
 
Keith Watford #:

Why have you assigned this to the variable start?

Hi Keith, 


It's purely educational to show how the syntax of HistorySelect works for any new users of MT5 out there. Obviously, you wouldn't need to make each of these variables if you didn't wish. 


Thanks,  

 
Meta_Work #: It's purely educational to show how the syntax of HistorySelect works for any new users of MT5 out there. Obviously, you wouldn't need to make each of these variables if you didn't wish. 

It would only be educational if the code was correct, but it wasn't. You should not be adding a datetime with an enumeration when they are not compatible.

On MQL4 the time-frame enumeration is in minutes and on MQL5 it is a mix of bit flags and minutes. In neither case is it compatible with the seconds elapsed of a datetime.

The more correct way would be something like this:

datetime start = end - PeriodSeconds( PERIOD_D1 );

But that however, is still questionable.

 
Meta_Work #:

Hi Keith, 


It's purely educational to show how the syntax of HistorySelect works for any new users of MT5 out there. Obviously, you wouldn't need to make each of these variables if you didn't wish. 


Thanks,  

It is not educational if it is wrong.

You have deleted the relevant post and modified the code.

You should at least admit that your code was wrong.

 
Fernando Carreiro #:
datetime start = end - PeriodSeconds( PERIOD_D1 );

Hello everyone, I hope you're well,


why not simply use 0 as the starting point, as shown in the documentation here ?

HistorySelect(0,TimeCurrent());


I'm wondering about my message here


Best Reguards,
ZeroCafeine

Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
HistorySelect - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I answer myself, beginner's mistake, and after several reflexions I think that this type of starting time is very good for me now 😉. 

   string sdate = TimeToString (TimeCurrent(), TIME_DATE);
   datetime start = StringToTime(sdate);
 
Absolute life saver! Thanks!