Slawa: static value resetting when changing chart periods

 
Hello again,

I have a static array of type int. In the fifth element I'm actually storing true or false value;

If my indicator changes the fifth element to true, then I change the period of the chart, after the chart period is changed code is running that should not:


static int monitorStat[5];

init()
    ......
// end: init()


start()
    {
    if(monitorStat[4]==false)
        {
        // code here only executes one time, but it is is executing again after chart period is changed
        Alert("Alert!!!");
        monitorStat[4]==true;
        }
    }
// end: start()



I thought static variables would be persistent. Is there something weird about storing false in an int array and if I stored 0 or 1 instead of false or true, does that help? I am not reinitializing the array in the init() or anywhere else.

Thanks for your help or explanation.

 
"Static variables are stored in the permanent memory, their values do not get lost when the function is exited. Any variables in a block, except for formal parameters of the function, can be defined as static. The static variable can be initialized by a constant of the corresponded type, unlike a simple local variable which can be initialized by any expression. If there is no explicit initialization, the static variable is initialized with zero. Static variables are initialized only once before calling of the "init()" function, that is at exit from the function inside which the static variable is declared, the value of this variable not getting lost."

Hmmm...

I find that a regular static int or static int array does not hold its value on change of timeframe or change of chart.

I think a close reading of the above means something like this:

int start(){
   for( int i = 0; i < 10; i++){
      static int countStatic= 0;        // initialized only one time
      int countNonStatic= 0;            // initialized every loop 
      countStatic++;
      countNonStatic++;
   }
   Comment("Count Static = ", countStatic, "  CountNonStatic = ", countNonStatic);
   return(0);
}


When a tick comes (or refesh) countStatic keeps counting up, 10, 20, 30, 40, etc.
Without the static designation countNonStatic always prints as 1.

The static variable is only initialized once ( until the whole thing gets initialized again, like at timefame change)

If you must save valuesbetween Indicator Initializations, put them out to a global variable or a file.


Reason: