Sidechair

ta.variance()

Browse the reference

Returns the variance of source over the last length bars: the mean squared distance from the window's average.

Try it in the editor

ta.variance takes the mean of the window, squares each value's distance from it and averages those squares. With biased = true the divisor is length; with false it is length - 1.

The result is in squared units of source and equals ta.stdev(source, length, biased) squared. Early bars, before the window fills, return na.

Syntax

Pine Script
ta.variance(source: series int/float, length: series int, biased: series bool (optional)) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredThe series whose squared spread around its own mean is measured over length bars.
lengthseries intrequiredNumber of bars in the window. A whole number of 1 or more; the unbiased form needs 2 or more.
biasedseries booltruetrue divides by length (population). false divides by length - 1 (sample).

Returns

series float: the variance of the window, or na while it is not full.

Remarks

  • Variance of a constant series is 0.

Example

Pine ScriptVariance of the closeOpen in editor
//@version=6
indicator("Variance")
plot(ta.variance(close, 20), "Variance", color = color.purple)
plot(math.pow(ta.stdev(close, 20), 2), "Stdev squared", color = color.orange)

See also

Runs on Sidechair