2
votes

I would like to execute a series of commands every time when a stop loss (or take profit) automatically triggers.

I am not sure how to do it, because I haven't found handlers for these events.

1

1 Answers

4
votes
void OnTradeTransaction(const MqlTradeTransaction& trans,const MqlTradeRequest& request,const MqlTradeResult& result)
   {
    if(trans.type!=TRADE_TRANSACTION_DEAL_ADD)return;
    if(!HistoryDealSelect(trans.deal))return;
    if(HistoryDealGetInteger(trans.deal,DEAL_MAGIC)!=InpMagicNumber)return;
    if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY)!=DEAL_ENTRY_OUT)return;
    if(HistoryDealGetString(trans.deal,DEAL_SYMBOL)!=_Symbol)return;
    long reason=HistoryDealGetInteger(trans.deal,DEAL_REASON);
    if(reason==DEAL_REASON_SL)
       {
        Print(__LINE__,__FILE__," we are afraid SL is hit);
       }
    else if(reason==DEAL_REASON_TP)
       {
        Print(__LINE__,__FILE__," TP is HIT!);
       }
   }