//+------------------------------------------------------------------+
//|                                                 Ratio RSI_v1.mq4 |
//| The Ratio code was written by 2007, Christof Risch (iya)         |
//| All credits to him                                               |
//| Linuxser 2007                                                    |
//| mladen   2008                                                    |
//+------------------------------------------------------------------+
#property copyright "Under The GNU General Public License V3"
#property link      "www.gnu.org"

#property indicator_separate_window
#property indicator_buffers   1
#property indicator_color1  DodgerBlue
#property indicator_level1   30
#property indicator_level2   70
#property indicator_minimum   0
#property indicator_maximum 100

extern string timeFrame    = "current time frame";
extern int    RSIperiod    = 14;
extern int    AppliedPrice =  0;

//
//
//
//
//

double Buffer1[];
int    TimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,Buffer1);
   
   //
   //
   //
   //
   //

   string PriceType;
   switch(AppliedPrice)
   {
      case PRICE_CLOSE:    PriceType = "Close";    break;  // 0
      case PRICE_OPEN:     PriceType = "Open";     break;  // 1
      case PRICE_HIGH:     PriceType = "High";     break;  // 2
      case PRICE_LOW:      PriceType = "Low";      break;  // 3
      case PRICE_MEDIAN:   PriceType = "Median";   break;  // 4
      case PRICE_TYPICAL:  PriceType = "Typical";  break;  // 5
      case PRICE_WEIGHTED: PriceType = "Weighted"; break;  // 6
   }      
   TimeFrame = stringToTimeFrame(timeFrame);
   string TimeFrameStr;
   switch(TimeFrame)
   {
      case PERIOD_M1:  TimeFrameStr="(M1)";      break;
      case PERIOD_M5:  TimeFrameStr="(M5)";      break;
      case PERIOD_M15: TimeFrameStr="(M15)";     break;
      case PERIOD_M30: TimeFrameStr="(M30)";     break;
      case PERIOD_H1:  TimeFrameStr="(H1)";      break;
      case PERIOD_H4:  TimeFrameStr="(H4)";      break;
      case PERIOD_D1:  TimeFrameStr="(Dayly)";   break;
      case PERIOD_W1:  TimeFrameStr="(Weekly)";  break;
      case PERIOD_MN1: TimeFrameStr="(Monthly)"; break;
      default :        TimeFrameStr="";
   }
   IndicatorShortName("Ratio_RSI("+RSIperiod+","+PriceType+")"+TimeFrameStr);
   return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
   int counted_bars = IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           if (TimeFrame > Period()) limit = MathMax(limit,TimeFrame/Period());

   //
   //
   //
   //
   //
   
	for(i = limit; i >= 0; i--)
	{
		int      shift1 = iBarShift(NULL,TimeFrame,Time[i]);
		datetime time1  = iTime    (NULL,TimeFrame,shift1);

   		Buffer1[i] = iRSI(NULL,TimeFrame,RSIperiod,AppliedPrice,shift1) ; 

         //
         //
         //
         //
         //
         
         if(TimeFrame <= Period() || iBarShift(NULL,TimeFrame,Time[i])==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

         //
         //
	      //   apply interpolation
	      //
	      //
		 
  		   for(int n = 1; i+n < Bars && Time[i+n] >= time1; n++) continue;	

  		   double factor = 1.0 / n;
         for(int k = 1; k < n; k++)
   			Buffer1[i+k] = k*factor*Buffer1[i+n] + (1.0-k*factor)*Buffer1[i];
	}
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int char = StringGetChar(tfs,l);
          if((char > 96 && char < 123) || (char > 223 && char < 256))
               tfs = StringSetChar(tfs, l, char - 32);
          else 
              if(char > -33 && char < 0)
                  tfs = StringSetChar(tfs, l, char + 224);
   }

   //
   //
   //
   //
   //
   
   int tf=0;
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
   return(tf);
}