//+------------------------------------------------------------------+
//|                                                  TP RapidRSI.mq4 |
//|                                                 NoName Copyright |
//|                                   http://TakeProfit.BlogSpot.com |
//+------------------------------------------------------------------+
#property copyright "Tak3Profit Copyright"
#property link      "http://TakeProfit.BlogSpot.com"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_level1 70
#property indicator_level2 50
#property indicator_level3 30

extern string TimeFrame   = "current time frame";
extern int RSIPeriod = 14;
extern int RSIPrice  = PRICE_CLOSE;
extern int MaPeriod  =  5;
extern int MaMethod  =  0;

double RSI[];
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0, RSI); SetIndexDrawBegin(0, RSIPeriod);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(timeFrame)+" RapidRSI [" + RSIPeriod + "," + MaPeriod + "]");
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { RSI[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(int i = limit; i>=0; i--)
      {
         double up=0, down=0, diff=0;
         for(int j= i + RSIPeriod - 1; j>=i; j--)
         {
            diff = iMA(NULL,0,MaPeriod,0,MaMethod,RSIPrice,j)- iMA(NULL,0,MaPeriod,0,MaMethod,RSIPrice,j+1);
            if(diff>0)
                  up   += diff;
            else  down += -diff;
         }
         if(up + down == 0)
               RSI[i] = 50;
         else  RSI[i] = 100 * up / (up + down);
      }
      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,RSIPrice,MaPeriod,MaMethod,0,y);
   }
   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);
}