Hello there,

i've been making my own custom indicator... well i don't know if something like that already exists or not and i've been too lazy to find out - so i tried programming it myself. Anyways there're some problems that i can't quite figure out. It works how it's intended (i suppose), but i think there's something missing in the Init() function. When i change timechart it becomes overflowed, same when i start a program anew - that's why i think there's a problem with init(). And i've got same problems with another of my custom indicators, so i think it has something to do with iMAOnArray() function too.

Note: it's an Average (ema) RSI indicator (created mostly by editing the RSI indicator)- thus the name ARSI


Code:
#property copyright "Scorch"
#property link      ""

#property indicator_separate_window
#property indicator_minimum 25
#property indicator_maximum 75
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int RSIPeriod=30;
extern int AveragePeriod=20;
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
double AverageBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(1,PosBuffer);
   SetIndexBuffer(2,NegBuffer);   
   SetIndexBuffer(3,RSIBuffer);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AverageBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="ARSI("+RSIPeriod+","+AveragePeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   if (RSIPeriod >= AveragePeriod) SetIndexDrawBegin(0,RSIPeriod+1);
      else SetIndexDrawBegin(0,AveragePeriod+1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
   double rel,negative,positive;
//----
   if(Bars<=RSIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-RSIPeriod-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=Close[k]-Close[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/RSIPeriod;
         negative=sumn/RSIPeriod;
        }
      else
        {
         //---- smoothed moving average
         rel=Close[i]-Close[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
         negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
        }
      PosBuffer[i]=positive;
      NegBuffer[i]=negative;
      if(negative==0.0) RSIBuffer[i]=0.0;
      else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
      
      //if (counted_bars>=AveragePeriod) 
      AverageBuffer[i] = iMAOnArray(RSIBuffer,0,AveragePeriod,0,MODE_SMA,i);
      
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
Please help.
Thx in advance