//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//+------------------------------------------------------------------+

#property  copyright ""
#property  link      ""

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Lime
#property  indicator_color2  Red
#property  indicator_color3  Red
#property  indicator_style3  STYLE_DOT
#property  indicator_color4  White
#property  indicator_style4  STYLE_DOT

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;

//---- indicator buffers
double     MacdBufferUp[];
double     MacdBufferDn[];
double     SignalBuffer[];
double     MacdBuffer[];

string shortname;
datetime nextbartime;
double myPoint;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+


int init()
  {

/////////////////////////////



/////////////////////////////

//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);


   SetIndexDrawBegin(1,SlowEMA);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBufferUp);
   SetIndexBuffer(1,MacdBufferDn);
   SetIndexBuffer(2,SignalBuffer);
   SetIndexBuffer(3,MacdBuffer);
//---- name for DataWindow and indicator subwindow label
	shortname=WindowExpertName();
   IndicatorShortName(shortname);
   SetIndexLabel(0,"MACD Up");
   SetIndexLabel(1,"MACD Down");
   SetIndexLabel(2,"Signal");
   SetIndexLabel(3,"MACD");
   
   myPoint = SetPoint();
   
//---- initialization done
   return(0);
}

int deinit()
{
}


//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {


   double dif1, dif2;
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
	if(Time[0]!=nextbartime)
	{
		limit=Bars-SlowEMA;
		nextbartime=Time[0];
	}
	else
   	limit=MathMin(Bars-SlowEMA,Bars-counted_bars);

//---- macd counted in the 1-st buffer
   for(int i=0;i<limit;i++) {
     	MacdBuffer[i] = iMACD(NULL,0,FastEMA,SlowEMA, SignalSMA,PRICE_CLOSE,MODE_MAIN,i);
      SignalBuffer[i] =iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL,i);
   }

// macd colored set here
   for(i=0;i<limit;i++)
   {
  		dif1 = MacdBuffer[i] - SignalBuffer[i];
  		dif2 = MacdBuffer[i+1] - SignalBuffer[i+1];
  		if(dif1>dif2)
   	{
  			MacdBufferUp[i]=dif1;  // Green
  			MacdBufferDn[i]=0;
   	}
  		else
  		{
  			MacdBufferDn[i]=dif1;  // Red
   		MacdBufferUp[i]=0;
   	}
   }
//---- done
   return(0);
}

double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

//+------------------------------------------------------------------+

