//+------------------------------------------------------------------+
//|                                               Pep_Stochastic.mq4 |
//|                                            Pepbullish San Badger |
//|                                      www.pepbullish.blogspot.com |
//+------------------------------------------------------------------+

#property copyright "Pepbullish San Badger"
#property link      "www.pepbullish.blogspot.com"

//-- Indicator Settings --

#property indicator_separate_window
#property indicator_buffers     8
#property indicator_level1      20
#property indicator_level2      80
#property indicator_minimum     0
#property indicator_maximum     100

//-- Input Parameters --

extern string  Stochastic       =  " -- Stochastic Settings -- ";

extern int     K_period         =  14;
extern int     D_period         =  3;
extern int     Slowing          =  3; 

extern string  RSI              =  " -- RSI Settings -- ";

extern int     RSI_period       =  14;
extern int     LevelRSI         =  50;

extern string  MovingAverage    =  " -- MA Settings -- ";

extern int     MA_period        =  20;
extern int     MA_shift         =  0;
extern int     MA_type          =  1;

extern string  Std_Dev_Ratio    =  " -- Standard Deviation Ratio Settings -- ";

extern int     Short            =  7;
extern int     Long             =  28;
extern double  LevelStd         =  0.5;

//-- Indicator Buffers --

double GoodTrade[];
double RsiBull[];
double RsiBear[];
double Bull[];
double Bear[];
double Ma[];
double Signal[];
double Stoch[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
  
//-- Drawing Settings and Buffers Mapping --     
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1,C'0,50,0');
   SetIndexBuffer(0,Bull);   
   
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1,0);
   SetIndexBuffer(1,Bear);    
   
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1,C'50,0,0');
   SetIndexBuffer(2,Ma);      
   
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2,Red);
   SetIndexBuffer(3,Signal);
   
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2,Blue);
   SetIndexBuffer(4,Stoch); 
   
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID,2,Teal);
   SetIndexBuffer(5,RsiBull);   
   
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID,2,Crimson);
   SetIndexBuffer(6,RsiBear);    
   
   SetIndexStyle(7,DRAW_LINE,STYLE_SOLID,3,Yellow);
   SetIndexBuffer(7,GoodTrade);     
   
//-- Name for DataWindow --

   IndicatorShortName("Pep_Stochastic   ( "+K_period+" , "+D_period+" , "+Slowing+" )   Values:");   

   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
  
   int    limit;
   int    counted_bars=IndicatorCounted();
   
   if (counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for (int i=0; i<limit; i++)
   
   Stoch[i] = iStochastic(NULL,0,K_period,D_period,Slowing,MODE_EMA,PRICE_CLOSE,MODE_MAIN,i);
   
   for (i=0; i<limit; i++) 
   
   Signal[i] = iStochastic(NULL,0,K_period,D_period,Slowing,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,i);
   
   for (i=0; i<limit; i++) 
   
   Ma[i] = iMAOnArray(Stoch,Bars,MA_period,MA_shift,MA_type,i);  
   
   for (i=0; i<limit; i++)
      {
      
         double Rsi = iRSI(NULL,0,RSI_period,PRICE_CLOSE,i);
         
         double StdDevShort = iStdDev(NULL,0,Short,0,0,PRICE_CLOSE,i);
         double StdDevLong  = iStdDev(NULL,0,Long,0,0,PRICE_CLOSE,i);
         
         if ( StdDevLong != 0 ) double StdDevRatio = StdDevShort/StdDevLong;
         
         if ( StdDevRatio > 0.5 ) GoodTrade[i] = 99;
         
         Bull[i] = 100;   
         Bear[i] = Ma[i];         
       
         if ( Rsi > LevelRSI )
         {
         RsiBull [i] = 8;
         }
         
         if ( Rsi < LevelRSI )
         {
         RsiBear [i] = 4;
         }    
        
      }              
      
   return(0);
  }
//+------------------------------------------------------------------+