Skip to content
Nexus Prop LLC

NexuScript 1.5

Arrays

Ordered lists. Indexes start at 1.

isempty

functionNexuScript 1.0

Tests whether an array has no items.

Syntax

isempty(a::Array) -> Bool

Parameters

NameTypeDefaultDescription
aArrayrequiredThe array to check.

Returns

Bool true when the array has no items.

Details

Check this before first, last, pop!, minimum or maximum, which stop the script on an empty array.

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))
end

See also

lengthfirstpop!

first

functionNexuScript 1.0

The first item of an array.

Syntax

first(a::Array) -> element type of the array

Parameters

NameTypeDefaultDescription
aArrayrequiredThe 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.

Missing values

If the first item is missing, you get missing.

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))
end

See also

lastpopfirst!isempty

last

functionNexuScript 1.0

The last item of an array.

Syntax

last(a::Array) -> element type of the array

Parameters

NameTypeDefaultDescription
aArrayrequiredThe 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.

Missing values

If the last item is missing, you get missing.

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))
end

See also

firstpop!isempty

sum

functionNexuScript 1.0

All the numbers in an array added together.

Syntax

sum(a::Array{Number}) -> Number

Parameters

NameTypeDefaultDescription
aArray{Number}requiredAn 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.

Missing values

If any item is missing, the total is missing.

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

See also

ta.summinimummaximum

minimum

functionNexuScript 1.0

The smallest number in an array.

Syntax

minimum(a::Array{Number}) -> Number

Parameters

NameTypeDefaultDescription
aArray{Number}requiredAn array of numbers. It must not be empty.

Returns

Number The smallest item.

Details

To compare a few separate numbers, use min instead.

Missing values

If any item is missing, the result is missing.

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))
end

See also

maximumminta.lowest

maximum

functionNexuScript 1.0

The largest number in an array.

Syntax

maximum(a::Array{Number}) -> Number

Parameters

NameTypeDefaultDescription
aArray{Number}requiredAn array of numbers. It must not be empty.

Returns

Number The largest item.

Details

To compare a few separate numbers, use max instead.

Missing values

If any item is missing, the result is missing.

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))
end

See also

minimummaxta.highest

fill

functionNexuScript 1.0

A new array made of the same value repeated.

Syntax

fill(value::Any, n::Number) -> Array{T}

Parameters

NameTypeDefaultDescription
valueAnyrequiredThe value to repeat. Any type works.
nNumberrequiredHow 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

  • NS503n is 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))
end

See also

push!lengthArray

push!

functionNexuScript 1.0

Adds an item to the end of an array.

Syntax

push!(a::Array, x::Any)

Parameters

NameTypeDefaultDescription
aArrayrequiredThe array to change. It must be held in a let or state variable.
xAnyrequiredThe item to add. It must have the same type as the array's items.

Details

The ! in the name means it changes the array you give it. Keep a rolling list by pairing it with popfirst! once the array is long enough.

Missing values

A missing number is added like any other item.

Errors

  • NS210x is 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))
end

See also

pushfirst!pop!state

pushfirst!

functionNexuScript 1.0

Adds an item to the front of an array.

Syntax

pushfirst!(a::Array, x::Any)

Parameters

NameTypeDefaultDescription
aArrayrequiredThe array to change. It must be held in a let or state variable.
xAnyrequiredThe 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

  • NS210x is 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])
end

See also

push!popfirst!state

pop!

functionNexuScript 1.0

Removes the last item of an array and gives it back.

Syntax

pop!(a::Array) -> element type

Parameters

NameTypeDefaultDescription
aArrayrequiredThe 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.

Missing values

If the removed item is missing, you get missing.

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
end

See also

push!popfirst!last

popfirst!

functionNexuScript 1.0

Removes the first item of an array and gives it back.

Syntax

popfirst!(a::Array) -> element type

Parameters

NameTypeDefaultDescription
aArrayrequiredThe 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.

Missing values

If the removed item is missing, you get missing.

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

See also

pop!push!first