Skip to main content

Table.cell in Pine Script: A Concise Guide for Traders and Developers

· 3 min read

In Pine Script, TradingView's scripting language, the table.cell function is a key tool for creating dynamic, customizable tables within trading indicators and strategies. Unlike plots that anchor to specific bars, tables float independently in the script's visual space, allowing you to organize and display data clearly and flexibly on your chart.

What is table.cell in Pine Script?

The table.cell function defines and populates individual cells within a table that you first create using table.new(). It does not create the table itself but sets the content and style of each cell. This function is essential for presenting data points, such as prices, indicators, or signals, in a structured tabular format directly on your chart.

The Best Pine Script Generator

How to Use table.cell

Step 1: Create a Table

Use table.new(position, columns, rows, ...) to initialize a table. You specify where the table appears (e.g., top_right), the number of columns and rows, and optional styling such as background color and borders.

table_id = table.new(position.top_right, 3, 2, border_color=color.white, border_width=2)

Step 2: Populate Cells

Call table.cell() for each cell, providing:

  • table_id: The table object
  • column and row: Cell indices starting at 0
  • text: The content string to display
  • Optional parameters: width, height, text color, alignment, background color, font, and tooltip

Example:

table.cell(table_id, 0, 0, "Current Price: " + str.tostring(close), text_color=color.white)
table.cell(table_id, 1, 0, "Price Change: " + str.tostring(close - close[1]))
table.cell(table_id, 2, 0, "Volatility: " + str.tostring(ta.stdev(close, 14)))

Each call fully defines the cell's properties, overwriting any previous settings for that cell.

Benefits of Using table.cell in Pine Script

  • Improved Data Clarity: Tables organize complex data for quick interpretation.
  • Customizable Layouts: Control size, position, colors, and fonts to match your trading style.
  • Dynamic Updates: Change cell content in real-time based on market data or user inputs.
  • Enhanced Visual Appeal: Tables make scripts more user-friendly and professional-looking.

Advanced Tips: On/Off Columns and Dynamic Content

You can create interactive tables with toggle states (e.g., ON/OFF) by using boolean variables and conditional logic to update cell text and background color dynamically. This technique enhances user engagement and makes your tables actionable.

Example snippet for an on/off toggle column:

var bool toggle = true
if bar_index % 2 == 0
toggle := not toggle
table.cell(table_id, 0, 1, toggle ? "ON" : "OFF", bgcolor=toggle ? color.green : color.red)

This approach allows your table to reflect changing conditions instantly.