array

The array namespace provides dynamic array manipulation and statistical operations. Arrays are Python lists that can store values of a specific type. Use array functions to create, modify, search, and analyze collections of data.

Quick Example

from pynecore.lib import (
    close, array, script
)

@script.indicator(title="Array Statistics", overlay=False)
def main():
    # Create and populate an array of closes
    closes: list[float] = array.new_float()
    array.push(closes, close)
    
    if array.size(closes) > 20:
        avg: float = array.avg(closes)
        max_val: float = array.max(closes)
        min_val: float = array.min(closes)

Functions

Creation

new_bool(size, initial_value)

Creates a new boolean array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valueboolInitial value for all elements, default na
Returnslist[bool]New boolean array

new_int(size, initial_value)

Creates a new integer array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valueintInitial value for all elements, default na
Returnslist[int]New integer array

new_float(size, initial_value)

Creates a new float array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuefloatInitial value for all elements, default na
Returnslist[float]New float array

new_string(size, initial_value)

Creates a new string array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuestrInitial value for all elements, default na
Returnslist[str]New string array

new_color(size, initial_value)

Creates a new color array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuecolorInitial value for all elements, default na
Returnslist[color]New color array

new_label(size, initial_value)

Creates a new label array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuelabelInitial value for all elements, default na
Returnslist[label]New label array

new_line(size, initial_value)

Creates a new line array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuelineInitial value for all elements, default na
Returnslist[line]New line array

new_box(size, initial_value)

Creates a new box array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valueboxInitial value for all elements, default na
Returnslist[box]New box array

new_linefill(size, initial_value)

Creates a new linefill array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuelinefillInitial value for all elements, default na
Returnslist[linefill]New linefill array

new_table(size, initial_value)

Creates a new table array.

ParameterTypeDescription
sizeintInitial size, default 0
initial_valuetableInitial value for all elements, default na
Returnslist[table]New table array

from_items(*items)

Creates an array from the specified elements. (In Pine Script this is array.from(), but from is a reserved keyword in Python.)

ParameterTypeDescription
*itemsanyElements to include
ReturnslistArray containing the elements

Example: arr: list[int] = array.from_items(1, 2, 3) # [1, 2, 3]

Access

get(id, index)

Returns the element at the specified index.

ParameterTypeDescription
idarrayInput array
indexintIndex of element; a float is truncated to an integer
ReturnsanyElement at the index, or na when index is na

Example: val: float = array.get(my_array, 0) # first element

first(id)

Returns the first element. Throws an error if the array is empty.

ParameterTypeDescription
idarrayInput array
ReturnsanyFirst element

last(id)

Returns the last element. Throws an error if the array is empty.

ParameterTypeDescription
idarrayInput array
ReturnsanyLast element

size(id)

Returns the number of elements in the array.

ParameterTypeDescription
idarrayInput array
ReturnsintArray size

Example: len: int = array.size(my_array) # 10

Modification

set(id, index, value)

Sets the element at the specified index.

ParameterTypeDescription
idarrayInput array
indexintIndex to set; a float is truncated to an integer
valueanyNew value

An na index is a no-op. A non-na index outside the array still raises an error.

push(id, value)

Appends an element to the end of the array.

ParameterTypeDescription
idarrayInput array
valueanyElement to append

pop(id)

Removes and returns the last element.

ParameterTypeDescription
idarrayInput array
ReturnsanyRemoved element

Example: last: float = array.pop(my_array) # removed from array

shift(id)

Removes and returns the first element.

ParameterTypeDescription
idarrayInput array
ReturnsanyRemoved element

Example: first: float = array.shift(my_array) # removed from array

unshift(id, value)

Inserts an element at the beginning of the array.

ParameterTypeDescription
idarrayInput array
valueanyElement to insert

insert(id, index, value)

Inserts an element at the specified index.

ParameterTypeDescription
idarrayInput array
indexintIndex to insert at; a float is truncated to an integer
valueanyElement to insert

An na index appends the value to the end of the array.

remove(id, index)

Removes and returns the element at the specified index.

ParameterTypeDescription
idarrayInput array
indexintIndex to remove; a float is truncated to an integer
ReturnsanyRemoved element, or na when index is na

An na index leaves the array unchanged.

Example: removed: float = array.remove(my_array, 2) # element at index 2

clear(id)

Removes all elements from the array.

ParameterTypeDescription
idarrayInput array

fill(id, value, index_from, index_to)

Fills a range of elements with a specified value.

ParameterTypeDescription
idarrayInput array or slice view
valueanyValue to fill with
index_fromintStart index, inclusive (default 0); na means 0
index_tointEnd index, exclusive (optional); na means the array size

Bounds are clamped to the existing array, so filling never changes its size.

Example: array.fill(my_array, 0.0, 5, 10) # fill indices 5-9 with 0.0

includes(id, value)

Returns true if the value exists in the array.

ParameterTypeDescription
idarrayInput array
valueanyValue to search for
ReturnsboolTrue if found, false otherwise

Example: found: bool = array.includes(my_array, 42) # True or False

indexof(id, value)

Returns the index of the first occurrence, or -1 if not found.

ParameterTypeDescription
idarrayInput array
valueanyValue to search for
ReturnsintIndex or -1

lastindexof(id, value)

Returns the index of the last occurrence, or -1 if not found.

ParameterTypeDescription
idarrayInput array
valueanyValue to search for
ReturnsintIndex or -1

binary_search(id, val)

Binary search in a sorted array. Returns the index or -1 if not found.

ParameterTypeDescription
idarraySorted array (ascending)
valanyValue to search for
ReturnsintIndex or -1

binary_search_leftmost(id, val)

Binary search in a sorted array. Returns the index of the value or the leftmost element greater than it.

ParameterTypeDescription
idarraySorted array (ascending)
valanyValue to search for
ReturnsintIndex

binary_search_rightmost(id, val)

Binary search in a sorted array. Returns the index of the value or the rightmost element less than it.

ParameterTypeDescription
idarraySorted array (ascending)
valanyValue to search for
ReturnsintIndex

Transformation

copy(id)

Creates a shallow copy of the array.

ParameterTypeDescription
idarrayInput array
ReturnsarrayCopy of the array

The array storage is independent, but reference-type elements such as drawings and user-defined objects remain shared with the original array.

Example: arr_copy: list[float] = array.copy(my_array) # independent array storage

concat(id1, id2)

Merges the second array into the first and returns the first.

ParameterTypeDescription
id1arrayFirst array (modified)
id2arraySecond array
ReturnsarrayFirst array with merged elements

slice(id, index_from, index_to)

Creates a view of a slice of the array. Changes made through the view are visible in the original array and vice versa.

ParameterTypeDescription
idarrayInput array
index_fromintStart index, inclusive; na means 0
index_tointEnd index, exclusive; na means the array size
Returnsarray viewView of the selected elements

The view is writable: push(), insert(), unshift(), remove(), pop(), shift(), clear() and concat() on a slice change the original array as well, inside the slice’s own bounds. Pushing to a slice therefore inserts at the slice end, not at the end of the original array, and pop() returns the slice’s last element rather than the array’s.

A slice is a plain index range over the original array, not a reference to the elements it held when it was created. Mutating the original array shifts what the view shows instead of moving the view, and a view whose range reaches past a shrunken array reports only the elements that still exist — without raising, and without losing its range when the array grows back.

index_from must address an existing element and index_to may reach one position past the last one, so index_from == index_to is a legal empty slice but a negative index, an index past the size, a reversed pair, or any slice of an empty array is an error.

reverse(id)

Reverses the order of elements in the array.

ParameterTypeDescription
idarrayInput array

sort(id, order)

Sorts the array in ascending or descending order.

ParameterTypeDescription
idarrayInput array
orderorderAscending or descending

In ascending order, na values sort after numeric values and before string values. Descending order reverses that result. sort_indices() follows the same ordering without changing the array.

sort_indices(id, order)

Returns indices that would sort the array without modifying the original.

ParameterTypeDescription
idarrayInput array
orderorderAscending or descending
Returnslist[int]Indices in sorted order

join(id, separator)

Concatenates all elements into a single string with separator.

ParameterTypeDescription
idarrayInput array
separatorstrSeparator string
ReturnsstrJoined string

Example: result: str = array.join(my_array, ",") # "1,2,3"

Statistics

sum(id)

Returns the sum of all elements.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsfloatSum

Example: total: float = array.sum(my_array) # 15.0

avg(id)

Returns the average (mean) of all elements.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsfloatAverage

Example: average: float = array.avg(my_array) # 5.0

min(id, nth)

Returns the smallest element or the element at the specified zero-based rank.

ParameterTypeDescription
idarrayInput array of numbers
nthintRank to return (default 0); na means 0 and a float is truncated
ReturnsnumberNth-smallest non-na value, or na when unavailable

Example: smallest: float = array.min(my_array) # 1.0

max(id, nth)

Returns the largest element or the element at the specified zero-based rank.

ParameterTypeDescription
idarrayInput array of numbers
nthintRank to return (default 0); na means 0 and a float is truncated
ReturnsnumberNth-largest non-na value, or na when unavailable

Example: largest: float = array.max(my_array) # 10.0

median(id)

Returns the median of all elements.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsfloatMedian value

mode(id)

Returns the most frequently occurring value (or smallest if tied).

ParameterTypeDescription
idarrayInput array of numbers
ReturnsnumberMost frequent value

stdev(id)

Returns the standard deviation of all elements.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsfloatStandard deviation

variance(id)

Returns the variance of all elements.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsfloatVariance

covariance(id1, id2, biased)

Returns the covariance between two arrays.

ParameterTypeDescription
id1arrayFirst array of numbers
id2arraySecond array of numbers
biasedboolIf true, use biased covariance (default)
ReturnsfloatCovariance

range(id)

Returns the difference between the maximum and minimum values.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsnumberRange

Example: r: float = array.range(my_array) # max - min

abs(id)

Returns an array of absolute values of each element.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsarrayArray of absolute values

Example: positives: list[float] = array.abs(my_array) # all positive

standardize(id)

Returns an array of standardized (z-score) values. na elements are excluded from the mean and population standard deviation and remain na in the result. Integer and float arrays use the same calculation. When all numeric elements are equal, each numeric result is 1.0.

ParameterTypeDescription
idarrayInput array of numbers
Returnslist[float]Standardized array

percentile_linear_interpolation(id, percentage)

Returns the value at the specified percentile using linear interpolation.

ParameterTypeDescription
idarrayInput array of numbers
percentagefloatPercentage (0-100); na produces na
ReturnsfloatValue at the percentile, or na for an empty array

percentile_nearest_rank(id, percentage)

Returns the value at the specified percentile using the nearest-rank method.

ParameterTypeDescription
idarrayInput array of numbers
percentagefloatPercentage (0-100); na is treated as 0
ReturnsfloatValue at the percentile, or na for an empty array

percentrank(id, index)

Returns the percentile rank of the element at the specified index.

ParameterTypeDescription
idarrayInput array of numbers
indexintElement index; na is treated as 0 and a float is truncated
ReturnsfloatPercentile rank, or na for arrays shorter than two elements

Logic

every(id)

Returns true if all elements are truthy, false otherwise.

ParameterTypeDescription
idarrayInput array of booleans
ReturnsboolTrue if all elements are true

some(id)

Returns true if at least one element is truthy, false otherwise.

ParameterTypeDescription
idarrayInput array of booleans
ReturnsboolTrue if any element is true

Compatibility Notes

The following Pine Script array functions are not available in PyneCore:

  • array.from — Use array.from_items() instead (in PyneCore, from is a reserved keyword)
  • array.new<type> — Use specific type constructors like array.new_float()

All other array functions are fully implemented.