//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Pink
#property indicator_color4 LightGreen
#include <WinUser32.mqh>
#include <stdlib.mqh>

#define EV						EMPTY_VALUE
#define NOT_IN_TRADE			0
#define IN_TRADE				1

extern int 		MaxBars	 	= 2000;
extern double 	DCGap 		= 50;
extern int 		MAFastPrd 	= 50;
extern double  MASlowPrd	= 200;
extern bool		UseTemaMA	= FALSE;

double HistoTop[];
double HistoBot[];
double MASlow[];
double MAFast[];

double RBoxSize, Pips, UPips, DPips, WPips=0, LPips=0, TPips=0, Spread = 0.0002;
bool   NB;
int	 Win=0, Loss=0;
int	 IndieCnt = 0, drawBegin;
int init()
{
	int BarType, i;
	initBuffer(HistoTop,  	"HistoTop",	DRAW_HISTOGRAM,116,	0,0,LightGreen);
	initBuffer(HistoBot,  	"HistoBot",	DRAW_HISTOGRAM,116,	0,0,Pink);
	initBuffer(MASlow,  		"MASlow",	DRAW_LINE,116,			0,2,Blue);
	initBuffer(MAFast,  		"MAFast",	DRAW_LINE,116,			0,2,Gold);
	IndicatorBuffers(IndieCnt);
	
	DCGap *=Point;
	Alert("DCGap="+DCGap);

	return(0);
}

int start()
{ 
   int      counted_bars=IndicatorCounted();
   int      limit, i, j, k;
   double   Gap;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
	limit = MathMin(Bars-counted_bars,Bars-1);
	if( MaxBars != 0 ) limit = MaxBars;
	Alert("Limit = "+limit);
   for(i = limit; i >= 0; i--)
   {
		if( UseTemaMA )
		{
			MASlow[i] = TemaSmooth(0,Close[i],MASlowPrd);
			MAFast[i] = TemaSmooth(1,Close[i],MAFastPrd);
		}
		else
		{
			MASlow[i] = iMA(NULL,0, MASlowPrd,0,MODE_SMA,PRICE_MEDIAN,i);
			MAFast[i] = iMA(NULL,0, MAFastPrd,0,MODE_SMA,PRICE_MEDIAN,i);
		}
		Gap = MAFast[i] - MASlow[i];
		if( MathAbs(Gap) > DCGap )
		{
			if( Gap > 0 ) {HistoTop[i] = MAFast[i]; HistoBot[i] = MASlow[i]; }
			if( Gap < 0 ) {HistoTop[i] = MASlow[i]; HistoBot[i] = MAFast[i]; }
		}
 	}
   return(0);
}
void initBuffer(double array[], string label = "", int type = DRAW_NONE, int arrow = 0, int style = EMPTY, int width = EMPTY, color clr = CLR_NONE) 
{
    SetIndexBuffer(IndieCnt, array);
    SetIndexLabel(IndieCnt, label);
    SetIndexEmptyValue(IndieCnt, EMPTY_VALUE);
    SetIndexDrawBegin(IndieCnt, drawBegin);
    SetIndexShift(IndieCnt, 0);
    SetIndexStyle(IndieCnt, type, style, width, clr);
    SetIndexArrow(IndieCnt, arrow);
    IndieCnt++;
}
double TemaSmooth( int idx, double Price0, double ThisPeriod )
{
	double 			FilterFactor = 2.0 / (1.0 + ThisPeriod);
   double 			Tema2, Tema20, Tema21, Tema22;
   double 			Tema1, Tema10, Tema11, Tema12;
   double 			Price1, Price2;
	static double 	LastTema1[20][3], LastTema2[20][3];
	
	if( FilterFactor >= 1.0 ) { Price1 = Price0; LastTema2[idx][2] = Price0; LastTema2[idx][1] = Price0; LastTema2[idx][0] = Price0; }
	else
	{	
		Tema22 = LastTema2[idx][2] + FilterFactor * (Price0  - LastTema2[idx][2]);
		Tema21 = LastTema2[idx][1] + FilterFactor * (Tema22  - LastTema2[idx][1]);
		Tema20 = LastTema2[idx][0] + FilterFactor * (Tema21  - LastTema2[idx][0]);
		Price1 = 3 * Tema22  - 3 * Tema21 + Tema20;

		LastTema2[idx][2] = Tema22; LastTema2[idx][1] = Tema21; LastTema2[idx][0] = Tema20;	
   }
	if( FilterFactor >= 1.0 ) { Price2 = Price1; LastTema1[idx][2] = Price1; LastTema1[idx][1] = Price1; LastTema1[idx][0] = Price1; }
	else
	{	
		Tema12 = LastTema1[idx][2] + FilterFactor * (Price1  - LastTema1[idx][2]);
		Tema11 = LastTema1[idx][1] + FilterFactor * (Tema12  - LastTema1[idx][1]);
		Tema10 = LastTema1[idx][0] + FilterFactor * (Tema11  - LastTema1[idx][0]);
		Price2 = NormalizeDouble(Price1 + ( Price1 - (3 * Tema12  - 3 * Tema11 + Tema10)),5);

		LastTema1[idx][2] = Tema12; LastTema1[idx][1] = Tema11; LastTema1[idx][0] = Tema10;	
	}
   return( Price2 );
}