Library · Pine Script v5 · Library

FunctionNNetwork

by ricardosantos79· code credited to RicardoSantos

This Pine Script v5 library exposes methods for a generalized neural network.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code211
Settings0inputs
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 library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

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

FunctionNNetwork.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

// @description Method for a generalized Neural Network.
library("FunctionNNetwork")

import RicardoSantos/FunctionNNLayer/3 as nl
import RicardoSantos/MLLossFunctions/1 as loss
import RicardoSantos/MLActivationFunctions/3 as activation
// reference:
//      https://blog.primen.dk/understanding-backpropagation/
//      https://ml-cheatsheet.readthedocs.io/en/latest/backpropagation.html
//      https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
//      https://www.anotsorandomwalk.com/backpropagation-example-with-numbers-step-by-step/
//      https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/
//      https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/

// @function helper method to generate random weights of (A * B) size.
// @param previous_size int, number of nodes on the left.
// @param next_size int, number of nodes on the right.
// @returns float array.
generate_random_weights (int previous_size, int next_size) => //{
    //                 |    col          |     row      |
    _weights = array.new_float(previous_size*next_size)
    for _i = 0 to previous_size-1
        for _j = 0 to next_size-1
            array.set(_weights, _i * next_size + _j, math.random() * 2.0 - 1.0)
    _weights
//}

// @function helper method to propagate the signals forward.
// @param x TODO: fill parameters
// @returns float array.
propagate_forward (float[] inputs, float[] weights, int[] layer_sizes, int weights_max_width) => //{
    // TODO: might be good to remove output layer from loop? (inputs -> loop hidden -> outputs)
    //     : missing bias parameter..
    int _size_i = array.size(inputs)
    int _size_ls = array.size(layer_sizes)

    float[] _layer_outputs = nl.layer(inputs, array.slice(weights, 0, _size_i * array.get(layer_sizes, 0)), array.get(layer_sizes, 0))
    if _size_ls > 1
        float[] _layer_output = _layer_outputs
        for _layer = 1 to _size_ls - 1
            // find the base level of current layer weights:
            //      base = (sum of nodes up to previous layer) * (max weights in a node)
            //      (weights in layer) = base + (previous layer nodes) * (current layer nodes)
            int _previous_layer_size = array.get(layer_sizes, _layer - 1)
            int _this_layer_size = array.get(layer_sizes, _layer)
            int _w_base_idx = array.sum(array.slice(layer_sizes, 0, _layer-1)) * weights_max_width
            float[] _layer_weights = array.slice(weights, _w_base_idx, _w_base_idx + _previous_layer_size * _this_layer_size)
            _layer_output := nl.layer(_layer_output, _layer_weights, _this_layer_size)
            array.concat(_layer_outputs, _layer_output)
    _layer_outputs
//}

// @function helper method to propagate the signals backward.
// @param x TODO: fill parameters
// @returns float array.
propback_calculate_errors_for_output (float[] errors, float[] output_nodes, float[] targets) => //{
    int _size_o = array.size(output_nodes)
    int _size_t = array.size(targets)
    
    switch
        (_size_o != _size_t) => runtime.error(str.format('FunctionNNetwork -> propback_calculate_errors(): parameters "output_nodes" and "targets" size does not match, found: {0}, {1} .', _size_o, _size_t))

    for _n = 0 to _size_o - 1
        float _target = array.get(targets, _n)
        float _output = array.get(output_nodes, _n)
        array.push(errors, _target - _output)
//{ test:
// if barstate.isfirst
//     _e = array.new_float(0)
//     _o = array.from(1.0, 2, 3)
//     _t = array.from(1.1, 1.1, 1.1)
//     propback_calculate_errors_for_output(_e, _o, _t)
//     label.new(bar_index, 0.0, str.format('{0}', str.tostring(_e)))
//}}

// @function helper method to propagate the signals backward.
// @param x TODO: fill parameters
// @returns float array.
propback_calculate_errors_for_hidden (float[] errors, float[] weights, float[] error_deltas) => //{
    // iterate over weights in current layer
    for _i = 0 to array.size(weights) - 1
        float _error = 0.0
        float _wi = array.get(weights, _i)
        // iterate over nodes in next layer
        for _n = 0 to array.size(error_deltas) - 1
            float _error_delta_in_next_layer_node = array.get(error_deltas, _n) // #############
            _error += _wi * _error_delta_in_next_layer_node
        array.push(errors, _error)
//{ test:
// if barstate.isfirst
//     _e = array.new_float(0)
//     _w = array.from(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
//     _d = array.from(1.1, 1.1, 1.1)
//     propback_calculate_errors_for_hidden(_e, _w, _d)
//     label.new(bar_index, 0.0, str.format('{0}', str.tostring(_e)))
//}}

// @function Generalized Neural Network Method.
// @param x TODO: add parameter x description here
// @returns TODO: add what function returns
export network (
     float[] inputs, float[] targets, float[] weights, 
     int[] layer_sizes, float[] layer_biases, string[] layer_functions, 
     string loss_function='mse', float learning_rate=0.01
     ) => //{
    // TODO: Layer bias is not implemented.
    
    int _weights_max_width = array.max(layer_sizes) // highest number of nodes on each layer as base for weight rectangle matrix slicing.
    int _total_nodes = array.sum(layer_sizes)
    
    int _size_i = array.size(inputs)
    int _size_t = array.size(targets)
    int _size_w = array.size(weights)
    int _size_ls = array.size(layer_sizes)
    int _size_lb = array.size(layer_biases)
    int _size_lf = array.size(layer_functions)
    int _size_wi = _size_ls * _weights_max_width
    
    // TODO: rework error messages.
    switch
        (_size_i < 1)   => runtime.error('FunctionNNetwork -> network(): "inputs" has wrong size.')
        (_size_t < 1)   => runtime.error('FunctionNNetwork -> network(): "targets" has wrong size.')
        (_size_w < 1)   => runtime.error('FunctionNNetwork -> network(): "weights" has wrong size.')
        (_size_ls < 1)  => runtime.error('FunctionNNetwork -> network(): "layer_sizes" has wrong size.')
        (_size_lb < 1)  => runtime.error('FunctionNNetwork -> network(): "layer_biases" has wrong size.')
        (_size_lf < 1)  => runtime.error('FunctionNNetwork -> network(): "layer_functions" has wrong size.')
        (_size_t != array.get(layer_sizes, _size_ls - 1))  => runtime.error('FunctionNNetwork -> network(): "targets" size does not match output layer size in "layer_sizes"')
        (na(loss_function)) => runtime.error('FunctionNNetwork -> network(): "loss_function" is invalid.') //temp just for use sake
        (na(learning_rate)) => runtime.error('FunctionNNetwork -> network(): "learning_rate" is invalid.') //temp just for use sake
    
    // propagate forward:
    float[] _layer_outputs = propagate_forward(inputs, weights, layer_sizes, _weights_max_width)

    // error:
    // TODO: function selection:
    float _error_total = loss.mse(targets, array.slice(_layer_outputs, _total_nodes - (array.get(layer_sizes, _size_ls - 1)), _total_nodes))
    
    // propagate backward:
    // reference for delta rule: https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
    // node_delta_error = (nodeoutput - nodetarget) * derivative(nodeoutput)
    // update_weight = weight - learning_rate * node_error_delta
    //##########################################################################
    // Work in progress...
    //##########################################################################
    float[] _errors = array.new_float(0)
    float[] _error_deltas = array.new_float(_total_nodes)
    int _last_delta_idx = -1

    // iterate over the network backwards 
    for _l = (_size_ls - 1) to 0
        int _this_layer_size = array.get(layer_sizes, _l)
        int _number_of_nodes_up_to_this_layer = array.sum(array.slice(layer_sizes, 0, _l))

        bool _is_not_last_layer = _l != (_size_ls - 1)
        if _is_not_last_layer
            int _next_layer_size = array.get(layer_sizes, _l + 1)
            int _w_base_idx = _number_of_nodes_up_to_this_layer * _weights_max_width
            int _number_of_weights_in_layer = _this_layer_size * _next_layer_size
            // get current layer weights:
            float[] _layer_weights = array.slice(weights, _w_base_idx, _w_base_idx + _number_of_weights_in_layer)
            float[] _layer_error_deltas = array.slice(_error_deltas, _number_of_nodes_up_to_this_layer, _number_of_nodes_up_to_this_layer + _this_layer_size)
            propback_calculate_errors_for_hidden(_errors, _layer_weights, _layer_error_deltas)//, _next_layer_size, _number_of_nodes_up_to_this_layer)
        else
            int _output_layer_size = array.get(layer_sizes, _size_ls - 1)
            float[] _output_layer = array.slice(_layer_outputs, _total_nodes - _output_layer_size, _total_nodes)
            propback_calculate_errors_for_output (_errors, _output_layer, targets)

        // calculate the error delta of each node in current layer:
        // TODO: 
        //      - derivative based on the activation function of current layer.
        for _n = 0 to _this_layer_size - 1
            float _this_node = array.get(_layer_outputs, _number_of_nodes_up_to_this_layer + _n)
            _last_delta_idx += 1
            array.set(_error_deltas, _last_delta_idx, nz(array.get(_errors, _n), 1.0) * activation.sigmoid_derivative(_this_node))//###########################
    
    // update weights:
    // https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/
    // TODO:
    //      for each layer
    //          for each node in layer
    //              for each weight in ?*input layer*?
    // // // # Update network weights with error
    // // def update_weights(network, row, l_rate):
    // //     for i in range(len(network)):
    // //         inputs = row[:-1]
    // //         if i != 0:
    // //             inputs = [neuron['output'] for neuron in network[i - 1]]
    // //         for neuron in network[i]:
    // //             for j in range(len(inputs)):
    // //                 neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]
    // //             neuron['weights'][-1] += l_rate * neuron['delta']

    [_layer_outputs, _error_deltas, _errors]
//{ usage:
if barstate.islastconfirmedhistory
    // 7, 5 = i3, h5 h3 o2
    inputs = array.from(0.0, 1, 2)
    expected_outputs = array.from(1.0, 1.0)
    weights = generate_random_weights(10, 5)
    layer_sizes = array.from(5, 3, 2)
    layer_biases = array.from(1.0, 1.0, 1.0)
    layer_functions = array.from('sigmoid', 'sigmoid', 'sigmoid')
    [o, d, e] = network(inputs, expected_outputs, weights, layer_sizes, layer_biases, layer_functions, 'mse')
    label.new(bar_index, 0.0, str.format('output:{0}\ndelta:{1}\nerror:{2}\nweights:{3}, errors:{4}', str.tostring(o), str.tostring(d), str.tostring(e), array.size(weights), array.size(e)))
//{ remarks:
//}}}

Overview

FunctionNNetwork builds random weight arrays, runs a forward pass through stacked layers using an imported layer function, and begins a backward pass that compares output nodes with targets. Callers supply inputs, weights, layer sizes, and targets. The excerpt does not finish the error loop, notes a missing bias, and defines no plots, inputs, entries, or exits.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Library, Pine Script v5

Draws

Library (no plots)

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.

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

MLExtensions

by artificial-intelligence-edge
75 stars

MLExtensions is a Pine Script library providing utility and mathematical helper functions for machine learning indicators, specifically optimized for approximate nearest neighbors classification in Lorentzian space. It includes functions to normalize series de

Library v5 AI / machine learning
Updated 2 days ago · MIT GitHub repository of MLExtensions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

Zignaly2Beta

by ruckard
13 stars

This library provides helper functions to format and build JSON webhook alert payloads for the Zignaly automated trading platform. It constructs formatted messages containing parameters such as trading pair, exchange, account type, leverage, margin mode, hedge

Library v5
Updated 1 year ago · None stated GitHub repository of Zignaly2Beta

More libraries

Trade Your Charts With a Regulated Broker

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

Compare brokers