Download MetaTrader 5

How to detect order close by stoploss?

To add comments, please log in or register
amarsinghbishen
84
amarsinghbishen  

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  
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
84
amarsinghbishen  
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
1024
Tonny Obare  
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
15085
whroeder1  
  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
1024
Tonny Obare  

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
84
amarsinghbishen  
Thanks to all for their response..............
To add comments, please log in or register