Tables in TradingView Pine Script provide an effective way to organize and display data directly on the chart interface. This is especially useful for traders who need to see multiple indicators or calculations at a glance without cluttering the main chart area.
Tables in TradingView Pine Script offer versatile applications for enhancing trading strategies and visual aids on charts. Here are five practical applications that can be created using tables in Pine Script:
- Indicator Dashboard: A table can be used to create an indicator dashboard that summarizes key trading indicators like moving averages, RSI, MACD, and Bollinger Bands. This dashboard provides a quick overview of various indicator values at a glance, helping traders make informed decisions without having to switch between different chart setups.
- Trade Management Panel: Implement a trade management panel that displays real-time information about current trades, such as entry price, current price, stop loss levels, and potential profit or loss. This helps traders manage their open positions more effectively and adjust their strategies on the fly.
Basic Usage of Tables in Pine Script
A table in Pine Script is created using table.new()
function where you can specify its position, number of columns, rows, and various style attributes like border color and width. Once created, you can add or modify data in the table using table.cell()
to set or update cell values.
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © algostudio
//@version=5
indicator("Table with Indicators", overlay=true)
// Input Variables for Table and Table Display Settings
textColor = input.color(defval = color.black, title = "Text Color")
showTable = input.bool(true, "Show Desktop Table?")
tablePosition = input.string(defval = "Top Right", title = "Table Position",
options=["Top Right", "Bottom Right", "Middle Right", "Bottom Center", "Middle Left"])
tableSize = input.string(defval = "Small", title = "Table Size", options = ["Small", "Large", "Medium"])
// Function to Get Table Position
gettablePos(pos) =>
switch pos
"Top Right" => position.top_right
"Bottom Right" => position.bottom_right
"Middle Right" => position.middle_right
"Bottom Center" => position.bottom_center
"Middle Left" => position.bottom_left
// Function to get the Table Size
gettableSize(size) =>
switch size
"Small" => size.small
"Large" => size.large
"Medium" => size.normal
// Creating Tables
var table myTable = table.new(gettablePos(tablePosition), 4, 6, border_width=4,
border_color=color.gray, frame_color=color.gray, frame_width=4)
// Variables for SMA and EMA
sma1 = math.round(ta.sma(close, 50),2)
sma2 = math.round(ta.sma(close, 100),2)
sma3 = math.round(ta.sma(close, 200),2)
ema1 = math.round(ta.ema(close, 50),2)
ema2 = math.round(ta.ema(close, 100),2)
ema3 = math.round(ta.ema(close, 200),2)
rsi = math.round(ta.rsi(close,14),2)
atr = math.round(ta.atr(14),2)
roc = math.round(ta.roc(close,1),2)
// Function to Plot Table
plotTable(tbl, tblSize) =>
// Define table content
openPrice = str.tostring(open)
closePrice = str.tostring(close)
changePercent = str.tostring(roc) + " %"
volumeData = str.tostring(volume)
rsiData = str.tostring(rsi)
atrData = str.tostring(atr)
// Set table cells
table.cell(tbl, 0, 0, bgcolor=color.black, text_color=color.white, text="Open Price", text_size = tblSize)
table.cell(tbl, 0, 1, bgcolor=color.black, text_color=color.white, text="Close Price", text_size = tblSize)
table.cell(tbl, 0, 2, bgcolor=color.black, text_color=color.white, text="Change Percent", text_size = tblSize)
table.cell(tbl, 0, 3, bgcolor=color.black, text_color=color.white, text="RSI", text_size = tblSize)
table.cell(tbl, 0, 4, bgcolor=color.black, text_color=color.white, text="ATR", text_size = tblSize)
table.cell(tbl, 0, 5, bgcolor=color.black, text_color=color.white, text="Volume", text_size = tblSize)
// Set values in table cells
table.cell(tbl, 1, 0, bgcolor=color.black, text_color=color.white, text=openPrice, text_size = tblSize)
table.cell(tbl, 1, 1, bgcolor=color.black, text_color=color.white, text=closePrice, text_size = tblSize)
table.cell(tbl, 1, 2, bgcolor=color.black, text_color=color.white, text=changePercent, text_size = tblSize)
table.cell(tbl, 1, 3, bgcolor=color.lime, text_color=color.white, text=rsiData, text_size = tblSize)
table.cell(tbl, 1, 4, bgcolor=color.black, text_color=color.white, text=atrData, text_size = tblSize)
table.cell(tbl, 1, 5, bgcolor=color.black, text_color=color.white, text=volumeData, text_size = tblSize)
// Additional rows for SMA and EMA values
table.cell(tbl, 2, 0, bgcolor=color.gray, text_color=color.white, text="SMA 50", text_size = tblSize)
table.cell(tbl, 2, 1, bgcolor=color.gray, text_color=color.white, text="SMA 100", text_size = tblSize)
table.cell(tbl, 2, 2, bgcolor=color.gray, text_color=color.white, text="SMA 200", text_size = tblSize)
table.cell(tbl, 2, 3, bgcolor=color.gray, text_color=color.white, text="EMA 50", text_size = tblSize)
table.cell(tbl, 2, 4, bgcolor=color.gray, text_color=color.white, text="EMA 100", text_size = tblSize)
table.cell(tbl, 2, 5, bgcolor=color.gray, text_color=color.white, text="EMA 200", text_size = tblSize)
// Display SMA and EMA values
table.cell(tbl, 3, 0, bgcolor=color.black, text_color=color.white, text=str.tostring(sma1), text_size = tblSize)
table.cell(tbl, 3, 1, bgcolor=color.black, text_color=color.white, text=str.tostring(sma2), text_size = tblSize)
table.cell(tbl, 3, 2, bgcolor=color.black, text_color=color.white, text=str.tostring(sma3), text_size = tblSize)
table.cell(tbl, 3, 3, bgcolor=color.black, text_color=color.white, text=str.tostring(ema1), text_size = tblSize)
table.cell(tbl, 3, 4, bgcolor=color.black, text_color=color.white, text=str.tostring(ema2), text_size = tblSize)
table.cell(tbl, 3, 5, bgcolor=color.black, text_color=color.white, text=str.tostring(ema3), text_size = tblSize)
// Call the plotting function at the last bar
//if barstate.islast
if showTable
plotTable(myTable, gettableSize(tableSize))
Here’s a simplified explanation of the provided Pine Script code that utilizes tables:
- Initialization: The script starts with basic settings such as the script name and version. The table settings are taken as inputs, allowing the user to customize text color, whether to show the table, its position, and size.
- Position and Size Functions: Functions
gettablePos()
andgettableSize()
are used to determine the position and size of the table based on user inputs. - Creating the Table: A table named
myTable
is initialized with specified position, columns, rows, and style settings like border and frame colors. - Indicator Calculations: Several key trading indicators like simple moving averages (SMA), exponential moving averages (EMA), Relative Strength Index (RSI), Average True Range (ATR), and rate of change (ROC) are calculated.
- Filling the Table: The
plotTable()
function is designed to fill the table with calculated data:- Labels: The first row contains labels such as “Open Price”, “Close Price”, etc.
- Data: The subsequent rows display corresponding values like open, close, change percentage, volume, etc.
- Indicator Values: Additional rows are used to display values of SMAs and EMAs.
- Display Control: The table is only plotted if the
showTable
input is true, allowing users to toggle the display of the table.
Conclusion
This Pine Script leverages the utility of tables to present a comprehensive view of various trading indicators, making it easier for traders to analyze data directly on the chart. By customizing table size, position, and whether it appears, users can adapt the script to their preferences and trading style. This script exemplifies how Pine Script can be used to enhance data visualization in TradingView charts.