OPEN-SOURCE SCRIPT
Cập nhật

Regime Classifier [RC Tools]

193
RC Tools — Regime Classifier
─────────────────────────────────────────────────────────────

█ OVERVIEW

Most indicators assume a single market condition and quietly fail in another. This tool doesn't generate signals — it tells you which of four market regimes you are currently in, so you can judge whether your existing tools are operating in conditions that suit them. It is a context tool, not a decision tool.

█ WHAT IT DOES

Classifies each confirmed bar into one of four states and colours the chart background accordingly:

• Trending — Expansion: directional, volatility rising
• Trending — Exhaustion: directional, volatility compressing
• Ranging — Quiet: no direction, low volatility
• Ranging — Volatile: no direction, high volatility (chop)

A table (top-right by default, repositionable) shows the current regime, how long price has been in it, and historical base rates — the average forward return and win rate seen after each regime, going back over the chart's full history.

█ THE THEORY BEHIND IT

Market behaviour is not stationary. A trend-following tool that performs well in directional expansion will bleed in volatile chop; a mean-reversion tool does the reverse. Rather than attempting to fix any single indicator, this tool identifies which environment you are in, using two independent dimensions — directionality and volatility state — that measure genuinely different properties of price behaviour rather than two correlated views of the same one.

█ HOW IT IS CALCULATED

DIRECTIONALITY — Efficiency Ratio over N bars:

ER = |close[0] − close[N]| ÷ Σ|close − close[i+1]|

Bounded 0–1. A value near 1 means price travelled almost directly from A to B (trending); near 0 means it wandered (ranging). No fitted parameters beyond the lookback. The Efficiency Ratio was introduced by Perry Kaufman as the core input to his Adaptive Moving Average (KAMA); it is used here purely as a directionality measure, independent of any moving average.

VOLATILITY STATE — realised volatility, percentile-ranked:

RV = stdev(log(close/close[1]), N)

RV is then ranked as a percentile against its own trailing distribution (default: 750 bars, ≈3 years on daily). An absolute volatility threshold is meaningless across assets — percentile ranking makes the classification behave identically on BTC, gold and equities with no parameter tuning.

The two dimensions are crossed to yield the four states. Classification occurs ONLY on confirmed bar close — the background never updates mid-bar and then flips back.

The base-rate table works by recording, for every historical bar, the forward N-bar return and whether it was positive, attributed back to whichever regime was active N bars earlier. Only fully-elapsed, already-known returns are used — nothing is looked up ahead of the current bar.

█ SETTINGS & CONFIGURATION

• Efficiency Ratio Lookback (default 20) — shorter = more responsive, noisier
• Realised Volatility Lookback (default 20)
• Percentile Ranking Window (default 750 bars ≈ 3 years daily) — longer = more stable, needs more history
• Directionality Threshold (default 0.35) — the ER above which price is considered trending
• Volatility Percentile Threshold (default 50) — the split between low and high volatility states
• Forward Return Window (default 20 bars) — the horizon used for the base-rate table
• Table position and background colours are fully configurable; the main-chart background painting can be toggled off if you only want the diagnostic pane

█ HOW TO USE IT

Use it as a filter on your existing process, not as an entry trigger. Example: if you run a breakout system, check whether it has historically performed in Ranging — Volatile; if not, consider standing aside when the background flags that state. Example: a mean-reversion system will typically show its worst results in Trending — Expansion.

Works on any asset and timeframe with sufficient history for the percentile window. Best used on daily and above, where regime persistence is greatest.

█ LIMITATIONS

This tool classifies the PRESENT. It does not predict the future, and any use of it as a forecast is a misuse.

• Regime identification is backward-looking by construction. The tool will confirm a regime change several bars AFTER it occurred. This lag cannot be removed without curve-fitting or repainting, and has not been.
• Classification is unstable near threshold boundaries; expect flickering between states when ER or volatility percentile sit close to the cut-offs.
• The percentile ranking requires substantial history. On assets with short histories, the ranking is unreliable and the tool should not be trusted.
• The base-rate table's early entries are built on fewer samples than its later ones — treat statistics as provisional until a state has accumulated a meaningful sample count.
• Four states is a deliberate simplification of a continuous reality. Markets do not actually occupy discrete regimes.
• This script does NOT repaint. All classification is computed on confirmed bar close only.

█ DISCLAIMER

For educational and informational purposes only. Nothing here is financial advice. Past behaviour of any market regime does not indicate future results. Trade at your own risk.
Phát hành các Ghi chú
Minor code cleaning adjustments.
Phát hành các Ghi chú
Updated image.
Phát hành các Ghi chú
Updated tags

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.