CodeBase Search with keywords "daily profit": https://www.mql5.com/en/search#!keyword=daily%20profit&module=mql5_module_codebase
Very first result ...
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 😊
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!
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 ?
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
- www.mql5.com
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);
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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😊