//+------------------------------------------------------------------+
//|                                                        TMACD.mq4 |
//| This indicator will plot the convergence divergence between two MA                                                        
//+------------------------------------------------------------------+

#property  copyright "Free"
#property  link      "kumars4x@gmail.com"


//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 5

#property  indicator_color1  Lime
#property  indicator_color2  Green
#property  indicator_color3  OrangeRed
#property  indicator_color4  Maroon

#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
#property  indicator_width4  2

extern string 	FASTEMA 		= "--FAST EMA Parameters --";
extern bool 	UseTMATrue		= true; //true will use TMATRrue.mq4 and false wil use TMA.mq4

extern int 		FastMAPeriod	= 56;
extern int 		FastMAType		= MODE_LWMA;
extern int 		FastMAShift		= 0;
extern int 		FastMAApplyTo	= PRICE_CLOSE;

extern string 	SLOWEMA 		= "--SLOW EMA Parameters --";
extern int 		SlowMAPeriod	= 240;
extern int 		SlowMAType		= MODE_SMMA;
extern int 		SlowMAShift		= 0;
extern int 		SlowMAApplyTo	= PRICE_MEDIAN;

double  UpBufferDiv[];
double  UpBufferConv[];
double  DownBufferDiv[];
double  DownBufferConv[];
double 	PipDiff[];

double 		myPoint;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{

	SetIndexStyle(0,DRAW_HISTOGRAM);
	SetIndexStyle(1,DRAW_HISTOGRAM);
	SetIndexStyle(2,DRAW_HISTOGRAM);
	SetIndexStyle(3,DRAW_HISTOGRAM);
	SetIndexStyle(4, DRAW_NONE); 
	
	SetIndexBuffer(0,UpBufferDiv);
	SetIndexBuffer(1,UpBufferConv);
	SetIndexBuffer(2,DownBufferDiv);
	SetIndexBuffer(3,DownBufferConv);
	SetIndexBuffer(4, PipDiff); 

	SetIndexLabel(0,NULL); 
	SetIndexLabel(1,NULL); 
	SetIndexLabel(2,NULL); 
	SetIndexLabel(3,NULL); 
	SetIndexLabel(4,"PipDiff");

	if(Digits < 4) 
		myPoint = 0.01;
	else
		myPoint = 0.0001;

	IndicatorDigits(1);
   
	IndicatorShortName("TMACD("+FastMAPeriod+","+SlowMAPeriod+")");
	
	return(0);
}

int start()
{
	int limit;
	int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted
	if(counted_bars>0) counted_bars--;
	limit=Bars-counted_bars;

	for(int i=limit; i>=0; i--)
	{

		UpBufferDiv[i] = EMPTY_VALUE;
		UpBufferConv[i] = EMPTY_VALUE;
		DownBufferDiv[i] = EMPTY_VALUE;
		DownBufferConv[i] = EMPTY_VALUE;
		
		if(UseTMATrue)
			PipDiff[i] = (iMA(NULL,0,FastMAPeriod,FastMAShift,FastMAType,FastMAApplyTo,i)-iMA(NULL,0,SlowMAPeriod,SlowMAShift,SlowMAType,SlowMAApplyTo,i))/myPoint;
		else
			PipDiff[i] = (iCustom(NULL,0,"TMA","current time frame",FastMAPeriod,FastMAApplyTo,1,1,true, false, false, false, false, false, false, 0,i)-iMA(NULL,0,SlowMAPeriod,SlowMAShift,SlowMAType,SlowMAApplyTo,i))/myPoint;
		
		if (PipDiff[i] < 0) 
		{
			if (PipDiff[i] > PipDiff[i+1]) 
			{
				DownBufferConv[i] = PipDiff[i];
			}
			else
			{
				DownBufferDiv[i] = PipDiff[i];
			}
		}
		else 
		{
			if (PipDiff[i] > PipDiff[i+1]) 
			{
				UpBufferDiv[i] = PipDiff[i];
			}
			else
			{
				UpBufferConv[i] = PipDiff[i];
			}
		}
	}
	return(0);
}
//+------------------------------------------------------------------+