NexuScript 1.5
Arrays
Ordered lists. Indexes start at 1.
Tests whether an array has no items.
Syntax
isempty(a::Array) -> BoolParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to check. |
Returns
Bool true when the array has no items.
Details
Missing values
Never missing. An array that holds only missing values is not empty.
Example
state highs::Array{Number} = []
on bar
if close > open
push!(highs, high)
end
plot("Highest up-bar high", isempty(highs) ? missing : maximum(highs))
endSee also
The first item of an array.
Syntax
first(a::Array) -> element type of the arrayParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to read. It must not be empty. |
Returns
element type of the array A copy of the item at position 1. The array is not changed.
Errors
- NS403the array is empty
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Oldest kept close", first(recent))
endSee also
The last item of an array.
Syntax
last(a::Array) -> element type of the arrayParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to read. It must not be empty. |
Returns
element type of the array A copy of the item at the end. The array is not changed.
Errors
- NS403the array is empty
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Newest kept close", last(recent))
endSee also
All the numbers in an array added together.
Syntax
sum(a::Array{Number}) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array{Number} | required | An array of numbers. |
Returns
Number The total, added from first to last. 0 for an empty array.
Details
To add up a value over the last few bars, ta.sum does it without an array.
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Average of last 10", sum(recent) / length(recent))
endSee also
The smallest number in an array.
Syntax
minimum(a::Array{Number}) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array{Number} | required | An array of numbers. It must not be empty. |
Returns
Number The smallest item.
Details
To compare a few separate numbers, use min instead.
Errors
- NS403the array is empty
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Lowest of last 10 closes", minimum(recent))
endSee also
The largest number in an array.
Syntax
maximum(a::Array{Number}) -> NumberParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array{Number} | required | An array of numbers. It must not be empty. |
Returns
Number The largest item.
Details
To compare a few separate numbers, use max instead.
Errors
- NS403the array is empty
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Highest of last 10 closes", maximum(recent))
endSee also
A new array made of the same value repeated.
Syntax
fill(value::Any, n::Number) -> Array{T}Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | Any | required | The value to repeat. Any type works. |
n | Number | required | How many copies to make, a whole number from 0 to 100,000. |
Returns
Array{T} An array of n copies of value. Its item type is the type of value.
Missing values
A missing value is copied like any other number. A missing n stops the script with NS503.
Errors
- NS503
nis not a whole number from 0 to 100,000, or the copies use up the memory a bar may take
Examples
let row = fill(close, 3)
plot("Three closes added", sum(row))state counts = fill(0, 7)
on bar
plot("Days kept", length(counts))
endSee also
Adds an item to the end of an array.
Syntax
push!(a::Array, x::Any)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to change. It must be held in a let or state variable. |
x | Any | required | The item to add. It must have the same type as the array's items. |
Details
Missing values
A missing number is added like any other item.
Errors
- NS210
xis not the same type as the array's items - NS503the array already holds 100,000 items
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Closes kept", length(recent))
endSee also
Adds an item to the front of an array.
Syntax
pushfirst!(a::Array, x::Any)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to change. It must be held in a let or state variable. |
x | Any | required | The item to add. It must have the same type as the array's items. |
Details
Every item already there moves one position along, so the new item is at position 1. With pushfirst!, recent[1] is always the newest value.
Missing values
A missing number is added like any other item.
Errors
- NS210
xis not the same type as the array's items - NS503the array already holds 100,000 items
Example
state newest::Array{Number} = []
on bar
pushfirst!(newest, close)
if length(newest) > 5
pop!(newest)
end
plot("Newest close", newest[1])
endSee also
Removes the last item of an array and gives it back.
Syntax
pop!(a::Array) -> element typeParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to change. It must be held in a let or state variable and must not be empty. |
Returns
element type The item that was removed. You may ignore it.
Errors
- NS403the array is empty
Example
state stack::Array{Number} = []
on bar
push!(stack, close)
if length(stack) > 3
let dropped = pop!(stack)
print(dropped)
end
endSee also
Removes the first item of an array and gives it back.
Syntax
popfirst!(a::Array) -> element typeParameters
| Name | Type | Default | Description |
|---|---|---|---|
a | Array | required | The array to change. It must be held in a let or state variable and must not be empty. |
Returns
element type The item that was removed. You may ignore it.
Details
Every item left moves one position forward. Paired with push!, it keeps a list of the last few values.
Errors
- NS403the array is empty
Example
state recent::Array{Number} = []
on bar
push!(recent, close)
if length(recent) > 10
popfirst!(recent)
end
plot("Average of last 10", sum(recent) / length(recent))
end