想用异常处理跳过这些坏的symbol,请问怎么做呢? ps: symbol文件是sp500.txt,已经上传附件。
- library(quantmod)
- library(tseries)
- library(timeDate)
- #get symbols
- symbols <- read.csv("sp500.txt", header = F, stringsAsFactors = F)
- nrStocks = length(symbols[,1])
- funds <- NULL
- dateStart <- "2009-1-1"
- for (i in 1:nrStocks) {
- cat("Downloading ", i, " out of ", nrStocks , "\n")
- prices <- get.hist.quote(instrument = symbols[i,], start = dateStart, quote = "AdjClose", retclass = "zoo", quiet = T)
- # Convert daily closing prices to monthly closing prices
- monthly.prices <- aggregate(prices, as.yearmon, tail, 1);
- # Convert selected monthly prices into monthly returns to run regression
- r <- diff(log(monthly.prices)); # convert prices to log returns
- r1 <- exp(r)-1 ; # back to simple returns
- # Now shift out of zoo object into ordinary matrix
- rj <- coredata(r1)
- # Put fund returns into matrix
- if(length(rj) ==length(funds[,1]) ) {funds <- cbind(funds, rj); }
- Sys.sleep(1);
- }


雷达卡




京公网安备 11010802022788号







