All indicators

Machine Learning: kNN-based Strategy (mtf)

Jan 11, 2021

Static chart image
SignalsMachine LearningDashboard

The Machine Learning: kNN-based Strategy (mtf) indicator leverages a k-Nearest Neighbors classification algorithm to identify potential market reversals and trend continuations across multiple timeframes.

Usage

The Usage section focuses on how the script processes historical data to generate real-time trading signals. Users can select specific technical indicators to serve as the feature set for the machine learning model.

  • Signal Generation: The script plots "Buy" and "Sell" labels on the chart when the kNN algorithm predicts a high probability of a price movement in a specific direction based on historical similarities.
  • Multi-Timeframe Analysis: By adjusting the "Resolution" setting, users can base the machine learning logic on data from higher timeframes while executing on a lower timeframe chart.
  • Volatility Filtering: When enabled, the indicator suppresses signals during periods of low volatility (based on ATR comparisons) to avoid choppy market conditions.
  • Backtesting Overlay: The script provides an on-chart information box displaying cumulative returns, win rates, and total trade counts to evaluate the performance of the selected parameters.

Details

The script implements a k-Nearest Neighbors (kNN) algorithm, a non-parametric supervised learning method. It functions by storing a "training" dataset of historical features (RSI, ROC, or CCI) and their resulting price outcomes within a user-defined training window.

When a new bar forms, the algorithm calculates the Euclidean distance between the current feature values and all stored historical instances. It identifies the "k" closest neighbors—where $k$ is dynamically set to the square root of the number of trading days in a year (252)—and performs a majority vote to determine the predicted class (Buy, Sell, or Hold). This multi-timeframe version utilizes request.security to fetch data from the specified resolution for its classification logic.

Settings

General Settings

  • Dataset: The source price data used for calculating the movement labels.
  • Resolution: The timeframe from which the indicator pulls historical data for the kNN model.
  • Indicator: Selects the feature set for the model (RSI, ROC, CCI, or an average of all three).
  • Fast Period: The lookback period for the "fast" version of the selected technical indicators.
  • Slow Period: The lookback period for the "slow" version of the selected technical indicators.
  • To Filter Out Signals By Volatility?: If enabled, signals only trigger if the short-term ATR is greater than the long-term ATR.

Training Window

  • Training Start Year/Month/Day: Defines the beginning of the historical data collection period for the model.
  • Training Stop Year/Month/Day: Defines the end of the historical data collection period.

Display and Simulation

  • Lot Size: The position size used to calculate the cumulative return in the information panel.
  • Show Information?: Toggles the visibility of the performance metrics label on the chart.

FAQ

How does the kNN algorithm determine a "neighbor"? The algorithm calculates the Euclidean distance between current indicator values and historical values. The points with the smallest distance (most similar characteristics) are considered the nearest neighbors.

Why are there no signals appearing on my chart? Ensure the "Training Start" and "Stop" dates encompass a valid range of historical data. Additionally, if the volatility filter is active, signals may be hidden during low-volatility periods.

How can I access the Machine Learning: kNN-based Strategy (mtf)? 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.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.