//+------------------------------------------------------------------+
//|														 Indicator: RSI KDJ.mq4 |
//+------------------------------------------------------------------+

#property	indicator_separate_window
#property	indicator_buffers	4

#property	indicator_type1	DRAW_HISTOGRAM
#property	indicator_style1	STYLE_SOLID
#property	indicator_width1	2
#property	indicator_color1	clrLime
#property	indicator_label1	"Buy"

#property	indicator_type2	DRAW_HISTOGRAM
#property	indicator_style2	STYLE_SOLID
#property	indicator_width2	2
#property	indicator_color2	clrRed
#property	indicator_label2	"Sell"

#property	indicator_type3	DRAW_HISTOGRAM
#property	indicator_style3	STYLE_SOLID
#property	indicator_width3	2
#property	indicator_color3	clrGreen
#property	indicator_label3	"BuySlow"

#property	indicator_type4	DRAW_HISTOGRAM
#property	indicator_style4	STYLE_SOLID
#property	indicator_width4	2
#property	indicator_color4	clrViolet
#property	indicator_label4	"SellSlow"

double UpTrend[];
double DnTrend[];
double BuySlow[];
double SellSlow[];

double	RSI, KDJ_M, KDJ_S;

//+------------------------------------------------------------------+
int OnInit()
{	
	IndicatorBuffers(3);
	SetIndexBuffer(0, UpTrend);
	SetIndexEmptyValue(0, EMPTY_VALUE);
	SetIndexBuffer(1, DnTrend);
	SetIndexEmptyValue(1, EMPTY_VALUE);
	SetIndexBuffer(2, BuySlow);
	SetIndexEmptyValue(2, EMPTY_VALUE);
	SetIndexBuffer(3, SellSlow);
	SetIndexEmptyValue(3, EMPTY_VALUE);

	return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
					const int prev_calculated,
					const datetime& time[],
					const double& open[],
					const double& high[],
					const double& low[],
					const double& close[],
					const long& tick_volume[],
					const long& volume[],
					const int& spread[])
{
	int limit = rates_total - prev_calculated;

	for(int i = limit - 1; i >= 0; i--)
	{
		RSI = iRSI(NULL,0,14,PRICE_CLOSE,i);
		KDJ_M	= iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,i);
		KDJ_S	= iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_SIGNAL,i);
		
		if(RSI > 50)
		{
			if(KDJ_M > KDJ_S)
			{
				UpTrend[i] = 1;
				DnTrend[i] = 0;
				BuySlow[i] = 0;
				SellSlow[i] = 0;
			}
			else
			{
				UpTrend[i] = 0;
				DnTrend[i] = 0;
				BuySlow[i] = 1;
				SellSlow[i] = 0;
			}
		}
		else
		{
			if(KDJ_M < KDJ_S)
			{
				UpTrend[i] = 0;
				DnTrend[i] = 1;
				BuySlow[i] = 0;
				SellSlow[i] = 0;
			}
			else
			{
				UpTrend[i] = 0;
				DnTrend[i] = 0;
				BuySlow[i] = 0;
				SellSlow[i] = 1;
			}
		}
	}
	return(rates_total);
}

//+------------------------------------------------------------------+
