//+------------------------------------------------------------------+
//| 2011 MJ  some changes/additions                                  |
//+------------------------------------------------------------------+



#property indicator_separate_window
#property indicator_buffers   5
#property  indicator_minimum 0
#property  indicator_maximum 1
#property indicator_color1 DarkGreen //Overbought
#property indicator_color2 Maroon //Oversold 
#property indicator_color3 Green //Uptrend
#property indicator_color4 FireBrick //Downtrend
#property indicator_color5 LightSlateGray  // SameDirection - same in between of Overbought and Oversold
#property indicator_width1 5
#property indicator_width2 5
#property indicator_width3 5
#property indicator_width4 5
#property indicator_width5 5


//---- parameters
extern string ______________0____________ = "==Stochastic settings==";
extern string note1 = "Chart Time Frame";
extern string note2 = "0=current time frame";
extern string note3 = "1=M1, 5=M5, 15=M15, 30=M30";
extern string note4 = "60=H1, 240=H4, 1440=D1";
extern string note5 = "10080=W1, 43200=MN1";
extern int	TimeFrame	= 0;		// {1=M1, 5=M5, 15=M15, ..., 1440=D1, 10080=W1, 43200=MN1}
extern string note6 = "Stochastic settings";
extern int       KPeriod     =  14;
extern int       sDPeriod     = 3;
extern int       Slowing     =  3;
extern string note7 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int       MAMethod    =   0;
extern string note8 = "0=high/low, 1=close/close";
extern int       PriceField  =   0;
extern string note9 = "overbought level";
extern int       overBought  =  80;
extern string note10 = "oversold level";
extern int       overSold    =  20;

//---- buffers

double SameDirection[];
double Uptrend[];
double Downtrend[];
double OverBought[];
double OverSold[];

double percentK;
double percentK_previous;
string	IndicatorName;
int  DPeriod     =  1;          // DPerid is not taken into account

bool alertflag=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 
    // Check TimeFrame setting not smaller than current 
    if(TimeFrame < Period() && TimeFrame != 0)
        {
        if(alertflag == 0) {Alert("StochColorBars_mj: \"TimeFrame\" is less than TimeFrame of current graph."); alertflag = 1;}
        return(0);
        }
         
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,OverBought);
   SetIndexLabel(0,""); 
     
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,OverSold);
   SetIndexLabel(1,"");  
   
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,Uptrend);
   SetIndexLabel(2,"");
   
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(3,Downtrend);
   SetIndexLabel(3,"");
   

   
   SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexBuffer(4,SameDirection);
   SetIndexLabel(4,"");
   
    IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
 
	switch(TimeFrame)
	{
		case 1:		IndicatorName="Period M1";	break;
		case 5:		IndicatorName="Period M5"; break;
		case 15:		IndicatorName="Period M15"; break;
		case 30:		IndicatorName="Period M30"; break;
		case 60:		IndicatorName="Period H1"; break;
		case 240:	IndicatorName="Period H4"; break;
		case 1440:	IndicatorName="Period D1"; break;
		case 10080:	IndicatorName="Period W1"; break;
		case 43200:	IndicatorName="Period MN1"; break;
		default:	  {TimeFrame = Period(); init(); return(0);}
	}

 
	IndicatorName = IndicatorName+" StochBars_mj("+KPeriod+","+DPeriod+","+Slowing+")";
	IndicatorShortName(IndicatorName); 
    IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
 


   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   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--;
   int limit=Bars-counted_bars+TimeFrame/Period();



   //-------------------------------2----------------------------------------
   datetime TimeArray[];
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);

   int  i,  y;

   for(i=0, y=0;i<limit-1;i++)
   {
   
      if (Time[i]<TimeArray[y]) y++;


       percentK = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,MODE_MAIN,y);
       percentK_previous = iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,MODE_MAIN,y+1);
            
        SameDirection[i] = EMPTY_VALUE;  
        Uptrend[i] = EMPTY_VALUE;                 
        Downtrend[i] = EMPTY_VALUE;                
        OverBought[i] = EMPTY_VALUE;                
        OverSold[i] = EMPTY_VALUE;       

         if (percentK > overBought)
            {
            OverBought[i] = 1;
            }
        else if (percentK < overSold)
            {
            OverSold[i] = 1;
            }
        else
            {
            if (percentK > percentK_previous)
                {
                Uptrend[i] = 1;    
                }
            else if (percentK < percentK_previous)
                {
                Downtrend[i] = 1;    
                }
            else if (percentK == percentK_previous)
                {
                SameDirection[i] = 1;    
                }                
            }   
   }

   return(0);
}
  