GET LOG RETURNS FUNCTION

Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

GET LOG RETURNS FUNCTION

Rmetrics mailing list
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

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Please I need Help on the functions below

Rmetrics mailing list
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]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.