π Basic Syntax request.security(symbol, timeframe, expression, gaps, lookahead, ignore_invalid_symbol, currency) π§ Mental Model Think of it as: "Go to this symbol and timeframe, run this code there, then bring the result back to my current chart." π§© Parameters That Matter Most | Parameter | What it does | Cool trick | |-----------|--------------|-------------| | symbol | Ticker ID (e.g., syminfo.tickerid , "AAPL" ) | Use syminfo.tickerid for current symbol | | timeframe | Target TF ( "D" , "240" , "1W" ) | Use "1D" for daily data even on 1min chart | | expression | Any Pine expression (ohlc, custom calc) | Can be a tuple β return multiple values! | | gaps | barmerge.gaps_on or barmerge.gaps_off | Gaps on = show missing bars; off = forward fill | | lookahead | lookahead.on / lookahead.off | π¨ Critical for repainting behavior | π― Example 1: The Classic Higher-Timeframe Moving Average //@version=6 indicator("HTF MA", overlay=true) htf_ma = request.security(syminfo.tickerid, "1D", ta.sma(close, 20)) plot(htf_ma, color=color.yellow, linewidth=2) Shows the 20-day SMA on an intraday chart. No repainting if lookahead is default ( barmerge.lookahead_off ). π§ Example 2: Tuple Magic β Return Multiple Values // Get daily high, low, and volume in one call [dailyHigh, dailyLow, dailyVol] = request.security(syminfo.tickerid, "D", [high, low, volume]) plot(dailyHigh, "Daily High", color.green) plot(dailyLow, "Daily Low", color.red) β οΈ The Repainting Trap (And How to Avoid It) If you use lookahead = barmerge.lookahead_on , your script can repaint β future bars affect past values. This makes backtests lie.
get_htf_ma(ma_len) => request.security(syminfo.tickerid, "1D", ta.sma(close, ma_len)) plot(get_htf_ma(20)) | v4 | v5 | |----|----| | security(sym, tf, expr) | request.security(sym, tf, expr) | | Gaps via set parameter | Explicit gaps= parameter | | No tuple support | Tuple support β | | Lookahead implicit | lookahead= explicit | π§ Pro Tip: Accessing Previous HTF Values Because HTF data only changes when a new HTF bar forms: pine script v5 request.security function documentation
// Plot the ratio of BTC to ETH btc_price = request.security("BINANCE:BTCUSDT", "60", close) eth_price = request.security("BINANCE:ETHUSDT", "60", close) ratio = btc_price / eth_price plot(ratio, "BTC/ETH Ratio") No β it must be at the global scope . But you can wrap it in a function: π Basic Syntax request
request.security(syminfo.tickerid, "60", close, lookahead = barmerge.lookahead_off) β π§ Example 2: Tuple Magic β Return Multiple