跳至内容

CopyRates

CopyRates

将指定符号周期中MqlRates结构的历史数据按指定数量复制到rates_array数组中。复制数据的元素顺序是从现在到过去,即起始位置0表示当前条形。

CopyRates

在复制未知数量的数据时,建议使用动态数组作为目标数组,因为如果请求的数据量少于(或多于)目标数组的长度,函数将尝试重新分配内存,使请求的数据完全适应。

如果你知道需要复制的数据量,最好将其复制到静态分配的缓冲区,以防止过度分配内存。

无论目标数组的属性是什么——as_series=true或as_series=false。数据将被复制,使得最旧的元素位于数组分配的物理内存的起始位置。函数调用有3种方式。

通过第一个位置和所需元素数量调用

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   int              start_pos,         // start position
   int              count,             // data count to copy
   MqlRates         rates_array[]      // target array to copy
   );

通过开始日期和所需元素数量调用

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   datetime         start_time,        // start date and time
   int              count,             // data count to copy
   MqlRates         rates_array[]      // target array to copy
   );

通过所需时间间隔的开始和结束日期调用

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   datetime         start_time,        // start date and time
   datetime         stop_time,         // end date and time
   MqlRates         rates_array[]      // target array to copy
   );

参数

symbol_name

[in] 符号名称。

timeframe

[in] 周期。

start_time

[in] 第一个要复制的条形的时间。

start_pos

[in] 第一个要复制的条形的起始位置。

count

[in] 要复制的数据数量。

stop_time

[in] 对应最后一个要复制的条形的时间。

rates_array[]

[out] MqlRates类型的数组。

返回值

返回复制的元素数量,或在发生错误时返回-1。

注意

如果请求的数据整个区间在服务器上的可用数据中不存在,函数将返回-1。如果请求超过TERMINAL_MAXBARS(图表上的最大条形数)的数据,函数也将返回-1。

如果请求的时间序列尚未构建或需要从服务器下载,函数将立即返回-1。

当通过开始日期和所需元素数量请求数据时,只返回日期早于或等于指定日期的数据。这意味着,任何条形的开放时间(无论是成交量、价差、指标缓冲区的值、价格开放、高、低、收盘价还是开放时间Time)始终小于或等于指定日期。

当在指定的日期范围内请求数据时,只返回该区间内的数据。区间以秒为单位计算。这意味着,任何条形的开放时间(无论是成交量、价差、指标缓冲区的值、价格开放、高、低、收盘价还是开放时间Time)始终在请求的区间内。

因此,如果当前日是星期六,尝试复制一周时间框架的数据,指定start_time=Last_Tuesday和stop_time=Last_Friday时,函数将返回0,因为一周时间框架的开放时间总是星期日,但一周的条形并不在指定的区间内。

如果你需要返回与当前未完成条形对应的值,可以使用第一种调用方式,指定start_pos=0和count=1。

示例:

void OnStart()
  {
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,100,rates);
   if(copied>0)
     {
      Print("Bars copied: "+copied);
      string format="open = %G, high = %G, low = %G, close = %G, volume = %d";
      string out;
      int size=fmin(copied,10);
      for(int i=0;i<size;i++)
        {
         out=i+":"+TimeToString(rates[i].time);
         out=out+" "+StringFormat(format,
                                  rates[i].open,
                                  rates[i].high,
                                  rates[i].low,
                                  rates[i].close,
                                  rates[i].tick_volume);
         Print(out);
        }
     }
   else Print("Failed to get history data for the symbol ",Symbol());
  }

另请参阅

结构和类TimeToStringStringFormat

最后更新于