R/DownloadData.R

Defines functions download_fred download_tiingo download_french download_shiller read_research_affiliates

Documented in download_fred download_french download_shiller download_tiingo

#' @title Download time-series from FRED database
#' @param ticker string of time-series to download
#' @param try_mode boolean to use try-error handling
#' @return data.frame contaning dates and economic data
#' @export
#' @details see https://fred.stlouisfed.org for more info
#' @examples
#' indust_prod <- download_fred('INDPRO')
download_fred <- function(ticker, try_mode = TRUE) {

  tmp <- tempfile()
  f_url <- paste0('https://fred.stlouisfed.org/series/',
                  ticker,
                  '/downloaddata/',
                  ticker,
                  '.csv')
  if (try_mode) {
    getdat <- try(download.file(f_url, destfile = tmp))
    if ('try-error' %in% class(getdat)) {
      warning(paste0('could not download ', ticker))
      return(NULL)
    }
  }
  dat <- read.csv(tmp, na.string = '.')
  colnames(dat) <- c('date', ticker)
  return(dat)
}


#' @title Download time-series from Tiingo
#' @param ticker string representing ticker or symbol to download
#' @param tiingo_api string containing your Tiingo API, see details
#' @param try_mode boolean to use try-error handling
#' @return data.frame containing dates and returns
#' @export
#' @importFrom jsonlite read_json
#' @details https://www.tiingo.com/ is a free source for financial time-series,
#' you need to sign up to create an account to get an API. Use your API as a
#' string for the \code{tiingo_api} parameter.
#' @examples
#' tiingo_api <- 'YourTiingoAPI'
#' spy <- download_tiingo('spy', tiingo_api)
download_tiingo <- function(ticker, tiingo_api, try_mode = TRUE) {

  t_url <- paste0('https://api.tiingo.com/tiingo/daily/',
                  ticker,
                  '/prices?startDate=1970-01-01',
                  '&endDate=', Sys.Date(),
                  '&token=', tiingo_api)
  if (try_mode) {
    dat <- try(jsonlite::read_json(t_url))
    if ('try-error' %in% class(dat)) {
      warning(paste0('could not download ', ticker))
      return(NULL)
    } else {
      dat <- jsonlite::read_json(t_url)
    }
  }
  date_raw <- sapply(dat, '[[', 'date')
  date_vec <- as.Date(date_raw)
  price_vec <- sapply(dat, '[[', 'adjClose')
  price <- data.frame(date_vec, price_vec)
  colnames(price) <- c('date', ticker)
  ret <- price_to_ret(price)
  return(ret)
}


#' @title Download from Ken French datalibrary
#' @param data_url subset of url specific to data, see details
#' @param skip how many header rows in csv file to skip
#' @return data.frame with time-series
#' @export
#' @details the base url for all downloads is
#' 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/', each
#' csv download has a unique url to add after the base, e.g., the \code{data_url}
#' for momentum is 'F-F_Momentum_Factor_daily_CSV.zip'
download_french <- function(data_url, skip) {

  tmp <- tempfile()
  base_url <- 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/'
  full_url <- paste0(base_url, data_url)
  download.file(full_url, destfile = tmp)
  uzfile <- unzip(tmp)
  clean_french_ret <- function(x) {
    x <- gsub('  ', '', x)
    x <- gsub(' ', '', x)
    x <- as.numeric(x)
    x[x >= 99.99] <- NA
    x[x <= -99.99] <- NA
    x / 100
  }
  dat <- read.csv(uzfile, skip = skip)
  dat$X <- as.Date(as.character(dat$X), format = '%Y%m%d')
  dat[, 2:ncol(dat)] <- apply(dat[, 2:ncol(dat), drop = FALSE], 2, clean_french_ret)
  check_time_series(dat)
}


#' @title Download data from Bob Shiller's datalibrary
#' @importFrom curl curl_download
#' @export
#' @return data.frame with S&P Data and CAPE ratios
#' @details returns are estimated as (price + dividend) /
#' (price t-1 + dividend t - 1) - 1
download_shiller <- function() {

  tmp <- tempfile()
  s_url <- 'http://www.econ.yale.edu/~shiller/data/ie_data.xls'
  curl::curl_download(s_url, tmp)
  dat <- readxl::read_xls(tmp, sheet = 'Data', skip = 7)
  dt <- formatC(dat$Date, digits = 2, format = 'f')
  yr <- substr(dt, 1, 4)
  mo <- substr(dt, 6, 7)
  dy <- rep('01', length(mo))
  dt <- as.Date(paste(yr, mo, dy, sep = '-'))
  eom <-  lubridate::ceiling_date(dt, unit = 'month') - 1
  dat$Date <- as.Date(eom, origin = '1970-01-01')
  dat$TP <- dat$P + dat$D
  dat <- as.data.frame(dat)
  ret <- price_to_ret(dat[, c('Date', 'TP')])
  dat$Ret <- c(NA, ret$TP)
  return(dat)
}


read_research_affiliates <- function(workbook) {

  exp_ret <- readxl::read_excel(workbook, 2, range = 'B4:L48')
  exp_ret_all <- readxl::read_excel(workbook, 3, range = 'B4:L121')
  hist_ret <- readxl::read_excel(workbook, 4, range = 'B5:L48')
  exp_corr <- readxl::read_excel(workbook, 5, range = 'B4:AC31')
  exp_cov <- readxl::read_excel(workbook, 5, range = 'B35:AC62')
  port_wgt <- readxl::read_excel(workbook, 6, range = 'B4:AC21')
  cape <- readxl::read_excel(workbook, 7, range = 'B4:M36')
  yield <- readxl::read_excel(workbook, 8, range = 'B4:R25')
  growth <- readxl::read_excel(workbook, 9, range = 'B4:E26')
  res <- list()
  res$exp_ret <- exp_ret
  res$exp_ret_all <- exp_ret_all
  res$hist_ret <- hist_ret
  res$exp_corr <- exp_corr
  res$exp_cov <- exp_cov
  res$port_wgt <- port_wgt
  res$cape <- cape
  res$yield <- yield
  res$growth <- growth
  return(res)
}
alejandro-sotolongo/InvestmentSuite documentation built on Jan. 19, 2020, 5:20 p.m.