CopyHigh
CopyHigh
该函数将选定符号-时期对的最高条形价格历史数据按指定数量复制到 high_array 数组中。需要注意的是,元素的排序是从现在到过去,即起始位置为0表示当前条形。

当复制未知数量的数据时,建议使用动态数组作为目标数组,因为如果请求的数据量少于(或多于)目标数组的长度,函数会尝试重新分配内存,使请求的数据完全适应。
如果你知道需要复制的数据量,最好将其复制到静态分配的缓冲区,以防止过度分配内存。
无论目标数组的属性是什么——as_series=true还是as_series=false。数据将被复制,使得最旧的元素位于数组分配的物理内存的起始位置。函数调用有3种方式。
通过第一个位置和所需元素数量调用
int CopyHigh(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
int start_pos, // start position
int count, // data count to copy
double high_array[] // target array to copy
);通过起始日期和所需元素数量调用
int CopyHigh(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
int count, // data count to copy
double high_array[] // target array to copy
);通过所需时间间隔的起始和结束日期调用
int CopyHigh(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
datetime stop_time, // stop date and time
double high_array[] // target array to copy
);参数
- symbol_name
[in] 符号名称。
- timeframe
[in] 时期。
- start_pos
[in] 第一个要复制的元素的起始位置。
- count
[in] 要复制的数据数量。
- start_time
[in] 第一个要复制的元素的开始时间。
- stop_time
[in] 条形时间,对应最后一个要复制的元素。
- high_array[]
[out] double类型的数组。
返回值
如果发生错误,则返回复制的数据数量或-1。
注意
如果请求的数据范围完全不在服务器上的可用数据中,函数将返回-1。如果请求的数据超出TERMINAL_MAXBARS(图表上的最大条形数),函数也将返回-1。
如果请求的时间序列尚未构建或需要从服务器下载,函数将立即返回-1。
当通过起始日期和所需元素数量请求数据时,只有日期小于或等于指定日期的数据才会被返回。这意味着,任何条形的开放时间(无论是成交量、价差、指示器缓冲区上的价值、开盘价、最高价、最低价还是开放时间Time)始终小于或等于指定的日期。
当在指定的日期范围内请求数据时,只有该区间内的数据会被返回。这个区间以秒为单位计算。这意味着,任何条形的开放时间(无论是成交量、价差、指示器缓冲区上的价值、开盘价、最高价、最低价还是开放时间Time)始终在请求的区间内。
因此,如果当前日是星期六,尝试复制周时间框架的数据,指定start_time=Last_Tuesday和stop_time=Last_Friday时,函数将返回0,因为周时间框架的开放时间总是星期日,但一周的条形并不在指定的区间内。
如果你需要返回与当前未完成的条形对应的价值,可以使用第一种调用方式,指定start_pos=0和count=1。
示例:
#property copyright "2009, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "An example for output of the High[i] and Low[i]"
#property description "for a random chosen bars"
double High[],Low[];
//+------------------------------------------------------------------+
//| Get Low for specified bar index |
//+------------------------------------------------------------------+
double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)
{
double low=0;
ArraySetAsSeries(Low,true);
int copied=CopyLow(symbol,timeframe,0,Bars(symbol,timeframe),Low);
if(copied>0 && index<copied) low=Low[index];
return(low);
}
//+------------------------------------------------------------------+
//| Get the High for specified bar index |
//+------------------------------------------------------------------+
double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)
{
double high=0;
ArraySetAsSeries(High,true);
int copied=CopyHigh(symbol,timeframe,0,Bars(symbol,timeframe),High);
if(copied>0 && index<copied) high=High[index];
return(high);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- on every tick we output the High and Low values for the bar with index,
//--- that is equal to the second, on which tick arrived
datetime t=TimeCurrent();
int sec=t%60;
printf("High[%d] = %G Low[%d] = %G",
sec,iHigh(Symbol(),0,sec),
sec,iLow(Symbol(),0,sec));
}