-
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:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Why would you call it once in OnInit? The value(s) never updates.
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.
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.
-
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:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
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 ?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Should an indicator be called and/or declared in OnTick() or OnInit() ?
Why ?
For example:
Thanks