Hodrick-Prescott Filter
The Hodrick-Prescott filter is standard tooling in macroeconomics for splitting a series into a smooth trend and a residual cycle, but it is almost never seen correctly implemented in MetaTrader. The reason is that the textbook filter is a single optimisation solved once across an entire series, using every point, including ones after the point being smoothed - a global fit that is excellent for one static chart of finished history and completely wrong for a live indicator, because every historical point would shift every time a new bar closes. This implementation solves that problem by never doing a global fit at all. Every bar's trend value comes from an HP filter solved independently over only the InpWindowPeriod bars trailing it, using the standard pentadiagonal normal equations that fall out of penalising the second difference of the fitted trend. Once a bar closes, its own trailing window is fixed forever, so its trend value never moves again - the honest trade-off is that this windowed trend will differ slightly from what a textbook full-sample HP filter would have produced with the benefit of hindsight. The linear system itself is solved by successive over-relaxation rather than a hand-derived banded elimination. The HP normal-equation matrix is symmetric positive definite by construction, which guarantees SOR converges from any starting point, and a fixed iteration cap keeps the cost of every bar bounded and predictable regardless of how demanding InpLambda is. Notes and limitations - This is a windowed approximation of the HP filter, not the classic full-sample solution - it will not exactly reproduce values from a statistics package run once over the same finished history, by design, in exchange for never repainting. - SOR is an iterative solver: with too few InpIterations relative to a very large InpLambda, the trend can be slightly under-converged. Raise InpIterations if the line looks jagged rather than smooth for the chosen lambda. - The exposed cycle buffer is close minus trend for the same window; it inherits the same windowing trade-off as the trend line itself. - This does not size a position, place a stop, or generate trade signals on its own.
Indicator · MT5 · v1.0 · Free
About Hodrick-Prescott Filter
Hodrick-Prescott Filter is an indicator for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Window period, Lambda and Iterations. MQL source code included.
Files in the download: HodrickPrescottFilter.ex5, HodrickPrescottFilter.mq5, HodrickPrescottFilter.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Window period | 40 | Bars in the trailing window solved on every bar |
| Lambda | 1600.0 | Smoothing penalty: higher = straighter trend, lower = hugs price |
| Iterations | 60 | SOR sweeps per bar; more = closer to the exact solution |
| Relaxation | 1.5 | SOR relaxation factor, must stay between 0 and 2 |
| Alerts | true | Alert once per closed bar when price crosses the trend line |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Indicators.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the indicator from the Navigator onto a chart and adjust the settings.
Tags: Trend · Trailing Stop & Break-even · Alerts · MT5 Indicator · MQL5 Source Code