run_max: Calculate the trailing maximum values of streaming _time...

View source: R/RcppExports.R

run_maxR Documentation

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

Description

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

Usage

run_max(tseries, lambda)

Arguments

tseries

A time series or a matrix.

lambda

A decay factor which multiplies past estimates.

Details

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

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

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

The first term in the sum is the maximum value multiplied by the decay factor \lambda, so that the past maximum value is gradually "forgotten". The second term pulls the maximum 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 maximum values persist for longer. This is equivalent to a long look-back interval. If \lambda is much less than 1 then the past maximum values decay quickly, and the trailing maximum 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^{max}_t = \lambda max(p_t, p^{max}_{t-1}) + (1-\lambda) p_t

The first term is the maximum value multiplied by the decay factor \lambda, so that the past maximum value is gradually "forgotten". The second term pulls the maximum 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_max() 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 maximums
lambdaf <- 0.9
pricmax <- HighFreq::run_max(closep, lambda=lambdaf)
# Plot dygraph of VTI prices and trailing maximums
datav <- cbind(quantmod::Cl(rutils::etfenv$VTI), pricmax)
colnames(datav) <- c("prices", "max")
colnamev <- colnames(datav)
dygraphs::dygraph(datav, main="VTI Prices and Trailing Maximums") %>%
  dySeries(label=colnamev[1], strokeWidth=2, col="blue") %>%
  dySeries(label=colnamev[2], strokeWidth=2, col="red")

## End(Not run)


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