//+------------------------------------------------------------------+
//|                                           Henderson's filter.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "www,forex-tsd.com"
#property link      "www,forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DeepSkyBlue
#property indicator_color2  PaleVioletRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern int    Price              = PRICE_CLOSE;
extern int    FilterSize         = 23;
extern bool   arrowsVisible      = false;
extern int    arrowsUpCode       = 241;
extern int    arrowsDownCode     = 242;
extern int    arrowsSize         = 1;
extern double arrowsUpGap        = 1;
extern double arrowsDownGap      = 1;
extern string arrowsIdentifier   = "HendersonsArrows";
extern color  arrowsUpColor      = DeepSkyBlue;
extern color  arrowsDnColor      = Red;


//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double trend[];
double weights[];
int half;

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,buffer3);
      SetIndexBuffer(3,trend);
      
      FilterSize = MathMax(FilterSize,3);
            if (MathMod(FilterSize,2)==0) FilterSize++;
      half = MathFloor(FilterSize/2.0); 

      //
      //
      //
      //
      //

         indicatorFileName = WindowExpertName();
         timeFrame         = stringToTimeFrame(TimeFrame);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars) return(0);
         calculateValue    = (TimeFrame=="calculateValue");
         if (calculateValue || timeFrame==Period())
         {
               ArrayResize(weights,FilterSize);
               
               //
               //
               //
               //
               //
               
               double m = half;
               double divisor = 8.0*(m+2.0)*((m+2.0)*(m+2.0)-1.0)*(4.0*(m+2.0)*(m+2)-1.0)*(4.0*(m+2.0)*(m+2.0)-9.0)*(4.0*(m+2.0)*(m+2.0)-25.0);  
               for (int j=-half; j<=half; j++)
                  weights[j+half] = 315.0*((m+1.0)*(m+1.0)-(j*j))*((m+2.0)*(m+2.0)-(j*j))*((m+3.0)*(m+3.0)-(j*j))*(3.0*(m+2.0)*(m+2.0)-11.0*(j*j)-16.0)/divisor;
         }
         if (calculateValue) return(0);
       
      //
      //
      //
      //
      //
     
   IndicatorShortName(timeFrameToString(timeFrame)+" Henderson\'s filter : "+FilterSize);
   return(0);
}

//
//
//
//
//

int deinit()
{
   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);
   }
   return(0);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int limit,counted_bars=IndicatorCounted();
   int size = ArraySize(weights);

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(MathMax(Bars-counted_bars,FilterSize),Bars-1);
         if (returnBars) { buffer1[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(int i=limit; i>=0; i--)
      {
         double mod = 1.0;
         if (i<half)
         {
            double weightTotal = 0; for (int j=half-i; j<size; j++) weightTotal += weights[j]; if (weightTotal!=0) mod = 1.0/weightTotal;
         }
         
         //
         //
         //
         // 
         //
               
         double sum = mod*weights[half]*iMA(NULL,0,1,0,MODE_SMA,Price,i);
         for(j=1; j<=half; j++)
         {
            if (j<=i) sum += mod*weights[half+j]*iMA(NULL,0,1,0,MODE_SMA,Price,i-j);
                      sum += mod*weights[half-j]*iMA(NULL,0,1,0,MODE_SMA,Price,i+j);
         }
         buffer3[i] = sum;
         buffer1[i] = EMPTY_VALUE;
         buffer2[i] = EMPTY_VALUE;
            if (buffer3[i]>buffer3[i+1]) trend[i] =  1;
            if (buffer3[i]<buffer3[i+1]) trend[i] = -1;
            if (trend[i] ==  1) buffer1[i] = 1;
            if (trend[i] == -1) buffer2[i] = 1;
            manageArrow(i);
      }            
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         buffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Price,FilterSize,0,y);
         buffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Price,FilterSize,1,y);
         trend[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Price,FilterSize,3,y);
         
         manageArrow(i);
   }
   
   //
   //
   //
   //
   //
   
   return(0);   
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageArrow(int i)
{
   if (!calculateValue && arrowsVisible )
   {
         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,arrowsDownCode,true);
         }
   }
}               

//
//
//
//
//

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);
         ObjectSet(name,OBJPROP_WIDTH,arrowsSize);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i]+gap*arrowsUpGap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i] -gap*arrowsDownGap);
}

//
//
//
//
//

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);
}