[Blotter-commits] r826 - in pkg/FinancialInstrument: R sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Oct 23 20:28:06 CEST 2011
Author: gsee
Date: 2011-10-23 20:28:05 +0200 (Sun, 23 Oct 2011)
New Revision: 826
Modified:
pkg/FinancialInstrument/R/load.instruments.R
pkg/FinancialInstrument/R/saveSymbols.R
pkg/FinancialInstrument/sandbox/download.tblox.R
Log:
- tblox funs in sandbox were not properly unlinking files/directories
- define_futures.tblox now calls setSymbolLookup (by adding a src field)
- better error handling in saveSymbols.common/.days
Modified: pkg/FinancialInstrument/R/load.instruments.R
===================================================================
--- pkg/FinancialInstrument/R/load.instruments.R 2011-10-23 01:42:51 UTC (rev 825)
+++ pkg/FinancialInstrument/R/load.instruments.R 2011-10-23 18:28:05 UTC (rev 826)
@@ -231,6 +231,7 @@
#' @param verbose TRUE/FALSE
#' @param auto.assign TRUE/FALSE
#' @seealso
+#' \code{\link{saveSymbols.days}}
#' \code{\link{instrument}}
#' \code{\link{setSymbolLookup.FI}}
#' \code{\link{load.instruments}}
Modified: pkg/FinancialInstrument/R/saveSymbols.R
===================================================================
--- pkg/FinancialInstrument/R/saveSymbols.R 2011-10-23 01:42:51 UTC (rev 825)
+++ pkg/FinancialInstrument/R/saveSymbols.R 2011-10-23 18:28:05 UTC (rev 826)
@@ -32,7 +32,7 @@
base_dir <- paste(base_dir,"/",sep="")
tmpenv <- new.env()
for (symbol in Symbols) {
- tmp <- try(get(symbol,pos=env))
+ tmp <- try(get(symbol,pos=env), silent=TRUE)
if (!is.null(tmp) && !inherits(tmp,'try-error')) {
D <- split(tmp,'days')
if (!file.exists(paste(base_dir,symbol,sep=""))) dir.create(paste(base_dir,symbol,sep=""))
@@ -43,7 +43,7 @@
assign(symbol,D[[i]],envir=tmpenv)
save(list=symbol,file=fnames[i],envir=tmpenv)
}
- }
+ } else if (inherits(tmp, 'try-error')) warning(paste(symbol, "could not be found in 'env' and was not saved."))
}
}
@@ -56,14 +56,14 @@
base_dir <- paste(base_dir, "/", sep = "")
tmpenv <- new.env()
for (symbol in Symbols) {
- tmp <- try(get(symbol, pos = env))
+ tmp <- try(get(symbol, pos = env), silent=TRUE)
if (!is.null(tmp) && !inherits(tmp, "try-error")) {
if (!file.exists(paste(base_dir, symbol, sep = "")))
dir.create(paste(base_dir, symbol, sep = ""))
fnames <- paste(base_dir, symbol, "/", symbol, ".rda", sep = "")
assign(symbol, tmp, envir = tmpenv)
save(list = symbol, file = fnames, envir = tmpenv)
- }
+ } else if (inherits(tmp, 'try-error')) warning(paste(symbol, "could not be found in 'env' and was not saved."))
}
}
Modified: pkg/FinancialInstrument/sandbox/download.tblox.R
===================================================================
--- pkg/FinancialInstrument/sandbox/download.tblox.R 2011-10-23 01:42:51 UTC (rev 825)
+++ pkg/FinancialInstrument/sandbox/download.tblox.R 2011-10-23 18:28:05 UTC (rev 826)
@@ -8,22 +8,28 @@
#' \code{getSymbols.tblox} will only return the Symbols you ask for.
get_tblox <- function(env='.GlobalEnv') {
# a function to download all data for all 40 instruments
+ tmpdir <- tempdir()
+ tblox.tmp <- paste(tmpdir, "tblox", sep="/")
+ if(!file.exists(tblox.tmp)) dir.create(tblox.tmp)
tmp <- tempfile()
- download.file("http://www.tradingblox.com/Data/DataOnly.zip",tmp)
- tblox.tmp <- tempdir()
+ download.file("http://www.tradingblox.com/Data/DataOnly.zip", tmp)
unzip(tmp,exdir=tblox.tmp)
def <- read.csv('http://www.tradingblox.com/tradingblox/CSIUA/FuturesInfo.txt',skip=1,header=FALSE)
+ badsym <- NULL
for (i in 1:length(def[,1])){
if (file.exists(paste(tblox.tmp,'Futures',def[i,4],sep='/'))) {
dat <-read.csv(paste(tblox.tmp,"/Futures/",def[i,4],sep=""),header=FALSE)
- idx <- as.Date(dat[,1],format='%Y%m%d')
+ idx <- as.Date(dat[,1],format='%Y%m%d', origin='1970-01-01')
x <- xts(dat[,2:9],order.by=as.Date(paste(dat[,1]),format="%Y%m%d"))
cn <- c(paste('Adj',c("Open","High","Low","Close"),sep="."),'Volume','OpInt','ExpMth','Unadj.Close')
colnames(x) <- paste(def[i,1],cn,sep=".")
assign(paste(def[i,1]), x, pos=env)
- }
+ } else badsym <- c(badsym, paste(def[i,1]))
}
- paste(def[,1])
+ unlink(tmp)
+ unlink(tblox.tmp, recursive=TRUE)
+ out <- paste(def[,1])
+ out[!out %in% badsym]
}
# a getSymbols method to get only the symbols you specify
@@ -42,9 +48,12 @@
if (missing(auto.assign))
auto.assign <- TRUE
if (!auto.assign) stop("must use auto.assign=TRUE for src='tblox'")
+
+ tmpdir <- tempdir()
+ tblox.tmp <- paste(tmpdir, "tblox", sep="/")
+ if(!file.exists(tblox.tmp)) dir.create(tblox.tmp)
tmp <- tempfile()
download.file("http://www.tradingblox.com/Data/DataOnly.zip",tmp)
- tblox.tmp <- tempdir()
unzip(tmp,exdir=tblox.tmp)
def <- read.csv('http://www.tradingblox.com/tradingblox/CSIUA/FuturesInfo.txt',skip=1,header=FALSE)
if (is.null(Symbols) || is.na(Symbols) || Symbols == "all" || Symbols == "")
@@ -69,6 +78,8 @@
sym.out <- c(sym.out, paste(def[i,1]))
}
}
+ unlink(tmp)
+ unlink(tblox.tmp, recursive=TRUE)
return(sym.out)
}
@@ -104,6 +115,7 @@
currency = paste(def[i,8]),
multiplier = as.numeric(gsub(",","",def[i,9])),
tick_size = as.numeric(tick),
+ src = 'tblox',
defined.by = 'tblox' )
}
instr <- getInstrument(primary_id)
More information about the Blotter-commits
mailing list