Indicator · Pine Script v4 · Own pane

Function: Decision Tree

by ricardosantos79· code credited to RicardoSantos

Function: Decision Tree is a Pine Script v4 study that demonstrates a weighted decision-tree path sampler in a separate pane.

View on GitHub

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code54
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

54 lines of Pine Script v4 from beta/arrays/test-034-decisiontree.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

test-034-decisiontree.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=4
study(title='Function: Decision Tree')

//| -------------------------------------------------------------------------------|
var float[] seed = array.new_float(size=1, initial_value=42.0)                  //-|
f_prng(_range)=>                                                              //---|
//| pseudo random function:                                                 // O O |
    var float _return = 1.0                                                 //- V -|
    float _seed = array.get(id=seed, index=0)                                //NNNN|
    float _reseed =  271.828                                                  // A |
    _return := ((3.1416 * _return % _seed) * _reseed) % _range                // V |
    array.set(id=seed, index=0, value=(1 + (_seed * 1.618) % _range) + _return) //||
    _return                                                                    // ||
//| -------------------------------------------------------------------------------|


f_decision_tree(_node_weights, _depth)=>
    int[] _path = array.new_int(size=0, initial_value=0)
    int _nweights = array.size(id=_node_weights)
    int _sumweights = ceil(array.sum(id=_node_weights))
    if _nweights > 0 and _depth > 0 and _sumweights == 1
        for _d = 1 to _depth
            for _w = 0 to 999
                int _select_weight_index = int(f_prng(_nweights))
                float _rng = f_prng(1.0)
                float _weight = array.get(id=_node_weights, index=_select_weight_index)
                if _weight >= _rng
                    array.push(id=_path, value=_select_weight_index)
                    break
    _path

//| Array to contain weights, sum of array values must be 1.
var float[] node_weights = array.new_float(size=0, initial_value=0)
if barstate.isfirst
    array.push(id=node_weights, value=0.5)
    array.push(id=node_weights, value=0.5)

//| Input Parameters:
int depth = input(defval=1, minval=1)
int paths = input(defval=2, minval=1)

string t = ''

for _p = 1 to paths
    _path = f_decision_tree(node_weights, depth)

    t := t + '\nPath ' + tostring(_p, '#') + ': ' + tostring(_path)

var label la = label.new(bar_index, 0.0, '')
label.set_xy(id=la, x=bar_index, y=0.0)
label.set_text(id=la, text=t)

Overview

A small pseudo-random generator reseeds itself from a stored float and picks node indices whose weights are at least the random draw. Two equal weights of 0.5 are loaded on the first bar, and those weights must sum to one. The depth input sets how many nodes each path records, and the paths input sets how many paths are built. Results appear as text in one label that follows the latest bar. It does not plot prices or issue trade signals.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the 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
depthint1depth
pathsint2paths
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

This indicator combines a dynamic Seasonal Random Index (SRI) with a dynamic Karobein Oscillator and a pattern-similarity search algorithm in a separate pane. It normalizes SRI into seasoned and deseasoned series, shading the area green or red to signal trend

Indicator v4 Oscillators & momentumTrend
Updated 1 year ago · None stated GitHub repository of Similarity Search, Karobein and Seasonal Random Index

This indicator plots an oscillator in a separate pane. It builds several SuperTrend series across a user-defined factor range and step, using ATR as the band width. On each bar it measures the distance between the close and each SuperTrend output, then groups

Indicator v5 TrendVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of AI SuperTrend Clustering Oscillator [LuxAlgo]
99 stars

This indicator applies the rectified linear unit to the difference between the selected source and a 20-period simple moving average. Positive deviations plot in green and negative deviations plot in red in a separate pane. The only input changes the price ser

Indicator v6 Moving averagesAI / machine learning
Updated 4 weeks ago · Apache-2.0 GitHub repository of Rectified Linear Unit (ReLU)
7 stars

Lorentzian Classification is a machine learning indicator that applies a k-nearest neighbors algorithm using Lorentzian distance to analyze market patterns. It processes configurable technical indicators, including RSI, WaveTrend, CCI, and ADX, as feature vect

Indicator v6 AI / machine learningOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of Lorentzian Classification
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

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