One of the many interesting features it offers is the ability to create tables using the table.new
function. In this blog, we will delve into the specifics of this function and how to use it effectively.
Understanding the table.new Function
What is table.new?
table.new
is a function that creates a new table in Pine Script. The basic syntax for this function is as follows:
table.new(position, columns, rows, bgcolor, frame_color, frame_width, border_color, border_width) → series table
This function returns the ID of a table object that can be passed to other table.*() functions.
Arguments of table.new
The table.new
function takes the following arguments:
position
: Position of the table. Possible values range from top_left, top_center, top_right to middle_left, middle_center, middle_right, and bottom_left, bottom_center, bottom_right.columns
: The number of columns in the table.rows
: The number of rows in the table.bgcolor
: The background color of the table. This is optional, and the default is no color.frame_color
: The color of the outer frame of the table. This is also optional, and the default is no color.frame_width
: The width of the outer frame of the table. Again, this is optional, and the default is 0.border_color
: The color of the borders of the cells (excluding the outer frame). This is optional, and the default is no color.border_width
: The width of the borders of the cells (excluding the outer frame). This is optional, and the default is 0.
Remarks
This function creates the table object itself, but the table will not be displayed until its cells are populated. To define a cell and change its contents or attributes, use table.cell
and other table.cell_*()
functions.
For performance reasons, it is advisable to use table.new
in conjunction with either the var
keyword or in an if barstate.islast
block.
A Practical Example
Here is a practical example of how the table.new
function is used in Pine Script:
//@version=5 indicator("table.new example") var testTable = table.new(position = position.middle_right, columns = 2, rows = 1, bgcolor = color.yellow, border_width = 1) if barstate.islast table.cell(table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open)) table.cell(table_id = testTable, column = 1, row = 0, text = "Close is " + str.tostring(close), bgcolor=color.teal)
In this example, we first create a new table with two columns and one row, placed at the top right of the chart. The background color of the table is yellow, and the border width is 1. We save the table’s ID in the testTable
variable.
Next, we use if barstate.islast
to ensure the following operations only occur on the last bar. In these operations, we populate the table’s cells using table.cell
. The first cell (0, 0) displays the open price, while the second cell (1, 0) displays the close price with a teal background color.
Key Takeaway
The table.new
function in Pine Script is a versatile and effective tool that allows users to create and customize tables to fit their trading needs. By understanding and utilizing this function, users can enhance their data presentation and trading strategies. Remember to use the var
keyword or the if barstate.islast
block for better performance when using table.new
.
Conclusion
Pine Script provides an array of robust functions for technical analysis and table.new is one of them. Mastering this function can help you to create more visually engaging and informative tools, and possibly unlock new ways to present and analyze your trading data. Continue exploring Pine Script to see what else you can achieve.