//+------------------------------------------------------------------+
//|                                                   Percentage.mq4 |
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?t=33898"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  Gold
#property indicator_color2  Gold
#property indicator_color3  Gold
#property indicator_color4  Red
#property indicator_width1  2
#property indicator_width2  1
#property indicator_width3  1
#property indicator_width4  2

//---- input parameters
extern int PERIOD = 1;

//---- indicator buffers
double BufferO[],
		 BufferH[],
		 BufferL[],
		 BufferC[];

//+------------------------------------------------------------------+
void init()
{
	string name = "Percentage Move("+PERIOD+")";
	IndicatorShortName(name);

	if(PERIOD>1)
	{
		SetIndexStyle (0,DRAW_LINE);
		SetIndexLabel (0,"Close "+name);
		SetIndexBuffer(0,BufferC);
	}
	else
	{
		SetIndexStyle (0,DRAW_HISTOGRAM);
		SetIndexStyle (1,DRAW_HISTOGRAM);
		SetIndexStyle (2,DRAW_HISTOGRAM);
		SetIndexStyle (3,DRAW_HISTOGRAM);
		SetIndexLabel (0,"Open "+name);
		SetIndexLabel (1,"High "+name);
		SetIndexLabel (2,"Low "+name);
		SetIndexLabel (3,"Close "+name);
		SetIndexBuffer(0,BufferO);
		SetIndexBuffer(1,BufferH);
		SetIndexBuffer(2,BufferL);
		SetIndexBuffer(3,BufferC);
	}
}

//+------------------------------------------------------------------+
void start()
{
	int count = IndicatorCounted(),
		 start = Bars-1-count,
		 limit = Bars-1-PERIOD;
	if(start > limit)
		start = limit;

	for(int i = start; i>=0; i--)
	{
		double a = Close[i+PERIOD];

		BufferC[i] = 100.0*(Close[i]-a) / a;

		if(PERIOD==1)
		{
			BufferO[i] = 100.0*(Open[i]-a) / a;
			BufferL[i] = 100.0*(Low [i]-a) / a;
			BufferH[i] = 100.0*(High[i]-a) / a;
		}
	}

//----
//	Calculate average absolute move once (~std.dev.)
	if(count==0)
	{
		double sum = 0.0;
		for(i = start; i>=0; i--)
			sum += MathAbs(BufferC[i]);
		Print("Average absolute move("+PERIOD+") = "+(sum/(start+1)+"%. ("+(start+1)+" bars analyzed)"));
	}
}

