OPEN-SOURCE SCRIPT
Market Regime Engine

Know the market you're in before you trade it — adaptive k-means volatility + a trend/range read that tells you whether to trend-follow, fade, or stand aside.
The same setup that prints money in a trend gets chopped to pieces in a range. Market Regime Engine reads the environment first — is price TRENDING or RANGING, and is volatility LOW, NORMAL or HIGH — then tells you which playbook fits: trend-follow, fade the range, or stand aside in the chop.
── TWO HONEST, ADAPTIVE READS ──
• Volatility (self-learning) — a genuine k-means clustering (k = 3) of ATR over a rolling window. The model continually re-learns the market's own low / normal / high volatility buckets instead of using fixed thresholds, so it adapts to each symbol and timeframe. No black box, no "AI" hand-waving — just clustering you can read in the code.
• Behaviour — the Kaufman Efficiency Ratio: how much of the recent range was actual directional progress versus back-and-forth noise. Above your threshold = TRENDING, below = RANGING.
Direction (▲/▼) comes from price versus a bias EMA, so a trend is labelled TRENDING ▲ or TRENDING ▼.
── WHAT YOU SEE ──
The background (and optionally the candles) colour by regime — green up-trend, red down-trend, blue range. A label marks each regime change. Everything is confirmed on close.
── READING THE DASHBOARD ──
• Regime — the current state: TRENDING ▲, TRENDING ▼ or RANGING.
• Confidence — 0–100 conviction. For a trend it rises the further the efficiency ratio is above your threshold; for a range it rises the further below. It measures how cleanly price fits the state, not a win rate.
• Volatility — Low / Normal / High, from the k-means cluster the current ATR falls into.
• Efficiency — the raw efficiency ratio as a percent, with your threshold beside it.
• Bias — whether price is above or below the bias EMA.
• Play — the suggested approach for this regime: "Trend-follow pullbacks", "Fade the range", or "Stand aside — chop" (ranging + high volatility).
── HOW TO USE IT ──
Use it as a filter on top of your own system:
• TRENDING → take trend-following entries (pullbacks, breakouts); avoid fading.
• RANGING + Low/Normal vol → mean-reversion at the range edges works best.
• RANGING + High vol → the danger zone; expect whipsaw, size down or stand aside.
It pairs naturally with structure/SMC or momentum tools — let the regime decide which of your setups to trust right now.
── NON-REPAINTING ──
The efficiency ratio, the volatility cluster and the regime are all evaluated on closed bars (barstate.isconfirmed), so a regime label does not repaint after the fact.
── SETTINGS ──
Trend/range window, trend threshold, bias EMA, and the volatility training window (how many bars the k-means model learns from) are all adjustable, along with background/candle colouring, the regime-change labels, dashboard position/size and every colour. The defaults (efficiency window 20, trend threshold 0.35, bias EMA 100, training window 150) were chosen from out-of-sample testing across the major coins on 1H–1D — the settings where a TRENDING call carried the strongest forward directional edge.
── A NOTE ON HONESTY ──
"k-means" here is a real, transparent clustering of volatility — deliberately simple and readable, not a predictive machine-learning oracle. It classifies the current environment; it does not forecast price.
Free and open-source (Mozilla Public License 2.0) — the full logic is on the Pine tab.
This tool is for research and education. It is not financial advice and does not guarantee profit. Test on your own markets and manage your own risk.
The same setup that prints money in a trend gets chopped to pieces in a range. Market Regime Engine reads the environment first — is price TRENDING or RANGING, and is volatility LOW, NORMAL or HIGH — then tells you which playbook fits: trend-follow, fade the range, or stand aside in the chop.
── TWO HONEST, ADAPTIVE READS ──
• Volatility (self-learning) — a genuine k-means clustering (k = 3) of ATR over a rolling window. The model continually re-learns the market's own low / normal / high volatility buckets instead of using fixed thresholds, so it adapts to each symbol and timeframe. No black box, no "AI" hand-waving — just clustering you can read in the code.
• Behaviour — the Kaufman Efficiency Ratio: how much of the recent range was actual directional progress versus back-and-forth noise. Above your threshold = TRENDING, below = RANGING.
Direction (▲/▼) comes from price versus a bias EMA, so a trend is labelled TRENDING ▲ or TRENDING ▼.
── WHAT YOU SEE ──
The background (and optionally the candles) colour by regime — green up-trend, red down-trend, blue range. A label marks each regime change. Everything is confirmed on close.
── READING THE DASHBOARD ──
• Regime — the current state: TRENDING ▲, TRENDING ▼ or RANGING.
• Confidence — 0–100 conviction. For a trend it rises the further the efficiency ratio is above your threshold; for a range it rises the further below. It measures how cleanly price fits the state, not a win rate.
• Volatility — Low / Normal / High, from the k-means cluster the current ATR falls into.
• Efficiency — the raw efficiency ratio as a percent, with your threshold beside it.
• Bias — whether price is above or below the bias EMA.
• Play — the suggested approach for this regime: "Trend-follow pullbacks", "Fade the range", or "Stand aside — chop" (ranging + high volatility).
── HOW TO USE IT ──
Use it as a filter on top of your own system:
• TRENDING → take trend-following entries (pullbacks, breakouts); avoid fading.
• RANGING + Low/Normal vol → mean-reversion at the range edges works best.
• RANGING + High vol → the danger zone; expect whipsaw, size down or stand aside.
It pairs naturally with structure/SMC or momentum tools — let the regime decide which of your setups to trust right now.
── NON-REPAINTING ──
The efficiency ratio, the volatility cluster and the regime are all evaluated on closed bars (barstate.isconfirmed), so a regime label does not repaint after the fact.
── SETTINGS ──
Trend/range window, trend threshold, bias EMA, and the volatility training window (how many bars the k-means model learns from) are all adjustable, along with background/candle colouring, the regime-change labels, dashboard position/size and every colour. The defaults (efficiency window 20, trend threshold 0.35, bias EMA 100, training window 150) were chosen from out-of-sample testing across the major coins on 1H–1D — the settings where a TRENDING call carried the strongest forward directional edge.
── A NOTE ON HONESTY ──
"k-means" here is a real, transparent clustering of volatility — deliberately simple and readable, not a predictive machine-learning oracle. It classifies the current environment; it does not forecast price.
Free and open-source (Mozilla Public License 2.0) — the full logic is on the Pine tab.
This tool is for research and education. It is not financial advice and does not guarantee profit. Test on your own markets and manage your own risk.
Open-source Skript
Ganz im Sinne von TradingView hat dieser Autor sein/ihr Script als Open-Source veröffentlicht. Auf diese Weise können nun auch andere Trader das Script rezensieren und die Funktionalität überprüfen. Vielen Dank an den Autor! Sie können das Script kostenlos verwenden, aber eine Wiederveröffentlichung des Codes unterliegt unseren Hausregeln.
Haftungsausschluss
Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.
Open-source Skript
Ganz im Sinne von TradingView hat dieser Autor sein/ihr Script als Open-Source veröffentlicht. Auf diese Weise können nun auch andere Trader das Script rezensieren und die Funktionalität überprüfen. Vielen Dank an den Autor! Sie können das Script kostenlos verwenden, aber eine Wiederveröffentlichung des Codes unterliegt unseren Hausregeln.
Haftungsausschluss
Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.