//+------------------------------------------------------------------+
//|                                            NeutralHedge MACD.mq4 |
//|                                            © 2008.07.08 SwingMan |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "© 2008.07.08 SwingMan"
#property link      ""

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  Turquoise
#property  indicator_color2  Red
#property  indicator_color3  Gray
#property  indicator_color4  Green
#property  indicator_color5  Green

#property  indicator_level1  0.0
#property  indicator_levelstyle  STYLE_SOLID
#property  indicator_levelcolor  Gray


//---- extern parameters ---------------------------------------------
extern int Period_FastMACD   = 5;
extern int Period_SlowMACD   = 13;
extern string ____MovingAverage____ = "0=SMA, 1=EMA, 2=SMMA, 3=LWMA";
extern int Mode_FastMavg   = 3; // 
extern string ____Price____ = "0=Close, 4=Median, 5=Typical, 6=Weighted";
extern int Price_FastMavg  = 5; // 
extern int Period_SmoothMACD = 55;
extern double Factor_TriggerLine = 1.0;
//--------------------------------------------------------------------

//---- indicator buffers
double     MacdBuffer[];
double     absMACDBuffer[];
double     avgMACDBuffer[];
double     MacdBufferUp[];
double     MacdBufferDown[];
double     SignalMacdLineUp[];
double     SignalMacdLineDown[];

//---- variables
string sWindowsName;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {   
   ArraySetAsSeries(absMACDBuffer, true);
   ArraySetAsSeries(avgMACDBuffer, true);
//---- indicator buffers mapping   
   SetIndexBuffer(0,MacdBufferUp);
   SetIndexBuffer(1,MacdBufferDown);
   SetIndexBuffer(2,MacdBuffer);
   SetIndexBuffer(3,SignalMacdLineUp);
   SetIndexBuffer(4,SignalMacdLineDown);      
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 2);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 2);
   SetIndexStyle(2,DRAW_LINE, 0, 1);
   SetIndexStyle(3,DRAW_LINE, 0, 1);
   SetIndexStyle(4,DRAW_LINE, 0, 1);
   SetIndexDrawBegin(0,Period_SlowMACD);
   SetIndexDrawBegin(1,Period_SlowMACD);
   SetIndexDrawBegin(2,Period_SlowMACD);
   SetIndexDrawBegin(3,Period_SlowMACD);
   SetIndexDrawBegin(4,Period_SlowMACD);
   
   IndicatorDigits(Digits);

//---- name for DataWindow and indicator subwindow label
   sWindowsName = "NeutralHedge MACD ("+Period_FastMACD+","+Period_SlowMACD+")";
   IndicatorShortName(sWindowsName);
   SetIndexLabel(0,"MACD up");
   SetIndexLabel(1,"MACD down");
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,"avgMACD");
   SetIndexLabel(4,NULL);

//---- initialization done   
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   ArrayResize(absMACDBuffer, Bars);
   ArrayResize(avgMACDBuffer, Bars);
   
   int i,limit;
   int counted_bars=IndicatorCounted();
   double current, prev, Diff;
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;  
   limit=Bars - counted_bars;   
   
//---- Upper TimeFrame ............................................
   for(i=0; i<limit; i++)  {    
      double FastValue = iMA(NULL,0,Period_FastMACD,0,Mode_FastMavg,Price_FastMavg,i);
      double SlowValue = iMA(NULL,0,Period_SlowMACD,0,Mode_FastMavg,Price_FastMavg,i);
      Diff = 0;
      if (SlowValue > 0)
         Diff = 10000 * (FastValue - SlowValue) / SlowValue;
      MacdBuffer[i]=Diff;
      //-- differences for StdDev
      absMACDBuffer[i] = MathAbs(Diff);
   }   
   //---- average differences
   for(i=0; i<limit; i++)  {
      avgMACDBuffer[i] = iMAOnArray(absMACDBuffer,0,Period_SmoothMACD,0,MODE_SMA,i);
      SignalMacdLineUp[i]   = avgMACDBuffer[i] * Factor_TriggerLine;
      SignalMacdLineDown[i] = -avgMACDBuffer[i] * Factor_TriggerLine;
   }     
     
   bool up=true;  
   for(i=0; i<limit; i++)    
   {
      current=MacdBuffer[i];
      prev   =MacdBuffer[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         MacdBufferDown[i]=current;
         MacdBufferUp[i]=0.0;
        }
      else
        {
         MacdBufferUp[i]=current;
         MacdBufferDown[i]=0.0;
        }
   }     
   
   return(0);
  }
//+------------------------------------------------------------------+