存储 getSymbols 返回的 xts 对象 [英] Storing the xts object returned by getSymbols

查看:73
本文介绍了存储 getSymbols 返回的 xts 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 quantmod 的开盘价和收盘价收集共同基金业绩数据.我已经抓取了一份包含 5000 只基金的清单,并试图遍历并为每个基金获得一个开盘价和收盘价.我很难调用由 getSymbols() 产生的 xts 对象,因为它被无形地调用到环境中.由于该对象存储为其股票代码名称,因此我尝试通过其股票代码名称调用它.

I'm trying to collect mutual fund performance data via open and close prices from quantmod. I have scraped a list of 5000 some funds and am trying to loop through and get one open and close price for each fund. I'm having difficulty calling the xts object yielded by getSymbols() as it is called invisibly into the environment. Since the object is stored as its ticker name, I tried calling it by its ticker name.

到目前为止的代码:

## loop thru list and use quantmod to calculate performance from 1/2/14 to 12/31/14
for(i in 1:4881){
    ticker <- tickernames[i]
    getSymbols(ticker)
    Open <- ticker["2014-01-02",1]
    Close <- ticker["2014-12-31",4]

    performance2014[i] = (Open - Close)/Open
}

有没有办法使用 ls() 调用对象?

Is there a way I can call the object using ls()?

推荐答案

关键是在getSymbols中将auto.assign参数设置为FALSE代码>.这样您就可以停用 getSymbols 对全局环境的自动分配.

The key is to set the auto.assign argument to FALSE in getSymbols. This way you deactivate getSymbols automatic assignment to the global environment.

以下示例可指导您逐步完成:

Here's an example that should guide you through step by step:

require(quantmod)

#Vector of symbols to fetch prices for
symbols <- c('MSFT','SBUX','GOOGL')

#Initialize a list to store the fetched prices
myList <- list()

#Loop through symbols, fetch prices, and store in myList
myList <-lapply(symbols, function(x) {getSymbols(x,auto.assign=FALSE)} )

#Housekeeping
names(myList) <- symbols

#Access MSFT prices
myList[['MSFT']]

#Access SBUX prices
myList[['SBUX']]

#Access GOOGL prices
myList[['GOOGL']]

希望这能回答您的问题.

Hope this answered your question.

这篇关于存储 getSymbols 返回的 xts 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆