Machine Learning: kNN Trend Predictor
Feb 6, 2025

The Machine Learning: kNN Trend Predictor indicator leverages the k-Nearest Neighbors algorithm to forecast price trends by identifying historical price patterns similar to current market behavior. This tool provides traders with a predictive curve and color-coded trend signals to assist in determining market direction and potential entry points.
Usage
The Usage section describes how the script can be used to identify market trends and potential reversal points.
- Trend Identification: The indicator plots a predictive curve on the chart. When the curve and background are colored green, it suggests a bullish trend based on the nearest neighbor analysis. Conversely, a red curve and background suggest a bearish trend.
- Trade Signals: When the "Show Trading Signals" option is enabled, the script will display triangle labels (▲ for Long, ▼ for Short) on the chart. These signals appear when the kNN model detects a shift in the predicted directional bias.
- Historical Comparison: By adjusting the Nearest Neighbors (N) and Distance Ranking (K) parameters, users can change how deep the algorithm looks into past data and how many patterns it averages to create its prediction.
Details
The script implements a k-Nearest Neighbors (kNN) approach, a non-parametric machine learning method. The execution logic follows these steps:
- Distance Calculation: The script computes the Euclidean distance between the current price and previous price points.
- Pattern Recognition: It maintains an array of the "K" most recent distance calculations to rank and identify the most similar past price movements.
- Averaging: The algorithm selects the "N" nearest neighbors and calculates their average to project a future price point.
- Bias Prevention: An optional adjustment factor is included to ensure that predictions are based solely on historical data, preventing look-ahead bias during backtesting.
- Directional Logic: A bullish signal is generated if the predicted value is lower than the price (suggesting upward mean reversion or trend continuation), and a bearish signal is generated if the predicted value is higher.
Settings
- Source: Determines the price data used for the kNN calculations (e.g., Close, Open, HL2).
- Nearest Neighbors (N): Specifies how many of the closest historical data points are averaged to generate the prediction.
- Distance Ranking (K): Defines the depth of the historical lookback for distance comparison; a higher value increases the data pool for finding patterns.
- Prevent Look-Ahead Bias: When enabled, the script ensures predictions are calculated without using future data points, making it suitable for live trading and backtesting.
- Show Trading Signals: Toggles the visibility of visual Long and Short labels on the chart.
FAQ
How do I interpret the kNN Curve? The kNN Curve acts as a dynamic baseline. When the price is moving in relation to the predicted average of its nearest neighbors, the color shifts to represent the expected trend direction (Green for bullish, Red for bearish).
What is the difference between N and K in this script? K represents the total number of historical distance samples the script considers, while N represents the specific subset of those samples that are the most similar (closest) to the current price.
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 indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.