#' 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
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.