KNN Machine Learning Mean Reversion Probability [Dots3Red]█ OVERVIEW
This script applies a K-Nearest Neighbors (KNN) machine learning algorithm to estimate the probability that price will revert to its moving average within a defined number of bars. Rather than predicting momentum direction, it asks a more specific question: how likely is it that this extension snaps back?
The model searches historical bars for situations that looked like the current one — same degree of stretch, same RSI exhaustion profile, same volume behavior — and measures how often those situations ended in a reversion to the basis MA. That proportion becomes the live probability shown on your chart.
█ METHODOLOGY
The indicator follows a supervised machine-learning pipeline with five distinct stages.
1 — Labeling (what we are predicting)
Each historical bar receives a label based on what actually happened next. If price was extended above the basis MA and touched it within the Reversion Window — that bar is labeled a successful reversion. If it did not touch — labeled as no reversion. The same logic applies from below. This is the core distinction from momentum KNN indicators: the target is reversion to fair value , not directional price movement.
2 — Feature engineering (what we measure)
Five features capture how stretched current price conditions are, each Z-score normalized to remove scale bias:
• MA Distance — signed % distance of close from the basis MA. The primary extension signal.
• Bollinger Band position — where price sits within the bands, normalizing extension relative to current volatility.
• RSI deviation — how far RSI has moved from neutral (50). Captures momentum exhaustion.
• Body compression — ratio of candle body to total range. Small bodies near extremes signal hesitation and loss of directional conviction.
• Volume fade — declining volume during an extension is a classic exhaustion signature.
3 — Z-score normalization
All five features are standardized using a rolling mean and standard deviation computed on prior bars only (look-ahead free). This ensures the KNN distance calculation is not biased by features of different scales.
4 — KNN engine
The algorithm scans the historical lookback window for the K most similar past bars, measured by Minkowski Distance across all five features simultaneously. Closer neighbors receive exponentially higher voting weight via a Gaussian Kernel , so the prediction is driven by the most relevant historical analogs — not a simple majority vote.
5 — Dual probability output
Two independent probabilities are maintained and tracked separately:
• P(reversion from above) — for overbought / extended-high setups.
• P(reversion from below) — for oversold / extended-low setups.
They are kept separate because bear-side extensions and bull-side extensions have statistically different behavior — bear moves are typically faster and sharper. A signal fires when the relevant probability crosses the user-defined threshold, and only when price is actually extended (see Extension Gate below).
█ WHAT MAKES THIS DIFFERENT
Most published KNN indicators predict momentum direction — will price go up or down next bar? This indicator predicts something more specific: will price return to its average?
The distinction matters for several reasons:
1 — A high momentum reading can persist for many bars. A stretched reading has a natural gravity pulling it back, and measuring the historical probability of that snap is a more tractable problem than direction forecasting.
2 — The two probability channels are trained on separate populations, accounting for the asymmetry between bull and bear extensions.
3 — The Extension Gate ensures signals only appear when there is actually something to revert from — no signals in flat, choppy, low-volatility conditions.
█ EXTENSION GATE
Even if the KNN model outputs a high reversion probability, no signal appears unless price is beyond Gate Multiplier × ATR from the basis MA. This prevents false signals in low-volatility or ranging conditions where mean reversion setups carry no statistical edge.
█ HOW TO USE
Signal shapes (▲ Rev / ▼ Rev)
Fire when P(reversion) crosses the threshold AND price passes the extension gate. The label at the signal bar shows the exact probability at the moment of firing.
Snap zone fill
When a signal is active, the region between current price and the basis MA is shaded. This is the reversion target zone — where price is statistically expected to return. The fill deactivates automatically once price reverts back through the basis.
Bar colors
• Bright green/red — active probability above the threshold on the current price side.
• Dimmed green/red — probability elevated but below threshold, approaching signal territory.
• No color — neutral or low reversion probability.
Background flash
A faint background confirms the exact bar on which a signal fired.
Recommended workflow
1 — Set the Basis MA to your preferred mean reversion average. EMA 20 is a common starting point for intraday and swing setups.
2 — Tune the Reversion Window to match your typical trade hold time in bars.
3 — Adjust the Extension Gate multiplier to the asset's volatility profile. Crypto typically requires higher values than forex or equities.
4 — Use the Probability Threshold to control signal frequency. 0.65 gives moderate frequency; 0.75 and above is more selective.
5 — Combine with volume analysis or candlestick confirmation at signal bars for additional confluence before entering a position.
█ SETTINGS REFERENCE
KNN Engine
• K Neighbors — how many historical analogs vote. Higher = smoother, slower to react.
• Lookback Window — size of the historical search space in bars.
• Reversion Window — bars within which price must touch the MA to count as a reversion.
• Minkowski p — distance metric exponent. 1 = Manhattan, 2 = Euclidean.
• Gaussian Bandwidth — controls how steeply neighbor weight falls with distance.
• Probability Threshold — minimum confidence required to show a signal.
Feature Settings
• Basis MA type / length — the fair value line all features are measured against.
• Bollinger Band mult — standard deviation multiplier for the BB position feature.
• RSI length — period for the RSI exhaustion feature.
• Volume MA length — baseline for the volume fade feature.
Extension Gate
• Require extension gate — toggle the ATR-based signal filter on/off.
• Gate band multiplier — how many ATRs from basis price must be before signaling.
• Gate ATR length — period for the ATR used in the gate calculation.
█ LIMITATIONS
• KNN is a lazy learner — it does not generalize beyond historical patterns in the lookback window. Strong trending regimes or structural breaks can produce elevated false signals.
• The reversion probability reflects historical frequency, not a guarantee of future behavior.
• On low-bar-count charts (e.g. weekly on newer assets), the lookback window may not contain enough samples to produce stable probability estimates.
• Computation scales with lookback window size. Very large windows may slow chart rendering.
█ DISCLAIMER
This indicator is a decision-support tool, not a trading system. It does not constitute financial advice. Always apply proper risk management and combine with your own analysis.
Algorithm: K-Nearest Neighbors (KNN)
Distance metric: Minkowski Distance
Preprocessing: Z-Score Normalization
Target: Probabilistic Mean Reversion
Indikator Pine Script®






















