roll_stats: Calculate a vector of statistics over an _OHLC_ time series,...

View source: R/HighFreq.R

roll_statsR Documentation

Calculate a vector of statistics over an OHLC time series, and calculate a rolling mean over the statistics.

Description

Calculate a vector of statistics over an OHLC time series, and calculate a rolling mean over the statistics.

Usage

roll_stats(
  ohlc,
  calc_stats = "ohlc_variance",
  look_back = 11,
  weighted = TRUE,
  ...
)

Arguments

...

additional parameters to the function calc_stats.

ohlc

An OHLC time series of prices and trading volumes, in xts format.

calc_stats

The name of the function for estimating statistics of a single row of OHLC data, such as volatility, skew, and higher moments.

look_back

The size of the look-back interval, equal to the number of rows of data used for calculating the rolling mean.

weighted

Boolean argument: should statistic be weighted by trade volume? (default TRUE)

Details

The function roll_stats() calculates a vector of statistics over an OHLC time series, such as volatility, skew, and higher moments. The statistics could also be any other aggregation of a single row of OHLC data, for example the High price minus the Low price squared. The length of the vector of statistics is equal to the number of rows of the argument ohlc. Then it calculates a trade volume weighted rolling mean over the vector of statistics over and calculate statistics.

Value

An xts time series with a single column and the same number of rows as the argument ohlc.

Examples

# Calculate time series of rolling variance and skew estimates
var_rolling <- roll_stats(ohlc=HighFreq::SPY, look_back=21)
skew_rolling <- roll_stats(ohlc=HighFreq::SPY, calc_stats="ohlc_skew", look_back=21)
skew_rolling <- skew_rolling/(var_rolling)^(1.5)
skew_rolling[1, ] <- 0
skew_rolling <- rutils::na_locf(skew_rolling)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.