Position Ticket on open and close

 

Hi,

I am looking to get the specific ticket position of orders when they hit a take profit or a stop loss. I wrote the following code to store the tickets whenever a new position is opened:

if(trade.Buy(lot_min, _Symbol, ask, stop_loss, take_profit))
{
   Print("TICKET OPEN: ", PositionGetTicket(PositionsTotal()-1));
   tickets[count] = PositionGetTicket(PositionsTotal()-1);      
} 

And the following to get the ticket of every positions whenever a take profit or stop loss is reached:

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
{
   //--- 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))
         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)
      {
         Print("TICKET HIT: ", trans.deal);    
      }
      else if((ENUM_DEAL_REASON)reason==DEAL_REASON_TP)
      {
         Print("TICKET HIT: ", trans.deal);  
      }
   }
}

However, in this way I am getting the ticket of each deal that is different for each opened/closed transaction. Instead, I would like to get the position ticket (so the same ticket of the open deal) within the OnTradeTransaction function to identify which of the position was closed. 

 

Your topic has been moved to the section: Expert Advisors and Automated Trading

You have been advised before, so please pay more careful attention in the future.

Consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

Reason: