[R-SIG-Finance] Please I need Help on the functions below
AIE ATUMA
gttg@2000 @end|ng |rom y@hoo@com
Sun Sep 27 22:42:53 CEST 2020
Dear All,
Please help a newbie.
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 = (Date))
+ } else {
+ log_returns <- log_returns_xts
+ }
+ log_returns
+ }
> "MA" %>%
+ get_stock_prices(return_format = "tibble") %>%
+ get_log_returns(return_format = "tibble")
Error in xts(x[, -1], order.by = x$Date) :
'order.by' cannot contain 'NA', 'NaN', or 'Inf'
In addition: Warning message:
All formats failed to parse. No formats found.
sp_500 <- sp_500[1:50,] %>% mutate( stock.prices = map('ticker.symbol', function(.x) get_stock_prices(.x, return_format = "tibble", from = "2015-01-01", to = "2018-01-01") ), log.returns = map(stock.prices, function(.x) get_log_returns(.x, return_format = "tibble")), mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)), sd.log.returns = map_dbl(log.returns, ~ sd(.$Log.Returns)), n.trade.days = map_dbl(stock.prices, nrow) )
Warning: ticker.symbol download failed; trying again.
Error in getSymbols.yahoo(Symbols = "ticker.symbol", env = <environment>, : Unable to import “ticker.symbol”.Send failure: Connection was resetCalled from: getSymbols.yahoo(Symbols = "ticker.symbol", env = <environment>, verbose = FALSE, warnings = TRUE, auto.assign = FALSE, from = "2015-01-01", to = "2018-01-01", .has1sym. = TRUE)
Thank You and Best Regards,
Emeka I. Atuma
Integrity - Walk Your Talk Don't Talk Your Work
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list