Download MetaTrader 5

CopyTicks

The function receives ticks accumulated by the terminal for the current work session and writes them into the ticks_array. It should be noted that the order of indexing goes from the past to the present, that is tick index 0 is the oldest one in the array.

int  CopyTicks(
   string           symbol_name,           // symbol name
   MqlTick&         ticks_array[],         // the array for receiving ticks
   uint             flags=COPY_TICKS_ALL,  // the flag that defines the type of the ticks that are received
   ulong            from=0,                // date starting from which ticks are requested
   uint             count=0                // the number of last ticks that you need to receive
   );

Parameters

symbol_name

[in]  Symbol.

ticks_array

[out] MqlTick type array for receiving ticks.

flags

[in]  The flag to define the type of the ticks you want to receive. COPY_TICKS_INFO — only Bid and Ask, COPY_TICKS_TRADE — only Last and Volume, COPY_TICKS_ALL — all ticks.

from

[in]  The first date of requested ticks. In milliseconds since 1970.01.01. If the parameter from=0 then last count tick will be written.

count

[in]  The number of requested ticks. If the date and number are not specified, all available ticks are received, but not more than 2000.

Return Value

The number of copied tick or -1 in case of an error.

Note

The OnTick() function does not handle every tick, it notifies the Expert Advisor of market changes. It can be a batch of changes: the terminal can simultaneously make a few ticks, but OnTick() will be called only once to notify the EA of the latest market state. The CopyTicks() function allows you to request and analyze all the ticks.

If the from and count parameters are not specified, all available ticks, but no more than 2000, will be written in the ticks_array[]. The flags parameter allows setting the type of required ticks.

Example:

//--- input parameters
input int      ticks=10;  // the number of requested ticks
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
/--- the array that receives ticks
   MqlTick tick_array[];
//--- requesting ticks
   int copied=CopyTicks(_Symbol,tick_array,COPY_TICKS_ALL,0,ticks);
//--- if ticks are received, show the Bid and Ask values on the chart  
   if(copied>0)
     {
      string comment="#  Time       Bid        Ask\r\n";
      //--- generate the comment contents      
      for(int i=0;i<copied;i++)
        {
         MqlTick tick=tick_array[i];
         string tick_string=StringFormat("%d: %s  %G  %G",
                                         i,
                                         TimeToString(tick.time,TIME_MINUTES|TIME_SECONDS),
                                         tick.bid,
                                         tick.ask);
         comment=comment+tick_string+"\r\n";
        }
        //--- show a comment on the chart        
        Comment(comment);
     }
   else // report an error that occurred when receiving ticks
     {
      Comment("Ticks could not be loaded. GetLastError()=",GetLastError());
     }
  }

See also

SymbolInfoTick, Structure for Current Prices, OnTick()


Updated: 2015.12.03