Hi,
I'ld like to know: is it useful to call "ArrayInitialize" to an INDICATOR_DATA buffer buffer of an indicator?
In other words: when we create a custom indicator, normally we create at least one data buffer that will be the signs shown in the graph. After calling "SetIndexBuffer" and "PlotIndexSetDouble(...,...,empyt_value)", the buffer array is ready ro receive some data to be ploted from inside OnCalculate. Until that comes to happen, I don't know the state of that buffer array: its size or which kind of data is inside. So that opens the question: is it useful to call "ArrayInitialize" in OnInit? I'm guessing that there maybe value to that in case OnCalculate returns some error that doesn't allow the full filling of the buffer array, but I'm not sure. So I'ld like to know :)
Hmm I guess this topic more or less gives the answer (curiously it didn't appear when I did the research). The difference here is that it's suggested the ArrayInitialize should be inside OnCalculate when doing the first calculation (prev_calculated == 0), not from within OnInit. Is there any difference between these two positions?
![Should I ALWAYS initialize indicator buffers? Should I ALWAYS initialize indicator buffers?](https://c.mql5.com/36/49/should-i-always-initialize-indicator.jpg)
- 2020.04.21
- www.mql5.com
- During the OnInit() the buffers are not yet in use and since they are dynamic arrays the size will probably be 0, so ArrayInitialize will do nothing!
- During OnCalculate() with rates_total = 0, the arrays will probably still have a size of 0 and ArrayInitialize will still do nothing
- During OnCalculate() with rates_total > 0 and prev_calculated == 0, your code should fill ALL the elements of the array explicitly anyway, even if it is just to fill the element with an empty value, so ArrayInitialize will be a waste of time anyway. If your code logic is done properly where data is always explicitly assigned, you will never need to use it.
Hmm I guess this topic more or less gives the answer (curiously it didn't appear when I did the research). The difference here is that it's suggested the ArrayInitialize should be inside OnCalculate when doing the first calculation (prev_calculated == 0), not from within OnInit. Is there any difference between these two positions?
As said by Fernando the only rule is all values of your buffers must be set, otherwise you will always have serious issues sooner or later. How you set the values doesn't matter. ArrayInitialize() is just one way to do it.
It must be done in OnCalculate(), because an indicator can be reset (call by prev_calculated=0) by the Terminal without calling OnInit().
![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'ld like to know: is it useful to call "ArrayInitialize" to an INDICATOR_DATA buffer buffer of an indicator?
In other words: when we create a custom indicator, normally we create at least one data buffer that will be the signs shown in the graph. After calling "SetIndexBuffer" and "PlotIndexSetDouble(...,...,empyt_value)", the buffer array is ready ro receive some data to be ploted from inside OnCalculate. Until that comes to happen, I don't know the state of that buffer array: its size or which kind of data is inside. So that opens the question: is it useful to call "ArrayInitialize" in OnInit? I'm guessing that there maybe value to that in case OnCalculate returns some error that doesn't allow the full filling of the buffer array, but I'm not sure. So I'ld like to know :)