CODESCRIPT

Arrays

57 entries. What each one takes, what it returns, and a working example.

array(...)

Creates an array from the given elements. Result: Arr(len=3, [1.0, 2.0, 3.0])

A new array holding the given elements.

...
any
The elements to put in the array (as many as you like; numbers, text, booleans, na may be mixed).

Note

Builds an array by listing elements directly; mixed types are allowed, but numeric summaries (arraySum, arrayAvg) count only the numbers.

When to use

To build a small array by hand with fixed elements: static levels, weights, a label list. If you want to size it first and fill later, arrayNew fits better.

CODESCRIPT
a = array(10, 20, 30)
plot(arraySum(a))

10+20+30 = 60 (the array sum is plotted on every bar).

CODESCRIPT
w = array(0.5, 0.3, 0.2)
plot(arraySum(w))

A weight vector: 0.5+0.3+0.2 = 1.0 (an array can be built from float values).

arrayAbs(array)

Element-wise absolute value (new array). Result: Arr(len=3, [3.0, 1.0, 2.0])

A NEW array holding each element's absolute value (the original is unchanged).

array
array
The array to take absolute values of.

Note

Works element-wise; non-numeric elements are copied as-is. It does not mutate the original.

When to use

To drop the sign of array values and work with magnitudes (deviation, difference size).

CODESCRIPT
a = arrayFrom(-3, 4, -5)
b = arrayAbs(a)
plot(arraySum(b))

|−3|+|4|+|−5| = 12.

CODESCRIPT
a = arrayFrom(-2, -4, -6)
b = arrayAbs(a)
plot(arrayMax(b))

The absolute values are 2,4,6 → the largest is 6 (the biggest magnitude).

arrayAvg(array)

Average of the numeric elements.

The mean of numeric elements; na if there are none.

array
array
The array to average.

Note

Counts only numbers. On an empty or fully non-numeric array it returns na (not 0).

When to use

To get the mean of values collected in an array — a window mean, a sample center.

Limits

Returns na on an empty array; guard the result with na() before feeding it elsewhere.

CODESCRIPT
a = arrayFrom(10, 20, 30)
plot(arrayAvg(a))

The mean: 20.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, close)
if arraySize(w) > 10 {
  arrayShift(w)
}
plot(arrayAvg(w))

The average of the last 10 closes — an SMA(10) built by hand with an array.

arrayBinarySearch(array, value)

Binary search in an ascending-sorted array (-1 if not found).

The index of the value; -1 if not found.

array
array
An ASCENDING sorted numeric array.
value
number
The value to search for.

Note

Assumes the array is ASCENDING sorted; on an unsorted array the result is meaningless. Much faster than linear search (arrayIndexOf) on large arrays.

When to use

To quickly find a value in a large sorted array. For which end to land on among equals, use arrayBinarySearchLeftmost/Rightmost.

Limits

Only correct on an ASCENDING sorted array; sort with arraySort first.

CODESCRIPT
a = arrayFrom(10, 20, 30, 40)
plot(arrayBinarySearch(a, 30))

The value 30 is at position 2.

CODESCRIPT
a = arrayFrom(10, 20, 30, 40)
plot(arrayBinarySearch(a, 25))

25 is not in the sorted array → -1 (binary search found nothing). The array must be sorted.

arrayBinarySearchLeftmost(array, value)

Leftmost position in a sorted array.

The first position equal to the value; if absent, the position where it would be inserted (leftmost insertion point).

array
array
An ASCENDING sorted numeric array.
value
number
The value to search for.

Note

Gives the leftmost position where the value could go in a sorted array. If the value repeats it returns the first copy's index.

When to use

To land on the first of repeated values in a sorted array, or the lower-bound position of a threshold.

CODESCRIPT
a = arrayFrom(1, 2, 2, 2, 3)
plot(arrayBinarySearchLeftmost(a, 2))

The first 2 is at position 1.

CODESCRIPT
a = arrayFrom(1, 3, 5)
plot(arrayBinarySearchLeftmost(a, 4))

4 is not in the array; the (leftmost) position where it would be inserted is 2. Requires a sorted array.

arrayBinarySearchRightmost(array, value)

Rightmost position in a sorted array.

The last position equal to the value; if absent, the position of the last element smaller than it (rightmost).

array
array
An ASCENDING sorted numeric array.
value
number
The value to search for.

Note

Gives the position of the value's last copy, or of the last element smaller than it, in a sorted array.

When to use

To land on the last of repeated values in a sorted array, or the upper-bound position of a threshold.

CODESCRIPT
a = arrayFrom(1, 2, 2, 2, 3)
plot(arrayBinarySearchRightmost(a, 2))

The last 2 is at position 3.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
plot(arrayBinarySearchRightmost(a, 3))

3 occurs once → the rightmost match is that same position: 2. With duplicates it would give the rightmost one.

arrayClear(array)

Deletes every item in the array; the array itself stays but becomes empty. Used to reset an accumulation when a new period starts.

Returns nothing (na); it empties the array in place.

array
array
The array to empty.

Note

Deletes all elements, size becomes 0; the array itself (the reference) is kept.

When to use

To reset a buffer before reusing it — when moving to a new window/period.

CODESCRIPT
a = arrayFrom(1, 2, 3)
arrayClear(a)
plot(arraySize(a))

Emptied → size 0.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, close)
if arraySize(w) >= 10 {
  arrayClear(w)
}
plot(arraySize(w))

When the size reaches 10 the array is cleared → a 1..10 sawtooth (a periodic bucket).

arrayConcat(array1, array2)

Appends dizi2 to dizi1, returns dizi1. Result: Arr(len=5, [3.0, 1.0, 2.0, 4.0])

The extended dizi1 (same reference).

array1
array
The target array to extend (the result is appended here).
array2
array
The source array whose elements are appended.

Note

Appends dizi2's elements to the end of dizi1 and mutates dizi1. dizi2 is left untouched.

When to use

To merge two lists into one. To preserve the original, take an arrayCopy first.

CODESCRIPT
a = arrayFrom(1, 2)
b = arrayFrom(3, 4)
arrayConcat(a, b)
plot(arraySize(a))

dizi1 grew to 4 elements.

CODESCRIPT
a = arrayFrom(1, 2)
b = arrayFrom(3, 4, 5)
arrayConcat(a, b)
plot(arrayAvg(a))

The two arrays are merged (1,2,3,4,5) → average 3 (merge, then reduce).

arrayCopy(array)

Copy of the array. Result: Arr(len=3, [3.0, 1.0, 2.0])

An independent new array with the same elements.

array
array
The array to copy.

Note

Returns a shallow copy; mutating the copy does not affect the original. Used to preserve the original before in-place functions (arraySort, arrayReverse).

When to use

To run in-place operations like sort/reverse on an array without spoiling it.

CODESCRIPT
a = arrayFrom(3, 1, 2)
b = arrayCopy(a)
arraySort(b, true)
plot(arrayFirst(a))

The copy is sorted; the original's first element is still 3.

CODESCRIPT
a = arrayFrom(1, 2, 3)
b = arrayCopy(a)
arrayPush(b, 4)
plot(arraySize(a))

An element is pushed to the copy but the original is untouched → the original size is still 3 (an independent copy).

arrayCovariance(array1, array2, biased?)

Covariance of two arrays. Result: -0.5

The covariance of the two arrays; na if fewer than 2 common numeric elements.

array1
array
The first numeric array.
array2
array
The second numeric array.
biased
bool
true for biased (divide by n), false for unbiased (n-1) (optional; defaults to true).

Note

Measures the direction and strength with which two series move together. If the arrays differ in length they are paired up to the shorter one. The default divisor is biased (n).

When to use

To measure whether two value arrays (e.g. two asset returns) tend to rise and fall together.

CODESCRIPT
a = arrayFrom(1, 2, 3)
b = arrayFrom(2, 4, 6)
plot(arrayCovariance(a, b))

Two arrays rising together → positive covariance (≈1.33, biased).

CODESCRIPT
a = arrayFrom(1, 2, 3)
b = arrayFrom(6, 4, 2)
plot(arrayCovariance(a, b))

As one rises the other falls → negative covariance (≈ -1.33).

arrayEvery(array)

Whether all elements are truthy/non-zero. Result: True

true if all elements are truthy/non-zero; false for an empty array.

array
array
The array to check.

Note

Evaluates each element for truthiness: 0/na/false count as false. Returns false for an empty array.

When to use

To verify that all conditions in an array hold at once (did they all pass).

CODESCRIPT
a = arrayFrom(1, 1, 1)
plot(arrayEvery(a) ? 1 : 0)

All elements are non-zero → true.

CODESCRIPT
a = arrayFrom(1, 0, 1)
plot(arrayEvery(a) ? 1 : 0)

One element is 0 → arrayEvery is false (0). Not all elements are non-zero.

arrayFill(array, value, start?, end?)

Fills the range with the value.

Returns nothing (na); it fills the array in place.

array
array
The array to fill.
value
any
The value to write into the range.
start
number
Start index (inclusive; optional, defaults to 0; negative = from the end).
end
number
End index (exclusive; optional, defaults to array end; negative = from the end).

Note

Writes the same value into every element of the range; it does not change the size. Fills the whole array if no range is given.

When to use

To reset/mark a portion of an array with a single value.

CODESCRIPT
a = arrayNew(5, 0)
arrayFill(a, 9, 1, 3)
plot(arraySum(a))

Elements 1 and 2 set to 9 → sum is 18.

CODESCRIPT
a = arrayNew(4, 1)
arrayFill(a, 0)
plot(arraySum(a))

With no range given the whole array is filled with 0 → sum 0 (a bulk reset).

arrayFirst(array)

Gives the first item of the array without changing it. It does the same as `arrayGet(d, 0)` but reads more clearly. ⚠ It fails on an empty array.

The first element; na if the array is empty.

array
array
The array to read the first element of.

Note

Same as arrayGet(array, 0) but reads clearer; it does not modify the array.

When to use

To read the value at the front of the array (the oldest / first record).

CODESCRIPT
a = arrayFrom(11, 22, 33)
plot(arrayFirst(a))

The first element: 11.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, close)
if arraySize(w) > 5 {
  arrayShift(w)
}
plot(arrayFirst(w))

The first (oldest) element of a 5-bar rolling window — the close from 5 bars ago.

arrayFrom(...values)

Builds a new array from the values you give, letting you write in one line what would otherwise be added one by one. Convenient for a fixed list.

A new array made from the given values.

...
any
The values to place into the array.

Note

Does the same job as array; use whichever reads better to you.

When to use

To quickly create an array from a ready list of values.

CODESCRIPT
a = arrayFrom(3, 1, 2)
plot(arrayMax(a))

The largest value in the array: 3.

CODESCRIPT
a = arrayFrom(5, 1, 9, 3, 7)
plot(arrayMedian(a))

The input may be unordered: sorted it is 1,3,5,7,9 → the exact middle (median) is 5.

arrayGet(array, i)

Gives the item at a given position in the array. Positions start at zero: the first item is 0, the second 1. Use a negative number to count from the end, where -1 is the last item. ⚠ Asking for a position the array does not have raises an error; checking with `arraySize` first is safer.

The element at i; returns na if the index is out of range (no error).

array
array
The array to read from.
i
number
Element index; 0 is the first element, a negative value counts from the end (-1 is the last).

Note

Negative index reaches from the end. An out-of-range index does not crash, it yields na — you can check the result with na().

When to use

To read the value at a specific position. For the first/last element, arrayFirst/arrayLast read cleaner.

CODESCRIPT
a = arrayFrom(10, 20, 30)
plot(arrayGet(a, -1))

The last element: 30.

CODESCRIPT
a = arrayFrom(close, close[1], close[2])
plot(arrayGet(a, 0) > arrayGet(a, 2) ? 1 : 0)

Access by positive index: 1 if the current close (element 0) is above the close two bars ago (element 2).

arrayIncludes(array, value)

Whether the value is in the array (true/false).

true if the value is in the array, false otherwise.

array
array
The array to search in.
value
any
The value whose presence is queried.

Note

Uses type-strict equality: numbers match numbers, text matches text, booleans match booleans.

When to use

To check whether a value is in a list — watchlist membership, de-duplication.

CODESCRIPT
a = arrayFrom(1, 2, 3)
plot(arrayIncludes(a, 2) ? 1 : 0)

2 is in the array → true (plotted as 1).

CODESCRIPT
a = arrayFrom(1, 5, 9)
plot(arrayIncludes(a, 4) ? 1 : 0)

4 is not in the array → false (plotted as 0) — the negative case of a membership test.

arrayIndexOf(array, value)

Position of the value (-1 if not found).

The index of the first match; -1 if not found.

array
array
The array to search in.
value
any
The value whose position is sought.

Note

Scans from the front and returns the first match's position. Applies type-strict equality. To search from the end, use arrayLastIndexOf.

When to use

To find a value's position in the array (then reach it with arrayGet/arraySet).

CODESCRIPT
a = arrayFrom(5, 6, 7)
plot(arrayIndexOf(a, 7))

The value 7 is at position 2.

CODESCRIPT
a = arrayFrom(5, 6, 7)
plot(arrayIndexOf(a, 99))

99 is not in the array → -1 (the not-found sentinel).

arrayInsert(array, i, value)

Inserts at position i.

Returns nothing (na); it grows the array in place.

array
array
The array to insert into.
i
number
Insert position (negative = from the end; clamped to front/back if out of range).
value
any
The value to insert.

Note

Inserts an element at the position; following elements shift right. If the position exceeds the size it goes to the end, if too small to the front.

When to use

To insert a value in the middle of an ordered list. For end/front only, use arrayPush/arrayUnshift.

CODESCRIPT
a = arrayFrom(1, 3)
arrayInsert(a, 1, 2)
plot(arrayGet(a, 1))

2 inserted at position 1 → array is 1,2,3.

CODESCRIPT
a = arrayFrom(1, 2, 3)
arrayInsert(a, 0, 0)
plot(arrayFirst(a))

0 is inserted at position 0 (the front) → the new first element is 0.

arrayJoin(array, separator?)

Joins the elements into a string. Result: 3, 1, 2

A single text of the elements joined by the separator.

array
array
The array to turn into text.
separator
string
The separator placed between elements (optional; defaults to ",").

Note

Converts each element to text and joins with the separator. Uses a comma when none is given.

When to use

When you want to turn an array into a single text for a label/table.

CODESCRIPT
a = arrayFrom(1, 2, 3)
s = arrayJoin(a, "-")
plot(s == "1-2-3" ? 1 : 0)

Elements joined with "-" → "1-2-3".

CODESCRIPT
a = arrayFrom(1, 2, 3)
s = arrayJoin(a)
plot(s == "1,2,3" ? 1 : 0)

With no separator given, the default is a comma → "1,2,3".

arrayLast(array)

Last element.

The last element; na if the array is empty.

array
array
The array to read the last element of.

Note

Same as arrayGet(array, -1) but reads clearer; it does not modify the array.

When to use

To read the value at the end of the array (the most recently added / latest record).

CODESCRIPT
a = arrayFrom(11, 22, 33)
plot(arrayLast(a))

The last element: 33.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, high)
plot(arrayLast(w))

high is pushed every bar; the last element is the most recently added (current) high.

arrayLastIndexOf(array, value)

Index of the last match (-1 if not found).

The index of the last match; -1 if not found.

array
array
The array to search in.
value
any
The value whose position is sought.

Note

Scans from the end toward the front and returns the last match's position. Applies type-strict equality.

When to use

When a value appears multiple times and you want the position of the last one.

CODESCRIPT
a = arrayFrom(5, 6, 5)
plot(arrayLastIndexOf(a, 5))

The last 5 is at position 2.

CODESCRIPT
a = arrayFrom(5, 6, 5)
plot(arrayLastIndexOf(a, 9))

9 is not in the array at all → -1 (a last-index search also finds nothing).

arrayMax(array)

Largest numeric element.

The largest numeric element; na if there are none.

array
array
The array to find the maximum of.

Note

Compares only numbers; text/na are skipped.

When to use

To find the highest value in an array (the best result, a ceiling level).

CODESCRIPT
a = arrayFrom(7, 3, 9)
plot(arrayMax(a))

The maximum: 9.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, high)
if arraySize(w) > 20 {
  arrayShift(w)
}
plot(arrayMax(w))

The highest high of the last 20 bars — a rolling resistance (Donchian upper band).

arrayMedian(array)

Median value.

The median value; na if empty. For an even count it is the average of the middle two.

array
array
The array to find the median of.

Note

Sorts the values and takes the middle; it is less sensitive to outliers than the mean.

When to use

When you want a typical value unspoiled by outliers (a robust center), instead of the mean.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
plot(arrayMedian(a))

Average of the middle two: (2+3)/2 = 2.5.

CODESCRIPT
a = arrayFrom(5, 1, 9, 3, 7)
plot(arrayMedian(a))

An odd number of elements: sorted 1,3,5,7,9 → the exact middle 5 (unlike the even-count case).

arrayMin(array)

Smallest numeric element.

The smallest numeric element; na if there are none.

array
array
The array to find the minimum of.

Note

Compares only numbers; text/na are skipped.

When to use

To find the lowest value in an array (the weakest result, a floor level).

CODESCRIPT
a = arrayFrom(7, 3, 9)
plot(arrayMin(a))

The minimum: 3.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, low)
if arraySize(w) > 20 {
  arrayShift(w)
}
plot(arrayMin(w))

The lowest low of the last 20 bars — a rolling support (Donchian lower band).

arrayMode(array)

Most frequent value (smallest on ties).

The most frequent numeric value; the smallest on a tie; na if empty.

array
array
The array to find the most frequent value in.

Note

If several values share the top frequency it returns the smallest. Counts only numeric elements.

When to use

To find the most repeated value in an array (a dominant level, the most common result).

CODESCRIPT
a = arrayFrom(2, 2, 1, 1)
plot(arrayMode(a))

1 and 2 tie in frequency → the smaller: 1.

CODESCRIPT
a = arrayFrom(1, 3, 3, 3, 2)
plot(arrayMode(a))

The value 3 occurs three times → the most frequent (mode) is 3 (a clear winner).

arrayNew(size?, start?)

Creates an array. With a size, that many elements (each set to init); without, an EMPTY array.

A new array of size elements, each equal to the initial value.

size
number
Number of elements the array will have.
start
any
Initial value for every element (optional; defaults to 0).

Note

Used to allocate a fixed-size array up front, then fill it with arraySet/arrayFill. Elements are 0 when no initial value is given.

When to use

To pre-allocate a buffer of known size: collecting the last N results, a fixed-length window. If you already know the elements, use array.

CODESCRIPT
a = arrayNew(4, 5)
plot(arraySum(a))

All 4 elements are 5 → sum is 20.

CODESCRIPT
a = arrayNew(3, 2)
arrayPush(a, 4)
plot(arrayAvg(a))

Three elements are built with 2, then a 4 is pushed → average (2+2+2+4)/4 = 2.5.

arrayNewBool(size?, start?)

Boolean array. Result: Arr(len=5, [False, False, False, False])

A new array of size elements, each equal to the initial value.

size
number
Number of elements.
start
bool
Initial value for each element (optional).

Note

Behaves the same as arrayNew; signals a boolean array. Defaults to 0 (acts falsy) when no initial value is given.

When to use

To allocate a buffer holding a flag/condition per position.

CODESCRIPT
a = arrayNewBool(3, true)
plot(arrayEvery(a) ? 1 : 0)

All elements true → arrayEvery is true.

CODESCRIPT
a = arrayNewBool(3, false)
arraySet(a, 1, true)
plot(arraySome(a) ? 1 : 0)

Three elements are built as false, then element 1 is set to true → arraySome is true (1).

arrayNewBox(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new box array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold box. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding box objects position by position.

CODESCRIPT
a = arrayNewBox(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
var boxes = arrayNewBox(0)
plot(arraySize(boxes))

An empty (0-element) box-handle store kept with var — carried across bars, starting at size 0.

arrayNewColor(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new color array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold color. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding color objects position by position.

CODESCRIPT
a = arrayNewColor(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
var palette = arrayNewColor(2, "#26a69a")
arraySet(palette, 1, "#ef5350")
plot(arraySize(palette))

Two colors are allocated, element 1 is replaced with a sell color → a buy/sell palette, size 2.

arrayNewFloat(size?, start?)

Creates an empty numeric array. If you will accumulate values bar by bar, create it with `var`; otherwise it is emptied on every bar.

A new array of size numeric elements, each equal to the initial value.

size
number
Number of elements.
start
number
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; signals a numeric (float) array. Defaults to 0 when no initial value is given.

When to use

To make intent explicit when allocating a numeric buffer.

CODESCRIPT
a = arrayNewFloat(3, 1.5)
plot(arraySum(a))

All 3 elements are 1.5 → sum is 4.5.

CODESCRIPT
a = arrayNewFloat(3, close)
plot(arrayGet(a, 0))

Three elements are built with the current close → element 0 is that bar's close (the initial value can be a series).

arrayNewInt(size?, start?)

Creates an empty integer array. Used for values that must be whole, such as bar numbers or positions. When accumulating bar by bar it must be created with `var`.

A new array of size elements, each equal to the initial value.

size
number
Number of elements.
start
number
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; signals an integer array. Numeric storage is floating-point; using values as integers is up to you.

When to use

To signal allocating a buffer holding integer values like counters/indices.

CODESCRIPT
a = arrayNewInt(4, 2)
plot(arraySum(a))

All 4 elements are 2 → sum is 8.

CODESCRIPT
a = arrayNewInt(3, 10)
arraySet(a, 0, 5)
plot(arrayMin(a))

Three elements are built with 10, then element 0 is set to 5 → the minimum is 5.

arrayNewLabel(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new label array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold label. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding label objects position by position.

CODESCRIPT
a = arrayNewLabel(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
a = arrayNewLabel(3)
arrayPop(a)
plot(arraySize(a))

Three label slots are allocated, one is popped → size 2.

arrayNewLine(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new line array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold line. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding line objects position by position.

CODESCRIPT
a = arrayNewLine(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
a = arrayNewLine(4)
arrayRemove(a, 0)
plot(arraySize(a))

Four line slots are allocated, element 0 is removed → size 3.

arrayNewLinefill(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new line fill array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold line fill. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding line fill objects position by position.

CODESCRIPT
a = arrayNewLinefill(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
a = arrayNewLinefill(2)
arrayClear(a)
plot(arraySize(a))

Two linefill slots are allocated, then the array is cleared → size 0.

arrayNewString(size?, start?)

String array. Result: Arr(len=5, ['', '', '', ''])

A new array of size elements, each equal to the initial text.

size
number
Number of elements.
start
string
Initial text for each element (optional; empty text "" if omitted).

Note

Builds a text array. Unlike the other arrayNew families: when no initial value is given the element is EMPTY TEXT (""), not 0.

When to use

To allocate a fixed-size buffer holding text (label/symbol names).

CODESCRIPT
a = arrayNewString(2)
plot(arrayGet(a, 0) == "" ? 1 : 0)

No initial value → elements are empty text.

CODESCRIPT
a = arrayNewString(2, "AL")
arraySet(a, 1, "SAT")
plot(arrayIndexOf(a, "SAT"))

Two elements are built as "AL", then element 1 is set to "SAT" → "SAT" is at position 1.

arrayNewTable(size?, start?)

Type-specific array constructor (same as arrayNew). Result: Arr(len=0, [])

A new table array of size elements.

size
number
Number of elements.
start
any
Initial value for each element (optional; defaults to 0).

Note

Behaves the same as arrayNew; a type-specific constructor signaling intent to hold table. Elements are 0 if no initial value is given (usually filled by passing na/an initial).

When to use

To allocate a fixed-size buffer holding table objects position by position.

CODESCRIPT
a = arrayNewTable(2)
plot(arraySize(a))

A 2-element array is allocated.

CODESCRIPT
a = arrayNewTable(1)
b = arrayNewTable(2)
arrayConcat(a, b)
plot(arraySize(a))

A 1-element and a 2-element table array are concatenated → size 3.

arrayPercentRank(array, index)

Percentile rank of the ith value (0-100).

The 0–100 percentile rank of element i; na if the index is invalid/non-numeric; 0 with fewer than 2 numeric elements.

array
array
The array to rank a position within.
index
number
Index of the element whose percentile rank is computed.

Note

Tells what percentile the given element sits at relative to the array: how many elements are less than or equal to it. 0 = smallest, 100 = largest.

When to use

To measure a value's relative position within a historical distribution (how high is this reading historically).

CODESCRIPT
a = arrayFrom(10, 20, 30, 40, 50)
plot(arrayPercentRank(a, 2))

The value 30 sits at the 50th percentile of the array.

CODESCRIPT
a = arrayFrom(10, 20, 30, 40, 50)
plot(arrayPercentRank(a, 4))

The largest element (50, index 4) sits at the 100th percentile of the array.

arrayPercentileLinearInterpolation(array, percent)

Percentile with linear interpolation. Result: 2.5

The linearly interpolated percentile value; na if empty.

array
array
The array to take the percentile of.
percent
number
Percentile between 0–100 (optional; defaults to 50 = median).

Note

Finds the position among sorted values by linear interpolation; the result may not be an actual array element. Gives a smoother/continuous percentile.

When to use

When you want a smooth percentile threshold in a distribution (e.g. a dynamic 90th level, the median).

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
plot(arrayPercentileLinearInterpolation(a, 50))

The 50th percentile (median) = 2.5.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
plot(arrayPercentileLinearInterpolation(a, 25))

The 25th percentile with linear interpolation = 1.75 (a slice other than the median).

arrayPercentileNearestRank(array, percent)

Nearest-rank percentile.

The percentile value by the nearest-rank method; na if empty.

array
array
The array to take the percentile of.
percent
number
Percentile between 0–100 (optional; defaults to 50).

Note

Rounds the rank matching the percentile up and returns the ACTUAL element at that position; it does not produce interpolated values.

When to use

When you require the percentile result to be an actual value present in the array.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
plot(arrayPercentileNearestRank(a, 50))

The 50th nearest-rank → actual array value 2.

CODESCRIPT
a = arrayFrom(10, 20, 30, 40, 50)
plot(arrayPercentileNearestRank(a, 90))

The 90th percentile by nearest rank → the real value at the top end, 50.

arrayPop(array)

Removes the last item of the array and gives it back. The array gets one shorter. Use it when you want to reach the most recently added item first.

The removed last element; na if the array is empty.

array
array
The array to remove the last element from.

Note

Shortens the array by one element and returns the removed value. On an empty array it does not crash, it returns na.

When to use

To take back the most recently added element in stack fashion.

CODESCRIPT
a = arrayFrom(1, 2, 3)
x = arrayPop(a)
plot(x)

The last element is removed and returned: 3.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
arrayPop(a)
arrayPop(a)
plot(arrayLast(a))

The last element is popped twice (4, then 3) → the remaining last element is 2 (LIFO).

arrayPush(array, value)

Adds an item to the end of the array and gives the new size. Used to collect values accumulating bar by bar. ⚠ For the array to live across bars, create it with `var`; otherwise it is emptied on every bar.

The new element count after the append.

array
array
The array to append to.
value
any
The value to add at the end.

Note

Grows the array by one element. The core tool for continuous accumulation (a log, a results list).

When to use

To accumulate results into an array bar by bar. To add at the front, use arrayUnshift.

CODESCRIPT
a = arrayNew(0)
arrayPush(a, close)
plot(arraySize(a))

close is appended each bar; the size grows with the bar count.

CODESCRIPT
var signals = arrayNew(0)
if crossover(close, sma(close, 20)) {
  arrayPush(signals, close)
}
plot(arraySize(signals))

close is pushed only on an up-cross of the 20-SMA; the size is the number of signals so far.

arrayRange(array)

Max - min difference.

The difference between the largest and smallest numeric element (max - min); na if empty.

array
array
The array to measure the range of.

Note

The simplest spread measure; very sensitive to a single outlier.

When to use

To quickly see the total distance between the extremes of an array.

CODESCRIPT
a = arrayFrom(3, 7, 1)
plot(arrayRange(a))

7 - 1 = 6.

CODESCRIPT
a = arrayFrom(-5, 0, 5, 10)
plot(arrayRange(a))

Max - min = 10 - (-5) = 15 (works with negative values too).

arrayRemove(array, i)

Removes the item at a given position and gives it back; the items after it shift one position. Used to drop a record that is no longer valid.

The removed element; na if the index is out of range.

array
array
The array to remove from.
i
number
Index to remove (negative = from the end).

Note

Removes the element at the position, shifts the rest left, and returns the removed value.

When to use

To delete an element at a specific position. For front/back use arrayShift/arrayPop.

CODESCRIPT
a = arrayFrom(5, 6, 7)
x = arrayRemove(a, 1)
plot(x)

Element 1 is removed and returned: 6.

CODESCRIPT
a = arrayFrom(5, 6, 7)
arrayRemove(a, 0)
plot(arrayFirst(a))

Element 0 (5) is removed and the rest shift down → the new first element is 6.

arrayReverse(array)

Reverses the order (in place).

Returns nothing (na); it reverses the array order in place.

array
array
The array to reverse.

Note

Flips the element order front-to-back; it mutates the array.

When to use

To turn an ascending array into descending, or an oldest-first list into newest-first.

CODESCRIPT
a = arrayFrom(1, 2, 3)
arrayReverse(a)
plot(arrayFirst(a))

Order reversed → the first element is now 3.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4)
arrayReverse(a)
plot(arrayLast(a))

The order is reversed → the last element is now the original first, 1.

arraySet(array, i, value)

Replaces the item at a given position in the array; it does not create a new array but updates the existing one. Positions start at zero.

Returns nothing (na); it modifies the array in place.

array
array
The array to modify.
i
number
Index to write (negative = from the end).
value
any
The value to store.

Note

Overwrites an existing position; it does not grow the array. If the index is out of range it silently does nothing.

When to use

To update a specific slot of a pre-allocated array. To append at the end, use arrayPush.

CODESCRIPT
a = arrayNew(3, 0)
arraySet(a, 1, 9)
plot(arraySum(a))

Element 1 set to 9, others 0 → sum is 9.

CODESCRIPT
a = arrayNew(3, 0)
arraySet(a, 0, high)
arraySet(a, 2, low)
plot(arrayGet(a, 0) - arrayGet(a, 2))

high is written to element 0 and low to element 2; their difference is the bar's range (high - low).

arrayShift(array)

Removes the first item of the array and gives it back; the remaining items shift one position forward. Use it to process the oldest item first — it is the counterpart of `arrayPop`, which takes from the end.

The removed first element; na if the array is empty.

array
array
The array to remove the first element from.

Note

Removes from the front and shifts the rest forward. Used in queue (FIFO) fashion.

When to use

To drop the oldest element and keep a fixed-length sliding window (push + shift).

CODESCRIPT
a = arrayFrom(1, 2, 3)
x = arrayShift(a)
plot(x)

The first element is removed and returned: 1.

CODESCRIPT
a = arrayFrom(10, 20, 30)
arrayShift(a)
plot(arrayFirst(a))

One element is dropped from the front (10) → the new first element is 20.

arraySize(array)

Gives the number of items in an array. Used as a loop bound or to check whether the array is empty; an empty array gives zero.

The number of elements in the array.

array
array
The array to measure.

Note

Returns 0 for an empty array. Used for loop bounds and emptiness checks.

When to use

To know the bound before iterating an array, or to check whether it is empty.

CODESCRIPT
a = arrayFrom(5, 6, 7)
plot(arraySize(a))

3 elements.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, close)
if arraySize(w) > 20 {
  arrayShift(w)
}
plot(arraySize(w) == 20 ? 1 : 0)

1 once the 20-bar rolling window is full (ready), 0 for the first 20 bars — a warmup check.

arraySlice(array, start, end?)

Sub-array copy (start..end) — an independent copy (not a view/window; does not affect the original). Result: Arr(len=3, [3.0, 1.0, 2.0])

A new copy array of the start..end range.

array
array
The source array (unchanged).
start
number
Start index (inclusive; negative = from the end).
end
number
End index (exclusive; optional, defaults to array end; negative = from the end).

Note

Does NOT modify the source; returns an independent sub-array copy. end is exclusive. Note: this is NOT a view/window — the returned array is fully independent from the source; changing one does not affect the other (in either direction).

When to use

To process a portion of an array separately (the last N elements, the first half).

Does NOT modify the source; returns an independent sub-array copy. end is exclusive.

CODESCRIPT
a = arrayFrom(0, 1, 2, 3, 4)
b = arraySlice(a, 1, 3)
plot(arraySize(b))

Elements 1 and 2 → a 2-element copy.

CODESCRIPT
a = arrayFrom(1, 2, 3, 4, 5)
b = arraySlice(a, 2)
plot(arraySum(b))

From element 2 to the end (no end given) → 3,4,5; sum 12.

arraySome(array)

Whether at least one element is truthy. Result: True

true if at least one element is truthy/non-zero; false if none is (or if empty).

array
array
The array to check.

Note

Evaluates each element for truthiness; becomes true at the first truthy element. false for an empty array.

When to use

To check that at least one condition in an array holds (did any pass).

CODESCRIPT
a = arrayFrom(0, 0, 1)
plot(arraySome(a) ? 1 : 0)

One element is non-zero → true.

CODESCRIPT
a = arrayFrom(0, 0, 0)
plot(arraySome(a) ? 1 : 0)

All elements are 0 → arraySome is false (0). There is no non-zero element.

arraySort(array, ascending?)

Sorts numerically (in place; ascending defaults to true).

Returns nothing (na); it sorts the array in place.

array
array
The array to sort.
ascending
bool
true for ascending, false for descending (optional; defaults to true). The named argument can also be given as `asc` (equivalent).

Note

Sorts only numeric elements; non-numeric ones go to the end in their original order. It mutates the array — use arrayCopy first if you need a copy.

When to use

To order values by magnitude — before percentile/median, or picking the best/worst N.

CODESCRIPT
a = arrayFrom(30, 10, 20)
arraySort(a, true)
plot(arrayFirst(a))

Sorted ascending → the first element is the smallest: 10.

CODESCRIPT
a = arrayFrom(30, 10, 20, 40)
arraySort(a, false)
plot(arrayFirst(a))

Descending sort (second argument false) → the first element is the largest: 40.

arraySortIndices(array, ascending?)

Array of sorting indices (for top-N). The named argument can be given as `asc` (or `artan`) — both are equivalent. Result: Arr(len=3, [0.0, 2.0, 1.0])

An array of indices that would sort the array; the array itself is unchanged.

array
array
The array to derive the sort order of (unchanged).
ascending
bool
true for ascending, false for descending (optional; defaults to true). The named argument can also be given as `asc` (equivalent; same names as its sibling arraySort).

Note

Without mutating the array, gives the element positions that put it in order. Only numeric elements enter the ordering. Ideal for picking the best/worst N.

When to use

To get the sort order without mutating the array, or to align a parallel array in the same order.

CODESCRIPT
a = arrayFrom(30, 10, 20)
b = arraySortIndices(a, true)
plot(arrayGet(b, 0))

The smallest value (10) is at position 1 → first index is 1.

CODESCRIPT
a = arrayFrom(30, 10, 20)
b = arraySortIndices(a, false)
plot(arrayGet(b, 0))

In descending order the largest value (30) is at position 0 → the first index is 0. The array itself is unchanged.

arrayStandardize(array)

Z-score: (x-mean)/std (new array). Result: Arr(len=3, [1.0, -1.0, 0.0])

A NEW array converting each element to its z-score (x−mean)/std; all zeros if the deviation is 0 or there are fewer than 2 elements.

array
array
The array to standardize.

Note

Rescales values to mean 0 and standard deviation 1 (using sample std). It does not mutate the original.

When to use

To make series on different scales comparable, or to measure extreme deviations (how many std away).

Limits

If all elements are equal (std 0) or there are fewer than 2, the result is all zeros.

CODESCRIPT
a = arrayFrom(1, 2, 3)
b = arrayStandardize(a)
plot(arrayGet(b, 2))

The largest element is +1 std from the mean → 1.

CODESCRIPT
a = arrayFrom(10, 20, 30, 40, 50)
b = arrayStandardize(a)
plot(arrayGet(b, 0))

The first element (10, the smallest) is about 1.26 standard deviations below the mean → its z-score ≈ -1.26.

arrayStdev(array, biased?)

Standard deviation (square root of variance). Population ÷N by default; pass biased=false for sample ÷(N-1). Result: 0.8164965809

The standard deviation (square root of variance); population (÷N) by default, sample (÷N-1) with biased=false; na with fewer than 2 numeric elements.

array
array
The array to compute standard deviation of.
biased
bool
Optional. true (default) uses the population divisor ÷N; false uses the sample divisor ÷(N-1).

Note

Gives spread in the same unit as the values; the square root of arrayVariance. Population divisor ÷N by default; sample with biased=false.

When to use

To measure the dispersion of array values in the same unit as the values (volatility-like).

Gives spread in the same unit as the values; the square root of arrayVariance.

CODESCRIPT
a = arrayFrom(2, 4, 4, 4, 5, 5, 7, 9)
plot(arrayStdev(a))

The sample standard deviation ≈ 2.14.

CODESCRIPT
var w = arrayNew(0)
arrayPush(w, close)
if arraySize(w) > 20 {
  arrayShift(w)
}
plot(arrayStdev(w))

The standard deviation of the last 20 closes — a rolling volatility measure.

arraySum(array)

Gives the sum of the numbers in the array. Combined with `arraySize` to take an average. An empty array gives zero.

The sum of numeric elements; 0 if there are no numeric elements.

array
array
The array to sum.

Note

Sums only numbers; it skips text/na elements. Yields 0 for an empty array.

When to use

To total the values you have accumulated in an array (weight sum, cumulative score).

CODESCRIPT
a = arrayFrom(10, 20, 30)
plot(arraySum(a))

The sum: 60.

CODESCRIPT
a = arrayFrom(close > open ? 1 : 0, close[1] > open[1] ? 1 : 0, close[2] > open[2] ? 1 : 0)
plot(arraySum(a))

Counts how many of the last 3 bars had close>open (0 to 3).

arrayUnshift(array, value)

Adds an item to the front of the array and gives the new size; the existing items shift one position. Use it when you want the newest value to sit at the front.

The new element count after the prepend.

array
array
The array to prepend to.
value
any
The value to add at the front.

Note

Grows the array at the front; existing elements shift one position to the right.

When to use

When you want the newest value kept at the front (a newest-first list).

CODESCRIPT
a = arrayFrom(2, 3)
arrayUnshift(a, 1)
plot(arrayFirst(a))

The new first element: 1.

CODESCRIPT
a = arrayFrom(3)
arrayUnshift(a, 2)
arrayUnshift(a, 1)
plot(arrayGet(a, 0))

2 then 1 are prepended → the array is 1,2,3; the first element is 1.

arrayVariance(array, biased?)

Variance (spread). Population ÷N by default; pass biased=false for sample ÷(N-1). Result: 0.6666666667

The variance; population (÷N) by default, sample (÷N-1) with biased=false; na with fewer than 2 numeric elements.

array
array
The array to compute variance of.
biased
bool
Optional. true (default) uses the population divisor ÷N; false uses the sample divisor ÷(N-1).

Note

Measures the spread of the distribution. Population divisor ÷N by default; sample divisor ÷(N-1) with biased=false. Its square root is arrayStdev.

When to use

To measure how dispersed the values collected in an array are.

Limits

Needs at least 2 numeric elements; na for a single element or empty array.

Measures the spread of the distribution; uses the n-1 (sample) divisor. Its square root is arrayStdev.

CODESCRIPT
a = arrayFrom(2, 4, 6)
plot(arrayVariance(a))

The sample variance: 4.

CODESCRIPT
a = arrayFrom(10, 10, 10, 10)
plot(arrayVariance(a))

All elements equal → no dispersion → variance 0.

split(text, separator?)

Splits the text into an array by the separator. Result: Arr(len=3, ['a', 'b', 'c'])

A text array holding the parts.

text
string
The text to split.
separator
string
The split separator (optional; defaults to ","; if an empty string is given each character becomes an element).

Note

Splits the text at the separator into an array. Uses a comma when none is given; an empty separator splits into characters.

When to use

To turn a comma-separated symbol/parameter list into individually processable array elements.

CODESCRIPT
a = split("BUY,SELL,HOLD", ",")
plot(arraySize(a))

Split into 3 parts.

CODESCRIPT
p = split("BUY,SELL,HOLD", ",")
plot(arrayIndexOf(p, "SAT"))

The text is split on commas; among the parts "SAT" is at position 1 (parse, then find).