//+------------------------------------------------------------------+
//|                                                      newMACD.mq4 |
//|                                Copyright © 2005, David W. Thomas |
//|                                           mailto:davidwt@usa.net |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright © 2005, David W. Thomas"
#property link      "mailto:davidwt@usa.net"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 LimeGreen
#property indicator_color4 Red
#property indicator_maximum  1
#property indicator_maximum -1
#property indicator_width1   2
#property indicator_width2   2
#property indicator_width3   2
#property indicator_width4   2
//---- input parameters
extern int FastMAPeriod = 12;
extern int SlowMAPeriod = 26;
extern int SignalMAPeriod = 9;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer[];
double Histomcu[];
double Histomcd[];
double Histoscu[];
double Histoscd[];
//---- variables
double alpha = 0;
double alpha_1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(Digits + 1);
   IndicatorBuffers(7);   
   SetIndexBuffer(0, Histomcu); SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(1, Histomcd); SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(2, Histoscu); SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexBuffer(3, Histoscd); SetIndexStyle(3, DRAW_HISTOGRAM);
   SetIndexBuffer(4, MACDLineBuffer);
   SetIndexBuffer(5, SignalLineBuffer);
   SetIndexBuffer(6, HistogramBuffer);
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("NMACD(" + FastMAPeriod+"," + SlowMAPeriod + "," + SignalMAPeriod + ")");

	  alpha = 2.0 / (SignalMAPeriod + 1.0);
	  alpha_1 = 1.0 - alpha;
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   //----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   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, MODE_EMA, PRICE_CLOSE, i) - 
                           iMA(NULL, 0, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
       SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
       HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
       Histomcu[i] = EMPTY_VALUE;
       Histomcd[i] = EMPTY_VALUE;
       Histoscu[i] = EMPTY_VALUE;
       Histoscd[i] = EMPTY_VALUE;
       if (MACDLineBuffer[i]>0)                   Histomcu[i] = 1;
       if (MACDLineBuffer[i]<0)                   Histomcd[i] = 1;
       if (MACDLineBuffer[i]>SignalLineBuffer[i]) Histoscu[i] = -1;
       if (MACDLineBuffer[i]<SignalLineBuffer[i]) Histoscd[i] = -1;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+turn(0);
 // }
//+------------------------------------------------------------------+