Yahoo Finance is probably the most widely used free source of historic share price data. Normally it works really well and the integration into R packages like quantmod makes it an easy to use and powerful solution. However, it is not without problems. Here are a couple of them and some solutions using Google Finance. I’m only going to talk about London traded shares and UK based mutual funds. I will be using the UK version of Google finance https://www.google.co.uk/finance throughout.
Obviously spurious data
This normally only happens for shares with small trading volumes or with ETFs and not for FTSE350 companies. Here are charts for the same ETF (Vanguard S&P 500, VUSA.L) from Yahoo Finance and Google Finance. Using quantmod you can easily change the source to use Google instead of Yahoo. The moral of the story is to always check the basic chart before attempting any more sophisticated analysis.
Missing data
library(XML) library(RCurl) library(lubridate) library(xts) ### Complete data required below symb <- "INDEXFTSE:UKXMV" fname <- "UKXMV" startdate <- ymd("2014-01-31") enddate <- ymd("2015-11-16") ## Construct basic URL sd <- day(startdate) sm <- month(startdate, label = T) sy <- year(startdate) ed <- day(enddate) em <- month(enddate, label = T) ey <- year(enddate) basicURL <- paste("https://www.google.co.uk/finance/historical?q=",symb, "&startdate=",sm,"+",sd,"+",sy,"&enddate=",em,"+",ed,"+",ey,"&num=200&start=", sep ="") nrows <- as.numeric(difftime( enddate, startdate, units = "days")/7*5) ### Download data and merge into a data frame called DF start <- seq( 0, nrows, by = 200) DF <- NULL for(i in 1:length(start)){ theurl <- getURL(paste(basicURL,start[i],sep = "")) temp <- readHTMLTable(theurl, which= 4, header = TRUE, stringsAsFactors = FALSE ) DF <- rbind(DF, temp) } ## Tidy up DF colnames(DF) <- gsub("\\s|\\\n",'',colnames(DF)) DF[DF == "-"] <- NA head(DF) tail(DF) DF[,1] <- mdy(DF[,1]) row.names(DF) <- DF[,1] DF[,1] <- NULL id <- c(1:ncol(DF)) DF[,id] <- as.numeric(as.character(unlist(DF[,id]))) head(DF) ## Convert the data frame DF into an XTS object DF.xts <- as.xts(DF) assign(fname, DF.xts) head(DF.xts) plot(DF.xts)
One thought on “Getting round some Yahoo Finance problems using Google Finance and R”