R quantmod::getFinancials

2019-04-12 05:32发布

I'm using the quantmodpackage. I've got a vector of tickers like this :

c("AAPL","GOOG","IBM","GS","AMZN","GE")

and I want to create a function to calculate the EBIT margin of a stock (= operating income / total revenue). So for a given stock, I use the following piece of code which only works for GE (provided a ".f" is added a the end of the ticker) :

require(quantmod)
getFinancials("GE",period="A")
ebit.margin <- function(stock.ticker.f){
   return(stock.ticker$IS$A["Operating Income",]/stock.ticker$IS$A["Total Revenue",])
}
ebit.margin("GE")

I would like to generalize this function in order to use then the applyfunction. There are several difficulties :

  • when applying the quantmod::getFinancialfunction to a ticker, the financial statements of the stocks are saved in the default environment. The viewFinancialhas then to be used to get and print the financial statements. I need a way to get access to the financial statements directly into the function
  • The function's argument function is a string like "GE.f" but it would more convenient to enter directly the ticker ("GE"). I've tried to use the paste0 and gsub to get a string like "GE.f" it doesn't work because "GE.f" doesn't belong to the financials class.

To sum up, I'm a bit lost...

标签: r xts quantmod
2条回答
孤傲高冷的网名
2楼-- · 2019-04-12 05:33

Anaother option is to laod your tickers in an new environnement.

tickers <-  new.env()
s <- c("AAPL","GOOG","IBM","GS","AMZN","GE")
lapply(s, getFinancials,env=tickers)
sapply(ls(envir=tickers),
       function(x) {x <- get(x) ## get the varible name
                    x$IS$A["Operating Income", ] / x$IS$A["Total Revenue",]})

              AAPL.f     AMZN.f       GE.f    GOOG.f      GS.f     IBM.f
2012-09-29 0.3529596 0.01106510 0.11811969 0.2543099 0.2689852 0.2095745
2011-09-24 0.3121507 0.01792957 0.13753327 0.3068724 0.1676678 0.1964439
2010-09-25 0.2818704 0.04110630 0.09415548 0.3540466 0.2804621 0.1974867
2009-09-26 0.2736278 0.04606471 0.06387029 0.3514585 0.3837401 0.1776439

EDIT

No need to use ls, get.... just the handy eapply (thanks @GSee) which applies FUN to the named values from an environment and returns the results as a list

eapply(tickers, function(x) 
              x$IS$A["Operating Income", ] / x$IS$A["Total Revenue",])
查看更多
Summer. ? 凉城
3楼-- · 2019-04-12 05:42

It's easier if you use auto.assign=FALSE

s <- c("AAPL","GOOG","IBM","GS","AMZN","GE")
fin <- lapply(s, getFinancials, auto.assign=FALSE)
names(fin) <- s
lapply(fin, function(x) x$IS$A["Operating Income", ] / x$IS$A["Total Revenue",])
#$AAPL
#2012-09-29 2011-09-24 2010-09-25 2009-09-26 
# 0.3529596  0.3121507  0.2818704  0.2736278 
#
#$GOOG
#2012-12-31 2011-12-31 2010-12-31 2009-12-31 
# 0.2543099  0.3068724  0.3540466  0.3514585 
#
#$IBM
#2012-12-31 2011-12-31 2010-12-31 2009-12-31 
# 0.2095745  0.1964439  0.1974867  0.1776439 
#
#$GS
#2012-12-31 2011-12-31 2010-12-31 2009-12-31 
#0.2689852  0.1676678  0.2804621  0.3837401 
#
#$AMZN
#2012-12-31 2011-12-31 2010-12-31 2009-12-31 
#0.01106510 0.01792957 0.04110630 0.04606471 
#
#$GE
#2012-12-31 2011-12-31 2010-12-31 2009-12-31 
#0.11811969 0.13753327 0.09415548 0.06387029 
查看更多
登录 后发表回答