This script demonstrates an experimental implementation of the K-means clustering algorithm in Pine Script using array structures and a custom pseudo-random number generator.
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
175 lines of Pine Script v4 from beta/arrays/test-025-kmeans.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.
Test 025 K-Means partitions a dataset into a specified number of clusters based on Euclidean distance calculations from initialized center points. Users can adjust the amount of historical data evaluated and the total cluster count. It serves as an overlay proof-of-concept for unsupervised machine learning techniques within Pine Script.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
ndata
int
20
ndata
nclusters
int
3
nclusters
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 calculates a locally weighted regression curve alongside a standard linear regression line to detect directional price shifts. It identifies potential buy signals when the weighted line crosses below the linear regression while falling over a de
This indicator calculates a trend direction and smoothed moving average using a k-nearest neighbours (k-NN) classification algorithm directly on the main price chart. Users can select input price types, target values, neighborhood size, and smoothing periods.
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
Calculates and plots a forward price prediction directly on the main chart using an on-chart multilayer perceptron neural network. The model processes technical input features including moving average ratios, RSI, normalized ATR, and Bollinger bandwidth throug
This indicator uses a Hidden Markov Model (HMM) framework combined with an adaptive dual moving average engine and Directional Movement Index (DMI) features to identify market regimes (bullish, bearish, or ranging/chop). It calculates regime probabilities on e