R quantmod:getFinancials [英] R quantmod:getFinancials

查看:48
本文介绍了R quantmod:getFinancials的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入在纽约证券交易所上市且市值大于样本前四分位数的所有公司的财务报表.这是我的代码:

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)

我删除了以下股票 c("IHG","AF","BAP","BBD","BBDO") 因为 quantmod 没有设法导入财务报表:我收到了这样的错误消息:

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)

这是我在使用 warnings() 函数时得到的结果:

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

警告消息(我有 39 条此类错误消息):

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?

推荐答案

您可以在 tryCatch 之间调用 getFinancials.举个例子:

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"  

这篇关于R quantmod:getFinancials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆