CTrade ResultPrice() is 0.0 in live trading

 

I have the problem, that I do not get the price of the execution, when I use the class CTrade, the position is opened or closed, but the price is 0.0.
I wrote a small expert Advisor for test. In the Demo Account everything works, but only at Livetrading the price is 0.0
Does anybody know, how to resolve this problem?

#include <trade/trade.mqh>
CTrade cTrade;
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (PositionsTotal() == 0)
   {
      if (cTrade.Buy(1, NULL))
      {
         Print(cTrade.ResultPrice());
      }
   }
  }
//+------------------------------------------------------------------+
 
EspressoMartini:

I have the problem, that I do not get the price of the execution, when I use the class CTrade, the position is opened or closed, but the price is 0.0.
I wrote a small expert Advisor for test. In the Demo Account everything works, but only at Livetrading the price is 0.0
Does anybody know, how to resolve this problem?

Normal on ECN account (Market execution).

You need to use OnTrade() or OnTradeTransaction() to catch the deal price.

 

Hello Alain,

I have the same problem, but cannot solve it.

I want to find out how to get the close price after closing a position.

In the following code "PositionMagic" is an identifyer for different trade types I'm using.


if(m_trade.PositionClose(m_position.Ticket())) {
      TTPositionOpenPriceR[PositionMagic] = PositionGetDouble(POSITION_PRICE_OPEN); // works in simulation and live trading

      HistorySelect(0, TimeCurrent());
      TTPositionClosePriceR[PositionMagic] = HistoryDealGetDouble(HistoryDealGetTicket(HistoryDealsTotal()-1), DEAL_PRICE);
      strPositionClosePriceR = DoubleToString(TTPositionClosePriceR[PositionMagic], 2); // works in simulation, but NOT in live trading

}


Any help would be very welcome.


Best regards

Jorg

 
EspressoMartini:

I have the problem, that I do not get the price of the execution, when I use the class CTrade, the position is opened or closed, but the price is 0.0.
I wrote a small expert Advisor for test. In the Demo Account everything works, but only at Livetrading the price is 0.0
Does anybody know, how to resolve this problem?

Dear EspressoMartini, 

Could you tell me Is CTrader better than MT4 please? 

Should we use it?

 
Jorg1:

Any help would be very welcome.

#property script_show_inputs

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

input TICKET_TYPE inPositionTicket = 0; // Position Ticket to Close.

void OnStart()
{
  if (OrderSelect(inPositionTicket, SELECT_BY_TICKET) && !OrderCloseTime()) // Select Position from Live
  {
    Print("Before Closing");
    OrderPrint();
    
    if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0) && // Position Close
        OrderSelect(inPositionTicket, SELECT_BY_TICKET))                // Select Position from History
    {
      Print("\nAfter Closing");
      OrderPrint();
      
      double TTPositionClosePriceR = OrderClosePrice();
    }
  }
}


Result

Before Closing
#170559 2019.11.26 17:03:24 sell 1.00 EURGBP 0.85663 0.00000 0.00000 0.83525 0.00 -9.94 2861.84 Hello World! 0

After Closing
#166002 2019.11.26 17:03:24 sell 1.00 EURGBP 0.85663 0.00000 0.00000 2019.12.13 14:43:00 0.83525 0.00 -9.94 2862.10 Hello World! 0
Reason: