Download MetaTrader 5

Array out of range

To add comments, please log in or register
Michael
718
Michael  

Most of the time, I get this on array's I create.  And it's usually due to a size limitation I placed on myself.

But this one has really got me stumped.

How is it that I manage to get the Array out of range error on this?

datetime LastTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   LastTime=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
10614
Keith Watford  

You may get this when restarting the platform.

It is because the Time array has not been loaded 

honest_knave
Moderator
2318
honest_knave  

I suggest you read this page to help clarify some things.

It is not recommended to call start() from init() or to perform trade operations from init(), because during initialization values of information environment parameters may be not ready (information about charts, market prices and so on).

Michael
718
Michael  
honest_knave:

I suggest you read this page to help clarify some things.

It is not recommended to call start() from init() or to perform trade operations from init(), because during initialization values of information environment parameters may be not ready (information about charts, market prices and so on).

Great.  But it's needed for starting off in determining when a new candle is formed.

datetime LastTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   LastTime=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   bool IsNewBar=funcIsNewBar();
   if(IsNewBar)
     {
       //Do stuff.
     } 

  }
//+------------------------------------------------------------------+
bool funcIsNewBar()
{
   bool ReturnValue=false;
   if(LastTime != Time[0]) 
   {
      ReturnValue=true;
      LastTime=Time[0];   
   }
   return(ReturnValue);
}
Keith Watford
Moderator
10614
Keith Watford  

Consider something like

datetime LastTime;
bool FirstTick;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  FirstTick=true; 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(FirstTick)
      {
      LastTime=Time[0];
      FirstTick=false;
      }
   
   bool IsNewBar=funcIsNewBar();
   if(IsNewBar)
     {
       //Do stuff.
     } 

  }
//+------------------------------------------------------------------+
Michael
718
Michael  
GumRai:

Consider something like

 

Great minds think alike!

datetime LastTime;
bool IsInit;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   IsInit=true;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   bool IsNewBar=funcIsNewBar();
   if(IsInit)
     {
      LastTime=Time[0];
      IsInit=false;
     }
   if(IsNewBar)
     {
       //Do stuff.
     } 

  }
//+------------------------------------------------------------------+
bool funcIsNewBar()
{
   bool ReturnValue=false;
   if(LastTime != Time[0]) 
   {
      ReturnValue=true;
      LastTime=Time[0];   
   }
   return(ReturnValue);
}
Now I just need to start the trade week off and test it. 
Alain Verleyen
Moderator
33503
Alain Verleyen  
nondisclosure:

Great minds think alike!

Now I just need to start the trade week off and test it. 


Unneeded complication :

bool funcIsNewBar()
{
   static datetime LastTime=Time[0];
   bool ReturnValue=false;
   if(LastTime != Time[0]) 
   {
      ReturnValue=true;
      LastTime=Time[0];   
   }
   return(ReturnValue);
}
Keith Watford
Moderator
10614
Keith Watford  
angevoyageur:

Unneeded complication :

Certainly, a static variable does the trick, but there may be occasions that you want a variable to be reset on initialisation.

I know that it may not often be the case that the user switches timeframes with an EA attached, but this could result in the EA calculating that a new bar has just opened. 

Siew Fei Wong
190
Siew Fei Wong  

Hi, May i ask is the error still exist? I am facing this kind of problem also.

May i ask how to solve it accordingly?

ydrol
590
ydrol  
angevoyageur:

Unneeded complication :

 Interesting. So to be clear local static variables can be initialized with a non-constant expression, contrary to documentation ?

 A static variable can be initialized by a constant or constant expression corresponding to its type, unlike a simple local variable, which can be initialized by any expression.

 And also they are initialised when the function is first called and not during compilation?

Alain Verleyen
Moderator
33503
Alain Verleyen  
ydrol:

 Interesting. So to be clear local static variables can be initialized with a non-constant expression, contrary to documentation ?

Not really. Only with predefined variable which are considered as constant by the compiler. (They are not modifiable by your code, and are managed by MT4).


 And also they are initialised when the function is first called and not during compilation?

Exactly. Just test it, it works fine.
12
To add comments, please log in or register