Reputation: 335
So I have large list of xts objects which is OHLC data for 900 plus names. It orginally came in a dataframe coupled with other stuff I didn't need. I now want to use it in quantmod using getsymbols and load it into my environment however it is taking far too long. Anyone know a more efficient way of doing this or what I might be doing wrong? Can getSymbols handle a list of xts?
load(file = "biglistofdataframes.Rdata")
### Convert the list of dataframes to xts
list_of_xts <- lapply(listofdataframes,function(x) xts(x[,2:6],x$date))
####change column names to match quantmod
list_of_xts <- lapply(list_of_xts, setNames,nm = c("Open","High","Low","Close","Volume"))
####Save to Rdatafile
save(list_of_xts, file="1.RData")
#First I clear the environment then I load the data back into the environment
load("1.RData")
##
getSymbols("list_of_xts", src="RData", auto.assign=TRUE)#this craps out on me
The reason I am trying to get it into this format is so that I can replicate Ross Bennett's momentum code. See below
https://rbresearch.wordpress.com/2012/10/20/momentum-in-r-part-2/
Upvotes: 0
Views: 323
Reputation: 176648
I wouldn't expect this code to work:
getSymbols("list_of_xts", src = "RData", auto.assign = TRUE)
?getSymbols.RData
says that Symbols
(the first argument) should be "a character vector specifying the names of each symbol to be loaded". You don't have a symbol and file named "list_of_xts.RData"
.
Also, getSymbols.RData()
expects each symbol to be in its own file, so you would have to write each xts object in your list to a separate file.
# Get some data
env_of_xts <- new.env()
getSymbols(symbols, env=env_of_xts)
# Write it to a temporary directory
tdir <- tempdir()
for (nm in names(env_of_xts)) {
save(list = nm, file = file.path(tdir, paste0(nm, ".RData")), envir = env_of_xts)
}
# Now you can use getSymbols() to load from file
getSymbols(symbols[1], src = "RData", dir = tdir, extension = "RData")
# [1] "AAPL"
head(AAPL)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
# 2007-01-03 12.32714 12.36857 11.70000 11.97143 309579900 8.137179
# 2007-01-04 12.00714 12.27857 11.97429 12.23714 211815100 8.317789
# 2007-01-05 12.25286 12.31428 12.05714 12.15000 208685400 8.258555
# 2007-01-08 12.28000 12.36143 12.18286 12.21000 199276700 8.299341
# 2007-01-09 12.35000 13.28286 12.16429 13.22429 837324600 8.988768
# 2007-01-10 13.53571 13.97143 13.35000 13.85714 738220000 9.418928
Upvotes: 1
Reputation: 335
i am unsure about what symbols RData contains, I do know however that you can generate a list of many xts with lapply and getsymbols. Here is my example using google for current stock data.
symbols<-c("AAPL", "AMZN","GOOGL", "F", "GM", "IBM", "ORCL")
List_of_xts<-lapply(symbols, function(sym){
List_of_Xts<-getSymbols(Symbols = sym, src = "google", auto.assign = FALSE)
})
str(List_of_xts)
resulting in:
> str(List_of_xts)
List of 7
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 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: "2018-02-07 21:48:05"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 38.7 38.6 38.7 38.2 37.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AMZN.Open" "AMZN.High" "AMZN.Low" "AMZN.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:06"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 233 235 241 244 243 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GOOGL.Open" "GOOGL.High" "GOOGL.Low" "GOOGL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:07"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 7.56 7.56 7.72 7.63 7.75 7.79 7.73 7.77 7.89 7.97 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "F.Open" "F.High" "F.Low" "F.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2010-11-18/2018-02-07 containing:
Data: num [1:1817, 1:5] 35 34.1 34.2 34 33.7 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GM.Open" "GM.High" "GM.Low" "GM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 97.2 97.2 97.6 98.5 99.1 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:09"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 17.2 17.6 17.6 17.6 17.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "ORCL.Open" "ORCL.High" "ORCL.Low" "ORCL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:10"> str(List_of_xts)
List of 7
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 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: "2018-02-07 21:48:05"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 38.7 38.6 38.7 38.2 37.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AMZN.Open" "AMZN.High" "AMZN.Low" "AMZN.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:06"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 233 235 241 244 243 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GOOGL.Open" "GOOGL.High" "GOOGL.Low" "GOOGL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:07"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 7.56 7.56 7.72 7.63 7.75 7.79 7.73 7.77 7.89 7.97 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "F.Open" "F.High" "F.Low" "F.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2010-11-18/2018-02-07 containing:
Data: num [1:1817, 1:5] 35 34.1 34.2 34 33.7 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GM.Open" "GM.High" "GM.Low" "GM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 97.2 97.2 97.6 98.5 99.1 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:09"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 17.2 17.6 17.6 17.6 17.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "ORCL.Open" "ORCL.High" "ORCL.Low" "ORCL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:10"
Upvotes: 0