Arrays#
An Array is an ordered collection of values that can have elements dynamically added or removed. Arrays are very useful when you need to manage a set of similar data — for example, closing prices of the most recent N bars, or the positions of multiple signal markers.
Creating Arrays#
array.new#
Use array.new<type>() to create an empty array of a specified type:
prices = array.new<float>() // float array
flags = array.new<bool>() // boolean array
labels = array.new<string>() // string arrayCreate an array with an initial size and default value:
// Create an array of length 5, all initialized to 0.0
prices = array.new<float>(5, 0.0)array.from#
Create an array directly from known values (the most convenient method):
levels = array.from(100.0, 105.0, 110.0, 95.0, 90.0)Adding and Removing Elements#
| Function | Description |
|---|---|
array.push(id, value) | Add an element to the end of the array |
array.unshift(id, value) | Add an element to the beginning of the array |
array.pop(id) | Remove and return the last element |
array.shift(id) | Remove and return the first element |
array.remove(id, index) | Remove the element at the specified index |
array.clear(id) | Clear the entire array |
//@version=6
indicator("Array Add/Remove")
var recentCloses = array.new<float>()
// Push the latest close on every bar, keeping at most 5 elements
array.push(recentCloses, close)
if array.size(recentCloses) > 5
array.shift(recentCloses) // Remove the oldest
// Display array size
plot(array.size(recentCloses), "Array Size")Reading and Modifying Elements#
| Function | Description |
|---|---|
array.get(id, index) | Get the value at a specified index |
array.set(id, index, value) | Set the value at a specified index |
array.size(id) | Return the array length |
array.first(id) | Get the first element |
array.last(id) | Get the last element |
levels = array.from(90.0, 95.0, 100.0, 105.0, 110.0)
firstLevel = array.first(levels) // 90.0
lastLevel = array.last(levels) // 110.0
thirdLevel = array.get(levels, 2) // 100.0 (index starts at 0)
array.set(levels, 2, 101.0) // Change the 3rd element to 101.0Statistical Functions#
Pine Script provides a set of array statistical functions:
| Function | Description |
|---|---|
array.avg(id) | Average |
array.sum(id) | Sum |
array.max(id) | Maximum |
array.min(id) | Minimum |
array.stdev(id) | Standard deviation |
array.median(id) | Median |
array.mode(id) | Mode |
//@version=6
indicator("Array Statistics")
var closes = array.new<float>()
array.push(closes, close)
if array.size(closes) > 20
array.shift(closes)
if array.size(closes) == 20
plot(array.avg(closes), "Average", color=color.blue)
plot(array.max(closes), "Max", color=color.green)
plot(array.min(closes), "Min", color=color.red)
plot(array.stdev(closes), "Std Dev", color=color.purple)Sorting and Searching#
prices = array.from(30.0, 10.0, 50.0, 20.0, 40.0)
// Sort (modifies the original array)
array.sort(prices, order.ascending) // ascending: 10, 20, 30, 40, 50
array.sort(prices, order.descending) // descending: 50, 40, 30, 20, 10
// Search
idx = array.indexof(prices, 30.0) // Returns index of 30.0, or -1 if not found
contains = array.includes(prices, 30.0) // true / falseCopying and Combining#
original = array.from(1.0, 2.0, 3.0)
// Copy the array
copied = array.copy(original)
// Append another array to the end
extra = array.from(4.0, 5.0)
array.concat(original, extra) // original becomes [1, 2, 3, 4, 5]Practical Example: Dynamic Support/Resistance Levels#
//@version=6
indicator("Dynamic Support/Resistance", overlay=true)
lookback = input.int(50, "Lookback Bars")
threshold = input.float(0.5, "Merge Threshold (%)", step=0.1) / 100
// Collect recent pivot highs and lows
var levels = array.new<float>()
// Check each bar for pivot points
isPivotHigh = ta.pivothigh(high, 3, 3)
isPivotLow = ta.pivotlow(low, 3, 3)
if not na(isPivotHigh)
array.push(levels, isPivotHigh)
if not na(isPivotLow)
array.push(levels, isPivotLow)
// Keep only the most recent 20 key price levels
while array.size(levels) > 20
array.shift(levels)
// Draw all key price levels
if barstate.islast
for level in levels
line.new(bar_index - lookback, level, bar_index, level,
color=color.new(color.gray, 50), width=1)