MT5 Python Library - How to get the FINAL stop loss price of a trade


The MetaTrader for Python documentation shows users can only use the history_orders_get and history_deals_get functions to access historical trade data, rather than a simple history_positions_get function.

On MT5, trades are split into 5 elements;

  1. Position
  2. 'In' Order
  3. 'In' Deal
  4. 'Out' Order
  5. 'Out' Deal

This can be visualised better from the MT Manager View

This is fine for execution trades etc, but for users wanting to create custom account reports in python, knowing the final stop loss value when the position was closed is valuable.

On the MetaTrader for Python library, it seems we only have access to the orders and deals, and so here is all the history data I can retrieve for position 275:

TradeOrder(ticket=275, time_setup=1657825200, time_setup_msc=1657825200024, time_done=1657825200, time_done_msc=1657825200053, time_expiration=0, type=0, type_time=0, type_filling=0, state=4, magic=0, position_id=275, position_by_id=0, reason=3, volume_initial=12.5, volume_current=0.0, price_open=0.0, sl=138.876, tp=140.266, price_current=139.154, price_stoplimit=0.0, symbol='EURJPY', comment='Long Breakout PERIOD_H1', external_id='')

TradeDeal(ticket=285, order=275, time=1657825200, time_msc=1657825200053, type=0, entry=0, magic=0, position_id=275, reason=3, volume=12.5, price=139.154, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='EURJPY', comment='Long Breakout PERIOD_H1', external_id='')

TradeOrder(ticket=276, time_setup=1657868538, time_setup_msc=1657868538652, time_done=1657868538, time_done_msc=1657868538652, time_expiration=0, type=1, type_time=0, type_filling=1, state=4, magic=0, position_id=275, position_by_id=0, reason=4, volume_initial=12.5, volume_current=0.0, price_open=139.243, sl=0.0, tp=0.0, price_current=139.243, price_stoplimit=0.0, symbol='EURJPY', comment='[sl 139.243]', external_id='')

TradeDeal(ticket=286, order=276, time=1657868538, time_msc=1657868538652, type=1, entry=1, magic=0, position_id=275, reason=4, volume=12.5, price=139.243, commission=0.0, swap=-45.15, profit=800.41, fee=0.0, symbol='EURJPY', comment='[sl 139.243]', external_id='')
As you can see, only the Order data gives info on the INITIAL SL position, and only for the In Order. For this trade, the only SL price given is 138.876, however when looking at the POSITION from an MT5 statement, we can see that the position was closed with a stop loss of 139.243:

MT5 Report

Technically speaking, the final stop loss SHOULD be available on the 'Out' Deal, but for some reason you cannot extract the SL or TP values from the Deals, just Orders.

I would appreciate some ideas on how to get the final stop loss price if it is possible to do this!

Thanks in advance.

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5