我正在尝试导入所有在纽约证券交易所上市的公司的财务报表,这些公司的市值超过了样本的第一个四分之一。下面是我的代码:
require(TTR)
require(quantmod)
data.init="2013/01/01"
start.date <- as.numeric(gsub("/", "",data.init))
nyse.symbols <- stockSymbols("NYSE")[,-c(3,5)]
nyse.symbols <- na.omit(nyse.symbols[which(nyse.symbols[,"MarketCap"]>0),])
######## Selection Criteria
# Filter 1 : stock mkt cap > 1st quartile --> remove the less liquid stocks
mktcap.filter <- quantile(nyse.symbols[,"MarketCap"],0.25)
nyse.symbols <- nyse.symbols[which(nyse.symbols[,"MarketCap"]>mktcap.filter),]
# Filter 2 :
nyse.fs <- new.env()
tickers.fs <- nyse.symbols[,1]
tickers.fs <- tickers.fs[- match(c("IHG","AF","BAP","BBD","BBDO"),tickers.fs)]
lapply(tickers.fs, getFinancials,env=nyse.fs)我删除了以下股票c("IHG","AF","BAP","BBD","BBDO"),因为quantmod无法导入财务报表:我收到了类似的错误消息:
Error in thead[x]:thead[x + 1] : NA/NaN argument
In addition: There were 39 warnings (use warnings() to see them)下面是我使用warnings()函数时得到的结果:
警告消息(我收到了39条此类型的错误消息):
1: In readLines(tmp) :
incomplete final line found on '/var/folders/9q/pwdpb5nj7bb8jjc_kb3np__h0000gn/T//RtmpeUS9Uh/file7de4698fa5b'
2: In readLines(tmp) :
incomplete final line found on '/var/folders/9q/pwdpb5nj7bb8jjc_kb3np__h0000gn/T//RtmpeUS9Uh/file7de655c9092'
3: In readLines(tmp) :
incomplete final line found on '/var/folders/9q/pwdpb5nj7bb8jjc_kb3np__h0000gn/T//RtmpeUS9Uh/file7de2017953b'我一步一步地找到了有问题的股票。我想要做的是自动摆脱所有财务报表不可用的股票。有什么想法吗?
发布于 2013-02-24 05:38:57
您可以将对getFinancials的调用置于tryCatch之间。下面是一个示例:
options(warn=-1) ## optional to not get horrible quantlib messages!
## here I choose 2 goods symbols and 2 bad symbols
ll <- lapply(c("AF","IHG","BAP",ny.se[1,1]), function(x)
tryCatch(getFinancials(x,env=nyse.fs),
error=function(e){print(paste(x,'not found'));NA}))
### "AF not found"
### "BAP not found"
options(warn=0)
## I apply to remove NA
rapply(ll,na.omit)
"IHG.f" "A.f" https://stackoverflow.com/questions/15043728
复制相似问题