加载要在 Quantmod 中使用的 xts 列表

Loading in list of xts to be used in Quantmod

所以我有大量 xts 对象列表,它是 900 多个名称的 OHLC 数据。它最初出现在一个数据框中,再加上我不需要的其他东西。我现在想使用 getsymbols 在 quantmod 中使用它并将它加载到我的环境中,但是它花费的时间太长了。任何人都知道这样做的更有效方法或我可能做错了什么? getSymbols 可以处理 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

我尝试将其转换为这种格式的原因是为了复制 Ross Bennett 的动量代码。见下文

https://rbresearch.wordpress.com/2012/10/20/momentum-in-r-part-2/

我不确定 RData 包含哪些符号,但我知道您可以使用 lapply 和 getsymbols 生成许多 xts 的列表。这是我使用 google 作为当前库存数据的示例。

  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)

导致:

  > 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"

我不希望这段代码有效:

getSymbols("list_of_xts", src = "RData", auto.assign = TRUE)

?getSymbols.RData 表示 Symbols(第一个参数)应该是 "a character vector specifying the names of each symbol to be loaded"。您没有名为 "list_of_xts.RData".

的符号和文件

此外,getSymbols.RData() 期望每个符号都在其自己的文件中,因此您必须将列表中的每个 xts 对象写入一个单独的文件。

# 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