//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  clrLavender
#property indicator_color2  clrLavenderBlush
#property indicator_color3  clrLavender
#property indicator_color4  clrLavenderBlush
#property indicator_color5  clrRoyalBlue
#property indicator_color6  clrCrimson
#property indicator_color7  clrNONE
#property indicator_color8  clrNONE
#property indicator_width1  10
#property indicator_width2  10
#property indicator_width3  10
#property indicator_width4  10
#property indicator_width5  2
#property indicator_width6  2
#property strict

//
//
//
//
//

extern ENUM_TIMEFRAMES    TimeFrame     = PERIOD_CURRENT;   // Time frame to use
input  ENUM_APPLIED_PRICE Macd1Price    = PRICE_CLOSE;      // 1st macd price
input  int                Macd1Fast     = 12;               // 1st macd fast period
input  int                Macd1Slow     = 26;               // 1st macd slow period
input  int                Macd1Signal   = 9;                // 1st macd signal period
input  ENUM_MA_METHOD     Macd1MaMode   = MODE_EMA;         // 1st macd ma method
input  ENUM_MA_METHOD     Signal1MaMode = MODE_EMA;         // 1st macd signal mode
input  ENUM_APPLIED_PRICE Macd2Price    = PRICE_CLOSE;      // 2nd macd price
input  int                Macd2Fast     = 0;               // 2nd macd fast period
input  int                Macd2Slow     = 0;               // 2nd macd slow period
input  int                Macd2Signal   = 0;               // 2nd macd signal period
input  ENUM_MA_METHOD     Macd2MaMode   = MODE_EMA;         // 2nd macd ma method
input  ENUM_MA_METHOD     Signal2MaMode = MODE_EMA;         // 1st macd signal mode
input  bool               Interpolate   = true;             // Interpolate in mtf mode

double macd1up[],macd1dn[],macd1ln[],macd1si[],macd2up[],macd2dn[],macd2ln[],macd2si[],count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Macd1Price,Macd1Fast,Macd1Slow,Macd1Signal,Macd1MaMode,Signal1MaMode,Macd2Price,Macd2Fast,Macd2Slow,Macd2Signal,Macd2MaMode,Signal2MaMode,_buff,_ind)

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(9);
   SetIndexBuffer(0,macd1up); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,macd1dn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,macd2up); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,macd2dn); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,macd1ln); SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(5,macd1si); SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(6,macd2ln); SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(7,macd2si); SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(8,count);
   
   indicatorFileName = WindowExpertName();
   TimeFrame         = fmax(TimeFrame,_Period);
   IndicatorShortName(timeFrameToString(TimeFrame)+" Macd x2");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = fmin(Bars-counted_bars,Bars-1); count[0] = limit;
         if (TimeFrame != _Period)
         {
            limit = (int)fmax(limit,fmin(Bars-1,_mtfCall(8,0)*TimeFrame/_Period));
            for (i=limit;i>=0 && !_StopFlag; i--)
            {
                int y = iBarShift(NULL,TimeFrame,Time[i]);
                   macd1up[i] = _mtfCall(0,y);
                   macd1dn[i] = _mtfCall(1,y);
                   macd2up[i] = _mtfCall(2,y);
                   macd2dn[i] = _mtfCall(3,y);
                   macd1ln[i] = _mtfCall(4,y);
                   macd1si[i] = _mtfCall(5,y);
                   macd2ln[i] = _mtfCall(6,y);
                   macd2si[i] = _mtfCall(7,y);
                   
                   //
                   //
                   //
                   //
                   //
                     
                   if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;
                   #define _interpolate(buff) buff[i+k] = buff[i]+(buff[i+n]-buff[i])*k/n
                   int n,k; datetime time = iTime(NULL,TimeFrame,y);
                      for(n = 1; (i+n)<Bars && Time[i+n] >= time; n++) continue;	
                      for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++)
                      {
                         _interpolate(macd1ln);
                         _interpolate(macd1si);
                         _interpolate(macd2ln);
                         _interpolate(macd2si);
                         if (macd1up[i]!= EMPTY_VALUE) macd1up[i+k] = macd1ln[i+k];
  	                      if (macd1dn[i]!= EMPTY_VALUE) macd1dn[i+k] = macd1ln[i+k];
  	                      if (macd2up[i]!= EMPTY_VALUE) macd2up[i+k] = macd2ln[i+k];
  	                      if (macd2dn[i]!= EMPTY_VALUE) macd2dn[i+k] = macd2ln[i+k];
                       }            
            }
   return(0);
   }
     
   //
   //
   //
   //
   //
   //

   for(i=limit; i >= 0; i--)
   {
      macd1ln[i] = iMA(NULL,0,Macd1Fast,0,Macd1MaMode,Macd1Price,i)-iMA(NULL,0,Macd1Slow,0,Macd1MaMode,Macd1Price,i);
      macd2ln[i] = iMA(NULL,0,Macd2Fast,0,Macd2MaMode,Macd2Price,i)-iMA(NULL,0,Macd2Slow,0,Macd2MaMode,Macd2Price,i);
      macd1up[i] = (macd1ln[i]>0) ? macd1ln[i] : EMPTY_VALUE;
      macd1dn[i] = (macd1ln[i]<0) ? macd1ln[i] : EMPTY_VALUE;
      macd2up[i] = (macd2ln[i]>0) ? macd2ln[i] : EMPTY_VALUE;
      macd2dn[i] = (macd2ln[i]<0) ? macd2ln[i] : EMPTY_VALUE;
   }   
   for(i=limit; i >= 0; i--)
   {
      macd1si[i] = iMAOnArray(macd1ln,0,Macd1Signal,0,Signal1MaMode,i);
      macd2si[i] = iMAOnArray(macd2ln,0,Macd2Signal,0,Signal2MaMode,i);
   }
   return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}