Importing and Managing Financial Data in R
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
getSymbols("MSFT", from = "2004-07-01", to = "2004-12-31")
"MSFT"
# Adjust data for splits and dividends
msft_adjusted <- adjustOHLC(MSFT)
# Object name is not ticker symbol
my_data <- MSFT
# Use symbol.name argument
my_data_adjusted <- adjustOHLC(my_data, symbol.name = "MSFT")
# Download split data from Yahoo Finance
splits <- getSplits("GE")
head(splits, n = 4)
GE.spl
1971-06-08 0.5
1983-06-02 0.5
1987-05-26 0.5
1994-05-16 0.5
# Download split-adjusted dividend data from Yahoo Finance
dividends <- getDividends("GE")
head(dividends, n = 4)
GE.div?1970-03-03 0.00677
1970-06-11 0.00677
1970-09-21 0.00677
1970-12-07 0.00677
# Download unadjusted dividend data from Yahoo Finance
dividends_raw <- getDividends("GE", split.adjust = FALSE)
# Compare adjusted and unadjusted dividends
head(merge(dividends, dividends_raw))
GE.div GE.div.1
1970-03-03 0.00677 0.64992
1970-06-11 0.00677 0.64992
1970-09-21 0.00677 0.64992
1970-12-07 0.00677 0.64992
1971-03-03 0.00677 0.64992
1971-06-17 0.00729 0.34992
splits
dividends
close
xts
object with 2 columns: Split
and DivgetSymbols("GE", from = "2000-01-01")
"GE"
close <- Cl(GE)
splits <- getSplits("GE")
dividends_raw <- getDividends("GE", split.adjust = FALSE)
# Pass splits, unadjusted dividends, and unadjusted close
ratios <- adjRatios(splits = splits,
dividends = dividends_raw,
close = close)
# Multiply unadjusted close by split and dividend ratios
close_adjusted <- close * ratios[, "Split"] * ratios[, "Div"]
head(merge(close, close_adjusted, Ad(GE)), n = 4)
GE.Close GE.Close.1 GE.Adjusted
2000-01-03 150.0000 29.50422 29.44630
2000-01-04 144.0000 28.32405 28.26845
2000-01-05 143.7500 28.27488 28.21937
2000-01-06 145.6718 28.65289 28.59664
Importing and Managing Financial Data in R