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

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
ReturnsanyElement at the index

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
valueanyNew value

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
valueanyElement to insert

remove(id, index)

Removes and returns the element at the specified index.

ParameterTypeDescription
idarrayInput array
indexintIndex to remove
ReturnsanyRemoved element

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
valueanyValue to fill with
index_fromintStart index (default 0)
index_tointEnd index (optional)

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

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

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 shallow copy of a slice of the array.

ParameterTypeDescription
idarrayInput array
index_fromintStart index
index_tointEnd index
ReturnsarraySliced array

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

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)

Returns the smallest element.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsnumberMinimum value

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

max(id)

Returns the largest element.

ParameterTypeDescription
idarrayInput array of numbers
ReturnsnumberMaximum value

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.

ParameterTypeDescription
idarrayInput array of numbers
Returnslist[float]Standardized array

percentile_linear_interpolation(id, percentile)

Returns the value at the specified percentile using linear interpolation.

ParameterTypeDescription
idarrayInput array of numbers
percentilefloatPercentile (0-100)
ReturnsfloatValue at percentile

percentile_nearest_rank(id, percentile)

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

ParameterTypeDescription
idarrayInput array of numbers
percentilefloatPercentile (0-100)
ReturnsfloatValue at percentile

percentrank(id, index)

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

ParameterTypeDescription
idarrayInput array of numbers
indexintElement index
ReturnsfloatPercentile rank

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()
  • array.new_table — Not supported

All other array functions are fully implemented.