Search code examples
rdatatablegoogle-finance

R download google finance data


I downloaded a script to download historical data from google, however, it only downloaded the data only for a year but I wanna download all the data, but the script has the start date for the data

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

It only downloaded 2016 to 2017 data, but anyone can advise me why ? Thank you.


Solution

  • I think your answer lies here : https://chrisconlan.com/download-historical-stock-data-google-r-python/ :

    "the Google Finance API stopped taking requests at this URL. It only returns a year’s worth of daily data as of the time of writing".