ArrayCopyRates
ArrayCopyRates
该函数将利率数据复制到数组中,并返回复制的条形图数量。该函数有两种变体:
int ArrayCopyRates(
MqlRates& rates_array[], // MqlRates array, passed by reference
string symbol=NULL, // symbol
int timeframe=0 // timeframe
);该函数将利率数据复制到 RateInfo[][6] 类型的双精度浮点数二维数组中,并返回复制的条形图数量。
int ArrayCopyRates(
void& dest_array[][], // destination array, passed by reference
string symbol=NULL, // symbol
int timeframe=0 // timeframe
);参数
- rates_array[]
[输出] 目标数组,类型为 MqlRates。
- dest_array[]
[输出] 二维目标数组,类型为 double。
- symbol=NULL
[输入] 符号名称。
- timeframe=0
[输入] 时间帧。可以是 ENUM_TIMEFRAMES 枚举值中的任意一种。0表示当前图表时间帧。
返回值
该函数返回复制的条形图数量,如果失败则返回 -1。
如果请求的数据(符号名称和/或时间帧)来自其他图表,可能会出现相应图表未在客户端终端中打开的情况,此时必须从服务器请求所需数据。在这种情况下,错误 ERR_HISTORY_WILL_UPDATED (4066 - 请求的历史数据正在更新) 将存储在 last_error 变量中,需要重新请求(参见 ArrayCopySeries() 的示例)。
注意
此利率数组通常用于向 DLL 函数传递数据。
在函数的第一种变体中,它执行到 MqlRates 类型的数组的虚拟数据复制。这意味着如果时间序列数据已更新,rates_array[] 数组将始终引用实际数据。
在第二种变体中,它执行到 dest_array[][] 数组的实际数据复制。目标数组将被调整大小以匹配时间序列的大小(即使目标数组已被声明为静态)。
RateInfo 数组的第一维包含条形图数量,第二维有 6 个元素:
0 - 时间, 1 - 开盘价, 2 - 最低价, 3 - 最高价, 4 - 收盘价, 5 - 成交量。
示例:
#property copyright "Copyright © 2013, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property description "Expert Advisor displaying two cases of"
#property description "ArrayCopyRates() function call"
//--- destination array for physical copying of historical data
double double_array[][6];
//--- destination array for logical copying of historical data
MqlRates mqlrates_array[];
//--- first call flag
bool first_call;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- run this EA at chart with M1 timeframe
if(Period()!=PeriodSeconds(PERIOD_M1)/60)
{
Alert("The Expert Advisor must be attached to M1 chart!");
return(INIT_FAILED);
}
//--- first call
first_call=true;
//--- all ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- delete all comments
Comment("");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(first_call)
{
//--- copying physically double_array
ArrayCopyRates(double_array,NULL,0);
//--- virtual copying -> mqlrates_array will contain the reference on the data
ArrayCopyRates(mqlrates_array,NULL,0);
//--- cancel first call flag
first_call=false;
}
//--- at each tick print the values of the 0-th array element to see the difference
Comment("The values of double_array[] are not changed (because of real data copying):\n",
"0 - time: ",(datetime)double_array[0][0],"\n",
"1 - open: ",double_array[0][1],"\n"
"2 - low: ",double_array[0][2],"\n"
"3 - high: ",double_array[0][3],"\n"
"4 - close: ",double_array[0][4],"\n"
"5 - volume: ",DoubleToString(double_array[0][5],0),"\n\n",
"The values of mqlrates_array[] are changed (because of virtual data copying):\n",
"0 - time: ",mqlrates_array[0].time,"\n",
"1 - open: ",mqlrates_array[0].open,"\n"
"2 - low: ",mqlrates_array[0].low,"\n"
"3 - high: ",mqlrates_array[0].high,"\n"
"4 - close: ",mqlrates_array[0].close,"\n"
"5 - volume: ",mqlrates_array[0].tick_volume);
}
最后更新于