#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  Gray
#property indicator_width1  3

#property indicator_color2  clrGreen
#property indicator_width2  3

#property indicator_color3  clrRed
#property indicator_width3  3


#property indicator_level1  45
#property indicator_level2  55
#property indicator_levelcolor  Gray


extern int Tframe1=240;
extern int Tframe2=1440;
extern int Tframe3=10080;
extern int Tframe4=0;
extern int HighLine = 55;
extern int LowLine = 45;



double Crsi[],CrsiH[],CrsiL[];
double four2, four9, day2, day9, week2, week9, mth2, mth9;
double RSItot;
int RSInum;
int fouri, dayi, weeki, monthi, i;

double value;

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0,Crsi); 
   SetIndexStyle(0,DRAW_LINE); 
   
   SetIndexBuffer(1,CrsiH); 
   SetIndexStyle(1,DRAW_LINE); 
   
   SetIndexBuffer(2,CrsiL); 
   SetIndexStyle(2,DRAW_LINE); 
   
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   
   return(0);
}
int deinit() { return(0); }


int start()
{
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   
   int limit = Bars-counted_bars;
   
   for ( i=limit; i>=0; i--)
   {
      fouri = iBarShift(Symbol(), Tframe1, Time[i], false); 
      dayi = iBarShift(Symbol(), Tframe2, Time[i], false);
      weeki = iBarShift(Symbol(), Tframe3, Time[i], false); 
      monthi = iBarShift(Symbol(), Tframe4, Time[i], false);
      
      RSInum=0;  RSItot=0;  

      if(Tframe1>0){
         four2 = iRSI(NULL,Tframe1,2,PRICE_CLOSE,fouri);   
         four9 = iRSI(NULL,Tframe1,9,PRICE_CLOSE,fouri); 
         RSInum=RSInum+2;  RSItot=RSItot+four2+four9;
      }  
      if(Tframe2>0){
         day2 = iRSI(NULL,Tframe2,2,PRICE_CLOSE,dayi);   
         day9 = iRSI(NULL,Tframe2,9,PRICE_CLOSE,dayi); 
         RSInum=RSInum+2;  RSItot=RSItot+day2+day9;
      }  
      if(Tframe3>0){
         week2 = iRSI(NULL,Tframe3,2,PRICE_CLOSE,weeki);   
         week9 = iRSI(NULL,Tframe3,9,PRICE_CLOSE,weeki); 
         RSInum=RSInum+2;  RSItot=RSItot+week2+week9;
      }  
      if(Tframe4>0){
         mth2 = iRSI(NULL,Tframe4,2,PRICE_CLOSE,monthi);   
         mth9 = iRSI(NULL,Tframe4,9,PRICE_CLOSE,monthi); 
         RSInum=RSInum+2;  RSItot=RSItot+mth2+mth9;
      } 

      value = RSItot/RSInum;
      Crsi[i] = value;
      
      if ( value >= HighLine ) CrsiH[i] = value;
      if ( value <= LowLine ) CrsiL[i] = value;
      
      
   }     

   return(0);
}  

