OrderSelect and HistoryOrderSelect both return false in OnTradeTransactions

 

Has anyone ever encountered OrderSelect and HistoryOrderSelect both return false in OnTradeTransactions when trans.type = TRADE_TRANSACTION_DEAL_ADD?

How do you deal with that?

I need to grab open price of the deal so I need to refer to the order ticket.

 
JrDevil :

Has anyone ever encountered OrderSelect and HistoryOrderSelect both return false in OnTradeTransactions when trans.type = TRADE_TRANSACTION_DEAL_ADD?

How do you deal with that?

I need to grab open price of the deal so I need to refer to the order ticket.

Use HistoryDealSelect

#include <Trade\SymbolInfo.mqh>
#include <Trade\DealInfo.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
CDealInfo      m_deal;                       // object of CDealInfo class
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
         return;
      if(m_deal.Symbol()==m_symbol.Name() && m_deal.Magic()==InpMagic)
        {
         if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
           {
            Print(EnumToString(m_deal.Entry()),", ",m_deal.Price());
           }
        }
     }
  }
 
Vladimir Karputov:

Use HistoryDealSelect

I really need to refer to the order. One use case is that I need to refer to the comment attached to the order.
Another one is that I really need the "Open Price" of the order, not the matched deal price.

 

Try this construction: in OnTradeTransaction we try to get HistoryOrderSelect - if it doesn’t work right away, then we wait in OnTick

//+------------------------------------------------------------------+
//|                                      OnTradeTransaction Info.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//---
#include <Trade\Trade.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
//--- input parameters

//--- [deal][order][confirmed->'1', no confirmed->'0']
ulong wait[][3];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   MqlTick tick;
   SymbolInfoTick(Symbol(),tick);
   for(int i=0; i<1; i++)
     {
      m_trade.SellLimit(0.01,tick.ask+0.00300)  ;
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int size=ArrayRange(wait,0);
   for(int i=0; i<size; i++)
     {
      if(wait[i][2]==1) // confirmed->'1', no confirmed->'0'
        {
         //--- try to get oeders ticket_history_order
         if(HistoryOrderSelect(wait[i][1]))
           {
            long o_type = HistoryOrderGetInteger(wait[i][1],ORDER_TYPE);
            Print("--- confirmed ---");
            Print(__FILE__," ",__FUNCTION__,", HistoryOrderSelect(",wait[i][1],")");
            Print(EnumToString((ENUM_ORDER_TYPE)o_type));
            ArrayRemove(wait,i,1);
            return;
           }
         else
           {
            Print("--- no confirmed ---");
            Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryOrderSelect(",wait[i][1],")");
           }
         return;
        }
     }
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition a deal to the history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      Print("---");
      Print(__FILE__," ",__FUNCTION__,", TRADE_TRANSACTION_DEAL_ADD");
      //---
      int size=ArrayRange(wait,0);
      if(HistoryDealSelect(trans.deal))
        {
         Print(__FILE__," ",__FUNCTION__,", HistoryDealSelect(",trans.deal,")");
         ArrayResize(wait,size+1);
         wait[size][0]=trans.deal;
         wait[size][1]=trans.order;
        }
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,")");
         return;
        }
      //---
      if(HistoryOrderSelect(trans.order))
        {
         Print(__FILE__," ",__FUNCTION__,", HistoryOrderSelect(",trans.order,
               "), order type: ",EnumToString((ENUM_ORDER_TYPE)HistoryOrderGetInteger(trans.order,ORDER_TYPE)));
         wait[size][2]=1; // confirmed->'1', no confirmed->'0'
        }
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryOrderSelect(",trans.order,")");
         wait[size][2]=0; // confirmed->'1', no confirmed->'0'
         return;
        }
     }
//--- if transaction is result of addition a order to the history
   if(type==TRADE_TRANSACTION_ORDER_ADD)
     {
      int size=ArrayRange(wait,0);
      for(int i=0; i<size; i++)
        {
         if(wait[i][1]==trans.order)
           {
            Print("--- TRADE_TRANSACTION_ORDER_ADD ---");
            wait[i][2]=1; // confirmed->'1', no confirmed->'0'
            return;
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

Try this construction: in OnTradeTransaction we try to get HistoryOrderSelect - if it doesn’t work right away, then we wait in OnTick

Thank you Vladimir Karputov
But with this being said, does this mean the MT5 platform has this moment of order is not either in "Order Object" or "History Order Object"?
Where is it stored in this moment then? I'm really curious.