Pine Script array.push() — How to Add Elements to Arrays
array.push(id, value) appends one element to an existing Pine array and returns no value. The array's element type determines which values can be added. Use it when you need an explicit collection, such as a bounded history of selected events; ordinary Pine series already support historical references for many bar-by-bar calculations.
How array.push() Works
The newest appended element occupies index array.size(id) - 1. Index zero remains the oldest element if you only append and remove from the front. array.shift() removes the first element; array.pop() removes the last. Check the array is nonempty before removing an element.
TradingView's array documentation covers the API, element types and persistence. An array is not a data feed: appending a ticker string does not request that symbol's prices.
A Complete Bounded-Window Example
This indicator stores up to a chosen number of confirmed closing prices. It plots their median and exposes the stored count in the Data Window. It is an educational data-handling example, not a trading strategy.
//@version=6
indicator("Confirmed close array window", overlay = true)
int capacity = input.int(20, "Window size", minval = 1, maxval = 500)
var array<float> closes = array.new<float>()
var float windowMedian = na
if barstate.isconfirmed
array.push(closes, close)
if array.size(closes) > capacity
array.shift(closes)
windowMedian := array.median(closes)
plot(windowMedian, "Median of stored closes", color = color.orange)
plot(array.size(closes), "Stored count", display = display.data_window)
Follow the Update Order
The var declaration creates one persistent array. On each confirmed bar, the script appends the close, removes the oldest value if the capacity is exceeded, and calculates the median of what remains. The operation leaves at most capacity elements after that update.
During the first few bars, the median uses the available observations, not a padded full window. On an open realtime bar, it retains the last confirmed result. barstate.isconfirmed is true on historical bars and on the closing update of a realtime bar; the example's chart execution context matters when interpreting this behavior.
Inspect the Data Window after changing the capacity to one, two and twenty. The count should grow only until the cap, while capacity one makes the confirmed median equal the latest stored close. This gives you observable checks beyond whether the script compiles.
Persistence and Realtime Updates
Removing var would create a fresh empty array on each execution; appending once would then leave one element. That can be useful for a temporary calculation, but it would destroy this rolling history.
var is not inherently a bug. Unbounded retention is the issue when the intended history is finite. Realtime rollback also matters: ordinary persistent arrays do not automatically retain every tick update as separate permanent observations. varip has different intrabar persistence behavior and should be used only when that behavior is intended. Review the execution model before designing a tick collector.
Avoid the Off-by-One Capacity Error
If you remove only when size is already greater than fifty before appending, the array can finish with fifty-one elements. Either append then remove while over the cap, as above, or make room before appending when size is already at the cap. If an operation can append several elements, one removal may not be enough.
Arrays have a documented limit of 100,000 elements each. That is not a recommended working size, and there is no universal failure threshold at one thousand or fifty thousand elements. Runtime depends on the whole script and its operations. See TradingView's resource limits.
Sorting, Medians and Chronological Order
Sorting the same rolling array in place changes its order. A subsequent shift() would remove the smallest value rather than the oldest one. Preserve the chronological array and sort a copy if a sorted view is needed, or use an appropriate statistical function without manually rearranging the stored history.
For an even number of observations, selecting the element at the floored middle index is not the usual median; it selects the upper middle element in zero-based indexing. The example uses array.median() instead of that incomplete calculation.
Selective Storage and Multiple Symbols
You can append only when an event occurs, such as a confirmed change exceeding a specified ATR threshold. The resulting collection is an event history, not the last N consecutive bars. Its size reduction depends on event frequency; no general 80% memory saving is established.
For multi-symbol work, request each intended symbol's data and define the update schedule before storing it. Repeatedly appending syminfo.ticker and RSI from close only repeats the current chart's symbol and RSI. Arrays do not bypass request limits or turn that pattern into an S&P 500 screener.
Queue and Stack Choices
Appending and removing from the front implements a queue: oldest in, oldest out. Appending and removing from the end implements a stack: newest in, newest out. A push immediately followed by a pop leaves no retained history. Choose the structure from the data you need rather than assuming a stack improves trailing stops.
For regular lookbacks, compare built-in series functions before building an array. Explicit arrays are most useful when selection, ordering or retention differs from ordinary consecutive-bar history.

