Detecting SL & TP

 

Greetings,

I'd like to detect when the SL or TP level is hit and take appropriate action.  Is the following snippet correct?

void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) {
        if(HistoryDealSelect(trans.deal)) {
                long reason = HistoryDealGetInteger(trans.deal, DEAL_REASON);
                if ((reason == DEAL_REASON_SL || reason == DEAL_REASON_TP) && HistoryDealGetInteger(trans.deal, DEAL_MAGIC) == eaMagic)
                        // do something...
        }
}

I'd appreciate any comments. This *appears* to work, but I'm not sure.

Thanks

 
Your topic has been moved to the section: Expert Advisors and Automated Trading — Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

 I use this code. 


#include <Trade\Trade.mqh>


void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {

      CDealInfo  objDeal;
      long reason=-1;

      //--- get transaction type as enumeration value
      ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
      //--- if transaction is result of addition of the transaction in history
      if(type==TRADE_TRANSACTION_DEAL_ADD)
      {
         if(HistoryDealSelect(trans.deal))
            objDeal.Ticket(trans.deal);

         else
         {
            Print(__FILE__," ",__FUNCTION__,", ERROR: HistoryDealSelect(",trans.deal,")");
            return;
         }
         //---
         if(!objDeal.InfoInteger(DEAL_REASON,reason))
         {
            Print(__FILE__," ",__FUNCTION__,", ERROR: InfoInteger(DEAL_REASON,reason)");
            return;
         }
         if((ENUM_DEAL_REASON)reason==DEAL_REASON_SL)
         {
            //Handle when price hit SL
         }
         else if((ENUM_DEAL_REASON)reason==DEAL_REASON_TP)
         {
            //Handle when price hit TP
         }
      }
   }

   
 
Alireza #: objDeal.InfoInteger(DEAL_REASON,reason)

Thanks for the reply Alireza. Your code appears to do the same as mine, just differently, except for the addition of:

trans.deal == TRADE_TRANSACTION_DEAL_ADD
 
HenZen:

Greetings,

I'd like to detect when the SL or TP level is hit and take appropriate action.  Is the following snippet correct?

I'd appreciate any comments. This *appears* to work, but I'm not sure.

Thanks

You can test your code in strategy tester and see whether it works. For example,....check below code

void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) {
        if(HistoryDealSelect(trans.deal)) {
                long reason = HistoryDealGetInteger(trans.deal, DEAL_REASON);
                if ((reason == DEAL_REASON_SL  && HistoryDealGetInteger(trans.deal, DEAL_MAGIC) == eaMagic)
                      Alert("StopLoss has been taken out");
        }
 if ((reason == DEAL_REASON_TP  && HistoryDealGetInteger(trans.deal, DEAL_MAGIC) == eaMagic)
                      Alert("TakeProfit has been hit");
        }
}
 
HenZen #:Thanks for the reply Alireza. Your code appears to do the same as mine, just differently, except for the addition of:

Code works fine as expected, what's the actual problem ?