#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.
fxsaber #:
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.

Documentation on MQL5: Event Handling / OnTradeTransaction
- www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
You can try also OnTrade function – this will be performed when trade event (like opening a trade) will be detected and then retrieve the open price from this most recently opened trade.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.