OPEN-SOURCE SCRIPT

Hodrick-Prescott Filter (YavuzAkbay)

The Hodrick-Prescott Filter indicator in Pine Script™ brings an established method from economics into trading by applying the Hodrick-Prescott (HP) filter for trend-cyclical decomposition. This filter is commonly used in economics to separate the trend and cyclical components of time series data. Here, it’s adapted into Pine Script to help traders differentiate long-term trends from short-term price fluctuations, making it easier to interpret market movements.

What This Script Does
Unlike moving averages, which simply smooth the data to approximate a trend, the HP Filter is designed to break down the price into two components: the trend (long-term) and the cycle (short-term fluctuations). While no smoothing method is lag-free, the HP Filter can often react faster to shifts in the trend compared to long moving averages, particularly with an optimized λ value. Moving averages, especially longer ones, tend to lag more as they rely directly on past prices, whereas the HP Filter’s recursive calculation adjusts the trend to minimize this delay.

How It Works
The Hodrick-Prescott filter uses a smoothness parameter (λ) to adjust the degree of smoothness applied to the trend component. The higher the value of λ, the smoother the trend component, which means it will respond less to short-term fluctuations in price. This can be set by the user with the "Smoothness Parameter (λ)" input.

How to Use This Indicator
  1. Trend Identification: The line shows the smoothed trend line, which can help in determining the general direction of the price. If the trend line is pointing upwards, it suggests a bullish trend, and if it's pointing downwards, it indicates a bearish trend.
  2. Cycle Component for Overbought/Oversold Signals: The Hodrick-Prescott Cycle Component indicator can be useful to spot potential reversals or short-term corrections. Large deviations from the zero line in the cycle component may indicate overbought (when cycle is significantly positive) or oversold (when cycle is significantly negative) conditions.
  3. Adjusting λ for Different Market Conditions: Users can adjust the λ parameter based on the type of asset and the desired sensitivity. Lower values of λ make the trend component more responsive to price changes, which is suitable for high-volatility assets or for traders focusing on shorter-term trends. Higher values smooth the trend more, which can be beneficial for long-term trend-following in stable markets or when analyzing weekly/monthly timeframes.


Practical Tips for Traders
  • Trend Following: Use the trend component to follow the direction of the market. If the trend component is steadily increasing, you may want to look for long opportunities, and vice versa for short opportunities.
  • Divergence Detection: If the cycle component shows a divergence from price (e.g., price makes a new high, but the cycle component does not), this can be an early warning of a potential reversal.
  • Sensitivity Testing: Experiment with different λ values to find a balance between smoothness and responsiveness that suits the asset and timeframe you’re analyzing.


Mathematical Background of the HP Filter
The Hodrick-Prescott filter separates a time series 𝑦𝑡 (in this case, price data) into a trend component 𝜏𝑡 and a cyclical component 𝑐𝑡 using this equation: yt​=τt​+ct​.
The goal of the HP filter is to minimize the following objective function:
t=1∑T​(y_t​−τ_t​)^2+λt=2∑T−1​((τ_(t+1)​−τ_t​)−(τ_t​−τ_(t−1​)))^2

Pine Script Implementation of the HP Filter
In my Pine Script implementation, the HP filter is approximated using a recursive formula for efficiency:
τ_t​=(y_t​+(λ−1)*τ_(t−1))/​​λ
averagehodrickprescotthodrickprescottfiltermoving_averageTrend Analysistrendlineanalysistrendtradetrendtrading

오픈 소스 스크립트

진정한 TradingView 정신에 따라, 이 스크립트의 저자는 트레이더들이 이해하고 검증할 수 있도록 오픈 소스로 공개했습니다. 저자에게 박수를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰에 의해 관리됩니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

차트에 이 스크립트를 사용하시겠습니까?


또한 다음에서도:

면책사항