results.price returning zero when used on a live account (returns expected value on demo account)

 

Hi,

I've been working on an MQL5  EA for a friend.

In my code I've got a snippet at the end of successfully placing an order (see below), notifying the result (from the standard documented MQL 5 MqlTradeResult result structure) to the Experts console and notifying it to email and phone push notifications .  Everything has been working fine in testing and running on an IC Markets demo account.

We've now started using the EA to trade live on a live account with IC Markets, but noticed that result.price is always returning zero on the live account whilst the anticipated deal price value is returned on the demo account.

I've searched online but can't find any similar issues reported.  The rest of the result structure seems intact as during trade management I can manipulate and close the order using result.order - it's just price that seems to be zero.

The only difference between my demo account and the live account is my friend downloaded and installed MT5 from IC Markets, whereas I downloaded and installed from metatrader and added ICmarkets as the broker and have only set up a demo account.

Anyone seen this/got any ideas.

thanks


Steve


      Print(">BUY order ",result.order," successfully placed");
      string tempString="Buy Order placed at ";
      StringAdd(tempString,(string)result.price);  
      myNotify(tempString); 
      if (verbose) Print("result=",result.retcode,"; deal=",result.deal,"; order=",result.order,"; price=",result.price);
      //if (verbose) Print("volume=",result.volume,"; comment=",result.comment);    
      if (mySound) PlaySound("ok.wav");
      return(result.order);

 
Show the code snippet that's sending the order. My guess is your order is executed asynchronously on live account, and so MqlTradeResult is filled with some premature values as the deal has not been done yet. To get the true result you would need to listen in OnTrade or OnTradeTransaction for incoming events related to that deal.
 
lippmaje:
To get the true result you would need to listen in OnTrade or OnTradeTransaction for incoming events related to that deal.

Or wait a deal through the cycle.

 
lippmaje:
Show the code snippet that's sending the order. My guess is your order is executed asynchronously on live account, and so MqlTradeResult is filled with some premature values as the deal has not been done yet. To get the true result you would need to listen in OnTrade or OnTradeTransaction for incoming events related to that deal.

Thanks very much lippmaje and fxsaber.  Asynchronous behaviour makes sense as to the reason.

Having said that I'm using OrderSend(request,result) and my assumption was that this wasn't asynchronous primarily because a specifically asynchronous version OrderSendAsync exists and my order handling code is presently more or less a cut and paste of the examples in the MQL5 reference, which uses Print to display the returned values!

However now I've got it all up and running I was about to refactor the code anyway to make the error handling more robust, use onTrade and OnTradeTransaction, use the async version to better hit the target placement price of "double headed" positions and deal properly with manual interventions that can interfere with hedging and other linked trade logic, so that should sort everything.


cheers


Steve

 
Hi Steve. I suggest to check the error message. Lak of money to cover the price and risk could be reasons or volume to short. 
 
Steve Clark:

However now I've got it all up and running ...

Great. Parsing events in OnTradeTransaction is sort of nerve-racking. This helped me debug it:

int DumpDealHistory(datetime dtfrom=0,datetime dtto=0)
  {
   string buffer[];
   int count=DumpDealHistory(buffer,dtfrom,dtto);
   for(int i=0;i<count;i++)
     {
      Print(" ",i,": ",buffer[i]);
     }
   return count;
  }

int DumpDealHistory(string &buffer[],datetime dtfrom=0,datetime dtto=0)
  {
   ulong deal_ticket;
   ulong order_ticket;
   datetime transaction_time;
   ENUM_DEAL_TYPE deal_type;
   long position_ID;
   string deal_description;
   double volume;
   string symbol;
   if(dtto==0) dtto=TimeCurrent();
   HistorySelect(dtfrom,dtto);
   int deals=HistoryDealsTotal();
   ArrayResize(buffer,deals);

   for(int i=0;i<deals;i++)
     {
      deal_ticket=HistoryDealGetTicket(i);
      volume=HistoryDealGetDouble(deal_ticket,DEAL_VOLUME);
      transaction_time=(datetime)HistoryDealGetInteger(deal_ticket,DEAL_TIME);
      order_ticket=HistoryDealGetInteger(deal_ticket,DEAL_ORDER);
      deal_type=(ENUM_DEAL_TYPE)HistoryDealGetInteger(deal_ticket,DEAL_TYPE);
      symbol=HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
      position_ID=HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
      deal_description=GetDealDescription(deal_type,volume,symbol,order_ticket,position_ID);
      string print_index=StringFormat("% 3d",i);
      buffer[i]=print_index+": deal #"+IntegerToString(deal_ticket)+" at "+TimeToString(transaction_time,TIME_SECONDS)+" "+deal_description;
     }
   return deals;
  }

string GetDealDescription(ENUM_DEAL_TYPE deal_type,double volume,string symbol,long ticket,long pos_ID)
  {
   string descr;
   switch(deal_type)
     {
      case DEAL_TYPE_BALANCE:                  return ("balance");
      case DEAL_TYPE_CREDIT:                   return ("credit");
      case DEAL_TYPE_CHARGE:                   return ("charge");
      case DEAL_TYPE_CORRECTION:               return ("correction");
      case DEAL_TYPE_BUY:                      descr="buy"; break;
      case DEAL_TYPE_SELL:                     descr="sell"; break;
      case DEAL_TYPE_BONUS:                    return ("bonus");
      case DEAL_TYPE_COMMISSION:               return ("additional commission");
      case DEAL_TYPE_COMMISSION_DAILY:         return ("daily commission");
      case DEAL_TYPE_COMMISSION_MONTHLY:       return ("monthly commission");
      case DEAL_TYPE_COMMISSION_AGENT_DAILY:   return ("daily agent commission");
      case DEAL_TYPE_COMMISSION_AGENT_MONTHLY: return ("monthly agent commission");
      case DEAL_TYPE_INTEREST:                 return ("interest rate");
      case DEAL_TYPE_BUY_CANCELED:             descr="cancelled buy deal"; break;
      case DEAL_TYPE_SELL_CANCELED:            descr="cancelled sell deal"; break;
     }
   return StringFormat("%s %G %s (order #%d, position ID %d)",descr,volume,symbol,ticket,pos_ID);
  }

string GetTransactionDescription(const MqlTradeTransaction &t)
  {
   int dgt=(int)SymbolInfoInteger(t.symbol,SYMBOL_DIGITS);
   string descr=t.symbol+" "+EnumToString(t.type);

   switch(t.type)
     {
      case TRADE_TRANSACTION_DEAL_ADD:
      case TRADE_TRANSACTION_DEAL_DELETE:
      case TRADE_TRANSACTION_DEAL_UPDATE:
         descr=StringFormat("%s %s deal: %d order: %d pos: %d by: %d",descr,EnumToString(t.deal_type),t.deal,t.order,t.position,t.position_by);
     }
   descr+=" price: "+DoubleToString(t.price,dgt)+" sl: "+DoubleToString(t.price_sl,dgt)+" tp: "+DoubleToString(t.price_tp,dgt)+" vol: "+DoubleToString(t.volume);
   return descr;
  }
 
Estuardo Pellecer:
Hi Steve. I suggest to check the error message. Lak of money to cover the price and risk could be reasons or volume to short. 


Thanks for the suggestion but I already do error checking and there was a valid confirmation of the trade and the trade was fully performed 

 
lippmaje:

Great. Parsing events in OnTradeTransaction is sort of nerve-racking. This helped me debug it:

Many thanks - that should save me some time!

 
I'm having the same trouble. During the backtests, trans.volume is ok, but when I'm running live with a demo account, even thoug the deal is executed and I can see the position, trans.volume is always returning zero.
 
Almost no one here knows how to correctly work with MT5-orders. Therefore, either you yourself will have to completely go through this path, or use ready-made trading libraries where such problems are solved.