KNN Machine Learning Momentum IndicatorKNN Machine Learning Momentum Indicator
🌌 Overview
This script implements a K-Nearest Neighbors (KNN) machine learning algorithm combined with Dimensionality Reduction to estimate short-term price momentum.
💬 Feedback & Suggestions
If you have any questions regarding the logic, or suggestions for new features, please feel free to leave a comment below! Your feedback helps improve the algorithm for everyone.
🧠 Core Methodology
The indicator follows a KNN machine learning pipeline:
Feature Engineering: It utilizes a multi-faceted feature set including RSI variations, Price-to-MA deviations, and Oscillatory Momentum.
Normalization (Z-Score): All features are standardized into Z-scores, ensuring the KNN distance calculation is not biased by different scales of data.
Dimensionality Reduction: To reduce noise and the "curse of dimensionality," the script compresses features into three orthogonal Principal Components (Momentum, Mean Reversion, and Dynamics).
KNN Engine: The algorithm searches the historical lookback window for the 'K' most similar patterns using the Minkowski Distance metric and applies a Gaussian Kernel to weight the closest neighbors more heavily.
🚀 Strategic Target: Momentum vs. Absolute Price
A key distinction of this algorithm is its target objective. Rather than attempting to predict the exact future price (which is often prone to extreme noise), this model focuses on estimating latent market momentum.By analyzing how current feature sets relate to historical momentum shifts, the KNN engine identifies the underlying "energy" of the market.
This approach allows traders to capture the probable direction and strength of the next move, providing a more robust edge in dynamic market regimes than unstable price forecasting.
🛡️ Understanding the "Curse of Dimensionality"
A common pitfall in KNN-based trading strategies is the Curse of Dimensionality. This Script addresses this by implementing a Dimensionality Reduction layer. By condensing 9 raw technical features into 3 highly descriptive "Principal Components," we maintain a dense feature space. This ensures that the "nearest neighbors" found by the algorithm are truly statistically significant patterns rather than random noise.
🛠️ Key Features
Dynamic Probability Engine: Visualizes the confidence of the model through gradient bar coloring.
Trend Filter (EMA): Integrated EMA filter to distinguish between "Trend" signals (stronger) and "Counter-Trend" signals (lighter).
Minkowski Distance Tuning: Adjust the p parameter to switch between Manhattan (p=1), Euclidean (p=2), or higher-order distance metrics.
Visual Analytics: Clean, institutional-grade UI with clear signal shapes and background highlighting.
📈 How to Trade
Bull/Bear Signals: Large shapes with labels indicate signals that align with the major EMA trend (High Conviction).Small Shapes: Indicate potential signals that are counter to the major trend (Use with caution/mean reversion).
Bar Colors:Bright Cyan/Red: High confidence prediction.
Slate Gray: Low confidence / Neutral market regime.
⚠️ Disclaimer
Machine learning models are subject to overfitting and market regime shifts. This indicator is intended to be used as a decision-support tool and should be combined with proper risk management and additional technical analysis.Algorithm: K-Nearest Neighbors (KNN)Distance Metric: Minkowski DistancePreprocessing: Z-Score Normalization + Dimensionality Reduction
Pine Script® インジケーター






















