Actually I added
periods = 20000;
at OnDeinit and it now works correctly. Don't know why it holds the value as a static or global var. Is there any workaround around this by having the EA count correctly on init?
Unlike indicators, EAs are loaded once. Static and global variables are set once. When you change TF, you go through a deinit/init cycle. Global/static variables are not reloaded.
Reinitialize your variable in OnInit.
Unlike indicators, EAs are loaded once. Static and global variables are set once. When you change TF, you go through a deinit/init cycle. Global/static variables are not reloaded.
Reinitialize your variable in OnInit.
Didn't know about that. Thanks :)
Global Variables - Variables - Language Basics - MQL4 Reference
Perhaps you should read the manual.
Actually I did. I just don't see anywhere in your link state that EA global vars are loaded once and apply even if you switch TFs or symbol, but indicators do not. Because it works as intended in my indi utilizing the same code when I switch TFs. Perhaps the manual should be more clear and state that EA actually holds global variables as static (c way) and indicators fail to do that.
Put on your reading glasses and read it again.
Global Variables - Variables - Language Basics - MQL4 Reference
Put on your reading glasses and read it again.
Even with glasses on I still don't see it saying there or anywhere else that it doesn't apply to indicators but only EAs (I am guessing an indicator is not a program in this case), so if you only made an indicator in the past you should expect different results with an EA. Anyway, point taken, everything is left to randomness so whroeder can come up with derogatory comments.
It use to be the same for indicators as EAs, that changed relatively recently but not the documentation.
Since indicators run through all bars and display results, they shouldn't need static storage and shouldn't be affected by reloads.
As for "derogatory comments," You started it "don't see anywhere in your link state that EA global vars are loaded once" whereas the link says "Global variables are initialized only once." So your statement simply meant you didn't bother to read the link provided. Now you change the subject adding indicators and attack at the same time - typical liberal behavior.
Apparently you don't want any more of my help, I will add you to my troll list and not respond in the future. Have a nice life.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am having an issue in an EA I am trying to write and which has to do with Bars or iBars counting. When I compile or attach the EA on the desired TF, the Bars are counted correctly:
When I switch from daily to monthly TF, it reads the Bars correctly and goes on:
But then, when I switch back to daily from monthly, the number of Bars stays the same (187) and does not re-count the number of bars in the new TF/chart, which messes up the calculation. This happens to whatever TF I move on from monthly to:
So it only recounts Bars where there are fewer on the chart and it doesn't count them again when there are more in a new TF.
The code is pretty simple. I have a global int variable which sets maximum bar count:
Then the function makes the following check, in order to avoid having the array fall out of bounds:
Tried with both methods just in case something was wrong. OnTick & OnInit are pretty much empty at this time, so nothing interferes with this code. I just used this one in case it could help but it can't:
Would appreciate if someone can help me overcome this issue.