Checking if Trade is closed on stoploss or takeprofit

 

Hi,

I would like to ask if someone has already tried to accurately check if a certain trade is closed by stoploss or takeprofit. I have a function that deals with this but its not that accurate because it checks the orderhistory then checks the comment if it has "tp" or "sl" on it but the problem with that one is i want to have a specific trade that if that trade will be closed either sl or tp, it will open another trade.


Thanks...

 
  1. Some brokers modify comment with tp/sl; others do not. Not a good idea, brokers can change comments, including complete replacement.
  2. There is no accurate way to check; you can guess if the close price is closer to TP vs SL but orders can also be closed by other means.
  3. if that trade will be closed either sl or tp
    Makes no sense; the future is unknown.
 

There is a fonction which indicate how the orders was closed, in this article :

https://www.mql5.com/en/articles/1399

 
ffoorr: There is a fonction which indicate how the orders was closed,

If you had bothered to read the article all it does is detect a change in orders (pending to open, closed or deleted)

Nothing there is about SL/TP

 

Well to me this problem is quite easy to solve:

1) define a 3-dim. array (double): {ticket,sl,tp,...}

2) if you open a new pos enter ticket, sl and tp.

3) If it was closed compare the exit price against sl and tp...

- Or you directly compare the OrderClosePrice() against OrderStoppLoss() and OrderTakeProfit()

 

A couple of complications to consider:

  • Slippage
  • Manually closing an order near to a TP or SL

 
gooly:

Well to me this problem is quite easy to solve:

1) define a 3-dim. array (double): {ticket,sl,tp,...}

2) if you open a new pos enter ticket, sl and tp.

3) If it was closed compare the exit price against sl and tp...

- Or you directly compare the OrderClosePrice() against OrderStoppLoss() and OrderTakeProfit()


Gooly, this method is also indicated in the comments of the article,


whroeder, yes, the fonction do indicate if the order were closed by SL or TP, see the expert provided with the article

 

But yet I feel we're still only making a good approximation of whether the trade was closed by SL or TP.

AFAIK, there is no way to tell definitively (using MQL) what method was used to close an order.

IIRC, the order history shows a color change if the close price was at TP (green) or SL (red) but I don't think there is a way to access that directly from MQL.

 

the easy way seem to be this one :


if ( MathAbs( OrderClosePrice() - OrderStopLoss() ) < Point ) // closed by SL
if ( MathAbs( OrderClosePrice() - OrderTakeProfit() ) < Point ) // closed by TP
https://www.mql5.com/en/articles/1399

 

Unfortunately it still isn't conclusive.

What if the trade was manually closed within a point on the SL or TP?

What if the trade was closed by SL or TP but the slippage was more than a point?

 

MT4 program is encapsuled, no one know what the program do,

May be it work you will have to test to know.  ;-))

Reason: