OPEN-SOURCE SCRIPT

Hodrick-Prescott Cycle Component (YavuzAkbay)

The Hodrick-Prescott Cycle Component indicator in Pine Script™ is an advanced tool that helps traders isolate and analyze the cyclical deviations in asset prices from their underlying trend. This script calculates the cycle component of the price series using the Hodrick-Prescott (HP) filter, allowing traders to observe and interpret the short-term price movements around the long-term trend. By providing two views—Percentage and Price Difference—this indicator gives flexibility in how these cyclical movements are visualized and interpreted.

What This Script Does
This indicator focuses exclusively on the cycle component of the price, which is the deviation of the current price from the long-term trend calculated by the HP filter. This deviation (or "cycle") is what traders analyze for mean-reversion opportunities and overbought/oversold conditions. The script allows users to see this deviation in two ways:
  1. Percentage Difference: Shows the deviation as a percentage of the trend, giving a normalized view of the price’s distance from its trend component.
  2. Price Difference: Shows the deviation in absolute price terms, reflecting how many price units the price is above or below the trend.


How It Works
  1. Trend Component Calculation with the HP Filter: Using the HP filter, the script isolates the trend component of the price. The smoothness of this trend is controlled by the smoothness parameter (λ), which can be adjusted by the user. A higher λ value results in a smoother trend, while a lower λ value makes it more responsive to short-term changes.
  2. Cycle Component Calculation: Percentage Deviation (cycle_pct) calculated as the difference between the current price and the trend, divided by the trend, and then multiplied by 100. This metric shows how far the price deviates from the trend in relative terms. Price Difference (cycle_price) simply the difference between the current price and the trend component, displaying the deviation in absolute price units.
  3. Conditional Plotting: The user can choose to view the cycle component as either a percentage or a price difference by selecting the Display Mode input. The indicator will plot the chosen mode in a separate pane, helping traders focus on the preferred measure of deviation.


How to Use This Indicator
  1. Identify Overbought/Oversold Conditions: When the cycle component deviates significantly from the zero line (shown with a dashed horizontal line), it may indicate overbought or oversold conditions. For instance, a high positive cycle component suggests the price may be overbought relative to the trend, while a large negative cycle suggests potential oversold conditions.
  2. Mean-Reversion Strategy: In mean-reverting markets, traders can use this indicator to spot potential reversal points. For example, if the cycle component shows an extreme deviation from zero, it could signal that the price is likely to revert to the trend. This can help traders with entry and exit points when the asset is expected to correct back toward its trend.
  3. Trend Strength and Cycle Analysis: By comparing the magnitude and duration of deviations, traders can gauge the strength of cycles and assess if a new trend might be forming. If the cycle component remains consistently positive or negative, it may indicate a persistent market bias, even as prices fluctuate around the trend.
  4. Percentage vs. Price Difference Views: Use the Percentage Difference mode to standardize deviations and compare across assets or different timeframes. This is especially helpful when analyzing assets with varying price levels. Use the Price Difference mode when an absolute deviation (price units) is more intuitive for spotting overbought/oversold levels based on the asset’s actual price.
  5. Using with Hodrick-Prescott: You can also use Hodrick-Prescott, another indicator that I have adapted to the Tradingview platform, to see the trend on the chart, and you can also use this indicator to see how far the price is deviating from the trend. This gives you a multifaceted perspective on your trades.


Practical Tips for Traders
  • Set the Smoothness Parameter (λ): Adjust the λ parameter to match your trading timeframe and asset characteristics. Lower values make the trend more sensitive, which might suit short-term trading, while higher values smooth out the trend for long-term analysis.
  • Cycle Component as Confirmation: Combine this indicator with other momentum or trend indicators for confirmation of overbought/oversold signals. For example, use the cycle component with RSI or MACD to validate the likelihood of mean-reversion.
  • Observe Divergences: Divergences between price movements and the cycle component can indicate potential reversals. If the price hits a new high, but the cycle component shows a smaller deviation than previous highs, it could signal a weakening trend.
cycleCyclesfilterhodrickprescotthodrickprescottfilterOscillatorstrendTrend AnalysisTrend Line Break

Mã nguồn mở

Theo tinh thần TradingView thực sự, tác giả của tập lệnh này đã xuất bản dưới dạng nguồn mở để các nhà giao dịch có thể hiểu và xác minh. Chúc mừng tác giả! Bạn có thể sử dụng miễn phí. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?


Ngoài ra, trên:

Thông báo miễn trừ trách nhiệm