//+------------------------------------------------------------------+
//|                                              MACD Adjustable.mq4 |
//|                        Copyright 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property link      "Histo & Line adjustments by cja"
#property link      "2 colour Histogram added by cja"
//updated to new MT4 26/04/2015 cja 
#property strict
 
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 clrGreen
#property indicator_color2 clrRed
#property indicator_color3 clrYellow
#property indicator_color4 clrDodgerBlue
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
#property indicator_width4 1


//---- indicator parameters
extern int  FastEMA          = 12;//Fast MACD
extern int  SlowEMA          = 26;//Slow MACD
extern int  SignalSMA        = 9;//Signal MACD

extern int  Histo_Size       = 3;//1 = Standard Lines & Histo
extern bool Show_MACDLine    = true;//Show MACD Main line 
extern bool Show_MACDSigLine = true;//Show MACD Signal line

//---- indicator buffers
double     ind_Buffer1[];
double     ind_Buffer2[];
double     ind_buffer3a[];
double     ind_buffer3b[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   IndicatorBuffers(4);
   IndicatorDigits(8);
//---- drawing settings
   SetIndexBuffer(0,ind_buffer3a);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ind_buffer3b);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   
   if(Show_MACDLine){SetIndexStyle(2,DRAW_LINE);}
   else {SetIndexStyle(2,DRAW_NONE); }
   SetIndexBuffer(2,ind_Buffer1);
    
   if(Show_MACDSigLine){SetIndexStyle(3,DRAW_LINE);}
   else {SetIndexStyle(3,DRAW_NONE); }
   SetIndexBuffer(3,ind_Buffer2);
    
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD ("+(string)FastEMA+","+(string)SlowEMA+","+(string)SignalSMA+")");

   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Average of Oscillator                                     |
//+------------------------------------------------------------------+
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=0; i<limit; i++)
      ind_Buffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)
                        -iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
   for(int i=0; i<limit; i++)
      ind_Buffer2[i]=iMAOnArray(ind_Buffer1,Bars,SignalSMA,0,MODE_EMA,i);
//---- main loop
   double value=0;
   for(int i=0; i<limit; i++)
      {
         ind_buffer3a[i]=0.0;
         ind_buffer3b[i]=0.0;      
         value=ind_Buffer1[i]-ind_Buffer2[i];
         if (value>0) ind_buffer3a[i]=value*Histo_Size;
         if (value<0) ind_buffer3b[i]=value*Histo_Size;
      }   
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

