OPEN-SOURCE SCRIPT

Adaptive Hurst Exponent Regime Filter

174
Adaptive Hurst Exponent Regime Filter (AHERF)

█ OVERVIEW

The Adaptive Hurst Exponent Regime Filter (AHERF) is designed to identify the prevailing market regime—be it Trending, Mean-Reverting, or a Random Walk/Transition phase. While the Hurst Exponent is a well-known tool for this purpose, AHERF introduces a key innovation: an adaptive threshold. Instead of relying solely on the traditional fixed 0.5 Hurst value, this indicator's threshold dynamically adjusts based on current market volatility, aiming to provide more nuanced and responsive regime classifications.

This tool can assist traders in:
  • Gauging the current character of the market.
  • Tailoring trading strategies to the identified regime (e.g., deploying trend-following systems in Trending markets or mean-reversion tactics in Mean-Reverting conditions).
  • Filtering out trades that may be counterproductive to the dominant market behavior.


█ HOW IT WORKS

The indicator operates through the following key calculations:

1. Hurst Exponent Calculation:
  • The script computes an approximate Hurst Exponent (H). It utilizes log price changes as its input series.
  • The `calculateHurst` function implements a variance scaling approach:
    It defines three sub-periods based on the main `Hurst Lookback Period`.
    It calculates the standard deviation of the input series over these sub-periods.
    The Hurst Exponent is then estimated from the slope of a log-log regression between the standard deviations and their respective sub-period lengths. A simplified calculation using the first and last sub-periods is performed: `H = (log(StdDev3) - log(StdDev1)) / (log(N3) - log(N1))`.
  • Theoretically, a Hurst Exponent:
    • H > 0.5 suggests persistence (trending behavior).
    • H < 0.5 suggests anti-persistence (mean-reverting behavior).
    • H ≈ 0.5 suggests a random walk (unpredictable movement).

Pine Script® Snippet (Hurst Calculation Call):
Pine Script®
float logPriceChange = math.log(close) - math.log(close[1]); // ... ensure logPriceChange is not na on first bar ... float hurstValue = calculateHurst(logPriceChange, hurstLookbackInput);


2. Volatility Proxy Calculation:
  • To enable the adaptive nature of the threshold, a volatility proxy is calculated.
  • Users can select the `Volatility Metric` to be either:
    Average True Range (ATR), normalized by the closing price.
    Standard Deviation (StdDev) of simple price returns.
  • This proxy quantifies the current degree of price activity or fluctuation in the market.

Pine Script® Snippet (Volatility Proxy Call):
Pine Script®
float volatilityProxy = getVolatilityProxy(volatilityMetricInput, volatilityLookbackInput);


3. Adaptive Threshold Calculation:
  • This is the core of AHERF's adaptability. Instead of a static 0.5 line as the sole determinant, the script computes a dynamic threshold.
  • The adaptive threshold is calculated as: `0.5 + (Threshold Sensitivity * Volatility Proxy)`.
  • This means the threshold starts at the baseline 0.5 level and then adjusts upwards or downwards based on the current `volatilityProxy` scaled by the `Threshold Sensitivity (k)` input.

Pine Script® Snippet (Adaptive Threshold Calculation):
Pine Script®
float adaptiveThreshold = 0.5 + sensitivityInput * nz(volatilityProxy, 0.0);


4. Regime Identification:
  • The prevailing market regime is determined by comparing the `hurstValue` to this `adaptiveThreshold`, incorporating a `Threshold Buffer` to reduce noise and clearly delineate zones:
    • Trending: `hurstValue > adaptiveThreshold + bufferInput`
    • Mean-Reverting: `hurstValue < adaptiveThreshold - bufferInput`
    • Random/Transition: Otherwise (Hurst value is within the buffer zone around the adaptive threshold).

Pine Script® Snippet (Regime Determination Logic):
Pine Script®
if not na(hurstValue) and not na(adaptiveThreshold) if hurstValue > adaptiveThreshold + bufferInput currentRegimeColor := TRENDING_COLOR regimeText := "Trending" else if hurstValue < adaptiveThreshold - bufferInput currentRegimeColor := MEAN_REVERTING_COLOR regimeText := "Mean-Reverting" // else remains Random/Transition


█ HOW TO USE IT

Interpreting the Visuals:
  • Observe the plotted `Hurst Exponent (H)` line (White) relative to the `Adaptive Threshold` line (Orange).
  • The background color provides an immediate indication of the current regime: Green for Trending, Red for Mean-Reverting, and Gray for Random/Transition.
  • The fixed `0.5 Level` (Dashed Gray) is plotted for reference against traditional Hurst interpretation.
  • Labels "T", "M", and "R" appear below bars to signal new entries into Trending, Mean-Reverting, or Random/Transition regimes, respectively.


Inputs Customization:
  • Hurst Exponent CalculationHurst Lookback Period: Defines the number of bars used for the Hurst Exponent calculation. Longer periods generally yield smoother Hurst values, reflecting longer-term market memory. Shorter periods are more responsive.
  • Adaptive Threshold Settings
    • Volatility Metric: Choose "ATR" or "StdDev" to drive the adaptive threshold. Experiment to see which best suits the asset.
    • Volatility Lookback: The lookback period for the selected volatility metric.
    • Threshold Sensitivity (k): A crucial multiplier determining how strongly volatility influences the adaptive threshold. Higher values mean volatility has a greater impact, potentially widening or shifting the regime bands more significantly.
    • Threshold Buffer: Creates a neutral zone around the adaptive threshold. This helps prevent overly frequent regime shifts due_to minor Hurst fluctuations.


█ ORIGINALITY AND USEFULNESS

The AHERF indicator distinguishes itself by:
  • Implementing an adaptive threshold mechanism for Hurst Exponent analysis. This threshold dynamically responds to changes in market volatility, offering a more flexible approach than a fixed 0.5 reference, potentially leading to more contextually relevant regime detection.
  • Providing clear, at-a-glance visualization of market regimes through background coloring and distinct plot shapes.
  • Offering user-configurable parameters for both the Hurst calculation and the adaptive threshold components, allowing for tuning across various assets and timeframes.

Traders can leverage AHERF to better align their chosen strategies with the prevailing market character, potentially enhancing trade filtering and decision-making processes.

█ VISUALIZATION

The indicator plots the following in a separate pane:
  • Hurst Exponent (H): A white line representing the calculated Hurst value.
  • Adaptive Threshold: An orange line representing the dynamic threshold.
  • Fixed 0.5 Level: A dashed gray horizontal line for traditional Hurst reference.
  • Background Color: Changes based on the identified regime:
    Green: Trending regime.
    Red: Mean-Reverting regime.
    Gray: Random/Transition regime.
  • Regime Entry Shapes: Plotted below the price bars (forced overlay for visibility):
    • "T" (Green Label): Signals entry into a Trending regime.
    • "M" (Teal Label): Signals entry into a Mean-Reverting regime.
    • "R" (Cyan Label): Signals entry into a Random/Transition regime.


█ ALERTS

The script provides alert conditions for changes in the market regime:
  • Regime Shift to Trending: Triggers when the Hurst Exponent crosses above the adaptive threshold into a Trending state.
  • Regime Shift to Mean-Reverting: Triggers when the Hurst Exponent crosses below the adaptive threshold into a Mean-Reverting state.
  • Regime Shift to Random/Transition: Triggers when the Hurst Exponent enters the Random/Transition zone around the adaptive threshold.

These can be configured directly from the TradingView alerts panel.

█ NOTES & DISCLAIMERS
  • The Hurst Exponent calculation is an approximation; various methods exist, each with its nuances.
  • The performance and relevance of the identified regimes can differ across financial instruments and timeframes. Parameter tuning is recommended.
  • This indicator is intended as a decision-support tool and should not be the sole basis for trading decisions. Always integrate its signals within a broader analytical framework.
  • Past performance of any trading system or indicator, including those derived from AHERF, is not indicative of future results.


█ CREDITS & LICENSE
  • Author: mastertop (Twitter: x.com/Top_Astray)
  • Color Palette: Uses the `MaterialPalette` library by MASTERTOP_ASTRAY.
  • This source code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
  • © mastertop, 2025

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.