#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

int period=0;
extern double pinBarFactor=0.35;
bool	histogram=true;

double buf[];

int init() {
	//if(period<=0) period=10;
	if(!histogram) SetIndexStyle(0,DRAW_LINE); else SetIndexStyle(0,DRAW_HISTOGRAM);
	SetIndexLabel(0,"TSize");
	//SetIndexShift(0,shift);
	//IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
	IndicatorDigits(0);
	IndicatorShortName("Value ("+period+")");
	SetIndexBuffer(0, buf);
	return(0);
}

int start() {
	if(Bars<period+2) return(0);
	int cb=IndicatorCounted();
	if(cb<0) return(-1);
	if(cb>0) cb--;
	int limit=Bars-cb-1;
	int maxbar=Bars-1-period;
	if(limit>maxbar) limit=maxbar;
	for(int 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]=0;
		double barSize=iHigh(Symbol(), Period(), i)-iLow(Symbol(), Period(), i);
		double maxBody, minBody;
		if (iOpen(Symbol(), Period(), i)>iClose(Symbol(), Period(), i)) {
			maxBody=iOpen(Symbol(), Period(), i);
			minBody=iClose(Symbol(), Period(), i);
		} else {
			maxBody=iClose(Symbol(), Period(), i);
			minBody=iOpen(Symbol(), Period(), i);
		}		
		//if ((iHigh(Symbol(), Period(), i)-iOpen(Symbol(), Period(), i))/barSize+(iHigh(Symbol(), Period(), i)-iClose(Symbol(), Period(), i))/barSize<pinBarFactor) buf[i]=1;
		//if ((iLow(Symbol(), Period(), i)-iOpen(Symbol(), Period(), i))/barSize+(iLow(Symbol(), Period(), i)-iClose(Symbol(), Period(), i))/barSize>-pinBarFactor) buf[i]=-1;
		if ((iHigh(Symbol(), Period(), i)-minBody)/barSize<pinBarFactor) buf[i]=1;
		if ((maxBody-iLow(Symbol(), Period(), i))/barSize<pinBarFactor) buf[i]=-1;
		//if (barSize/(iHigh(Symbol(), Period(), i)-iOpen(Symbol(), Period(), i))<0.3) buf[i]=2;
		//if (iLow(Symbol(), Period(), i)<iLow(Symbol(), Period(), i+1) && iHigh(Symbol(), Period(), i)>iHigh(Symbol(), Period(), i+1)) buf[i]=1;
	}
	return(0);
}
