Pine Script v6 data requests

Pine Script request.security: Context and Timeframe Alignment

Pine Script request.security() evaluates an expression in a requested symbol and timeframe, then maps its result onto chart bars. For a confirmed higher-timeframe value, use a previous-bar expression such as close[1] with barmerge.lookahead_on, and require the requested timeframe to be strictly higher. The offset selects completed data; lookahead_on makes it available at the start of the next HTF period. This timing rule does not apply unchanged to lower-timeframe requests.

Review My Data Requests

Chart context and requested context are different

On a 15-minute chart, requesting a 20-period hourly EMA means calculating over hourly closes before alignment. Calculating an EMA after requesting and filling hourly closes instead averages a chart-bar series with repeated values. Those are different calculations. Context also includes session settings, price adjustments, and chart type.

  • For another symbol at the chart timeframe, start with timeframe.period. Do not add an HTF delay unless the intended rule requires one.
  • For invalid symbols, ignore_invalid_symbol = true returns na instead of stopping the script. Handle that missing value explicitly.
ArgumentDecision to make
symbolUse syminfo.tickerid for the chart ticker, or an explicit exchange-qualified ticker for another market.
timeframe"60" means 60 minutes; "1D" means one day. An empty string uses the chart timeframe.
expressionCalculate in the requested context. Put the history offset inside the expression when selecting a previous HTF bar.
gapsgaps_off fills missing mapped values; gaps_on leaves na where new requested data is not yet confirmed.
lookaheadControls historical alignment. lookahead_off is the default; lookahead_on needs special care with HTF expressions.

Choose an HTF confirmation rule before writing a signal

Historical bars run with completed data, while an open HTF candle can change during live execution. A chart-bar close does not also close that HTF candle. TradingView documents that barstate.isconfirmed does not work inside request.security(); using it outside the request only confirms the chart bar.

  • The confirmed pattern delays information until the next HTF period. It does not predict the current period close.
  • Offset each tuple member that needs confirmation. Offsetting the returned series afterward moves by chart bars, not requested bars.
  • Data-feed revisions can still change history. A confirmed request is not a guarantee about every dependency in a complete trading script.
HTF expressionAlignmentConsequence
Current closelookahead_offNo historical future leak, but the live developing close can change after reload.
Current closelookahead_onHistorical bars receive the final HTF close too early. This leaks future information.
Previous close: close[1]lookahead_onPrevious completed HTF close from the start of the new HTF period.
Previous close: close[1]lookahead_offHistorical delivery waits until the end of the HTF period. This is not the same timing as the preceding row.

A four-bar timeline: 15-minute chart, 60-minute request

This synthetic example assumes uninterrupted trading and hourly bars aligned at 10:00 and 11:00. The 08:00 hour closed at 98, the 09:00 hour at 100, and the 10:00 hour eventually closes at 104. Read each row at the close of that 15-minute chart bar. All historical columns use gaps_off.

  • At the first update of the new 11:00 hour, previous close + lookahead_on advances to 104.
  • Current close + lookahead_on would show 104 on all four historical bars, including bars that ended before 104 was knowable.
  • The live column is an invented price path for explaining timing, not a measured trading result. Exchange sessions and missing bars can change the bar grid.
Chart barCurrent close + off, reloaded historyCurrent close + off, illustrative live valuePrevious close + on, history and livePrevious close + off, reloaded history
10:00 to 10:1510010110098
10:15 to 10:3010010210098
10:30 to 10:451009910098
10:45 to 11:00104104100100

Pine Script v6: compare developing and confirmed HTF data

Run this indicator on a standard 15-minute chart with the default 60-minute input. The orange line uses the current hourly close. The blue line and teal EMA use the previous completed hour. The input guard rejects equal or lower timeframes; the EMA may be na while its requested history is insufficient.

  • Record both close values during a live hour and at its boundary, then reload the chart. Compare timestamps as well as prices.
  • Check the blue value against the preceding completed hourly candle. Check the EMA on an hourly chart with the same symbol, session, and length.
  • Repeat with equal and lower timeframe inputs to verify the runtime guard. Syntax validation alone cannot establish live-versus-history consistency.
One developing request and one confirmed tuple requestpinescript
//@version=6
indicator("HTF alignment comparison [Pineify]", overlay = true)

htf = input.timeframe("60", "Higher timeframe")
length = input.int(20, "HTF EMA length", minval = 1)

if timeframe.in_seconds(htf) <= timeframe.in_seconds()
    runtime.error("Choose a timeframe strictly higher than the chart.")

developingClose = request.security(syminfo.tickerid, htf, close,
     gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
[confirmedClose, confirmedEma] = request.security(syminfo.tickerid, htf,
     [close[1], ta.ema(close, length)[1]],
     gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)

plot(developingClose, "Developing HTF close", color = color.orange)
plot(confirmedClose, "Previous HTF close", color = color.blue,
     linewidth = 2, style = plot.style_stepline)
plot(confirmedEma, "Previous HTF EMA", color = color.teal, linewidth = 2)

Gaps control missing values, not confirmation policy

With an unshifted HTF expression, gaps_off uses the last confirmed value on historical bars and the developing value on realtime bars. gaps_on returns na when new requested data is not confirmed. Neither option replaces a deliberate expression offset and alignment rule.

  • If missing values matter, use plot.style_linebr to avoid drawing a line across na gaps.
  • Do not replace missing data with zero unless zero has a defined meaning in the calculation.
  • For two symbols, check trading sessions and timestamps before comparing prices. A carried value can be stale while the other market is trading.

Lower timeframes: one sample cannot describe every intrabar

For lower-timeframe data, request.security() returns only one intrabar result per chart bar. Historically, lookahead_off selects the last available intrabar and lookahead_on the first; realtime requests track the latest available value. Use request.security_lower_tf() for an array of available intrabar values, and handle an empty array.

  • Synthetic example: four available closes of 100, 103, 99, and 101 have a mean of 100.75. Selecting only the last close returns 101 and loses that distribution.
  • This mean is not the chart candle close or a volume-weighted price. The live array can change while the chart bar is open.
  • Available intrabar counts can vary with feed gaps, trading activity, and historical-data limits. Do not assume every 5-minute bar contains five 1-minute observations.
Pine Script v6: mean of available intrabar closespinescript
//@version=6
indicator("Intrabar close mean [Pineify]", overlay = true)

ltf = input.timeframe("1", "Lower timeframe")
if timeframe.in_seconds(ltf) >= timeframe.in_seconds()
    runtime.error("Choose a timeframe strictly lower than the chart.")

closes = request.security_lower_tf(syminfo.tickerid, ltf, close)
meanClose = array.size(closes) > 0 ? array.avg(closes) : na

plot(meanClose, "Mean available intrabar close", color = color.purple)
plot(array.size(closes), "Available intrabar count", display = display.data_window)

Version and request-budget checks

Pine v4 used security(); v5 moved it to request.security() and renamed the resolution parameter to timeframe. Pine v6 enables dynamic requests by default, including series context arguments and requests in local scopes. A loop is not inherently wrong; the number of distinct requests it executes matters.

  • TradingView currently permits 40 unique request.*() calls, or 64 on Ultimate. This budget spans the request namespace, not just request.security().
  • Group values from one context in a tuple when appropriate. Tuples and arrays are different types; one tuple request returns separate series values.
  • Identical requests can reuse data. Changing symbols or timeframes dynamically can consume additional unique requests; count those contexts before scaling a scanner.
Where Pineify fits

Pine Script AI Coding Agent

Bring your script, chart timeframe, requested timeframe, and intended update time. Ask the agent to explain the requests and draft a revision. Review the source, check syntax, and compare recorded live values with reloaded history before relying on signals.

Review My Data Requests

Frequently asked questions

Educational programming examples, not investment advice. Confirmation timing, feed corrections, session differences, and missing intrabars affect results. Syntax checks do not prove that an entire script avoids repainting or produces profitable trades.

Sources and verification