//+------------------------------------------------------------------+
//|                                                        DTOsc.mq4 |
//|                             Copyright © 2006, House of Nuts Inc. |
//|                                       http://www.houseofnuts.org |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, House of Nuts Inc."
#property link      "http://www.houseofnuts.org"

#property indicator_separate_window
#property indicator_level1 25
#property indicator_level2 75
//#property indicator_minimum -5
//#property indicator_maximum 105
#property indicator_buffers 4
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Magenta

//---- input parameters
extern int       rsiLength=13;
extern int       kLength=8;
extern int       KSmooth=8;
extern int       DLength=5;
extern bool      SmoothKLine=true;
//---- buffers
double K[];
double D[];
double rsi[];
double stoch[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,K);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,D);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(2,rsi);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,stoch);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

   for (int i = Bars - counted_bars; i >= 0; i--)
   {
      rsi[i] = iRSI(Symbol(), 0, rsiLength, PRICE_TYPICAL, i); 
      stoch[i] = iStochK (kLength, i); 
      K[i] = Smooth(stoch, KSmooth, i);
      D[i] = Smooth(K, DLength, i);
   }

   return(0);
  }
  
double iStochK(int len, int bar)
{
   double ll = 9999, hh = -5555;
   for(int i = 0; i < len; i++)
   {
      if (ll > rsi[bar + i]) ll = rsi[bar + i];
      if (hh < rsi[bar + i]) hh = rsi[bar + i];
   }
   
   if (ll == hh) return (stoch[bar+1]);
   
   return (100 * (rsi[bar] - ll) / (hh - ll));
}


double Smooth (double array[], int period, int shift)
{
   double sum = 0;
   for (int i = shift + period - 1; i >= shift; i--)
   {
      sum += array[i];
   }
   return (sum / period);
}


//+------------------------------------------------------------------+