MQL4 Reference Array Functions ArrayCopySeries

ArrayCopySeries

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

int  ArrayCopySeries(
   void&  array[],           // destination array
   int    series_index,      // series array identifier
   string symbol=NULL,       // symbol
   int    timeframe=0        // timeframe
   );

Parameters

array[]

[out]  Destination array of double type.

series_index

[in]  Series array identifier. It can be any of the Series array identifier enumeration values.

symbol

[in]  Symbol name.

timeframe

[in]  Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.

Returned value

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

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

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 series_index is MODE_TIME, the array to be passed to the function must be of the datetime type.

Example:

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