
// indicator by rockit, 2014
// copyleft

#property strict

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

input int interval = 20; // Period

double hbuf[];
double lbuf[];

int OnInit(void)
{
	IndicatorShortName("DC_mod(" + interval + ")");
   IndicatorDigits(Digits);
	SetIndexStyle(0, DRAW_LINE);
	SetIndexDrawBegin(0, 0);
	SetIndexStyle(1, DRAW_LINE);
	SetIndexDrawBegin(1, 0);
	SetIndexBuffer(0, hbuf);
	SetIndexBuffer(1, lbuf);
	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[])
{
	if(rates_total < (interval + 1)) return(0);
	int limit = rates_total - prev_calculated;
	if(limit == rates_total) limit -= interval + 1 + interval;
	for(int i = 0; i < limit; i++) {
		if(TimeDayOfWeek(time[i]) == SUNDAY) {
			hbuf[i] = EMPTY_VALUE;
			lbuf[i] = EMPTY_VALUE;
			continue;
		}
		double h = 0; double l = DBL_MAX; int j = i + 1;
		for(int k = 0; k < interval; k++) {
			if(TimeDayOfWeek(time[j]) == SUNDAY) j += 1;
			if(h < high[j]) h = high[j];
			if(l > low[j]) l = low[j];
			j += 1;
		}
		hbuf[i] = h;
		lbuf[i] = l;
	}
	return(rates_total);
}
