//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  DarkOrange

//
//
//
//
//

extern string TimeFrame    = "Current time frame";
extern double FastPeriod   = 14;
extern int    FastOrder    =  2;
extern double SlowPeriod   = 34;
extern int    SlowOrder    =  2;
extern int    SignalPeriod =  9;
extern int    SignalOrder  =  2;
extern int    SignalMethod = MODE_EMA;
extern int    Price        = PRICE_CLOSE;

//
//
//
//
//

double macduu[];
double macdud[];
double macddd[];
double macddu[];
double macd[];
double signal[];
double slope[];
double trend[];
string indicatorFileName;
bool   returnBars;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,macduu); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,macddd); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(4,macd);   
   SetIndexBuffer(5,signal);
   SetIndexBuffer(6,slope);
   SetIndexBuffer(7,trend);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame == "returnBars"; if (returnBars) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
      IndicatorShortName(timeFrameToString(timeFrame)+" MACD gaussian ("+DoubleToStr(FastPeriod,2)+","+DoubleToStr(SlowPeriod,2)+","+SignalPeriod+")");
   return(0);
}
int deinit()
{
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { macduu[0] = limit+1; return(0); }
         if (timeFrame != Period())
         {
            limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
            for(int i=limit; i>=0; i--)
            {
               int y = iBarShift(NULL,timeFrame,Time[i]);               
                  macduu[i] = iCustom(NULL,timeFrame,indicatorFileName,"",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,0,y);
                  macddd[i] = iCustom(NULL,timeFrame,indicatorFileName,"",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,1,y);
            }
            return(0);
         }
         
   //
   //
   //
   //
   //
    
      for(i = limit; i>=0; i--)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,Price,i);
            macd[i]   = iGFilter(price,FastPeriod,FastOrder,i,0)-iGFilter(price,SlowPeriod,SlowOrder,i,1);
               if (macd[i]>0) { macduu[i] = High[i]; macddd[i] = Low[i]; }
               if (macd[i]<0) { macddd[i] = High[i]; macduu[i] = Low[i]; }
      }            
         
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define Pi 3.141592653589793238462643

int    periods[3];
double coeffs[][9];
double filters[][3];
double iGFilter(double price, int period, int order, int i, int instanceNo=0)
{
   if (ArrayRange(filters,0)!=Bars)  ArrayResize(filters,Bars);
   if (ArrayRange(coeffs,0)<order+1) ArrayResize(coeffs,order+1);
   if (periods[instanceNo]!=period)
   {
      periods[instanceNo]=period;
         double b = (1.0 - MathCos(2.0*Pi/period))/(MathPow(MathSqrt(2.0),2.0/order) - 1.0);
         double a = -b + MathSqrt(b*b + 2.0*b);
         for(int r=0; r<=order; r++)
         {
             coeffs[r][instanceNo*3+0] = fact(order)/(fact(order-r)*fact(r));
             coeffs[r][instanceNo*3+1] = MathPow(    a,r);
             coeffs[r][instanceNo*3+2] = MathPow(1.0-a,r);
         }
   }

   //
   //
   //
   //
   //
   
   i = Bars-i-1;
   filters[i][instanceNo] = price*coeffs[order][instanceNo*3+1];
      double sign = 1;
         for (r=1; r <= order; r++, sign *= -1.0)
                  filters[i][instanceNo] += sign*coeffs[r][instanceNo*3+0]*coeffs[r][instanceNo*3+2]*filters[i-r][instanceNo];
   return(filters[i][instanceNo]);
}

//
//
//
//
//

double fact(int n)
{
   double a=1;
         for(int i=1; i<=n; i++) a*=i;
   return(a);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}