CopyTickVolume
CopyTickVolume
该函数将所选符号-周期对的历史Tick成交量数据按指定数量存入volume_array中。需要注意的是,元素的排序是从现在到过去,即起始位置为0表示当前条形。

当复制未知数量的数据时,建议使用动态数组作为目标数组,因为如果请求的数据量少于(或多于)目标数组的长度,函数会尝试重新分配内存,使请求的数据完全适应。
如果您知道需要复制的数据量,最好将其存储到静态分配的缓冲区中,以防止过度分配内存。
无论目标数组的属性是什么——as_series=true还是as_series=false。数据将被复制,使得最旧的元素位于数组分配的物理内存的起始位置。函数调用有3种方式。
通过第一个位置和所需元素数量调用
int CopyTickVolume(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
int start_pos, // start position
int count, // data count to copy
long volume_array[] // target array for tick volumes
);通过起始日期和所需元素数量调用
int CopyTickVolume(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
int count, // data count to copy
long volume_array[] // target array for tick volumes
);通过所需时间区间的起始和结束日期调用
int CopyTickVolume(
string symbol_name, // symbol name
ENUM_TIMEFRAMES timeframe, // period
datetime start_time, // start date and time
datetime stop_time, // stop date and time
long volume_array[] // target array for tick volumes
);参数
- symbol_name
[in] 符号名称。
- timeframe
[in] 周期。
- start_pos
[in] 第一个要复制的元素的起始位置。
- count
[in] 要复制的数据数量。
- start_time
[in] 第一个要复制的元素的开始时间。
- stop_time
[in] 最后一个要复制的元素的条形时间。
- volume_array[]
[out] 长类型的数组。
返回值
如果发生错误,则返回复制的数据数量或-1。
注意
如果请求的数据整个区间在服务器上的可用数据之外,函数将返回-1。如果请求的数据超出TERMINAL_MAXBARS(图表上最多条形数),函数也将返回-1。
如果请求的时间序列尚未构建或需要从服务器下载,函数将立即返回-1。
当通过起始日期和所需元素数量请求数据时,只返回日期早于或等于指定日期的数据。这意味着,任何条形开盘时间的值(成交量、价差、指标缓冲区上的值、开盘价、最高价、最低价、收盘价或开盘时间Time)始终小于或等于指定日期。
当在指定的日期范围内请求数据时,只返回该区间内的数据。区间以秒为单位计算。这意味着,任何条形开盘时间的值(成交量、价差、指标缓冲区上的值、开盘价、最高价、最低价、收盘价或开盘时间Time)始终在请求的区间内。
因此,如果当前日期是星期六,尝试复制周周期时间框架的数据,指定start_time=Last_Tuesday和stop_time=Last_Friday时,函数将返回0,因为周周期时间框架的开盘时间总是星期日,但一周的条形并不在指定的区间内。
如果您需要返回与当前未完成条形对应的值,可以使用第一种调用方式,指定start_pos=0和count=1。
示例:
#property strict
#property indicator_separate_window
#property indicator_buffers 1
//---- plot TickVolume
#property indicator_label1 "TickVolume"
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 C'143,188,139'
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int bars=3000;
//--- indicator buffers
double TickVolumeBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,TickVolumeBuffer,INDICATOR_DATA);
IndicatorSetInteger(INDICATOR_DIGITS,0);
//---
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
if(prev_calculated==0)
{
long timeseries[];
ArraySetAsSeries(timeseries,true);
int copied=CopyTickVolume(Symbol(),0,0,bars,timeseries);
for(int i=rates_total-copied-1;i>copied-1;i--) TickVolumeBuffer[i]=0.0;
for(int i=0;i<copied;i++) TickVolumeBuffer[i]=(double)timeseries[i];
Print("We have received the following number of TickVolume values: "+(string)copied);
}
else
{
long timeseries[];
int copied=CopyTickVolume(Symbol(),0,0,1,timeseries);
TickVolumeBuffer[0]=(double)timeseries[0];
}
//--- return value of prev_calculated for next call
return(rates_total);
}