我正在尝试导入在纽约证券交易所上市的所有公司的财务报表,这些公司的市值大于样本的第一个四分位数。
这是我的代码:
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'
我已逐步发现有问题的股票。我想做的是自动清除所有财务报表不可用的股票。
任何想法?
最佳答案
您可以在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"
关于r - RQuantmod:getFinancials,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15043728/