Importing and Managing Financial Data in R
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer
setSymbolLookup(AAPL = "google")
aapl <- getSymbols("AAPL", auto.assign = FALSE)
str(aapl) # note the 'src' attribute
An ‘xts’ object on 2007-01-03/2017-02-22 containing:
Data: num [1:2552, 1:5] 12.3 12 12.2 12.3 12.3 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "google"
$ updated: POSIXct[1:1], format: "2017-02-23 14:16:55"
setSymbolLookup(MSFT = list(src = "google", from = "2016-01-01"))
msft <- getSymbols("MSFT", auto.assign = FALSE)
str(msft) # note the 'src' attribute and first date
An ‘xts’ object on 2016-01-04/2017-02-27 containing:
Data: num [1:290, 1:5] 54.3 54.9 54.3 52.7 52.4 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "MSFT.Open" "MSFT.High" "MSFT.Low" "MSFT.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "google"
$ updated: POSIXct[1:1], format: "2017-02-23 14:20:21"
# Set default
setSymbolLookup(AAPL = "google")
# Verify the default changed
getSymbolLookup()
$AAPL
$AAPL$src
"google"
# Save lookup
saveSymbolLookup("symbol_lookup.rda")
# Remove lookup
setSymbolLookup(AAPL = NULL)
# Verify the default is removed
getSymbolLookup()
named list()
# Load lookup
loadSymbolLookup("symbol_lookup.rda")
# Verify the default is restored
getSymbolLookup()
$AAPL
$AAPL$src
"google"
Importing and Managing Financial Data in R