//+------------------------------------------------------------------+
//|                                             Color Stochastic.mq4 |
//|                                                           mladen |
//|                                                                  |
//| 2011 MJ  some changes/additions                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""


#property indicator_separate_window
#property indicator_buffers   5
#property  indicator_minimum 0
#property  indicator_maximum 1
#property indicator_color1 LightSlateGray // SameDirection - same in between of Overbought and Oversold
#property indicator_color2 Orange //Uptrend
#property indicator_color3 DeepSkyBlue //Downtrend
#property indicator_color4 Red //Overbought
#property indicator_color5 Blue //Oversold
#property indicator_width1 5
#property indicator_width2 5
#property indicator_width3 5
#property indicator_width4 5
#property indicator_width5 5

//===================================
//---- indicator parameters
//===================================

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     =  5;
extern string       sDPeriod     = "DPeriod is set to 1";          // DPerid is not taken into account
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;

double SameDirection[];
double Uptrend[];
double Downtrend[];
double OverBought[];
double OverSold[];


//----
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,SameDirection);
   SetIndexLabel(0,"");
   
   
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Uptrend);
   SetIndexLabel(1,"");
   
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,Downtrend);
   SetIndexLabel(2,"");
   
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(3,OverBought);
   SetIndexLabel(3,""); 
     
   SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexBuffer(4,OverSold);
   SetIndexLabel(4,"");          
         
         
	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("+KPeriod+","+DPeriod+","+Slowing+")";
	IndicatorShortName(IndicatorName); 
    IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
     
	//IndicatorDigits(1);
    return(0);
}
//+----------------------------------------------------------------------------+



//+----------------------------------------------------------------------------+
//|                                                                            |
//|  Custom indicator deinitialization function                                |
//|                                                                            |
//+----------------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+----------------------------------------------------------------------------+


//+----------------------------------------------------------------------------+
//|                                                                            |
//|  Custom indicator iteration function                                       |
//|                                                                            |
//+----------------------------------------------------------------------------
int start()
    {

    double percentK, percentK_previous;
    datetime TimeArray[];
    int i,y;
    int counted_bars=IndicatorCounted();
    
 
// Plot defined timeframe on to current timeframe   
    ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
       
//---- check for possible errors
    if(counted_bars<0) 
        {
        return(-1);
        }
//---- last counted bar will be recounted
    if(counted_bars>0) 
       {
       counted_bars--;
       }
    
    int counted_limit=Bars-counted_bars;//MathMax(Bars-counted_bars,TimeFrame/Period());
      
      
    for(i=0,y=0;i<counted_limit-1;i++)
        {
        if (Time[i]<TimeArray[y]) y++; 
         
        SameDirection[i] = EMPTY_VALUE;  
        Uptrend[i] = EMPTY_VALUE;                 
        Downtrend[i] = EMPTY_VALUE;                
        OverBought[i] = EMPTY_VALUE;                
        OverSold[i] = EMPTY_VALUE;       
                

        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);
        
        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);
}


