to_period: Aggregate an _OHLC_ time series to a lower periodicity.

View source: R/rutils.R

to_periodR Documentation

Aggregate an OHLC time series to a lower periodicity.

Description

Given an OHLC time series at high periodicity (say seconds), calculates the OHLC prices at a lower periodicity (say minutes).

Usage

to_period(
  timeser,
  period = "minutes",
  k = 1,
  endpoints = xts::endpoints(timeser, period, k)
)

Arguments

timeser

An OHLC time series of prices in xts format.

period

aggregation interval ("seconds", "minutes", "hours", "days", "weeks", "months", "quarters", and "years").

k

The number of periods to aggregate over (for example if period="minutes" and k=2, then aggregate over two minute intervals.)

endpoints

An integer vector of end points.

Details

The function to_period() performs a similar aggregation as function xts::to.period() from package xts, but has the flexibility to aggregate to a user-specified vector of end points. The function to_period() simply calls the compiled function toPeriod() (from package xts), to perform the actual aggregations. If endpoints are passed in explicitly, then the period argument is ignored.

Value

A OHLC time series of prices in xts format, with a lower periodicity defined by the endpoints.

Examples

## Not run: 
# Define end points at 10-minute intervals (HighFreq::SPY are minutes bars)
endd <- rutils::calc_endpoints(HighFreq::SPY["2009"], interval=10)
# Aggregate over 10-minute endpoints:
ohlc <- rutils::to_period(timeser=HighFreq::SPY["2009"], endpoints=endd)
# Aggregate over days:
ohlc <- rutils::to_period(timeser=HighFreq::SPY["2009"], period="days")
# Equivalent to:
ohlc <- xts::to.period(x=HighFreq::SPY["2009"], period="days", name=rutils::get_name(colnames(HighFreq::SPY)[1])

## End(Not run)


algoquant/rutils documentation built on Sept. 7, 2024, 7:22 p.m.