Getting the Position Price

 

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.

 
#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");
}
 
fxsaber #:


MT4-Style:

Thank you. So, can we assume that right after t.Buy returns the position is always ready to be selected even on real account? 
 
Laszlo Tormasi #:
Thank you. So, can we assume that right after t.Buy returns the position is always ready to be selected even on real account? 

In 99.9% of cases this is true. If this is not.

CTrade t;
t.SetAsyncMode(true);
 
fxsaber #:

In 99.9% of cases this is true. If this is not.

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()));
 
Laszlo Tormasi #:

Seems my case is in the 0.1%

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

Getting the Position Price

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
Documentation on MQL5: Event Handling / OnTradeTransaction
  • www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Laszlo Tormasi #:

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. 

There are several high-level trading libraries. Use the one that suits your needs.

 
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.
Reason: