第九节 数组函数英文

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Array functions
ArrayBsearch()
ArrayCopy()
ArrayCopyRates()
ArrayCopySeries()
rrayDimension()
ArrayGetAsSeries()
ArrayInitialize()
rayIsSeries()
ayMaximum()
ayMinimum()
Range()
yResize()
ArraySetAsSeries()
ize()
ort()
int ArrayBsearch( double array[], double value, int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND)
Returns a index to the first occurrence of value in the first dimension of specified array if found or nearest if it isn't.
The function can't be used with string arrays and serial numeric arrays.
Note: Binary search processes sorted arrays only. To sort numeric arrays use ort() functions. Parameters
array[]- The numeric array to search.
value- The value to search for.
count- Elements count to search. By default search in the whole array.
start- Starting index to search. By default search starts from first element.
direction- Search direction. It can be any one of the following values:
MODE_ASCEND searching in forward direction,
MODE_DESCEND searching in backward direction.
Sample
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] timeseries are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]>=daytimes[0]) dayshift=0;
else
dayshift =
ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()<PERIOD_D1) dayshift++;
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ",
TimeToStr(daytimes[dayshift]));
int ArrayCopy( object& dest[], object source[], int start_dest=0, int start_source=0, int count=WHOLE_ARRAY)
Copies one array to another array. Arrays must be same type, but double[], int[], datetime[], color[] and bool[] arrays can be copied as one type.
Returns actual copied elements count.
Parameters
dest[]- Destination array.
source[]- Source array.
start_dest- Starting index for the destination array. By default start index is 0.
start_source- Starting index for the source array. By default start index is 0.
count- Elements count to copy. By default is WHOLE_ARRAY constant.
Sample
double array1[][6];
double array2[10][6];
// fill array with some data
ArrayCopyRates(array1);
ArrayCopy(array2, array1,0,Bars-9,10);
// now array2 has first 10 bars in the history
int ArrayCopyRates( d ouble& dest_array[], string symbol=NULL,
int period=0)
Copies rates to the two dimensions array from chart's RateInfo array, where second dimension has 6 elements:
0 - time,
1 - open,
2 - low,
3 - high,
5 - volume.
Note: Usually retrieved array used to pass large blocks of data to the DLL functions.
Parameters
dest_array[]- Reference to the two dimensional destination numeric array.
symbol- symbol name, by default used current chart symbol name.
period- Time frame, by default used current chart period. It can be any one of ration" Time frame enumeration values.
Sample
double array1[][6];
ArrayCopyRates(array1,"EURUSD", PERIOD_H1);
Print("Current bar ",TimeToStr(array1[0][0])," Open ", array1[0][1]);
int ArrayCopySeries( double& array[], int series_index, string symbol=NULL, int period=0)
Copies some series array to another array and returns copied item count.
Note: When series_identifier is MODE_TIME first parameter must be a datetime array. Parameters
array[]- Reference to the destination one-dimensional numeric array.
series_index- Series array identifier. It can be any one of ntifier" Series array identifiers enumeration values.
symbol- Symbol name, by default used current chart symbol name.
period- Time frame, by default used current chart period. It can be any one of ration" Time frame enumeration values.
Sample
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] timeseries are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]>=daytimes[0]) dayshift=0;
else
{
dayshift =
ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()<PERIOD_D1) dayshift++;
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ",
TimeToStr(daytimes[dayshift]));
int ArrayDimension( o bject array[])
Returns array dimensions count.
Parameters
array[]- array to retrieve dimensions count.
Sample
int num_array[10][5];
int dim_size;
dim_size = ArrayDimension(num_array);
// dim_size is 2
bool ArrayGetAsSeries( o bject array[])
Returns true if array is organized as series array (array elements indexed from last to first) otherwise return false.
Parameters
array[]- Array to check.
Sample
if(ArrayGetAsSeries(array1)==true)
Print("array1 is indexed as series array");
else
Print("array1 is normal indexed (from left to right)");
int ArrayInitialize( d ouble& array[], double value)
Sets all elements of numeric array to same value. Returns initialized element count.
Note: There is useless to initialize index buffers in the custom indicator's init() function. Parameters
array[]- Numeric array to initialize.
value- New value to set.
Sample
//---- setting all elements of array to 2.1
double myarray[10];
ArrayInitialize(myarray,2.1);
bool ArrayIsSeries( o bject array[])
Returns true if checked array is series array (time,open,close,high,low or volume).
Parameters
array[]- Array to check.
Sample
if(ArrayIsSeries(array1)==false)
ArrayInitialize(array1,0);
else
{
Print("Series array cannot be initialized!");
return(-1);
}
int ArrayMaximum( d ouble array[], int count=WHOLE_ARRAY, int start=0)
Searches element with maximum value and returns it's position.
Parameters
array[]- The numeric array to search.
count- Scan for count elements in array.
start- Start searching from start index.
Sample
double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
int maxValueIdx = ArrayMaximum(num_array);
Print("Max value = ", num_array[maxValueIdx]);
int ArrayMinimum( d ouble array[], int count=WHOLE_ARRAY, int start=0)
Searches element with minimum value and returns it's position.
Parameters
array[]- The numeric array to search.
count- Scan for count elements in array.
start- Start searching from start index.
Sample
double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
double minValueidx = ArrayMinimum(num_array);
Print("Min value = ", num_array[minValueIdx]);
int ArrayRange( o bject array[], int range_index)
Returns elements count in the pointed dimension of the array. Since indexes are zero-based, the size of dimension is 1 greater than the largest index.
Parameters
array[]- Array to check
range_index- Dimension index.
Sample
int dim_size;
double num_array[10,10,10];
dim_size=ArrayRange(num_array, 1);
int ArrayResize( o bject& array[], int new_size)
Sets new size to the first dimension. If success returns count of all elements contained in the array after resizing, otherwise returns zero and array is not resized.
Parameters
array[]- Array to resize.
new_size- New size for the first dimension.
Sample
double array1[10][4];
int element_count = ArrayResize(array, 20);
// element count is 80 elements
bool ArraySetAsSeries( d ouble& array[], bool set)
Sets indexing order of the array like a series arrays, i.e. last element has zero index. Returns previous state.
Parameters
array[]- The numeric array to set.
set- The Series flag to set (true) or drop (false).
Sample
double macd_buffer[300];
double signal_buffer[300];
int i,limit=ArraySize(macd_buffer);
ArraySetAsSeries(macd_buffer,true);
for(i=0; i<limit; i++)
macd_buffer[i]=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26 ,0,MODE_EMA,PRICE_CLOSE,i);
for(i=0; i<limit; i++)
signal_buffer[i]=iMAOnArray(macd_buffer,limit,9,0,MODE_SMA,i); int ArraySize( o bject array[])
Returns the count of elements contained in the array.
Parameters
array[]- Array of any type.
Sample
int count=ArraySize(array1);
for(int i=0; i<count; i++)
{
// do some calculations.
}
int ArraySort( double& array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND)
Sorts numeric arrays by first dimension. Series arrays can't be sorted by ArraySort(). Parameters
array[]- The numeric array to sort.
count- Count of elements to sort.
start- Starting index.
sort_dir- Array sorting direction. It can be any one of the following values:
MODE_ASCEND - sort ascending,
MODE_DESCEND - sort descending.
Sample
double num_array[5]={4,1,6,3,9};
// now array contains values 4,1,6,3,9
ArraySort(num_array);
// now array is sorted 1,3,4,6,9
ArraySort(num_array,MODE_DESCEND);
// now array is sorted 9,6,4,3,1
数组函数[Array Functions]
int ArrayBsearch( double array[], double value, int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND)
搜索一个值在数组中的位置
此函数不能用在字符型或连续数字的数组上.
:: 输入参数
array[] - 需要搜索的数组
value - 将要搜索的值
count - 搜索的数量,默认搜索所有的数组
start - 搜索的开始点,默认从头开始
direction - 搜索的方向,MODE_ASCEND 顺序搜索MODE_DESCEND 倒序搜索
示例:
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] timeseries are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]&gt>=daytimes[0]) dayshift=0;
else
{
dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()<PERIOD_D1)
dayshift++;
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ", TimeToStr(daytimes[dayshift]));
int ArrayCopy( object& dest[], object source[], int start_dest=0, int
start_source=0, int count=WHOLE_ARRAY)
复制一个数组到另外一个数组。

只有double[], int[], datetime[], color[], 和bool[] 这些类型的数组可以被复制。

:: 输入参数
dest[] - 目标数组
source[] - 源数组
start_dest - 从目标数组的第几位开始写入,默认为0
start_source - 从源数组的第几位开始读取,默认为0
count - 读取多少位的数组
示例:
double array1[][6];
double array2[10][6];
// fill array with some data
ArrayCopyRates(array1);
ArrayCopy(array2, array1,0,Bars-9,10);
// now array2 has first 10 bars in the history
int ArrayCopyRates( double& dest_array[], string symbol=NULL, int timeframe=0) 复制一段走势图上的数据到一个二维数组,数组的第二维只有6个项目分别是:
0 - 时间,
1 - 开盘价,
2 - 最低价,
3 - 最高价,
4 - 收盘价,
5 - 成交量.
:: 输入参数
dest_array[] - 目标数组
symbol - 标示,当前所需要的通货的标示
timeframe - 图表的时间线
示例:
double array1[][6];
ArrayCopyRates(array1,"EURUSD", PERIOD_H1);
Print("Current bar ",TimeToStr(array1[0][0]),"Open", array1[0][1]);
int ArrayCopySeries( double& array[], int series_index, string symbol=NULL, int timeframe=0)
复制一个系列的走势图数据到数组上
注: 如果series_index是MODE_TIME, 那么第一个参数必须是日期型的数组
:: 输入参数
dest_array[] - 目标数组
series_index - 想要取的系列的名称或编号,0-5
symbol - 标示,当前所需要的通货的标示
timeframe - 图表的时间线
示例:
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] timeseries are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]>=daytimes[0]) dayshift=0;
else
{
dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ", TimeToStr(daytimes[dayshift]));
int ArrayDimension( int array[])
返回数组的维数
:: 输入参数
array[] - 需要检查的数组
示例:
int num_array[10][5];
int dim_size;
dim_size=ArrayDimension(num_array);
// dim_size is 2
bool ArrayGetAsSeries(object array[])
检查数组是否是有组织序列的数组(是否从最后到最开始排序过的),如果不是返回否:: 输入参数
array[] - 需要检查的数组
示例:
if(ArrayGetAsSeries(array1)==true)
Print("array1 is indexed as a series array");
else
Print("array1 is indexed normally (from left to right)");
int ArrayInitialize( double& array[], double value)
对数组进行初始化,返回经过初始化的数组项的个数
:: 输入参数
array[] - 需要初始化的数组
value - 新的数组项的值
示例:
//---- 把所有数组项的值设置为2.1
double myarray[10];
ArrayInitialize(myarray,2.1);
bool ArrayIsSeries( object array[])
检查数组是否连续的(time,open,close,high,low, or volume).
:: 输入参数
array[] - 需要检查的数组
示例:
if(ArrayIsSeries(array1)==false)
ArrayInitialize(array1,0);
else
{
Print("Series array cannot be initialized!");
return(-1);
}
int ArrayMaximum( double array[], int count=WHOLE_ARRAY, int start=0) 找出数组中最大值的定位
:: 输入参数
array[] - 需要检查的数组
count - 搜索数组中项目的个数
start - 搜索的开始点
示例:
double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
int maxValueIdx=ArrayMaximum(num_array);
Print("Max value = ", num_array[maxValueIdx]);
int ArrayMinimum( double array[], int count=WHOLE_ARRAY, int start=0) 找出数组中最小值的定位
:: 输入参数
array[] - 需要检查的数组
count - 搜索数组中项目的个数
start - 搜索的开始点
示例:
double num_array[15]={4,1,6,3,9,4,1,6,3,9,4,1,6,3,9};
double minValueidx=ArrayMinimum(num_array);
Print("Min value = ", num_array[minValueIdx]);
int ArrayRange( object array[], int range_index)
取数组中指定维数中项目的数量。

:: 输入参数
array[] - 需要检查的数组
range_index - 指定的维数
示例:
int dim_size;
double num_array[10,10,10];
dim_size=ArrayRange(num_array, 1);
int ArrayResize( object& array[], int new_size)
重定义数组大小
:: 输入参数
array[] - 需要检查的数组
new_size - 第一维中数组的新大小
示例:
double array1[][4];
int element_count=ArrayResize(array, 20);
// 数组中总项目数为80
bool ArraySetAsSeries( double& array[], bool set)
设置指数数组为系列数组,数组0位的值是最后的值。

返回之前的数组状态:: 输入参数
array[] - 需要处理的数组
set - 是否是设置为系列数组,true或者false
示例:
double macd_buffer[300];
double signal_buffer[300];
int i,limit=ArraySize(macd_buffer);
ArraySetAsSeries(macd_buffer,true);
for(i=0; i
macd_buffer[i]=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26,0,MO DE_EMA,PRICE_CLOSE,i);
for(i=0; i
signal_buffer[i]=iMAOnArray(macd_buffer,limit,9,0,MODE_SMA,i);
int ArraySize( object array[])
返回数组的项目数
:: 输入参数
array[] - 需要处理的数组
示例:
int count=ArraySize(array1);
for(int i=0; i
{
// do some calculations.
}
int ArraySort( double& array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND)
对数组进行排序,系列数组不可进行排序
:: 输入参数
array[] - 需要处理的数组
count - 对多少个数组项进行排序
start - 排序的开始点
sort_dir - 排序方式,MODE_ASCEND顺序排列MODE_DESCEND倒序排列
示例:
double num_array[5]={4,1,6,3,9};
// now array contains values 4,1,6,3,9
ArraySort(num_array);
// now array is sorted 1,3,4,6,9
ArraySort(num_array,MODE_DESCEND);
// now array is sorted 9,6,4,3,1
类型转换函数[Conversion Functions]
string CharToStr( int char_code)
将字符型转换成字符串型结果返回
:: 输入参数
char_code - 字符的ACSII码
string str="WORL" + CharToStr(44); // 44 is code for 'D' // resulting string will be WORLD
string DoubleToStr( double value, int digits)
将双精度浮点型转换成字符串型结果返回
:: 输入参数
value - 浮点型数字
digits - 小数点后多少位,0-8
示例:
string value=DoubleToStr(1.28473418, 5);
// value is 1.28473
double NormalizeDouble( double value, int digits)
将双精度浮点型格式化后结果返回
:: 输入参数
value - 浮点型数字
digits - 小数点后多少位,0-8
示例:
double var1=0.123456789;
Print(NormalizeDouble(var1,5));
// output: 0.12346
double StrToDouble( string value)
将字符串型转换成双精度浮点型结果返回
:: 输入参数
value - 数字的字符串
示例:
double var=StrToDouble("103.2812");
int StrToInteger( string value)
将字符串型转换成整型结果返回
:: 输入参数
value - 数字的字符串
int var1=StrToInteger("1024");
datetime StrToTime( string value)
将字符串型转换成时间型结果返回,输入格式为yyyy.mm.dd hh:mi
:: 输入参数
value - 时间的字符串
示例:
datetime var1;
var1=StrToTime("2003.8.12 17:35");
var1=StrToTime("17:35"); // returns with current date
var1=StrToTime("2003.8.12"); // returns with midnight time "00:00"
string TimeToStr( datetime value, int mode=TIME_DATE|TIME_MINUTES)
将时间型转换成字符串型返回
:: 输入参数
value - 时间的数字,从1970.1.1 0:0:0 到现在的秒数
mode - 返回字符串的格式
TIME_DATE(yyyy.mm.dd),TIME_MINUTES(hh:mi),TIME_SECONDS(hh:mi:ss) 示例:
strign var1=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
他和前面的不同。

相关文档
最新文档