Home » Table Functions » Understanding the table.cell_set_height() Function in Pine Script

Understanding the table.cell_set_height() Function in Pine Script

Photo of author
Published on

Pine Script provides a rich set of functions allowing users to customize tables for displaying data on charts. One such function is table.cell_set_height(), which adjusts the height of a specific cell within a table. This tutorial will explore the syntax, arguments, and practical applications of this function.

Syntax

The table.cell_set_height() function is designed to modify the height of a cell within a table. Its syntax is as follows:

table.cell_set_height(table_id, column, row, height) → void

This function does not return any value (hence, void), but it directly affects the table’s appearance on the chart.

Arguments

  • table_id (series table): This is the identifier for the table object you want to modify. It links the function call to a specific table created earlier in your script.
  • column (series int): Specifies the column index of the cell whose height you wish to set. Column indexing starts at 0, meaning the first column is referenced as 0.
  • row (series int): Indicates the row index of the cell. Similar to columns, row indexing also starts at 0.
  • height (series int/float): Determines the new height of the cell as a percentage of the chart window. Setting this argument to 0 will make the cell height auto-adjust to fit the text inside.

Example

Imagine we have a table designed to display trading signals, and we want to adjust the height of a specific cell to emphasize its content.

First, let’s create a table and then modify the height of a cell:

//@version=5
indicator("My Table Example", overlay=true)

// Creating a table
myTable = table.new(position.top_right, 3, 2)

// Populating the table with some data
table.cell(myTable, 0, 0, "Signal")
table.cell(myTable, 1, 0, "Buy")
table.cell(myTable, 0, 1, "Price")
table.cell(myTable, 1, 1, tostring(close))

// Adjusting the height of the 'Price' cell
table.cell_set_height(myTable, 0, 1, 20)

plot(close)
Example

Walkthrough of Code

  1. //@version=5: This indicates the version of Pine Script being used, which is version 5 in this case.
  2. indicator("My Table Example", overlay=true): This line declares the script as an indicator with the title “My Table Example” and sets the overlay parameter to true, meaning it will be plotted on the main chart.
  3. myTable = table.new(position.top_right, 3, 2): This line creates a new table with 3 rows and 2 columns positioned at the top-right corner of the chart.
  4. table.cell(myTable, 0, 0, "Signal"): This line sets the content of the cell in the first row and first column of the table to “Signal”.
  5. table.cell(myTable, 1, 0, "Buy"): This line sets the content of the cell in the second row and first column of the table to “Buy”.
  6. table.cell(myTable, 0, 1, "Price"): This line sets the content of the cell in the first row and second column of the table to “Price”.
  7. table.cell(myTable, 1, 1, tostring(close)): This line sets the content of the cell in the second row and second column of the table to the string representation of the close price.
  8. table.cell_set_height(myTable, 0, 1, 20): This line adjusts the height of the cell in the first row and second column of the table to 20 pixels.
  9. plot(close): This line plots the closing price on the chart.

Key Features and Takeaways

  • Function Usability: The table.cell_set_height() function is crucial for customizing the visual representation of tables in Pine Script, offering flexibility in displaying data.
  • Syntax and Application: Understanding the function’s arguments is key to accurately targeting and adjusting cell heights within your tables.
  • Practical Use Case: This function is particularly useful in scenarios where certain data points need emphasis or better visibility within a table.

By mastering the table.cell_set_height() function, you can enhance the readability and visual appeal of your tables in Pine Script, making your trading indicators and strategies more informative and user-friendly.

Leave a Comment