//+------------------------------------------------------------------+
//|                                       rsi of hull moving average |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-station.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  DeepSkyBlue 
#property indicator_width1  2
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  70
#property indicator_level2  30

//
//
//
//
//

extern string TimeFrame   = "Current time frame";
extern int    RsiPeriod   =  9;
extern int    HullPeriod  =  9;
extern int    HullPrice   = PRICE_CLOSE;
extern bool   Interpolate = true;

//
//
//
//
//

double rsi[];
double work1[];
double work2[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(3);
      SetIndexBuffer(0,rsi);
      SetIndexBuffer(1,work1);
      SetIndexBuffer(2,work2);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue";   if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";       if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(timeFrame)+" Rsi of Hull ("+RsiPeriod+","+HullPeriod+")");
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int HalfPeriod = MathFloor(HullPeriod/2);
   int SqrtPeriod = MathFloor(MathSqrt(HullPeriod));
   int i,limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { rsi[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--) work1[i] = 2.0*iMA(NULL,0,HalfPeriod,0,MODE_LWMA,HullPrice,i)-iMA(NULL,0,HullPeriod,0,MODE_LWMA,HullPrice,i);
      for(i=limit; i>=0; i--) work2[i] = iMAOnArray(work1,0,SqrtPeriod,0,MODE_LWMA,i);
      for(i=limit; i>=0; i--) rsi[i]   = iRSIOnArray(work2,0,RsiPeriod,i);
      return(0);
   }

   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         rsi[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,HullPeriod,HullPrice,0,y);

         //
         //
         //
         //
         //
         
            if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
            datetime time = iTime(NULL,timeFrame,y);
               for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
               for(int k = 1; k < n; k++)
                  rsi[i+k] = rsi[i] + (rsi[i+n] - rsi[i])*k/n;
   }
   return(0);         
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}