R quantmod:getFinancials

泪湿孤枕 提交于 2019-12-22 09:56:00

问题


I'm trying to import the financials statements of all the companies listed on the NYSE whose market cap is in greater than the first quartile of the sample. Here is my code :

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)

I've removed the following stocks c("IHG","AF","BAP","BBD","BBDO")because quantmoddoes not manage to import the financials statements: I've got an error message like that :

Error in thead[x]:thead[x + 1] : NA/NaN argument
In addition: There were 39 warnings (use warnings() to see them)

and here is what I get when using the warnings() function:

Warning messages (I've got 39 errors messages of this type):

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'

I've found the problematic stocks step by step. What I would like to do is automatically get rid of all the stocks whose financials statements are not available. Any idea?


回答1:


You can put the call to getFinancials between tryCatch. Here an example:

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/r-quantmodgetfinancials

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!