Getting Position ID of the trade

 

I want to get the position ID of a trade right after opening it, but it always returns 0.

Here is the code I’m using:

if(trade.Sell(volume, symbol, 0, sl, tp))
{
   ulong order_ticket = trade.ResultOrder();

   // Trying to get the position ID
   ulong pos_id = HistoryDealGetInteger(order_ticket, DEAL_POSITION_ID);

   Print("order_ticket = ", order_ticket, "  pos_id = ", pos_id);
}

How can I fix this so that I reliably get the position ID of the just-opened trade?

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be...
 
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 don't use the Standard Library, but according the documentation CTrade's ResultOrder returns an "Order" ticket, not a "Deal" ticket.

Use the HistoryOrderGetInteger instead (with ORDER_POSITION_ID), but first you need to select the Order with HistoryOrderSelect.

Documentation on MQL5: Trade Functions / HistoryOrderGetInteger
Documentation on MQL5: Trade Functions / HistoryOrderGetInteger
  • www.mql5.com
Returns the requested property of an order. The order property must be of datetime, int type. There are 2 variants of the function. 1. Immediately...
 

Also, I recommend reading the following if you have not yet read it ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Jamshidbek Kuchkarov:

Documentation on MQL5: Trade Functions / PositionGetInteger

PositionGetInteger(POSITION_IDENTIFIER)
Documentation on MQL5: Trade Functions / PositionGetInteger
Documentation on MQL5: Trade Functions / PositionGetInteger
  • www.mql5.com
The function returns the requested property of an open position, pre-selected using PositionGetSymbol or PositionSelect . The position property...
 
That only works if the Position is already selected, which is not the case if using the CTrade.Sell class method.
 

Hi

You need to use HistorySelect and select a position before using HistoryDealGetInteger function. Check the documentation for this function there should be some examples how to use it.
But there is an easier way when you open a trade, you can resign from Ctrade class and simply open it by OrderSend function, this way you have access to MqlTradeResult. You can get pos id directly from this function after opening the trade:

MqlTradeRequest mrequest;  
   MqlTradeResult mresult;   
   ZeroMemory(mrequest);     
   if(orderType == ORDER_TYPE_BUY || orderType == ORDER_TYPE_SELL){
      mrequest.action = TRADE_ACTION_DEAL;   
   }else{
      mrequest.action = TRADE_ACTION_PENDING; 
      if(expiration!=0){
         mrequest.type_time = ORDER_TIME_SPECIFIED;
         mrequest.expiration = expiration;
      }
   }               
   mrequest.price = openPrice;             
   mrequest.sl = _stopLoss;   
   mrequest.tp = _takeProfit; 
   mrequest.symbol = _Symbol;                                            
   mrequest.volume = lots;                                 
   mrequest.magic = _magic;                                               
   mrequest.type = orderType;                                         
   mrequest.type_filling = GetFilling(_Symbol);                              
   mrequest.deviation=_slippage; 
   mrequest.comment = comment;                       
   bool result = OrderSend(mrequest,mresult);

   int pos_id = mresult.order;

Best Regards

 
Marzena Maria Szmit #:

Hi

You need to use HistorySelect and select a position before using HistoryDealGetInteger function. Check the documentation for this function there should be some examples how to use it.
But there is an easier way when you open a trade, you can resign from Ctrade class and simply open it by OrderSend function, this way you have access to MqlTradeResult. You can get pos id directly from this function after opening the trade:

Best Regards

Using CTrade or raw function OrderSend() is irrelevant. CTrade also gives you access to MqlTradeResult if you like.