Daily Profit and Losses ?

 
Hi everyone 😊,

I'm still a beginner in this fabulous language,

would you have a simple example code or a link with which I could calculate the daily profit or loss please?

I've done some research on the website but I often find MQL4 codes and not MQL5.

Best Reguards,
ZeroCafeine😊

 

CodeBase Search with keywords "daily profit": https://www.mql5.com/en/search#!keyword=daily%20profit&module=mql5_module_codebase

Very first result ...

Code Base

Daily Profit Function

Meta_Work, 2022.11.15 15:39

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

hi  Fernando Carreiro, Thank you for your reply


I've transformed the function a bit with your correction and a few extra lines, is there anything I'm still missing to check?

double DayProfit(){
    double   dayprof  = 0.0;
    datetime end      = TimeCurrent();
    datetime start    = end - PeriodSeconds(PERIOD_D1);
    
    HistorySelect(start,end);

    int TotalDeals = HistoryDealsTotal();
    for(int i = 0; i < TotalDeals; i++) {
        ulong    ticket   = HistoryDealGetTicket(i);
        string   symbol   = HistoryDealGetString(ticket , DEAL_SYMBOL);
        ulong    magic    = HistoryDealGetInteger(ticket, DEAL_MAGIC);
        long     dealType = HistoryDealGetInteger(ticket, DEAL_ENTRY);

        bool C1 = symbol == _Symbol && magic == InpLongMagicNumber && dealType == DEAL_ENTRY_OUT;
        if(C1){
            dayprof += HistoryDealGetDouble(ticket, DEAL_PROFIT);
        }    
    }
   return dayprof;
   // https://www.mql5.com/en/code/41406
}


tks in advance for any  advice,

Best Reguards,
ZeroCafeine 😊

 
ZeroCafeine #: hi  Fernando Carreiro, Thank you for your reply. I've transformed the function a bit with your correction and a few extra lines, is there anything I'm still missing to check?

My apologies, but I have decided to step away from the forum for a period of time.

Hopefully someone else will look at your code and offer you some advice.

Best regards!

 
Fernando Carreiro #:

My apologies, but I have decided to step away from the forum for a period of time.

Hopefully someone else will look at your code and offer you some advice.

Best regards!

I guess it's for the vacations, so enjoy! 😉

 
ZeroCafeine #:I guess it's for the vacations, so enjoy! 😉
No, not for vacation. I just want to step away from this forum in particular for a while!
 

ok no problem, 


If I'm not mistaken, I thought you were a moderator and therefore perhaps earn a salary for moderating the Forum?

double DayProfit(){
    double   profits  = 0.0;
    double   dayprof  = 0.0;
    double   dayloss  = 0.0;
    datetime end      = TimeCurrent(); 
    datetime start    = end - PeriodSeconds(PERIOD_D1);
    
    HistorySelect(start,end);

    int TotalDeals = HistoryDealsTotal();
    for(int i = 0; i < TotalDeals; i++) {
        ulong    ticket   = HistoryDealGetTicket(i);
        string   symbol   = HistoryDealGetString(ticket , DEAL_SYMBOL);
        ulong    magic    = HistoryDealGetInteger(ticket, DEAL_MAGIC);
        long     dealType = HistoryDealGetInteger(ticket, DEAL_ENTRY);

        bool C1 = symbol == _Symbol && magic == InpLongMagicNumber && dealType == DEAL_ENTRY_OUT;
        if(C1){
            double LatestProfit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
            profits += HistoryDealGetDouble(ticket, DEAL_PROFIT);

            if (HistoryDealGetDouble(ticket, DEAL_PROFIT) > 0){
                dayprof += HistoryDealGetDouble(ticket, DEAL_PROFIT);
            }

            if (HistoryDealGetDouble(ticket, DEAL_PROFIT) < 0){
                dayloss += HistoryDealGetDouble(ticket, DEAL_PROFIT);
            }            
        }  

    }
   Print("Function result - profits : ", profits);
   Print("Function result - dayprof : ", dayprof);
   Print("Function result - dayloss : ", dayloss);    
   return profits;

   // https://www.mql5.com/en/code/41406
}

@All

Otherwise here's a new version of the code, I'll try to separate gains from losses, and also I think there's a problem with the line of code with the line of code : end - PeriodSeconds(PERIOD_D1), I don't think it's very compatible with the  TimeCurrent() function, I'll look for more on my side 😊


What do you recommend for recovering the 3 variables? It might be better to pass them as a reference ?

 
Fernando, I think you are one of the people who provides the most help through forums. I am certain that many of us know what we know about MQL4 and MQL5 thanks to you. Publicly, I thank you for your excellent contributions on this platform. I hope you come back soon. 👍
 

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());


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
 
hooooo, sorry for my stupid question, 0 here mean from the beginning 
 

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);