//+------------------------------------------------------------------+
//|                                                     StochRSI.mq4 |
//|                          Copyright © 2006, Robertson Enterprises |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Robertson Enterprises"
#property link      ""

#property indicator_separate_window
#property indicator_level1  0.8
#property indicator_level2  0.2
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red

//---- input parameters
extern int       RSI_Period=5;
extern int       K_Period=3;
extern int       D_Period=3;
//---- buffers
double StochRSI_Buffer[];
double Signal_Buffer[];
double RSI_Buffer[];
double HiRSI_Buffer[];
double LowRSI_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexBuffer(2,RSI_Buffer);
   SetIndexBuffer(3,HiRSI_Buffer);
   SetIndexBuffer(4,LowRSI_Buffer);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,K_Period);
   SetIndexBuffer(0,StochRSI_Buffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Signal_Buffer);
   SetIndexDrawBegin(1,K_Period+D_Period);
   
//---- name for DPeriodataWindow and indicator subwindow label
   IndicatorShortName("Stochastic RSI("+RSI_Period+",%"+K_Period+",%"+D_Period+")");
   SetIndexLabel(0,"StochRSI");
   SetIndexLabel(1,"Signal");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,limit;
   int counted_bars=IndicatorCounted();

   //---- check for possible errors
   if(counted_bars<0) return(-1);

   if(Bars<=RSI_Period || Bars<=K_Period) return(0);
   //---- initial zero
   if(counted_bars<1)
   {
      for(i=1;i<=MathMax(RSI_Period,K_Period);i++) StochRSI_Buffer[Bars-i]=0.0;
      for(i=1;i<=RSI_Period;i++) RSI_Buffer[Bars-i]=0.0;
      for(i=1;i<=K_Period+D_Period;i++) Signal_Buffer[Bars-i]=0.0;
   }
//----
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for(i=0; i<limit; i++)
   {
      static int direction;
      RSI_Buffer[i]=iRSI(NULL,0,RSI_Period,PRICE_TYPICAL,i);
      HiRSI_Buffer[i] = RSI_Buffer[ArrayMaximum(RSI_Buffer,K_Period,0)];
      LowRSI_Buffer[i] = RSI_Buffer[ArrayMinimum(RSI_Buffer,K_Period,0)];
      StochRSI_Buffer[i] = ((iRSI(NULL,0,RSI_Period,PRICE_TYPICAL,i) - LowRSI_Buffer[i])/(HiRSI_Buffer[i] - LowRSI_Buffer[i]));
   }
   
   for(i=0; i<limit; i++)
      Signal_Buffer[i] = iMAOnArray(StochRSI_Buffer,Bars,D_Period,0,MODE_EMA,i);  
//----       

   return(0);
  }
//+------------------------------------------------------------------+

