//+------------------------------------------------------------------+
//|                                                   StochBlocs.mq4 |
//|                      Copyright © 2012, Kilian Brachtendorf       |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Kilian Brachtendorf"
#property link      ".."

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 Gray
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];


extern int K_Period = 9;
extern int D_Period = 5;
extern int Slowing = 3;
extern string MA_Info = "SMA(0) EMA(1) SMMA(2) LWMA(3)";
extern int MA_Type = 0;
extern string PriceFieldInfo = "Low/High(0) Close/Close(1)";
extern int PriceField = 0;
extern string Mode_Info = "MainLine(0) SignalLine(1)";
extern int Mode =0;

extern int Overbought = 80;
extern int Oversold = 20;

extern int Histogram_Width = 3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,Histogram_Width);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,Histogram_Width);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,Histogram_Width);
   SetIndexBuffer(2,ExtMapBuffer3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {   
    int limit;
    int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop
     for(int i=0; i<limit; i++)
       {
       double Stoch  = iStochastic(NULL,0,K_Period,D_Period,Slowing,MA_Type,PriceField,Mode,i);
       double Stoch1 = iStochastic(NULL,0,K_Period,D_Period,Slowing,MA_Type,PriceField,Mode,i+1);
       
            if(Stoch1 > Overbought && Stoch < Overbought){ExtMapBuffer1[i] = 0;ExtMapBuffer2[i] = 1; ExtMapBuffer3[i] = 0;}
       else if(Stoch1 < Oversold   && Stoch > Oversold)  {ExtMapBuffer1[i] = 1;ExtMapBuffer2[i] = 0; ExtMapBuffer3[i] = 0;}
       else {ExtMapBuffer1[i] = 0;ExtMapBuffer2[i] = 0; ExtMapBuffer3[i] = 1;}
       }
  //---- done
     return(0);
    }
 
//+------------------------------------------------------------------+