Should I ALWAYS initialize indicator buffers?

 

Hello,

I saw it mentioned on the forum that we should initialize all buffers. However, when I look at examples (like CCI or Envelopes) I don't see any initialization happening.

Being precise, should I have the following snippet in my every indicator, in the onCalculate?

if(prev_calculated == 0)
     {
      ArrayInitialize(indicator_data, EMPTY_VALUE);

     }

indicator_data is an indicator buffer (SetIndexBuffer(0,indicator_data,INDICATOR_DATA)).

And why would we do it if later we set a value for each index in the buffer?


Cheers!

 
dmdamiyo:

Hello,

I saw it mentioned on the forum that we should initialize all buffers. However, when I look at examples (like CCI or Envelopes) I don't see any initialization happening.

Being precise, should I have the following snippet in my every indicator, in the onCalculate?

if(prev_calculated == 0)
     {
      ArrayInitialize(indicator_data, EMPTY_VALUE);

     }

indicator_data is an indicator buffer (SetIndexBuffer(0,indicator_data,INDICATOR_DATA)).

And why would we do it if later we set a value for each index in the buffer?


Cheers!

Don't do that - do you buffers cleanup on loops for each and every element. Otherwise you shall have repainting indicators

 
Mladen Rakic:

Don't do that - do you buffers cleanup on loops for each and every element. Otherwise you shall have repainting indicators

Huh ? Why is that ?

Of course it's not needed to always use ArrayInitialize() but using the above code would not lead to repainting indicators at all.

 
dmdamiyo:

Hello,

I saw it mentioned on the forum that we should initialize all buffers. However, when I look at examples (like CCI or Envelopes) I don't see any initialization happening.

Being precise, should I have the following snippet in my every indicator, in the onCalculate?

if(prev_calculated == 0)
     {
      ArrayInitialize(indicator_data, EMPTY_VALUE);

     }

indicator_data is an indicator buffer (SetIndexBuffer(0,indicator_data,INDICATOR_DATA)).

And why would we do it if later we set a value for each index in the buffer?


Cheers!

Why are you opening a new topic ?

You ALWAYS need to understand what you are doing. A buffer is an array automatically managed by MT5, but with uninitialized values. So you NEED to see a value for each index of the array, it can be done in any way provided that all values are set explicitly. Otherwise you will have garbage graphics like explained on the other topic.

 
Alain Verleyen:

Huh ? Why is that ?

Of course it's not needed to always use ArrayInitialize() but using the above code would not lead to repainting indicators at all.

That is exactly my point : people think that initializing a buffer solves all the stuff that they usually have to do in their code (mainly in loops). I have seen that 100s of times, and then in the code where the job is actually done I have seen a complete disregard to the "clean up" part which lead to heavy repainting (my guess is that they thought : "I have initialized it, no need to do anything else" kind of stuff)

Even if he initializes the buffer, he shall have to write the code correctly to "clean up" the buffers on each and every new calculation loop - hence that initialization is of no actual at all if the rest of the code is written as it should be (initialized buffers or no, does not matter). 

 
Mladen Rakic:

That is exactly my point : people think that initializing a buffer solves all the stuff that they usually have to do in their code (mainly in loops). I have seen that 100s of times, and then in the code where the job is actually done I have seen a complete disregard to the "clean up" part which lead to heavy repainting (my guess is that they thought : "I have initialized it, no need to do anything else" kind of stuff)

Even if he initializes the buffer, he shall have to write the code correctly to "clean up" the buffers on each and every new calculation loop - hence that initialization is of no actual at all if the rest of the code is written as it should be (initialized buffers or no, does not matter). 

Ah ok, I had misunderstood your point. You are right of course.