#property copyright "www,forex-station.com"
#property link      "www,forex-station.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  clrDodgerBlue
#property indicator_level1  30
#property indicator_level2  70
#property indicator_minimum 0
#property indicator_maximum 100
#property strict

extern ENUM_TIMEFRAMES    TimeFrame      = PERIOD_CURRENT;    // Time frame
extern int                RSIperiod      = 14;                // Rsi period
extern ENUM_APPLIED_PRICE applied_price = PRICE_CLOSE;        // Rsi price to use
extern bool               Interpolate   = true;               // Interpolate in mtf mode?


double rsi[],count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RSIperiod,applied_price,_buff,_ind)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(2);
   SetIndexBuffer(0,rsi);
   SetIndexBuffer(1,count);
  
   indicatorFileName = WindowExpertName();
   TimeFrame         = fmax(TimeFrame,_Period);  
         
   IndicatorShortName(timeFrameToString(TimeFrame)+" RSI ("+(string)RSIperiod+")");
return(0);
}
 
//+------------------------------------------------------------------+
//| MTF RSI                                            |
//+------------------------------------------------------------------+

int start()
{
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = fmin(Bars-counted_bars,Bars-1); count[0]=limit;
            if (TimeFrame!=_Period)
            {
               limit = (int)fmax(limit,fmin(Bars-1,_mtfCall(2,0)*TimeFrame/_Period));
               for (i=limit;i>=0 && !_StopFlag; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);
                     rsi[i] = _mtfCall(0,y);
                     
                     //
                     //
                     //
                     //
                     //
                     
                     if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;
                        #define _interpolate(buff) buff[i+k] = buff[i]+(buff[i+n]-buff[i])*k/n
                        int n,k; datetime time = iTime(NULL,TimeFrame,y);
                           for(n = 1; (i+n)<Bars && Time[i+n] >= time; n++) continue;	
                           for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++)  _interpolate(rsi);                                      
              }           
      return(0);
      }
      
      //
      //
      //
      //
      //
 
      for(i=limit; i>=0; i--) rsi[i] = iRSI(NULL,0,RSIperiod,applied_price,i);  
return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

