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

Understanding the table.cell_set_text_color() Function in Pine Script

Photo of author
Published on

The table.cell_set_text_color() function in Pine Script allows developers to dynamically change the color of text within a cell of a table. This functionality can be used to highlight, differentiate, or categorize data based on certain conditions or criteria.

Syntax and Arguments

The syntax of the table.cell_set_text_color() function is straightforward, yet it offers flexibility for customization:

table.cell_set_text_color(table_id, column, row, text_color) → void

Let’s break down each argument to understand its role:

  • table_id (series table): This argument requires you to pass the identifier of the table you wish to modify. The table must have been previously created using the table.new() function.
  • column (series int): This specifies the index of the column containing the cell whose text color you want to change. Indexing starts at 0, meaning that the first column is represented by 0.
  • row (series int): Similar to the column index, this argument specifies the row index of the cell. It also starts at 0, with 0 representing the first row.
  • text_color (series color): Here, you define the color that the text inside the specified cell should change to. Pine Script supports a variety of color constants, or you can use color functions to generate a color dynamically.


To illustrate the use of table.cell_set_text_color(), let’s consider a simple example where we create a table to display the current price of an asset and change the text color based on whether the price has increased or decreased compared to the previous close.

indicator("Price Highlight Table", overlay=true)
var table priceTable = table.new(position.top_right, 2, 1)
var color upColor = color.green
var color downColor = color.red

if (bar_index > 1)
    priceChangeColor = close > close[1] ? upColor : downColor
    table.cell_set_text_color(priceTable, 0, 0, priceChangeColor)
    table.cell_set_text(priceTable, 0, 0, str.tostring(close, "#.##"))

Line by Line Explanation

  1. Initialization of the Indicator:
    • //@version=5: Specifies the version of Pine Script used for the script. Version 5 is the latest version with enhanced features and functionalities.
    • indicator("Price Highlight Table", overlay=true): This line declares a new indicator named “Price Highlight Table”. The overlay=true parameter means that this indicator will be drawn over the price chart itself.
  2. Creating a Table for Price Display:
    • var table priceTable = table.new(position.top_right, 2, 1): A table named priceTable is created and placed at the top right of the chart. This table has 2 columns and 1 row. The var keyword is used to ensure that the table is only created once when the script is added to the chart.
  3. Defining Color Variables:
    • var color upColor = color.green: Defines a variable upColor to hold the color green. This color is used to indicate that the price has increased.
    • var color downColor = color.red: Similarly, downColor is defined to hold the color red, used to indicate a price decrease.
  4. Conditional Price Change Detection:
    • The if (bar_index > 1) condition checks if there are at least two bars on the chart. This is necessary because the script compares the current close price with the previous close price, requiring at least two bars to perform this comparison.
  5. Determining the Color Based on Price Movement:
    • priceChangeColor = close > close[1] ? upColor : downColor: This line uses a ternary operator to determine the color of the text based on price movement. If the current close price (close) is greater than the previous close price (close[1]), priceChangeColor is set to upColor (green); otherwise, it is set to downColor (red).
  6. Applying the Color and Updating the Text in the Table:
    • table.cell_set_text_color(priceTable, 0, 0, priceChangeColor): This function call changes the text color of the first cell in priceTable (column 0, row 0) to either green or red, depending on the price movement.
    • table.cell_set_text(priceTable, 0, 0, str.tostring(close, "#.##")): Updates the text of the same cell to display the current close price, formatted to two decimal places. The str.tostring() function converts the numerical close price to a string for display.

Key Features and Takeaways

  • The table.cell_set_text_color() function enhances the visual presentation of data in tables, making it easier to highlight important information.
  • It allows for dynamic changes in text color based on data conditions or criteria, adding an interactive element to your financial charts.
  • Understanding the indexing system (starting at 0) is crucial for accurately targeting the desired cells in a table.
  • Combining table.cell_set_text_color() with other table manipulation functions opens up a wide range of possibilities for data presentation in Pine Script.

In summary, the table.cell_set_text_color() function is a versatile tool for anyone looking to enhance their financial charts with dynamically colored table data. By understanding and applying this function, developers can create more intuitive and visually appealing charts that communicate information effectively.

Leave a Comment