Make a Pine Script input to select a TradingView instrument symbol
Make a Pine Script input to select a TradingView instrument symbol
Section titled “Make a Pine Script input to select a TradingView instrument symbol”TL;DR
Useinput.symbol(defval, title=...)to present an instrument search box. Store the returned ticker string (e.g.,"NASDAQ:QQQ") and feed it torequest.security()or UI elements.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Inputs |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Symbol picker demo", overlay=false)
otherSymbol = input.symbol("NASDAQ:QQQ", "Compare with")otherClose = request.security(otherSymbol, timeframe.period, close)
plot(close, "Chart close")plot(otherClose, "Other close", color=color.new(color.orange, 0))Why It Matters
Section titled “Why It Matters”Symbol inputs let end users compare assets, drive correlation stats, or synchronise signals across markets without editing code. Pairing the selection with request.security() keeps background data in sync with the active chart.
What You’ll Learn
Section titled “What You’ll Learn”- Configure
input.symbol()with titles, groups, and tooltips. - Store the selected ticker string and reuse it in requests or labels.
- Build comparison charts or lookups using
request.security()and plotting helpers. - Handle edge cases when users clear or change the selection.
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
input.symbol(defval, title=?, tooltip=?, group=?, inline=?, confirm=?) | Renders the instrument picker. |
request.security(symbol, timeframe, expression, ...) | Loads data for the chosen instrument. |
plot(series, title, color?) | Visualises the comparison instrument. |
label.new(x, y, text, ...) | Annotates the chart with the selected ticker for clarity. |
input.source() | Optional companion input for selecting which price series to compare. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Create the symbol input
Supply a helpful default, a clear label, and optional grouping/tooltip text.otherSymbol = input.symbol("SP:SPX", "Benchmark", group="Comparison", tooltip="Instrument to compare against the chart") -
Load the selected instrument
Request data from the returned symbol using the current chart period (timeframe.period) or a custom resolution.otherPrice = request.security(otherSymbol, timeframe.period, close) -
Display or use the data
Plot, compute statistics, or emit alerts based on the secondary instrument.correlation = ta.correlation(close, otherPrice, 30)plot(correlation, "30-bar correlation", color=color.new(color.blue, 0))
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Cross-market watcher", overlay=false)
benchmark = input.symbol("SP:SPX", "Benchmark symbol")priceSrc = input.source(close, "Price source")length = input.int(50, "Correlation length", minval=5)
benchmarkSeries = request.security(benchmark, timeframe.period, priceSrc)correlation = ta.correlation(priceSrc, benchmarkSeries, length)
plot(correlation, "Correlation", color=color.new(color.green, 0))hline(0.8, "High correlation", color=color.new(color.green, 60))hline(0.0, "Neutral", color=color.new(color.gray, 60))hline(-0.8, "Inverse correlation", color=color.new(color.red, 60))
if barstate.islast label.new(bar_index, correlation, text="Benchmark: " + benchmark + "\nLength: " + str.tostring(length), style=label.style_label_left, textcolor=color.white, bgcolor=color.new(color.blue, 70))- The symbol input gives users control over which instrument to compare.
- `request.security()` keeps benchmark data aligned with the chart timeframe.
- A label reiterates the chosen ticker, reducing confusion when sharing screenshots.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Inputs require constant arguments. If you want dynamic defaults, compute them first, then pass the literal string.
- Use the
groupargument to cluster related inputs (e.g., “Benchmark settings”). - When requesting intraday data on a higher-time-frame chart, specify a custom timeframe string inside
request.security(e.g.,"60"). - Guard against
naoutputs if the benchmark lacks data for the requested period—wraprequest.securityin a conditional or use theignore_invalid_symbolflag.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The input shows a different exchange than expected
Section titled “The input shows a different exchange than expected”TradingView remembers the last exchange the user chose. Include the exchange prefix ("NYSE:IBM") in the default to force a specific listing.
request.security throws errors after changing the symbol
Section titled “request.security throws errors after changing the symbol”Some instruments lack history at the sampled timeframe. Switch to a supported resolution or provide fallbacks (e.g., default to close if the request returns na).
Can I load multiple symbols?
Section titled “Can I load multiple symbols?”Yes—create additional input.symbol() controls or accept comma-separated tickers and iterate through them in arrays.
Key Takeaways
Section titled “Key Takeaways”input.symbol()adds a familiar instrument search box to Pine Script settings.- Store the returned ticker and feed it into
request.securityor display elements to leverage user selections. - Combine with other inputs (source, length) for flexible comparison or correlation tools.
- Provide descriptive defaults and tooltips so users quickly understand what the picker controls.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.