In OnInit(), I need to learn which side (low or high) of a past bar generated firstly?
If I'm correct, there are no tick data for the past bars, so how can I do that?
Thanks.
Avoid all calculations in OnInit. Data may not be loaded yet.
On higher time-frames, you may be able to determine by using minute timeframe, but is not infallible.
Avoid all calculations in OnInit. Data may not be loaded yet.
On higher time-frames, you may be able to determine by using minute timeframe, but is not infallible.
- 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.
- 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.
So, new history is received after OnInit is called, and as Keith said, "Avoid all calculations in OnInit. Data may not be loaded yet".
My EA was hard coded for EURUSD, for a specified timeframe, and I always use it on the same chart.
I close the terminal at 20:00 at nights and reopen it at 8:00, so it only needs 12 hours of history in every trading morning.
For this specific case, should I move the calculations into OnTick, currently taken place in OnInit?
I'm looping with iLow, iHigh, etc functions in OnInit, and never have a problem so far.
Maybe these functions get a bit history needed on the flow, I don't know.
I haven't embedded the functionality of sending orders, but, if I decide to do it later, this point may be important to think about.
Thanks a lot to all.
I'm looping with iLow, iHigh, etc functions in OnInit, and never have a problem so far.
You may not see any problems now, but there are sure to be times that you will.
I do all my early calculations like this
bool FirstTick; //global scope variable //+------------------------------------------------------------------+ int OnInit() { FirstTick=true; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnTick() { if(FirstTick) { //Check that data is updated //Do your calcuations here FirstTick=false; } }
You may not see any problems now, but there are sure to be times that you will.
I do all my early calculations like this
Thank you, then let me do it now.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
In OnInit(), I need to learn which side (low or high) of a past bar generated firstly?
If I'm correct, there are no tick data for the past bars, so how can I do that?
Thanks.