//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Averages Convergence/Divergence"
#property strict

#include <MovingAverages.mqh>

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Blue
#property  indicator_color2  Green
#property  indicator_color3  Red
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
//--- indicator parameters
input int InpFastEMA=12;   // Fast EMA Period
input int InpSlowEMA=26;   // Slow EMA Period
input int InpSignalSMA=9;  // Signal SMA Period
//--- indicator buffers
extern string S          = "Настройки МА";
extern int    ma_period  = 80;
extern int    ma_shift   = 0 ;      // сдвиг средней
extern int    ma_method  = 0;       // метод усреднения
extern int    applied_price =0;     // тип цены
extern int    hift       = 0;       // сдвиг

extern int        CountBars            = 400;
double    ExtMacdBuffer[];
double    ExtMacdBufferUp[];
double    ExtMacdBufferDown[];
double    Macd;
double    Macd1;
double    MA;
double    MA1;
bool      ExtParameters=false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMacdBuffer);
   SetIndexBuffer(1,ExtMacdBufferUp);
   SetIndexBuffer(2,ExtMacdBufferDown);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"SignalUP");
   SetIndexLabel(1,"SignalDOWN");
//--- check for input parameters

   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,
                 const int prev_calculated,
                 const datetime& time[],
                 const double& open[],
                 const double& high[],
                 const double& low[],
                 const double& close[],
                 const long& tick_volume[],
                 const long& volume[],
                 const int& spread[])
  {
   int i,limit;
 

   limit = MathMin(rates_total-prev_calculated,CountBars);
   for ( i=limit; i>=0; i--){
         MA   = NormalizeDouble(iMA(NULL,0,ma_period,ma_shift,ma_method,applied_price,hift+i) ,Digits+1);
         MA1  = NormalizeDouble(iMA(NULL,0,ma_period,ma_shift,ma_method,applied_price,hift+i+1),Digits+1);
         Macd  =NormalizeDouble(iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i),Digits+1);
         Macd1 =NormalizeDouble(iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i+1)-iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i+1),Digits+1);
         ExtMacdBuffer[i]=Macd;ExtMacdBufferUp[i]=0;ExtMacdBufferDown[i]=0;
         
     if(MA >MA1 &&Macd>Macd1){ExtMacdBufferUp[i]=Macd; ExtMacdBuffer[i]=0;ExtMacdBufferDown[i]=0;}
     if(MA <MA1 &&Macd<Macd1){ExtMacdBufferDown[i]= Macd;ExtMacdBuffer[i]=0;ExtMacdBufferUp[i]=0;}
     
      }
//--- done
  
   return(rates_total);
  }
//+------------------------------------------------------------------+