CopySeries refuses to work, 3 AIs and I can't see why

 

I created a script to show the error. When I tried to run it in the original script, it gave me:

Error: <4113> [CHART_WINDOW_NOT_FOUND]

... even if I hard-coded the symbol "USDJPY" (which appears as the first in the list in my Market Watch window in the terminal), and used "_Period" as the timeframe. 

Since I put it in the example script (below), it doesn't give me that bizarre error, in fact it gives no error at all...

But neither does it give me any data.


double          daOpen[];
double          daHigh[];
double          daLow[];
double          daClose[];
datetime        dtaTime[];

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
        string sSymbol                                                                  = "USDJPY";
//
   ResetLastError();
   int iBarsCNT1                                        = CopySeries(sSymbol,PERIOD_M5,0,WHOLE_ARRAY,COPY_RATES_TIME|COPY_RATES_OPEN|COPY_RATES_HIGH|COPY_RATES_LOW|COPY_RATES_CLOSE,dtaTime,daOpen,daHigh,daLow,daClose);
   if(iBarsCNT <= 0)
   {
                Print("ChartID<",ChartID(),">: ["+__FILE__+" ["+__FUNCTION__+"] LINE "+(string)__LINE__+" - CopySeries() using detailed rates mask FAILED, aborting..."," Error: <", 
                GetLastError(),"> [",GetLastErrorMsg(GetLastError()),"] sSymbol<",sSymbol,"> timeframe<",_Period,"> iBarsCNT1<",iBarsCNT1,">");  
   }

   ResetLastError();
   int iBarsCNT2                                        = CopySeries(sSymbol,PERIOD_M5,0,WHOLE_ARRAY,COPY_RATES_OHLCT,dtaTime,daOpen,daHigh,daLow,daClose);
   if(iBarsCNT2 <= 0)
   { 
        Print("ChartID<",ChartID(),">: ["+__FILE__+" ["+__FUNCTION__+"] LINE "+(string)__LINE__+" - CopySeries()  using COPY_RATES_OHLCT FAILED, aborting..."," Error: <", 
                GetLastError(),"> [",GetLastErrorMsg(GetLastError()),"] sSymbol<",sSymbol,"> timeframe<",_Period,"> iBarsCNT2<",iBarsCNT2,">");  
   }
}


Here's the output:

2024.06.20 16:02:50.164 Test 07 (USDJPY,M5)     ChartID<133608959982095519>: [Test 07.mq5 [OnStart] LINE 41 - CopySeries() using detailed rates mask FAILED, aborting... Error: <0> [SUCCESS] sSymbol<USDJPY> timeframe<5> iBarsCNT1<-1>
2024.06.20 16:02:50.164 Test 07 (USDJPY,M5)     ChartID<133608959982095519>: [Test 07.mq5 [OnStart] LINE 49 - CopySeries()  using COPY_RATES_OHLCT FAILED, aborting... Error: <0> [SUCCESS] sSymbol<USDJPY> timeframe<5> iBarsCNT2<-1>

Can't I use CopySeries in a script? I thought that's exactly why the function exists: to give access to rate series data in scripts.


PS. it was ironic when it threw the 4113 error when (as you can see) my error message Print() statements include the chart ID.

Documentation on MQL5: Chart Operations / ChartID
Documentation on MQL5: Chart Operations / ChartID
  • www.mql5.com
Returns the ID of the current chart. Return Value Value of long type.  ...
 

I believe there's something wrong with the WHOLE_ARRAY constant in this case. The function doesn't work with it for me aswell and no error is thrown . It may be a bug or may be intended, because the documentation doesn't specify the usage of this constant for this function (usually this constant is specified as a default argument for the count parameter whenever it is allowed, which is not the case).

A possible workaround is calling the function with the maximum terminal bars:

int max_bars = TerminalInfoInteger(TERMINAL_MAXBARS);
int iBarsCNT1 = CopySeries(sSymbol, PERIOD_M5, 0, max_bars, COPY_RATES_TIME|COPY_RATES_OPEN|COPY_RATES_HIGH|COPY_RATES_LOW|COPY_RATES_CLOSE, dtaTime, daOpen, daHigh, daLow, daClose);
 

You are not allowed to use WHOLE_ARRAY in such place. WHOLE ARRAY is defined as "-1". 

int  CopySeries(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   int              start_pos,         // start position
   int              count,             // amount to copy
   ulong            rates_mask,        // combination of flags to specify the requested series
   void&            array1[],          // array to receive the data of the first copies timeseries
   void&            array2[]           // array to receive the data of the second copied timeseries
   ...
   );

You have to specify a positive value for count input parameter. (Something like 1000)

 
Yashar Seyyedin #:

You are not allowed to use WHOLE_ARRAY in such place. WHOLE ARRAY is defined as "-1". 

You have to specify a positive value for count input parameter. (Something like 1000)

Emanuel and Yashar, incredible. You're right. I never thought of that because I always use WHOLE_ARRAY with the data copy functions. Works everywhere else. Yet another undocumented "feature" I guess.

That did the trick, thanks guys!