//+------------------------------------------------------------------+
//|                                                         MACD.mq4 |
//|                                Copyright © 2005, David W. Thomas |
//|                                           mailto:davidwt@usa.net |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, David W. Thomas"
#property link      "mailto:davidwt@usa.net"

/*--------------------------------------------------------------------
MACD True:

The referenced copyright indicator is the correct computation and 
display of the standard MACD study.  This version displays both the 
MACD and Signal lines on top of the histogram for better visibility, 
expecially when the histogram bars are widened.  A Zeroline has been 
added which, for esthetics, does not extend beyond the histogram bars.
It is most usefull for when the study is set up to display only the 
MACD and Signal lines, but not the histogram.
                                                       - Traderathome
---------------------------------------------------------------------*/                                                         

//---- indicator settings
#property indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  DarkGreen    //Histogram
#property  indicator_color2  DimGray      //Zeroline
#property  indicator_color3  DeepSkyBlue  //Macd Line
#property  indicator_color4  Red          //Signal Line     
#property  indicator_width1  2
#property  indicator_width2  1 
#property  indicator_width3  1
#property  indicator_width4  1
#property indicator_style1   STYLE_SOLID  //STYLE_SOLID
#property indicator_style2   STYLE_DOT    //STYLE_DOT
#property indicator_style3   STYLE_SOLID
#property indicator_style4   STYLE_DOT

//---- input parameters
extern int  FastMAPeriod      = 12;
extern int  SlowMAPeriod      = 26;
extern int  SignalMAPeriod    = 9;

//---- buffers
double      HistogramBuffer[];
double      Zeroline[];
double      MACDLineBuffer[];
double      SignalLineBuffer[];

//---- variables
double      alpha   = 0;
double      alpha_1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
   //---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,HistogramBuffer);
   SetIndexDrawBegin(0,SlowMAPeriod+SignalMAPeriod);
   
   SetIndexStyle(1,DRAW_LINE); 
   SetIndexBuffer(1,Zeroline);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,MACDLineBuffer);
   SetIndexDrawBegin(2,SlowMAPeriod);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,SignalLineBuffer);
   SetIndexDrawBegin(3,SlowMAPeriod+SignalMAPeriod);
      
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD True  ("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");  
   SetIndexLabel(0, NULL);  //(0,"MACD");
   SetIndexLabel(1, NULL);  //(1,"Signal");
   SetIndexLabel(2, NULL); 
   SetIndexLabel(3, NULL);
   SetIndexLabel(4, NULL);
   SetIndexLabel(5, NULL);
   SetIndexLabel(6, NULL);
   SetIndexLabel(7, NULL);
      
   //----
	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;
   
   //---- items counted into buffers
   for(int i=limit; i>=0; i--)
   {      
      Zeroline[i] = 0;  
      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]);    
   }
   
   //----
   return(0);
}
//+------------------------------------------------------------------+