run_min: Calculate the trailing minimum values of streaming _time...

View source: R/RcppExports.R

run_minR Documentation

Calculate the trailing minimum values of streaming time series data using an online recursive formula.

Description

Calculate the trailing minimum values of streaming time series data using an online recursive formula.

Usage

run_min(tseries, lambda)

Arguments

tseries

A time series or a matrix.

lambda

A decay factor which multiplies past estimates.

Details

The function run_min() calculates the trailing minimum values of streaming time series data by recursively weighting present and past values using the decay factor \lambda.

It calculates the trailing minimum values p^{min}_t of the streaming data p_t as follows:

p^{min}_t = min(p_t, \lambda p^{min}_{t-1} + (1-\lambda) p_t)

The first term in the sum is the minimum value multiplied by the decay factor \lambda, so that the past minimum value is gradually "forgotten". The second term pulls the minimum value to the current value p_t.

The value of the decay factor \lambda must be in the range between 0 and 1. If \lambda is close to 1 then the past minimum values persist for longer. This is equivalent to a long look-back interval. If \lambda is much less than 1 then the past minimum values decay quickly, and the trailing minimum depends on the more recent streaming data. This is equivalent to a short look-back interval.

The above formula can also be expressed as:

p^{min}_t = \lambda min(p_t, p^{min}_{t-1}) + (1-\lambda) p_t

The first term is the minimum value multiplied by the decay factor \lambda, so that the past minimum value is gradually "forgotten". The second term pulls the minimum value to the current value p_t.

The above recursive formula is convenient for processing live streaming data because it doesn't require maintaining a buffer of past data.

The function run_min() returns a matrix with the same dimensions as the input argument tseries.

Value

A matrix with the same dimensions as the input argument tseries.

Examples

## Not run: 
# Calculate historical prices
closep <- zoo::coredata(quantmod::Cl(rutils::etfenv$VTI))
# Calculate the trailing minimums
lambdaf <- 0.9
pricmin <- HighFreq::run_min(closep, lambda=lambdaf)
# Plot dygraph of VTI prices and trailing minimums
datav <- cbind(quantmod::Cl(rutils::etfenv$VTI), pricmin)
colnames(datav) <- c("prices", "min")
colnamev <- colnames(datav)
dygraphs::dygraph(datav, main="VTI Prices and Trailing Minimums") %>%
  dySeries(label=colnamev[1], strokeWidth=1, col="blue") %>%
  dySeries(label=colnamev[2], strokeWidth=1, col="red")

## End(Not run)


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