How to know daily closed profit

 

Hi, I want to know today's closed total profit.(whole account)

Here is the code.

However, when I open a new account and run the EA,

it shows as same as account balance like below picture.

This is because there is no closed profit today?

Do I need to add some code ?

double ProfitDey(int type) 
{double Profit = 0;

   for (int cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--) {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY))
      {if (  OrderCloseTime()>=iTime(Symbol(),1440,0) && (OrderType() == type || type==-1))Profit += OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());}}
       return (Profit);}


 

I found some issue.

When I use below code. it looks like it reads normal order profit +  AccountBalance...

 for (int cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--) {
 
Hong Ling Mu:
{if (  OrderCloseTime()>=iTime(Symbol(),1440,0) && (OrderType() == type || type==-1))Profit += OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());}}

Are swap and commission always greater than zero?

If so, then the MathAbs() is meaningless. If not, then you are misrepresenting profits.
 
Hong Ling Mu:
if (  OrderCloseTime()>=iTime(Symbol(),1440,0) && (OrderType() == type || type==-1))

This is a very slow thing. Slow things are best called after checking fast ones (this way they will be called fewer times):

double ProfitDey(int type) 
  {
   double Profit = 0;
   for(int cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
     {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY))
        {
         if((OrderType() == type || type==-1) && OrderCloseTime() >= iTime(Symbol(),1440,0)) // Calling after type checking
            Profit += OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());
        }
     }
   return(Profit);
  }

There is a way to call this bad function even fewer times:

double ProfitDey(int type) 
  {
   double Profit = 0;
   datetime dtBeginOfToday = iTime(Symbol(),1440,0);
   for(int cnt = OrdersHistoryTotal() - 1; cnt >= 0; cnt--)
     {
      if(!OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY)) // Nesting removed
         continue;
      if((OrderType() == type || type == -1) && OrderCloseTime() >= dtBeginOfToday)
         Profit += OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());
     }
   return(Profit);
  }

But the iTime() function is generally better not to use. In addition to being very slow, it can return an incorrect value, generating a 4066 error.

You should think about how to implement this without using iTime().


The above is far from all the problems of your code...😅

 
// globals
double todaysprofit; 
int    lastminute;

void ProfitToday() 
{
   if (lastminute!=Minute()) { lastminute=Minute(); } else return; // one minute updates
   todaysprofit=0; // reset to sum again 
  
   string todaysdate=TimeToStr(TimeCurrent(),TIME_DATE);

   for (int pos=OrdersHistoryTotal()-1; pos>=0; pos--)
   {
      if (OrderSelect(pos,SELECT_BY_POS, MODE_HISTORY)==TRUE /*&& OrderSymbol()==_Symbol && OrderMagicNumber()==MAGIC*/ ) 
      {
         bool today=TimeToStr(OrderOpenTime(),TIME_DATE)==todaysdate;
         if (today) todaysprofit += OrderProfit(); // sum profits & losses for today
         else break; // no need to look at entire trade history once today is calculated
      }
   }
}
 
maximo #:

That's much better.

But it's better to use a MqlDateTime than to work with strings.

And we can update profit once a day instead of once a minute (here I'm talking about analysing the whole history).

[EDITED]:

maximo #:

if (today) todaysprofit += OrderProfit(); // sum profits & losses for today
else break; // no need to look at entire trade history once today is calculated

This may not work correctly in live trading, since it is not guaranteed that the orders in the history will follow in the order they were opened.

 

wow, so many support!

Thank you so much.

It is really helpful to study coding!

 
Vladislav Boyko #: since it is not guaranteed that the orders in the history will follow in the order they were opened.

The orders in history are closed orders. Nothing to do at all with their open time.

  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

    >R/O - 1,000 EUR/USD @0.52

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

 
William Roeder #:
Do not assume history is ordered by date, it's not.

That's exactly what I was trying to say.


William Roeder #:
Total Profit is OrderProfit() + OrderSwap() + OrderCommission()

This is what I have written about here:

Vladislav Boyko #:

Are swap and commission always greater than zero?

If so, then the MathAbs() is meaningless. If not, then you are misrepresenting profits.

I deliberately didn't say it. These were leading questions😄


Thank you William! :)