A question about "Indicator_buffers".

 

I don;t understand how the indicator lines to be draw just by indicator buffers.

I will explain my doubt by below codes:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver

extern int       MAPeriod=9;
double Buffer[];

int init()
  {
   IndicatorBuffers(1);
   SetIndexBuffer(0, Buffer);
   SetIndexStyle(0,DRAW_LINE);
   return(0);
   }

int deinit()
  {
  return(0);
  }


int start()
  {
   int    counted_bars=IndicatorCounted();
   if(Bars<=MAPeriod) return(0);

   int i=Bars-MAPeriod-1;
   if(counted_bars>=MAPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
     Buffer[i]=Close[i];
     i--;
     }
   return(0);
  }

these code are very simple,and just draw an indicator line to connect each bar's close price,(suppose the mount of bars(including the latest one) is 100).

my question's detail is that:

for the first executed, counted_bars==0;Bars==100; i==99; after while(i>=0), Buffer[100], Buffer[99], Buffer[98], Buffer[97], Buffer[96], Buffer[95], .........., Buffer[3], Buffer[2], Buffer[1] and Buffer[0] all will get their values, then we can get an indicator line immediately(which is produced by connecting each bars' close price).

for the second executed, counted_bars==99, Bars==100, then i=0; now "while(i>=0)", only Buffer[0]'s value has been changed.

then some time past, when these codes were executed, counted_bars==100, Bars==101, then i still is 0; and while(i>=0); still change Buffer[0] only. So how the indicator line can be extended according to numerous new bars coming?

 
vx0532:


then some time past, when these codes were executed, counted_bars==100, Bars==101, then i still is 0; and while(i>=0); still change Buffer[0] only. So how the indicator line can be extended according to numerous new bars coming?

This is part of what makes a buffer a buffer and not simply an array, the size of the buffer increases automatically when a new bar is created so there is no need for ArrayResize()
 
vx0532: these codes were executed, counted_bars==100, Bars==101, then i still is 0; and while(i>=0); still change Buffer[0] only. So how the indicator line can be extended according to numerous new bars coming?
  1. Per what RaptorUK said, on the creation of new bar, the buffer is resized AND the elements are shifted left, and Buffer[0] is set to the empty value
  2. Bars increases from 100 to 101 but counted_bars is NOT increased from 99 to 100 for the first tick of a new bar. i then is one (not zero) and the indicator updates Buffer[1] with the final close price and then Buffer[0].
  3. I prefer using a for loop instead of the while
    int start()
      {
       int    counted_bars=IndicatorCounted();
       if(Bars<=MAPeriod) return(0);
    
       int i=Bars-MAPeriod-1;
       if(counted_bars>=MAPeriod) i=Bars-counted_bars-1;
       while(i>=0)
         {
         Buffer[i]=Close[i];
         i--;
         }
    :
    int start()
      {
       int    counted_bars = IndicatorCounted();
       if(counted_bars < MAPeriod) counted_bars = MAPeriod; // Look back
       for(int i = Bars - 1 - counted_bars; i >= 0; i--)
         {
         Buffer[i]=Close[i];
         }
    :