R/make_closes_xts.R

Defines functions make_closes_xts

Documented in make_closes_xts

#' @title make_closes_xts
#' @description You probably don't need to use this.
#' @param asset_symbols character, a string of asset_symbols
#' @param n integer, passed to `utils::tail()`, Default: 430
#' @param ret_periodicity character, the return periodicity which is passed to `xts::endpoints()`, Default: 'days'
#' @note See help for `xts::endpoints()`.
#' @return An xts of closes.
#' @export 
#' @importFrom utils tail
#' @importFrom xts merge.xts
#' @importFrom zoo na.approx
#' @importFrom xts endpoints
make_closes_xts <- function(asset_symbols, n = 430, ret_periodicity = 'days')
{
  lapply(asset_symbols, function(x) {
    readRDS(file.path('prices', x)) %>% utils::tail(n) %>% .[,4]
  }) %>%
    do.call(what = xts::merge.xts) %>%
    zoo::na.approx(na.rm = FALSE) %>%
    .[xts::endpoints(., on = ret_periodicity)] %>%
    `names<-`(asset_symbols)
}
causality-loop/clhelpers documentation built on Aug. 31, 2022, 3:39 a.m.