In this article, we will dive deep into how to use table.set_frame_color()
, understand its syntax, and explore its practical applications.
Syntax Overview
The syntax for table.set_frame_color()
is straightforward and consists of two parameters:
table.set_frame_color(table_id, frame_color) → void
table_id
(series table): This is a reference to the table object whose frame color you wish to set. The table object is created using thetable.new()
function.frame_color
(series color): Specifies the color of the table’s frame. This parameter is optional, with the default behavior being no color set to the frame.
Parameters Explained
- Table ID: The
table_id
parameter is essentially a variable that holds the table object. This object is generated when you create a new table in Pine Script usingtable.new()
. It’s important to note that each table in your script must have a unique identifier. - Frame Color: The
frame_color
parameter allows for customization of the table frame’s color. Pine Script supports a wide range of colors, and you can use built-in color constants likecolor.red
,color.blue
,color.green
, etc., or create custom colors using thecolor.new()
function. If this parameter is not specified, the table frame will not have a color, effectively making it transparent.
Example
Let’s illustrate how to use table.set_frame_color()
with a simple example. In this scenario, we’re creating a table and setting its frame color to red.
//@version=5 indicator("White Frame Table Example", overlay=true) // Creating a table with more rows and columns, with a semi-transparent background for contrast expandedTable = table.new(position = position.top_right, columns = 3, rows = 2, bgcolor=color.new(color.white, 90)) // Setting the frame color of the table to white table.set_frame_color(expandedTable, color.white) // Populating the table with data, using red for headers for coherence with the white frame table.cell(expandedTable, 0, 0, "Header 1", bgcolor=color.red, text_color=color.white) table.cell(expandedTable, 1, 0, "Header 2", bgcolor=color.red, text_color=color.white) table.cell(expandedTable, 2, 0, "Header 3", bgcolor=color.red, text_color=color.white) table.cell(expandedTable, 0, 1, "Data 1", text_color=color.rgb(255, 0, 0)) table.cell(expandedTable, 1, 1, "Data 2", text_color=color.rgb(255, 0, 0)) table.cell(expandedTable, 2, 1, "Data 3", text_color=color.rgb(255, 0, 0))
In this example, we first create a table object named myTable
using the table.new()
function, positioning it at the top right of the chart with one row and two columns. Then, we use table.set_frame_color()
to set the frame color of myTable
to red.
Line-by-Line Explanation
- The code begins with
//@version=5
, indicating the version of Pine Script being used (version 5 in this case). indicator("White Frame Table Example", overlay=true)
is used to define an indicator titled “White Frame Table Example”. The parameteroverlay=true
indicates that the indicator will be overlaid on the price chart.expandedTable = table.new(position = position.top_right, columns = 3, rows = 2, bgcolor=color.new(color.white, 90))
creates a new table object. It specifies the position of the table to be at the top right corner of the chart (position.top_right
), with 3 columns and 2 rows. The background color of the table is set to white with 90% opacity.table.set_frame_color(expandedTable, color.white)
sets the frame color of the table to white, ensuring it matches the background color.- Header cells are populated using
table.cell()
function. Each call totable.cell()
specifies the table object (expandedTable
), row index, column index, cell content, background color, and text color.
Key Features and Takeaways
- Function Usability:
table.set_frame_color()
is used to modify the aesthetic of table frames in Pine Script, allowing for better visual differentiation or integration with the chart’s theme. - Syntax and Application: The function requires a table object and an optional color parameter. It’s a void function, meaning it doesn’t return any value but alters the appearance of the table.
- Practical Use: This function is particularly useful in scripts where multiple tables are used, and there’s a need to distinguish them visually or match them to the chart or branding colors.