Calculate previous month's profit

 

Hello everybody.

I developed the code below to return last month's profits. For some reason beyond my knowledge, the returned value is different from the MT5 one.

Could someone tell me where I'm going wrong?

//---
double GetProfit()
  {
   double profit = 0;

//---
   int timeDay         = 24 * 60 * 60;
   datetime startMonth = iTime(_Symbol, PERIOD_MN1, 1);
   datetime endMonth   = iTime(_Symbol, PERIOD_MN1, 0) - timeDay;

   if(HistorySelect(startMonth, endMonth))
     {
      int total = HistoryDealsTotal();

      for(int i = 0; i < total; i++)
        {
         ulong dealTicket = HistoryDealGetTicket(i);

         if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
           {
            datetime dealTime  = datetime(HistoryDealGetInteger(dealTicket, DEAL_TIME));

            if(dealTime >= startMonth && dealTime <= endMonth)
              {
               double dealProfit     = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
               double dealCommission = HistoryDealGetDouble(dealTicket, DEAL_COMMISSION);
               double dealSwap       = HistoryDealGetDouble(dealTicket, DEAL_SWAP);

               profit += dealProfit + dealSwap + dealCommission;
              }
           }
        }
     }

   return profit;
  } 
 
This could be because there is a commission in entry deals also.
 
Yashar Seyyedin #:
This could be because there is a commission in entry deals also.

But the data I am recovering is already realized data. I'm missing somewhere, I just need to know where. Any idea?

 
//---
double GetProfit()
  {
   double profit = 0;

//---
   int timeDay         = 24 * 60 * 60;
   datetime startMonth = iTime(_Symbol, PERIOD_MN1, 1);
   datetime endMonth   = iTime(_Symbol, PERIOD_MN1, 0) - timeDay;

   if(HistorySelect(startMonth, endMonth))
     {
      int total = HistoryDealsTotal();

      for(int i = 0; i < total; i++)
        {
         ulong dealTicket = HistoryDealGetTicket(i);

         if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
           {
            datetime dealTime  = datetime(HistoryDealGetInteger(dealTicket, DEAL_TIME));

            if(dealTime >= startMonth && dealTime <= endMonth)
              {
               double dealProfit     = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
               double dealCommission = HistoryDealGetDouble(dealTicket, DEAL_COMMISSION);
               double dealSwap       = HistoryDealGetDouble(dealTicket, DEAL_SWAP);

               profit += dealProfit + dealSwap + dealCommission;
              }
           }
         if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_IN)
           {
            datetime dealTime  = datetime(HistoryDealGetInteger(dealTicket, DEAL_TIME));

            if(dealTime >= startMonth && dealTime <= endMonth)
              {
               double dealCommission = HistoryDealGetDouble(dealTicket, DEAL_COMMISSION);

               profit += dealCommission;
              }
           }
        }
     }

   return profit;
  } 
 
Yashar Seyyedin #:

It worked. Thanks.

Reason: