aggregatePrice: Aggregate a time series but keep first and last observation

View source: R/dataHandling.R

aggregatePriceR Documentation

Aggregate a time series but keep first and last observation

Description

Function to aggregate high frequency data by last tick aggregation to an arbitrary periodicity based on wall clocks. Alternatively the aggregation can be done by number of ticks. In case we DON'T do tick-based aggregation, this function accepts arbitrary number of symbols over a arbitrary number of days. Although the function has the word Price in the name, the function is general and works on arbitrary time series, either xts or data.table objects the latter requires a DT column containing POSIXct time stamps.

Usage

aggregatePrice(
  pData,
  alignBy = "minutes",
  alignPeriod = 1,
  marketOpen = "09:30:00",
  marketClose = "16:00:00",
  fill = FALSE,
  tz = NULL
)

Arguments

pData

data.table or xts object to be aggregated containing the intraday price series, possibly across multiple days.

alignBy

character, indicating the time scale in which alignPeriod is expressed. Possible values are: "secs", "seconds", "mins", "minutes","hours", and "ticks". To aggregate based on a 5 minute frequency, set alignPeriod to 5 and alignBy to "minutes".

alignPeriod

positive numeric, indicating the number of periods to aggregate over. E.g. to aggregate based on a 5 minute frequency, set alignPeriod to 5 and alignBy to "minutes".

marketOpen

the market opening time, by default: marketOpen = "09:30:00".

marketClose

the market closing time, by default: marketClose = "16:00:00".

fill

indicates whether rows without trades should be added with the most recent value, FALSE by default.

tz

fallback time zone used in case we we are unable to identify the timezone of the data, by default: tz = NULL. We attempt to extract the timezone from the DT column (or index) of the data, which may fail. In case of failure we use tz if specified, and if it is not specified, we use "UTC"

Details

The time stamps of the new time series are the closing times and/or days of the intervals. The element of the returned series with e.g. time stamp 09:35:00 contains the last observation up to that point, including the value at 09:35:00 itself.

In case alignBy = "ticks", the sampling is done such the sampling starts on the first tick, and the last tick is always included. For example, if 14 observations are made on one day, and these are 1, 2, 3, ... 14. Then, with alignBy = "ticks" and alignPeriod = 3, the output will be 1, 4, 7, 10, 13, 14.

Value

A data.table or xts object containing the aggregated time series.

Author(s)

Jonathan Cornelissen, Kris Boudt, Onno Kleen, and Emil Sjoerup.

Examples

# Aggregate price data to the 30 second frequency
aggregatePrice(sampleTData, alignBy = "secs", alignPeriod = 30)
# Aggregate price data to the 30 second frequency including zero return price changes
aggregatePrice(sampleTData, alignBy = "secs", alignPeriod = 30)

# Aggregate price data to half a second frequency including zero return price changes
aggregatePrice(sampleTData, alignBy = "milliseconds", alignPeriod = 500, fill = TRUE)

jonathancornelissen/highfrequency documentation built on Jan. 10, 2023, 7:29 p.m.