Machine Learning : Torben's Moving Median KNN Bands
Jun 15, 2023

The Machine Learning : Torben's Moving Median KNN Bands indicator utilizes a hybrid approach combining non-linear median filtering with K-Nearest Neighbors regression to produce smoothed, noise-resistant trend bands.
Usage
The Usage section describes how the script can be used to identify trend direction and volatility ranges through machine learning-derived levels.
- Trend Identification: The script plots two primary KNN lines (Fast and Slow). A bullish trend is indicated when the Fast KNN line is above the Slow KNN line, typically highlighted by a green background fill. Conversely, a bearish trend is indicated when the Fast KNN line is below the Slow KNN line, highlighted by a red background fill.
- Volatility Bands: The indicator includes standard deviation bands projected from the KNN lines. These acts as dynamic support and resistance zones, helping traders visualize the expected price range based on recent volatility.
- Signal Crosses: Crossovers between the Fast and Slow KNN lines can be used as potential entry or exit signals, as they represent shifts in the underlying price distribution processed by the KNN algorithm.
Details
The script operates through a two-stage process to filter market noise while preserving significant price movements.
- Torben's Moving Median: Unlike standard moving averages which can be heavily skewed by outliers, the Torben's Moving Median algorithm effectively removes "salt and pepper" noise. It iteratively estimates the median within a window by partitioning data, providing a robust baseline that preserves price edges better than a simple mean.
- KNN Regression: The K-Nearest Neighbors (KNN) algorithm is applied to the output of the Torben filter. It searches for the most similar data points within a defined lookback to predict the expected price level. This machine learning layer allows the indicator to adapt to the underlying distribution of the data rather than relying on fixed linear calculations.
The combination of these techniques minimizes the blurring effect common in traditional filters while ensuring the output remains responsive to structural market changes.
Settings
- Fast Length: Determines the window size for the fast Torben Moving Median and its corresponding KNN calculation. A smaller value increases responsiveness.
- Slow Length: Determines the window size for the slow Torben Moving Median and its corresponding KNN calculation. A larger value provides a smoother, long-term trend perspective.
- Source: The price data used for all calculations (default is HLC3).
FAQ
How do I interpret the colored fill between the lines? The fill represents the relationship between the Fast and Slow KNN lines; a green fill suggests bullish momentum (Fast > Slow), while a red fill suggests bearish momentum (Slow > Fast).
What makes Torben's Median different from a regular Median? Torben's method is an optimized, iterative approach to finding the median that is computationally efficient for signal processing, specifically designed to handle outliers without the lag associated with high-period moving averages.
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.