Tables
Draw a table of numbers in a corner of the chart. How to build it once on the last bar, fill its cells, colour them, and the limits on size.
A table is a grid of text cells fixed to a corner of the chart. It is the usual way to show a dashboard: a few numbers, the state of a signal, the result of a calculation.
Build it on the last bar#
A table does not move with the bars, so it is built once, on the newest bar. table.new(position, columns, rows) makes the grid, and table.cell(table, column, row, text) writes a cell. Columns and rows are counted from 0.
indicator "Dashboard"
edition = 1
display = price
input length: int = 20, name = "Length", min = 2
let average = tech.sma(close, length)
let strength = tech.rsi(close, 14)
if bar.is_last
let t = table.new(
top_right,
2,
3,
background = color.fade(color.black, 70),
border_color = color.gray,
border_width = 1,
)
let names = ["Close", "Average", "RSI"]
let values = [close, average, strength]
for i in 0 to 2
table.cell(t, 0, i, names.at(i), text_color = color.silver, h_align = left)
table.cell(t, 1, i, f"{values.at(i):.2f?'n/a'}", text_color = color.white)
plot(average, name = "Average")The loop writes one row for each name. In the second column {...:.2f?'n/a'} writes the number with two decimals, or n/a while a value is missing. Text and number formatting covers the formats.
position is one of top_left, top_center, top_right, middle_left, middle_center, middle_right, bottom_left, bottom_center or bottom_right.
Colour and size#
A cell takes text_color, background, text_size (auto, tiny, small, normal, large, huge), h_align (left, center, right) and v_align (top, bottom). The colour can depend on a value:
indicator "Signal cell"
edition = 1
display = price
if bar.is_last
let t = table.new(bottom_left, 1, 1)
table.cell(
t,
0,
0,
if close > open then "Up bar" else "Down bar",
text_color = color.white,
background = if close > open then color.green else color.red,
text_size = large,
)
plot(close)Limits#
- A table has at most 100 columns, 500 rows and 10,000 cells. All the live tables of a script together hold at most 20,000 cells.
- The chart keeps the newest 40 tables of a script. If the script makes a new table on every bar, the older ones are dropped and the engine says how many; build the table once, on
bar.is_last, or delete the old one before you make another. - A cell outside the table stops the script with a message that names the cell and the line.
- A text in a cell is cut at 2,000 characters.
indicator "A cell outside the table"
edition = 1
display = price
if bar.is_last
let t = table.new(top_right, 2, 2)
table.cell(t, 5, 0, "x")
plot(close)See also: Plots and drawings, Text and number formatting, Limits and budgets.