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

Understanding table.cell_set_text_size() Function in Pine Script

Photo of author
Published on

In Pine Script, table.cell_set_text_size() is a function designed to modify the size of the text within a specific cell of a table. This feature is crucial for creating visually distinctive and hierarchically structured displays in trading indicators or strategies, allowing for a more organized presentation of data. Let’s dive into a detailed tutorial covering its syntax, usage, and an illustrative example to implement this function effectively.

Syntax

The syntax for table.cell_set_text_size() is as follows:

table.cell_set_text_size(table_id, column, row, text_size) → void

Arguments:

  • table_id (series table): This is the identifier for the table object where the text size adjustment will be applied.
  • column (series int): The index of the column containing the cell to be modified. Column indexing starts at 0.
  • row (series int): The index of the row containing the cell to be adjusted. Row indexing also starts at 0.
  • text_size (series string): Specifies the desired size of the text. Acceptable values include size.auto, size.tiny, size.small, size.normal, size.large, and size.huge.

Example Usage

Let’s take a basic example to illustrate how to use table.cell_set_text_size() in a Pine Script indicator. In this example, we’ll modify the text size of specific cells within a table that displays the RSI and MACD values.

//@version=5
indicator("Customized Indicator", overlay=true)

// Calculation of MACD values
[value1, macdValue, value2] = ta.macd(close, 12, 26, 9)

// Initialize the table with 2 columns and 3 rows
indicatorTable = table.new(position = position.top_right, columns = 2, rows = 3)

// Populate the table with headers and calculated values
table.cell(indicatorTable, 0, 0, "Indicator", text_color = color.red)
table.cell(indicatorTable, 1, 0, "Value", text_color = color.red)
table.cell(indicatorTable, 0, 1, "RSI", text_color = color.red)
table.cell(indicatorTable, 1, 1, str.tostring(ta.rsi(close, 14)), text_color = color.red)
table.cell(indicatorTable, 0, 2, "MACD", text_color = color.red)
table.cell(indicatorTable, 1, 2, str.tostring(macdValue), text_color = color.red)

// Set the text size for specific cells
table.cell_set_text_size(indicatorTable, 0, 0, size.large)
table.cell_set_text_size(indicatorTable, 1, 0, size.large)

Code Walkthrough

  1. Indicator Declaration:
    • //@version=5: Specifies the version of Pine Script used, which is version 5 in this case. This is necessary for compatibility and to access the latest features.
    • indicator("Customized Indicator", overlay=true): This line declares a new indicator titled “Customized Indicator”. The overlay=true parameter means that the indicator will be drawn over the price chart.
  2. MACD Calculation:
    • [value1, macdValue, value2] = ta.macd(close, 12, 26, 9): Calculates the Moving Average Convergence Divergence (MACD) values based on the closing prices of the chart. The MACD is calculated using fast period of 12, slow period of 26, and signal smoothing of 9 periods. The variable macdValue stores the MACD line values, which will be displayed in the table.
  3. Table Initialization:
    • indicatorTable = table.new(position = position.top_right, columns = 2, rows = 3): Initializes a new table with the variable name indicatorTable, positioned at the top right of the chart. The table is set to have 2 columns and 3 rows.
  4. Populating the Table:
    • The table.cell() function is used multiple times to fill the table with data:
      • Headers: “Indicator” and “Value” are set as headers in the first row of each column, with text color red.
      • RSI Value: The second row displays “RSI” and its calculated value using str.tostring(ta.rsi(close, 14)), converting the RSI value to a string format for display.
      • MACD Value: The third row shows “MACD” and its calculated value stored in macdValue, also converted to string format for display.
    • All text is colored red (text_color = color.red) for consistency and emphasis.
  5. Setting Text Size:
    • table.cell_set_text_size(indicatorTable, 0, 0, size.large): Sets the text size of the cell located at the first column and first row (header “Indicator”) to large. This enhances visibility for the table header.
    • Similarly, the text size for the “Value” header in the first row of the second column is set to large, making both headers more prominent within the table.

Key Takeaways

  • The table.cell_set_text_size() function allows for customization of text size within table cells, enhancing readability and visual appeal.
  • It requires specifying the table ID, column index, row index, and the desired text size.
  • This function supports various text sizes, from size.tiny to size.huge, catering to diverse design needs.
  • Proper use of table.cell_set_text_size() can significantly improve the presentation of data in custom trading indicators or strategies on the TradingView platform.

By incorporating table.cell_set_text_size() into your Pine Script projects, you can achieve a more effective data presentation, facilitating better decision-making processes for users of your trading tools.

Leave a Comment