What specific reason is there to call an indicator in the OnTick() vs OnInit() ?

 

Should an indicator be called and/or declared in OnTick() or OnInit() ?
Why ? 

For example:

   val1=iFractals(NULL, 0, MODE_UPPER,3);
   val2=iFractals(NULL, 0, MODE_LOWER,3);



Thanks 

How to call indicators in MQL5
How to call indicators in MQL5
  • www.mql5.com
With new version of MQL programming language available not only the approach of dealing with indicators have changed, but there are also new ways of how to create indicators. Furthermore, you have additional flexibility working with indicator's buffers - now you can specify the desired direction of indexing and get exactly as many indicator's values as you want. This article explains the basic methods of calling indicators and retrieving data from the indicator's buffers.
 
  1. Only in OnTick, after the indicator has updated.

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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, account information and prices are valid.

  2. Why would you call it once in OnInit? The value(s) never updates.

 
Agent86: Should an indicator be called and/or declared in OnTick() or OnInit() ? Why ?

Please be aware that the method used in MQL4 is very different to MQL5.

In MQL5, you obtain the Indicator's handle in OnInit() and use that handle in OnTick().

However, that is not the case in MQL4, where you only use it in OnTick() directly as there are no indicator handles.

 
Thanks all. 

I suspected exactly what you all have posted, but sometimes it's hard to confirm things by searching and reading others codes.
I see codebase and other codes at times with declarations like this in OnInit which I never understood. 

So glad for forum posts and active forums. 
 
William Roeder #:
  1. Only in OnTick, after the indicator has updated.

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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, account information and prices are valid.

  2. Why would you call it once in OnInit? The value(s) never updates.

Thanks, 

What about in a function then call the function in OnTick ?


 
Agent86 #: What about in a function then call the function in OnTick ?

After two years, you should already know the answer.