How to get the history profits by comment?

 

I use the code, got the profits = 0.

Please help me correct it.

thank you.

#property script_show_inputs

input string   com="break";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double profit=0; int pos=0;
   
   datetime start=TimeCurrent()-PeriodSeconds(PERIOD_MN1);
   
   if(HistorySelect(start, TimeCurrent()))
    
   for(int i = 0; i<HistoryDealsTotal(); i++)
      {
       ulong tik = HistoryDealGetTicket(i);
          
       if(tik>0 && HistoryDealGetString(tik,DEAL_COMMENT)==com)
         {
          pos ++;
          profit += HistoryDealGetDouble(tik,DEAL_PROFIT);
         }
      }
      
   Print(com+" profit sum = ",profit,",  pos = ",pos);
  }
//+------------------------------------------------------------------+
 
This question is probably a bit difficult, can anyone help me?
 
sdlg #:
This question is probably a bit difficult, can anyone help me?

In MT4, it can be easily solved, but in MT5, it becomes a big mountain, complicating many simple methods. This is evolution? No wonder people are unwilling to give up on MT4.

 
sdlg #:

In MT4, it can be easily solved, but in MT5, it becomes a big mountain, complicating many simple methods. This is evolution? No wonder people are unwilling to give up on MT4.

My friend you need to debug your code. 

Are you sure there are orders out there with the comment you selected. If yes select some of them directly with their ticket and log their saved data and see what happens. Eventually you will find where the problem resides.

 
sdlg #:

In MT4, it can be easily solved, but in MT5, it becomes a big mountain, complicating many simple methods. This is evolution? No wonder people are unwilling to give up on MT4.

Don't confuse deal and position, these are two different concepts. I've never done this before so I can't help you any further. But pay attention to this.

This is Position history:

And these are deals:

That's why profit returns 0

 

In the Documentation they said: 

Do not confuse ordersdeals and positions. Each deal is the result of the execution of an order, each position is the summary result of one or more deals.

https://www.mql5.com/en/docs/trading/historydealgetinteger

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I did not test this... Something like this may help.

You should check deal_in for comment and deal_out for profit:

bool GetPnLWithComment(double &TotalUnrealizedProfit, string _comment)
{
   if(HistorySelect(0, TimeCurrent())==false) 
   {
      Print("Error History Select...");
      return false;
   }

   int total = HistoryDealsTotal();
   TotalUnrealizedProfit=0;
   for(int i = 0; i < total; i++) //iterate to find the out deals
   {
      ulong out_dealTicket = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(out_dealTicket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue;
      double profit=HistoryDealGetDouble(out_dealTicket, DEAL_PROFIT);
      ulong positionTicket=HistoryDealGetInteger(out_dealTicket, DEAL_POSITION_ID);
      if(HistorySelectByPosition(positionTicket)==true)
      {
         int _total = HistoryDealsTotal();
         for(int j = 0; j < _total; j++) //iterate to find the coressponding in deal
         {
            ulong in_dealTicket = HistoryDealGetTicket(j);
            if(HistoryDealGetInteger(in_dealTicket, DEAL_ENTRY) != DEAL_ENTRY_IN) continue;
            if(HistoryDealGetString(in_dealTicket, DEAL_COMMENT) != _comment) continue;
            TotalUnrealizedProfit+=profit;
            break;
         }
      }
      HistorySelect(0, TimeCurrent());
   }
   return true;
}
 
Yashar Seyyedin #:

I did not test this... Something like this may help.

You should check deal_in for comment and deal_out for profit:

Thank you for your reply. However, the problem remains unresolved. Because the closing profit is in the 'out'order, while the 'comment' is in the 'in' order, and these two orders cannot be linked, the fatal point is here. MT5 is not as straightforward in historical records as MT4.

 
sdlg #:

Thank you for your reply. However, the problem remains unresolved. Because the closing profit is in the 'out'order, while the 'comment' is in the 'in' order, and these two orders cannot be linked, the fatal point is here. MT5 is not as straightforward in historical records as MT4.

They are linked. Please study the code I shared closely.
 

Check this documentation page to see how they are linked:

https://www.mql5.com/en/docs/trading/historyselectbyposition

Documentation on MQL5: Trade Functions / HistorySelectByPosition
Documentation on MQL5: Trade Functions / HistorySelectByPosition
  • www.mql5.com
HistorySelectByPosition - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yashar Seyyedin #:
They are linked. Please study the code I shared closely.

Great!yes, solved. 

Thank you so much !

Reason: