TradeOnReplayDOCSSearch the whole guidetradeonreplay.com

Guide / Scripts

Maps and matrices

A map looks values up by key. A matrix is a grid of rows and columns.

//@version=5
indicator("Up / down days")

var tally = map.new<string, int>()
key = close > open ? "up" : "down"
tally.put(key, nz(tally.get(key)) + 1)

plot(nz(tally.get("up")), "Up", color.green)
plot(nz(tally.get("down")), "Down", color.red)

Maps

map.new<string, float>() with put get contains remove clear size keys values copy put_all.

  • put and remove give back the old value, or na. get gives na for a missing key.
  • Keys are numbers, strings, bools or colours, never na.
  • for [key, value] in m loops over entries in the order they were added.
  • At most 100,000 entries.

Matrices

matrix.new<float>(rows, columns, initial) with get set rows columns elements_count row col add_row add_col remove_row remove_col swap_rows swap_columns fill copy transpose reshape sum diff mult det inv trace avg min max.

  • Rows and columns count from 0. An index outside the matrix is an error that names the size.
  • mult takes a matrix, an array (matrix times vector, gives an array) or a number.
  • sum and diff take a matrix of the same size, or a number.
  • inv of a singular matrix is an error.
  • At most 100,000 elements.

Method form

Method calls work on everything that has a collection or drawing object: m.put(k, v), x.get(r, c), tbl.cell(0, 0, "x"), lbl.set_text("y"). When a variable was created by table.new, map.new, array.new... and so on, the call is checked with that type's argument names. Otherwise the function is chosen from the value when it runs, with positional arguments only.