0

I would like to make a matrix with 3 dimensions for storing a grid of values overtime.

ive tried making multiple matrixs but caling them is tricky and i would like to have it easily scalable

1 Answers1

0

The only solution I've found so far, is making a user defined type, and make an object that have few properties. It gets a bit complicated to code, but it works.

Here is a quick example:

//@version=5
indicator("My script")

// creating UDT
type bbT
    float middle = na
    float upper = na
    float lower = na

// creating new objects
bb1 = bbT.new()
bb2 = bbT.new()
bb3 = bbT.new()

float basis = ta.sma(close, 20)
float dev = 2 * ta.stdev(close, 20)

bb1.middle := basis
bb1.upper := basis + dev
bb1.lower := basis - dev

bb2.middle := basis
bb2.upper := basis + dev
bb2.lower := basis - dev

bb3.middle := basis
bb3.upper := basis + dev
bb3.lower := basis - dev

// creating a matrix
myArray = array.from(bb1, bb2, bb3)
myMatrix = matrix.new<bbT>(1, 3)

matrix.add_row(myMatrix, 0, myArray)

// calling the properties, within the elements of the matrix
a = matrix.get(myMatrix, 0, 0)
b = matrix.get(myMatrix, 0, 1)
c = matrix.get(myMatrix, 0, 2)

if barstate.islast
    label.new(bar_index, high, str.tostring(a.upper))
mr_statler
  • 1,913
  • 2
  • 3
  • 14