request.security() - Pine Script Function
request.security()
Section titled “request.security()”Overview
Section titled “Overview”Requests the result of an expression from a specified context (symbol and timeframe).
Syntax
Section titled “Syntax”request.security(symbol, timeframe, expression, gaps, lookahead, ignore_invalid_symbol, currency, calc_bars_count) → series <type>Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| symbol | series string | Symbol or ticker identifier of the requested data. Use an empty string or syminfo.tickerid to request data using the chart’s symbol. To retrieve data with additional modifiers (extended sessions |
| dividend | — | adjustments |
| non-standard | — | chart types like Heikin Ashi and Renko |
| etc.) | — | — |
| create | — | a custom ticker ID for the request using the functions in the ticker.* namespace. |
Returns
Section titled “Returns”A result determined by expression.
Remarks
Section titled “Remarks”- Scripts using this function might calculate differently on historical and realtime bars, leading to repainting.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("Advanced `request.security()` calls")// This calculates a 10-period moving average on the active chart.sma = ta.sma(close, 10)// This sends the `sma` calculationfor execution in the context of the "AAPL" symbol at a "240" (4 hours)timeframe.aaplSma = request.security("AAPL", "240", sma)plot(aaplSma)// To avoid differences on historical and realtime bars, you can use this technique, which only returns a value from the higher timeframe on the bar after it completes:indexHighTF = barstate.isrealtime ? 1 : 0indexCurrTF = barstate.isrealtime ? 0 : 1nonRepaintingClose = request.security(syminfo.tickerid, "1D", close[indexHighTF])[indexCurrTF]plot(nonRepaintingClose, "Non-repainting close")// Returns the 1H close of "AAPL", extended session included. The value is dividend-adjusted.extendedTicker = ticker.modify("NASDAQ:AAPL", session = session.extended, adjustment = adjustment.dividends)aaplExtAdj = request.security(extendedTicker, "60", close)plot(aaplExtAdj)// Returns the result of a user-defined function.// The `max` variable is mutable, but we can pass it to `request.security()` because it is wrapped in a function.allTimeHigh(source) =>var max = source max := math.max(max, source)allTimeHigh1D = request.security(syminfo.tickerid, "1D", allTimeHigh(high))// By using a tuple `expression`, we obtain several values with only one `request.security()` call.[open1D, high1D, low1D, close1D, ema1D] = request.security(syminfo.tickerid, "1D", [open, high, low, close, ta.ema(close, 10)])plotcandle(open1D, high1D, low1D, close1D)plot(ema1D)// Returns an array containing the OHLC values of the chart's symbol from the 1D timeframe.ohlcArray = request.security(syminfo.tickerid, "1D", array.from(open, high, low, close))plotcandle(array.get(ohlcArray, 0), array.get(ohlcArray, 1), array.get(ohlcArray, 2), array.get(ohlcArray, 3))