Sidechair

array.avg()

Browse the reference

Returns the arithmetic mean of the numbers in an array.

Try it in the editor

The sum of the numbers divided by how many there are. Items that are na are left out of both, so a single gap does not spoil the result.

An array with no numbers returns na.

Syntax

Pine Script
array.avg(id: array<int/float>) -> series float
array.avg(id: array<int>) -> series int
array.avg() -> series float
array.avg() -> series int

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to average. na items are skipped, so they do not drag the mean down.

Returns

series float or series int: The mean of the numbers, or na.

Remarks

  • It matches ta.sma() when the array holds the last N values of a series.

Example

Pine ScriptMean of the last 7 closesOpen in editor
//@version=6
indicator("array.avg: 7-bar mean", overlay = true)
var win = array.new_float()
array.push(win, close)
if array.size(win) > 7
    array.shift(win)
plot(array.avg(win), "Mean close")

See also

Runs on Sidechair