Last active
May 25, 2017 21:15
-
-
Save enxt/86d420172204409b6cc31d6820ff404f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getSymbol.gainc <- function(pair, year, month) { | |
# Required packages, loads both zoo and xts | |
require("xts") | |
op <- options() | |
options("digits.secs"=5) | |
options("download.file.method"="wget") | |
basepair = paste(tolower(pair), year, month, sep="_") | |
pair = toupper(pair) | |
pair = paste(substr(pair, 1, 3), "_", substr(pair,4, 6), sep="") | |
if(file.exists(paste(toupper(basepair), ".rdata", sep=""))) { | |
cat(toupper(basepair), ".rdata", sep="") | |
load(paste(toupper(basepair), ".rdata", sep="")) | |
return (get(pair)) | |
} else if(file.exists(paste(basepair, ".zip", sep=""))) { | |
print("El fichero existe pasandolo a data") | |
data <- read.csv(unz(paste(basepair, ".zip", sep=""), paste(basepair, ".csv", sep="")), header=TRUE ,sep=",") | |
# unlink(paste(basepair, ".csv", sep="")) | |
} else { | |
# Parsing the arguments | |
year = as.numeric(year) | |
month = as.numeric(month) | |
mon = c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') [month] | |
slash = ifelse((month<10), ("/0"), ("/")) | |
# Download html file; works on linux with | |
tmpname = paste("gaindatatempfile", year, month, sep="") | |
htmlurl = paste("http://ratedata.gaincapital.com/", year, slash, month, "%20", mon, "/", sep="") | |
download.file(htmlurl, tmpname, quiet=TRUE) | |
# Parse the html file for pair records | |
txtl=invisible(readLines(file(tmpname))) | |
m <- regexpr(paste(pair, "(.*?)zip", sep=""), txtl) | |
pairlist <- regmatches(txtl, m) | |
# Close the connection and delete the html file | |
closeAllConnections() | |
# unlink (tmpname) | |
# Preparing to download data files | |
dlurl <- paste("http://ratedata.gaincapital.com/", year, slash, month, " ", mon, "/", pairlist[], sep="") | |
for (i in 1:length(pairlist)) { | |
# Tempororary string variables | |
zipfile = pairlist[i] | |
print(paste("Tratando fichero ", zipfile)) | |
csvfile = gsub(".zip", ".csv", pairlist[i]) | |
# Download zip file and extract the csv | |
if(!file.exists(zipfile)) { | |
print("Fichero no existe, bajando") | |
download.file(dlurl[i], zipfile) | |
} else { | |
print("Fichero ya existe") | |
} | |
print("Descomprimiendo en tmpdata") | |
tmpdata = read.csv(unz(zipfile, csvfile), header=TRUE ,sep=",") | |
# Create zoo objects and append in series | |
if (i == 1) { | |
print("Es el primer fichero paso a data") | |
data = tmpdata | |
} else { | |
print("No es el primer fichero lo paso a data con rbind") | |
data = rbind(data, tmpdata) | |
} | |
# Close all connections and delete the zip file | |
closeAllConnections() | |
# unlink(csvfile) | |
# unlink (zipfile) | |
} | |
} | |
data <- data[!duplicated(data$RateDateTime),] | |
data <- as.xts(zoo(data[, c("RateBid", "RateAsk")], as.POSIXct(strptime((data[,c("RateDateTime")]), "%Y-%m-%d %H:%M:%OS")))) | |
colnames(data) <- c("sell", "buy") | |
cat("Asignando data a ", pair, "\n") | |
assign(pair, data) | |
rm(data) | |
save(list=pair, file=paste(toupper(basepair),".rdata", sep="")) | |
return (get(pair)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment