我是R,loops和quantmod的新手。我試圖說服quantmod跳過它無法處理的任何股票,並繼續到下一個股票代碼,而不是停止。我想我找到了我的答案在這裏how do I loop through all the stocks with quantmod and ttr?但我不能夠得到霧凇的解決方案的工作:使用quantmod循環
If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following
# Actual loop:
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) {
symbols[i]-> symbol
...
下面是我的原代碼,只返回多個值的8(所以我假設9是麻煩點)。
library(gdata)
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F)
library(quantmod)
sym <- as.character(d[,1])
results <- NULL
for (ii in sym){
data1 <- getSymbols(Symbols = ii,
src = "yahoo",
from = Sys.Date() - 100,
auto.assign = FALSE)
de = head(data1,150)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
results <- rbind(results,cbind(
paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
}
colnames(results) <- c("overnightRtn2")
rownames(results) <- sym
View(results)
當我改變for(ii in sym)
到for(ii in 9:length(sym))
我得到一個錯誤:
could not find function "getSymbols.9"
這裏是d[,1]
開始:
[1] "ABX" "ACC" "ACCO" "ACE" "ACG" "ACH" "ACI" "ACM" "ACMP" "ACN"
使用'try'比'tryCatch'簡單得多。 –
@JoshuaUlrich當我只使用'try'時它停止了,可能我做錯了什麼。 – Mutador
'data1 < - try(getSymbols(...));如果(繼承(data1,「嘗試錯誤」))下一個 –