1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
218 lines of Pine Script v5 from scripts/096-ML-kNN-based-Strategy-(s).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.
Machine Learning: kNN-based Strategy calculates normalized values from selected technical indicators, such as RSI, ROC, CCI, and volume, across short and long lookback windows. The algorithm finds historical nearest neighbors to classify current price behavior and plot buy and sell signals directly on the price chart. Users can adjust window lengths, base neighbor counts, indicator selection, and volatility filtering. Note that the signals repaint.
k-Nearest Neighbors machine learning classification model
Multi-indicator feature extraction including RSI, ROC, CCI, and volume
Configurable base neighbor count and lookback window periods
Optional volatility filter for trade signal refinement
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Indicator
string
All
Indicator
Short Period [1..n]
int
14
ShortWinow
Long Period [2..n]
int
28
LongWindow
To Filter Out Signals By Volatility?
bool
false
Filter
Base No. of Neighbours (K) [5..n]
int
252
BaseK
Start Date
time
startdate
StartDate
Stop Date
time
stopdate
StopDate
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This indicator uses a Lorentzian distance k-nearest neighbors classification algorithm to predict market direction based on up to five technical features, such as RSI, WaveTrend, CCI, and ADX. It compares current feature values against historical bar patterns
This indicator classifies the current bar by comparing its feature vector with historical bars using Lorentzian distance and a k-nearest-neighbors vote. Configurable features include RSI, WaveTrend, CCI, and ADX. Optional volatility, regime, ADX, and EMA filte
The Machine Learning Momentum Index uses a k-Nearest Neighbors classification algorithm to evaluate momentum and trend direction directly on the price chart. It records historical parameter pairs derived from weighted moving averages and smoothed RSI values, t
Farid Makram Custom Levels is a multi-signal indicator for TradingView that combines RSI momentum analysis with MOBO oscillator signals based on the DPO. It generates buy and sell signals when RSI crosses its moving average or when DPO crosses zero, with confi
This indicator overlays a weighted ensemble of three models on the price chart. It compares the current bar with historical bars using RSI, volume ratio, price change and range position, then blends that KNN vote with a linear-regression projection and an EMA-
This indicator implements a k-nearest neighbours classification model to forecast price direction several bars ahead. It extracts three normalized technical features: RSI, an EMA spread scaled by ATR, and normalized candle range volatility. Historical samples