Free Machine Learning Price Action TradingView

KNN Supertrend Horizon

k-Nearest Neighbours machine learning combined with a Supertrend indicator for dynamic, adaptive trend classification that responds to real market regime changes.

Published23 Mar 2026
CategoryMachine Learning
PlatformTradingView
ScriptPine Script v5
Views4,214
RepaintsNo

Overview

The KNN Supertrend Horizon is an advanced machine learning indicator that enhances the classic Supertrend algorithm with k-Nearest Neighbours (kNN) classification. Where the original Supertrend simply uses ATR-based bands to determine trend direction, this indicator uses kNN to analyse multiple features simultaneously including price velocity, volatility regime, volume profile, and momentum to determine whether a trend signal is likely to be reliable or not.

The result is a Supertrend line that adapts dynamically to changing market conditions, avoiding the whipsaw signals that commonly plague simpler ATR-based implementations in choppy, low-volatility environments.

How It Works

The indicator operates in three stages:

  1. Feature Extraction: For each bar, the algorithm extracts a feature vector consisting of normalised ATR, rate of change, RSI, volume deviation from average, and candle body ratio.
  2. kNN Classification: The current bar's feature vector is compared to the K most similar historical bars (default K=8). The directional outcome of those historical bars is used to vote on the current bar's likely direction.
  3. Supertrend Output: The standard Supertrend formula is then applied, but the multiplier is dynamically adjusted based on the kNN confidence score. High-confidence trending conditions tighten the bands; low-confidence choppy conditions widen them.

How to Use

Entry signals:

  • Go long when the indicator flips from red to green AND the kNN confidence level is above 60%.
  • Go short when the indicator flips from green to red AND the kNN confidence level is above 60%.
  • Avoid signals when confidence is below 50% these occur in choppy, directionless markets.

Stop placement: Place stops just beyond the Supertrend line. The line's dynamic width means stops are automatically wider in volatile conditions and tighter in trending conditions.

Best used on: 1H, 4H, and Daily timeframes. Works across Forex, Indices, and Crypto.

Settings

ParameterDefaultDescription
atr_period10ATR period for Supertrend band calculation
atr_multiplier3.0Base ATR multiplier (dynamically adjusted by kNN confidence)
k_neighbors8Number of nearest neighbours for kNN voting
lookback200Historical bars used to build the kNN training set
min_confidence0.50Minimum kNN confidence required to display a signal
show_confidencetrueDisplay confidence score as a subplot

Important Notes

  • This indicator does not repaint. All signals are confirmed on bar close.
  • The kNN model is recalibrated on every bar using the trailing lookback window. No offline training is required.
  • Performance is best on liquid instruments with reliable volume data (avoid thinly traded assets).
  • As with all indicators, this tool is for analysis only and does not constitute financial advice.