The rutils package is a library of functions designed for simplifying data management and data modeling, and is used by many other algoquant packages.
The rutils package contains functions for:

The rutils package also includes a dataset with OHLC time series data for a portfolio of symbols. The data is contained in an environment called etf_env, which includes:

Each individual xts time series contains the columns: Open prices, High prices, Low prices, Close prices, trading Volume, Adjusted prices.

========

Installation and loading

How to install rutils package from GitHub:

install.packages("devtools")
library(devtools)
devtools::install_github(repo="algoquant/rutils")
library(rutils)

========

Datasets

The rutils package contains a dataset of daily OHLC time series in xts format, for a portfolio of stock (ETF) symbols. The time series are contained in an environment called etf_env. The data is set up for lazy loading, so it doesn't require calling data(etf_data) to load it before being able to call it.

suppressMessages(suppressWarnings(library(rutils)))
# show list of ETF time series in etf_env
rutils::etf_env$sym_bols
# get first six rows of VTI prices
head(rutils::etf_env$VTI)
# plot
quantmod::chart_Series(x=rutils::etf_env$VTI["2009-11"])

========

Examples

Download time series data from an external source (by default OHLC prices from YAHOO), and save it into an environment.

suppressMessages(suppressWarnings(library(rutils)))
# new environment for data
etf_env <- new.env()
# download data and copy it into environment
get_symbols("XOM", env_out=etf_env, start_date="1990-01-01")
# plot
x11()
quantmod::chart_Series(x=rutils::etf_env$XOM["2016/"], TA="add_Vo()", name="XOM stock")

Extract the name of an OHLC time series from its first column name:

suppressMessages(suppressWarnings(library(rutils)))
# get name for VTI
get_name(colnames(rutils::etf_env$VTI)[1])
# get first six rows of VTI prices
head(rutils::etf_env$VTI)
# get first six rows of price_s
rutils::etf_env$price_s[1:6, 1:4]
# get first six rows of re_turns
rutils::etf_env$re_turns[1:6, 1:4]

Calculate a vector of equally spaced end points for a time series:

# calculate end points with initial stub interval
calc_endpoints(rutils::etf_env$VTI, inter_val=7)

Extract columns of prices from an OHLC time series:

# get close prices for VTI
get_col(rutils::etf_env$VTI)
# get volumes for VTI
get_col(rutils::etf_env$VTI, col_name="vol")

Apply a lag to a vector or matrix:

# lag vector by 2 periods
lag_it(1:10, lag=2)
# lag matrix by negative 2 periods
lag_it(matrix(1:10, ncol=2), lag=-2)

Calculate the row differences of a vector or matrix:

# diff vector by 2 periods
diff_it(1:10, lag=2)
# diff matrix by negative 2 periods
diff_it(matrix(1:10, ncol=2), lag=-2)

Calculate the time differences of an xts time series and pad with zeros:

# calculate time differences over lag by 10 periods
diff_xts(rutils::etf_env$VTI, lag=10)

Recursively rbind a list of objects:

# create xts time series
x_ts <- xts(x=rnorm(1000), order.by=(Sys.time()-3600*(1:1000)))
# split time series into daily list
list_xts <- split(x_ts, "days")
# rbind the list back into a time series and compare with the original
identical(x_ts, rutils::do_call_rbind(list_xts))

Recursively apply a function to a list of objects:

# create xts time series
x_ts <- xts(x=rnorm(1000), order.by=(Sys.time()-3600*(1:1000)))
# split time series into daily list
list_xts <- split(x_ts, "days")
# rbind the list back into a time series and compare with the original
identical(x_ts, rutils::do_call(rbind, list_xts))

Apply a function to a list of objects, merge the outputs into a single object, and assign the object to the output environment:

rutils::do_call_assign(
   func_tion=get_col,
   sym_bols=rutils::etf_env$sym_bols,
   out_put="price_s",
   env_in=etf_env, env_out=new_env)

========

Plotting

Plot an xts time series with custom y-axis range and with vertical background shading (using package quantmod):

suppressMessages(suppressWarnings(library(rutils)))
chart_xts(rutils::etf_env$VTI["2015-11"], x_11=FALSE,
          name="VTI in Nov 2015", ylim=c(102, 108),
          in_dic=index(rutils::etf_env$VTI["2015-11"]) > as.Date("2015-11-18"))

Plot a candlestick plot for an OHLC time series, with vertical background shading (using package quantmod):

suppressMessages(suppressWarnings(library(rutils)))
# select VTI
oh_lc <- rutils::etf_env$VTI
# calculate volume-weighted average price
v_wap <- rutils::roll_sum(x_ts=oh_lc[, 4]*oh_lc[, 5], look_back=22)
volume_rolling <- rutils::roll_sum(x_ts=oh_lc[, 5], look_back=22)
v_wap <- v_wap/volume_rolling
v_wap[is.na(v_wap)] <- 0
# plot candlesticks with vertical background shading and trading volume.
rutils::chart_xts(oh_lc["2016"], x_11=FALSE,
                  name="VTI plus VWAP",
                  TA="add_Vo(); add_TA(v_wap['2016'], col='red', lwd=2, on=1)",
                  in_dic=(oh_lc["2016", 4] > v_wap["2016"]))

Plot two xts time series with two y-axes in an x11 window (using package zoo):

suppressMessages(suppressWarnings(library(rutils)))
rutils::chart_xts2y(cbind(quantmod::Cl(rutils::etf_env$VTI), 
                          quantmod::Cl(rutils::etf_env$IEF))["2016"], 
                    x_11=FALSE)

Plot an interactive dygraphs candlestick plot with background shading (using package dygraphs):

suppressMessages(suppressWarnings(library(rutils)))
oh_lc <- rutils::etf_env$VTI
v_wap <- TTR::VWAP(price=quantmod::Cl(oh_lc), volume=quantmod::Vo(oh_lc), n=20)
oh_lc <- cbind(oh_lc[, c(1:4)], v_wap)["2016"]
rutils::chart_dygraph(oh_lc, in_dic=(oh_lc[, 4] > v_wap))


algoquant/rutils documentation built on April 18, 2024, 12:05 a.m.