Alex_P
Alex_P

Reputation: 143

How to get Financial Information of Stocks in R

I am trying to get financial Information like NAV, Dividends, Liabilities. I try to get all the stock information using quantmod package. But it is not working for financials. Is there any way to get financial data in R. I used below code and it gives me error.

CODE

library(quantmod)
statements <- getFinancials("AAPL", auto.assign=FALSE)

ERROR:

Error: ‘getFinancials.google’ is defunct.
Google Finance stopped providing data in March, 2018.

Is there any other way to get financial Information?

Upvotes: 1

Views: 2848

Answers (1)

user113156
user113156

Reputation: 7107

Perhaps the following will work for you:

getFin <- function(stock){
  if ("rvest" %in% installed.packages()) {
    library(rvest)
  }else{
    install.packages("rvest")
    library(rvest)
  }
  for (i in 1:length(stock)) {
    tryCatch(
      {
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/financials?p=",stock[i])
        wahis.session <- html_session(url)                                
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        IS <- p[[1]]
        colnames(IS) <- paste(IS[1,])
        IS <- IS[-c(1,5,12,20,25),]
        names_row <- paste(IS[,1])
        IS <- IS[,-1]
        IS <- apply(IS,2,function(x){gsub(",","",x)})
        IS <- as.data.frame(apply(IS,2,as.numeric))
        rownames(IS) <- paste(names_row)
        temp1 <- IS
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/balance-sheet?p=",stock[i])
        wahis.session <- html_session(url)
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        BS <- p[[1]]
        colnames(BS) <- BS[1,]
        BS <- BS[-c(1,2,17,28),]
        names_row <- BS[,1]
        BS <- BS[,-1] 
        BS <- apply(BS,2,function(x){gsub(",","",x)})
        BS <- as.data.frame(apply(BS,2,as.numeric))
        rownames(BS) <- paste(names_row)
        temp2 <- BS
        url <- "https://finance.yahoo.com/quote/"
        url <- paste0(url,stock[i],"/cash-flow?p=",stock[i])
        wahis.session <- html_session(url)
        p <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="Col1-1-Financials-Proxy"]/section/div[3]/table')%>%
          html_table(fill = TRUE)
        CF <- p[[1]]
        colnames(CF) <- CF[1,]
        CF <- CF[-c(1,3,11,16),]
        names_row <- CF[,1]
        CF <- CF[,-1] 
        CF <- apply(CF,2,function(x){gsub(",","",x)})
        CF <- as.data.frame(apply(CF,2,as.numeric))
        rownames(CF) <- paste(names_row)
        temp3 <- CF
        assign(paste0(stock[i],'.f'),value = list(IS = temp1,BS = temp2,CF = temp3),envir = parent.frame())

      },
      error = function(cond){
        message(stock[i], "Give error ",cond)
      }
    )
  }
}

Then simply

symbols <- c("NVDA", "GOOG", "GE")
getFin(symbols)

GOOG.f$IS
GOOG.f$BS
GOOG.f$CF

Which returns;

> head(GOOG.f$CF)
                                12/31/2017
Net Income                        12662000
Depreciation                       6899000
Adjustments To Net Income          8284000
Changes In Accounts Receivables   -3768000
Changes In Liabilities             1121000
Changes In Inventories                  NA
                                12/31/2016
Net Income                        19478000
Depreciation                       6100000
Adjustments To Net Income          7158000
Changes In Accounts Receivables   -2578000
Changes In Liabilities              333000
Changes In Inventories                  NA
                                12/31/2015
Net Income                        16348000
Depreciation                       5024000
Adjustments To Net Income          5609000
Changes In Accounts Receivables   -2094000
Changes In Liabilities              246000
Changes In Inventories                  NA
                                12/31/2014
Net Income                        14136000
Depreciation                       4601000
Adjustments To Net Income          3615000
Changes In Accounts Receivables   -1641000
Changes In Liabilities              261000
Changes In Inventories                  NA

Credit to Everton Reis

Upvotes: 1

Related Questions