Machine Learning: kNN-based Strategy (update)
Feb 19, 2021

The Machine Learning: kNN-based Strategy (update) indicator provides a classification-based approach to market direction by utilizing a k-Nearest Neighbors algorithm to identify potential buy and sell signals across all timeframes.
Usage
The Usage section describes how the script can be used to identify trend shifts and potential entry/exit points. The indicator analyzes historical data points (features) and classifies current market conditions based on their similarity to past observations.
- Signal Generation: The tool plots "Buy" labels when the kNN model predicts upward movement and "Sell" labels for downward movement. These signals are filtered by volatility and volume conditions to ensure higher-quality entries.
- Trade Management: Cross icons appear on the chart to signify the end of a holding period or a reversal in the predicted signal, indicating where a trade might be closed.
- Time Filtering: The script includes a "Time Threshold" feature that prevents signals from appearing too early in a bar's formation, reducing the impact of premature price fluctuations.
Details
The script utilizes the k-Nearest Neighbors (kNN) algorithm, a non-parametric supervised learning method. It functions by storing "features" (market indicators) and their resulting price direction (Buy/Sell) in arrays.
- Feature Selection: The model uses four primary indicators—Relative Strength Index (RSI), Rate of Change (ROC), Commodity Channel Index (CCI), and Momentum (MOM)—to define the "distance" between data points.
- Distance Calculation: It uses Euclidean distance to find the $k$ closest historical neighbors to the current market state. The $k$ value is dynamically calculated as the square root of the user-defined $K$ input.
- Signal Filtering: To reduce noise, signals are passed through a filter that checks for volatility breaks (ATR-based) and volume breaks (RSI of volume).
- Performance Tracking: A dashboard on the right side of the chart provides real-time statistics, including cumulative return, win rate, and win/loss ratios based on the selected lot size.
Settings
- K Value for kNN Model: Sets the number of historical data points the algorithm considers.
- Indicator: Determines which technical indicator (RSI, ROC, CCI, MOM, or an average of All) is used as the feature for classification.
- Fast/Slow Period: Adjusts the lookback periods for the underlying feature indicators.
- Filter Signals by: Options to filter signals based on Volatility, Volume, Both, or None.
- Holding Period: Defines how many bars a trade should be held before an automated exit signal is generated.
- Time Threshold: A percentage filter that ensures signals only trigger after a certain portion of the bar's timeframe has elapsed.
- Information: Toggles the visibility of the performance dashboard.
- Lot Size: Sets the hypothetical position size used for the cumulative return calculations.
FAQ
How do I adjust the sensitivity of the signals? You can adjust the $K$ value; a lower value makes the model more sensitive to recent local data, while a higher value looks for broader historical patterns. Additionally, changing the "Filter Signals by" setting to "None" will increase signal frequency.
What do the colored labels and crosses represent? The "Buy" and "Sell" labels indicate the start of a predicted trend. The transparency of the label reflects the strength of the kNN prediction. The small crosses indicate the end of the trade based on the "Holding Period" or a signal reversal.
How can I access this tool? 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.