HP_Extrapolator

Download script
Author: Copyright � 2008, gpwr
HP_Extrapolator

This tool is designed to analyze historical market price data and project potential future movements based on mathematical trends. Rather than simply drawing a line that follows the most recent prices, it uses an advanced statistical technique known as the Hodrick-Prescott (HP) Filter to distinguish between long-term trends and short-term "noise."

Core Logic and Functionality

The script operates by processing a large volume of historical data (the "Past Bars") to understand the underlying momentum of an asset. It then applies that logic to project a specific number of future points (the "Fut Bars"). Here is how it handles that process:

1. Trend Filtering (The HP Filter) The primary engine of this script is the Hodrick-Prescott Filter. In financial terms, this means the tool looks at old data and tries to separate the "cycle" (the steady growth or decline) from the "irregularity" (temporary spikes, dips, or random fluctuations). By smoothing out these irregularities, the script can better identify where the price is actually heading in the long run.

2. Dynamic Parameter Adjustment (Lambda) The script uses a specific variable called "Lambda" to determine how much weight to give to the trend versus the noise. This value isn't static; it adjusts automatically based on the time horizon you are looking at, ensuring that the projection remains mathematically consistent whether you are looking at a short-term or medium-term window.

3. Prediction Methods The script offers two different ways to calculate these projections:

  • Method 1 (Independent Filters): This method calculates the projection by repeatedly applying the filter to different lengths of historical data. It essentially looks at multiple "views" of the past to build a cumulative estimate of where the price should go next.
  • Method 2 (Recursive Prediction): This is a more complex approach where the script uses its own previous predictions as part of the calculation for the next step. It creates a feedback loop, using both the actual historical data and its own mathematical "guesses" to refine the curve of the future projection.

Summary of User Inputs

  • Method: Choose between the two different calculation styles described above.
  • LastBar: Determines how far back from the current price the tool should start its primary observation.
  • PastBars: Defines the total amount of historical data the tool "studies" to learn the trend. A larger number provides a more established trend but may be slower to react to very recent changes.
  • FutBars: Sets how far into the future the red line will extend on your chart.

In essence, this script acts as a sophisticated "trend-following" forecaster that attempts to strip away market distractions to show a projected path based on deep historical patterns.

Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
HP_Extrapolator
//+--------------------------------------------------------------------------------------+
//|                                                                  HP Extrapolator.mq4 |
//|                                                               Copyright © 2008, gpwr |
//|                                                                   vlad1004@yahoo.com |
//+--------------------------------------------------------------------------------------+
#property copyright "Copyright © 2008, gpwr"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

//Global constants
#define pi 3.141592653589793238462643383279502884197169399375105820974944592

//Input parameters
extern int Method   =1;    //Prediction method
//Method 1: use only existing past values and different length filters
//Method 2: use both existing and prediced values with the filter length of 2*FastBars+1
extern int LastBar  =150;  //Last bar in the past data
extern int PastBars =10000;//Number of past bars to fit HP filter
extern int FutBars  =100;  //Number of bars to predict 

//Indicator buffers
double fv[];

int init()
{
   IndicatorBuffers(1);
   SetIndexBuffer(0,fv);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexShift(0,FutBars-LastBar);
   IndicatorShortName("HP Extrapolator");
   return(0);
}
//+---------------------------------------------------------------------------------------+
int start()
{
   int n;
   double x[],y[];
   ArrayResize(x,PastBars);
   ArrayResize(y,PastBars);
   for(int i=0;i<PastBars;i++) x[i]=Close[i+LastBar];
   ArrayInitialize(fv,EMPTY_VALUE);
   fv[FutBars]=Close[LastBar];
   double sum=Close[LastBar];
   if(Method==2)
   {
      n=2*FutBars+1;
      HPF(PastBars,Lambda(n),x,y);
      for(i=1;i<=n-2;i++) sum+=Close[i+LastBar];
   }
   for(i=1;i<=FutBars;i++)
   {
      if(Method==1)
      {
         n=2*i+1;
         HPF(PastBars,Lambda(n),x,y);
         sum+=Close[i+LastBar];
         fv[FutBars-i]=n*y[0]-sum;
         sum=n*y[0];
      }
      else
      {
         fv[FutBars-i]=n*y[FutBars-i]-sum;
         sum+=fv[FutBars-i]-Close[n-1+LastBar-i];
      }
   }
   return(0);
}
// Hodrick-Prescott Filter----------------------------------------------------------------+
void HPF(int nobs, double lambda, double x[], double& y[])
{
   double a[],b[],c[],H1,H2,H3,H4,H5,HH1,HH2,HH3,HH4,HH5,HB,HC,Z;
   ArrayResize(a,nobs);
   ArrayResize(b,nobs);
   ArrayResize(c,nobs);

   a[0]=1.0+lambda;
   b[0]=-2.0*lambda;
   c[0]=lambda;
   for(int i=1;i<nobs-2;i++)
   {
      a[i]=6.0*lambda+1.0;
      b[i]=-4.0*lambda;
      c[i]=lambda;
   }
   a[1]=5.0*lambda+1;
   a[nobs-1]=1.0+lambda;
   a[nobs-2]=5.0*lambda+1.0;
   b[nobs-2]=-2.0*lambda;
   b[nobs-1]=0.0;
   c[nobs-2]=0.0;
   c[nobs-1]=0.0;
   
   //Forward
   for(i=0;i<nobs;i++)
   {
      Z=a[i]-H4*H1-HH5*HH2;
      HB=b[i];
      HH1=H1;
      H1=(HB-H4*H2)/Z;
      b[i]=H1;
      HC=c[i];
      HH2=H2;
      H2=HC/Z;
      c[i]=H2;
      a[i]=(x[i]-HH3*HH5-H3*H4)/Z;
      HH3=H3;
      H3=a[i];
      H4=HB-H5*HH1;
      HH5=H5;
      H5=HC;
   }
   
   //Backward 
   H2=0;
   H1=a[nobs-1];
   y[nobs-1]=H1;
   for(i=nobs-2;i>=0;i--)
   {
      y[i]=a[i]-b[i]*H1-c[i]*H2;
      H2=H1;
      H1=y[i];
   }
}
// Lambda for Hodrick-Prescott Filter------------------------------------------------+
double Lambda(int n)
{
   double w; // w=pi*frequency
   if(n<=37)
      switch(n)
      {
         case  2: w=pi/3.0;
         case  3: w=MathArctan(MathSqrt(0.6));
         case  4: w=2.153460564/n;
         case  5: w=1.923796031/n;
         case  6: w=1.915022415/n;
         case  7: w=1.909786299/n;
         case  8: w=1.906409362/n;
         case  9: w=1.904103844/n;
         case 10: w=1.902459533/n;
         case 11: w=1.901245508/n;
         case 12: w=1.900323600/n;
         case 13: w=1.899607018/n;
         case 14: w=1.899038987/n;
         case 15: w=1.898581041/n;
         case 16: w=1.898206498/n;
         case 17: w=1.897896254/n;
         case 18: w=1.897636390/n;
         case 19: w=1.897416484/n;
         case 20: w=1.897228842/n;
         case 21: w=1.897067382/n;
         case 22: w=1.896927473/n;
         case 23: w=1.896805427/n;
         case 24: w=1.896698359/n;
         case 25: w=1.896603866/n;
         case 26: w=1.896520032/n;
         case 27: w=1.896445477/n;
         case 28: w=1.896378692/n;
         case 29: w=1.896318725/n;
         case 30: w=1.896264646/n;
         case 31: w=1.896215693/n;
         case 32: w=1.896171301/n;
         case 33: w=1.896130841/n;
         case 34: w=1.896094060/n;
         case 35: w=1.896060192/n;
         case 36: w=1.896029169/n;
         case 37: w=1.896000584/n;
      }
   else w=1.896/n;
   return(0.0625/MathPow(MathSin(w),4));
}

Comments