R/get_log_returns.R

Defines functions get_log_returns

#' get_log_returns
#' @export get_log_returns
get_log_returns <- function(x, return_format = "tibble", period = 'daily', ...) {
  # Convert tibble to xts
  if (!is.xts(x)) {
    x <- xts(x[,-1], order.by = x$Date)
  }
  # Get log returns
  log_returns_xts <- periodReturn(x = x$Adjusted, type = 'log', period = period, ...)
  # Rename
  names(log_returns_xts) <- "Log.Returns"
  # Return in xts format if tibble is not specified
  if (return_format == "tibble") {
    log_returns <- log_returns_xts %>%
      fortify.zoo() %>%
      as_tibble() %>%
      rename(Date = Index)
  } else {
    log_returns <- log_returns_xts
  }
  log_returns
}
pereira94/stockanalysis documentation built on Feb. 5, 2021, 12:48 a.m.