搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  sp500.txt
资料下载链接地址: https://bbs.pinggu.org/a-1581949.html
附件大小:
各位大大,本菜想从yahoo finance上面弄一些数据,我有一个文档里面是股票的symbol,但貌似有些symbol不能用,所以循环总会出现问题。
想用异常处理跳过这些坏的symbol,请问怎么做呢? ps: symbol文件是sp500.txt,已经上传附件。
  1. library(quantmod)
  2. library(tseries)
  3. library(timeDate)

  4. #get symbols
  5. symbols <- read.csv("sp500.txt", header = F, stringsAsFactors = F)

  6. nrStocks = length(symbols[,1])
  7. funds <- NULL
  8. dateStart <- "2009-1-1"

  9. for (i in 1:nrStocks) {
  10. cat("Downloading ", i, " out of ", nrStocks , "\n")
  11. prices <- get.hist.quote(instrument = symbols[i,], start = dateStart, quote = "AdjClose", retclass = "zoo", quiet = T)
  12. # Convert daily closing prices to monthly closing prices
  13. monthly.prices <- aggregate(prices, as.yearmon, tail, 1);
  14. # Convert selected monthly prices into monthly returns to run regression
  15. r <- diff(log(monthly.prices));# convert prices to log returns
  16. r1 <- exp(r)-1 ; # back to simple returns
  17. # Now shift out of zoo object into ordinary matrix
  18. rj <- coredata(r1)
  19. # Put fund returns into matrix
  20. if(length(rj) ==length(funds[,1]) ) {funds <- cbind(funds, rj); }
  21. Sys.sleep(1);
  22. }
复制代码




    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2026-1-2 03:27