Download MetaTrader 5

mqlrates out of range error

To add comments, please log in or register
maz
15
maz  

Hello


I am trying to understand why in example below my EA stops and I receive array out of range error (pointing that problem is in following line:

 Print( "15 mins Rates time: ", mqlrates_array_15[0].time);



My entire idea is to be able to refer to MqlRates of different timeframes of the same symbol.

The EA runs for some time and then crashes. 

Can anyone help please?




#property copyright "Copyright © 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property description "Expert Advisor displaying two cases of"
#property description "ArrayCopyRates() function call"
//--- destination array for physical copying of historical data
double   double_array[][6];
//--- destination array for logical copying of historical data
MqlRates mqlrates_array[];
MqlRates mqlrates_array_15[];
//--- first call flag
bool first_call;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- run this EA at chart with M1 timeframe
   //if(Period()!=PeriodSeconds(PERIOD_M1)/60)
    // {
    // Alert("The Expert Advisor must be attached to M1 chart!");
   //  return(INIT_FAILED);
   //  }
//--- first call
   first_call=true;
//--- all ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- delete all comments
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(first_call)
     {
      //--- copying physically  double_array
      ArrayCopyRates(double_array,NULL,0);
      //--- virtual copying -> mqlrates_array will contain the reference on the data
      ArrayCopyRates(mqlrates_array,NULL,0);
      ArrayCopyRates(mqlrates_array_15,NULL,PERIOD_M15);
       
      //--- cancel first call flag
      first_call=false;
     }
   //--- at each tick print the values of the 0-th array element to see the difference
   Comment("The values of double_array[] are not changed (because of real data copying):\n",
           "0 - time: ",(datetime)double_array[0][0],"\n",
           "1 - open: ",double_array[0][1],"\n"
           "2 - low: ",double_array[0][2],"\n"
           "3 - high: ",double_array[0][3],"\n"
           "4 - close: ",double_array[0][4],"\n"
           "5 - volume: ",DoubleToString(double_array[0][5],0),"\n\n",
           "The values of mqlrates_array[] are changed (because of virtual data copying):\n",
           "0 - time: ",mqlrates_array[0].time,"\n",
           "1 - open: ",mqlrates_array[0].open,"\n"
           "2 - low: ",mqlrates_array[0].low,"\n"
           "3 - high: ",mqlrates_array[0].high,"\n"
           "4 - close: ",mqlrates_array[0].close,"\n"
           "5 - volume: ",mqlrates_array[0].tick_volume);
  //Print("Last error: ",GetLastError());  
  
  if(GetLastError()==4066)
  {
   //---- make two more attempts to read
   Alert("last error");
   for(int i=0;i<2; i++)
     {
      Sleep(5000);
      ArrayCopyRates(mqlrates_array_15,NULL,PERIOD_M15);
      //---- check the current daily bar time
      datetime last_bar=mqlrates_array_15[0].time;
      if(Year()==TimeYear(last_bar) && Month()==TimeMonth(last_bar) && Day()==TimeDay(last_bar)) break;
     }
  }
   else      
   { Print( "Rates time: ", mqlrates_array[0].time);
     Print( "15 mins Rates time: ", mqlrates_array_15[0].time); }  
  
  //SeriesInfoInteger(  
  }
whroeder1
15913
whroeder1  
      ArrayCopyRates(mqlrates_array_15,NULL,PERIOD_M15);
Check your return codes and you would know why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 4066, sleep, refreshRates and retry
maz
15
maz  
whroeder1:
Check your return codes and you would know why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 4066, sleep, refreshRates and retry

Thanks a lot for you replay . I have checked return values of ArrayCopyRates but the result confuses me.

If I use ArrayCopyRates for H1 bars I receive return value of 2098 elements copied. But if I am running EA with tick counter - I will go out of range after 187 ticks?


Also - so to avoid going out of range do I need to keep resizing my mqlrates arrays?


I have attached code with my recent changes

Files:
whroeder1
15913
whroeder1  
    Print(counter, ":60 mins Rates time: ", mqlrates_array_60[counter].time);
  1. What does ticks have to do with bars. After one tick you look back one hour. After 24 ticks you look back 24 hours. Bogus.
  2. You do not resize your arrays, after calling ACR successfully, they will auto-update.
maz
15
maz  
whroeder1:
  1. What does ticks have to do with bars. After one tick you look back one hour. After 24 ticks you look back 24 hours. Bogus.
  2. You do not resize your arrays, after calling ACR successfully, they will auto-update.

I used tick to check how far I can go and I was expecting to reach 2098 elements of H1 mqlrates (so 2098 hours) - but it went out after 187 elements.

Anyway - so problem is with wrong way of calling ACR function?,


does it mean I am copying array instead of passing it by reference?

 I used code from https://docs.mql4.com/array/arraycopyrates - and I though it is way to use it.

ArrayCopyRates - Array Functions - MQL4 Reference
ArrayCopyRates - Array Functions - MQL4 Reference
  • docs.mql4.com
ArrayCopyRates - Array Functions - MQL4 Reference
whroeder1
15913
whroeder1  
maz: I used tick to check how far I can go and I was expecting to reach 2098 elements of H1 mqlrates (so 2098 hours) - but it went out after 187 elements.
So you don't have H1 history. So what? You can only see ArraySize amount.
To add comments, please log in or register