The error is occurring because the code contains em-dashes (—) instead of regular hyphens (-) in the comments. This happens when copying from formatted text. Here's the corrected code with proper characters:
pinescript
//@version=5
indicator(title="On-Chart Watchlist", shorttitle="Watchlist", overlay=true)
// --- 1. Inputs ---
lengthMA = input.int(title="MA Length", defval=20, group="Settings")
// Define the watchlist symbols (cannot be dynamic, must be explicit inputs)
sym1 = input.symbol("NASDAQ:AAPL", "Symbol 1", group="Watchlist")
sym2 = input.symbol("NASDAQ:MSFT", "Symbol 2", group="Watchlist")
sym3 = input.symbol("NASDAQ:GOOGL", "Symbol 3", group="Watchlist")
sym4 = input.symbol("NASDAQ:AMZN", "Symbol 4", group="Watchlist")
// Define colors
headerColor = color.new(color.gray, 60)
textColor = color.white
dataColor = color.new(color.gray, 80)
// --- 2. Data Retrieval Function ---
// Function to fetch both Close price and SMA for a symbol efficiently
fetchWatchlistData(string sym, int len) =>
// Request a tuple [close price, sma calculation] from the security
[secClose, secMA] = request.security(sym, timeframe.period, [close, ta.sma(close, len)])
// Return the results as a tuple
[secClose, secMA]
// --- 3. Fetch Data (Global Scope) ---
[c1, ma1] = fetchWatchlistData(sym1, lengthMA)
[c2, ma2] = fetchWatchlistData(sym2, lengthMA)
[c3, ma3] = fetchWatchlistData(sym3, lengthMA)
[c4, ma4] = fetchWatchlistData(sym4, lengthMA)
// --- 4. Table Creation and Population ---
// Initialize the table. 'var' ensures it's only created once.
// 3 columns (Symbol, Price, MA), 5 rows (Header + 4 symbols)
var watchlistTable = table.new(position.top_right, 3, 5, border_width=1)
// Helper function to fill data rows
fillDataRow(int rowIndex, string symName, float price, float maValue) =>
// Convert numerical data to strings for display, format using the chart's minimum tick movement
priceStr = str.tostring(price, format.mintick)
maStr = str.tostring(maValue, format.mintick)
table.cell(watchlistTable, 0, rowIndex, symName, bgcolor=dataColor, text_color=textColor)
table.cell(watchlistTable, 1, rowIndex, priceStr, bgcolor=dataColor, text_color=textColor, text_halign=text.align_right)
table.cell(watchlistTable, 2, rowIndex, maStr, bgcolor=dataColor, text_color=textColor, text_halign=text.align_right)
// Function to fill the header row
fillHeader() =>
table.cell(watchlistTable, 0, 0, "Symbol", bgcolor=headerColor, text_color=textColor, text_size=size.small)
table.cell(watchlistTable, 1, 0, "Price", bgcolor=headerColor, text_color=textColor, text_size=size.small)
table.cell(watchlistTable, 2, 0, str.format("SMA ({0})", lengthMA), bgcolor=headerColor, text_color=textColor, text_size=size.small)
// --- 5. Execution Logic ---
// We only update the table on the very last bar of the chart
if barstate.islast
// Draw Header (Row 0)
fillHeader()
// Draw Data Rows (Rows 1 through 4)
fillDataRow(1, sym1, c1, ma1)
fillDataRow(2, sym2, c2, ma2)
fillDataRow(3, sym3, c3, ma3)
fillDataRow(4, sym4, c4, ma4)
The issue was that the comment lines contained em-dashes (—) instead of regular hyphens/minus signs (-). I've replaced all of them with the correct character. The code should now compile without errors in TradingView's Pine Editor.