pine-scriptpine-script-v5

pinescript 5- runtime error with table - column out of bounds


below is my table with 3 columns and 6 rows-

// Create and configure the table
var tbl = table.new(position.top_right, 3, 6, border_width=1, border_color=color.gray, bgcolor=color.new(color.black, 90))

// Populate table headers (executed only once)
if barstate.islast
    table.cell(tbl, 0, 0, "Symbol", bgcolor=color.new(color.blue, 80), text_color=color.white)
    table.cell(tbl, 0, 1, "Prev Value", bgcolor=color.new(color.blue, 80), text_color=color.white)
    table.cell(tbl, 0, 2, "Today Value", bgcolor=color.new(color.blue, 80), text_color=color.white)

// Populate table rows
if barstate.islast
    table.cell(tbl, 1, 0, symbol1, text_color=color.white)
    table.cell(tbl, 1, 1, str.tostring(prevVal1), text_color=color.white)
    table.cell(tbl, 1, 2, str.tostring(todayVal1), text_color=color.white)

    table.cell(tbl, 2, 0, symbol2, text_color=color.white)
    table.cell(tbl, 2, 1, str.tostring(prevVal2), text_color=color.white)
    table.cell(tbl, 2, 2, str.tostring(todayVal2), text_color=color.white)

    table.cell(tbl, 3, 0, symbol3, text_color=color.white)
    table.cell(tbl, 3, 1, str.tostring(prevVal3), text_color=color.white)
    table.cell(tbl, 3, 2, str.tostring(todayVal3), text_color=color.white)

    table.cell(tbl, 4, 0, symbol4, text_color=color.white)
    table.cell(tbl, 4, 1, str.tostring(prevVal4), text_color=color.white)
    table.cell(tbl, 4, 2, str.tostring(todayVal4), text_color=color.white)

    table.cell(tbl, 5, 0, symbol5, text_color=color.white)
    table.cell(tbl, 5, 1, str.tostring(prevVal5), text_color=color.white)
    table.cell(tbl, 5, 2, str.tostring(todayVal5), text_color=color.white)

I am getting run time error - Column 3 is out of table bounds, number of coulmns is 3. I am unable to trace what is wrong with this code.


Solution

  • It appears that the columns and rows arguments in the table declaration are switched. The script is attempting to populate 6 columns and 3 rows, while the table is defined with 3 columns and 6 rows. Replace:

    var tbl = table.new(position.top_right, 3, 6, border_width=1, border_color=color.gray, bgcolor=color.new(color.black, 90))
    

    With

    var tbl = table.new(position.top_right, 6, 3, border_width=1, border_color=color.gray, bgcolor=color.new(color.black, 90))
    

    To see the result:

    result