Error out of range in Copyrates() with Build 1932

 

Hi all,

I get an error compiling with Build 1932 version in CopyRates() function.

I have done a script to see the error:

//+------------------------------------------------------------------+
//|                                          Example_CopyRates.mq5 |
//|                                                                                |
//+------------------------------------------------------------------+
#property script_show_inputs

input int TAM_BUFFER=350000;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//to see CopyRates() error

MqlRates mrate[];
ZeroMemory(mrate);
ArraySetAsSeries(mrate,true);
if(CopyRates(_Symbol,_Period,0,TAM_BUFFER,mrate)<0)

     {
      PrintFormat("Error recovering history data - error: %d",GetLastError());
      ResetLastError();
     return;
     }
  //calculating allowed max size of the array mrate[]
  int tam_mrate=ArrayRange(mrate,0);

//In case of error both values are different


  if (tam_mrate!=TAM_BUFFER)
      {
      PrintFormat("CopyRate function Error. Maximum size allowed= %d",tam_mrate);
      }
  
  }
//+------------------------------------------------------------------+

By executing the script, it returns an allowed maximum size of mrate[] of 10,200. Depending on the TAM_BUFFER parameter, it oscillates more or less between 10,205 and 10,210.

Is this an error in the new build version?

Thanks.

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.


 

Sorry, I wanted to say:

It oscillates more or less between 100,205 and 100,210, very far from 300,000 (TAM_BUFFER).

Thanks again.

 
Sergey Golubev:

OK, thanks.
 

This is the code:


//+------------------------------------------------------------------+
//|                                          Example_CopyRates.mq5   |
//|                                                                  |
//+------------------------------------------------------------------+
#property script_show_inputs

input int TAM_BUFFER=350000;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//to see CopyRates() error

MqlRates mrate[];
ZeroMemory(mrate);
ArraySetAsSeries(mrate,true);
if(CopyRates(_Symbol,_Period,0,TAM_BUFFER,mrate)<0)

     {
      PrintFormat("Error recovering history data - error: %d",GetLastError());
      ResetLastError();
     return;
     }
  //calculating allowed max size of the array mrate[]
  int tam_mrate=ArrayRange(mrate,0);

//In case of error both values are different


  if (tam_mrate!=TAM_BUFFER)
      {
      PrintFormat("CopyRate function Error. Maximum size allowed= %d",tam_mrate);
      }
  
  }
//+------------------------------------------------------------------+
 
txusbeck:

This is the code:


First, CopyRates will resize and fill the dynamic array you pass to it. So even though you ask for n number of rates you won't always get that many in return. Next, you are using the wrong function for ArraySize. 

#property script_show_inputs

input int TAM_BUFFER=350000;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   MqlRates mrate[];
   ZeroMemory(mrate);
   ArraySetAsSeries(mrate,true);
   int num_copied_rates = CopyRates(_Symbol, _Period, 0, TAM_BUFFER, mrate);
   if(num_copied_rates < 0){
      printf("Error recovering history data - error: %d", GetLastError());
      return;
   }
   int rates_array_size = ArraySize(mrate);
   Print("test asserts: rates_array_size == num_copied_rates <= TAM_BUFFER: ",
      string(rates_array_size == num_copied_rates && num_copied_rates <= TAM_BUFFER)
   );
}
 
nicholi shen:

First, CopyRates will resize and fill the dynamic array you pass to it. So even though you ask for n number of rates you won't always get that many in return. Next, you are using the wrong function for ArraySize. 

Dear Nicholi,

Thank you for your help. Effectively it is a good method to control the array size by using ArraySize() function and the return of CopyRates().

Anyway, the last version (build 1940) has fixed the error consisting of the incapacity of CopyRates() of reserving all the requested memory. This is, rates_array_size and num_copied_rates are equal in size in this new version.

Thanks again,

Miguel.

 
How to fix indicator errors?