Author: gpwr
Hey traders! Today, we’re diving into the Hodrick-Prescott (HP) filter—a tool that can really sharpen your trading edge. The standout feature of this filter is that it doesn’t lag behind the market; it’s calculated by minimizing an objective function. Let’s break it down:
Objective Function:
F = Sum((y[i] - x[i])^2,i=0..n-1) + lambda*Sum((y[i+1]+y[i-1]-2*y[i])^2,i=1..n-2
Where:
- x[] - prices
- y[] - filter values
Check out the chart below to see how the filter performs (you can find the HP.mq4 file attached):

So, if the HP filter can look ahead, what future values does it suggest? To find out, we need to create a low-frequency digital filter similar to the HP filter but based on past values from its own “twin filter.” Here’s how it breaks down:
Low-Frequency Filter Equations:
y[i] = Sum(a[k]*x[i-k],k=0..nx-1) - FIR filter
or
y[i] = Sum(a[k]*x[i-k],k=0..nx-1) + Sum(b[k]*y[i-k],k=1..ny) - IIR filter
For the best results, choose a “twin filter” with a constant group delay (Tdel). IIR filters don’t fit the bill here. If you’re using FIR filters, the condition for frequency-independent delay is:
a[i] = +/-a[nx-1-i], i = 0..nx-1
The simplest FIR filter that provides constant delay is the Simple Moving Average (SMA):
y[i] = Sum(x[i-k],k=0..nx-1)/nx
When nx is odd, Tdel = (nx-1)/2. By shifting the SMA filter values back by Tdel, you can align the SMA with the HP filter. However, keep in mind that the frequency parameters of these filters can differ significantly (check out the chart below):

For the closest match between the filter values, make sure their channel widths are similar (for instance, -6dB). The HP filter’s channel width at -6dB is calculated like this:
wc = 2*arcsin(0.5/lambda^0.25).
For the SMA filter, you can find the -6dB channel width through numerical computation:
|H(w)| = sin(nx*wc/2)/sin(wc/2)/nx = 0.5
Take a look at the comparison of the two filters with similar channel widths below: the red line represents the HP filter (FiltPer = 25), and the blue line is the SMA (Period = 15, Shift = -7). Notice how the SMA doesn’t have data for the last 7 bars because it needs future prices to compute. Meanwhile, the HP filter (red) provides values. If the shifted SMA mirrors the HP filter on those last 7 bars after future prices come in, what could those values be?

Prediction Algorithms:
This indicator comes packed with two methods for making predictions:
Method 1:
- Set the SMA length to 3 and shift it back by 1 bar. This length means the shifted SMA only lacks data for the last bar (Bar = 0) since it needs the next future price Close[-1].
- Calculate the SMA filter’s channel width and set it equal to that of the HP filter. Find lambda.
- Determine the HP filter value at the last bar HP[0] and assume that SMA[0] (with unknown Close[-1]) gives the same value.
- Find Close[-1] = 3*HP[0] - Close[0] - Close[1]
- Increase the SMA length to 5. Repeat the calculations to find Close[-2] = 5*HP[0] - Close[-1] - Close[0] - Close[1] - Close[2]. Keep going until you’ve calculated the desired number of future prices (FutBars).
Method 2:
- Set the SMA length to 2*FutBars+1 and shift it back by FutBars.
- Calculate the SMA filter’s channel width and set it equal to that of the HP filter. Find lambda.
- Calculate the HP filter values for the last FutBars and assume the SMA behaves similarly when new prices come in.
- Find Close[-1] = (2*FutBars+1)*HP[FutBars-1] - Sum(Close[i],i=0..2*FutBars-1), Close[-2] = (2*FutBars+1)*HP[FutBars-2] - Sum(Close[i],i=-1..2*FutBars-2), and so on.
The indicator features the following inputs:
- Method - choose your prediction method
- LastBar - the number of the last bar to check predictions on existing prices (LastBar >= 0)
- PastBars - number of previous bars for the HP filter calculations (the more, the better, ideally PastBars > 2*FutBars)
- FutBars - number of predicted future values
The indicator highlights predicted values in red. Check out the example of Method 1 below:

And here’s Method 2:

While Method 2 tends to be more accurate, keep in mind that it can sometimes produce large spikes in the predicted first price. You can enhance this prediction method by exploring FIR filters with frequency parameters closer to those of the HP filter. Consider experimenting with Hanning, Blackman, Kaiser, and other filters with constant delay instead of just the SMA.
Lastly, a big shoutout to user Korey for the original Hodrick-Prescott filter indicator shared in this forum thread.
Related Posts
- Unlocking MetaCOT 2: Your Ultimate CFTC Indicator Toolkit for MT4
- Unlocking the Power of Master Tools for MetaTrader 4
- Mastering the Open Range Breakout Indicator for MetaTrader 5
- Visualize Current Trends Across All Time Frames with This MetaTrader 4 Indicator
- Hourly Buffers for Data Collection in MetaTrader 5: A Simple Guide