How to limit bars an indicator uses

 

Hey,

I wrote an indicator displaying me lines at highs and lows. Unfortunately, I get hundreds of lines as all bars of the chart are processed. I would prefer if the indicator just processes the last 500 bars or so. For the sake of convenience I've just inserted the part with the resistance. Can someone help me fixing this problem?

Cheers

#property version   "1.00"
#property strict
#property indicator_chart_window


extern int periode_to_next_extremum = 20;
int bars_to_check = 40;

double ResLevel[], SupLevel[];
datetime ResTime[],SupTime[];


int objectcounter = 1;
string NameRES = "line";
string TextRES = "RES";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArrayResize(ResLevel,1);
   ArrayResize(ResTime,2);
//--- indicator buffers mapping

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors 
   if(counted_bars<0) return(-1);
   limit=Bars - counted_bars;

   
//---- main loop 
   for(int i = 1; i < limit; i++)
      {
      //Resistance
      if(iHighest(NULL,0,MODE_HIGH,bars_to_check,i) == i + periode_to_next_extremum)
         {
         ResLevel[0] = High[i + periode_to_next_extremum];
         ResTime[0] = Time[i + periode_to_next_extremum];
         ResTime[1] = Time[i + periode_to_next_extremum] + 9999999;
         objectcounter++;
         if(!ObjectCreate(NameRES + (string)objectcounter,OBJ_TREND,0,ResTime[0],ResLevel[0],ResTime[1],ResLevel[0]))
            {
            Print("There must be sth wrong Trend: ", GetLastError());      
            }
            else Print("Trend has been drawn: ",NameRES + (string)objectcounter);
         if(!ObjectCreate(TextRES + (string)objectcounter,OBJ_TEXT,0,ResTime[0],ResLevel[0]))
            {
            Print("There must be sth wrong Text: ", GetLastError());
            }
         if(!ObjectSetText(TextRES + (string)objectcounter,TextRES + (string)objectcounter,10,"Times New Roman", Red))
            {
            Print("There must be sth wrong Change Text: ", GetLastError());
            }            
         }
      //Support

      }
      
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
   {
   int number_of_objects_deleted = ObjectsDeleteAll(0,"",0,-1);
   Print("number_of_objects-deleted = ", number_of_objects_deleted);
   }
 
//---
   int limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors 
   if(counted_bars<0) return(-1);
   limit=Bars - counted_bars;

   if(limit > 500) limit = 500; //Add this line
 
Meng Yin Teoh:

it worked, thanks.

 
Meng Yin Teoh:
Worked for me too, thank you :)
Reason: