//+------------------------------------------------------------------+
//| MACD Color.mq4
//|	Fedorov Valeriy
//+------------------------------------------------------------------+
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 6

#property  indicator_color1  DarkGreen
#property  indicator_color2  Maroon
#property  indicator_color3  Maroon
#property  indicator_color4  DarkGreen
#property  indicator_color5  Blue
#property  indicator_color6  CLR_NONE

#property  indicator_width1  3
#property  indicator_width2  3
#property  indicator_width3  3
#property  indicator_width4  3
#property  indicator_width5  2

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double Mup1[], Mup2[], Mdn3[], Mdn4[];
double SignalBuffer[];
double MacdBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_HISTOGRAM);

   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_LINE);
   
   SetIndexDrawBegin(4,SignalSMA);
   IndicatorDigits(Digits+1);

//---- indicator buffers mapping
   SetIndexBuffer(0,Mup1);
   SetIndexBuffer(1,Mup2);
   SetIndexBuffer(2,Mdn3);
   SetIndexBuffer(3,Mdn4);
   SetIndexBuffer(4,SignalBuffer);

   SetIndexBuffer(5,MacdBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD Color("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"MACD");
   SetIndexLabel(2,"MACD");
   SetIndexLabel(3,"MACD");
   SetIndexLabel(4,"Signal");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{
	int limit,i;
	int counted_bars=IndicatorCounted();

	if(counted_bars>0) counted_bars--;
	limit=Bars-counted_bars;

	for(i=0; i<limit; i++)
		MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
	for(i=0; i<limit; i++)
		SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

	for(i=0; i<limit; i++)
		{
		Mup1[i]=0;
		Mup2[i]=0;
		Mdn3[i]=0;
		Mdn4[i]=0;
		if(MacdBuffer[i]>0) 
			{
			if(MacdBuffer[i]>=MacdBuffer[i+1]) Mup1[i]=MacdBuffer[i];
			else Mup2[i]=MacdBuffer[i];
			}
		if(MacdBuffer[i]<0)
			{
			if(MacdBuffer[i]<=MacdBuffer[i+1]) Mdn3[i]=MacdBuffer[i];
			else Mdn4[i]=MacdBuffer[i];
			}
		}
	return(0);
}
//+------------------------------------------------------------------+