我使用此link中的代码从Google财经获取Google股票数据,但是,我无法获得超过一年的历史数据。有没有办法通过Google财经获取更多数据?
# 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
我也尝试过quantmod包来获取数据,但是出现了错误:
getSymbols("JNJ",src="google")
Error in fr[, -1] : incorrect number of dimensions