#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

extern int       period     =10;
extern bool      histogram    =true;
//extern int       shift        =0;  

double buf[];

int init() {
	string short_name;
	if(period<=0) {
		period=10;
		Alert("ERperiod readjusted");
	}                        

	if(!histogram) SetIndexStyle(0,DRAW_LINE);
	else           SetIndexStyle(0,DRAW_HISTOGRAM);
	SetIndexLabel(0,"TSize");
	//SetIndexShift(0,shift);
	//IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
	IndicatorDigits(0);
	short_name="TSize(";
	IndicatorShortName(short_name+period+")");

	SetIndexBuffer(0, buf);
	return(0);
}

//+--------------------------------------------------------------------------------------------------+
//| Custom indicator iteration function                                                              |
//+--------------------------------------------------------------------------------------------------+
int start() {
	// ---- optimization
	if(Bars<period+2) return(0);

	int counted_bars=IndicatorCounted(),
	limit, maxbar,
	i;

	if(counted_bars<0) return(-1);
	if(counted_bars>0) counted_bars--;

	limit=Bars-counted_bars-1;
	maxbar=Bars-1-period;
	if(limit>maxbar) limit=maxbar;

	for(i=limit; i>=0; i--) {
		double max=0, min=1000000;
		for (int j=0; j<period; j++) {
			if (min>iLow(Symbol(), Period(), i+j)) min=iLow(Symbol(), Period(), i+j);
			if (max<iHigh(Symbol(), Period(), i+j)) max=iHigh(Symbol(), Period(), i+j);
		}
		buf[i]=(max-min)/Point;
	}

	// ----
	return(0);
}
