更新 ahmohamed的解决方案
(lapply(quote_data,is.null))
,因此您可以像我们通过list_in <- list_in(-which(lapply(quote_data,is.null)))
结果
Warning message:
In download.file(url, destfile, method = method, quiet = quiet) :
only first element of 'url' argument used
,其中
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
price <- try(get.hist.quote(instrument=ticker,quiet=TRUE, start='2013-01-01',end=Sys.Date()))
if(class(price) == "try-error") #if error occurred
price <- NULL
return(price)
}
这个简单的功能,使用getPrice()从yahoo下载股票价格,进行测试并过滤合适的数据集并将名称保存在矩阵中。如果出现错误,我会使用tryCatch。结果显示为底部。我的问题是,如何跳过错误并继续完成循环,或者至少将数据集“结果”切断?
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
tryCatch({price<-get.hist.quote(instrument=ticker,start='2013-01-01',end='2015-02-14',quiet=TRUE)})
return(price)
}
然后进行ADF测试,如果数据完好则返回测试结果,否则返回NULL。
tts<-function(s1,s2){
require(fUnitRoots)
a<-getPrice(s1)[,4]
b<-getPrice(s2)[,4]
if (length(a)==length(b)){
fit<-lm(a~b+0)
result<-suppressWarnings(adfTest(fit$residuals,type='nc'))
return(result)
}
else return(NULL)
}
这是循环函数
screenStock<-function(list_in){
result<-data.frame()
n<-0
for (i in 1:(length(list_in)-1)){
s1<-getPrice(list_in[i])
for (j in 2:length(list_in)){
s2<-getPrice(list_in[j])
adf_T<-tts(s1,s2)
if (!is.null(adf_T) && adf_T@test$p.value==0.01){
n<-n+1
result[n,1]<-list_in[i]
result[n,2]<-list_in[j]
result[n,3]<-adf_T@test$statistic}
}
}
return(result)
}
嗯,结果并不顺利。
> screenStock(HSlist)
download error, retrying ...
download error, retrying ...
download error, retrying ...
download error, retrying ...
Error in get.hist.quote(instrument = ticker, start = "2013-01-01", end = "2015-02-14", :
cannot open URL 'http://chart.yahoo.com/table.csv?s=c("000001", "000002", "000004", "000005", "000006", "000007", "000008", "000009", "000010", "000011", "000012", "000014", "000016", "000017", "000018", "000019", "000020", "000021", "000022", "000023", "000024", "000025", "000026", "000027", "000028", "000029", "000030", "000031", "000032", "000033", "000034", "000035", "000036", "000037", "000039", "000040", "000042", "000043", "000045", "000046", "000048", "000049", "000050", "000055", "000056", "000058", "000059", "000060", "000061", "000062",
"000063", "000065", "000066", "000068", "000655", "000656", "000659", "000661", "000662", "000663", "000665", "000666", "000667", "000668", "000669", "000671", "000673", "000676", "000677", "000678", "000679", "000680", "000682", "000683", "000685", "000686", "000687", "000690", "000691", "000692", "000695", "000697", "000698", "000700", "000701", "000702", "000703", "000705", "000707", "000708", "000709", "000710", "000711", "0007
In addition: Warning messages:
1: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
2: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
3: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
4: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
5: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
答案 0 :(得分:0)
首先,您需要检查下载过程中是否发生错误。您可以在 getPrice 函数中完成此操作。
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
price <- try(get.hist.quote(instrument=ticker,start='2013-01-01',end='2015-02-14',quiet=TRUE))
if(class(price) == "try-error") #if error occurred
price <- NULL
return(price)
}
现在, getPrice 将为错误返回NULL。然后,您可以检查 t 中 s1 或 s2 是否为空
if (!is.null(s1) && !is.null(s2) && (length(a)==length(b))){
# Do your test
}
注意: 我没有 get_hist_quote 功能的经验,但是如果它每次调用都会下载数据,那么你应该调用 getPrice 函数对列表中的所有代码执行一次,并将数据存储在变量中供以后使用。例如,
screenStock<-function(list_in){
quote_data <- lapply(list_in, getPrice)
filtered_quote_data <- quote_data[-which(lapply(quote_data,is.null))]
for (i in 1:(length(filtered_quote_data)-1)){
s1<-filtered_quote_data[i]
for (j in 2:length(filtered_quote_data)){
s2<-filtered_quote_data[j]
# Continue your code.