The Security Function & Multi-Timeframe Data
request.security() deep dive, security tuples, higher-timeframe EMA, HTF high/low indicators, and time settings guide.
From Thrive on YouTube
Watch the matching Thrive video
Multi-Timeframe Analysis for Crypto Futures
Part of Start Here: Crypto Futures Basics. Education only. Not financial advice.
request.security() Deep Dive
request.security() is PineScript's gateway to multi-timeframe (MTF) analysis. It lets you fetch data from any symbol and any timeframe, regardless of your current chart. You can pull the daily EMA while viewing a 5-minute chart of BTCUSDT, or grab the weekly RSI of ETHUSDT while charting SOL. Mastering this function is the single biggest unlock for building professional-grade indicators.
The function signature is request.security(symbol, timeframe, expression, gaps, lookahead). Every parameter matters. Getting even one wrong can produce repainting data, gaps in your plots, or values that appear correct on historical bars but diverge wildly in real time.
| symbol | The ticker to fetch. Use syminfo.tickerid for the current chart's symbol, or a string like "BINANCE:BTCUSDT" for a specific exchange pair. |
| timeframe | The timeframe string: "1" (1m), "5" (5m), "15" (15m), "60" (1H), "240" (4H), "D" (daily), "W" (weekly), "M" (monthly). Empty string "" means the chart's current timeframe. |
| expression | Any PineScript expression: a built-in variable like close, a function call like ta.ema(close, 21), or a tuple of multiple values. |
| gaps | barmerge.gaps_off (default) fills gaps with the last value. barmerge.gaps_on leaves na between higher-timeframe updates. |
| lookahead | barmerge.lookahead_off (default) uses the current HTF bar. barmerge.lookahead_on peeks ahead: combine with [1] to avoid repainting. |
1//@version=52indicator("Security Deep Dive", overlay=true)34// Basic: get daily close on any intraday chart5dailyClose = request.security(syminfo.tickerid, "D", close)67// Non-repainting: use previous completed bar with lookahead8dailyCloseConfirmed = request.security(syminfo.tickerid, "D",9 close[1], lookahead=barmerge.lookahead_on)1011// With gaps: only show values when HTF bar updates12dailyCloseGaps = request.security(syminfo.tickerid, "D",13 close, gaps=barmerge.gaps_on)1415// Cross-symbol: fetch ETH price while on BTC chart16ethPrice = request.security("BINANCE:ETHUSDT", "D",17 close[1], lookahead=barmerge.lookahead_on)1819plot(dailyCloseConfirmed, "Daily Close", color.orange, 2, plot.style_stepline)20plot(ethPrice, "ETH Daily", color.blue, 1, display=display.status_line)Security Tuples: Fetching Multiple Values in One Call
PineScript limits you to 40 request.security() calls per script. If you need the daily open, high, low, close, and volume, that would use 5 calls. Tuples solve this: you can return multiple values from a single call by wrapping them in square brackets. This is essential for building data-rich multi-timeframe dashboards without hitting limits.
1//@version=52indicator("Security Tuples", overlay=true)34// Fetch 5 daily values in ONE request.security() call5[dOpen, dHigh, dLow, dClose, dVol] = request.security(6 syminfo.tickerid, "D",7 [open[1], high[1], low[1], close[1], volume[1]],8 lookahead=barmerge.lookahead_on)910// Fetch daily EMA + RSI in one call11[dEma21, dRsi] = request.security(12 syminfo.tickerid, "D",13 [ta.ema(close, 21)[1], ta.rsi(close, 14)[1]],14 lookahead=barmerge.lookahead_on)1516// Draw yesterday's high/low range17plot(dHigh, "Prev Day High", color.new(color.red, 30), 1, plot.style_stepline)18plot(dLow, "Prev Day Low", color.new(color.green, 30), 1, plot.style_stepline)19bgcolor(close > dEma21 ? color.new(color.green, 93) : color.new(color.red, 93))Tuple limit
request.security() calls. This still uses only 2 of your 40 allowed calls instead of 10+.Higher-Timeframe EMA Indicator
One of the most requested indicators in crypto trading: plotting a higher-timeframe EMA on your execution chart. If you trade on the 15-minute chart, you still need to see the daily 21 EMA: the level that institutional traders and algorithms respect on BTCUSDT. This indicator draws it correctly without repainting.
1//@version=52indicator("HTF EMA Overlay", overlay=true)34htf = input.timeframe("D", "Higher Timeframe")5emaLen = input.int(21, "EMA Length", minval=1)6src = input.source(close, "Source")78// Non-repainting: confirmed HTF EMA9htfEma = request.security(syminfo.tickerid, htf,10 ta.ema(src, emaLen)[1],11 lookahead=barmerge.lookahead_on)1213// Trend detection14aboveEma = close > htfEma15trendCol = aboveEma ? color.new(color.green, 0) : color.new(color.red, 0)1617plot(htfEma, "HTF EMA", trendCol, 3, plot.style_stepline)18bgcolor(aboveEma ? color.new(color.green, 94) : color.new(color.red, 94))HTF High/Low Indicator: Daily Levels on Intraday Charts
Drawing the previous day's high and low on an intraday chart is foundational for session trading. These levels act as natural support and resistance because they represent the extremes where buyers and sellers previously exhausted themselves. On BTC and ETH, the previous daily high/low are among the most-watched levels by active traders.
1//@version=52indicator("Daily H/L on Intraday", overlay=true)34htf = input.timeframe("D", "Reference Timeframe")56[prevHigh, prevLow, prevClose] = request.security(7 syminfo.tickerid, htf,8 [high[1], low[1], close[1]],9 lookahead=barmerge.lookahead_on)1011midpoint = (prevHigh + prevLow) / 2.01213plot(prevHigh, "Prev High", color.new(color.red, 20), 2, plot.style_stepline)14plot(prevLow, "Prev Low", color.new(color.green, 20), 2, plot.style_stepline)15plot(midpoint, "Midpoint", color.new(color.gray, 40), 1, plot.style_stepline)1617// Color fill between high and low18h = plot(prevHigh, display=display.none)19l = plot(prevLow, display=display.none)20fill(h, l, color.new(color.gray, 95), title="Range Fill")Timeframe Strings and Timezone Settings
Timeframe strings in PineScript follow a specific format. Getting these wrong is a common source of errors, especially for minute-based timeframes. The timezone parameter in request.security() controls when daily bars reset: critical for crypto, which trades 24/7 and has no natural "session close."
| "1", "3", "5", "15", "30", "45" | Minute-based timeframes. Just the number as a string: no suffix needed. |
| "60", "120", "240", "480", "720" | Hourly timeframes expressed in minutes. "60" = 1H, "240" = 4H, "720" = 12H. |
| "D", "W", "M" | Daily, Weekly, Monthly. Capital letters, no numbers needed for single-unit timeframes. |
| "2D", "3W", "2M" | Multi-day/week/month: "2D" = 2-day bars, "3W" = 3-week bars. Useful for swing analysis. |
| timeframe.period | Returns the current chart's timeframe string. Use to compare or prevent running on wrong TFs. |
| "America/New_York" | TradingView uses exchange timezone by default. Override with specific timezone for session calculations. |
Higher vs Lower Timeframe Requests
Requesting higher timeframes (e.g., daily data on a 15m chart) is straightforward. Each higher-timeframe bar maps to many lower-timeframe bars. The value simply stays constant until the next HTF bar completes. Requesting lower timeframes (e.g., 1m data on a daily chart) is fundamentally different and has major caveats.
Higher timeframe → safe and common
Lower timeframe → use with caution
Same timeframe → valid but redundant
Lower timeframe intrabars
request.security_lower_tf() for accessing intrabar data as an array. However, this function has strict limitations: maximum 100,000 intrabars across all calls, and it returns arrays that require special handling. For most crypto trading use cases, sticking to higher-timeframe requests is more reliable and performant.Complete Build: 5-Timeframe Confluence Dashboard
This is the flagship indicator of multi-timeframe analysis: a dashboard that shows trend alignment across five timeframes simultaneously. When all five align bullish on BTCUSDT, you have maximum confluence: the highest-probability setup for trend-following entries. The table updates in real time and uses only 5 of your 40 security calls.
1//@version=52indicator("5-TF Confluence", overlay=true)34emaLen = input.int(21, "EMA Length", minval=1)56// Helper: non-repainting trend detection7getTrend(tf) =>8 [emaVal, clVal] = request.security(syminfo.tickerid, tf,9 [ta.ema(close, emaLen)[1], close[1]],10 lookahead=barmerge.lookahead_on)11 clVal > emaVal ? 1 : -11213// 5 timeframes (5 security calls via tuples)14t15m = getTrend("15")15t1h = getTrend("60")16t4h = getTrend("240")17tD = getTrend("D")18tW = getTrend("W")1920score = t15m + t1h + t4h + tD + tW21allBull = score == 522allBear = score == -52324// Table dashboard25if barstate.islast26 t = table.new(position.top_right, 3, 7,27 bgcolor=color.new(#0d0b14, 10),28 border_width=1, border_color=color.new(color.gray, 70))2930 table.cell(t, 0, 0, "TF", text_color=color.gray, text_size=size.small)31 table.cell(t, 1, 0, "Trend", text_color=color.gray, text_size=size.small)32 table.cell(t, 2, 0, "EMA", text_color=color.gray, text_size=size.small)3334 addRow(tbl, row, label, trend) =>35 isBull = trend == 136 table.cell(tbl, 0, row, label,37 text_color=color.white, text_size=size.small)38 table.cell(tbl, 1, row, isBull ? "▲" : "▼",39 text_color=isBull ? color.green : color.red,40 text_size=size.small)41 table.cell(tbl, 2, row, isBull ? "Above" : "Below",42 text_color=isBull ? color.green : color.red,43 text_size=size.small)4445 addRow(t, 1, "15m", t15m)46 addRow(t, 2, "1H", t1h)47 addRow(t, 3, "4H", t4h)48 addRow(t, 4, "1D", tD)49 addRow(t, 5, "1W", tW)5051 // Score row52 scoreCol = score >= 3 ? color.green : score <= -3 ? color.red : color.yellow53 table.cell(t, 0, 6, "Score", text_color=color.gray, text_size=size.small)54 table.cell(t, 1, 6, str.tostring(score) + "/5",55 text_color=scoreCol, text_size=size.small)56 table.cell(t, 2, 6, score >= 3 ? "Bullish" : score <= -3 ? "Bearish" : "Mixed",57 text_color=scoreCol, text_size=size.small)5859// Background coloring based on confluence60bgCol = allBull ? color.new(color.green, 90) :61 allBear ? color.new(color.red, 90) :62 score >= 3 ? color.new(color.green, 95) :63 score <= -3 ? color.new(color.red, 95) : na64bgcolor(bgCol, title="Confluence BG")6566// Signal on full alignment + crossover67currentEma = ta.ema(close, emaLen)68bullEntry = allBull and ta.crossover(close, currentEma) and barstate.isconfirmed69bearEntry = allBear and ta.crossunder(close, currentEma) and barstate.isconfirmed7071plotshape(bullEntry, "Full Bull", shape.triangleup,72 location.belowbar, color.green, size=size.small, text="5/5")73plotshape(bearEntry, "Full Bear", shape.triangledown,74 location.abovebar, color.red, size=size.small, text="5/5")7576alertcondition(bullEntry, "5-TF Bull Confluence",77 "All 5 timeframes bullish with EMA crossover")78alertcondition(bearEntry, "5-TF Bear Confluence",79 "All 5 timeframes bearish with EMA crossunder")Trading with confluence scores
Put this lesson to work
Free on Thrive, no account needed.