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.
- www.mql5.com
Also, I recommend reading the following if you have not yet read it ...
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.
Documentation on MQL5: Trade Functions / PositionGetInteger
PositionGetInteger(POSITION_IDENTIFIER)
- www.mql5.com
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
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
How can I fix this so that I reliably get the position ID of the just-opened trade?