//+------------------------------------------------------------------+
//|                                                      MACD-2c.mq4 |
//|                                Copyright © 2005-2006, David W. Thomas |
//|                                           http://www.davidwt.com |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright © 2005-2006, David W. Thomas"
#property link      "http://www.davidwt.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Crimson
#property indicator_color3 Green
#property indicator_color4 Red

//---- input parameters
extern int       FastMAPeriod=12;
extern int       SlowMAPeriod=26;
extern int       SignalMAPeriod=9;
extern int       OscillatorMethod=MODE_EMA;
extern int       SignalMethod=MODE_SMA;
extern int       AppliedPrice=PRICE_CLOSE;

//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBufferBull[];
double HistogramBufferBear[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   //---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MACDLineBuffer);
   SetIndexDrawBegin(0,SlowMAPeriod);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,SignalLineBuffer);
   SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,HistogramBufferBull);
   SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(3,HistogramBufferBear);
   SetIndexDrawBegin(3,SlowMAPeriod+SignalMAPeriod);
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //---- 
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   double diff;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;

   for(int i=limit; i>=0; i--)
   {
      MACDLineBuffer[i] = iMA(NULL,0,FastMAPeriod,0,OscillatorMethod,AppliedPrice,i) - iMA(NULL,0,SlowMAPeriod,0,OscillatorMethod,AppliedPrice,i);
      SignalLineBuffer[i] = iMAOnArray(MACDLineBuffer, 0, SignalMAPeriod, i, SignalMethod, 0);
      diff = MACDLineBuffer[i] - SignalLineBuffer[i];
      if (diff > 0)
      {
         HistogramBufferBull[i] = diff;
         HistogramBufferBear[i] = EMPTY_VALUE;
      }
      else
      {
      	HistogramBufferBull[i] = EMPTY_VALUE;
         HistogramBufferBear[i] = diff;
      }
   }
   
   //----
   return(0);
}
//+------------------------------------------------------------------+