Tutorial

request.security_lower_tf in Pine Script: Intrabar Data

Learn how request.security_lower_tf returns intrabar arrays in Pine Script v6, with three complete scripts and the mistakes that break them.

•10 min read

You have a 1 hour chart and a question the hourly candle cannot answer. Did the volume of that bar arrive while price was rising or falling? Did the high form in the first ten minutes or the last ten? The candle only stores four prices and one volume number, so everything that happened in between is gone. request.security_lower_tf() is the Pine Script function that brings it back by returning the lower timeframe bars that make up each chart bar.

It is also one of the easiest functions in Pine Script to use wrongly. It does not return a series like request.security() does. It returns an array, the array has a different length on different bars, and the number of bars it can hold is limited. This guide walks through what the function returns, three complete v6 scripts that use it, and the mistakes that cause most of the confusing errors.

What request.security_lower_tf() actually returns

The signature is request.security_lower_tf(symbol, timeframe, expression, ignore_invalid_symbol, currency, ignore_invalid_timeframe, calc_bars_count). The first three arguments do the work. You give it a symbol, a timeframe that is lower than the chart's timeframe, and an expression to evaluate on each of those lower timeframe bars.

Compare that with request.security, which evaluates the expression on a higher or equal timeframe and hands you back one value per chart bar. When you request a lower timeframe with request.security(), Pine has no way to fit several values into one slot, so it returns only the last one. That is almost never what you wanted. request.security_lower_tf() solves this by returning every value as an array, ordered from the earliest intrabar to the latest.

If the chart is on 60 minutes and you ask for 1 minute data, each chart bar gets an array of up to 60 elements. On the next bar you get a new array of up to 60 elements. The array belongs to that one bar only, so there is nothing to carry over unless you store it yourself. Because the return type is array<float> for a float expression, everything you already know about Pine Script arrays applies: you loop over it, you ask for its size, you take its max, min, sum, or average.

You can also pass a tuple as the expression. If you request [close, open, volume] you get back three arrays, and element i of each array describes the same intrabar. That matters because it keeps related values aligned without any extra work.

The timeframe rule

The timeframe you request must be lower than the timeframe of the chart. Ask for 1 hour data on a 15 minute chart and the script stops with a runtime error, because there is no way to split a 15 minute bar into one hour pieces. This is the reverse of the rule for higher timeframe requests, and it is the first thing worth checking when a script that worked on one chart fails on another.

You can handle this in two ways. The function has an ignore_invalid_timeframe parameter that tells it to tolerate an invalid timeframe instead of raising an error. The other way is to compare the two timeframes yourself with timeframe.in_seconds() and stop with a message that tells the user what to change. A clear message is usually kinder than a silent blank indicator, so the scripts below use runtime.error() with an explanation.

The intrabar timeframe is also a trade-off you control. A smaller intrabar timeframe gives you finer detail, but it means more bars per chart bar, and TradingView limits how many intrabars a single call can return. The exact limit depends on your TradingView plan and can change, so check the Pine Script user manual on TradingView for the current number. In practice the rule of thumb is to request the coarsest intrabar timeframe that still answers your question. A 5 minute intrabar on a 1 hour chart gives 12 values per bar and reaches much further back in history than a 1 minute request gives 60 values per bar.

Example 1: splitting a bar's volume into up and down

The classic use is splitting volume. A 1 hour bar can close higher while most of its volume traded on falling minutes. The bar's colour tells you nothing about that, but the intrabars do. The script below requests the open, close and volume of each 1 minute bar, then adds each bar's volume to an up total or a down total depending on whether that minute closed above its open.

pine
//@version=6
indicator("Intrabar Up/Down Volume", format = format.volume)

string ltf = input.timeframe("1", "Intrabar timeframe")

if timeframe.in_seconds(ltf) >= timeframe.in_seconds(timeframe.period)
    runtime.error("Pick an intrabar timeframe lower than the chart timeframe.")

[opens, closes, vols] = request.security_lower_tf(syminfo.tickerid, ltf, [open, close, volume])

float upVol = 0.0
float downVol = 0.0

for [i, v] in vols
    if array.get(closes, i) >= array.get(opens, i)
        upVol += v
    else
        downVol += v

plot(upVol, "Up volume", color = color.teal, style = plot.style_columns)
plot(-downVol, "Down volume", color = color.red, style = plot.style_columns)

A few details are worth slowing down for. The loop uses the for [i, v] in vols form rather than for i = 0 to array.size(vols) - 1. They look equivalent, but the counted version has a trap. When the array is empty, array.size(vols) - 1 is -1, and a Pine for loop from 0 to -1 counts downward and visits index 0 and then -1, which raises an out of bounds error. The for...in form simply does nothing on an empty array, so it is safe without any extra guard.

The script also creates upVol and downVol fresh on every bar. That is deliberate. The array describes only the current chart bar, so the totals should start from zero each time. If you declared them with var they would accumulate across the whole chart, which gives you a running total instead of a per bar split.

Finally, notice that this is an estimate built from what each intrabar did, not a record of who bought and sold. If you want to push the idea further into cumulative terms, the delta flow post covers an estimated cumulative delta built on the same intrabar principle.

Example 2: where inside the bar did the high form

Arrays of intrabar values let you ask about timing, which no chart bar can tell you. If the high of an hourly bar formed in its first minutes and price then drifted lower, that is a different bar from one that made its high in the last minutes, even if the candles look the same.

Because the array is ordered by time, the position of the maximum within the array is the position of the high within the bar. The script below expresses that position as a percentage, where 0 means the first intrabar and 100 means the last.

pine
//@version=6
indicator("Where the High Formed", format = format.percent)

string ltf = input.timeframe("1", "Intrabar timeframe")

if timeframe.in_seconds(ltf) >= timeframe.in_seconds(timeframe.period)
    runtime.error("Pick an intrabar timeframe lower than the chart timeframe.")

array<float> highs = request.security_lower_tf(syminfo.tickerid, ltf, high)

int n = array.size(highs)
float highPos = na

if n > 1
    int idx = array.indexof(highs, array.max(highs))
    highPos := idx / (n - 1) * 100

plot(highPos, "High position", style = plot.style_columns)
hline(50, "Middle of bar")

The guard n > 1 is doing real work. With zero elements there is no maximum, and with exactly one element the division by n - 1 would divide by zero. Leaving highPos as na on those bars is the honest result, because the data to answer the question is not there. The plot simply skips them.

If the same high price appears in several intrabars, array.indexof() returns the first one. That is usually what you want when asking when the high was first reached, but it is worth knowing before you read too much into one value. This is the kind of detail that shapes the answer, and it is why describing intrabar logic precisely matters.

Example 3: checking that the array is as long as you think

The most common wrong assumption about this function is that the array always has a fixed length. It does not. A 1 hour bar on a 1 minute request is expected to hold 60 elements, but it holds fewer whenever the market had no trades in some minutes, whenever data is missing, and on partial bars. On a symbol that trades around the clock the arrays are usually full, but on one with sessions, gaps and half days, they vary a lot.

Before you trust any intrabar calculation, plot how many intrabars you actually received next to how many you expected.

pine
//@version=6
indicator("Intrabar Coverage", precision = 0)

string ltf = input.timeframe("1", "Intrabar timeframe")

if timeframe.in_seconds(ltf) >= timeframe.in_seconds(timeframe.period)
    runtime.error("Pick an intrabar timeframe lower than the chart timeframe.")

array<float> vols = request.security_lower_tf(syminfo.tickerid, ltf, volume)

int expected = int(timeframe.in_seconds(timeframe.period) / timeframe.in_seconds(ltf))
int got = array.size(vols)

plot(got, "Intrabars received", style = plot.style_columns)
plot(expected, "Intrabars expected", color = color.gray)
bgcolor(got < expected ? color.new(color.orange, 85) : na)

Orange bars mark chart bars where the array came back short. If a lot of the chart is orange, any statistic you compute from the array, such as an average, a maximum or a share of volume, is built on incomplete bars. You can decide whether that matters for your purpose, but you should decide it knowingly. Many surprising results from intrabar scripts turn out to be coverage problems rather than logic problems.

The realtime bar behaves differently

On historical bars, the array for each chart bar is complete from the start, because all the intrabars already exist. On the realtime bar, the array grows as new intrabars arrive, so the values you calculate change as the bar develops. The up volume of a 1 hour bar at minute 20 is only the first 20 minutes of it.

This is the same effect that makes any intrabar value repaint on the live bar, and it is the reason a script can look different on a fresh load than it did while you watched it. If you use intrabar values to drive an alert or a marker, wait for the bar to finish with barstate.isconfirmed, or accept that the value is provisional. The repainting guide lists the other ways a script can behave differently live and on history.

What about the Bar Magnifier?

A strategy that needs intraday fills uses a different tool. The Bar Magnifier makes the Strategy Tester use lower timeframe bars to decide order fills. It changes how orders are simulated, but it does not hand you the intrabar values for use in your own calculations. If you need to read intrabar data in your own logic, as in the examples above, request.security_lower_tf() is the function that does it.

Common mistakes

The first mistake is requesting a timeframe that is not lower than the chart's. It works on one chart and breaks on the next, and the cure is the explicit check shown above.

The second is treating the result as a series. You cannot plot an array, add it to a number, or compare it to a price. You have to reduce it to a single value first, using array.sum(), array.avg(), array.max(), array.min() or a loop.

The third is forgetting that an empty array is a valid result. Any code that reads element 0, or divides by the array's size, needs a guard. The for...in loop handles the empty case by doing nothing, but explicit index access does not.

The fourth is requesting too fine an intrabar timeframe. If a script loads slowly, or stops with a limit error on a long chart, move the intrabar timeframe up a step before you rewrite any logic. The slow scripts guide covers other causes of slow scripts.

The fifth is calling the function more times than you need. Each call is a separate data request. If you need the open, close and volume, request them together as a tuple, as in Example 1, rather than making three calls.

Getting intrabar logic written and checked

Intrabar code is the kind of Pine Script that is tedious to write by hand and easy to get subtly wrong: array lengths that vary, loops that fail on empty arrays, a timeframe rule that only shows up on some charts. If you would rather describe the result than hand-write the loops, the AI editor in PineScripter takes a plain English description such as "split each bar's volume into up and down using 1 minute data" and writes Pine Script v6. It checks the code for compile errors the same way TradingView does, fixes what it finds itself, and when you ask for a change later it edits only the lines that change, so a tweak to the intrabar timeframe does not rewrite the script. The code is yours to copy into TradingView.

Whether you write it yourself or not, the habit worth keeping is the one from Example 3. Look at what the array actually contains before you build on it. The function is powerful because it exposes what the candle hides, and the same detail is exactly why you need to check it.

Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.

Back to Blog

Frequently asked questions

What does request.security_lower_tf return?

It returns an array holding the expression value for every lower timeframe bar inside the current chart bar, ordered from earliest to latest. With a tuple expression it returns one array per element.

Why does my script fail when I change the chart timeframe?

The requested timeframe must be lower than the chart timeframe. If you move to a chart at or below the intrabar timeframe, the call raises a runtime error. Compare the two with timeframe.in_seconds() and stop with a clear message.

Why is the array shorter than I expected?

Intrabar arrays only hold bars that exist. Minutes with no trades, session gaps, missing data and partial bars all produce shorter arrays, so plot array.size() against the expected count before trusting a result.

How is it different from request.security?

request.security returns one value per chart bar and, on a lower timeframe request, keeps only the last value. request.security_lower_tf returns all the intrabar values as an array.

Can I loop over an empty array safely?

Use the for...in form. It does nothing on an empty array, while a counted loop from 0 to array.size() - 1 runs backwards when the size is zero and causes an out of bounds error.

Describe your indicator in plain English and get Pine Script that compiles →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.