Static array resets on timeframe changes

 
Hi all.
I've been using static variables but now i have problem for declaring static arrays. I want elements of array not  to be change when timeframe changes.
static double a[3];
static double b[];
int OnInit()
{
        ArrayResize(b,3);
}

int OnCalculate()
{
	Comment (a[0]);
	Comment (b[0]);
        // some modification of arrays a and b

}

What's wrong about above code? I've trided putting arrays definition in OnCacluate() block but no help. Both comments in above code return 0 when timeframe changes.
 
That's very simple. The indicator or EA gets reloaded if timeframe changes.

There is no way except for file or database storage to overcome this limit.
 
srh124:
Hi all.
I've been using static variables but now i have problem for declaring static arrays. I want elements of array not  to be change when timeframe changes.

What's wrong about above code? I've trided putting arrays definition in OnCacluate() block but no help. Both comments in above code return 0 when timeframe changes.
You'll have to store in another place either in a file, database or globals, when you change the timeframe, the indicator is "reloaded".
Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global Variables of the Terminal - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thanks Dominik and Alexandra.
What's difference between arrays and ordinary types (like int, double) regrading static variables? I mean why static double c = 0; works perfectly and reserve its value between timeframe changes and static arrays cannot?

Also, i have encountered following link which shows static arrays can be used inside classes. Why this is not true for outside of classes?

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

Failure declaring static array
Failure declaring static array
  • 2018.04.23
  • www.mql5.com
Hello, I want to declare static arrays to be used in different classes, so they should be initialized only once...
 
srh124 #:

Thanks Dominik and Alexandra.
What's difference between arrays and ordinary types (like int, double) regrading static variables? I mean why static double c = 0; works perfectly and reserve its value between timeframe changes and static arrays cannot?

Also, i have encountered following link which shows static arrays can be used inside classes. Why this is not true for outside of classes?

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

Alexandre* lol

If you do "static double c = 0", it is not preserving the value between timeframe changes(at least it shouldn't, maybe a bug in MT5), what it does is initialize the variable as soon as the timeframe changes.
If all you want is to initialize, do it inside the OnInit function as it'll be called everytime the timeframe changes.

Also, static arrays would not make sense in the global scope since the indicator is reloaded on timeframe changes. May aswell use a normal array.
 
Alexandre Borela #: If you do "static double c = 0", it is not preserving the value between timeframe changes(at least it shouldn't, maybe a bug in MT5),

MT4: Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
          external static variable - MQL4 programming forum #2 (2013)

Do not assume. If you want them preserved across deinit/init cycles, use persistent storage (files or GV+Flush.)
 

Thanks Alexandre (sorry for bad calling. I'm not english man) and William.

It was my misunderstanding of static variables in indicators. I think the reason for my misunderstandig is that i already used to split my code into another class and call that class in OnClaculate method. It sounds declaring class variable as static preserves variables defined inside that class, as stated in this link:
https://www.mql5.com/en/forum/140302#comment_3553026

Second approach could be storing that variables in hidden label objects. And the third way is using  Global variables of the Client Terminal.
What's the preferred way?

I think the third approach may have some impacts on Metatrader speed, if number of Global variables grows larger and larger.

Static variable behaviour
Static variable behaviour
  • 2012.07.08
  • www.mql5.com
Hi, I find that for indicators, the static variable content never hold whenever change of timeframe, this never happen to EA...