Pine Script symbol comparison

Plot Another Asset as a Line in TradingView

For a line graph of another asset, TradingView has a built-in Compare tool. For a custom indicator, use input.symbol() to select the asset, request.security() to retrieve its close, and plot() to draw it. A separate pane keeps its price scale independent of the chart. Use percentage changes when the task is comparing performance rather than price levels.

Build a Symbol Comparison Indicator

Add a symbol without an indicator

Open Compare from the chart toolbar, search for the second symbol, and add it. Choose a new price scale or a new pane if you want to read its price independently. Write a Pine indicator when you need a custom calculation, reusable inputs, or conditions based on both symbols.

Plot the second symbol in a separate pane

Paste this complete script into Pine Editor, save it, and add it to the chart. Change Secondary Asset in the indicator settings. The example requests the chart timeframe and draws the second symbol in a separate pane. It does not convert the quote currency or calculate a return.

  • input.symbol() provides a symbol selector, including the exchange prefix.
  • overlay=false gives the indicator a separate pane.
  • ignore_invalid_symbol=true returns na for an invalid request; it does not repair the symbol or supply missing history.
Complete Pine Script v6 indicator: raw secondary pricepinescript
//@version=6
indicator("Secondary Asset Line", overlay=false)

sec_ticker = input.symbol("NASDAQ:QQQ", "Secondary Asset")
sec_close = request.security(sec_ticker, timeframe.period, close,
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off,
     ignore_invalid_symbol=true)

plot(sec_close, "Secondary Close", color=color.orange, linewidth=2)

Choose raw prices, percentage changes, or a ratio

Two raw price series can share a pane, but sharing one price scale may flatten the smaller series. A separate scale preserves the price units. Percentage changes answer a different question: how much each asset moved over a defined interval. A ratio also has a different meaning and is not a percentage return.

  • The history reference is applied after the request, so N means chart bars, not N trading bars of the second asset.
  • For example, a move from 100 to 105 is 5%, and a move from 200 to 206 is 3%. Their difference is 2 percentage points.
  • Missing history and a zero denominator produce na, not a fabricated 0% return.
  • This rolling calculation does not reproduce an anchored comparison from the first visible bar.
OutputCalculationInterpretation
Raw priceSecondary closePrice in the requested symbol's quote units; use its own pane or scale.
Anchored return100 * (price / anchor - 1)Change from a specified starting value. Both assets need a documented anchor time.
Rolling return100 * (price / price[N] - 1)Change over a moving window. The start point advances with each bar.
Price ratioChart close / secondary closeRelative price level. Currency and contract units affect the result.
Complete Pine Script v6 indicator: returns over chart barspinescript
//@version=6
indicator("Two-Symbol Rolling Returns", overlay=false, precision=2)

sec_ticker = input.symbol("NASDAQ:QQQ", "Comparison Symbol")
lookback = input.int(20, "Lookback in chart bars", minval=1)
sec_close = request.security(sec_ticker, timeframe.period, close,
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off,
     ignore_invalid_symbol=true)

pct_change(float current, float previous) =>
    not na(current) and not na(previous) and previous != 0 ?
         100.0 * (current / previous - 1.0) : na

chart_pct = pct_change(close, close[lookback])
sec_pct = pct_change(sec_close, sec_close[lookback])
plot(chart_pct, "Chart Return (%)", color=color.blue, linewidth=2)
plot(sec_pct, "Comparison Return (%)", color=color.orange, linewidth=2)
hline(0, "Zero Return", color=color.gray)

Make session gaps visible when they matter

The chart supplies the bars on which the indicator executes. An equity chart cannot display every weekend crypto bar. On a crypto chart, an equity series may have no new data while its exchange is closed. Session settings and available feeds affect which observations exist.

ChoiceData behaviorDisplay consequence
gaps_offFills historical gaps with the last confirmed value; realtime requests can use developing values.A continuous line can contain stale prices.
gaps_onReturns na where new requested data is not confirmed.Use plot.style_linebr to leave breaks; the default line style bridges na.
Invalid symbolWith ignore_invalid_symbol=true, the request returns na.Check the selected ticker before treating a blank plot as zero.
Complete Pine Script v6 indicator: show missing observationspinescript
//@version=6
indicator("Secondary Asset With Gaps", overlay=false)

sec_ticker = input.symbol("NASDAQ:QQQ", "Secondary Asset")
sec_close = request.security(sec_ticker, timeframe.period, close,
     gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off,
     ignore_invalid_symbol=true)

plot(sec_close, "Secondary Close", color=color.teal, linewidth=2,
     style=plot.style_linebr)

Check live values before adding alerts

lookahead_off prevents higher-timeframe future leakage on historical bars, but does not freeze an open realtime bar. These examples use current closes, so their lines can move while prices update. If you later request a strictly higher timeframe, the documented confirmed-value pattern uses an expression offset such as close[1] with lookahead_on. That returns the previous completed higher-timeframe bar and introduces a delay. Confirming the chart bar alone does not confirm a higher-timeframe bar.

  • Reproducible check: select the chart symbol as the comparison symbol. The two rolling-return plots should match with equivalent feed and session settings.
  • Check an early bar with insufficient lookback history: the return should be missing.
  • Compare an equity and crypto symbol across a weekend, then reverse which symbol owns the chart.
  • Observe an open bar, wait for it to close, and reload the indicator. Check whether the values relevant to your alert changed.
  • These are chart verification steps, not reported live-market test results.
Where Pineify fits

Pine Script AI Coding Agent

Describe the two symbols, comparison window, pane, and gap policy to generate a Pine Script draft. Review the calculation and test its chart behavior before adding alerts.

Build a Symbol Comparison Indicator

Frequently asked questions

Educational programming examples, not investment advice. Syntax validation does not establish data quality, alert behavior, or trading performance. Verify the selected feeds and sessions on your own chart.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.