roll_var_ohlc: Calculate a _vector_ of variance estimates over a rolling...

View source: R/RcppExports.R

roll_var_ohlcR Documentation

Calculate a vector of variance estimates over a rolling look-back interval attached at the end points of a time series or a matrix with OHLC price data.

Description

Calculate a vector of variance estimates over a rolling look-back interval attached at the end points of a time series or a matrix with OHLC price data.

Usage

roll_var_ohlc(
  ohlc,
  startp = 0L,
  endd = 0L,
  step = 1L,
  lookb = 1L,
  stub = 0L,
  method = "yang_zhang",
  scale = TRUE,
  index = 0L
)

Arguments

ohlc

A time series or a matrix with OHLC price data.

startp

An integer vector of start points (the default is startp = 0).

endd

An integer vector of end points (the default is endd = 0).

step

The number of time periods between the end points (the default is step = 1).

lookb

The number of end points in the look-back interval (the default is lookb = 1).

stub

An integer value equal to the first end point for calculating the end points (the default is stub = 0).

method

A character string representing the price range estimator for calculating the variance. The estimators include:

  • "close" close-to-close estimator,

  • "rogers_satchell" Rogers-Satchell estimator,

  • "garman_klass" Garman-Klass estimator,

  • "garman_klass_yz" Garman-Klass with account for close-to-open price jumps,

  • "yang_zhang" Yang-Zhang estimator,

(The default is the "yang_zhang" estimator.)

scale

Boolean argument: Should the returns be divided by the time index, the number of seconds in each period? (The default is scale = TRUE.)

index

A vector with the time index of the time series. This is an optional argument (the default is index=0).

Details

The function roll_var_ohlc() calculates a vector of variance estimates over a rolling look-back interval attached at the end points of the time series ohlc.

The input OHLC time series ohlc is assumed to contain the log prices.

The function roll_var_ohlc() performs a loop over the end points, subsets the previous (past) rows of ohlc, and passes them into the function calc_var_ohlc().

At each end point, the variance is calculated over a look-back interval equal to lookb number of end points. In the initial warmup period, the variance is calculated over an expanding look-back interval.

If the arguments endd and startp are not given then it first calculates a vector of end points separated by step time periods. It calculates the end points along the rows of ohlc using the function calc_endpoints(), with the number of time periods between the end points equal to step time periods.

For example, the rolling variance at daily end points with an 11 day look-back, can be calculated using the parameters step = 1 and lookb = 1 (Assuming the ohlc data has daily frequency.)

Similarly, the rolling variance at 25 day end points with a 75 day look-back, can be calculated using the parameters step = 25 and lookb = 3 (because 3*25 = 75).

The function roll_var_ohlc() calculates the variance from all the different intra-day and day-over-day returns (defined as the differences between OHLC prices), using several different variance estimation methods.

The default method is "yang_zhang", which theoretically has the lowest standard error among unbiased estimators. The methods "close", "garman_klass_yz", and "yang_zhang" do account for close-to-open price jumps, while the methods "garman_klass" and "rogers_satchell" do not account for close-to-open price jumps.

If scale is TRUE (the default), then the returns are divided by the differences of the time index (which scales the variance to the units of variance per second squared.) This is useful when calculating the variance from minutes bar data, because dividing returns by the number of seconds decreases the effect of overnight price jumps. If the time index is in days, then the variance is equal to the variance per day squared.

The optional argument index is the time index of the time series ohlc. If the time index is in seconds, then the differences of the index are equal to the number of seconds in each time period. If the time index is in days, then the differences are equal to the number of days in each time period.

The function roll_var_ohlc() is implemented in RcppArmadillo C++ code, which makes it several times faster than R code.

Value

A column vector of variance estimates, with the number of rows equal to the number of end points.

Examples

## Not run: 
# Extract the log OHLC prices of SPY
ohlc <- log(HighFreq::SPY)
# Extract the time index of SPY prices
indeks <- c(1, diff(xts::.index(ohlc)))
# Rolling variance at minutes end points, with a 21 minute look-back
varoll <- HighFreq::roll_var_ohlc(ohlc, 
                              step=1, lookb=21, 
                              method="yang_zhang", 
                              index=indeks, scale=TRUE)
# Daily OHLC prices
ohlc <- rutils::etfenv$VTI
indeks <- c(1, diff(xts::.index(ohlc)))
# Rolling variance at 5 day end points, with a 20 day look-back (20=4*5)
varoll <- HighFreq::roll_var_ohlc(ohlc, 
                              step=5, lookb=4, 
                              method="yang_zhang", 
                              index=indeks, scale=TRUE)
# Same calculation in R
nrows <- NROW(ohlc)
closel = HighFreq::lagit(ohlc[, 4])
endd <- drop(HighFreq::calc_endpoints(nrows, 3)) + 1
startp <- drop(HighFreq::calc_startpoints(endd, 2))
npts <- NROW(endd)
varollr <- sapply(2:npts, function(it) {
  rangev <- startp[it]:endd[it]
  sub_ohlc = ohlc[rangev, ]
  sub_close = closel[rangev]
  sub_index = indeks[rangev]
  HighFreq::calc_var_ohlc(sub_ohlc, closel=sub_close, scale=TRUE, index=sub_index)
})  # end sapply
varollr <- c(0, varollr)
all.equal(drop(var_rolling), varollr)

## End(Not run)

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