Skip to content

Instantly share code, notes, and snippets.

@jaehyeon-kim
Created November 19, 2014 09:54
Show Gist options
  • Save jaehyeon-kim/17d49c265c1a01e25566 to your computer and use it in GitHub Desktop.
Save jaehyeon-kim/17d49c265c1a01e25566 to your computer and use it in GitHub Desktop.
download and merge NASDAQ stock data
library(lubridate)
library(data.table)
library(plyr)
library(dplyr)
# create data folder
dataDir <- paste0("data",format(Sys.Date(),"_%Y_%m_%d"))
if(file.exists(dataDir)) {
unlink(dataDir, recursive = TRUE)
dir.create(dataDir)
} else {
dir.create(dataDir)
}
# assumes codes are known beforehand
codes <- c("MSFT", "TCHC")
urls <- paste0("http://www.google.com/finance/historical?q=NASDAQ:",
codes,"&output=csv")
paths <- paste0(dataDir,"\\",codes,".csv")
# simple error handling in case file doesn't exists
downloadFile <- function(url, path, ...) {
# remove file if exists already
if(file.exists(path)) file.remove(path)
# download file
tryCatch(
download.file(url, path, ...), error = function(c) {
# remove file if error
if(file.exists(path)) file.remove(path)
# create error message
c$message <- paste(substr(path, 1, 4),"failed")
message(c$message)
}
)
}
# wrapper of mapply
Map(downloadFile, urls, paths)
# read all csv files and merge
files <- dir(dataDir, full.name = TRUE)
dataList <- llply(files, function(file){
data <- fread(file, stringsAsFactors = FALSE)
# first column's name is funny
names(data) <- c("Date","Open","High","Low","Close","Volume")
data$Date <- dmy(data$Date)
data$Open <- as.numeric(data$Open)
data$High <- as.numeric(data$High)
data$Low <- as.numeric(data$Low)
data$Close <- as.numeric(data$Close)
data$Volume <- as.integer(data$Volume)
data
}, .progress = "text")
data <- rbind_all(dataList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment