Sidechair

array.new_float()

Browse the reference

Builds an array of floats for prices, ratios and other decimal values, empty by default or pre-filled to a size.

Try it in the editor

A float array is the natural buffer for prices, returns and levels. array.new_float(5, 1.5) starts with five items that all hold 1.5, and array.new_float() starts empty, ready for array.push().

Items you do not fill are na, not 0, and arithmetic on an na item stays na. Pass 0.0 as the initial_value when the slots are running totals. array.avg() and array.stdev() read the items directly, so a pushed window of closes turns into bands in a few lines.

Syntax

Pine Script
array.new_float(size: series int (optional), initial_value: series int/float (optional)) -> array<float>

Arguments

NameTypeDefaultDescription
sizeseries int0How many float slots to start with: 0 or more, up to 100,000. The default of 0 gives an empty array that grows with array.push().
initial_valueseries int/floatnaThe float written into every starting slot; whole numbers are widened to floats. Left out, each slot is na rather than 0.

Returns

array<float>: A new array of floats with the requested size, every slot holding initial_value or na.

Remarks

  • An int initial_value such as 1 is stored as 1.0, so the array stays a float array.
  • Put var in front of the declaration when the window has to survive from bar to bar; without it the array is rebuilt empty on every bar.

Example

Pine ScriptBands from a 20-bar windowOpen in editor
//@version=6
indicator("array.new_float: bands from a window", overlay = true)
var window = array.new_float(0)
array.push(window, close)
if array.size(window) > 20
    array.shift(window)
mid = array.avg(window)
dev = array.stdev(window)
plot(mid, "Mean", color.gray)
plot(mid + 2 * dev, "Upper", color.green)
plot(mid - 2 * dev, "Lower", color.red)

See also

Runs on Sidechair