Quantmod 将代码保存到循环或 lapply 中的文件 [英] Quantmod save tickers to files in a loop or lapply

查看:51
本文介绍了Quantmod 将代码保存到循环或 lapply 中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个次要的 quantmod 问题;如果有人可以建议对我的代码进行调整,我将非常感激.我不知道编程;也许这就是我错过显而易见的原因.出现问题是因为 getSymbols 将字符串作为输入(例如YHOO"),但仅返回 YHOO(不带引号)作为保存数据的 xts 对象.此外,对于市场指数,雅虎在代码字符串中包含一个脱字符(例如^GSPC"),但 quantmod 返回纯 GSPC 作为数据对象.

I am stuck with a minor quantmod problem; if anyone can suggest a tweak to my code, I’d really appreciate that. I don’t know progamming as such; maybe that’s why I miss the obvious. The problem is arising because getSymbols takes a string as input (e.g. "YHOO"), but returns just YHOO (without quotes) as the xts object which holds the data. Also, for market indices, Yahoo includes a caret in the string for the code (e.g. "^GSPC"), but quantmod returns plain GSPC as the data object.

我正在尝试将多个股票代码的数据下载并保存到单独的二进制文件中.这是为了创建一个工作环境,该环境可以利用存储在磁盘上的数据运行,而不必一定要访问互联网.

I am trying to download and save to individual binary files the data of multiple tickers. This is so as to create a work environment which can function from data stored on disk, instead of requiring internet access necessarily.

我尝试编写函数:

buildhist <- function(x,start,end) {
  getSymbols(x, from=start, to=end, adjust=TRUE)   
  save(get(x), file= paste(x, "hist.rda", sep="_"), ascii = FALSE)  
}

然后使用

require(quantmod)
tckr <- c("YHOO","XLB")
lapply(tckr,buildhist,start="1995-01-01",end="2011-11-30")

但是,它在保存命令时出错(说找不到对象‘get(x)’").如果我不使用 get(x)save 命令只会将股票代码名称保存为字符串,所以我不能使用它.没有其他版本,例如 save(noquote(x), file=paste(x, "hist.rda", sep="_"), ascii=FALSE) 也不起作用.

But, it errors out at the save command (saying "object ‘get(x)’ not found"). If I don’t use get(x), the save command will only save the ticker name as string, so I can’t use that. No other version such as save(noquote(x), file=paste(x, "hist.rda", sep="_"), ascii=FALSE) works either.

我应该使用什么命令,以便使用与 quantmod 最初返回的对象名称相同的对象名称保存股票代码数据?在我上面的代码中,我什至没有尝试解决另一个问题——如果存在的话,从名称中去除插入符号.任何指向此的指针也将不胜感激.

What command should I use so that the ticker data will be saved using the same object name as it is originally returned by quantmod? In my code above I haven’t even tried to tackle the other problem – that of stripping the caret sign from the name if it exists. Any pointers to that would be much appreciated too.

推荐答案

UPDATE:下面的解决方案没有解决 OP 的问题(见评论).查看跳转后的编辑.

UPDATE: The solution below doesn't solve the OP's problem (see comments). See the edit after the jump.

auto.assign=TRUE 的默认值应该可以在交互使用 getSymbols 时使事情变得更容易.在函数中使用 getSymbols 时设置 auto.assign=FALSE;它会让事情变得更容易.

The default of auto.assign=TRUE is supposed to make things easier when using getSymbols interactively. Set auto.assign=FALSE when using getSymbols in a function; it will make things much easier.

buildhist <- function(x,start,end) {
  y <- getSymbols(x, from=start, to=end, adjust=TRUE, auto.assign=FALSE)
  save(y, file= paste(x, "hist.rda", sep="_"), ascii = FALSE)  
}

您可以通过 gsub 删除标点符号(包括插入符号).有关详细信息,请参阅 ?gsub?regex.

You can remove punctuation characters (including the caret) via gsub. See ?gsub and ?regex for details.

X <- gsub("[[:punct:]]","",x)  # remove all punctuation
X <- gsub("\\^","",x)          # remove just the carat

<小时>

我没有测试我的初始答案.此解决方案应该有效.


I didn't test my initial answer. This solution should work.

buildhist <- function(x,start,end) {
  getSymbols(x, from=start, to=end, adjust=TRUE)
  X <- toupper(gsub("\\^","",x))  # what getSymbols.yahoo does
  save(list=X, file= paste(X, "hist.rda", sep="_"), ascii = FALSE)  
}

require(quantmod)
tckr <- c("^GSPC","YHOO","XLB")
lapply(tckr,buildhist,start="1995-01-01",end="2011-11-30")

如果您只使用少量交易品种的每日数据,您可以将它们全部加载到一个环境中,然后保存该环境.那可以为你省去很多麻烦.然后,您可以将环境加载到新会话中,附加它,然后将所有数据放在您的手指上.

If you're only using daily data on a small number of symbols, you may be able to load them all to one environment and just save the environment. That could save you a lot of trouble. Then you could load the environment to a new session, attach it, and have all the data at your fingers.

myEnv <- new.env()
getSymbols(paste(tckr,sep=";"), start="1995-01-01", end="2011-11-30",
  env=myEnv, adjust=TRUE)
save(myEnv, file="myTickerData.rda")

这篇关于Quantmod 将代码保存到循环或 lapply 中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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