summarise_ohlc: Calculate OHLC prices

Description Usage Arguments Details Examples

View source: R/summarise_ohlc.R

Description

summarise_ohlc() calculates new OHLC price columns from specified price columns. aggregate_ohlc() takes an existing set of OHLC columns and reaggregates them to a coarser periodicity (daily to monthly, etc). The easiest way to reaggregate is to collapse your index to a different period, group by it, and call aggregate_ohlc().

Usage

1
2
3
4
5
summarise_ohlc(.tbl_time, ...)

summarize_ohlc(.tbl_time, ...)

aggregate_ohlc(.tbl_time, open, high, low, close)

Arguments

.tbl_time

A tbl_time object.

...

Bare column names of the price columns to be converted to OHLC format.

open, high, low, close

Bare column names of existing OHLC columns to be reaggregated.

Details

Both summarise_ohlc() and aggregate_ohlc() are wrappers around a common dplyr::summarise() style call that calculates the Open (first), High (max), Low (min) and Close (last) values. Neither function knows how to transform to a new period on its own, so the way to aggregate from, say, daily to monthly involves collapsing a daily column of dates to monthly using tibbletime::collapse_index(), grouping on the collapsed date, and then calling aggregate_ohlc(). The same workflow applies to summarise_ohlc() when doing the similar calculation of immediately creating monthly OHLC summaries from a daily price column.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# ---------------------------------------------------------------------------
# Setup

library(tibbletime)
library(dplyr)

data(FB)
FB_time <- FB %>%
  as_tbl_time(date)

# ---------------------------------------------------------------------------
# Calculate monthly OHLC

FB_monthly <- FB_time %>%
  collapse_by("monthly") %>%
  group_by(date) %>%
  summarise_ohlc(adjusted)

FB_monthly

# ---------------------------------------------------------------------------
# Reaggregate existing monthly OHLC up to yearly OHLC

FB_monthly %>%
  collapse_by("yearly") %>%
  group_by(date) %>%
  aggregate_ohlc(adjusted_open, adjusted_high, adjusted_low, adjusted_close)

DavisVaughan/tidyfinance documentation built on May 24, 2019, 8:46 p.m.