ML KNN Supertrend [Quantum Edge]ML KNN Supertrend is a machine-learning-enhanced trend-following system built by Quantum Edge Capital LLC that fuses a K-Nearest Neighbors (KNN) classifier with a classic SuperTrend band to produce a confidence-weighted, noise-filtered trend signal. Instead of following raw price crossovers, the trend direction is validated by a real-time ML probability score derived from hundreds of historical analogs — only confirming a trend when the statistical weight of similar past conditions agrees with the structural signal.
The result: fewer false flips, gradient candle coloring that reflects conviction strength, and 3D rejection orbs that mark institutional wick-rejection events with volume context.
⚙️ How It Works
K-Nearest Neighbors (KNN) Engine
On every bar, the indicator extracts two normalized features from current price conditions:
F1 — RSI(14): Momentum state of the smoothed price source
F2 — ATR/Price %: Normalized volatility relative to price level
It then searches the last Search Window bars (default: 500) for the K most similar historical bars by Euclidean distance in this two-dimensional feature space:
d
=
(
F
1
c
u
r
r
e
n
t
−
F
1
h
i
s
t
o
r
i
c
a
l
)
2
+
(
F
2
c
u
r
r
e
n
t
−
F
2
h
i
s
t
o
r
i
c
a
l
)
2
d=
(F1
current
−F1
historical
)
2
+(F2
current
−F2
historical
)
2
Among the K nearest neighbors, the algorithm counts how many had a bullish SuperTrend vs. bearish SuperTrend on the following bar. The ratio produces an ML Probability score (0–100%):
Score above 50 + ML Buffer → ML confirms bullish
Score below 50 - ML Buffer → ML confirms bearish
Score within the buffer zone → prior bias is maintained (no flip)
The ML probability is smoothed with an EMA (Liquid Smoothness) to remove bar-to-bar noise before the final trend state is determined.
Confidence Buffer
The ML Confidence Buffer (%) setting creates a dead zone around 50% — the ML signal must cross convincingly past this threshold before changing trend state. This prevents rapid oscillation during uncertain market conditions where the KNN vote is close to 50/50.
Price Smoothing
When Smooth Price Input is enabled, the HMA (Hull Moving Average) of close is used as the feature input source instead of raw close — reducing the influence of single-bar spikes on the ML feature calculation.
🎨 Visuals
📈 ML SuperTrend Band
The standard SuperTrend band (ATR × Factor above/below hl2) is plotted with color and opacity driven by ML confidence — brighter and more opaque when conviction is high, faded when confidence is moderate.
🕯️ Gradient Candle Coloring
When enabled, every candle is colored on a gradient scale from the bull or bear color. The intensity of the color reflects the ML confidence level — high-confidence bars are deeply saturated, low-confidence bars appear faded. This gives an immediate visual read on trend conviction across the entire chart without reading numbers.
🌊 Glow Bars
Large bar characters are plotted at the top (bear) or bottom (bull) of each bar using plotchar, colored with a gradient glow that intensifies with ML probability. This creates a visual "heat signature" beneath or above the price action showing the strength of the current trend pulse.
🫧 3D Rejection Orbs
When Show 3D Rejection Orbs is enabled, bars where price wicks significantly through the SuperTrend band but closes back on the trend side are flagged as rejection events. Each orb is rendered as a layered 5-label pseudo-3D sphere:
Outer shadow layer (dark, semi-transparent)
Base color layer (bull/bear color, 70% transparent)
Core fill layer (bull/bear color, 15% transparent — brightest)
Highlight ring (white, 85% transparent)
Specular glint (white, 40% transparent — small, offset for 3D effect)
A dashed stem line connects the orb to the wick tip. The volume of the rejection bar is printed in K/M/B shorthand inside a label below (bull) or above (bear) the orb. Orb size scales dynamically with the Z-score of the bar's volume relative to the 100-bar average — larger orbs = more unusual volume on the rejection.
A minimum gap between orbs (Min Bubble Gap) prevents clustering on consecutive bars.
🛠️ Settings
Group Setting Default Description
Machine Learning K-Neighbors 10 Number of nearest historical analogs to vote (1–50)
Machine Learning Search Window 500 Historical bars to search for neighbors (100–2000)
SuperTrend Settings ATR Length 10 ATR lookback for band calculation
SuperTrend Settings Factor 3.0 ATR multiplier for band distance
Noise Filter Smooth Price Input On Uses HMA instead of raw close for ML features
Noise Filter Smoothing Length 10 HMA period for price smoothing
Noise Filter ML Confidence Buffer (%) 5.0 Dead zone around 50% — prevents flip on weak signals
Rejection Signals Show 3D Rejection Orbs On Toggle the layered rejection orb system
Rejection Signals Min Wick-to-Body Multiplier 1.5 Wick must be this many times larger than body to qualify
Rejection Signals Min Bubble Gap (Bars) 5 Minimum bars between consecutive orbs
Visual Settings Uptrend / Downtrend Color Green / Red Base colors for all gradient visuals
Visual Settings Liquid Smoothness 20 EMA length for smoothing the ML probability output
Visual Settings Vibrancy 1.5 Power curve for gradient intensity (1.0–3.0)
Visual Settings Gradient Candle Coloring On Toggle candle color gradient
Dashboard Show Dashboard On Toggle live stats table
Dashboard Position / Size Top Right / Small Table placement and text size
📋 Dashboard
The live on-chart dashboard updates every bar with:
Metric Description
Trend Direction Current ML-confirmed trend — Bullish or Bearish
ML Confidence Smoothed KNN probability score (0–100%)
Bars In Trend Bars elapsed since the last trend direction change
ST Distance % distance between current close and the SuperTrend band
Rel. Volatility Current ATR as a % of price — normalized volatility reading
📋 How to Use
Use ML Confidence as a position sizing guide — A confidence score of 80%+ means the KNN model has strong historical agreement for the current trend. Scale in heavier. A score of 55–65% is a weak signal — reduce size or wait for confirmation.
Only take trend-direction trades — If Trend Direction shows Bearish, do not take long setups regardless of what price structure looks like. The ML signal is the macro filter; structure provides the entry.
Use rejection orbs as entry triggers — A bullish rejection orb (price wicked below the band, closed back above) during an ML-bullish trend = high-probability long entry. The orb's volume label confirms whether institutional size was present on the rejection.
Watch candle saturation for fading trends — Deeply colored candles = high conviction. Fading candle color = ML confidence declining. If you see several consecutive faded candles, the trend may be losing statistical support — tighten stops or reduce exposure.
Increase K-Neighbors for stability on HTF — On higher timeframes (1H, 4H, Daily), increase K to 20–30 for a more robust vote. On lower timeframes (1m–15m), keep K at 5–15 to stay responsive.
Increase ML Buffer on choppy instruments — On forex pairs or low-volume sessions, set the buffer to 8–12% to require stronger consensus before flipping trend state.
⚠️ Notes
Built in Pine Script v6 with max_bars_back = 2001 to support large search windows.
The KNN search runs on every bar — on large Search Window values (1000–2000), script execution time increases. If performance degrades, reduce the window or switch to a higher timeframe.
The 3D orb system uses 6 labels per orb plus 1 line — on active sessions, this can approach TradingView's 500-label limit. The Min Bubble Gap setting directly controls label consumption.
Orb size is volume-adaptive: the Z-score of the rejection bar's volume vs. the 100-bar SMA determines rendered size (clamped between 8–30 units), ensuring orbs reflect genuine institutional activity, not noise.
Vibrancy controls the power curve of gradient intensity — values above 2.0 create a more dramatic visual distinction between high and low confidence bars.
© Quantum Edge Capital LLC. Licensed under CC BY-NC-SA 4.0. Non-commercial use only
אינדיקטור Pine Script®






















