//+------------------------------------------------------------------+
//|                                                      KG MACD.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2008, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 7
#property  indicator_level1 0
#property  indicator_levelcolor Gray
#property  indicator_color1  Lime
#property  indicator_color2  DimGray
#property  indicator_color3  DimGray
#property  indicator_color4  Black
#property  indicator_color5  Red
#property  indicator_color6  DodgerBlue

//---- indicator buffers
extern int  Fast_MA          = 8;
extern int  Slow_MA          = 24;
extern int  MA_Method        = MODE_SMA;
extern int  Price_Type       = PRICE_WEIGHTED;
extern int  BB_Period        = 24;
extern bool MACD_Classic     = true;
double      MACD[], Mid_BB[], Upper_BB[], Lower_BB[], RedDot[], BlueDot[];
string crossing_txt, trend_txt, BB_Cond_txt;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(8);
//---- drawing settings
   if(MACD_Classic==false){
   SetIndexStyle(0,DRAW_LINE,EMPTY,1); 
   SetIndexStyle(1,DRAW_LINE,EMPTY,1);
   SetIndexStyle(2,DRAW_LINE,EMPTY,1);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_ARROW,EMPTY,0);
   SetIndexArrow(4,108);
   SetIndexStyle(5,DRAW_ARROW,EMPTY,0);
   SetIndexArrow(5,108);
   }
   else{
   SetIndexStyle(0,DRAW_LINE,EMPTY,1); 
   SetIndexStyle(1,DRAW_LINE,EMPTY,1);
   SetIndexStyle(2,DRAW_LINE,EMPTY,1);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_HISTOGRAM,EMPTY,2);
   SetIndexStyle(5,DRAW_HISTOGRAM,EMPTY,2);
   }
//---- 4 indicator buffers mapping
   SetIndexBuffer(0,Mid_BB);   
   SetIndexBuffer(1,Upper_BB);
   SetIndexBuffer(2,Lower_BB);
   SetIndexBuffer(3,MACD);
   SetIndexBuffer(4,BlueDot);
   SetIndexBuffer(5,RedDot);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator                               |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current,middle,upper,lower,close,sma_middle;
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   //---- macd counted in the 1-st additional buffer
   for(int i=0; i<limit; i++)
   {
      MACD[i]=iMA(NULL,0,Fast_MA,0,MA_Method,Price_Type,i)-iMA(NULL,0,Slow_MA,0,MA_Method,Price_Type,i);
   }
   for(i=0; i<limit; i++)
      {   Mid_BB[i]   =iMAOnArray(MACD,Bars,BB_Period,0,MODE_SMA,i);
          Upper_BB[i] =iBandsOnArray(MACD,Bars,BB_Period,1,0,MODE_UPPER,i);
          Lower_BB[i] =iBandsOnArray(MACD,Bars,BB_Period,1,0,MODE_LOWER,i);
      }
   for(i=0; i<limit; i++)
      {   BlueDot[i] = MACD[i];      // Uptrend MACD
          RedDot[i]  = MACD[i];      // Downtrend MACD
          if (MACD[i]<MACD[i+1]) { RedDot[i]=EMPTY_VALUE;  }
          if (MACD[i]>MACD[i+1]) { BlueDot[i]=EMPTY_VALUE; }
      }
      current=MACD[0]; 
      prev=MACD[1];
      middle=Mid_BB[0]; 
      upper=Upper_BB[0]; 
      lower=Lower_BB[0];
      close=iClose(NULL,0,0);
      sma_middle=iMA(NULL,0,BB_Period,0,MODE_SMA,Price_Type,0);
       
       if(current>prev)  {crossing_txt="UP";}
       if(current<prev)  {crossing_txt="DOWN";}
       if(current==prev)  {crossing_txt="FLAT";}
       
       if(current>middle && close>sma_middle)  {trend_txt="TRENDING UP";}
       if(current<middle && close<sma_middle)  {trend_txt="TRENDING DOWN";}
       if( (current>middle && close<sma_middle) || (current<middle && close>sma_middle) ) {trend_txt="SIDEWAY";}
       
       if(current>upper)  {BB_Cond_txt="UBNORMAL UP";}
       if(current<lower)  {BB_Cond_txt="UBNORMAL DOWN";}
       if((current>=lower && current<=upper))  {BB_Cond_txt="NORMAL";}
       
       IndicatorShortName("MA (Fast="+Fast_MA+" Slow="+Slow_MA+")("+"Wave="+crossing_txt+")("+"BB="+BB_Cond_txt+")("+"Trend="+trend_txt+")");
   //---- done
   return(0);
  }
//+------------------------------------------------------------------+

