//+------------------------------------------------------------------+ //| Hodrick Prescott Filter noLambda.mq4 | //| mladen | //| | //| original by Kurt Annen | //| | //| changed the original to calculate lambda | //| and not entering it directly as the original | //| HP filter works | //+------------------------------------------------------------------+ #property copyright "copyleft mladen" #property link "mladenfx@gmail.com" #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 clrLimeGreen #property indicator_color2 clrRed #property indicator_width1 2 #property indicator_width2 2 #property indicator_minimum 0 #property indicator_maximum 1 #property strict // // // // // input int FiltPer = 25; input int NumberOfBars = 0; input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; input int Shift = 0; double hp[],histoUp[],histoDn[],valc[],sourceValues[],calcValues[],a[],b[],c[],Lambda; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int OnInit() { IndicatorBuffers(4); SetIndexBuffer(0,histoUp); SetIndexShift(0,Shift); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexBuffer(1,histoDn); SetIndexShift(1,Shift); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexBuffer(2,hp); SetIndexShift(2,Shift); SetIndexBuffer(3,valc); ArrayResize(sourceValues,NumberOfBars); ArrayResize(calcValues ,NumberOfBars); ArrayResize(a ,NumberOfBars); ArrayResize(b ,NumberOfBars); ArrayResize(c ,NumberOfBars); Lambda=0.0625/pow(sin(M_PI/FiltPer),4); return(INIT_SUCCEEDED); } void OnDeinit(const int reason){ } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // int start() { static datetime barTime; int counted_bars=IndicatorCounted(); int i,n,limit; // // // // // if (NumberOfBars==0) n = Bars; else n = NumberOfBars; if (n > Bars) n = Bars; if (ArraySize(sourceValues) != n) { ArrayResize(sourceValues,n); ArrayResize(calcValues,n); ArrayResize(a,n); ArrayResize(b,n); ArrayResize(c,n); } // // // // // if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; limit = MathMin(Bars-counted_bars,n-1); SetIndexDrawBegin(0,Bars-n); if (barTime!=Time[0]) { barTime=Time[0]; limit=n-1; } // // // // // for(i=limit; i>=0; i--) sourceValues[i]=iMA(NULL,0,1,0,MODE_SMA,Price,i); hp_filter(sourceValues,n,Lambda,calcValues,true); ArrayCopy(hp,calcValues); for(i=n-1; i>=0; i--) { valc[i] = (ihp[i+1]) ? 1 : (hp[i]=0; i--) { output[i]=a[i]-b[i]*H1-c[i]*H2; H2=H1; H1=output[i]; } if (ret==false) for(int i=0; i