Skip to contents

rolling_variance() is a generic S3 function that preserves the input class: double vector in, double vector out.

Handling of NA values

Leading NAs are always produced for the initial lookback period where insufficient data is available. If the input itself contains NAs, the behaviour depends on na.bridge:

na.bridge = FALSE (default)

NAs propagate through the TA-Lib C routine. Because rolling statistics smooth across time, a single NA in the input typically poisons every subsequent value.

na.bridge = TRUE

Input NAs are stripped, the statistic is computed on the dense series, and NAs are re-inserted at the original positions. Output length matches input length, but the computation treats non-consecutive observations as if they were adjacent - fine for sparse missing values, misleading across clustered gaps.

Usage

rolling_variance(x, n = 5, k = 1, na.bridge = FALSE)

Arguments

x

(double). A double vector.

n

(integer). Lookback period (window size). A positive integer of length 1.

k

multiplier

na.bridge

(logical). A logical of length 1. FALSE by default. When FALSE, input NAs propagate through the TA-Lib C routine (the rolling computation typically fills the remaining output with NA). When TRUE, input NA rows are stripped before computation and re-inserted at the original positions in the output, causing the statistic to treat non-consecutive non-NA observations as if they were adjacent - see the Handling of NA values section above for the consequences.

Value

A double vector with the same length of x

Author

Serkan Korkmaz

Examples

## load Bitcoin (BTC)
## series
data(BTC, package = "talib")

## calculate the indicator
## Open
output <- talib::rolling_variance(x = BTC[[1]])

## display the results
utils::tail(output)
#> [1] 3341785 3588907 4321770 4099366 4106782 1183102