3

我正在尝试导入在纽约证券交易所上市的所有市值超过样本前四分之一的公司的财务报表。这是我的代码:

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'

我一步一步发现了有问题的股票。我想做的是自动摆脱所有财务报表不可用的股票。任何想法?

4

1 回答 1

3

您可以将呼叫置于getFinancialsbetween 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"  
于 2013-02-23T21:38:57.033 回答