Getting the Position Price
#include <Trade\Trade.mqh> void OnStart() { CTrade t; if (t.Buy(0.1)) { MqlTradeResult Result; t.Result(Result); const ulong Ticket = Result.order; if (PositionSelectByTicket(Ticket)) Print("Position " + (string)Ticket + " at " + DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN), _Digits)); else Print("Unknown error"); } else Print("Position open Error"); }
MT4-Style:
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 #define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK) void OnStart() { if (OrderSelect(OrderSend(_Symbol, OP_BUY, 0.1, Ask, 0, 0, 0), SELECT_BY_TICKET)) Print("Position " + (string)OrderTicket() + " at " + DoubleToString(OrderOpenPrice(), _Digits)); else Print("Position open Error"); }
Seems my case is in the 0.1%
bool res=trade.PositionOpen(sym,(ENUM_ORDER_TYPE)(t[type]),t[lot],price,t[sl],t[tp],comment); trade.PrintResult(); Print(trade.ResultDeal()); Print("orderselect: "+(string)PositionSelectByTicket(trade.ResultOrder()));
All right. The standard library is inconvenient not only syntactically, but also architecturally. That's why I use this method myself.
Forum on trading, automated trading systems and testing trading strategies
fxsaber, 2023.01.11 16:09
MT4-Style:
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 #define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK) void OnStart() { if (OrderSelect(OrderSend(_Symbol, OP_BUY, 0.1, Ask, 0, 0, 0), SELECT_BY_TICKET)) Print("Position " + (string)OrderTicket() + " at " + DoubleToString(OrderOpenPrice(), _Digits)); else Print("Position open Error"); }
It will work in your case.
All right. The standard library is inconvenient not only syntactically, but also architecturally. That's why I use this method myself.
It will work in your case.
Thank you. It is really sad that MT5 is unable to solve such a simple task in a straight forward way. OnTradeTransaction and OnTrade examples in the documentation are not very useful.
- www.mql5.com

Let's say we open a position with the CTrade class.
CTrade t; t.Buy(0.1);What is the quickest way to get the open price and ticket of this position or detect failure? As simple as that. Thank you.