//+------------------------------------------------------------------+
//|                                                      macd-cci.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  Black
#property  indicator_color2  Green
#property  indicator_color3  Red
#property  indicator_color4  SteelBlue
#property  indicator_color5  Goldenrod

extern int MACDFastPeriod = 12;
extern int MACDSlowPeriod = 26;
extern int MACDSignalSMA  =  9;
extern int CCIPeriod      = 14;

//---- indicator buffers
double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     ExtBuffer3[]; //signal buffer
double     ExtBuffer4[]; //cci buffer
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   //---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   IndicatorDigits(Digits+1);
   SetIndexDrawBegin(0,MACDSlowPeriod);
   SetIndexDrawBegin(1,MACDSlowPeriod);
   SetIndexDrawBegin(2,MACDSlowPeriod);
   SetIndexDrawBegin(3,MACDSlowPeriod+MACDSignalSMA);
//---- 3 indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD+CCI(" 
      + DoubleToStr(MACDFastPeriod,0) + "," 
      + DoubleToStr(MACDSlowPeriod,0) + "," 
      + DoubleToStr(MACDSignalSMA, 0) + ","
      + DoubleToStr(CCIPeriod, 0)     + ")");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Awesome Oscillator                                               |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 

   for(int i=0; i<limit; i++)
   {
      //macd
      ExtBuffer0[i] = iMA(NULL,0,MACDFastPeriod,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,MACDSlowPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      //cci
      ExtBuffer4[i] = iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i) / MathPow(10, Digits);
   }  
   //macd signal line 
   for(i=0; i<limit; i++)
      ExtBuffer3[i]=iMAOnArray(ExtBuffer0,Bars,MACDSignalSMA,0,MODE_SMA,i);


            
//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ExtBuffer0[i];
      prev=ExtBuffer0[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ExtBuffer2[i]=current;
         ExtBuffer1[i]=0.0;
        }
      else
        {
         ExtBuffer1[i]=current;
         ExtBuffer2[i]=0.0;
        }
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

