Indicator · Pine Script v5 · On the chart

Function K-Means Clustering

by ricardosantos79· code credited to RicardoSantos

Calculates K-Means clustering on two-dimensional float data arrays directly within Pine Script.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code161
Settings2inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

161 lines of Pine Script v5 from beta/libs/v4_to_v5_conversions/appended to libs/SignalProcessingKMeansClustering.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

SignalProcessingKMeansClustering.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5
indicator(title='Function K-Means Clustering', overlay=true, max_labels_count=500)

// @function performs K-Means Algorithm to find n clusters.
// @param _X float array, with x data points
// @param _Y float array, y with data points
// @param _number_of_clusters int, number of clusters to find center.
// @returns float array with the cluster centers for the given data.
f_kmeans_cluster(float[] _X, float[] _Y, int _number_of_clusters=2) =>
//| -----------------------------------------------------------
//| 1:  setup and errors
    int _X_size = array.size(id=_X)
    int _Y_size = array.size(id=_X)

    float[] _centers_x = array.new_float(size=0, initial_value=0.)
    float[] _centers_y = array.new_float(size=0, initial_value=0.)

    if _X_size < 1
        runtime.error('f_kmeans_cluster(): "_X" has the wrong size')
        [_centers_x, _centers_y]
    else if _Y_size < 1
        runtime.error('f_kmeans_cluster(): "_Y" has the wrong size')
        [_centers_x, _centers_y]
    else if _X_size != _Y_size
        runtime.error('f_kmeans_cluster(): "_X" and "_Y" do not match')
        [_centers_x, _centers_y]
    else if _number_of_clusters < 1
        runtime.error('f_kmeans_cluster(): "_number_of_clusters" must be a positive number > 0')
        [_centers_x, _centers_y]
    else if _number_of_clusters > _X_size
        runtime.error('f_kmeans_cluster(): "_number_of_clusters" must not exceed the number of data points in "_X, _Y".')
        [_centers_x, _centers_y]
    else
        int[] _centers_count = array.new_int(size=_number_of_clusters, initial_value=1)
    
        float[] _distances = array.new_float(size=0, initial_value=0.)
        int[] _group = array.new_int(size=0, initial_value=0)

//| 2:  initial cluster centers
//|     can use alternative method of initialization (kmeans++, random) for now will only use random
        for _i = 0 to _number_of_clusters - 1 by 1
            int _value = int(math.random(_X_size))
            array.push(id=_centers_x, value=array.get(id=_X, index=_value))
            array.push(id=_centers_y, value=array.get(id=_Y, index=_value))
//|     2.b: calculate distances to centers:
        for _i = 0 to _number_of_clusters - 1 by 1
            float _total_sum_of_distance = 0.0
            for _j = 0 to _X_size - 1 by 1
                float _x = array.get(id=_X, index=_j)
                float _y = array.get(id=_Y, index=_j)
                //  calculate distance to cluster and populate the _group
                int _min_group = 0
                float _min_distance = math.sqrt(math.pow(_x - array.get(id=_centers_x, index=0), 2) + math.pow(_y - array.get(id=_centers_x, index=0), 2))
                for _k = 1 to _number_of_clusters - 1 by 1
                    float _c_x = array.get(id=_centers_x, index=_k)
                    float _c_y = array.get(id=_centers_y, index=_k)
                    float _distance = math.sqrt(math.pow(_x - _c_x, 2) + math.pow(_y - _c_y, 2))  //some examples did not sqrt
                    if _distance < _min_distance
                        _min_distance := _distance
                        _min_group := _k
                        _min_group
                array.push(id=_group, value=_min_group)
                array.push(id=_distances, value=_min_distance)
                _total_sum_of_distance := _total_sum_of_distance + _min_distance
                _total_sum_of_distance

            _total_sum_of_distance := _total_sum_of_distance * math.random(1.0)

            for _j = 0 to _X_size - 1 by 1
                _total_sum_of_distance := _total_sum_of_distance - array.get(id=_group, index=_j)
                if _total_sum_of_distance > 0
                    continue
                else
                    array.set(id=_centers_x, index=_i, value=array.get(id=_X, index=_j))
                    array.set(id=_centers_y, index=_i, value=array.get(id=_Y, index=_j))
                    break
//| 3:  Choose center with probability proportional to its squared distance from existing centers.
            for _j = 0 to _X_size - 1 by 1
                float _x = array.get(id=_X, index=_j)
                float _y = array.get(id=_Y, index=_j)
                //  calculate distance to cluster and populate the _group
                int _min_group = 0
                float _min_distance = math.sqrt(math.pow(_x - array.get(id=_centers_x, index=0), 2) + math.pow(_y - array.get(id=_centers_x, index=0), 2))
                for _k = 1 to _number_of_clusters - 1 by 1
                    float _c_x = array.get(id=_centers_x, index=_k)
                    float _c_y = array.get(id=_centers_y, index=_k)
                    float _distance = math.sqrt(math.pow(_x - _c_x, 2) + math.pow(_y - _c_y, 2))  //some examples did not sqrt
                    if _distance < _min_distance
                        _min_distance := _distance
                        _min_group := _k
                        _min_group
                array.set(id=_group, index=_j, value=_min_group)
                array.set(id=_distances, index=_j, value=_min_distance)
//| 4:  find cluster centers.
        for _i = 0 to _X_size - 1 by 1
            float _x = array.get(id=_X, index=_i)
            float _y = array.get(id=_Y, index=_i)
            int _group_i = array.get(id=_group, index=_i)

            int _center_count = array.get(id=_centers_count, index=_group_i)
            float _center_x = array.get(id=_centers_x, index=_group_i)
            float _center_y = array.get(id=_centers_y, index=_group_i)
            array.set(id=_centers_count, index=_group_i, value=_center_count + 1)
            array.set(id=_centers_x, index=_group_i, value=_center_x + _x)
            array.set(id=_centers_y, index=_group_i, value=_center_y + _y)

        for _i = 0 to _number_of_clusters - 1 by 1
            int _center_count = array.get(id=_centers_count, index=_i)
            float _center_x = array.get(id=_centers_x, index=_i)
            float _center_y = array.get(id=_centers_y, index=_i)

            array.set(id=_centers_x, index=_i, value=_center_x / _center_count)
            array.set(id=_centers_y, index=_i, value=_center_y / _center_count)
//| 6:  Output.
        [_centers_x, _centers_y]
//{ usage:
//{ remarks:
//      http://rosettacode.org/wiki/K-means%2B%2B_clustering
//}}}


size = input(20)
data_x = array.new_float(size)
data_y = array.new_float(size)
for _i = 0 to size-1
    array.set(data_x, _i, math.floor(math.random() * 500))
    array.set(data_y, _i, math.random() * 500)

flabel(_x, _y, _t='', _col=color.blue, _size=size.tiny)=>label.new(bar_index+int(_x), 0.0+_y, _t, color=color.new(_col, 20), style=label.style_circle, size=_size)
fline(_x1, _y1, _x2, _y2, _col=color.blue)=>
    line.new(bar_index+int(_x1), 0.0+_y1, bar_index+int(_x2), 0.0+_y2, extend=extend.right, color=_col, style=line.style_dashed, width=2)
    line.new(bar_index+int(_x1), 0.0+_y1, bar_index+int(_x2), 0.0+_y2, extend=extend.none, color=_col, style=line.style_arrow_right, width=1)
clusters = input(3)
[kx, ky] = f_kmeans_cluster(data_x, data_y, clusters)

float centerx = array.avg(kx)
float centery = array.avg(ky)

f_c(_i)=>color.rgb(255-_i*(255/clusters), _i*(255/clusters), 100, 0)

if barstate.islastconfirmedhistory
    // data points
    for _i = 0 to size-1
        flabel(array.get(data_x, _i), array.get(data_y, _i))
    // cluster centers
    for _k = 0 to clusters-1
        float _kx = array.get(kx, _k)
        float _ky = array.get(ky, _k)
        color _c = f_c(_k)
        flabel(_kx, _ky, str.tostring(_k), _col=_c, _size=size.huge)
        if _k == 0
            float _mx = (array.get(kx, clusters-1) + _kx) / 2.0
            float _my = (array.get(ky, clusters-1) + _ky) / 2.0
            fline(centerx, centery, _mx, _my, _col=_c)
        else
            float _mx = (centerx + array.get(kx, _k-1) + _kx) / 3.0
            float _my = (centery + array.get(ky, _k-1) + _ky) / 3.0
            fline(centerx, centery, _mx, _my, _col=_c)

Overview

The script initializes cluster centers randomly from data points and iteratively assigns points to clusters based on Euclidean distance calculations. User settings control the size of the data sample and the target number of clusters to detect. It renders cluster data directly as an overlay on the main price chart.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

AI / machine learning

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
sizeint20size
clustersint3clusters
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.

Similar Pine Scripts

257 stars

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

Indicator v5 AI / machine learningOscillators & momentum
Updated 6 weeks ago · None stated GitHub repository of Donald - Machine Learning Options

AI Trend Navigator

by hasnocool
106 stars

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.

Indicator v5 AI / machine learningTrend
Updated 2 years ago · GPL-3.0 GitHub repository of AI Trend Navigator

Machine Learning: Lorentzian Classification

by artificial-intelligence-edge
75 stars

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

Indicator v5 AI / machine learningOscillators & momentum
Updated 2 days ago · MIT GitHub repository of Machine Learning: Lorentzian Classification
47 stars

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

Indicator v6 AI / machine learning
Updated 8 months ago · MIT GitHub repository of Multilayer Perceptron Predictor
46 stars

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

Indicator v5 AI / machine learningTrend
Updated 7 weeks ago · None stated GitHub repository of HMM Enhanced: Regime Probability

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers