MQL4 Array Functions

Hi folks,

We have talked about the Arrays in a previous article and we covered all the aspects of the Arrays in general and in MQL4 in particular. But MQL4 concerned more about the arrays and for that it built a set of functions handling the arrays.

Today will talk about this set of build-in MQL4 functions one by one.

  


ArrayBsearch:

Syntax:

int ArrayBsearch( double array[], double value, int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND)

Description:

The ArrayBsearch function searches an array for the occurrence of a value. The function returns the index of the first occurrence of the value if the value is found in the array and returns the nearest one if the value is not found.

The function searches only the first dimension of the array and cannot be used with the arrays of strings or serial numbers.

Note: You have to sort the numeric arrays using ArraySort() function before performing binary search.

 Parameters:

double array[]
The numeric array you want to search it.

double value
The value you are searching for in the array.

int count
The count of elements in the array you want to search them. By default the function searches the whole of the array (WHOLE_ARRAY or 0).

int start
The index you want to start from your searching. By default, the search starts on the first element (0).

int direction
The searching direction; It can be one of the following values:
MODE_ASCEND: forward direction searching;
MODE_DESCEND: backward direction searching.

Example:

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


ArrayCopy:

Syntax:

int ArrayCopy( object& dest[], object source[], int start_dest=0, int start_source=0, int count=WHOLE_ARRAY)

Description:

The ArrayCopy function copies an array to another array. The source and destination arrays must be of the same type, but arrays with type double[], int[], datetime[], color[], and bool[] can be copied as arrays with same type.

The function returns the number of the elements has been copied.

 Parameters:

object& dest[]
The destination array - the array you want to copy to.

object source[]
The source array - the array you want to copy from.

int start_dest
The starting index for the destination array. By default, start index is 0.

int start_source
The starting index for the source array. By default, start index is 0.

int count
The count of elements that should be copied. By default, all the elements of the source array (WHOLE_ARRAY).

Example

 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


ArrayCopyRates:

Syntax:

int ArrayCopyRates( double& dest_array[], string symbol=NULL, int timeframe=0)

Description:

The ArrayCopyRates function copies the rate from the chart RateInfo array to a two-dimensional array where the second dimension has these elements.

0 – time;
1 – open;
2 – low;
3 – high;
4 – close;
5 - volume.

Note: The numbers in front of the above rate are the index in the array.

Parameters:

double& dest_array[]
The array you want to copy the chart rate info to.

string symbol
The symbol name of the currency pair you want to get the rate info. Default (NULL) means the current currency used in the chart.

int timeframe
The chart periodicity you want to use. It can be any of the following values:

Constant

Value

Description

PERIOD_M1

1

1 minute.

PERIOD_M5

5

5 minutes.

PERIOD_M15

15

15 minutes.

PERIOD_M30

30

30 minutes.

PERIOD_H1

60

1 hour.

PERIOD_H4

240

4 hour.

PERIOD_D1

1440

Daily.

PERIOD_W1

10080

Weekly.

PERIOD_MN1

43200

Monthly.

0 (zero)

0

The current Time frame used on the chart.

Use 0 if you want the current timeframe of the chart.

 Example:

double array1[][6];
ArrayCopyRates(array1,"EURUSD", PERIOD_H1);
 Print("Current bar ",TimeToStr(array1[0][0]),"Open", array1[0][1]);


ArrayCopySeries:

Syntax:

int ArrayCopySeries( double& array[], int series_index, string symbol=NULL, int timeframe=0)

Description:

The ArrayCopyRates function copies a series array to another array. The function returns the number of the elements has been copied.

Parameters:

double& array[]
The array you want to copy to.

int series_index
The type of the series array you want to copy. It can be any of following values:

Constant

Value

Description

MODE_OPEN

0

Open price.

MODE_LOW

1

Low price.

MODE_HIGH

2

High price.

MODE_CLOSE

3

Close price.

MODE_VOLUME

4

Volume, used in Lowest() and Highest() functions.

MODE_TIME

5

Bar open time, used in ArrayCopySeries() function.

Note: If the series_index is MODE_TIME, the first distension array must be a datetime array.

 string symbol
The symbol name of the currency pair you want to get the rate info. Default (NULL) means the current currency used in the chart.

int timeframe
The chart periodicity you want to use. Use 0 if you want the current timeframe of the chart.

 Example:

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

  ArrayDimension:

Syntax:

int ArrayDimension( int array[])

Description:

The ArrayDimension function returns the dimensions count of the given array.

Parameters:

int array[]
The array you want to retrieve its dimensions count.

Example:

int num_array[10][5];
 int dim_size;
 dim_size=ArrayDimension(num_array);
 // dim_size is 2


ArrayGetAsSeries:

Syntax:

bool ArrayGetAsSeries( object array[])

Description:

The ArrayGetAsSeries function checks the elements of given array and retruns true if the array is a series array (array elements indexed from last to first) and false otherwise.

Parameters:

int array[]
The array you want to check  its elements.

Example:

if(ArrayGetAsSeries(array1)==true)
 Print("array1 is indexed as a series array");
 else
 Print("array1 is indexed normally (from left to right)");

ArrayInitialize:

Syntax:

int ArrayInitialize( double& array[], double value)

Description:

The ArrayInitialize function sets all the elements of the given numeric array to the same value. The function returns the count of the elements in the created array.

Parameters:

double& array[]
The numeric array you want to initialize.

double value
The value you want to set all the elements of the array to.

Example:

//---- setting all elements of array to 2.1
 double myarray[10];
 ArrayInitialize(myarray,2.1);


ArrayIsSeries:

Syntax:

bool ArrayIsSeries( object array[])

Description:

The ArrayIsSeries function checks the given array if it’s a series array (time, open, close, high, low, or volume) or not. The function returns true if the array is a series array and false otherwise.

Parameters:

object array[]
The array you want to check.

Example:

if(ArrayIsSeries(array1)==false)
 ArrayInitialize(array1,0);
 else
 {
 Print("Series array cannot be initialized!");
 return(-1);
 }


ArrayMaximum:

Syntax:

int ArrayMaximum( double array[], int count=WHOLE_ARRAY, int start=0)

Description:

The ArrayMaximum function searches the elements of the given array for the maximum value. The function returns the index of the maximum value

Parameters:

double array[]
The array you want to search.

int count
The count of elements in the array you want to search them. By default the function searches the whole of the array (WHOLE_ARRAY or 0).

int start
The index you want to start from your searching. By default, the search starts on the first element (0).

Example:

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


ArrayMinimum:

Syntax:

int ArrayMinimum ( double array[], int count=WHOLE_ARRAY, int start=0)

Description:

The ArrayMinimum function searches the elements of the given array for the minimum value. The function returns the index of the minimum value

Parameters:

double array[]
The array you want to search.

int count
The count of elements in the array you want to search them. By default the function searches the whole of the array (WHOLE_ARRAY or 0).

int start
The index you want to start from your searching. By default, the search starts on the first element (0).

Example:

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


ArrayRange:

Syntax:

int ArrayRange( object array[], int range_index)

Description:

The ArrayRange function returns the count of the elements of the given dimension of the given array.

Note: Because the arrays in MQL4 are zero-based array, the size of the array is the largest index + 1.

Parameters:

object array[]
The array you want to check.

int range_index
The dimension you want to check.

Example:

int dim_size;
 double num_array[10,10,10];
 dim_size=ArrayRange(num_array, 1);


ArrayResize:

Syntax:

int ArrayResize( object& array[], int new_size)

Description:

The ArrayResize function sets a new size for the first dimension of the given array. The function returns the count of the elements of the resized array if it successfully resized the array and 0 otherwise.

Parameters:

Object& array[]
The array you want to resize.

int new_size
The new size.

Example:

double array1[][4];
 int element_count=ArrayResize(array, 20);
 // element count is 80 elements


ArraySetAsSeries:

Syntax:

bool ArraySetAsSeries( double& array[], bool set)

Description:

The ArraySetAsSeries function reverses the index order of the given array to a series array. The function returns true in success and false otherwise.

 Parameters:

double & array[]
The array you want to reverse its elements.

bool set
The series flag to set (true) or drop (false).

Example:

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


ArraySize:

Syntax:

int ArraySize( object array[])

Description:

The ArraySize function returns the size of the given array.

 Parameters:

object  array[]
The array (any type) you want to get its size.

Example:

int count=ArraySize(array1);
 for(int i=0; i<count; i++)
 {
 // do some calculations.
 }


ArraySort:

Syntax:

int ArraySort( double& array[], int count=WHOLE_ARRAY, int start=0, int sort_dir=MODE_ASCEND)

Description:

The ArraySort function sorts the first dimension of the given numeric array.

Note: Series arrays can't be sorted by this function.

 Parameters:

double& array[]
The numeric array you want to sort.

int count
The count of elements in the array you want to sort them. By default the function sorts the whole of the array (WHOLE_ARRAY or 0).

int start
The index you want to start from your sorting from. By default the sort starts from the first element (0).

int sort_dir
The sorting direction; It can be one of the following values:
MODE_ASCEND: sort ascending;
MODE_DESCEND: sort descending. 

Example:

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

 

I hope you enjoyed the article!
Coders Guru