Guest User

Untitled

a guest
Oct 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. # Make sure data.table is installed
  2. if(!'data.table' %in% installed.packages()[,1])
  3. install.packages('data.table')
  4.  
  5. # Function to fetch google stock data
  6. google <- function(sym, current = TRUE, sy = 2005, sm = 1, sd = 1, ey,
  7. em, ed)
  8. {
  9. if(current){
  10. system_time <- as.character(Sys.time())
  11. ey <- as.numeric(substr(system_time, start = 1, stop = 4))
  12. em <- as.numeric(substr(system_time, start = 6, stop = 7))
  13. ed <- as.numeric(substr(system_time, start = 9, stop = 10))
  14. }
  15.  
  16. require(data.table)
  17.  
  18. google_out = tryCatch(
  19. suppressWarnings(
  20. fread(paste0("http://www.google.com/finance/historical",
  21. "?q=", sym,
  22. "&startdate=", paste(sm, sd, sy, sep = "+"),
  23. "&enddate=", paste(em, ed, ey, sep = "+"),
  24. "&output=csv"), sep = ",")),
  25. error = function(e) NULL)
  26.  
  27. if(!is.null(google_out)){
  28. names(google_out)[1] = "Date"
  29. }
  30.  
  31. return(google_out)}
Add Comment
Please, Sign In to add comment