Search code examples
rfinancequantmodfinancial

How do I use multiple tickers when using getFin in r?


I want to be able to generate tables that show select financial statement line items and financial statement ratios (e.g., revene, OpEx, EBITDA, enterprise value). I'm trying to get getFin to accept a list of tickers.

library(quantmod)
library(xts)

ticker <- c("MS", "GS", "CS")
ticker1 <- getSymbols(ticker)
getFin(ticker1) #env.class

income<-viewFin(ticker1, "IS","A") # annual income statement
balancesheet<-viewFin(ticker1,"BS","A") # annual balance sheet

However, getFin is only recognizing the MS ticker as MS.f, so GS.f and CS.f do not get added to the environment. This also prevents income and balancesheet to be added to the environment.

Let me know how this can be fix. Thanks for the help.


Solution

  • Load the data into an environment you create, rather than the global environment. Then use eapply to loop over all objects in the environment and call viewFin on each.

    e <- new.env()
    getFin(paste0(ticker,collapse=";"), env=e)
    income <- eapply(e, viewFin, type="IS", period="A")
    balancesheet <- eapply(e, viewFin, type="BS", period="A")
    

    income and balancesheet will be lists of the relevant statements for each of the objects specified by ticker.