Concept
K-means Regime Clustering
K-means Regime Clustering is a Machine Learning concept. The Library holds 6 implementations, each one a working definition you can pull into Quant.
Top K-means Regime Clustering indicators
6 total
What is K-means Regime Clustering?
K-means is an unsupervised algorithm that partitions observations into k groups. It alternates two steps until assignments stop changing: assign each observation to its nearest centroid by Euclidean distance, then move each centroid to the mean of the observations assigned to it, a loop that drives within-cluster variance to a local minimum. Applied to markets, the observations are feature values collected over a lookback window, such as ATR readings, realized volatility, or trend-strength measures, and the resulting clusters are read as regimes: data-defined low, medium, and high states rather than fixed thresholds. The current bar's regime is simply the cluster whose centroid its features sit closest to.
This matters because most indicator settings are regime-dependent: a trailing multiplier that fits a quiet market is too tight in a volatile one, and clustering lets recent data decide what counts as quiet or volatile. The limits are classical. K must be chosen in advance; convergence is to a local optimum that depends on initialization; distance-based assignment assumes features are scaled comparably; and the clusters describe the present without saying anything about when the regime will switch. Labels also flip easily near cluster boundaries, so stable use usually adds confirmation or hysteresis.
How to calculate a k-means regime label
The loop itself is short; the design choices around it (features, scaling, k) decide whether the labels mean anything.
- 1Build the feature set: collect the values to be clustered over a lookback window and scale them so no single feature dominates the distance.
- 2Initialize k centroids, commonly by picking k observations or spreading starting points across the data's range.
- 3Iterate: assign every observation to its nearest centroid, then move each centroid to the mean of its members; stop when assignments no longer change.
- 4Label the current bar by its nearest centroid and read that centroid's values as the regime's characteristic level, for example the volatility that defines the high state.
How traders use it
- For volatility regimes: a window of ATR or realized-volatility values is clustered into a few groups and the current cluster gates behavior, a data-driven form of volatility regime classification.
- For adaptive settings: candidate parameter values are scored on recent performance, the scores are clustered, and the winning cluster's centroid is adopted, so something like a Supertrend factor is selected from the data, one flavor of adaptive parameterization.
- For level discovery: clustering prices themselves turns centroids into candidate support and resistance zones, the same idea used by dedicated level clustering algorithms.
- For strategy switching: bars labeled by cluster drive a trend-versus-range decision, enabling trend-following logic in one regime and mean-reversion logic in another.
K-means Regime Clustering vs other regime models
Gaussian Mixture Models: GMMs make soft, probabilistic assignments and allow clusters with different shapes and spreads; k-means makes hard nearest-centroid assignments and implicitly favors similar, roughly spherical clusters.
Hidden Markov / Markov-switching Regimes: Markov-switching models treat regimes as hidden states with transition probabilities, so sequence and persistence are modeled explicitly; k-means clusters each observation with no notion of time order.
Volatility Regime Classification: The broader task: regimes can also be classified with fixed thresholds or percentile cutoffs. K-means is one way of letting the data set those boundaries instead of choosing them by hand.
More K-means Regime Clustering implementations
Related concepts · Regimes & clustering
Concept family
Machine Learning
32 concepts mapped · 21 in the Library
K-means Regime Clustering FAQ
How do I choose the number of clusters k for market regimes?
There is no single correct k. Charting implementations usually fix a small k, often two to four, because the regimes must stay interpretable, such as low, medium, and high volatility. Heuristics like the elbow method on within-cluster variance can guide the choice, but stability matters more: if labels reshuffle every time the window advances, the chosen k is likely doing more harm than good.
Is k-means regime clustering predictive?
Not by itself. It is descriptive: it labels the current state from recent features and carries no model of how regimes evolve or when they switch. Any forward value rides on regimes persisting, which is not guaranteed. When transition timing is the point, sequence-aware approaches such as Markov-switching models address exactly what k-means ignores.
Why do k-means regime labels keep flipping?
Two mechanics cause it. Near a cluster boundary, a tiny feature change crosses the nearest-centroid line and the label switches; and as the rolling window advances, the centroids themselves drift, moving the boundary as well. Requiring several consecutive bars in the new regime, or a minimum distance from the boundary, trades a little delay for much steadier labels.
Build K-means Regime Clustering your way.
Quant writes, tests, and refines it with you — then it runs on LuxAlgo charting or ports to TradingView.


.png&w=3840&q=75)