How to calculate total PnL in mql5

 
Hi there, any one can help with a well detailed code to calculate total profit and loss including swap and commission. Help will be much appreciated. 
 
//+------------------------------------------------------------------+
//| Calculate proftis and losses of the day                          |
//+------------------------------------------------------------------+
double PnL()
   {
    HistorySelect(TimeCurrent() - PERIOD_D1, TimeCurrent());

    double PnL = 0;

    for(int i = 0; i < HistoryDealsTotal(); i++)
       {
        if(HistoryDealGetTicket(i) > 0)
           {
            if(HistoryDealGetInteger(HistoryDealGetTicket(i), DEAL_ENTRY) == DEAL_ENTRY_OUT)
               {
                PnL += HistoryDealGetDouble(HistoryDealGetTicket(i), DEAL_PROFIT);
               }
           }
       }
    return (PnL);
   }
 
Carl Emil Bograd #:
//+------------------------------------------------------------------+
//| Calculate proftis and losses of the day                          |
//+------------------------------------------------------------------+
double PnL()
   {
    HistorySelect(TimeCurrent() - PERIOD_D1, TimeCurrent());

Current time minus a few seconds is nonsense.

  1. If you want the last twenty-four (24) hours, you need:

    HistorySelect(TimeCurrent() - PeriodSeconds(PERIOD_D1), TimeCurrent());
    
  2. If you want the current day, you need:

    HistorySelect(date(), TimeCurrent());

              Find bar of the same time one day ago - MQL4 programming forum #1 & #6

 
William Roeder #:

Current time minus a few seconds is nonsense.

  1. If you want the last twenty-four (24) hours, you need:

  2. If you want the current day, you need:

              Find bar of the same time one day ago - MQL4 programming forum #1 & #6

Oh yeah, right! My bad, thanks for pointing that out. Wrote it in a jif.

How about just:
HistorySelect(iTime(Symbol(), PERIOD_D1, 0), TimeCurrent());
Would return the actual current date PnL.