//+------------------------------------------------------------------+
//|                                                           rsi ma |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DeepSkyBlue
#property indicator_color2  OrangeRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern string TimeFrame   = "Current time frame";
extern int    RsiPeriod   = 14;
extern int    RsiPrice    = PRICE_CLOSE;

//
//
//
//
//

double values[];
double valuehUp[];
double valuehDn[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,valuehUp); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,valuehDn); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,values);
      SetIndexBuffer(3,trend);

      //
      //
      //
      //
      //

      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);

   //
   //
   //
   //
   //
      
   IndicatorShortName(timeFrameToString(timeFrame)+" rsi ma ("+RsiPeriod+")");
   return(0); 
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            limit = MathMin(Bars-counted_bars,Bars-1);
            if (returnBars) { valuehUp[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--)
      {
         int maSlope   = (iMA(NULL,0,RsiPeriod,0,MODE_EMA,PRICE_WEIGHTED,i)-iMA(NULL,0,RsiPeriod,0,MODE_EMA,PRICE_WEIGHTED,i+1))/Point;
             values[i] = MathMax(MathMin(iRSI(NULL,0,RsiPeriod,RsiPrice,i)*maSlope,100),0);
             valuehUp[i] = EMPTY_VALUE;
             valuehDn[i] = EMPTY_VALUE;
             trend[i]    = trend[i+1];

             if (values[i]>values[i+1]) trend[i] =  1;
             if (values[i]<values[i+1]) trend[i] = -1;
             if (trend[i]== 1) valuehUp[i] = 1;
             if (trend[i]==-1) valuehDn[i] = 1;
      }
      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]);
         valuehUp[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,0,y);
         valuehDn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod,RsiPrice,1,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);
}