Reading a Table from Yahoo Finance

As you know, the composition of an Index changes over time and this script takes the Yahoo Finance page and retrieves all individual stock symbols for you and stores it in the object called tickers.

I had to update my R-Version to use the RCurl package - this is required as it is a https:// connection we would like to use.

I was surprised how easy it is to parse the xml page and retrieve all the tables on the page.



In this little example we are interested in retrieving all symbols of the DAX (30 largest listed companies in Germany) from this link https://de.finance.yahoo.com/q/cp?s=^GDAXI

It is the table 5 which holds the relevant data for us  - it is completely transparent to the user and I enjoyed using the XML - Package.

library(XML)
library(RCurl)

universum <-function()
{
  world=c("^GDAXI","^MDAXI","^SDAXI","^TECDAX")
  my=NA
  for(i in 1:NROW(world)) {
    url=paste("https://de.finance.yahoo.com/q/cp?s=",world[[i]],sep = "")
    s <- getURL(url)  #rcurl package
    t=readHTMLTable(s)
    t=t[[5]]
    t = t[-(1:5), ]
    newtickers=t[,1]
    a=NROW(my)
    b=NROW(newtickers)
    my[a+1:b]=as.character(newtickers)
   
  }
  my = my[-1]
  tickers=my
  rm(t)
  rm(a)
  rm(b)
  rm(i)
  rm(my)
  rm(newtickers)
  return(tickers)
}

3 comments :

  1. Why not make the code a little more succinct? E.g.

    universum <- function(world=c("^GDAXI","^MDAXI","^SDAXI","^TECDAX")) {
    do.call(c, lapply(world,
    function(chr) {
    url <- paste0("https://de.finance.yahoo.com/q/cp?s=", chr)
    tickets <- XML::readHTMLTable(RCurl::getURL(url))[[5]][-(1:5),][, 1]
    as.character(tickets)
    }))
    }

    ReplyDelete
  2. You're welcome. I find it much easier to understand what is going on if the code is "to the point", so to speak. One thing you should note is that it is never necessary to use rm() inside functions (well, nearly never...). This is because functions do not leave a mess after them. They do their own cleaning up and they do it fast and efficiently.

    ReplyDelete

Further Readings