Retrieve SL/TP of closed position/deal (hedging mode)

 

Hi all,

for the purpose of history trade analysis (forex), I write a script to save screenshot of every closed trade to see its progress. I typically open a trade manually at market price and after that I manually set SL/TP and let it run... At the trade screenshot I have enabled the 'Trade history' arrows, I also position the open time to the chart center and I want to indicate open price and SL and TP levels using extra horizontal lines. However the issue is to retrieve the value of SL in case the trade hit TP and vice versa.

I have searched the forums for several days, but haven't found a way to achieve that. I'm familiar with the order/deal/position architecture.

When I look into MT5 terminal - History tab I can see that this information is available when using 'Positions' history (columns 'S/L' and 'T/P') - see attached screenshot 'MT5-History-Positions.png' 

As position history row should be a consolidated view on position-related orders and deals history, I change the history view to 'Deals' - and really the 'trade closing' deal (Direcion=out) contains both the SL/TP information as it was at the time the position was closed - see attached screenshot 'MT5-History-Deals.png'. So I want to retrieve this from the Deal history via mql5 script, but oops, there is not such property like SL or TP for a Deal! (see doc - https://www.mql5.com/en/docs/trading/historydealgetdouble).

I can only find SL/TP properties for an Order .. but the closing Order (triggered by SL or TP) does not contain such information - not surprisingly according to the order/deal philosophy - see attached screenshot 'MT5-History-Orders.png'.

Sure, if the closing order is triggered due to SL, the order price can be assumed an SL, but then there is no way to get the TP level and vice versa.

Is there anything I am missing? Or is it such a non-standard request to be able to retrieve the basic trade history details via script? For me as a forex trader the following is the key information for each trade - opening time/price, closing time/price, sl/tp levels (at close time - I don't change SL/TP).

Thanks for any help.


Just to be complete, I already explored these topics, but none of them lead to a solution to my problem:

Documentation on MQL5: Trade Functions / HistoryDealGetDouble
Documentation on MQL5: Trade Functions / HistoryDealGetDouble
  • www.mql5.com
HistoryDealGetDouble - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
This is a known old issue. And it still has no solution.
 
I was facing the same challenge and I found a way to preserve the information by storing them in the comment.

Although I am using an EA to open the positions, it should be a possible solution.

As an idea, as I was facing issues by retrieving the values again, there is a post in which I share exactly this code to read the values from the comment.

Here the link:
https://www.mql5.com/en/forum/361862

 
Dominik Egert:
I was facing the same challenge and I found a way to preserve the information by storing them in the comment.

Although I am using an EA to open the positions, it should be a possible solution.

As an idea, as I was facing issues by retrieving the values again, there is a post in which I share exactly this code to read the values from the comment.

Here the link:
https://www.mql5.com/en/forum/361862

Appreciate your idea and will definitely consider some workaround for my future trades.

But for the already closed trades, this would require editing of the comment first, which AFAIK is not possible.

Instead, since I have all the details recorded in my trade journal (Google sheet), I'm considering the following idea now:

  • load the trade data from the journal into a structure
  • use it to lookup missing SL/TP data
Reason: