MT5 Position History Information

 

Hello,

I am struggling to find a way to get Position history data using MQL5, specifically finding the final stop loss value.

It seems that using HistoryOrder and HistoryDeal functions cannot find the last stop loss value which as far as I remember was possible on MQL4 by using 

OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)

Please advise how I can retrieve the final stop loss of a closed trade on MQL5, thanks!

 
  1. Select the History by the position identifier — HistorySelectByPosition
  2. Scan the Deals — HistoryDealsTotal, HistoryDealGetTicket
  3. Find the deals where the "reason" is hitting stop-loss. — HistoryDealGetInteger, DEAL_REASON = DEAL_REASON_SL
  4. Retrieve the deal price (i.e. stop-loss price) — HistoryDealGetDouble, DEAL_PRICE
EDIT: Attention! Depending on market/exchange type and/or filling mode, you have may multiple deals at different prices for hitting the stop-loss.
 
Fernando Carreiro #:
  1. Select the History by the position identifier — HistorySelectByPosition
  2. Scan the Deals — HistoryDealsTotal, HistoryDealGetTicket
  3. Find the deals where the "reason" is hitting stop-loss. — HistoryDealGetInteger, DEAL_REASON = DEAL_REASON_SL
  4. Retrieve the deal price (i.e. stop-loss price) — HistoryDealGetDouble, DEAL_PRICE
EDIT: Attention! Depending on market/exchange type and/or filling mode, you have may multiple deals at different prices for hitting the stop-loss.

I see, but if I wanted to get the stop loss value of ALL trades regardless of how they were closed to be able to provide the same data as in the client history tab for Positions:

Open Time Position Symbol Type Volume Open Price S / L T /P Close Time Close Price Commission Swap Profit
2022.09.07 13:47:00 32482 AUDUSD buy 0.02 0.67196 0.67230 0.00000 2022.09.07 14:47:14 0.67228 -0.08 0 0.64
 

Forum on trading, automated trading systems and testing trading strategies

OrderCloseTime Expert Advisor MQL5

fxsaber, 2018.07.05 22:40

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY))
  {
    Print(OrderStopLoss());
    
    OrderPrint();
  }
}
 
fxsaber #:

Yep, this returns zero despite the only trade on this test account having a closing SL of 1.34189:

2023.02.07 17:39:50.439 Close SL (EURUSD,H1) 0.0

2023.02.07 17:39:50.439 Close SL (EURUSD,H1) #1141714 2023.02.05 22:24:00.195 sell 40.54 USDCAD 1.34061 0.00000 0.00000 2023.02.05 22:24:19.353 1.34066 -134.52 0.00 -125.44 80011807 0


 
Ben Hardman #:

Yep, this returns zero despite the only trade on this test account having a closing SL of 1.34189:

Download the latest version of the library.

 
Done that. Still returns 0.
 
Ben Hardman #:
Done that. Still returns 0.
#property script_show_inputs

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

input long inPositionTicket = 0; // Position's Ticket

void OnStart()
{
  Print(__MT4ORDERS__);
  Print(TerminalInfoInteger(TERMINAL_BUILD));
  
  if (OrderSelect(inPositionTicket, SELECT_BY_TICKET))
  {
    OrderPrint();
    
    Print(OrderStopLoss());
    Print(EnumToString(OrderCloseReason()));
  }
}

2022.08.29
3577
#50348403263 2023.02.08 08:58:44.851 buy 0.01 EURUSD 1.07296 1.07292 0.00000 2023.02.08 08:59:03.424 1.07292 0.00 0.00 -0.04 [sl 1.07292] 0
1.07292
DEAL_REASON_SL
 
fxsaber #:


Ah. Does this only work if the trade was closed by stop loss?

 
Ben Hardman #:

Ah. Does this only work if the trade was closed by stop loss?

Any. Like MT4.

Reason: