OnInit() should be used to retrieve the indicator handles only. Their values should be queried during OnTick() calls.
If you need indicator values during OnInit() you should think about improving your EA design. It may work for some indicators but it's totally unreliable.
I know my work around is to put it in OnTick and execute once and turn off the flag so that it wont be executed again.
However, that appears to be a workaround, and does not feel like a good place to put a "initialization" part of the program.
Just wanted to know if there are some other stuff that i have not thought about.
I know my work around is to put it in OnTick and execute once and turn off the flag so that it wont be executed again.
However, that appears to be a workaround, and does not feel like a good place to put a "initialization" part of the program.
Just wanted to know if there are some other stuff that i have not thought about.
The indicator handle is created in OnInit - this is the law. You cannot try to get indicator data in OnInit - this is the law. Preparatory procedures are performed in OnInit, and the OnTick event ensures that the full operation of the program has begun.
Don't try to use any price or server related functions in OnInit (or on load,) 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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have problems getting indicator values within the OnInit() function. Same methods have no issue in OnTick() function.
Below is a demo of the issue.
The following is the code used.
The result is
Note that OnInit, Close returns value, but not ATR.
OnTick, using same methods, both returns value.
In Oninit, close.GetData(x) where x = 0 to 10 has no issues.
Trying to do same with atr.Main(x) or even atr.GetData(0,x)
Can anyone enlighten? How do i get values from indicators in OnInit??
I am using ATR as a demo. The same happens for Custom indicators.