calc_var_ohlc | R Documentation |
Calculate the variance of returns from OHLC prices using different price range estimators.
calc_var_ohlc(
ohlc,
method = "yang_zhang",
closel = 0L,
scale = TRUE,
index = 0L
)
ohlc |
A time series or a matrix of OHLC prices. |
method |
A character string representing the price range estimator for calculating the variance. The estimators include:
(The default is the |
closel |
A vector with the lagged close prices
of the OHLC time series. This is an optional argument. (The
default is |
scale |
Boolean argument: Should the returns be divided
by the time index, the number of seconds in each period? (The default is
|
index |
A vector with the time index of the time
series. This is an optional argument (the default is |
The function calc_var_ohlc()
calculates the variance from all the
different intra-day and day-over-day returns (defined as the differences
of OHLC prices), using several different variance estimation
methods.
The function calc_var_ohlc()
does not calculate the logarithm of
the prices.
So if the argument ohlc
contains dollar prices then
calc_var_ohlc()
calculates the dollar variance.
If the argument ohlc
contains the log prices then
calc_var_ohlc()
calculates the percentage variance.
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.
If the number of rows of ohlc
is less than 3
then it
returns zero.
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 optional argument closel
are the lagged close prices
of the OHLC time series. Passing in the lagged close prices
speeds up the calculation, so it's useful for rolling calculations.
The function calc_var_ohlc()
is implemented in RcppArmadillo
C++
code, and it's over 10
times faster than
calc_var_ohlc_r()
, which is implemented in R
code.
A single numeric value equal to the variance of the OHLC time series.
## 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)))
# Calculate the variance of SPY returns, with scaling of the returns
HighFreq::calc_var_ohlc(ohlc,
method="yang_zhang", scale=TRUE, index=indeks)
# Calculate variance without accounting for overnight jumps
HighFreq::calc_var_ohlc(ohlc,
method="rogers_satchell", scale=TRUE, index=indeks)
# Calculate the variance without scaling the returns
HighFreq::calc_var_ohlc(ohlc, scale=FALSE)
# Calculate the variance by passing in the lagged close prices
closel <- HighFreq::lagit(ohlc[, 4])
all.equal(HighFreq::calc_var_ohlc(ohlc),
HighFreq::calc_var_ohlc(ohlc, closel=closel))
# Compare with HighFreq::calc_var_ohlc_r()
all.equal(HighFreq::calc_var_ohlc(ohlc, index=indeks),
HighFreq::calc_var_ohlc_r(ohlc))
# Compare the speed of Rcpp with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::calc_var_ohlc(ohlc),
Rcode=HighFreq::calc_var_ohlc_r(ohlc),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.