How to detect order close by stoploss?

 

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

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

 
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.
 
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.......
 
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.
 
  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.
 

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.

 
Thanks to all for their response..............
 
 Should I use for loop to to select an order if I want to check for closed orders 
Reason: