This tutorial will break down the syntax, arguments, and application of ta.variance()
to enhance your scripting capabilities.
Syntax of ta.variance()
The ta.variance()
function is formulated as follows:
ta.variance(dataSeries, periodLength, isBiased) → series float
dataSeries
(series int/float): This is the series of values you wish to analyze. It can be any series of numbers, like closing prices of a stock.periodLength
(series int): This defines the number of bars to consider for the calculation.isBiased
(series bool): This optional parameter allows you to choose between a biased (true) or unbiased (false) estimate. The default setting is true.
Key Arguments Explained
- Source (
dataSeries
): This argument is the backbone of the variance calculation, representing the dataset from which the variance will be computed. For instance, if you’re analyzing the variance in closing prices, this series would consist of the closing price values. - Length (
periodLength
): The length parameter determines the scope of the calculation, specifying the number of bars to include in the variance calculation. A higher number offers a broader view but may smooth out short-term fluctuations. - Biased (
isBiased
): The biased versus unbiased distinction is crucial for accurate statistical analysis. A biased estimate (true) assumes that your dataset represents the entire population, while an unbiased estimate (false) treats your dataset as a sample of a larger population. The choice between these two options affects the calculation of variance, with the unbiased estimate adjusting for the sample size.
Example: Calculating Daily Price Variance
Let’s apply ta.variance()
to calculate the 20-day variance of a stock’s closing prices, using an unbiased estimate.
//@version=5 indicator("20 Day Price Variance", overlay=true) / // Define the source and length priceData = close days = 20 // Calculate variance using an unbiased estimate priceVariance = ta.variance(priceData, days, false) // Plot the variance plot(priceVariance, title="Price Variance")
In this example, priceData
is set to the closing price (close
), and days
is set to 20, specifying a 20-day period for the variance calculation. By setting isBiased
to false, we opt for an unbiased estimate, appropriate for treating the 20-day window as a sample of a larger dataset.
Code Walkthrough
- Script Declaration with Version and Overlay:
//@version=5
: This line specifies the version of Pine Script used for the script. Version 5 is the latest version, offering the most updated features and functionalities.indicator("20 Day Price Variance", overlay=true)
: This line declares the script as an indicator named “20 Day Price Variance” and setsoverlay=true
, indicating that the indicator will be plotted directly on the price chart, overlaying the price movements.
- Define the Source and Length:
priceData = close
: This line assigns the closing prices of the bars to a variable namedpriceData
. In Pine Script,close
represents the closing price of the current bar.days = 20
: This line sets the variabledays
to 20, indicating the period length over which the variance will be calculated. Here, it specifies that the variance calculation will consider the last 20 bars.
- Calculate Variance Using an Unbiased Estimate:
priceVariance = ta.variance(priceData, days, false)
: This line calculates the variance of thepriceData
(closing prices) over the last 20 bars (days
) using an unbiased estimate. Thefalse
parameter indicates that the calculation is for an unbiased estimate, suitable for a sample of a larger population, rather than the entire population.
- Plot the Variance:
plot(priceVariance, title="Price Variance")
: This line plots the calculated variance on the chart. Theplot
function takes thepriceVariance
variable as input and displays it on the chart with the title “Price Variance”. This allows users to visually assess the variance of closing prices over the selected period directly on the trading chart.
Key Features and Takeaways
- Function Useability:
ta.variance()
is versatile, supporting both biased and unbiased estimates to fit different analysis needs. - Syntax and Application: The function’s syntax is straightforward, making it accessible for both novice and experienced Pine Script users.
- Analytical Power: By providing insights into market volatility,
ta.variance()
is a powerful tool for developing more informed trading strategies.
This tutorial covers the fundamentals of the ta.variance()
function in Pine Script, setting a foundation for its practical application in market analysis and strategy development.