Introducing Pine tables

May 25, 2021

Tables are objects that can be used to position information in specific and fixed locations in a script’s visual space. Contrary to all other plots or objects drawn in Pine, tables are not anchored to specific bars: they float in a script’s space, whether in overlay or pane mode, in studies or strategies, independently of the chart bars being viewed or the zoom factor used.

This new functionality enables you to present data in a way that has never been possible before. You can create an auto-generated display panel with relevant data for the current symbol and pin it to the side of your screen, or create a dynamic heatmap, or provide quick access to any information available in Pine — the possibilities are endless. Needless to say, tables can be used in conjunction with all other PineScript features to further improve your existing indicators.

Go to the Pine User Manual’s page on tables to jump right in and start creating tables.

This performance table, inspired by our sidebar’s “Performance” section, helps you track the performance of the chart’s symbol:

//@version=4
study("Performance", overlay=true)

var table perfTable = table.new(position.top_right, 3, 2, border_width = 3)

LIGHTTRANSP = 90
AVGTRANSP   = 80
HEAVYTRANSP = 70

i_posColor = input(color.rgb(38, 166, 154), title="Positive Color")
i_negColor = input(color.rgb(240, 83, 80), title="Negative Color")

f_rateOfreturn(_v1, _v2) => (_v1 - _v2) * 100 / abs(_v2)

f_performance(_barsBack) =>
    _performance = security(syminfo.tickerid, "1D", f_rateOfreturn(close, close[_barsBack]))
    _performance
   
lastYearClose = security(syminfo.tickerid, "12M", close[1], lookahead=barmerge.lookahead_on)

f_fillCell(_table, _column, _row, _value, _timeframe) =>
    _c_color = _value >= 0 ? i_posColor : i_negColor
    _transp = abs(_value) > 10 ? HEAVYTRANSP : abs(_value) > 5 ? AVGTRANSP : LIGHTTRANSP
    _cellText = tostring(_value, "#.##") + "%\n" + _timeframe
    table.cell(_table, _column, _row, _cellText, bgcolor = color.new(_c_color, _transp), text_color = _c_color, width = 6)

if barstate.islast
    f_fillCell(perfTable, 0, 0, f_performance(5), "1W")
    f_fillCell(perfTable, 1, 0, f_performance(21), "1M")
    f_fillCell(perfTable, 2, 0, f_performance(63), "3M")
    f_fillCell(perfTable, 0, 1, f_performance(126), "6M")
    f_fillCell(perfTable, 1, 1, f_rateOfreturn(close, lastYearClose), "YTD")
    f_fillCell(perfTable, 2, 1, f_performance(251), "1Y")

The following examples show tables used in scripts by the PineCoders who help us test new Pine features:

Indicator Panel by LonesomeTheBlue

TA Information Panel by RicardoSantos:

Correlation Matrix Heatmap by RicardoSantos:

And, if you ever want to take a break from trading, you can use Pine tables to play chess with your friends:

//@version=4
study("Chess", overlay=true)

// Coordinate conversion system
var a_xString = array.from(na, "a", "b", "c", "d", "e", "f", "g", "h")
var a_yString = array.from(na, "8", "7", "6", "5", "4", "3", "2", "1")

// Creating base table
f_even(_num) =>
    _isEven = true
    if _num % 2 == 0
        _isEven := false
    _isEven
   
f_cellColor(_column, _row) =>
    color _cellColor = color.white
    if f_even(_column)
        if not f_even(_row)
            _cellColor := color.gray
    else
        if f_even(_row)
            _cellColor := color.gray
    _cellColor
   
PADDING = "◯"

var table chessBoard = table.new(position.middle_right, 10, 10, frame_color = color.black, 
     border_color = color.black, frame_width = 3, border_width = 1, bgcolor = color.black)

if barstate.isfirst
    for _column = 1 to 8
        for _row = 1 to 8
            table.cell(chessBoard, _column, _row, text = PADDING, bgcolor = f_cellColor(_column, _row), text_size = size.huge, text_color = #00ff0000)

    for _row = 1 to 8
        table.cell(chessBoard, 0, int(abs(9 - _row)), text = tostring(_row), text_size = size.small, text_color = color.white)
        table.cell(chessBoard, 9, int(abs(9 - _row)), text = tostring(_row), text_size = size.small, text_color = color.white)

    for _column = 1 to 8
        table.cell(chessBoard, _column, 0, text = array.get(a_xString, _column), text_size = size.small, text_color = color.white)
        table.cell(chessBoard, _column, 9, text = array.get(a_xString, _column), text_size = size.small, text_color = color.white)

// Two letters before the input signify the piece color/type

// White pieces
var i_whitePawn1 = "wp" + input("a2", "", group="White Pawns", inline="White Pawns #1")
var i_whitePawn2 = "wp" + input("b2", "", group="White Pawns", inline="White Pawns #1")
var i_whitePawn3 = "wp" + input("c2", "", group="White Pawns", inline="White Pawns #1")
var i_whitePawn4 = "wp" + input("d2", "", group="White Pawns", inline="White Pawns #1")
var i_whitePawn5 = "wp" + input("e2", "", group="White Pawns", inline="White Pawns #2")
var i_whitePawn6 = "wp" + input("f2", "", group="White Pawns", inline="White Pawns #2")
var i_whitePawn7 = "wp" + input("g2", "", group="White Pawns", inline="White Pawns #2")
var i_whitePawn8 = "wp" + input("h2", "", group="White Pawns", inline="White Pawns #2")

var i_whiteRook1   = "wr" + input("a1", inline="White Rooks"  , group="White Pieces", title="Rooks   ") 
var i_whiteRook2   = "wr" + input("h1", inline="White Rooks"  , group="White Pieces", title="") 
var i_whiteKnight1 = "wn" + input("b1", inline="White Knights", group="White Pieces", title="Knights")
var i_whiteKnight2 = "wn" + input("g1", inline="White Knights", group="White Pieces", title="")
var i_whiteBishop1 = "wb" + input("c1", inline="White Bishops", group="White Pieces", title="Bishops")
var i_whiteBishop2 = "wb" + input("f1", inline="White Bishops", group="White Pieces", title="")
var i_whiteQueen   = "wq" + input("d1", inline="White Queen"  , group="White Pieces", title="Queen  ")
var i_whiteKing    = "wk" + input("e1", inline="White King"   , group="White Pieces", title="King      ")

var a_whitePieces = array.from(i_whitePawn1, i_whitePawn2, i_whitePawn3, i_whitePawn4, i_whitePawn5, i_whitePawn6, i_whitePawn7, i_whitePawn8, 
                         i_whiteRook1, i_whiteRook2, i_whiteKnight1, i_whiteKnight2, i_whiteBishop1, i_whiteBishop2, i_whiteQueen, i_whiteKing)

// Black pieces
var i_blackPawn1 = "bp" + input("a7", "", group="Black Pawns", inline="Black Pawns #1")
var i_blackPawn2 = "bp" + input("b7", "", group="Black Pawns", inline="Black Pawns #1")
var i_blackPawn3 = "bp" + input("c7", "", group="Black Pawns", inline="Black Pawns #1")
var i_blackPawn4 = "bp" + input("d7", "", group="Black Pawns", inline="Black Pawns #1")
var i_blackPawn5 = "bp" + input("e7", "", group="Black Pawns", inline="Black Pawns #2")
var i_blackPawn6 = "bp" + input("f7", "", group="Black Pawns", inline="Black Pawns #2")
var i_blackPawn7 = "bp" + input("g7", "", group="Black Pawns", inline="Black Pawns #2")
var i_blackPawn8 = "bp" + input("h7", "", group="Black Pawns", inline="Black Pawns #2")

var i_blackRook1   = "br" + input("a8", inline="Black Rooks"  , group="Black Pieces", title="Rooks   ") 
var i_blackRook2   = "br" + input("h8", inline="Black Rooks"  , group="Black Pieces", title="") 
var i_blackKnight1 = "bn" + input("b8", inline="Black Knights", group="Black Pieces", title="Knights")
var i_blackKnight2 = "bn" + input("g8", inline="Black Knights", group="Black Pieces", title="")
var i_blackBishop1 = "bb" + input("c8", inline="Black Bishops", group="Black Pieces", title="Bishops")
var i_blackBishop2 = "bb" + input("f8", inline="Black Bishops", group="Black Pieces", title="")
var i_blackQueen   = "bq" + input("d8", inline="Black Queen"  , group="Black Pieces", title="Queen  ")
var i_blackKing    = "bk" + input("e8", inline="Black King"   , group="Black Pieces", title="King      ")

var a_blackPieces = array.from(i_blackPawn1, i_blackPawn2, i_blackPawn3, i_blackPawn4, 
 i_blackPawn5, i_blackPawn6, i_blackPawn7, i_blackPawn8, i_blackRook1, i_blackRook2, 
 i_blackKnight1, i_blackKnight2, i_blackBishop1, i_blackBishop2, i_blackQueen, i_blackKing)

f_getIcon(_str) =>
    var _pieces = array.from( "k", "♔", "♚", 
                             "q", "♕", "♛",
                             "r", "♖", "♜",
                             "b", "♗", "♝",
                             "n", "♘", "♞",
                             "p", "♙", "♟")

    _split = str.split(_str, "")
   
    if array.size(_split) == 4
        _color = array.get(_split, 0)
        _piece = array.get(_split, 1)
        _piece_offset = _color == "w" ? 1 : _color == "b" ? 2 : na
        array.get(_pieces, array.indexof(_pieces, _piece) + _piece_offset)

f_placePiece(_piece_input) =>
    _split = str.split(_piece_input, "")
    if array.size(_split) == 4
        _icon = f_getIcon(_piece_input)
        _x = array.indexof(a_xString, array.get(_split, 2))
        _y = array.indexof(a_yString, array.get(_split, 3))
        table.cell_set_text(chessBoard, _x, _y, _icon)
        table.cell_set_text_color(chessBoard, _x, _y, color.black)

if barstate.islast
    for i = 0 to array.size(a_whitePieces) - 1
        f_placePiece(array.get(a_whitePieces, i))
   
    for i = 0 to array.size(a_blackPieces) - 1
        f_placePiece(array.get(a_blackPieces, i))

We hope this new feature you requested is useful. Please continue sending us your suggestions for improvement. We build TradingView for you and love receiving your feedback.

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart