//+------------------------------------------------------------------+
//|                                                DL Hurst MACD.mq4 |
//|                                         Copyright 2015, Pips400  |
//+------------------------------------------------------------------+

#property copyright "Copyright 2015, Pips400, Dragpons Lair Forex"
#property link      "https://www.youtube.com/channel/UC3-o47xzGmPkgb4QFYl8D0A"
#property version   "1.01"
#property  description "Dragons Lair Forex aims to interpret price action"
#property  description "on a day to day basis using classic and timeless "
#property  description "principles to gain an orientation and identify"
#property  description "key areas of interest for optimal trading." 
#property strict


#include <MovingAverages.mqh>

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Green
#property  indicator_color2  Red
#property  indicator_color3  Silver
#property  indicator_width1  3
#property  indicator_width2  3

input bool use_SMA = true;
input int SignalSMA=9;  // Signal SMA Period

extern string TMA_centered_name = "TMAcentered";
extern int TMA_slow_value = 10;
extern int TMA_fast_value = 5;


//--- indicator buffers
double    MacdBuffer1[];
double    MacdBuffer2[];
double    SignalBuffer[];
double    MACDmaster[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
   
   SetIndexBuffer(0,MacdBuffer1);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   
   SetIndexBuffer(1,MacdBuffer2);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   
   SetIndexBuffer(2,SignalBuffer);
   SetIndexStyle(2,DRAW_LINE);
   
   SetIndexBuffer(3,MACDmaster);
   SetIndexStyle(3,DRAW_NONE);
   
   SetIndexDrawBegin(1,SignalSMA);
   
   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;
//---
   if(rates_total<=SignalSMA)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
      
//--- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
      {
      
       double a = iCustom (NULL,0,TMA_centered_name,TMA_fast_value,0,i);
       double b = iCustom (NULL,0,TMA_centered_name,TMA_slow_value,0,i);
       double c = iCustom (NULL,0,TMA_centered_name,TMA_fast_value,0,i+1);
       double d = iCustom (NULL,0,TMA_centered_name,TMA_slow_value,0,i+1);
       
       MACDmaster[i] = a - b;
       
       if ( a > b)
         if ( a - b > c - d ) 
             { MacdBuffer1[i] = (a - b) ;
               MacdBuffer2[i] = EMPTY_VALUE;
             }
             else
             { MacdBuffer2[i] = (a - b) ;
               MacdBuffer1[i] = EMPTY_VALUE;
             }
        else
            if ( b - a > d - c ) 
             { MacdBuffer2[i] = (a - b) ;
               MacdBuffer1[i] = EMPTY_VALUE;
             }
             else
             { MacdBuffer1[i] = (a - b) ;
               MacdBuffer2[i] = EMPTY_VALUE;
             }

     }

   if (use_SMA)
   SimpleMAOnBuffer(rates_total,prev_calculated,0,SignalSMA,MACDmaster,SignalBuffer);

   return(rates_total);
  }
//+------------------------------------------------------------------+


