Year() returning bad value inside OnInit() - Indicator

To add comments, please log in or register
Marco Strazzeri
92
Marco Strazzeri  

Here is the code i'm using


int OnInit()
{
   Print("init Year: "+Year());
   
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   Print("Year: "+Year());
//--- return value of prev_calculated for next call
   return(rates_total);
  }

While the Print inside OnCalculate works properly, it's not the case for the Print inside OnInit, which seem to always print 1900.

What is the reason for that?

William Roeder
20207
William Roeder  
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.
To add comments, please log in or register