to.period: Convert time series data to an OHLC series

Description Usage Arguments Details Value Note Author(s) Examples

Description

Convert an OHLC or univariate object to a specified periodicity lower than the given data object. For example, convert a daily series to a monthly series, or a monthly series to a yearly one, or a one minute series to an hourly series.

The result will contain the open and close for the given period, as well as the maximum and minimum over the new period, reflected in the new high and low, respectively.

If volume for a period was available, the new volume will also be calculated.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
to.minutes(x,k,name,...)
to.minutes3(x,name,...)
to.minutes5(x,name,...)
to.minutes10(x,name,...)
to.minutes15(x,name,...)
to.minutes30(x,name,...)
to.hourly(x,name,...)
to.daily(x,drop.time=TRUE,name,...)

to.weekly(x,drop.time=TRUE,name,...)
to.monthly(x,indexAt='yearmon',drop.time=TRUE,name,...)
to.quarterly(x,indexAt='yearqtr',drop.time=TRUE,name,...)
to.yearly(x,drop.time=TRUE,name,...)

to.period(x,
          period = 'months', 
          k = 1, 
          indexAt, 
          name=NULL,
          OHLC = TRUE,
          ...)

Arguments

x

a univariate or OHLC type time-series object

period

period to convert to. See details.

indexAt

convert final index to new class or date. See details

drop.time

remove time component of POSIX datestamp (if any)

k

number of sub periods to aggregate on (only for minutes and seconds)

name

override column names

OHLC

should an OHLC object be returned? (only OHLC=TRUE currently supported)

...

additional arguments

Details

Essentially an easy and reliable way to convert one periodicity of data into any new periodicity. It is important to note that all dates will be aligned to the end of each period by default - with the exception of to.monthly and to.quarterly, which index by ‘yearmon’ and ‘yearqtr’ from the zoo package, respectively.

Valid period character strings include: "seconds", "minutes", "hours", "days", "weeks", "months", "quarters", and "years". These are calculated internally via endpoints. See that function's help page for further details.

To adjust the final indexing style, it is possible to set indexAt to one of the following: ‘yearmon’, ‘yearqtr’, ‘firstof’, ‘lastof’, ‘startof’, or ‘endof’. The final index will then be yearmon, yearqtr, the first time of the period, the last time of the period, the starting time in the data for that period, or the ending time in the data for that period, respectively.

It is also possible to pass a single time series, such as a univariate exchange rate, and return an OHLC object of lower frequency - e.g. the weekly OHLC of the daily series.

Setting drop.time to TRUE (the default) will convert a series that includes a time component into one with just a date index, as the time index is often of little value in lower frequency series.

It is not possible to convert a series from a lower periodicity to a higher periodicity - e.g. weekly to daily or daily to 5 minute bars, as that would require magic.

Value

An object of the original type, with new periodicity.

Note

In order for this function to work properly on OHLC data, it is necessary that the Open, High, Low and Close columns be names as such; including the first letter capitalized and the full spelling found. Internally a call is made to reorder the data into the correct column order, and then a verification step to make sure that this ordering and naming has succeeded. All other data formats must be aggregated with functions such as aggregate and period.apply.

This method should work on almost all time-series-like objects. Including ‘timeSeries’, ‘zoo’, ‘ts’, ‘its’, and ‘irts’. It is even likely to work well for other data structures - including ‘data.frames’ and ‘matrix’ objects.

Internally a call to as.xts converts the original x into the universal xts format, and then re-converts back to the original type.

A special note with respect to ‘ts’ objects. As these are strictly regular they may include NA values. These are stripped for aggregation purposes, though replaced before returning. This inevitably leads to many, many additional ‘NA’ values in the data. It is more beneficial to consider using an ‘xts’ object originally, or converting to one in the function call by means of as.xts.

Author(s)

Jeffrey A. Ryan

Examples

1
2
3
4
5
6
7
8
9

Example output

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

         samplexts.Open samplexts.High samplexts.Low samplexts.Close
Jan 2007       50.03978       50.77336      49.76308        50.22578
Feb 2007       50.22448       51.32342      50.19101        50.77091
Mar 2007       50.81620       50.81620      48.23648        48.97490
Apr 2007       48.94407       50.33781      48.80962        49.33974
May 2007       49.34572       49.69097      47.51796        47.73780
Jun 2007       47.74432       47.94127      47.09144        47.76719
         sample_matrix.Open sample_matrix.High sample_matrix.Low
Jan 2007           50.03978           50.77336          49.76308
Feb 2007           50.22448           51.32342          50.19101
Mar 2007           50.81620           50.81620          48.23648
Apr 2007           48.94407           50.33781          48.80962
May 2007           49.34572           49.69097          47.51796
Jun 2007           47.74432           47.94127          47.09144
         sample_matrix.Close
Jan 2007            50.22578
Feb 2007            50.77091
Mar 2007            48.97490
Apr 2007            49.33974
May 2007            47.73780
Jun 2007            47.76719
An 'xts' object on Jan 2007/Jun 2007 containing:
  Data: num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "samplexts.Open" "samplexts.High" "samplexts.Low" "samplexts.Close"
  Indexed by objects of class: [yearmon] TZ: 
  xts Attributes:  
 NULL
 num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" ...
  ..$ : chr [1:4] "sample_matrix.Open" "sample_matrix.High" "sample_matrix.Low" "sample_matrix.Close"

xts documentation built on May 2, 2019, 5:18 p.m.