How to get take profit level of closed position?

 

To get the current TP of an open position (hedging mode), I use

if (PositionSelectByTicket(positionId)) { 
   return PositionGetDouble(POSITION_TP); 
} 

Once the position is closed, I search for the corresponding ENTRY_IN deal, and query the corresponding order for the TP:

if (HistorySelectByPosition(positionId)) { 
   for (int i = HistoryDealsTotal()-1; i >= 0; i--) { 
      ulong dealId = HistoryDealGetTicket(i); 
      if (HistoryDealGetInteger(dealId, DEAL_POSITION_ID) != positionId) continue; 
      if ((ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealId, DEAL_ENTRY) != DEAL_ENTRY_IN) continue; 
      ulong orderId = HistoryDealGetInteger(dealId, DEAL_ORDER); 
      if (HistoryOrderSelect(orderId)) { 
         return HistoryOrderGetDouble(orderId, ORDER_TP); 
      } 
   } 
}

However, this only works if the TP was set directly while opening the position. If the TP was set later, this does not work. I examined all deals and orders corresponding to the positionId , but was not able to find the TP anywhere.

What is a robust method to obtain the TP of a closed position?

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

Position modifications are not saved in the trading history.

Example:

1. Open Sell 0.03

2. Modify TP 100 Points

3. Close Sell 0.03


And now the request for the trade history (deals and orders) using the History Deals and Orders code.

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1214696126          |1231936756          |2022.01.03 06:37:47 |1641191867528       |DEAL_TYPE_SELL      |DEAL_ENTRY_IN       |0                   |DEAL_REASON_CLIENT  |1231936756         
|Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.03                |1.13471             |0.00                |0.00                |0.00                |EURUSD              |                                         |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1231936756          |2022.01.03 06:37:47 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2022.01.03 06:37:47 |1641191867527       |1641191867528       |ORDER_FILLING_FOK  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |0                   |ORDER_REASON_CLIENT |1231936756          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.03                |0.00                |1.13471             |0.00000             |0.00000             |1.13471             |0.00000            
|Symbol              |Comment                                  |External id        
|EURUSD              |                                         |                   

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1214696255          |1231936894          |2022.01.03 06:38:43 |1641191923740       |DEAL_TYPE_BUY       |DEAL_ENTRY_OUT      |0                   |DEAL_REASON_CLIENT  |1231936756         
|Volume              |Price               |Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.03                |1.13473             |0.00                |0.00                |-0.06               |EURUSD              |                                         |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1231936894          |2022.01.03 06:38:43 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2022.01.03 06:38:43 |1641191923740       |1641191923740       |ORDER_FILLING_FOK  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |0                   |ORDER_REASON_CLIENT |1231936756          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.03                |0.00                |1.13473             |0.00000             |0.00000             |1.13473             |0.00000            
|Symbol              |Comment                                  |External id        
|EURUSD              |                                         |                   

As you can see, there is no mention of position modifications in the trading history.

History Deals and Orders
History Deals and Orders
  • www.mql5.com
The 'History Deals and Orders' script shows absolutely all properties of deals and orders from the trading history over a given time interval.
 

Forum on trading, automated trading systems and testing trading strategies

New MetaTrader 5 platform build 3081: Improvements in MQL5 services and design updates

MetaQuotes, 2021.09.24 16:48

  1. MQL5: Added DEAL_SL and DEAL_TP values in the ENUM_DEAL_PROPERTY_DOUBLE enumeration — the Stop Loss and Take Profit levels of a deal.

    The relevant values for entry and reversal deals are set in accordance with the Stop Loss/Take Profit of orders, which initiated these deals. The Stop Loss/Take Profit values ​​of appropriate positions as of the time of position closure are used for exit deals.
 
fxsaber # :

Thanks! Somehow I did not pay attention.

 

History Deals and Orders version 1.007 (version 1.007 is available for download from the RUSSIAN part of CodeBase)

Example:

1. Open Sell 0.03

2. Modify TP 100 Points

3. Close Sell 0.03

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1214696126          |1231936756          |2022.01.03 06:37:47 |1641191867528       |DEAL_TYPE_SELL      |DEAL_ENTRY_IN       |0                   |DEAL_REASON_CLIENT  |1231936756         
|Volume              |Price               |Stop Loss           |Take Profit        
|0.03                |1.13471             |0.00000             |0.00000             
|Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.00                |0.00                |0.00                |EURUSD              |                                         |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1231936756          |2022.01.03 06:37:47 |ORDER_TYPE_SELL     |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2022.01.03 06:37:47 |1641191867527       |1641191867528       |ORDER_FILLING_FOK  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |0                   |ORDER_REASON_CLIENT |1231936756          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.03                |0.00                |1.13471             |0.00000             |0.00000             |1.13471             |0.00000            
|Symbol              |Comment                                  |External id        
|EURUSD              |                                         |                   

Deal:
|Ticket              |Order               |Time                |Time msc            |Type                |Entry               |Magic               |Reason              |Position ID        
|1214696255          |1231936894          |2022.01.03 06:38:43 |1641191923740       |DEAL_TYPE_BUY       |DEAL_ENTRY_OUT      |0                   |DEAL_REASON_CLIENT  |1231936756         
|Volume              |Price               |Stop Loss           |Take Profit        
|0.03                |1.13473             |0.00000             |1.13271             
|Commission          |Swap                |Profit              |Symbol              |Comment                                  |External ID        
|0.00                |0.00                |-0.06               |EURUSD              |                                         |                   
Order:
|Ticket              |Time setup          |Type                |State               |Time expiration     |Time done           |Time setup msc      |Time done msc       |Type filling       
|1231936894          |2022.01.03 06:38:43 |ORDER_TYPE_BUY      |ORDER_STATE_FILLED  |1970.01.01 00:00:00 |2022.01.03 06:38:43 |1641191923740       |1641191923740       |ORDER_FILLING_FOK  
|Type time           |Magic               |Reason              |Position id         |Position by id     
|1970.01.01 00:00:00 |0                   |ORDER_REASON_CLIENT |1231936756          |0                  
|Volume initial      |Volume current      |Open price          |sl                  |tp                  |Price current       |Price stoplimit    
|0.03                |0.00                |1.13473             |0.00000             |0.00000             |1.13473             |0.00000            
|Symbol              |Comment                                  |External id        
|EURUSD              |                                         |                   

Pay attention: now you can read Take Profit for the deal!
History Deals and Orders
History Deals and Orders
  • www.mql5.com
The 'History Deals and Orders' script shows absolutely all properties of deals and orders from the trading history over a given time interval.
 
fxsaber #:

@ fxsaber Thank you! Indeed, the DEAL_TP property of the DEAL_ENTRY_OUT deal contains the last set TP, regardless whether it was set immediately when the order was opened, or whether it was later updated. No need to examine the order associated with the deal.

Exactly what I was looking for. Thank you for your help!