Skip to content
kabirsandhu edited this page Jun 9, 2016 · 1 revision

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

ArrayBSearch

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)

Parameters

  • 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

Returns

  • 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.

Note

  • Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function.

Example

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]));

ArrayCopy

It copies an array into another one.

protected int ArrayCopy(Array destination, Array source, int startDest = 0, int startSource = 0, int count = WHOLE_ARRAY)

Parameters

  • 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)

Returns

  • It returns the number of copied elements

Note

  • 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).

Example

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]);

ArrayCopyRates

Copies rates data to the array and returns the amount of bars copied

protected int ArrayCopyRates(Array array, string symbol = null, int timeFrame = 0)

Parameters

  • 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

Returns

  • The function returns copied bars amount, or -1 if failed

Example

ArrayCopyRates(double_array,null,0);

ArrrayCopySeries

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)

Parameters

  • 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

Returns

  • The function returns copied elements amount, or -1 if failed

Example

ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);

ArrayGetAsSeries

It checks direction of an array index

protected bool ArrayGetAsSeries(Array array)

Parameters

  • array - Checked array

Returns

  • 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)

Example

ArrayGetAsSeries(buffer);

ArrayDimension

Returns the multidimensional array rank

protected int ArrayDimension(object array)

Parameters

  • array - Array for which the rank will be returned

Returns

  • Rank (dimension) of multidimensional array or -1 if error

Example

int num_array[10][5];
int dim_size=ArrayDimension(num_array);// dim_size=2
Print("Dimension of num_array=",dim_size);

ArrayInitialize

The function initializes a numeric array by a preset value

protected int ArrayInitialize(Array array, T value)

Parameters

  • array - Numeric array that should be initialized
  • value - New value that should be set to all array elements

Returns

  • No return value

Example

ArrayInitialize(array,0);

ArrayMaximum

The function searches a maximal element in a one-dimension numeric array

protected int ArrayMaximum(Array array, int count = WHOLE_ARRAY, int start = 0)

Parameters

  • 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

Returns

  • Index of a found element. In case of failure it returns -1

Example

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);

ArrayMinimum

The function searches a minimal element in a one-dimension numeric array

protected int ArrayMinimum(Array array, int count = WHOLE_ARRAY, int start = 0)

Parameters

  • 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

Returns

  • Index of a found element. In case of failure it returns -1

Example

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);

ArrayResize

The function sets a new size for the first dimension

protected int ArrayResize(Array data, int newSize)

Parameters

  • data - array for changing sizes
  • newSize - New size for the first dimension

Returns

  • If executed successfully, it returns count of all elements contained in the array after resizing, otherwise, returns -1, and array is not resized

Example

double arr[];
ArrayResize(arr,100000);

ArraySetAsSeries

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)

Parameters

  • data - Numeric array to set
  • b - Numeric array to set. True denotes reverse order of indexing

Returns

  • Return previous value of AS_SERIES flag

Example

ArraySetAsSeries(array,true);

ArraySize

The function returns the number of elements of a selected array

protected int ArraySize(Array array)

Parameters

  • array - array to get size

Returns

  • Returns size of array

Example

int size = ArraySize(array);

ArraySort

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)

Parameters

  • 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

  • Returns array elements count

Example

ArraySort(num_array,WHOLE_ARRAY,0,MODE_DESCEND);