Search code examples
rquantmod

Importing 10-year history of company financials


Loading financial data like income statements and balance sheets is possible usingquantmod package:

library(quantmod)
getFinancials("GOOG")
viewFinancials(GOOG.f, type='BS', period='A')

The data is pulled from Google Finance and does not go beyond 2012.

Is there a meaningful way in R for loading 10-year history instead of 5-year?

Edit:

It looks like MorningStar provides 10-year data on some of the key ratios for free. For example, in case of Google, the direct link to the csv would be financials.morningstar.com/ajax/exportKR2CSV.html?&t=GOOG

How would I be able to lead it in R using read.csv() function, and without hardcoding the stock name into the link? The code below is not correct but I suppose something of this sort:

ticker<-"GOOG"
read.csv(url(financials.morningstar.com/ajax/exportKR2CSV.html?&t=ticker))

Any suggestions?


Solution

  • This will get you 10 years worth of data, where it exists.

    stocks <- c("AXP", "BA", "CAT", "CSCO")
    
    for (s in stocks) {
        names(urls) <- sprintf("http://financials.morningstar.com/ajax/exportKR2CSV.html?&t=%s", stocks)
        lst <- lapply(urls, read.csv, header = TRUE, stringsAsFactors = FALSE, skip = 2)
        lst1 <- lapply(lst, `[`, -12)
        write.csv(lst1, file = "C:/Users/your_path/Desktop/files/data.csv", row.names = FALSE, col.names = FALSE, na = "")
    }