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.
![Documentation on MQL5: Global Variables of the Terminal Documentation on MQL5: Global Variables of the Terminal](https://c.mql5.com/36/52/documentation-on-mql5-global-variables.png)
- www.mql5.com
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?
![Failure declaring static array Failure declaring static array](https://c.mql5.com/36/52/failure-declaring-static-array.jpg)
- 2018.04.23
- www.mql5.com
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?
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.
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)
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](https://c.mql5.com/36/52/static-variable-behaviour.jpg)
- 2012.07.08
- www.mql5.com
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- 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'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.