AI-SuperTrend (KNN Machine Learning)AI-SuperTrend (KNN Machine Learning)
▶️Overview
The AI-SuperTrend (KNN Machine Learning) is a trend-following indicator that integrates a K-Nearest Neighbors (KNN) classification engine into the classic SuperTrend algorithm. Rather than attempting to "predict" the future in the traditionally volatile and noise-heavy financial markets, this tool treats the market as a multi-dimensional state to be estimated.
By continuously sampling historical data, the engine identifies clusters of past conditions that mirror the present. It then analyzes the trend of those neighbors to deduce the "True State" of the current market, using this statistical consensus to validate SuperTrend signals and filter out deceptive market noise.
▶️Why KNN for Financial Markets?
In the noise-heavy environment of financial markets, complex parametric models like Support Vector Machines (SVM) or Deep Neural Networks often struggle with stability. These models frequently suffer from convergence issues during training, or they produce outputs that stagnate around the mean due to the low signal-to-noise ratio of financial data. Most critically, they are highly prone to overfitting, capturing random price fluctuations as if they were true alpha.
KNN offers a distinct advantage through its Robustness and Adaptability:
Non-parametric Nature:
KNN makes no underlying assumptions about the distribution of data, allowing it to adapt to non-linear and evolving market regimes.
Rolling Window Learning:
The model utilizes a rolling "Learning Window" that naturally aligns with the bar-by-bar execution of Pine Script. This approach ensures that the engine is always synchronized with the most relevant, recent market structures while remaining computationally efficient within the platform's resource constraints.
▶️Core Methodology: KNN and State Estimation
1. The KNN Engine
K-Nearest Neighbors is a non-parametric "Lazy Learning" algorithm. Instead of building a static model, it looks at the current market "Feature Vector" and searches the historical database for the K most similar instances.
Distance Metric: Uses the Minkowski Distance. This is adjustable via the p-parameter, where p=1 represents Manhattan distance and p=2 represents Euclidean distance.
Gaussian Weighting: Not all neighbors are equal. The script applies a Weighting kernel where neighbors closer to the current state carry significantly more weight in the final prediction than those further away.
2. State Estimation (Bayesian-like Approach)
The state estimation logic implemented in this script follows the methodology used by myself in the "KNN Machine Learning Momentum Indicator." By applying this approach to the SuperTrend framework, the indicator achieves a higher level of precision in trend validation.
Probability Calculation: The probability of a Bullish state is calculated as (Sum of Weights of Bullish Neighbors) divided by (Total Weights of all K Neighbors).
Synergistic Robustness: By combining the volatility-based boundaries of SuperTrend with the KNN state estimation, the system significantly improves robustness against market noise. A SuperTrend flip is only considered a "Major" signal if the AI confirms that the underlying market state has truly shifted, based on historical probability.
Confirmation: A signal is only triggered if the estimated probability exceeds the user-defined Prediction Threshold (e.g., 0.9 or 90%).
3. Sampling Stride (Efficiency and Diversity)
To balance computational load and data diversity within Pine Script's limits, the engine utilizes a Stride mechanism:
Computational Efficiency: Instead of checking every single bar in the lookback window, the script samples data at intervals defined by the Stride (e.g., every 15th bar).
Pattern Diversity: By skipping adjacent, highly correlated bars, the "Learning Window" covers a broader range of market structures. This ensures the KNN engine sees various types of volatility and price action rather than redundant near-term data.
▶️Key Features
Multi-Dimensional Feature Engineering
The AI analyzes a "Feature Space" consisting of:
RSI Momentum Clusters: Captures momentum across three different time horizons (Short, Medium, Long) to detect lead/lag convergence.
MA Deviations: Measures the "stretch" or distance from the mean using various Moving Average types (ZLSMA, HMA, etc.).
PCA Compression: An optional Dimensionality Reduction toggle that merges correlated features into 3 Principal Components. This reduces the "Curse of Dimensionality" and focuses the AI on the most impactful data trends.
▶️Parameter Guide
🔲SuperTrend Settings
ATR Length: The lookback period for volatility calculation.
Factor: The multiplier that determines the distance of the SuperTrend line from price.
🔲Machine Learning Engine
K-Neighbors (K): The number of historical patterns to compare. A smaller K is more sensitive to recent changes, while a larger K is more robust but may lag.
Learning Window Size: How far back in history the AI "remembers" or searches for neighbors.
Stride: The sampling interval. A stride of 15 means the AI learns from every 15th bar, increasing the effective historical range without hitting script calculation limits.
Prediction Threshold: The confidence level (0.1 to 1.0) required to trigger a signal. A value of 0.9 means the AI must be 90% certain based on historical weights.
🔲Feature Engineering
Feature MA Type: Choose the baseline for deviation (e.g., ZLSMA for zero-lag, HMA for speed).
Normalizing Window: The lookback for Z-Score normalization, ensuring all features are on the same scale (mean=0, std=1).
Minkowski Parameter (p): Controls the distance logic. p=1 is Manhattan, p=2 is Euclidean.
Shape Parameter: Controls the sensitivity of the Gaussian weighting. Higher values make the weights drop off more aggressively as distance increases.
▶️Visual Analytics
Major Signals (▲/▼): High-confidence trend changes confirmed by the AI. These are plotted only when the SuperTrend direction aligns with the AI's predicted direction and its probability exceeds the defined threshold.
Probability Labels: At every SuperTrend reversal point, the indicator displays a label showing the AI's estimated probability for that trend direction (e.g., "Pred 92%"). This allows for real-time visual assessment of the AI's confidence in the SuperTrend flip.
Major Signals: High-confidence trend changes confirmed by the AI.
ST Dots: Standard SuperTrend flips without full AI confirmation.
Dynamic Bar Color: A gradient representing the real-time AI confidence score.
Blue/Cyan: High Bullish Confidence.
Red/Pink: High Bearish Confidence.
Gray: Neutral or Indecisive state.
Disclaimer
Past performance does not guarantee future results. This indicator is a tool for statistical analysis and should be used in conjunction with a complete risk management strategy.
Pine Script® indicator






















