【问题标题】:Loading in list of xts to be used in Quantmod加载要在 Quantmod 中使用的 xts 列表
【发布时间】:2018-07-18 12:11:54
【问题描述】:

所以我有大量 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/

【问题讨论】:

    标签: r list xts quantmod


    【解决方案1】:

    我不希望这段代码可以工作:

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

    ?getSymbols.RData 表示Symbols(第一个参数)应该是“指定要加载的每个符号名称的字符向量”。您没有名为 "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
    

    【讨论】:

    • 非常感谢您所做的正是我需要的!我会更新我的问题并提供更详细的答案。
    【解决方案2】:

    我不确定 RData 包含哪些符号,但是我知道您可以使用 lapply 和 getsymbols 生成许多 xts 的列表。这是我使用谷歌获取当前股票数据的示例。

      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"
    

    【讨论】:

    • 我知道你可以创建一个 xts 对象列表,我想做的是调用 getSymbols 从 Rdata 文件中获取它。我的最后一行代码耗时太长,迫使我重置我的 Rsession
    • 为此,我会将其分解为 100 或更小的部分,而不是之后的组合。我不知道如何调整 R 上的内存使用或文件大小默认值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2014-12-25
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多