Search code examples
pine-scriptuser-defined-functionspine-script-v5

Does Pine Script support calling a user function inside a table?


I have a table in Pine Script with several rows, and each row requires around 20+ lines of code. I made a user function to create the rows but I'm having trouble getting it to work.

Does Pine Script support calling a user function inside a table, i.e. to build the table rows?


Solution

  • Yes it does. Here is a very simple script that uses a function to create the table cells and then also calls another function inside of that function to multiply the closing price by 2.

    //@version=5
    indicator("My Table", overlay = true)
    
    //create the table
    var myTable = table.new(position.top_right, 2, 2, bgcolor = color.aqua, frame_color = color.black, frame_width = 2, border_color = color.black, border_width = 2)
    
    createCell(_table, _column, _row, _text) =>
        table.cell(_table, _column, _row, _text, text_color = color.white)
    
    multiplier(_close) => _close * 2 
    
    createCell(myTable, 0, 0, 'Symbol')
    createCell(myTable, 0, 1, str.tostring(syminfo.tickerid))
    createCell(myTable, 1, 0, 'Closing Price * 2')
    createCell(myTable, 1, 1, str.tostring(multiplier(close), format.mintick))
    

    Picture of the result Table top right