Arrays in Pine Script — push, shift and the var trap · Pine Script EP 15 #shorts

A Pine Script array is a list your script keeps. array.new<float>() creates an empty one; push() adds a value to the end, shift() removes the first (oldest) element and returns it — so push + shift with a size check keeps a rolling window of the last 5 values. Declare it with var: without var the array is created again, empty, on every bar; with var it is created once and kept bar to bar. avg(), size() and get() are built in, and a for…in loop walks every element. An array can hold up to 100,000 elements.

The code:
//@version=6
indicator(“Demo · array”)
var ranges = array.new<float>()
ranges.push(high – low)
if ranges.size() > 5
ranges.shift()
plot(ranges.avg(), color = color.orange)

Pine Script EP 15. Next: EP 16 — inputs that look pro.

Educational example.

Your rules. Coded. · marketalgobot.com
https://marketalgobot.com

#pinescript #tradingview #coding #learntocode #marketalgobot