//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"

/*--------------------------------------------------------------------
MACD MT4:

The referenced copyright indicator is a customized MACD study and does
not display the standard MACD histogram.  This version adds the MACD
Line (which happens to outline the custom histogram) and 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    

//---- indicator parameters
extern int  FastEMA          = 12;
extern int  SlowEMA          = 26;
extern int  SignalSMA        = 9;

//---- indicator buffers
double      MacdBuffer[];
double      Zeroline[];
double      MACDLineBuffer[];
double      SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
 
   //---- drawing settings       
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE); 
   SetIndexStyle(2,DRAW_LINE);   
   SetIndexDrawBegin(2,SlowEMA);  
   SetIndexStyle(3,DRAW_LINE);
   SetIndexDrawBegin(3,SignalSMA);
      
   //---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer);
   SetIndexBuffer(1,Zeroline);
   SetIndexBuffer(2,MACDLineBuffer);
   SetIndexBuffer(3,SignalBuffer);
   
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD MT4  ("+FastEMA+","+SlowEMA+","+SignalSMA+")");       
   SetIndexLabel(0, NULL);  //SetIndexLabel(0,"MACD");
   SetIndexLabel(1, NULL);  //SetIndexLabel(1,"Signal"); 
   SetIndexLabel(2, NULL);
   SetIndexLabel(3, NULL);
   SetIndexLabel(4, NULL);
   SetIndexLabel(5, NULL);
   SetIndexLabel(6, NULL);
   SetIndexLabel(7, NULL);
     
   //---- initialization done
   return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   //---- 
   
   //----
   return(0);
} 
 
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{
   int i,limit;
   int counted_bars=IndicatorCounted();
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   //---- items counted in buffers
   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++)      
      Zeroline[i] = 0;
   for(i=0; i<limit; i++)                  
      MACDLineBuffer[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);
          
   //---- done
   return(0);
}
  
//+------------------------------------------------------------------+
//| End of Program                                                   |
//+------------------------------------------------------------------+