roll_apply: Apply an aggregation function over a rolling look-back...

View source: R/HighFreq.R

roll_applyR Documentation

Apply an aggregation function over a rolling look-back interval and the end points of an OHLC time series, using R code.

Description

Apply an aggregation function over a rolling look-back interval and the end points of an OHLC time series, using R code.

Usage

roll_apply(
  xtsv,
  agg_fun,
  look_back = 2,
  endpoints = seq_along(xtsv),
  by_columns = FALSE,
  out_xts = TRUE,
  ...
)

Arguments

...

additional parameters to the function agg_fun.

xtsv

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

agg_fun

The name of the aggregation function to be applied over a rolling look-back interval.

look_back

The number of end points in the look-back interval used for applying the aggregation function (including the current row).

by_columns

Boolean argument: should the function agg_fun() be applied column-wise (individually), or should it be applied to all the columns combined? (default is FALSE)

out_xts

Boolean argument: should the output be coerced into an xts series? (default is TRUE)

endpoints

An integer vector of end points.

Details

The function roll_apply() applies an aggregation function over a rolling look-back interval attached at the end points of an OHLC time series.

The function roll_apply() is implemented in R code.

HighFreq::roll_apply() performs similar operations to the functions rollapply() and period.apply() from package xts, and also the function apply.rolling() from package PerformanceAnalytics. (The function rollapply() isn't exported from the package xts.)

But HighFreq::roll_apply() is faster because it performs less type-checking and skips other overhead. Unlike the other functions, roll_apply() doesn't produce any leading NA values.

The function roll_apply() can be called in two different ways, depending on the argument endpoints. If the argument endpoints isn't explicitly passed to roll_apply(), then the default value is used, and roll_apply() performs aggregations over overlapping intervals at each point in time.

If the argument endpoints is explicitly passed to roll_apply(), then roll_apply() performs aggregations over intervals attached at the endpoints. If look_back=2 then the aggregations are performed over non-overlapping intervals, otherwise they are performed over overlapping intervals.

If the argument out_xts is TRUE (the default) then the output is coerced into an xts series, with the number of rows equal to the length of argument endpoints. Otherwise a list is returned, with the length equal to the length of argument endpoints.

If out_xts is TRUE and the aggregation function agg_fun() returns a single value, then roll_apply() returns an xts time series with a single column. If out_xts is TRUE and if agg_fun() returns a vector of values, then roll_apply() returns an xts time series with multiple columns, equal to the length of the vector returned by the aggregation function agg_fun().

Value

Either an xts time series with the number of rows equal to the length of argument endpoints, or a list the length of argument endpoints.

Examples

# extract a single day of SPY data
ohlc <- HighFreq::SPY["2012-02-13"]
interval <- 11  # number of data points between end points
look_back <- 4  # number of end points in look-back interval
# Calculate the rolling sums of ohlc columns over a rolling look-back interval
agg_regations <- roll_apply(ohlc, agg_fun=sum, look_back=look_back, by_columns=TRUE)
# Apply a vector-valued aggregation function over a rolling look-back interval
agg_function <- function(ohlc)  c(max(ohlc[, 2]), min(ohlc[, 3]))
agg_regations <- roll_apply(ohlc, agg_fun=agg_function, look_back=look_back)
# Define end points at 11-minute intervals (HighFreq::SPY is minutely bars)
endpoints <- rutils::endpoints(ohlc, interval=interval)
# Calculate the sums of ohlc columns over endpoints using non-overlapping intervals
agg_regations <- roll_apply(ohlc, agg_fun=sum, endpoints=endpoints, by_columns=TRUE)
# Apply a vector-valued aggregation function over the endpoints of ohlc
# using overlapping intervals
agg_regations <- roll_apply(ohlc, agg_fun=agg_function,
                            look_back=5, endpoints=endpoints)
                            

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