Builds an array of any element type named in angle brackets, filled to a chosen size with one starting value.
Write the item type in angle brackets: array.new<float>(10, 0.0) holds ten floats that start at 0.0. Any type works, including string, bool, color, line, label, box, and user-defined types. Leave out the size to get an empty array.
Every starting item receives the same initial_value. For arrays of objects such as lines or labels, that means the items all point at one object, not at separate copies. Without an initial_value the items are na (false for bool).
Syntax
array.new(size: series int (optional), initial_value: <array_type> (optional)) -> array<type>Arguments
| Name | Type | Default | Description |
|---|---|---|---|
| size | series int | 0 | How many starting items: 0 up to 100,000, with 0 as the default for an empty array. A negative size or na stops the script. |
| initial_value | <array_type> | na | The value copied into every starting item. Leave it out and numbers and objects start as na while bool starts as false. |
Returns
array<type>: A new array whose element type is the one in angle brackets, or the type of initial_value when the brackets are left out.
Remarks
- The size can be as large as 100,000. A size above that, or a negative size, stops the script with an error.
array.new_float(),array.new_int()and the other typed creators are shorthand for this function with the type fixed.- On Sidechair: when the angle brackets are left out, the type is taken from initial_value, and an array with neither is an array of float.
Examples
//@version=6
indicator("array.new: buffer of changes", overlay = false)
var buf = array.new<float>(3, 0.0)
array.unshift(buf, close - close[1])
array.pop(buf)
plot(array.sum(buf), "Sum of last 3 changes")//@version=6
indicator("array.new: label pool", overlay = true)
var pool = array.new<label>()
if bar_index % 50 == 0
array.push(pool, label.new(bar_index, high, str.tostring(bar_index)))
plot(array.size(pool), "Labels kept", display = display.data_window)See also
Runs on Sidechair