//+------------------------------------------------------------------+
//|                                                       FIR_MA.mq4 |
//|                                                  v.1  09/04/2006 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
//Global constants
#define pi 3.141592653589793238462643383279502884197169399375105820974944592
//Input parameters
extern int  Periods = 4;   // 1/(2*Periods) sets the filter bandwidth
extern int  Taps    = 21;  // must be an odd number
extern int  Window  = 4;   // selects windowing function
//Indicator buffers
double FIRMA[];
double FIRMADa[];
double FIRMADb[];
double slope[];

double w[];
double wsum = 0.0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0, FIRMA);   SetIndexShift(0, -(Taps - 1) / 2);
   SetIndexBuffer(1, FIRMADa); SetIndexShift(1, -(Taps - 1) / 2);
   SetIndexBuffer(2, FIRMADb); SetIndexShift(2, -(Taps - 1) / 2);
   SetIndexBuffer(3, slope);

   ArrayResize(w, Taps);
   for(int k = 0; k < Taps; k++)
     {
       switch(Window)
         {
           // Rectangular window
           case 1: w[k] = 1.0; break; 
           // Hanning window
           case 2: w[k] = 0.50 - 0.50*MathCos(2.0*pi*k / Taps); break;
           // Hamming window
           case 3: w[k] = 0.54 - 0.46*MathCos(2.0*pi*k / Taps); break;
           //Blackman window
           case 4: w[k] = 0.42 - 0.50*MathCos(2.0*pi*k / Taps) + 
                          0.08*MathCos(4.0*pi*k / Taps); break;
           //Blackman-Harris window 
           case 5: w[k] = 0.35875 - 0.48829*MathCos(2.0*pi*k / Taps) + 
                          0.14128*MathCos(4.0*pi*k / Taps) - 
                          0.01168*MathCos(6.0*pi*k / Taps); break;
           //Rectangular window 
           default: w[k] = 1;break;
         }
       if(k != Taps / 2.0) 
           w[k] = w[k]*MathSin(pi*(k - Taps / 2.0) / Periods) / pi / (k - Taps / 2.0);
       wsum+=w[k];
     }

   IndicatorShortName("FIRMA");
   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 (slope[limit]==-1) CleanPoint(limit,FIRMADa,FIRMADb);
   for(int i = limit; i >= 0; i--)
   {
      slope[i]   = slope[i+1];
      FIRMADa[i] = EMPTY_VALUE;
      FIRMADb[i] = EMPTY_VALUE;
      FIRMA[i] = 0.0;
            for(int k = 0; k < Taps; k++) FIRMA[i] += Close[i+k]*w[k] / wsum;
            if (FIRMA[i] > FIRMA[i+1]) slope[i] =  1;
            if (FIRMA[i] < FIRMA[i+1]) slope[i] = -1;
            if (slope[i]==-1) PlotPoint(i,FIRMADa,FIRMADb,FIRMA);
   }
   return(0);
  }
//+------------------------------------------------------------------+

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}