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.
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 orders, deals and positions. Each deal is the result of the execution of an order, each position is the summary result of one or more deals.
- www.mql5.com
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; }
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.
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.
Check this documentation page to see how they are linked:
https://www.mql5.com/en/docs/trading/historyselectbyposition
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I use the code, got the profits = 0.
Please help me correct it.
thank you.