February 4, 2021
Even More Pine Functions Now Support Dynamic Length Arguments

In September, some of our built-in Pine functions started supporting dynamic values as their length arguments. And now we’ve expanded this list even further. The following functions can now also be used with dynamically calculated lengths:

bb()
bbw()
cci()
cmo()
cog()
correlation()
dev()
falling()
mfi()
percentile_linear_interpolation()
percentile_nearest_rank()
percentrank()
rising()
roc()
stdev()
stoch()
variance()
wpr()

Passing series as the length argument makes it easier to create adaptive Pine indicators that use calculations based on variable periods. For example, the following script uses the built-in stdev() function to calculate the Standard Deviation bands for the VWAP indicator. The length of the stdev() calculation increases by 1 with every bar until the VWAP period resets:

//@version=4
study(title="VWAP", shorttitle="VWAP", overlay=true)
src = input(title = "Source", type = input.source, defval = hlc3)
t = time("D")
start = na(t[1]) or t > t[1]
sumSrc = src * volume
sumVol = volume
sumSrc := start ? sumSrc : sumSrc + sumSrc[1]
sumVol := start ? sumVol : sumVol + sumVol[1]
vwapValue = sumSrc / sumVol
var barsSinceStart = 0
if start
    barsSinceStart := 0
barsSinceStart := barsSinceStart + 1
stdevLen = bar_index == 0 ? 3000 : barsSinceStart // max_bars_back workaround
vwapStdev = stdev(vwapValue, stdevLen)
plot(vwapValue, title="VWAP", color=color.blue)
upperBand = plot(vwapValue+vwapStdev, title="Upper Band", color=color.green)
lowerBand = plot(vwapValue-vwapStdev, title="Lower Band", color=color.green)
upperBand2 = plot(vwapValue+vwapStdev*2, title="Upper Band 2", color=color.yellow)
lowerBand2 = plot(vwapValue-vwapStdev*2, title="Lower Band 2", color=color.yellow)
fill(upperBand2, lowerBand2, color=color.yellow, transp=95)

Keep in mind that these functions require evaluation on every bar, so they should be used outside `for` loops or `if` blocks that are not executed on every bar. Additionally, passing a series as the length for a built-in function can sometimes cause the `Pine cannot determine the referencing length of a series` error. You can learn more about it and how to work around the error in our Help Center.

Read about all updates to Pine in our User Manual’s Release Notes.

We hope this new feature you requested is useful. Please continue to send us your feedback and suggestions. We’re building TradingView for you, and we’re excited to hear what you think about our platform updates.

The fastest way to follow markets

Launch Chart