[R-SIG-Finance] GET LOG RETURNS FUNCTION

AIE ATUMA gttg@2000 @end|ng |rom y@hoo@com
Sat Sep 26 23:16:56 CEST 2020


Dear All,

Kindly help to resolve the below.

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 %>%
as_tibble() %>%
rownames_to_column(var = "Date") %>%
mutate(Date = ymd(Date))
} else {
log_returns <- log_returns_xts
}
log_returns
}
"MA" %>%
get_stock_prices(return_format = 'tibble')%>%
get_log_returns(return_format = "tibble")%>%head()


ERROR MESSAGE:

Error in to_period(xx, period = on.opts[[period]], ...) : 
  unsupported type
Called from: to_period(xx, period = on.opts[[period]], ...)


Thank You and Best Regards, 
Emeka I. Atuma
Integrity - Walk Your Talk Don't Talk Your Work



More information about the R-SIG-Finance mailing list