OPEN-SOURCE SCRIPT
Regime Map

Regime Map — Volatility State Detector
This indicator is a PineScript friendly approximation of a more advanced Python regime-analysis engine.
The original backed identifies market regimes using structural break detection, Hidden-Markov Models, wavelet decomposition, and long-horizon volatility clustering. Since Pine Script cannot execute these statistical models directly, this version implements a lightweight, real-time proxy using realised volatility and statistical thresholds.
The purpose is to provide a clear visual map of evolving volatility conditions without requiring any heavy offline computation.
________________________________________
Mathematical Basis: Python vs Pine
1. Volatility Estimation
Python (Realised Volatility):
RVₜ = √N × stdev( log(Pₜ) − log(Pₜ₋₁) )
Pine Approximation:
RVₜ = stdev( log(Pₜ) − log(Pₜ₋₁), lookback )
Rationale:
Realised volatility captures volatility clustering — a key characteristic of regime transitions.
________________________________________
2. Regime Classification
Python (HMM Volatility States):
Volatility is modelled as belonging to hidden states with different means and variances:
State μ₁, σ₁
State μ₂, σ₂
State μ₃, σ₃
with state transitions determined by a probability matrix.
Pine Approximation (Z-Score Regimes):
Zₜ = ( RVₜ − mean(RV) ) / stdev(RV)
Regime assignment:
• Regime 0 (Low Vol): Zₜ < Zₗₒw
• Regime 1 (Normal): Zₗₒw ≤ Zₜ ≤ Zₕᵢgh
• Regime 2 (High Vol): Zₜ > Zₕᵢgh
Rationale:
Z-scores provide clean statistical boundaries that behave similarly to HMM state separation but are computable in real time.
________________________________________
3. Structural Break Detection vs Rolling Windows
Python (Bai–Perron Structural Breaks):
Segments the volatility series into periods with distinct statistical properties by minimising squared error over multiple regimes.
Pine Approximation:
Rolling mean and rolling standard deviation of volatility over a long window.
Rationale:
When structural breaks are not available, long-window smoothing approximates slow regime changes effectively.
________________________________________
4. Multi-Scale Cycles
Python (Wavelet Decomposition):
Volatility decomposed into long-cycle (A₄) and short-cycle components (D bands).
Pine Approximation:
Single-scale smoothing using long-horizon averages of RV.
Rationale:
Wavelets reveal multi-frequency behaviour; Pine captures the dominant low-frequency component.
________________________________________
Indicator Output
The background colour reflects the active volatility regime:
• Low Volatility (Green): trending behaviour, cleaner directional movement
• Normal Volatility (Yellow): balanced environment
• High Volatility (Red): sharp swings, traps, mean-reversion phases
Regime labels appear on the chart, with a status panel displaying the current regime.
________________________________________
Operational Logic
1. Compute log returns
2. Calculate short-horizon realised volatility
3. Compute long-horizon mean and standard deviation
4. Derive volatility Z-score
5. Assign regime classification
6. Update background colour and labels
This provides a stable, real-time map of market state transitions.
________________________________________
Practical Applications
Intraday Trading
• Low-volatility regimes favour trend and breakout continuation
• High-volatility regimes favour mean reversion and wide stop placement
Swing Trading
• Compression phases often precede multi-day trending moves
• Volatility expansions accompany distribution or panic events
Risk Management
• Enables volatility-adjusted position sizing
• Helps avoid leverage during expansion regimes
________________________________________
Notes
• Does not repaint
• Fully configurable thresholds and lookbacks
• Works across indices, stocks, FX, crypto
• Designed for real-time volatility regime identification
________________________________________
Disclaimer
This script is intended solely for educational and research purposes.
It does not constitute financial advice or a recommendation to buy or sell any instrument.
Trading involves risk, and past volatility patterns do not guarantee future outcomes.
Users are responsible for their own trading decisions, and the author assumes no liability for financial loss.
This indicator is a PineScript friendly approximation of a more advanced Python regime-analysis engine.
The original backed identifies market regimes using structural break detection, Hidden-Markov Models, wavelet decomposition, and long-horizon volatility clustering. Since Pine Script cannot execute these statistical models directly, this version implements a lightweight, real-time proxy using realised volatility and statistical thresholds.
The purpose is to provide a clear visual map of evolving volatility conditions without requiring any heavy offline computation.
________________________________________
Mathematical Basis: Python vs Pine
1. Volatility Estimation
Python (Realised Volatility):
RVₜ = √N × stdev( log(Pₜ) − log(Pₜ₋₁) )
Pine Approximation:
RVₜ = stdev( log(Pₜ) − log(Pₜ₋₁), lookback )
Rationale:
Realised volatility captures volatility clustering — a key characteristic of regime transitions.
________________________________________
2. Regime Classification
Python (HMM Volatility States):
Volatility is modelled as belonging to hidden states with different means and variances:
State μ₁, σ₁
State μ₂, σ₂
State μ₃, σ₃
with state transitions determined by a probability matrix.
Pine Approximation (Z-Score Regimes):
Zₜ = ( RVₜ − mean(RV) ) / stdev(RV)
Regime assignment:
• Regime 0 (Low Vol): Zₜ < Zₗₒw
• Regime 1 (Normal): Zₗₒw ≤ Zₜ ≤ Zₕᵢgh
• Regime 2 (High Vol): Zₜ > Zₕᵢgh
Rationale:
Z-scores provide clean statistical boundaries that behave similarly to HMM state separation but are computable in real time.
________________________________________
3. Structural Break Detection vs Rolling Windows
Python (Bai–Perron Structural Breaks):
Segments the volatility series into periods with distinct statistical properties by minimising squared error over multiple regimes.
Pine Approximation:
Rolling mean and rolling standard deviation of volatility over a long window.
Rationale:
When structural breaks are not available, long-window smoothing approximates slow regime changes effectively.
________________________________________
4. Multi-Scale Cycles
Python (Wavelet Decomposition):
Volatility decomposed into long-cycle (A₄) and short-cycle components (D bands).
Pine Approximation:
Single-scale smoothing using long-horizon averages of RV.
Rationale:
Wavelets reveal multi-frequency behaviour; Pine captures the dominant low-frequency component.
________________________________________
Indicator Output
The background colour reflects the active volatility regime:
• Low Volatility (Green): trending behaviour, cleaner directional movement
• Normal Volatility (Yellow): balanced environment
• High Volatility (Red): sharp swings, traps, mean-reversion phases
Regime labels appear on the chart, with a status panel displaying the current regime.
________________________________________
Operational Logic
1. Compute log returns
2. Calculate short-horizon realised volatility
3. Compute long-horizon mean and standard deviation
4. Derive volatility Z-score
5. Assign regime classification
6. Update background colour and labels
This provides a stable, real-time map of market state transitions.
________________________________________
Practical Applications
Intraday Trading
• Low-volatility regimes favour trend and breakout continuation
• High-volatility regimes favour mean reversion and wide stop placement
Swing Trading
• Compression phases often precede multi-day trending moves
• Volatility expansions accompany distribution or panic events
Risk Management
• Enables volatility-adjusted position sizing
• Helps avoid leverage during expansion regimes
________________________________________
Notes
• Does not repaint
• Fully configurable thresholds and lookbacks
• Works across indices, stocks, FX, crypto
• Designed for real-time volatility regime identification
________________________________________
Disclaimer
This script is intended solely for educational and research purposes.
It does not constitute financial advice or a recommendation to buy or sell any instrument.
Trading involves risk, and past volatility patterns do not guarantee future outcomes.
Users are responsible for their own trading decisions, and the author assumes no liability for financial loss.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
AI-driven market structure analysis.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
AI-driven market structure analysis.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.