Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with log returns #16

Closed
msperlin opened this issue Aug 4, 2022 · 0 comments
Closed

issue with log returns #16

msperlin opened this issue Aug 4, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@msperlin
Copy link
Collaborator

msperlin commented Aug 4, 2022

Boa tarde Marcelo, parabéns pelo pacote yfR.
Identifiquei 2 problemas, relacionados ao cálculo da coluna cumret_adjusted_prices.

  1. No caso de log returns teria que ajustar a função (1+cumsum( x) por cumsum(1+x)):

calc_cum <- function(x, type_return) {
this_cum_ret <- switch(type_return, arit = cumprod(1 + x), log = 1+cumsum( x))
return(this_cum_ret)
}

Mesmo feito dessa forma pode acabar com um acumulado negativo (ver ao final exemplo reproduzível)

Pessoalmente, dado que a ideia é mostrar o retorno acumulado de 1 real, seria melhor calcular um índice a partir das cotações ajustadas (x = price_adjusted), válido para ambos tipos de retornos:
calc_cum1 <- function(x) {
this_cum_ret <- x/x[1]
return(this_cum_ret)
}

  1. Quando a lista de símbolos não está ordenada, como você usa split na funação calc_cum_ret, o resultado é ordenado alfabeticamente, organizando os dados de forma diferente, que não necessariamente corresponde ao original. Para superar isso sem precisar maiores alterações sugiro incluir

tickers <- sort(tickers)
ao início da função yf_get

Abraço

StartDat = as.Date("2015-01-01") [#inicio](https://www.linkedin.com/feed/hashtag/?keywords=%23inicio)
EndDat = as.Date("2022-03-25") [#Fim](https://www.linkedin.com/feed/hashtag/?keywords=%23Fim) data

symbols<-c("^BVSP","PETR4.SA","VALE3.SA","LREN3.SA","MGLU3.SA") # s?imbolos no Yahoo
symbols <- sort(symbols)
[#Download](https://www.linkedin.com/feed/hashtag/?keywords=%23Download) dados

df_yf <- yf_get(tickers = symbols,
first_date = StartDat,
last_date = EndDat,
type_return = "log") # arit, log


# correct cum for log
ret=df_yf$ret_adjusted_prices
tickers = df_yf$ticker [#df_yf](https://www.linkedin.com/feed/hashtag/?keywords=%23df_yf)$ticker
unique(tickers)
type_return = "log"

idx <- is.na(ret)
ret[idx] <- 0
l_ret <- split(ret, tickers)
l_cot <- split(df_yf$price_adjusted, tickers)
calc_cum <- function(x, type_return) {
this_cum_ret <- switch(type_return, arit = cumprod(1 + x), log = 1+cumsum( x))
return(this_cum_ret)
}
calc_cum1 <- function(x) {
this_cum_ret <- x/x[1]
return(this_cum_ret)
}
l_cum_ret <- purrr::map(l_ret, calc_cum, type_return = type_return)
l_cum_ret1 <- purrr::map(l_cot, calc_cum1)
names(l_ret)
names(l_cum_ret)
cum_ret <- do.call(c, l_cum_ret)
cum_ret1 <- do.call(c, l_cum_ret1)
names(cum_ret) <- NULL
names(cum_ret1) <- NULL
df_yf$cumret_adjusted_prices = cum_ret
df_yf$cumret_adjusted_prices1 = cum_ret1

plot(df_yf$price_adjusted[df_yf$ticker=="MGLU3.SA"],log = "y")


library(ggplot2)

p <- ggplot(df_yf,
aes(x = ref_date,
y = cumret_adjusted_prices1,
color = ticker)) +
geom_line() +
labs(
title = paste0("Index Value (",
year(min(df_yf$ref_date)), ' - ',
year(max(df_yf$ref_date)), ")"
),
x = "Time",
y = "Accumulated Return (from 100%)",
caption = "Data from Yahoo Finance <https://finance.yahoo.com//>") +
theme_light() + scale_y_log10()

p
@msperlin msperlin self-assigned this Aug 4, 2022
@msperlin msperlin added the bug Something isn't working label Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant