Download MetaTrader 5

How to detect order close by stoploss?

To add comments, please log in or register
Add our Twitter channel to friends and follow the news!
amarsinghbishen
69
amarsinghbishen 2013.11.28 14:54 

I want to know that how may I know my order is closed by stop loss or not?

Can anyone help?Thanks in advance.......

GreenMoney
379
GreenMoney 2013.11.28 15:06  
amarsinghbishen:

I want to know that how may I know my order is closed by stop loss or not?

Use OrderSelect() to select the order and then compare the OrderClosePrice() to the OrderStopLoss().

  • if the order was an OP_BUY and the OrderClosePrice <= OrderStopLoss, the order was closed because the Bid price hit the order's stoploss price.
  • if the order was an OP_SELL and the OrderClosePrice >= OrderStopLoss, the order was closed because the Ask price hit the order's stoploss price.
amarsinghbishen
69
amarsinghbishen 2013.11.28 15:08  
Thirteen:

Just loop through the order history using OrderSelect() and compare the OrderClosePrice() to the OrderStopLoss().

  • if the order was an OP_BUY and the OrderClosePrice <= OrderStopLoss, the order was closed because the Bid price hit the order's stoploss price.
  • if the order was an OP_SELL and the OrderClosePrice >= OrderStopLoss, the order was closed because the Ask price hit the order's stoploss price.

Thank you very much Thirteen.......
Tonny Obare
1018
Tonny Obare 2013.11.28 15:53  
Simple if OrderClosePrice()==OrderStopLoss() then it hit SL and if OrderClosePrice()==OrderTakeProfit() it hit TP while if OrderClosePrice() is not equal to OrderStopLoss() and also not equal to OrderTakeProfit() then it was most likely closed manually or some people might say broker conspiracy.
whroeder1
14480
whroeder1 2013.11.28 16:14  
  1. tonny: Simple if OrderClosePrice()==OrderStopLoss()
    They will NEVER be equal (except in the tester.) Once close price hits or passes the SL, the SL becomes a market order and closes at the current market. (On a gap down it may close very far below SL.)
  2. Thirteen: if the order was an OP_BUY and the OrderClosePrice <= OrderStopLoss, the order was closed because the Bid price hit the order's stoploss price.
    It is not guaranteed that OrderClosePrice <= OrderStopLoss because of slippage. On news, the SL may be triggered but close very far above SL
  3. amarsinghbishen: I want to know that how may I know my order is closed by stop loss or not?
    If you're not also closing orders (only TP/SL) then use
    bool isSL = MathAbs( OrderClosePrice() - OrderStopLoss() ) < MathAbs( OrderClosePrice() - OrderTakeProfit() );
    If you are also closing order either remember it or zero TP/SL and then close.
Tonny Obare
1018
Tonny Obare 2013.11.29 05:56  

I have a code that works well so far using that way to check if hit sl. The slippage issue is rare or maybe if on a slow broker server. If you check your history for trades that hit stops you'll see this. Here is the proof of a trade that hit TP.

amarsinghbishen
69
amarsinghbishen 2013.11.29 12:08  
Thanks to all for their response..............
To add comments, please log in or register