Closed Courvoisier13 closed 11 months ago
Could you please provide a minimal example that only uses quantmod? I'm not familiar with any of the packages you've used, and I'd rather not have to figure out how they're using quantmod behind the scenes.
Is the below better? I am only using tidyverse
and dropped tidyquant
(wrapper around quantmod
). If yes, I can edit the question and replace the reproducible example.
library(tidyverse)
library(quantmod)
assets = c('RSPT', 'QQQ', 'RSPS')
enddate = today(tzone = "EST")
startdate = enddate - years(5)
getSymbols(Symbols = assets)
RSPT = dailyReturn(RSPT$RSPT.Adjusted)
QQQ = dailyReturn(QQQ$QQQ.Adjusted)
RSPS = dailyReturn(RSPS$RSPS.Adjusted)
RSPT = as.data.frame(RSPT) %>% rownames_to_column(var = "date") %>% select(date, daily.returns) %>% mutate(ticker = "RSPT")
QQQ = as.data.frame(QQQ) %>% rownames_to_column(var = "date") %>% select(date, daily.returns) %>% mutate(ticker = "QQQ")
RSPS = as.data.frame(RSPS) %>% rownames_to_column(var = "date") %>% select(date, daily.returns) %>% mutate(ticker = "RSPS")
prices_yahoo = RSPT %>% bind_rows(QQQ) %>% bind_rows(RSPS)
prices_yahoo = prices_yahoo %>% mutate(date = as.Date(date))
cumprod_ignore_na <- function(x) {
y = cumprod(replace(x, is.na(x), 1)) ; y[is.na(x)] <- NA
return(y)
}
prices_yahoo = prices_yahoo %>%
group_by(ticker) %>%
mutate(perf = cumprod_ignore_na(1+daily.returns))
prices_yahoo %>% ggplot(aes(date, perf, color = ticker)) + geom_line() + ggtitle("yahoo")
prices_yahoo %>%
filter(date>startdate) %>%
group_by(ticker) %>%
mutate(perf = cumprod_ignore_na(1+daily.returns)) %>%
ggplot(aes(date, perf, color = ticker)) + geom_line() + ggtitle("yahoo")
Thanks for the simpler example. tidyverse
and ggplot2
include ~40 other packages and attach 9. Attaching tidyverse
also attaches dplyr
, and that breaks how the base R lag()
function works. Issues like that are why I ask for minimal examples.
Here's what I did (for future me, not my expectation from you)
library(quantmod)
assets <- c('RSPT', 'QQQ', 'RSPS')
getSymbols(assets, from = "2018-08-01", to = "2023-08-01", env = (e <- new.env()))
p <- do.call(merge, lapply(e, Cl))
wp <- cumprod(1+ROC(p, 1, "discrete")[-1])
plot(wp, main = "wealth index from close prices")
addLegend("topleft", lty = 1)
a <- do.call(merge, lapply(e, Ad))
wa <- cumprod(1+ROC(a, 1, "discrete")[-1])
plot(wa, main = "wealth index from adjusted prices")
addLegend("topleft", lty = 1)
So the charts from the websites are using the plain close prices, while your code uses the adjusted close prices. The adjusted prices give you a more accurate result based on what you'd actually receive if you had owned those securities over the time horizon.
I'm going to close this because it's not an issue with quantmod. But I'm happy to continue the discussion if you'd like.
thanks @joshuaulrich . the websites are using adjusted. that one I am 100% sure. morningstar even has the option to display growth adjusted or not adjusted. The second graph you show is for sure wrong, do it over 10 years. you see RSPT is up 1100% (vs QQQ 400% - there is no world where the equal weight is that much different from the cap weighted). I think there is an issue with the API endpoint from yahoo. I will do the adjustments myself and see what we get.
just checked. getDividends("RSPT")
returns dividends that are 10x what they should be. seems like a yahoo issue. not quantmod.
there was a split last week. I thin that triggered the problem.
Yeah, they've frequently changed the data they return. There were times where the regular OHLC prices were adjusted for splits but not dividends; times where they weren't adjusted for either; times where the volume wasn't adjusted for splits and/or dividends, etc. It's just a mess.
I use Tiingo @tiingo instead of Yahoo because of this (I'm not compensated for mentioning them). Here's what their adjusted prices show for the past 5 years (2018-01-01/2023-08-01).
library(quantmod)
assets <- c('RSPT', 'QQQ', 'RSPS')
getSymbols(assets, from = "2018-08-01", to = "2023-08-01", src = "tiingo", env = (e <- new.env()))
p <- do.call(merge, lapply(e, Ad))
wp <- cumprod(1+ROC(p, 1, "discrete")[-1])-1
plot(wp, main = "wealth index from Tiingo adjusted close prices")
addLegend("topleft", lty = 1)
dividends that are 10x what they should be
...so they're not adjusting dividends for splits... or doing it wrong.
People have suggested that quantmod should try to return the correct data regardless of what Yahoo returns, but that would be a lot of work for me. And I'd prefer people use a different public data source instead of trying to keep up with Yahoo's changes.
you mean do the adjustments in the package? will investigate if that is practical. pb is as you point what happens when they correct the issue. there does not seem to be a rule. one day they adjust, one day they dont.
Description
The yahoo API is returning to be giving different adjusted data to what is shown on the website?
Here is an example with RSPT, RSPS vs QQQ.
You see here RSPT blowing past QQQ and RSPS even ending at the same point as QQQ.
For comparaison here is what the yahoo website shows:![image](https://github.com/joshuaulrich/quantmod/assets/5888620/b0a6eed4-178f-40b3-b7a1-e5cffe3d8915)
and koyfin:![image](https://github.com/joshuaulrich/quantmod/assets/5888620/485ace05-fdd7-4556-b7da-502c3d7e1ea5)
and morningstar![image](https://github.com/joshuaulrich/quantmod/assets/5888620/7ea6e019-7b12-4aed-b092-045f4f4e81d3)
Any clue on what is going on here? I have the latest version of
quantmod
. I also useyfinance
(just updated to latest version) on python and it's the same issue. Is there an issue with the API?Expected behavior
correct adjusted close prices, similar to the yahoo website.
Minimal, reproducible example
Session Info