✅
📜 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. pine script v5 request.security function documentation
request.security(syminfo.tickerid, "60", close, lookahead = barmerge.lookahead_on) You can fetch data from any symbol – even FX, crypto, or stocks – and combine them: ✅ 📜 Basic Syntax request
// 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: 🧠 Example 2: Tuple Magic – Return Multiple
request.security(syminfo.tickerid, "60", close, lookahead = barmerge.lookahead_off) ❌
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: