Adjusting for corporate actions

Importing and Managing Financial Data in R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

Adjust for stock splits and dividends (1)

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")
Importing and Managing Financial Data in R

Adjust for stock splits and dividends (2)

Importing and Managing Financial Data in R
# 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
Importing and Managing Financial Data in R

Download unadjusted dividends

# 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
Importing and Managing Financial Data in R

adjRatios()

  • Back-adjust any series for splits, dividends, or both
  • Has 3 arguments:
    • splits
    • dividends
    • close
  • Returns xts object with 2 columns: Split and Div
Importing and Managing Financial Data in R

Adjust univariate series for splits and dividends

getSymbols("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)
Importing and Managing Financial Data in R

Adjust univariate series for splits and dividends

# 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

Let's practice!

Importing and Managing Financial Data in R

Preparing Video For Download...