MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading Language Documentation

How to Use MQL5 Constructions? Check the Documentation!
MQL5 ReferenceTrade FunctionsHistoryDealGetInteger 


Returns the requested property of a deal. The deal property must be of the datetime, int type. There are 2 variants of the function.

1. Immediately returns the property value.

long  HistoryDealGetInteger(
   ulong                       ticket_number,     // Ticket
   ENUM_DEAL_PROPERTY_INTEGER  property_id        // Property identifier

2. Returns true or false, depending on the success of the function. If successful, the value of the property is placed into a target variable passed by reference by the last parameter.

bool  HistoryDealGetInteger(
   ulong                       ticket_number,     // Ticket
   ENUM_DEAL_PROPERTY_INTEGER  property_id,       // Property identifier
   long&                       long_var           // Here we accept the property value



[in]  Trade ticket.


[in]  Identifier of the deal property. The value can be one of the values of the ENUM_DEAL_PROPERTY_INTEGER enumeration.


[out]  Variable of the long type that accepts the value of the requested property.

Return Value

Value of the long type.


Do not confuse orders, deals and positions. Each deal is the result of the execution of an order, each position is the summary result of one or more deals.


//| Trade function                                                   |
void OnTrade()
//--- receive the last deal's ticket from week's trading history
   ulong last_deal=GetLastDealTicket();
      //--- time of deal execution in milliseconds since 01.01.1970
      long deal_time_msc=HistoryDealGetInteger(last_deal,DEAL_TIME_MSC);
      PrintFormat("Deal #%d DEAL_TIME_MSC=%i64 => %s",
      PrintFormat("HistoryDealSelect() failed for #%d. Eror code=%d",
//| Returns the last deal ticket in history or -1                    |
ulong GetLastDealTicket()
//--- request history for the last 7 days
      //--- notify on unsuccessful call and return -1
      Print(__FUNCTION__," HistorySelect() returned false");
      return -1;
   ulong first_deal,last_deal,deals=HistoryOrdersTotal();
//--- work with orders if there are any
      Print("Deals = ",deals);
      PrintFormat("first_deal = %d",first_deal);
         PrintFormat("last_deal = %d",last_deal);
         return last_deal;
      return first_deal;
//--- no deal found, return -1
   return -1;
//| Requests history for the last days and returns false in case of failure  |
bool GetTradeHistory(int days)
//--- set a week period to request trade history
   datetime to=TimeCurrent();
   datetime from=to-days*PeriodSeconds(PERIOD_D1);
//--- make a request and check the result
      Print(__FUNCTION__," HistorySelect=false. Error code=",GetLastError());
      return false;
//--- history received successfully
   return true;

See also

HistoryDealsTotal(), HistorySelect(), HistoryDealGetTicket(), Deal Properties

Updated: 2014.10.23