Download MetaTrader 5
To add comments, please log in or register
All activity at MQL5.community is clearly visible. Have a look at the Wall!
Michael
500
Michael 2015.01.09 21:26 

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
8791
Keith Watford 2015.01.09 21:50  

You may get this when restarting the platform.

It is because the Time array has not been loaded 

honest_knave
1292
honest_knave 2015.01.09 21:52  

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
500
Michael 2015.01.09 22:58  
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
8791
Keith Watford 2015.01.09 23:18  

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
500
Michael 2015.01.10 00:00  
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
29476
Alain Verleyen 2015.01.10 00:22  
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
8791
Keith Watford 2015.01.10 01:19  
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
188
Siew Fei Wong 2015.06.19 02:50  

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
593
ydrol 2015.06.19 08:47  
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
29476
Alain Verleyen 2015.06.19 10:17  
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