myeatrade

MQL4 Reference

MQL4 Array functions (1)

A group of functions to work with arrays.

Arrays are allowed to be maximum four-dimensional. Each dimension is indexed from 0 to dimension size-1. In a particular case of a one-dimensional array of 50 elements, calling of the first element will appear as array[0], of the last one – as array[49].

Using these functions (except for those which change quantitative and qualitative characteristics of the array) one can process predefined time series Time[], Open[], High[], Low[], Close[], Volume[]

 

ArrayBsearch()

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

If the element with the specified value doesn’t exist in the array, the function returns the index of the nearest smallest value of the elements between which the searched value is located.

The function cannot be used with string arrays and series arrays (with the exception of the series array of the bar open time).

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

Parameters:

array[]   - The numeric array to search for. 
value     - The value to search for. 
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. 

Sample:

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

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

Copies an array to another one. Arrays must be of the same type, but arrays with type double[], int[], datetime[], color[], and bool[] can be copied as arrays of the same type.

Returns the amount of copied elements.

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        - The count of elements that should be copied. By default, it is WHOLE_ARRAY constant. 

Sample:

  double array1[][6];
  double array2[10][6];
  // array2 is filled with some data
  ArrayCopyRates(array1);
  ArrayCopy(array2,array1,0,0,60);
  // array2 is having the first 10 bars from the history now (first bar considered as bar with index [Bars-1])
  ArrayCopy(array2,array1,0,Bars*6-60,60);
  // array2 is having the last 10 bars from the history now (last bar considered as current bar, bar wit index [0])

ArrayCopyRates()

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

Copies rates to the two-dimensional array from chart RateInfo array and returns copied bars amount, or -1 if failed. First dimension of RateInfo array contains bars amount, second dimension has 6 elements:

  • 0 – time,
  • 1 – open,
  • 2 – low,
  • 3 – high,
  • 4 – close,
  • 5 – volume.

If data (symbol name and/or timeframe differ from the current ones) are requested from another chart, the situation is possible that the corresponding chart was not opened in the client terminal and the necessary data must be requested from the server. In this case, error ERR_HISTORY_WILL_UPDATED (4066 – the requested history data are under updating) will be placed in the last_error variable, and one will has to re-request (see example of ArrayCopySeries()).

Notes: This rates array is normally used to pass data to a DLL function.

Memory is not really allocated for data array, and no real copying is performed. When such an array is accessed, the access will be redirected.

Parameters:

dest_array[] - Reference to the two-dimensional destination array of double type. 
symbol       - Symbol name (currency pair name) 
timeframe    - Timeframe. It can be any of the listed timeframes values. 

Sample:

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

ArrayCopySeries()

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

Copies a series array to another one and returns the count of the copied elements.

There is no real memory allocation for data array and nothing is copied. When such an array is accessed, the access is redirected. Excluded are arrays that are assigned as indexed ones in custom indicators. In this case, data are really copied.

If data are copied from another chart with different symbol and/or timeframe, it is possible that the necessary data will lack. In this case, error ERR_HISTORY_WILL_UPDATED (4066 – requested history data under updating) will be placed into the last_error variable, and there will be necessary to retry copying after a certain period of time.

Note: If series_index is MODE_TIME, the array to be passed to the function must be of the datetime type.

Parameters:

array[]      - Reference to the destination one-dimensional numeric array. 
series_index - Series array identifier. It must be one of series array listed identifiers values. 
symbol       - Symbol name (the name of the currency pair) 
timeframe    - Timeframe of the chart. It can be any of Timeframe list values. 

Sample:

datetime daytimes[];
int      shift=10,dayshift,error;
//---- the Time[] array was sroted in the descending order
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
error=GetLastError();
if(error==4066)
  {
   //---- make two more attempts to read
   for(int i=0;i<2; i++)
     {
      Sleep(5000);
      ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
      //---- check the current daily bar time
      datetime last_day=daytimes[0];
      if(Year()==TimeYear(last_day) && Month()==TimeMonth(last_day) && Day()==TimeDay(last_day)) break;
     }
  }
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]));
This article url: http://www.myeatrade.com/431/

Please login to post comment, if you haven't account you can register free