Property indicator_buffers and IndicatorBuffers() Whats the difference?

 

Hi guys,

See this code snippet :-

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Black
#property  indicator_color2  Green
#property  indicator_color3  Red
//---- indicator buffers
double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     ExtBuffer3[];
double     ExtBuffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
Please can some-one explain what the difference is between the #property indicator_buffers 3 and the IndicatorBuffers(5).

A search reveals that this is used when you may wish to use the extra 2 buffers for calculations.....fair enough, so why not just declare the 5 buffers in the first place in the #property section?

Many thanks for any comments and inputs........

Kenny
 

Have you figured it out?

I just read this post,  

#property indicator_buffers 3  --> sets the number of the visual buffers; try increasing this number on any Indicator code, and launch. To see the difference peek into the Color tab (before & after).

 IndicatorBuffers(5);  --> sets the pre-buffers  quantity used in calculating the visual one ( plus the visual one ). The buffers allocated with this ( via SetIndexBuffer) is automatically sized & initialized. You can opt not to use this but in that case you'd need to manage your own calculating arrays. 

HMA in some version for example needs two pre-calculating buffers to produce one visual line. So "  #property indicator_buffers 1,  IndicatorBuffers(3)  ". 

 

cameofx,

Thank you for the reply.......I hadn't figured it out yet. Unfortunately it is still a bit vague for me, but I am slowly building up the pieces of the puzzle.

So, I now understand that you cannot use IndicatorBuffers() for the visual buffers. I also understand that IndicatorBuffers() number = sum of visual buffers(#property) plus the pre-buffers.

Am I correct in assuming that any buffer initialised by IndicatorBuffers() cannot be displayed visually(unless declared in the #property)? I also think I now understand that the buffers initialised like this will "mirror" any arrays assigned to it by the SetIndexBuffer command.

 
  1. #property indicator_buffers  : a controlling compilation
  2. void  IndicatorBuffers : a Custom Indicator function

This is best explained  with an example :  

Suppose I want to make an Indy that calculates an EMA on top of MACD on top of SMA. And display only the resulting EMA. 

I would then have 3 buffers : 2 pre-calculating ones and 1 last-calculating-visual one. So :

# property indicator_buffers 1

int init()

{ 
  IndicatorBuffers(3); 
  SetIndexBuffer(0, EMA_result);
  SetIndexBuffer(1,MACD_on_SMA);
  SetIndexBuffer(2,SMA );
} 

I can opt not to use the automatically allocated IndicatorBuffers to calculate the EMA_result; then it would be :

# property indicator_buffers 1

int init()
{ 
  IndicatorBuffers(1); 
  SetIndexBuffer(0, EMA_result);

  ArrayResize(SMA,Bars); ArrayInitialize(SMA,0.0);
  ArrayResize(MACD_on_SMA,Bars); ArrayInitialize(MACD_on_SMA,0.0);
} 

but if I wanted to display the pre-calculating buffers too, then :

#property indicator_buffers 3

int init()
{ 
  IndicatorBuffers(3); 
  SetIndexBuffer(0, EMA_result);
  SetIndexBuffer(1,MACD_on_SMA);
  SetIndexBuffer(2,SMA );
} 

// or --- last incorrect example deleted

 the visual ones are always also the calculating ones. The visual ones must always be allocated by IndicatorBuffers. So, you cannot have 

#property indicator_buffers 1  while IndicatorBuffers (0);  // this would mean the visual-calculating buffer does not get allocated memory

or  #property indicator_buffers 2  while IndicatorBuffers (1);  // the 2nd visual-calculating buffer does not get allocated

or  #property indicator_buffers n  while IndicatorBuffers (n-x);   // hence IndicatorBuffers cannot be less than #property indicator_buffers

 

but you can have it the other way around. i.e. allocating more buffers with IndicatorBuffers but displaying less with #property indicator_buffers.

PS:  I found  explaining it is more difficult than applying it :)))

 hth. 

 

 

Ok, I think I got it now. Thanks for taking the time to explain it!

I guess the benefit of the calulation buffer is it would expand the array with bars being added to the chart, whereas if you elect to use a normal array, you need to resize and I imagine need to shift the array elements 1 bar to the left each time, using ArrayCopy.

Some indicator don't have the IndicatorBuffers() command, so would it be correct to assume the MQL4 transparently assigns the IndicatorBuffers value to equal the #property indicator_buffers if it is absent?

Sorry, for all the questions but I guess this is how I learn........Please can you confirm my understanding of the controlling compilation :-

From your reference above.....

Every MQL4 program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs without the necessity to launch them explicitly. This concerns external settings of indicators, first of all

If my understanding is correct, you could launch your indicator using IndicatorBuffers() only and use the SetIndexStyle to adjust its appearance and properties. Using the controlling compilation simply allows the user to adjust the properties externally? Is this correct?

Regards

Kenny

 

 Some indicator don't have the IndicatorBuffers() command, so would it be correct to assume the MQL4 transparently assigns the IndicatorBuffers value to equal the #property indicator_buffers if it is absent?

 void IndicatorBuffers( int count)

Allocates memory for buffers used for custom indicator calculations. The amount of buffers cannot exceed 8 or be less than the value given in the indicator_buffers property.
If custom indicator requires additional buffers for counting, this function must be used for specifying of the total amount of buffers. 

AFAIK :

- #property indicator_buffers x  -->  must always match with x quantity of SetIndexBuffer 

- IndicatorBuffers(x)  --> only needs to be declared if we are using the preset index buffer(s)  as an extra precalculating container for the buffe(s)r declared for visual.

A more simple illustration  would be : 

#property indicator_buffers 1  // I'm assigning one of the used preset buffer(s) as visual outlet.
                               // (consequently MT will churn out the necessary parameters like DRAW_LINE, colors, etc.
                               // equal to the qty declared )

IndicatorBuffers(3)  // but for displaying that one visual, I'm using / ordering 2 more of your preset buffers to calculate it. 
                     // But I'm not gonna display it (implied in the above #property qty).
                     // ( MT consequently bind these 2 to the visual one)
                     // (what happened if we don't do this, presumeably the visual will display incorrect values).

I guess the benefit of the calulation buffer is it would expand the array with bars being added to the chart, whereas if you elect to use a normal array, you need to resize and I imagine need to shift the array elements 1 bar to the left each time, using ArrayCopy. 

 not true. Below is big quote-unquote AFAIK. I haven't verify this.  

The values and size of regular arrays can be maintained by :

if new bar arrives :  

ArraySetAsSeries(buffer,false);

ArrayResize(buffer,Bars);

ArraySetAsSeries(buffer,true);

This is because regular arrays are not indexed reversed . While a preset IndexBuffer is. 

 If my understanding is correct, you could launch your indicator using IndicatorBuffers() only and use the SetIndexStyle to adjust its appearance and properties. Using the controlling compilation simply allows the user to adjust the properties externally? Is this correct?

rather, the #property indicator_buffers & SetIndexBuffer alone. Make a new Indy with MetaEditor wizard, Add different indexes qty and it will churn out #property indicator_buffers accordingly - not IndicatorBuffers(). 

you're talking about other #property controlling compilation. It's not related to IndicatorBuffers(). 

it's SetIndexStyle (externally & programmatically) vs. #property indicator_colorN etc. (externally only)

not IndicatorBuffers() vs. controlling compilation 

 Sorry, for all the questions but I guess this is how I learn.......

my pleasure Kenny,  It works both ways... 

 

Ok, thanks a mil.....I think I am slowly getting a handle on it.