how to limit the NUMBER of bars to count for an indicator?

 
for example if i only want the indicator to show the past 500 bars (i am assuming this will same some calculations for EAs)

//+------------------------------------------------------------------+
//|                                                         MACD Histogram.mq4 |
//|                      Copyright ?2004, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property  copyright "Copyright ?2004, MetaQuotes Software Corp."
#property  link      "https://www.metaquotes.net//"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;
extern int       NoOfBars = 1000;
//---- indicator buffers
double     ind_buffer1[];
double     ind_buffer2[];
double     ind_buffer3[];
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexDrawBegin(0,SignalEMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
   if(!SetIndexBuffer(0,ind_buffer1) &&
      !SetIndexBuffer(1,ind_buffer2) &&
      !SetIndexBuffer(2,ind_buffer3))
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD Histogram("+FastEMA+","+SlowEMA+","+SignalEMA+")");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Average of Oscillator                                     |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
  for(int i=limit; i>=0; i--)
   {
      ind_buffer2[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
   }
  for(i=limit; i>=0; i--)
   {
      ind_buffer3[i] = iMAOnArray(ind_buffer2,0,SignalEMA,i,MODE_EMA,0);
   }
   for(i=limit; i>=0; i--)
   {
      ind_buffer1[i] = ind_buffer2[i] - ind_buffer3[i];
   }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
so i declared that the NoOfBars = 1000; i'v tried inserting it in different places in the indicator code, but it just completely messes the indicator. please help. thank you.
 
replace limit=Bars-counted_bars;
with limit=NoOfBars-counted_bars;

also check that NoOfBars is less than Bars
 
i tried that, but it does a nasty thing at the left end of the indicator, goes to something like -400 for an macd!
 
tried

SetIndexDrawBegin(0,NoOfBars);

but what it does is it subtracts the NoOfBars from the left side of the chart, instead of show #bars on chart, not exactly working.
thank you irtron! do you know if doing this will take less system resource? i have too many charts.
 
Combined with the previous suggestion, it certainly will. I'm not sure if the function call alone does the trick though. The documentation says that it sets the default value, but it doesn't specify whether or not the whole array is calculated or initialised. May be the developers could shed light on this.
 
o6o2:
tried

SetIndexDrawBegin(0,NoOfBars);

but what it does is it subtracts the NoOfBars from the left side of the chart, instead of show #bars on chart, not exactly working.
Well, the question contains the answer - "it subtracts the NoOfBars from the left side of the chart".

SetIndexDrawBegin(0, Bars - NoOfBars);
 
you are a genius! it works! although i have a hard time wrapping my head around the reasoning of how it works, thank you soooo much irtron!
 
o6o2:
it works! although i have a hard time wrapping my head around the reasoning of how it works

     ______________________________________Bars____________________________________________
     ________NoOfBars________                                     ________NoOfBars________
     I|||||||||||||||||||||||I||||||||||||||||||||||||||||||||||||I|||||||||||||||||||||||I
     ^                       ^                                    ^                       ^
bar[Bars-1]       SetIndexDraw(0,NoOfBars)           SetIndexDraw(0,Bars-NoOfBars)      bar[0]
o6o2:
thank you soooo much irtron!

You're welcome,

mqlexpert [at] gmail [dot] com
 
more better is :

SetIndexDrawBegin(0, Bars-WindowFirstVisibleBar( )) ;

for only draw you can see.
 
interesting take on it, could you explain why its better?
Reason: