KNN Machine Learning Mean Reversion Probability

May 19, 2026

Static chart image
Volume Based
Signals
Machine Learning
Moving Averages
Volatility

The KNN Machine Learning Mean Reversion Probability indicator utilizes a K-Nearest Neighbors (KNN) algorithm to calculate the statistical probability of price returning to its moving average within a specific timeframe. By analyzing historical price extensions and momentum exhaustion, the tool provides a probabilistic confidence score for mean reversion setups.

Usage

The Usage section describes how the script can be used to identify potential snap-back opportunities when price becomes overextended.

  • Signal Interpretation: When the calculated probability crosses the user-defined threshold and price passes the "Extension Gate," a signal (▲ Rev or ▼ Rev) is generated. These signals include a label showing the exact historical probability of a reversion occurring.
  • Snap Zone Fill: When a signal is active, the area between the current price and the basis MA is shaded. This serves as the target zone for the mean reversion trade. The shading disappears once price touches the basis MA.
  • Bar Coloring: The script dynamically colors bars based on probability strength. Bright colors indicate probabilities above the threshold, while dimmed colors suggest approaching signal territory.
  • Recommended Workflow: Users typically set the Basis MA to a preferred value (such as a 20-period EMA) and tune the Reversion Window to match their expected holding time. The Probability Threshold can be increased (e.g., to 0.75) for more selective, higher-confidence signals.

Details

The indicator follows a supervised machine-learning pipeline to generate its outputs:

  • Labeling: Historical bars are labeled as "successful" or "unsuccessful" based on whether price touched the basis MA within the defined Reversion Window.
  • Feature Engineering: Five normalized features are used to define the "state" of the market: Distance from MA, Bollinger Band position, RSI deviation, Candle Body compression, and Volume fade.
  • KNN Engine: The algorithm uses Minkowski Distance to find the $K$ most similar historical analogs. A Gaussian Kernel is applied so that closer neighbors have a higher weight in the final probability calculation.
  • Extension Gate: This logic ensures that signals only fire when price is at a minimum distance from the MA (defined by ATR). This prevents signals in low-volatility or sideways markets where mean reversion has less statistical edge.

Settings

KNN Engine

  • K Neighbors: The number of historical analogs used for voting.
  • Lookback Window: The number of historical bars the algorithm searches through.
  • Reversion Window: The maximum number of bars allowed for a price to touch the MA for it to be considered a successful reversion.
  • Minkowski p: The exponent for the distance metric (1 for Manhattan, 2 for Euclidean).
  • Gaussian Bandwidth: Determines how much weight is lost as historical neighbors become less similar.
  • Probability Threshold: The minimum probability required to trigger a signal.

Feature Settings

  • Basis MA Type/Length: Selects the moving average type (SMA, EMA, etc.) and period used as the mean.
  • Bollinger Band mult: The standard deviation multiplier for volatility-adjusted extension features.
  • RSI length: The period used for the RSI exhaustion feature.
  • Volume MA length: The period used to determine the volume baseline for the volume fade feature.

Extension Gate

  • Require extension gate: Enables or disables the volatility-based signal filter.
  • Gate band multiplier: How many ATRs price must be away from the MA to allow a signal.
  • Gate ATR length: The period used for the Average True Range calculation.

FAQ

How does this differ from standard momentum KNN indicators? While most KNN indicators try to predict the direction of the next candle, this tool specifically measures the likelihood of price returning to its average "fair value" after a period of overextension.

Why are there two separate probability readings? The script tracks probabilities for "reversion from above" and "reversion from below" separately because market behavior during bull-side and bear-side extensions is often statistically asymmetrical.

How can I access this indicator? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free access on the following platforms
tradingviewSymbolTradingView
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

ninjatraderNinjaTrader
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

metatrader4MetaTrader 4/5
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

thinkorswimThinkorswim
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.

Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.

As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.

Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.

Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.

This does not represent our full Disclaimer. Please read our full disclaimer.

© 2026 LuxAlgo Global, LLC.