Skip to content

Commit

Permalink
Yahoo dividends are no longer split adjusted
Browse files Browse the repository at this point in the history
Yahoo Finance used to return split-adjusted dividends. Now they return
the raw dividend data. Update getDividends() to adjust the raw dividend
data for splits by default, and do nothing if 'split.adjust = FALSE'.

Thanks to Douglas Barnard for the detailed report!

Fixes #253.
  • Loading branch information
joshuaulrich committed Nov 5, 2018
1 parent 3451f11 commit 8321ee8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/getDividends.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function(Symbol,from='1970-01-01',to=Sys.Date(),env=parent.frame(),src='yahoo',
fr <- xts(fr[,2],as.Date(fr[,1]))
colnames(fr) <- paste(Symbol.name,'div',sep='.')

# dividends from Yahoo are split-adjusted; need to un-adjust
if(src[1] == "yahoo" && !split.adjust) {
# dividends from Yahoo are not split-adjusted
if(src[1] == "yahoo" && split.adjust) {
splits <- getSplits(Symbol.name, from="1900-01-01")
if(is.xts(splits) && is.xts(fr) && nrow(splits) > 0 && nrow(fr) > 0) {
fr <- fr / adjRatios(splits=merge(splits, index(fr)))[,1]
fr <- fr * adjRatios(splits=merge(splits, index(fr)))[,1]
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/test_getDividends.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library(quantmod)

# split-adjusted by default
cf.div.adj <- as.numeric(getDividends("CF")["2015"])
stopifnot(isTRUE(all.equal(cf.div.adj, rep(0.3, 4))))
cf.div.raw <- as.numeric(getDividends("CF", split.adjust = FALSE)["2015"])
stopifnot(isTRUE(all.equal(cf.div.raw, c(1.5, 1.5, 0.3, 0.3))))

0 comments on commit 8321ee8

Please sign in to comment.