Search code examples
pine-scriptpine-script-v5

Why does this function display a single column instead of 5?


Using a function, I try to display a 5-column table, but it only displays one column, i.e. the last function call.

I think that each time the function is called, it creates a new table

I'm trying to keep a single loop instead of duplicating it 5 times

Do you know a workaround?

arr_list1 = array.from('1', '1', '1', '1', '1', '1')
arr_list2 = array.from('2', '2', '2', '2', '2', '2')
arr_list3 = array.from('3', '3', '3', '3', '3', '3')
arr_list4 = array.from('4', '4', '4', '4', '4', '4')
arr_list5 = array.from('5', '5', '5', '5', '5', '5')
arr_list6 = array.from('6', '6', '6', '6', '6', '6')

table(_arr_list, _column) =>
    var tab = table.new(position=position.top_right, columns=7, rows=41, border_width=3, frame_width=1)
    
    if barstate.islast
        //table.clear(tab, 0, 1, 6, 40)
        table.cell(table_id=tab, column=0, row=0, text='txt1', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)
        table.cell(table_id=tab, column=1, row=0, text='txt2', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)
        table.cell(table_id=tab, column=2, row=0, text='txt3', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)
        table.cell(table_id=tab, column=3, row=0, text='txt4', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)
        table.cell(table_id=tab, column=4, row=0, text='txt5', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)
        table.cell(table_id=tab, column=5, row=0, text='txt6', text_size=size.large, bgcolor=color.new(gray, 90), text_color=gray)

    if barstate.isrealtime
        for i = 0 to array.size(_arr_list) - 1 by 1  
            table.cell(table_id=tab, column=_column, row=i+1, text=str.tostring(array.get(_arr_list, i)), text_size=size.large, text_color=color.white, bgcolor=color.new(color.gray, 90))

table(arr_list1, 0)
table(arr_list2, 1)
table(arr_list3, 2)
table(arr_list4, 3)
table(arr_list5, 4)
table(arr_list6, 5)

Solution

  • because var tab is a local variable and it being destroyed after each call (https://en.wikipedia.org/wiki/Closure_(computer_programming))

    Use this

    //@version=5
    indicator("")
    arr_list1 = array.from('1', '1', '1', '1', '1', '1')
    arr_list2 = array.from('2', '2', '2', '2', '2', '2')
    arr_list3 = array.from('3', '3', '3', '3', '3', '3')
    arr_list4 = array.from('4', '4', '4', '4', '4', '4')
    arr_list5 = array.from('5', '5', '5', '5', '5', '5')
    arr_list6 = array.from('6', '6', '6', '6', '6', '6')
    
    
    var tab = table.new(position=position.top_right, columns=7, rows=41, border_width=3, frame_width=1)
    
    table(table tab, _arr_list, _column) =>
         
        if barstate.islast
            //table.clear(tab, 0, 1, 6, 40)
            table.cell(table_id=tab, column=0, row=0, text='txt1', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
            table.cell(table_id=tab, column=1, row=0, text='txt2', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
            table.cell(table_id=tab, column=2, row=0, text='txt3', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
            table.cell(table_id=tab, column=3, row=0, text='txt4', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
            table.cell(table_id=tab, column=4, row=0, text='txt5', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
            table.cell(table_id=tab, column=5, row=0, text='txt6', text_size=size.large, bgcolor=color.new(color.gray, 90), text_color=color.gray)
    
        if barstate.isrealtime
            for i = 0 to array.size(_arr_list) - 1 by 1  
                table.cell(table_id=tab, column=_column, row=i+1, text=str.tostring(array.get(_arr_list, i)), text_size=size.large, text_color=color.white, bgcolor=color.new(color.gray, 90))
    
    table(tab, arr_list1, 0)
    table(tab, arr_list2, 1)
    table(tab, arr_list3, 2)
    table(tab, arr_list4, 3)
    table(tab, arr_list5, 4)
    table(tab, arr_list6, 5)