How to find if a trade hit SL or TP?

 

Sorry if I have missed this somewhere in the documentation.

Sometimes in my EAs, I will want to do something if a trade is closed by hitting the SL or TP.

I would like to know if there is a foolproof way of determining this?

I know that some brokers will add "sl or "tp" to the comment field, but I don't know whether this is true of all brokers.

Most of the time, the OrderClosePrice will be within a pip or 2 of the OrderStopLoss or OrderTakeProfit, but there may occasionally be large slippage, so

if(MathAbs(OrderClosePrice()-OrderStopLoss() )<=2*Pips)

may not always do the job.

 

For buy trades:

  • if the OrderClosePrice() is equal to or greater than the OrderTakeProfit(), the trade hit the TP.
  • if the OrderClosePrice() is equal to or less than the OrderStopLoss(), the trade hit the SL.
For sell trades:

  • if the OrderClosePrice() is equal to or less than the OrderTakeProfit(), the trade hit the TP.
  • if the OrderClosePrice() is equal to or greater than the OrderStopLoss(), the trade hit the SL.
Slippage would be the deviation between the OrderClosePrice() and either OrderTakeProfit() or OrderStopLoss().

 
Thirteen:

For buy trades:

  • if the OrderClosePrice() is equal to or greater than the OrderTakeProfit(), the trade hit the TP.
  • if the OrderClosePrice() is equal to or less than the OrderStopLoss(), the trade hit the SL.
For sell trades:

  • if the OrderClosePrice() is equal to or less than the OrderTakeProfit(), the trade hit the TP.
  • if the OrderClosePrice() is equal to or greater than the OrderStopLoss(), the trade hit the SL.
Slippage would be the deviation between the OrderClosePrice() and either OrderTakeProfit() or OrderStopLoss().


Just to be sure, in the case of a buy, there will never be negative slippage when hitting the TP? The closing price will always be equal to or higher than the TP?
 
GumRai:

Just to be sure, in the case of a buy, there will never be negative slippage when hitting the TP? The closing price will always be equal to or higher than the TP?

Negative? The broker already got their cut.

The closing price will always equal to TP but you have to factor in commissions and so on. So, you might get less but not much. Most of the time you got what you want and it is exact though.

 
GumRai:

Sorry if I have missed this somewhere in the documentation.

Sometimes in my EAs, I will want to do something if a trade is closed by hitting the SL or TP.

I would like to know if there is a foolproof way of determining this?

I know that some brokers will add "sl or "tp" to the comment field, but I don't know whether this is true of all brokers.

Most of the time, the OrderClosePrice will be within a pip or 2 of the OrderStopLoss or OrderTakeProfit, but there may occasionally be large slippage, so

may not always do the job.

I do it by using a two array system

I put all new tickets in first array when order is opened.

When order meets critera for OrderClose delete it from first array and move it to second array to be worked on by Order Close function

That leaves only tickets in the first array that are not to be closed by EA

On Tick event before any of the above, loop through that first array, by select by ticket. If OrderCloseTime() is > 0, that order has definately closed by SL/TP

There is still a slim chance that an order was in the first array and still open when array was checked, AND that order meets closing critera on the same tick as SL/TP is hit AND order was subsequntly closed by TP/SL during that tick, AND Order ticket was moved to second array for closing, so to cover that possibility error handling will get a invalid ticket error when OrderClose() tries to close it. If that happens you can test if order was already closed, if it was it had to be a SL/TP.

Thats about the closest to foolproof I could come up with lol

The one thing I really dont know what to do about is if orderclose is trying to close an order and gets no response from the server even though close was successful.

 
deysmacro:

Negative? The broker already got their cut.

The closing price will always equal to TP but you have to factor in commissions and so on. So, you might get less but not much. Most of the time you got what you want and it is exact though.


You have misunderstood or maybe I didn't explain very well :).

What I am asking is, if price hits TP on a buy order, the broker will pass that trade to the bank. In times of high volatility, the price may have dropped by X pips, so the bank will either requote or will fill the order at the lower price. I don't really know how it works. If the bank fills the order at a lower price, I really can't see the broker absorbing your loss, so I imagine that it is possible for an order to hit TP, but be settled at a lower price.

I'm not interested in commissions etc, I just want to be sure of knowing whether a trade hit SL or TP

 
Well, my broker, if I set TP at certain price, it hits. However, on high volatility, it might missed, say, 1 pip or 2, but the event is very rare. Basically less than 1% of my total trades.
 

Weekend gaps can mess up the predicted SL/TP level.

 
SDC:

I do it by using a two array system

I put all new tickets in first array when order is opened.

When order meets critera for OrderClose delete it from first array and move it to second array to be worked on by Order Close function

That leaves only tickets in the first array that are not to be closed by EA

On Tick event before any of the above, loop through that first array, by select by ticket. If OrderCloseTime() is > 0, that order has definately closed by SL/TP

There is still a slim chance that an order was in the first array and still open when array was checked, AND that order meets closing critera on the same tick as SL/TP is hit AND order was subsequntly closed by TP/SL during that tick, AND Order ticket was moved to second array for closing, so to cover that possibility error handling will get a invalid ticket error when OrderClose() tries to close it. If that happens you can test if order was already closed, if it was it had to be a SL/TP.

Thats about the closest to foolproof I could come up with lol

The one thing I really dont know what to do about is if orderclose is trying to close an order and gets no response from the server even though close was successful.


It's a solution, but the problem is when the user closes a trade manually without it actually hitting either SL or TP.
 

Just for information, there is now the OnTrade() event : https://forum.mql4.com/61012

 
It's a solution, but the problem is when the user closes a trade manually without it actually hitting either SL or TP. 

And here I thought that you are talking about SL/TP settings which broker will honor or not.

Reason: