Count Total Profit Closed Today in MQL5

 
Hello,

in Mql4, this code is work nice to calculate total closed profit today (current day), 

double Today_Closed_Profit()
{
    int time = 0;
    int order_nya = 0;
    double Profit_History = 0;
    datetime on11  = iTime(Symbol(),PERIOD_D1,0);// Current  open time
    datetime on111 = iTime(Symbol(),PERIOD_D1,1);// Previous open time
        for ( int i = OrdersHistoryTotal();i >= 1;i--) 
        {
            if(OrderSelect(i-1, SELECT_BY_POS, MODE_HISTORY) == false) continue;
            if(OrderSymbol () != Symbol()) continue;
            if (OrderComment() != Order__Comment_) continue;
            if(OrderCloseTime() <= on111) continue;
            if(OrderType() <= 1 && OrderCloseTime() >= on11) 
            {
                order_nya++;
                Profit_History += OrderProfit() + OrderSwap() + OrderCommission();
                
            }
        }
    return(Profit_History);
}

Then How in Mql5 code, ??,
Can someone help me, was try to make mql5 version like above code, but still confuse ing in mql5 version,


Regards,
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please insert the code correctly: when editing a message, press the button         Codeand paste your code into the pop-up window 
 
Vladimir Karputov #:
Please insert the code correctly: when editing a message, press the button         and paste your code into the pop-up window 
Ok, was type above from my mobile phone browser, there's no code insert button,

Will edit on my desktop PC.

Thank u
 
Syarif Nur Arief #:
Ok, was type above from my mobile phone browser, there's no code insert button,

Will edit on my desktop PC.

Thank u

Example:  Daily Trading Statistics:

Daily Trading Statistics

 

So below code to count closed profit current day is ok on mql5 :

   MqlDateTime SDateTime;
   TimeToStruct(TimeCurrent(),SDateTime);

   SDateTime.hour=0;
   SDateTime.min=0;
   SDateTime.sec=0;
   datetime  from_date=StructToTime(SDateTime);     // From date

   SDateTime.hour=23;
   SDateTime.min=59;
   SDateTime.sec=59;
   datetime  to_date=StructToTime(SDateTime);     // To date
   to_date+=60*60*24;

   HistorySelect(from_date,to_date);
   int trades_of_day=0;
   double wining_trade=0.0;
   double losing_trade=0.0;
   double total_profit=0.0;
   uint total=HistoryDealsTotal();
   ulong    ticket=0;
//--- for all deals
   for(uint i=0; i<total; i++)
     {
      //--- try to get deals ticket
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         long entry=HistoryDealGetInteger(ticket,DEAL_ENTRY);
         if(entry==DEAL_ENTRY_IN)
            continue;
         //--- get deals properties
         trades_of_day++;
         double deal_commission=HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         double deal_swap=HistoryDealGetDouble(ticket,DEAL_SWAP);
         double deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         double profit=deal_commission+deal_swap+deal_profit;
         if(profit>0.0)
            wining_trade+=profit;
         if(profit<0.0)
            losing_trade+=profit;
         total_profit+=profit;
        }
     }
 
Syarif Nur Arief # :

So below code to count closed profit current day is ok on mql5 :

Condition

         long entry=HistoryDealGetInteger(ticket,DEAL_ENTRY);
         if(entry==DEAL_ENTRY_IN)
            continue;

can be removed - as some brokers charge a commission for entering a trade.

 

Complete Function of Closed_Profit_Today(), Like this :

   
double Closed_Profit_Today()
{
   MqlDateTime SDateTime;
   TimeToStruct(TimeCurrent(),SDateTime);

   datetime SDateTime.hour=0;
   datetime SDateTime.min=0;
   datetime SDateTime.sec=0;
   datetime from_date=StructToTime(SDateTime);     // From date

   datetime SDateTime.hour=23;
   datetime SDateTime.min=59;
   datetime SDateTime.sec=59;
   datetime to_date=StructToTime(SDateTime);     // To date
   to_date+=60*60*24;

   HistorySelect(from_date,to_date);
   int    trades_of_day=0;
   double wining_trade=0.0;
   double losing_trade=0.0;
   double total_profit=0.0;
   uint   total=HistoryDealsTotal();
   ulong  ticket=0;
//--- for all deals
   for(uint i=0; i<total; i++)
     {
      //--- try to get deals ticket
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         ///////////----
         //--- get deals properties
         trades_of_day++;
         double deal_commission=HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         double deal_swap=HistoryDealGetDouble(ticket,DEAL_SWAP);
         double deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         double profit=deal_commission+deal_swap+deal_profit;
         if(profit>0.0)
            wining_trade+=profit;
         if(profit<0.0)
            losing_trade+=profit;
         total_profit+=profit;
        }
     }

return(total_profit);
}
 
Hello, once we have set the function Closed_Profit_Today, do we have to create a loop "for" ? because when I get total_profit I have 0.

thank you
Reason: