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?
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.
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: 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.)
- 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
- 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.
I want to know that how may I know my order is closed by stop loss or not?
Can anyone help?Thanks in advance.......