How to caculate how many orders are closed by StopLoss or TakeProfit?

 

How to caculate how many orders are closed by StopLoss or TakeProfit?

I can use OrderClose to know how many orders or closed normally, but the order closed by StopLoss or TakeProfit can't be known through OrderClose.

Thanks.

 
Check the comment field.
 
Check all historyOrders !
 
phy:
Check the comment field.

It seems that comment can only be set in OrderSend, if I use OrderSelect, how can I choose comment I've set through OrderSelect?

 

Select an order, and OrderComment() will return the comment string.

Observe what you get for orders closed with TP or SL.

 
comments can only be manipulated by MetaTrader itself. As Phy said, just see how comments change throughout the Open and Close cycle. Try closing the orders with StopLoss and TakeProfit. later you can analyze the comment using OrderComment() after selecting the order.
 
I set comment with OrderSend and add Alert(OrderComment()) after every OrderClose and OrderModify, and found that there were always the origianl comment I set shown. Could you give me more details? Thanks again.
 
valleyfir wrote >>
I set comment with OrderSend and add Alert(OrderComment()) after every OrderClose and OrderModify, and found that there were always the origianl comment I set shown. Could you give me more details? Thanks again.

All the ordens closed by SL and TP will have an [sl] or [tp] added to you comment. Sometimes I see in my account also [tp/a] and [sl/a] and I dont know yet what it means :)

hope it helps

 
forex.sb:

All the ordens closed by SL and TP will have an [sl] or [tp] added to you comment. Sometimes I see in my account also [tp/a] and [sl/a] and I dont know yet what it means :)

hope it helps

There are still no result. I think i didn't put the Alert(OrderComment()) at a proper position. When StopLoss happened, there are no messages shown. Where should I put the Alert(OrderComment()) sentence?

 

To tell what trades were closed by SL or TP, you need to scan through the closed ( historical ) orders, select each one, and query for the comment.

Another method might be to compare the closing price to the stoploss or takeprofit settings, but that might be affected by slippage (?)

for(int i = OrdersHistoryTotal()-1; i >=0; i--){
   OrderSelect(i, SEL_BY_POS, MODE_HISTORY);
   if(StringFind(OrderComment(), "[tp", 0) >= 0){
      ... you found one, do something...
   }
   if(StringFind(OrderComment(), "[sl", 0) >= 0){
      ... you found one, do something...
   }
}
 
phy:

To tell what trades were closed by SL or TP, you need to scan through the closed ( historical ) orders, select each one, and query for the comment.

Thanks phy, it works!
Reason: