StatisticalPine Script v6

ta.variance()Variance

Variance is the square of the standard deviation. It measures how far a set of numbers are spread out from their average value.

TradingView language reference

Syntax

Syntax
ta.variance(source, length, biased) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars in the rolling window.
biasedseries boolOptional. Population vs sample variance divisor; default true.

Returns

Variance of source for length bars back.

Remarks

na source values are ignored and the calculation uses length non-na observations. Variance has squared source units; price-level variance and return variance answer different questions.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.variance")
plot(ta.variance(close, 20, true))

// Relationship to stdev
plot(math.sqrt(ta.variance(close, 20, true)))
plot(ta.stdev(close, 20, true))

Trading Applications

Measure price dispersion for volatility analysis

Build custom volatility indicators

Calculate risk metrics for portfolio management

Use as input for statistical trading models

Related Functions

ta.stdevta.dev

Frequently Asked Questions

ta.variance() computes rolling variance of the source over length bars (average squared deviation from the rolling mean), after ignoring na values.

Standard deviation is the square root of variance. In Pine, ta.stdev() and ta.variance() are consistent when using the same biased flag and length.

When biased is true (default), variance divides the squared deviations by n; when false, it divides by n−1 and needs more than one observation. Match this flag to ta.stdev() for consistency.

Generate ta.variance() Code with AI

Draft Pine Script using ta.variance() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.