profit problem
if(dealTime >= startTime && dealTime <= endTime)
This line is redundant because you have already selected deals in the time range.
string dealComment = HistoryDealGetString(dealTicket, DEAL_COMMENT);
Comment is held in DEAL_IN while the selected deal may be DEAL_OUT.
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); }
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.