R/daily.R

Defines functions daily_returns daily_prices daily_cl daily_ad

Documented in daily_ad daily_cl daily_prices daily_returns

#' Computes daily periodicity of adjusted close prices.
#' Price history must include adjusted prices typically as
#' found from Quandl and provided by the \code{quantmod} package.
#' @param x an XTS price history with adjusted column
#' @return an XTS price history in daily periodicity,
#' returning adjusted column only
#' @seealso quantmod::Ad, daily_prices
daily_ad <- function(x){
  sym <- sub("\\..*$", "", names(x)[1])
  # Ad(to.daily(x, indexAt = 'lastof', drop.time = TRUE, name = sym))
  x <- quantmod::Ad(x)
  colnames(x) <- sym
  x
}

#' Computes daily periodicity of close prices.
#' Price history must include adjusted prices typically as
#' found from Quandl and provided by the \code{quantmod} package.
#' @param x an XTS price history with close column
#' @return an XTS price history in daily periodicity,
#' returning close column only
#' @seealso quantmod::Cl, monthly_prices
daily_cl <- function(x){
  sym <- sub("\\..*$", "", names(x)[1])
  x <- quantmod::Cl(x)
  colnames(x) <- sym
  x
}

#' Given a list of ticker symbols, retrieve the symbol's
#' price history from the given environment and compute
#' daily close prices for each, returning an XTS
#' object containing columns for each symbol.
#' @param symbols the list of ticker symbols
#' @param env the environment containing price histories,
#' default \code{.GlobalEnv}
#' @return merged daily close prices for each symbol
#' @seealso daily_cl
daily_prices <- function(symbols, env = .GlobalEnv) {
  list.sym <- list()
  for(i in 1:length(symbols)) {
    list.sym[[symbols[i]]] <- get(symbols[i], envir = env)
  }

  do.call(merge, lapply(list.sym, daily_cl))
}

#' Given a list of ticker symbols, retrieve the symbol's
#' price history from the given environment and compute
#' the daily close price return for each symbol.
#' Returns an XTS object containing columns for each symbol.
#' @param symbols the list of ticker symbols
#' @param env the environment containing price histories,
#' default \code{.GlobalEnv}
#' @param n_roc the number of periods for return lookback,
#' default 1
#' @return merged close monthly price returns for each symbol
#' @seealso TTR::ROC
daily_returns <- function(symbols, n_roc = 1, env = .GlobalEnv) {
  TTR::ROC(x = daily_prices(symbols, env = env), n = n_roc, type = "discrete", na.pad = TRUE)
}
greatgray/scorecard documentation built on Feb. 14, 2018, 4:28 a.m.