In MQL5, get ticket number of order that just hit take profit

Viewed 58

I have an EA and I am easily able to detect a TP or SL, but I am not able to determine what ticket number hit the TP or SL. Seems so easy, but it's really giving me trouble. Here is the working code that shows an alert when a trade hits the TP, but I need it to show which ticket number hit the TP.

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

   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)){
         m_deal.Ticket(trans.deal);
      } else {
         Print(__FILE__," ",__FUNCTION__,", ERROR: HistoryDealSelect(",trans.deal,")");
         return;
      }

      long reason=-1;
      if(!m_deal.InfoInteger(DEAL_REASON,reason)){
         Print(__FILE__," ",__FUNCTION__,", ERROR: InfoInteger(DEAL_REASON,reason)");
         return;
      }
  
      if((ENUM_DEAL_REASON)reason==DEAL_REASON_SL){
         Alert("Stop Loss activation");
     } else {
         if((ENUM_DEAL_REASON)reason==DEAL_REASON_TP){
            //SHOW TICKET NUMBER
         }
      }
    }
}
1 Answers

Alert(trans.position) is the solution.

Related