Indicator · Pine Script v4 · Own pane

Matrix functions - test

by JorisDuyck

This script provides utility functions to treat one-dimensional Pine Script arrays as two-dimensional matrices.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code137
Settings0inputs
LicenceMIT
Last update5 years ago25 Feb 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

137 lines of Pine Script v4 from array2D info-set-get-indexing functions.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

array2D info-set-get-indexing functions.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Joris Duyck
// Various functions to use 1D Pinescript arrays as 2D matrices
//
// The arrays are declared in the following way:
// (header with dimensional metadata)  (body of the matrix, containing the actual data)
// [number_of_columns, number_of_rows, value(x0,y0), value(x1,y0), value(x2,y0), value(x0,y1), value(x1,y1),...]
//
//
// The functions here can be used to:
// - retrieve dimensions info from the array
// - initialise an array with a header containing the dimensional info
// - set values in the array with (x, y) coordinates
// - get values in the array with (x, y) coordinates
// - convert 1d array indeces to (x, y) coordinates and vice versa

//@version=4
study("Matrix functions -  test")

/// MATRIX INFO FUNCTIONS ///
//{

// OK! function to get total number of columns from a 2D matrix
//{
f_array2D_get_columns(_m) =>
    _columns = int(array.get(_m, 0))
    _columns
//}

// OK! function to get total number of rows from a 2D matrix
//{
f_array2D_get_rows(_m) =>
    _rows    = int(array.get(_m, 1))
    _rows
//}

// OK! function to get total number of columns and rows from a 2D matrix
//{
f_array2D_get_size(_m) =>
    _columns = int(array.get(_m, 0))
    _rows    = int(array.get(_m, 1))
    [_columns, _rows]
//}
//}

/// INITIALISATION AND SET FUNCTIONS ///
//{
// 2D matrix init function, builds 2D matrix with dimensional metadata in first two values
//{
f_array2D_init(_x, _y, _fill) =>
    _size  = int(_x * _y)
    _m     = array.new_float(_size + 2, _fill)
    array.set(_m, 0, _x), array.set(_m, 1, _y)
    _m
//}

// function to set values in 2D matrix with (x, y) (index for x and y both starts at 0 !!)
//{
f_array2D_set(_m, _x, _y, _val) =>
    _columns = f_array2D_get_columns(_m)
    array.set(_m, 2 + int(_x + (_y * _columns)), _val)
//}
//}

/// GET FUNCTIONS ///
//{
// function to get values from 2D matrix with (x, y) (index for x and y both starts at 0 !!)
//{
f_array2D_get(_m, _x, _y) =>
    _columns = f_array2D_get_columns(_m)
    _val     = array.get(_m, 2 + int(_x + (_y * _columns)))
    _val
//}

// function to cut of the metadata header and return the array body
//{
f_array_get_body(_m) =>
    _array_body = array.copy(_m)
    for i = 0 to 1
        array.remove(_array_body, 0)
    _array_body
//}

/// 1D <=> 2D COORDINATE CONVERSION FUNCTIONS (handy with for loop indexes) ///
//{
// OK! function to get x coordinate from 2D matrix from 1D index (index for x and y both starts at 0 !!)
//{
f_array2D_get_x(_m, _index) =>
    _columns = f_array2D_get_columns(_m)
    _x       = _index % _columns
    _x
//}

// OK! function to get y coordinate in 2D matrix from 1D index (index for x and y both starts at 0 !!)
//{
f_array2D_get_y(_m, _index) =>
    _columns = f_array2D_get_columns(_m)
    _y       = floor(_index / _columns)
    _y
//}

// OK! function to get( x, y) coordinates in 2D matrix from 1D index (index starts at 0 and does not include the header!!)
//{
f_array2D_get_xy(_m, _index) =>
    _columns = f_array2D_get_columns(_m)
    _x = (_index % _columns)
    _y = floor(_index / _columns)
    [_x, _y]
//}

// OK! function to get 1D index from ( x, y) coordinates in 2D matrix (index for x and y both starts at 0 !! Index starts at 0 and does not include the header!!)
//{
f_array2D_get_index(_m, _x, _y) =>
    _columns = f_array2D_get_columns(_m)
    _index   = _x + _y * _columns
//}
//}
//}

// OK! function to print matrix
//{
f_array2D_print(_m) =>
    _text = "(" + tostring(array.get(_m, 0)) + "x" + tostring(array.get(_m, 1)) + ")"
    _text := _text + "\n" + "\n"+"["
    for i = 2 to array.size(_m) - 1 
        _text := _text + (i == 2 ? "" : ((i -2) % array.get(_m, 0)) == 0 ? "]" + "\n" + "[" : " ") + tostring(array.get(_m, i))
    _text := _text + "]"
//}

/// ARRAY SET AND PRINT EXAMPLE //

a = f_array2D_init(2, 3, 0)
for i = 0 to 5
    array.set(a, 2 + i, i)

var label lbl_a = na, label.delete(lbl_a)
lbl_a := label.new(bar_index - 0, 0, text = f_array2D_print(a))

Overview

Matrix functions - test includes functions to get the number of columns and rows from a matrix, initialize a new matrix with given dimensions and a fill value, set values at specific x-y coordinates, retrieve values at coordinates, and convert between one-dimensional indices and two-dimensional coordinates. The functions support matrix operations and data organization in arrays. The script is written in Pine Script version 4 and does not plot any data directly.

Details

Published by

JorisDuyck on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

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

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
549 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

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