profit problem

 

In this code, after string dealComment = HistoryDealGetString(dealTicket, DEAL_COMMENT); , I print the value of dealComment , and it returns all the results correctly. However, totalProfit shows zero for all trades, even though it's actually not zero.

double CalculateExpertProfit(datetime startTime, datetime endTime)
{
   double totalProfit = 0.0;
   // Select history deals within specified time range
   if(HistorySelect(startTime, endTime))
   {
      int totalDeals = HistoryDealsTotal();
      
      for(int i = 0; i < totalDeals; i++)
      {
         ulong dealTicket = HistoryDealGetTicket(i);
         
         datetime dealTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME);
         // Although HistorySelect already restricts the deals, double-check the time range:
         if(dealTime >= startTime && dealTime <= endTime)
         {
            string dealComment = HistoryDealGetString(dealTicket, DEAL_COMMENT);
            
            if(StringFind(dealComment, EAComment) >= 0)
            
               totalProfit += HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
              
               
         }
      }
   }
   return(totalProfit);
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by...
 
Armin Abolfathi:
if(dealTime >= startTime && dealTime <= endTime)

This line is redundant because you have already selected deals in the time range.

Armin Abolfathi:
string dealComment = HistoryDealGetString(dealTicket, DEAL_COMMENT);

Comment is held in DEAL_IN while the selected deal may be DEAL_OUT.

Armin Abolfathi:
totalProfit += HistoryDealGetDouble(dealTicket, DEAL_PROFIT);

PnL is accessible via DEAL_OUT and not DEAL_IN.

So you will need to check deals and access comment through deal_in only. Added that comments can be altered by brokers and it is not suggested to rely on them.

Something like this(I did not test this):

string ReadComment(ulong ticket)
  {
   if(HistorySelectByPosition(ticket)==true)
     {
      ulong deal_ticket = HistoryDealGetTicket(0); //deal in is always the first deal
      return HistoryDealGetString(deal_ticket, DEAL_COMMENT);
     }
   return "";
  }

double CalculateExpertProfit(datetime startTime, datetime endTime)
  {
   double totalProfit = 0.0;
// Select history deals within specified time range
   if(HistorySelect(startTime, endTime))
     {
      int totalDeals = HistoryDealsTotal();

      for(int i = 0; i < totalDeals; i++)
        {
         ulong dealTicket = HistoryDealGetTicket(i);
         if(HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT)
            continue;

         ulong positionTicket = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
         string dealComment = ReadComment(positionTicket);
         if(StringFind(dealComment, EAComment) >= 0)
	    if(HistorySelect(startTime, endTime))
               if(HistoryDealSelect(i))
                  totalProfit += HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
        }
     }
   return(totalProfit);
  }