0

I have this function, which obtains financial account information from yahoo finance

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)
      }
    )
  }
}

It works like the following;

symbols <- c("BABA", "7752.T", "3231.TW", "GOOG")
getFin(symbols)

My question is, is it possible to extract the currency information from yahoo finance. For example going to Alibaba financial accounts (BABA) here we can see that "Currency in CNY. All numbers in thousands". Going to "7752.T" it states "All numbers in thousands" which I assume to be in USD. The same goes for "3231.TW" and "GOOG".

I would like to take all accounts with CNY and convert them to USD on a particular date. I am just wondering how to extract CNY so I can link it to a CNY/USD conversion so all financial accounts will be in USD.

The same goes for any currency that yahoo might report in. i.e. if there is HKD I would like to extract this info and use it to convert to USD.

EDIT: Note, that the getFin function here is not the same as the getFin or getFinancials function in quantmod.

user113156
  • 6,761
  • 5
  • 35
  • 81
  • `getFin` and `getFinancials` in quantmod are defunct / not working anymore. You might want to investigate Quandl or tiingo for this data. Or a decent broker with an api. – phiver Sep 25 '18 at 14:08
  • getFin here is a custom function developed by a user on SO. It works well for collecting financial information from yahoo finance. I am not familiar with collecting financial information from Quandl or tiingo: https://stackoverflow.com/questions/49452906/getfinancials-quantmod-and-tq-get-tidy-quant-not-working – user113156 Sep 25 '18 at 14:13
  • Don't give a function the same name that is being used in quantmod and use the quantmod tag. Some webscraping tags might be of better use. But use yahoo data at your own risk. – phiver Sep 25 '18 at 14:20
  • Thats actually a good point which I did not think of. May I ask why yaho data should be used at your own risk? The financial statement data seems pretty accurate to me (I do not like some of the ways they calculated things on their balance sheets since its hard for me to re-create some things like EBITDA and EV). – user113156 Sep 25 '18 at 14:23
  • Yahoo's financial statements are sometimes off. Often enough that I don't look at them anymore. Look at the revenues from PSA for 2017. Yahoo 2,744 million, Seeking Alfa 2.67Billion and stockrow 2,668 million. Looking at the 10K it should be 2,668,528k. Which corresponds with stockrow and seeking alpha and my broker. If revenues are wrong, then what else is wrong? – phiver Sep 25 '18 at 15:14

0 Answers0