//+------------------------------------------------------------------+
//|                                                   Stochastic.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 3
//---- input parameters
extern int KPeriod=14;
extern int DPeriod=1;
extern int Slowing=3;
//---- buffers
double FastStoch[];
double MainBuffer[];
double HighestBuffer[];
double LowestBuffer[];
//----
int draw_begin1=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(2, HighestBuffer);
   SetIndexBuffer(3, LowestBuffer);
   SetIndexBuffer(4, FastStoch);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, MainBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="Sto With SHIFT("+KPeriod+","+DPeriod+","+Slowing+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);

//----
   draw_begin1=KPeriod+Slowing;
   SetIndexDrawBegin(0,draw_begin1);

   SetIndexShift(0, 1);//DisplayShift = 1 Chart Bar into the future;

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k;
   int    counted_bars=IndicatorCounted();
   double price;
//----
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
 
     }
//---- minimums counting
   i=Bars-KPeriod;
   if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double min=1000000;
      k=i+KPeriod-1;
      while(k>=i)
        {
         price=Low[k];
         if(min>price) min=price;
         k--;
        }
      LowestBuffer[i]=min;
      i--;
     }
//---- maximums counting
   i=Bars-KPeriod;
   if(counted_bars>KPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double max=-1000000;
      k=i+KPeriod-1;
      while(k>=i)
        {
         price=High[k];
         if(max<price) max=price;
         k--;
        }
      HighestBuffer[i]=max;
      i--;
     }
//---- FastStochastic
   i=Bars-draw_begin1;
   if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumlow=0.0;
      double sumhigh=0.0;
      sumlow+=Close[i]-LowestBuffer[i];
      sumhigh+=HighestBuffer[i]-LowestBuffer[i];
      if(sumhigh==0.0) FastStoch[i]=100.0;
      else FastStoch[i]=sumlow/sumhigh*100;
      i--;
     }
//---- %K line
   i=Bars-draw_begin1;
   if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      MainBuffer[i]=iMAOnArray(FastStoch,Bars,Slowing,0,MODE_SMA,i);
      i--;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//----
   return(0);
  }
//+------------------------------------------------------------------+