-
Notifications
You must be signed in to change notification settings - Fork 17
Array API
The Array API provides access to several different expanded array options. Copy, Dimension, Maximum, Minimum, etc.
ArrayBSearch
ArrayCopy
ArrayCopyRates
ArrayCopySeries
ArrayGetAsSeries
ArrayDimension
ArrayInitialize
ArrayMaximum
ArrayMinimum
ArrayResize
ArraySetAsSeries
ArraySize
ArraySort
Searches for a specified value in a multidimensional numeric array sorted in the ascending order. The search is performed in the first dimension taking into account the AS_SERIES flag
protected int ArrayBsearch(Array array, T value, int count = WHOLE_ARRAY, int start = 0, int direction = (int)SearchDirection.MODE_ASCEND)
- array - Numeric array for search
- value - Value for search
- count - Count of elements to search for. By default, it searches in the whole array.
- start - Starting index to search for. By default, the search starts at the first element.
-
direction - Search direction. It can be any of the following values:
- MODE_ASCEND - searching in forward direction
- MODE_DESCEND - searching in backward direction
- The function returns index of a found element. If the wanted value isn't found, the function returns the index of an element nearest in value.
- Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function.
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] series 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]));
It copies an array into another one.
protected int ArrayCopy(Array destination, Array source, int startDest = 0, int startSource = 0, int count = WHOLE_ARRAY)
- destination - Destination array
- source - Source array
- startDest - Starting index from the destination array. By default, start index is 0
- startSource - Starting index for the source array. By default, start index is 0
- count - Number of elements that should be copied. By default, the whole array is copied (count=WHOLE_ARRAY)
- It returns the number of copied elements
- If count<=0 or count>src_size-src_start, all the remaining array part is copied. Arrays are copied from left to right. For series arrays, the starting position is correctly defined adjusted for copying from left to right. If an array is copied to itself, the result is undefined. If arrays are of different types, during copying it tries to transform each element of a source array into the type of the destination array. A string array can be copied into a string array only. Array of classes and structures containing objects that require initialization aren't copied. An array of structures can be copied into an array of the same type only. For static and dynamic arrays (except for class and structure members), the size of a destination array is automatically increased to the amount of copied data (if the latter exceeds the array size).
int src_data[10];
for (int i=0; i<ArraySize(src_data); i++) src_data[i]=i;
int dst_data[];
//--- copy data to dst_data[]
ArrayCopy(dst_data,src_data,0,0,WHOLE_ARRAY);
//--- print copied data[]
PrintFormat("Copied array size=%d",ArraySize(dst_data));
for (int i=0; i<ArraySize(dst_data); i++) PrintFormat("index=%d, value=%d",i,dst_data[i]);
Copies rates data to the array and returns the amount of bars copied
protected int ArrayCopyRates(Array array, string symbol = null, int timeFrame = 0)
- array - Destination array
- symbol - Symbol name. null means the current symbol
- timeFrame - Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe
- The function returns copied bars amount, or -1 if failed
ArrayCopyRates(double_array,null,0);
Copies a series array to another one and returns the count of the copied elements
protected int ArrayCopySeries(Array array, int series, string symbol = null, int timeFrame = 0)
- array - Destination array
- series - Series array identifier. It can be any of the Series array identifier enumeration values
- symbol - Symbol name. null means the current symbol
- timeFrame - Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe
- The function returns copied elements amount, or -1 if failed
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
It checks direction of an array index
protected bool ArrayGetAsSeries(Array array)
- array - Checked array
- Returns true, if the specified array has the AS_SERIES flag set, i.e. access to the array is performed back to front as in timeseries. A timeseries differs from a usual array in that the indexing of timeseries elements is performed from its end to beginning (from the newest data to old)
ArrayGetAsSeries(buffer);
Returns the multidimensional array rank
protected int ArrayDimension(object array)
- array - Array for which the rank will be returned
- Rank (dimension) of multidimensional array or -1 if error
int num_array[10][5];
int dim_size=ArrayDimension(num_array);// dim_size=2
Print("Dimension of num_array=",dim_size);
The function initializes a numeric array by a preset value
protected int ArrayInitialize(Array array, T value)
- array - Numeric array that should be initialized
- value - New value that should be set to all array elements
- No return value
ArrayInitialize(array,0);
The function searches a maximal element in a one-dimension numeric array
protected int ArrayMaximum(Array array, int count = WHOLE_ARRAY, int start = 0)
- array - A numeric array, in which search is made
- count - Number of elements for search. By default, searches in the entire array (count=WHOLE_ARRAY)
- start - Index to start checking with
- Index of a found element. In case of failure it returns -1
double num_array[15]={4,1,6,3,19,4,2,6,3,9,4,5,6,3,9};
int maxValueIdx=ArrayMaximum(num_array,WHOLE_ARRAY,0);
Print("Max value = ",num_array[maxValueIdx]," at index=",maxValueIdx);
The function searches a minimal element in a one-dimension numeric array
protected int ArrayMinimum(Array array, int count = WHOLE_ARRAY, int start = 0)
- array - A numeric array, in which search is made
- count - Number of elements for search. By default, searches in the entire array (count=WHOLE_ARRAY)
- start - Index to start checking with
- Index of a found element. In case of failure it returns -1
double num_array[15]={4,1,6,3,19,4,2,6,3,9,4,5,6,3,9};
int minValueIdx=ArrayMinimum(num_array,WHOLE_ARRAY,0);
Print("Min value = ",num_array[minValueIdx]," at index=",minValueIdx);
The function sets a new size for the first dimension
protected int ArrayResize(Array data, int newSize)
- data - array for changing sizes
- newSize - New size for the first dimension
- If executed successfully, it returns count of all elements contained in the array after resizing, otherwise, returns -1, and array is not resized
double arr[];
ArrayResize(arr,100000);
The function sets the AS_SERIES flag to a selected object of a dynamic array, and elements will be indexed like in timeseries
protected bool ArraySetAsSeries(Array data, bool b)
- data - Numeric array to set
- b - Numeric array to set. True denotes reverse order of indexing
- Return previous value of AS_SERIES flag
ArraySetAsSeries(array,true);
The function returns the number of elements of a selected array
protected int ArraySize(Array array)
- array - array to get size
- Returns size of array
int size = ArraySize(array);
Sort numeric arrays in ascending order from left to right
protected int ArraySort(Array data, int count = WHOLE_ARRAY, int start = 0, int sortDir = MODE_ASCEND)
- data - Array for sortDir
- count - Number of elements for sorting (WHOLE_ARRAY default)
- start - Index of start element for sorting (0 default)
- sortDir - Sort direction (MODE_ASCEND default)
- Returns array elements count
ArraySort(num_array,WHOLE_ARRAY,0,MODE_DESCEND);