/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2011, FX Cash Extreme"
#property link      "http://www.FXCashFormula.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DarkGreen
#property indicator_color2 MediumSeaGreen
#property indicator_color3 Brown
#property indicator_color4 DarkGoldenrod

extern string TimeFrame = "H4";

int PeriodLR = 50;
double K2 = 2.0;

int TF;
double kTF;

double buf1[];
double buf2[];
double buf3[];
double buf4[];

bool Start=true;
//int	TFBarCount;


int init() 
{
	Start=true;
	
	if(TimeFrame=="M1") 			TF=1;
	else if(TimeFrame=="M5") 	TF=5;
	else if(TimeFrame=="M15") TF=15;
	else if(TimeFrame=="M30") TF=30;
	else if(TimeFrame=="H1") 	TF=60;
	else if(TimeFrame=="H4")	TF=240;
	else if(TimeFrame=="D1")	TF=1440;
	else if(TimeFrame=="W1") 	TF=10080;
	else if(TimeFrame=="MN1") TF=43200;
	else 
		{
		switch(Period())
			{
			case 1		:	TimeFrame="M5";		TF=5; break;
			case 5		:	TimeFrame="M15";	TF=15; break;
			case 15		:	TimeFrame="M30";	TF=30; break;
			case 30		:	TimeFrame="H1";		TF=60; break;
			case 60		:	TimeFrame="H4";		TF=240; break;
			case 240	:	TimeFrame="D1";		TF=1440; break;
			case 1440	:	TimeFrame="W1";		TF=10080; break;
			case 10080:	TimeFrame="MN1";	TF=43200; break;
			case 43200:	TimeFrame="MN1";	TF=43200; break;
			}
		Alert("Wrong TimeFrame! TimeFrame is set to " + TimeFrame);
		}
	
	kTF=TF/Period();
	PeriodLR=PeriodLR*kTF;
	
	IndicatorShortName("FC Extreme MTF " + TimeFrame);
	
	SetIndexStyle(0, DRAW_HISTOGRAM,DRAW_LINE,3);
	SetIndexBuffer(0, buf1);
	SetIndexEmptyValue(0, EMPTY_VALUE);

	SetIndexStyle(1, DRAW_HISTOGRAM,DRAW_LINE,3);
	SetIndexBuffer(1, buf2);
	SetIndexEmptyValue(1, EMPTY_VALUE);

	SetIndexStyle(2, DRAW_HISTOGRAM,DRAW_LINE,3);
	SetIndexBuffer(2, buf3);
	SetIndexEmptyValue(2, EMPTY_VALUE);

	SetIndexStyle(3, DRAW_HISTOGRAM,DRAW_LINE,3);
	SetIndexBuffer(3, buf4);
	SetIndexEmptyValue(3, EMPTY_VALUE);

	return (0);
}

int deinit() 
{
   return (0);
}

//+------------------------------------------------------------------+
int start()
{
	double LRV;
	int counted_bars=IndicatorCounted();

	int i=Bars-counted_bars-1;
	while(i>0)
		{
		LRV=LinearRegressionValue(PeriodLR,i);
		if(LRV > 0.0) 
			{
			buf3[i] = 0; 
			buf4[i] = 0;
			if(LRV>=buf1[i+1]+buf2[i+1]) {buf1[i] = LRV; buf2[i] = 0;}
			else {buf1[i] = 0; buf2[i] = LRV;}
			} 
		else 
			{
			buf1[i] = 0;
			buf2[i] = 0;
			if(LRV<=buf3[i+1]+buf4[i+1]) {buf3[i] = LRV; buf4[i]=0;}
			else {buf3[i] = 0; buf4[i]=LRV;}
			}
		i--;
		}
	//----
	return(0);
}

//+------------------------------------------------------------------+
double LinearRegressionValue(int perLR, int shift) 
{
	double L,H,l,h;
	double pr = 0;
	double pr6 = 0;
	double sum = 0;
	double isum = 0;
	double PrSum = 0;
	double val1 = 0;
	double v1=0;
	
	pr = perLR * (perLR - 1) / 2.0;
	pr6 = (perLR - 1) * perLR * (perLR * 2 - 1) / 6;
	
	for (int i = 0; i <= perLR - 1; i++) 
		{
		L = Low[i + shift];
		H = High[i + shift];
		for (int j = i; j <= i + perLR - 1; j++) 
			{
			h=High[j + shift];
			l=Low[j + shift];
			if(H<h) H=h;
			if(L>l) L=l;
			}
		v1=Close[i + shift] - ((L + H) / 2.0 + iMA(NULL, 0, perLR, 0, MODE_EMA, PRICE_CLOSE, i + shift)) / 2.0;
		isum += i * v1;
		sum += v1;
		}
	PrSum = pr * sum;
	
	double ch = perLR * isum - PrSum;
	double zn = pr * pr - perLR * pr6;
	
	if (zn != 0.0) val1 = ch / zn;
	else val1 = 0;
	
	double val2 = (sum - val1 * pr) / perLR;
	double lrv = val2 + val1 * (perLR - 1);
	return (lrv);
}

