//+------------------------------------------------------------------+
//|                                                         MACD.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 5
#property indicator_color1 0x707017//DarkSlateGray     // 0 HistogramBuffer1
#property indicator_color5 Red         // 4 SignalLineBuffer
#property indicator_color4 Aqua        // 3 MACDLineBuffer
#property indicator_color3 Gold        // 2 SignalLineGold
#property indicator_color2 Olive       // 1 HistogramBuffer2

#property indicator_width1 4
#property indicator_width2 2
#property indicator_width3 2

//---- input parameters
extern int       FastMAPeriod=75;//144
extern int       SlowMAPeriod=365;//312
extern int       SignalMAPeriod=60;//108

//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer1[];
double HistogramBuffer2[];
double SignalLineGold[];

//---- variables
double alpha = 0;
double alpha_1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   //---- indicators
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MACDLineBuffer);
   SetIndexDrawBegin(3,0);//SlowMAPeriod);

   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,SignalLineBuffer);  
   SetIndexDrawBegin(4,SlowMAPeriod);//SlowMAPeriod+SignalMAPeriod);

   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,SignalLineGold);
   SetIndexDrawBegin(2,SlowMAPeriod);

   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,HistogramBuffer1);
   SetIndexDrawBegin(1,0);//SlowMAPeriod+SignalMAPeriod);

   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,HistogramBuffer2);
   SetIndexDrawBegin(0,SlowMAPeriod);
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("BMI2_Long("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
   //----
	alpha =12.0 / (SignalMAPeriod + 6.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])*1);

      SignalLineGold[i] = (MACDLineBuffer[i] - SignalLineBuffer[i])*12;

      HistogramBuffer2[i] = MACDLineBuffer[i];// - SignalLineGold[i];
      HistogramBuffer1[i] = (((MACDLineBuffer[i] - SignalLineBuffer[i])*12))
                             ;//-HistogramBuffer2[i+1];

   }
   
   //----
   return(0);
}
//+-------------------------------------------