Home » Array Functions » Array.variance Function in Pine Script

Array.variance Function in Pine Script

Photo of author
Published on

In this tutorial, we will delve into the array.variance function in Pine Script. The array.variance function is a powerful tool in the Pine Script toolbox, allowing you to compute the variance of an array’s elements. Variance is a statistical measurement of the spread between numbers in a data set. It is often used in financial markets to measure volatility or the dispersion of asset prices.

Syntax of array.variance

The array.variance function uses the following syntax:

array.variance(id, biased) → series float
array.variance(id, biased) → series int

It has two arguments:

  1. id – this represents an array object, either a series of floats or integers.
  2. biased – this is a series of booleans that determines which estimate should be used. It is optional, and the default is set to true.

Understanding the Arguments

The ‘id’ Argument

The id argument in the array.variance function represents the array you are performing the operation on. This can be an array of integers or floats, i.e., numbers with or without decimal points.

The ‘biased’ Argument

The biased argument is a boolean series that determines whether a biased or unbiased estimate of variance should be used.

  • If biased is set to true, the function will calculate the variance using a biased estimate of the entire population. This means that the variance is computed by dividing the sum of squared deviations from the mean by the number of observations.
  • If biased is set to false, the function will use an unbiased estimate of a sample. In this case, the variance is computed by dividing the sum of squared deviations from the mean by the number of observations minus 1.

Example of array.variance in Use

Let’s examine a practical application of array.variance function.

//@version=5
indicator("array.variance example")
a = array.new_float(0)
for i = 0 to 9
    array.push(a, close[i])
plot(array.variance(a, true), color = color.red)
plot(array.variance(a, false), color = color.blue)

In the script above, we first define an empty float array a. Then, we fill this array with the last 10 closing prices using a for loop. We push each closing price into the array using the array.push function.

Finally, we calculate and plot the variance of this array of closing prices. We do it twice, once using the biased estimate (red line) and once using the unbiased estimate (blue line).

The plot function is used to visualize the calculated variances on the chart. The color.red and color.blue parameters are used to distinguish between the biased and unbiased variance plots.

Key Takeaways

  • The array.variance function is used to calculate the variance of an array’s elements in Pine Script.
  • The function can work with both integer and float arrays.
  • The ‘biased’ parameter lets you choose between a biased estimate (dividing by the number of observations) or an unbiased estimate (dividing by the number of observations minus 1).
  • The array.variance function can be used to measure the volatility or dispersion of asset prices in financial markets.

Conclusion

Understanding the array.variance function is essential for performing statistical calculations in Pine Script. This function is a powerful tool that can enhance your trading strategies by providing insights into price volatility and market dynamics. As always, practice and experimentation are key to mastering any new function.

Leave a Comment