How to get order open price

 

I am sending an order that is executed and shown in MT5.

    MqlTradeResult tradeResult = trade.sendBuyMarket(2.);      
    OrderSelect(tradeResult.order);  
    double price = OrderGetDouble(ORDER_PRICE_OPEN);

MqlTradeResult shows 0.0 for price and so does OrderGetDouble(ORDER_PRICE_OPEN):


Is it due to the brokers settings that the order open price is not send back or am I doing something wrong?

 
Jan Tarnogrocki:

I am sending an order that is executed and shown in MT5.

MqlTradeResult shows 0.0 for price and so does OrderGetDouble(ORDER_PRICE_OPEN):


Is it due to the brokers settings that the order open price is not send back or am I doing something wrong?

You are doing something wrong.

 

I tried it again using some example code from the MT5 Help (F1):

//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request={};
   MqlTradeResult  result={};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.2;                                   // volume of 0.2 lot
   request.type     =ORDER_TYPE_SELL;                       // order type
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID); // price for opening
   request.deviation=5;                                     // allowed deviation from the price
   request.magic    =123;                          // MagicNumber of the order
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);


With the same result:

That is on a demo account at Admiral Markets.

So I tried on another demo account at Active Trades and there it works:
AT

Changing the account made the difference here, not changing the code. Hope this might help if someone is facing the same issue.
IF there can be something changed in the code so it works with any broker I would be happy to be assisted.

 
If the broker sends a trade order to an external system, then you must catch the transaction in OnTradeTransaction.
 
Jan Tarnogrocki #:

IF there can be something changed in the code so it works with any broker I would be happy to be assisted.

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)

void OnStart()
{
  TICKET_TYPE Ticket = OrderSend(_Symbol, OP_SELL, 0.2, Bid, 5, 0, 0, NULL, 123);
  
  if (OrderSelect(Ticket, SELECT_BY_TICKET))
  {
    OrderPrint();
    
    Print(OrderOpenPrice());
  }
}
 
fxsaber #:
It´s MT5 not MT4.
 
MT5: first select a position via CPositionInfo or directly.
 
Jan Tarnogrocki #:
It´s MT5 not MT4.

This is MT5-code.

 
Alain Verleyen #:

You are doing something wrong.

You are very helpful - why do you even comment? And you are a moderator? You should be banned.
 
Imre #:
You are very helpful - why do you even comment? And you are a moderator? You should be banned.

Why are you so upset ? I commented as it's a user issue and not something related to the broker as the OP suggested.

When the Ordersend() function returns (or any wrapper using it), there order is not yet executed but placed, so there is no order to select and no price.

OrderSelect() is a function returning a value which the OP should have checked.