What is the right way to detect a stop loss order triggered?
enum postype {BUY,SELL,NONE}; static int prevposition=NONE; void OnTradeTransaction( const MqlTradeTransaction& trans, // estrutura das transações de negócios const MqlTradeRequest& request, // estrutura solicitada const MqlTradeResult& results // resultado da estrutura ) { HistorySelect(TimeCurrent()-3600,TimeCurrent()); if(trans.type==TRADE_TRANSACTION_DEAL_ADD && trans.deal_type==DEAL_TYPE_SELL && PositionsTotal()==0 && prevposition==BUY) { if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_TP){ Print("BUY + DEAL_REASON_TP"); prevposition=NONE;} if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_SL){ Print("BUY + DEAL_REASON_SL") prevposition=NONE;} } if(trans.type==TRADE_TRANSACTION_DEAL_ADD && trans.deal_type==DEAL_TYPE_BUY && PositionsTotal()==0 && prevposition==SELL) { if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_TP){ Print("SELL + DEAL_REASON_TP"); prevposition=NONE;} if(HistoryDealGetInteger(trans.deal,DEAL_REASON)==DEAL_REASON_SL){ Print("SELL + DEAL_REASON_SL"); prevposition=NONE;} } }This code works for me!!! Note I assign BUY/SELL to prevposition when I send the order and get confirmation.
When stop loss is triggered, the following log is generated by MT5.
2015.04.10 16:22:14 Core 1 2015.01.19 11:35:29 stop loss triggered sell 1.00 EURUSD 1.15417 sl: 1.16217 tp: 1.15317 [#5 buy 1.00 EURUSD at 1.16217]
This is exactly the info I need in EA, getting price volume, against which order previously executed.
How can I obtain this in EA?
I tried intercept TRADE_TRANSACTION_HISTORY_ADD on OnTradeTransaction() event handler, and compare the price, direction, with previous recorded order, but no success, since it is too complex and I lost int his process. I realize this may not be the intended way.
Can you please show me the standard way to detect stop loss execution, and get the following information?
1) stop loss order price
2) stop loss order volume
3) against which previous order (price, volume)
4) net loss of the this stop loss execution
Thank you very much!