How to detect order close by stoploss?

87

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

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

379

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

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

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

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

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.

87

Thanks to all for their response..............