Cannot count bars correct when switching from TF with few bars to one with plenty

 
https://www.mql5.com/en/forum/130523

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:

int periods = 20000;

Then the function makes the following check, in order to avoid having the array fall out of bounds:

void ArrayFillNeg() {
        if (iBars(_Symbol, PERIOD_CURRENT) < periods) periods = iBars(_Symbol, PERIOD_CURRENT) - 1;
        Print("neg" + periods);

and 

void ArrayFillPos() {
        if (Bars < periods) periods = Bars - 1;
        Print("pos" + periods);

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.
How to avoid wrong initialization of indicator with iBarShift
How to avoid wrong initialization of indicator with iBarShift
  • 2010.12.09
  • www.mql5.com
Hi all, this time i come here with a question .. i dont see some mistakes...
 

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?

 
Thanasis Filippidis: 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.

 
whroeder1:

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 :)

 
Perhaps you should read the manual.
Global variables are initialized only once after the program is loaded into the client terminal memory and before the first handling of the Init event.
          Global Variables - Variables - Language Basics - MQL4 Reference
 
whroeder1:
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.


 
Thanasis Filippidis: Actually I did. I just don't see anywhere in your link state that EA global vars are loaded once

Put on your reading glasses and read it again.

Global variables are initialized only once after the program is loaded into the client terminal memory and before the first handling of the Init event. For global variables representing class objects, during their initialization the corresponding constructors are called.
          Global Variables - Variables - Language Basics - MQL4 Reference
 
whroeder1:

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.

 
Thanasis Filippidis: 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

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.

Reason: