//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  Silver
#property indicator_color2  Silver
#property indicator_color3  PaleVioletRed
#property indicator_color4  Red
#property indicator_color5  Lime
#property indicator_width2  1
#property indicator_width3  1
#property indicator_level1  0

//
//
//
//
//

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;
extern string _JMA_            = "parameter for JMA";
extern bool   JMA_par          = true;  
extern int    JMAPeriod        = 5;       
extern bool   ShowArrows       = false;
extern string arrowsIdentifier = "Gaussian MACD Arrows";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 241;
extern int    arrowsDnCode     = 242;
            
//
//
//
//
//

double macd[];
double macdl[];
double signal[];
double v11[];
double v22[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{  
   IndicatorBuffers(6);
   SetIndexBuffer(0,macd); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,macdl);
   SetIndexBuffer(2,signal);
   SetIndexBuffer(3,v11);      
   SetIndexBuffer(4,v22);      
   SetIndexBuffer(5,trend);
   
   //
   //
   //
   //
   //
   
   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
   timeFrame         = stringToTimeFrame(TimeFrame);
   
   //
   //
   //
   //
   //
   
   IndicatorShortName(timeFrameToString(timeFrame)+" MACD gaussian smoother("+DoubleToStr(FastPeriod,2)+","+DoubleToStr(SlowPeriod,2)+","+SignalPeriod+")");
   return(0);
}
int deinit() 
{  
   deleteArrows(); 
   
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) { macd[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]);               
                      macd[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,
                                  "",JMA_par,JMAPeriod,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y);
                      macdl[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,
                                  "",JMA_par,JMAPeriod,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,1,y);
                      signal[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,
                                  "",JMA_par,JMAPeriod,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,2,y);
                      v11[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,
                                  "",JMA_par,JMAPeriod,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,3,y);
                      v22[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastPeriod,FastOrder,SlowPeriod,SlowOrder,SignalPeriod,SignalOrder,SignalMethod,Price,
                                  "",JMA_par,JMAPeriod,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,4,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);
      macdl[i] = macd[i];
   }            
   for(i = limit; i>=0; i--)
   {
      if (SignalMethod==4)
            signal[i] = iGFilter(macd[i],SignalPeriod,SignalOrder,i,2);
      else  signal[i] = iMAOnArray(macd,0,SignalPeriod,0,SignalMethod,i);
         
      if(JMA_par)
      { 
        v11[i] = iSmooth(macdl[i], JMAPeriod,i,0);   
        v22[i] = iSmooth(signal[i],JMAPeriod,i,1); 
        trend[i] = trend[i+1];
           if (v22[i]<signal[i]) trend[i] = 1;
           if (v22[i]>signal[i]) trend[i] =-1; 
      }
         
      //
      //
      //
      //
      //
           
      if (ShowArrows)
      {
        deleteArrow(Time[i]);
        if (trend[i] != trend[i+1])
        {
           if (trend[i] == 1)  drawArrow(i,arrowsUpColor,arrowsUpCode,false);
           if (trend[i] ==-1)  drawArrow(i,arrowsDnColor,arrowsDnCode, true);
        }
      }
               
   }
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);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workSmooth[][10];
double iSmooth(double price, double length, int r, int instanceNo=0)
{
   if (ArrayRange(workSmooth,0)!=Bars) ArrayResize(workSmooth,Bars); instanceNo *= 5; r = Bars-r-1;
 	if(r<=2) { workSmooth[r][instanceNo] = price; workSmooth[r][instanceNo+2] = price; workSmooth[r][instanceNo+4] = price; return(price); }
   
   //
   //
   //
   //
   //
   
	double alpha = 0.45*(length-1.0)/(0.45*(length-1.0)+2.0);
   	  workSmooth[r][instanceNo+0] =  price+alpha*(workSmooth[r-1][instanceNo]-price);
	     workSmooth[r][instanceNo+1] = (price - workSmooth[r][instanceNo])*(1-alpha)+alpha*workSmooth[r-1][instanceNo+1];
	     workSmooth[r][instanceNo+2] =  workSmooth[r][instanceNo+0] + workSmooth[r][instanceNo+1];
	     workSmooth[r][instanceNo+3] = (workSmooth[r][instanceNo+2] - workSmooth[r-1][instanceNo+4])*MathPow(1.0-alpha,2) + MathPow(alpha,2)*workSmooth[r-1][instanceNo+3];
	     workSmooth[r][instanceNo+4] =  workSmooth[r][instanceNo+3] + workSmooth[r-1][instanceNo+4]; 
   return(workSmooth[r][instanceNo+4]);
}

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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 char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}



