
#property  indicator_separate_window
#property  indicator_buffers 6

#property  indicator_color6  Black
#property  indicator_width6  1

extern int stochperiod=300;
extern int bbperiod=24;
extern string what_is_pricefield = "0: H/L, 1: C/C (Stochastics indy)";
extern int pricefield=0;


#property indicator_minimum 0
#property indicator_maximum 2.2

   SetLevelValue (1, 1);       // The horizontal line level is set
   SetLevelValue (2, 2);       // The horizontal line level is set
   SetLevelStyle(STYLE_SOLID,1,DarkKhaki);

double stochi[],price_ma[],price_std[],stochi_ma[],stochi_std[],dif[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(0,stochi);
SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(1,price_ma);
SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(2,price_std);
SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,stochi_ma);
SetIndexStyle(4,DRAW_NONE);
   SetIndexBuffer(4,stochi_std);
SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,dif);

   return(0);
  }

int start()
  {
   int limit, handle,i;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   double stochi_perc,price_perc;

   for(i=0; i<limit; i++)
      {
      stochi[i]=iStochastic(NULL,0,stochperiod,1,1,MODE_SMA,pricefield,MODE_MAIN,i);
      price_ma[i]=iMA(NULL,0,bbperiod,0,MODE_SMA,PRICE_CLOSE,i);
      price_std[i]=iStdDev(NULL,0,bbperiod,0,MODE_SMA,PRICE_CLOSE,i);
      }
      
   for(i=0; i<limit; i++)
      {
      stochi_ma[i]=iMAOnArray(stochi,0,bbperiod,0,MODE_SMA,i);
      stochi_std[i]=iStdDevOnArray(stochi,0,bbperiod,0,MODE_SMA,i);
      }      

   for(i=0; i<limit; i++)
      {
      if (stochi_std[i]>0) stochi_perc=(stochi[i]-stochi_ma[i])/(2*stochi_std[i]);
      if (price_std[i]>0) price_perc=(Close[i]-price_ma[i])/(2*price_std[i]);
      dif[i]=MathAbs(stochi_perc-price_perc);
      }  

   return(0);
  }
//+------------------------------------------------------------------+