1

I used the code from this link to get Google stock data from Google finance, however, I was not able to get more than one year of historical data. Is there a way to get more data via Google finance?

# Make sure data.table is installed
if(!'data.table' %in% installed.packages()[,1]) install.packages('data.table')

# Function to fetch google stock data
google <- function(sym, current = TRUE, sy = 2005, sm = 1, sd = 1, ey, em, ed)
{

    if(current){
        system_time <- as.character(Sys.time())
        ey <- as.numeric(substr(system_time, start = 1, stop = 4))
        em <- as.numeric(substr(system_time, start = 6, stop = 7))
        ed <- as.numeric(substr(system_time, start = 9, stop = 10))
    }

    require(data.table)

    google_out = tryCatch(
        suppressWarnings(
            fread(paste0("http://www.google.com/finance/historical",
                         "?q=", sym,
                         "&startdate=", paste(sm, sd, sy, sep = "+"),
                         "&enddate=", paste(em, ed, ey, sep = "+"),
                         "&output=csv"), sep = ",")),
        error = function(e) NULL
    )

    if(!is.null(google_out)){
        names(google_out)[1] = "Date"
    }

    return(google_out)
}

# Test it
google_data = google('GOOGL')
tail(google_data)
      Date   Open   High    Low  Close  Volume
1: 17-Oct-16 805.99 813.49 803.83 806.84 1056367
2: 14-Oct-16 807.45 810.09 802.32 804.60 1111934
3: 13-Oct-16 806.07 806.56 798.62 804.08 1368981
4: 12-Oct-16 811.96 814.50 808.55 811.77  907876
5: 11-Oct-16 814.17 819.86 807.37 809.57 1721575
6: 10-Oct-16 803.93 817.38 802.24 814.17 1496115
  • I also tried quantmod package to get the data but got an error:

    getSymbols("JNJ",src="google") Error in fr[, -1] : incorrect number of dimensions

mql4beginner
  • 2,193
  • 5
  • 34
  • 73
  • I copied everything but the last 2 lines of your script body and I got the error: "Error in google("JNJ") : could not find function 'google' ". Should the line after `# Test it` say `JNJ(google)` instead of google(JNJ)? – shea Oct 09 '17 at 13:29
  • If I try it as `JNJ(google)`, it generates a URL "http://www.google.com/finance/historical?q=google&startdate=1+1+2005-01-01&enddate=10+9+2017&output=csv" and the line after that is NULL, so I don't get any data at all. If I paste that URL into browser, Google finance gives an error message "The request could not be processed." – shea Oct 09 '17 at 13:36
  • Hello @shea, I updated the code and tested it, should be work fine now. the stock data is for Google.Thanks for bringing up the code issue. – mql4beginner Oct 09 '17 at 13:39
  • 1
    I get the same limitation as you. I don't know much about APIs or scraping, so I don't know what's going on. However, I read the post that you linked to and the Notes at the end include a lot of info that makes me think that Google finance is acting like a jerk. For example: "I have found evidence of a Google representative stating that this API is not to be used in the backend of any public application. I am curious about whether or not I will be able to use this code in Automated Trading with R in light of this statement. I believe it will not be a problem since ..." – shea Oct 09 '17 at 14:04
  • 1
    "... quantmod has been publicly using it for so many years." So maybe it won't work. I tried altering the URL generated and going to the finance site directly and it still wouldn't go back more than one year. I manually wrote "http://www.google.com/finance/historical?q=goog&startdate=10+1+2010&enddate=10+9+2011" and pasted that into the finance site and that just pulled up the last year's data, like the initial request. – shea Oct 09 '17 at 14:06

0 Answers0