How to check if an order has been closed for stop loss

 

I'm writing an EA, modifying my EA written for MT4. I open an order using OrderSend. 

Simply if I need to understand (perhasps from History), if the order sent (by the way.... I have to check Order, Deals or Position?), has been closed because it has reached StopLoss.
I checked on forums, but I have not found what I need.... 

Using HistoryOrderGetDouble with paramter ENUM_ORDER_PROPERTY_DOUBLE

ORDER_PRICE_CURRENT

gives only the current price of the order symbol

 that cannot be compared to ORDER_SL. I suppose ORDER_PRICE_CURRENT is only the current price of symbol and not the Order close price .

Thanks to all for your support.

 

 

See this topic, probably a little confusing but you can find the idea.

The MT5 trading system is very different from MT4, so you have to change your thinking.

Detect stop trigger in OnTradeTransaction backtest
Detect stop trigger in OnTradeTransaction backtest
  • www.mql5.com
Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction. - - Category: technical indicators
 
frankge973:

I'm writing an EA, modifying my EA written for MT4. I open an order using OrderSend. 

Simply if I need to understand (perhasps from History), if the order sent (by the way.... I have to check Order, Deals or Position?), has been closed because it has reached StopLoss.
I checked on forums, but I have not found what I need.... 

Using HistoryOrderGetDouble with paramter ENUM_ORDER_PROPERTY_DOUBLE

ORDER_PRICE_CURRENT

gives only the current price of the order symbol

 that cannot be compared to ORDER_SL. I suppose ORDER_PRICE_CURRENT is only the current price of symbol and not the Order close price .

Thanks to all for your support.

 

 

Is it this?

//-------------------------------- ES CIERRE POR SL ----------------------------------------------
bool esCierreSL(int ind, int modoInd= SELECT_BY_TICKET, int modoHist= MODE_TRADES)
{
    bool resp= OrderSelect(ind, modoInd, modoHist)? (StringFind(OrderComment(), "[sl]", 0)>-1): false;
    return(resp);
}
 
Jose Miguel Soriano:

Is it this?

The question is about mql5, your code is mql4.

Order comment is not a reliable way to code.

 
Alain Verleyen:

The question is about mql5, your code is mql4.

Order comment is not a reliable way to code.

Sorry for necro, but this is quite interesting in my opinion.

I'm doing transition from mql4 to mql5, and i find the trading and history systems changes quite challenging.

Iin order to know if a trade on history book was closed for stop-loss, take-profit, or otherwise:

  • in MT4 you could comparate OrderStopLoss(), OrderTakeProfit() and OrderClosePrice(), or simply check OrderComment(). 
  • in MT5 the only possible way seems to me checking the comment of a deal: HistoryDealGetString(tk, DEAL_COMMENT)
Both MT4 and MT5 when an order closes for stop-loss or take-profit write [SL price] or [TP price] in the comment (in different ways.. but still).

I have read the article: https://www.mql5.com/en/forum/11996

It's interesting but if i understood it correctly, it's aimed on identifying trade changes based on event handling, on the fly.

So there must be a program listening (like an expert advisor running on a chart) or else nothing is detected.

In my opinion, trade close reason detection must be done on history trade book because it is (or should be) the safest way to know what happened, since it's writed somewhere and not catched from the air.

Is my analysis wrong? what do you think? 

Detect stop trigger in OnTradeTransaction backtest
Detect stop trigger in OnTradeTransaction backtest
  • www.mql5.com
Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction.
 
Marco: Both MT4 and MT5 when an order closes for stop-loss or take-profit write [SL price] or [TP price] in the comment (in different ways.. but still).
No, they do not. That is your broker.
Not a good idea, brokers can change comments, including complete replacement.
 
whroeder1:
No, they do not. That is your broker.

Ok, both MT4 and MT5 brokers put that string on the comment.. at least the few i have tested.

And i agree, this is not a good idea, since the comment may be subject to change by broker's hand.

But i fail to see how this could be done differently, since i can't find other traces in trade history book.

Is there a better way to achieve this goal? 

 

I contacted the service desk 2 years ago (regarding MT4).

...It is not clear to me whether all brokers incorporate the letters sp or tp in the order comment when a trade hits the StopLoss or TakeProfit. Is this at the brokers' discretion or is it automatically done with every broker?

and they replied

It is automatically done with every broker. And also the broker can change this value itself.

so it is done by the system, not the broker. Still not helpful if the broker can then change it though.

Mind you, I have not noticed any occasion when the broker has changed the order comment.

Usually, in my code, it is more important to know whether a trade closed with profit or loss. When using a trailing stop a trade can hit the SL, but still be in profit.

 
Keith Watford:

I contacted the service desk 2 years ago (regarding MT4).

and they replied

so it is done by the system, not the broker. Still not helpful if the broker can then change it though.

Mind you, I have not noticed any occasion when the broker has changed the order comment.

Usually, in my code, it is more important to know whether a trade closed with profit or loss. When using a trailing stop a trade can hit the SL, but still be in profit.

Thank you Keith, that's what i have hoped for, since i can't found a better workaround. 

 
honest_knave:
I believe both posters are converting MQL4 into MQL5

Anyway, just compare close price with stoploss price.

Note: In real (especially ECN) account, probably trigger price has a gap between pre-set stoploss price, so use "<=" or ">=".

 
Xiangdong Guo:

Anyway, just compare close price with stoploss price.

Note: In real (especially ECN) account, probably trigger price has a gap between pre-set stoploss price, so use "<=" or ">=".

You cannot compare the close price to the stop loss price. Slippage means that it could close higher or lower than the stop loss.

So "<=" or ">=" is not useful.

Reason: