Documentation
MQL5 ReferenceTrade FunctionsHistoryDealGetTicket 

HistoryDealGetTicket

The function selects a deal for further processing and returns the deal ticket in history.

ulong  HistoryDealGetTicket(
   int  index      // ticket deal
   );

Parameters

index

[in]  Number of a deal in the list of deals

Return Value

Value of the ulong 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. The list of orders and deals displayed in the "History" tab, depends on the depth of history that can be set manually or using the HistorySelect() function.

HistoryDealGetTicket() not only returns the ticket of a deal in history, but also selects it to further refer to by means of HistoryDealGetDouble(), HistoryDealGetInteger() and HistoryDealGetString(). Thus, HistoryDealSelect() selects a deal for the specified ticket, while HistoryDealGetTicket() selects a deal by its number in the list of deals in history.

Example:

void OnStart()
  {
   ulong deal_ticket;            // deal ticket
   ulong order_ticket;           // ticket of the order the deal was executed on
   datetime transaction_time;    // time of a deal execution 
   long deal_type ;              // type of a trade operation
   long position_ID;             // position ID
   string deal_description;      // operation description
   double volume;                // operation volume
   string symbol;                // symbol of the deal
//--- set the start and end date to request the history of deals
   datetime from_date=0;          // from the very beginning
   datetime to_date=TimeCurrent();// till the current moment
//--- request the history of deals in the specified period
   HistorySelect(from_date,to_date);
//--- total number in the list of deals
   int deals=HistoryDealsTotal();
//--- now process each trade
   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=                 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);
      //--- perform fine formatting for the deal number
      string print_index=StringFormat("% 3d",i);
      //--- show information on the deal
      Print(print_index+": deal #",deal_ticket," at ",transaction_time,deal_description);
     }
  }
//+------------------------------------------------------------------+
//|  returns the string description of the operation                 |
//+------------------------------------------------------------------+
string GetDealDescription(long 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_CORRECTIONreturn ("correction");
      case DEAL_TYPE_BUY:        descr="buy"break;
      case DEAL_TYPE_SELL:       descr="sell"break;
     }
   descr=StringFormat("%s %G %s (order #%d, position ID %d)",
                      descr,  // current description
                      volume, // deal volume
                      symbol, // deal symbol
                      ticket, // ticket of the order that caused the deal
                      pos_ID  // ID of a position, in which the deal is included
                      );
   return(descr);
//---
  }

 

See also

HistorySelect(), HistoryDealsTotal(), HistoryDealSelect(), Deal Properties