[Blotter-commits] r885 - in pkg/FinancialInstrument: . R inst/parser man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 22 18:55:21 CET 2011


Author: gsee
Date: 2011-12-22 18:55:21 +0100 (Thu, 22 Dec 2011)
New Revision: 885

Added:
   pkg/FinancialInstrument/R/Tick2Sec.R
   pkg/FinancialInstrument/man/alltick2sec.Rd
   pkg/FinancialInstrument/man/to_secBATV.Rd
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/NAMESPACE
   pkg/FinancialInstrument/R/load.instruments.R
   pkg/FinancialInstrument/inst/parser/TRTH_BackFill.R
Log:
 - add Tick2Sec.R file (to_secBATV and alltick2sec funs)
 - TRTH_BackFill now uses FinancialInstrument:::to_secBATV, and Tick2Sec_file no longer needs to be specified


Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2011-12-22 17:43:25 UTC (rev 884)
+++ pkg/FinancialInstrument/DESCRIPTION	2011-12-22 17:55:21 UTC (rev 885)
@@ -11,13 +11,15 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios. Still
     in heavy development.
-Version: 0.9.14
+Version: 0.9.15
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:
     R (>= 2.12.0),
     quantmod(>= 0.3-17),
     zoo(>= 1.7-5)
+Suggests:
+    foreach
 Collate:
     'buildHierarchy.R'
     'buildSpread.R'
@@ -43,3 +45,4 @@
     'saveInstruments.R'
     'ls_expiries.R'
     'saveSymbols.R'
+    'Tick2Sec.R'

Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE	2011-12-22 17:43:25 UTC (rev 884)
+++ pkg/FinancialInstrument/NAMESPACE	2011-12-22 17:55:21 UTC (rev 885)
@@ -106,6 +106,7 @@
 export(synthetic.instrument)
 export(synthetic.ratio)
 export(.to_daily)
+export(to_secBATV)
 export(update_instruments.TTR)
 export(update_instruments.yahoo)
 export(volep)

Added: pkg/FinancialInstrument/R/Tick2Sec.R
===================================================================
--- pkg/FinancialInstrument/R/Tick2Sec.R	                        (rev 0)
+++ pkg/FinancialInstrument/R/Tick2Sec.R	2011-12-22 17:55:21 UTC (rev 885)
@@ -0,0 +1,112 @@
+#' convert tick data to one-second data
+#'
+#' From tick data with columns: \dQuote{Price}, \dQuote{Volume}, \dQuote{Bid.Price},
+#' \dQuote{Bid.Size}, \dQuote{Ask.Price}, \dQuote{Ask.Size}, to data of one second frequency
+#' with columns \dQuote{Bid.Price}, \dQuote{Bid.Size}, \dQuote{Ask.Price}, \dQuote{Ask.Size},
+#' \dQuote{Trade.Price}, and \dQuote{Volume}
+#'
+#' The primary purpose of this function is to reduce the amount of data on disk so that
+#' it will take less time to load the data into memory.
+#'
+#' If there are no trades or bid/ask price updates in a given second, we will not make
+#' a row for that timestamp.  If there were no trades, but the bid or ask
+#' price changed, then we _will_ have a row but the Volume and Trade.Price will be NA.  
+#' 
+#' @param x the xts series to convert to 1 minute BATMV
+#' @return an xts object of 1 second frequency
+#' @author gsee
+#' @examples
+#' \dontrun{
+#' getSymbols("CLU1")
+#' system.time(xsec <- to_secBATV(CLU1))
+#' }
+#' @export
+to_secBATV <- function(x) {
+    #require(qmao)
+    # Define Bi and As functions (copied from qmao package)
+    Bi <- function(x) {
+        if (has.Bid(x)) 
+            return(x[, grep("Bid", colnames(x), ignore.case = TRUE)])
+        stop("subscript out of bounds: no column name containing \"Bid\"")
+    }
+    As <- function(x) {
+        if (has.Ask(x)) 
+            return(x[, grep("Ask", colnames(x), ignore.case = TRUE)])
+        stop("subscript out of bounds: no column name containing \"Ask\"")
+    }
+
+    x <- make.index.unique(x)
+    ohlcv <- suppressWarnings(to.period(x[,1:2], 'seconds', 1))
+    Volm <- if (!has.Vo(ohlcv)) {
+                rep(NA, NROW(ohlcv)) 
+            } else Vo(ohlcv)
+    ClVo <- if(length(ohlcv) != 0) { ohlcv[, 4:5] } else {
+        tmp <- xts(cbind(rep(NA, NROW(x)), rep(NA, NROW(x))), index(x))
+        tmp[endpoints(tmp, 'seconds')]
+    }
+    xx <- x[endpoints(x, 'seconds')]
+    xx <- cbind(Bi(xx), As(xx), ClVo, all=TRUE)
+    xx[, 1:4] <- na.locf(xx[, 1:4])
+    colnames(xx) <- c("Bid.Price", "Bid.Size", "Ask.Price", "Ask.Size", "Trade.Price", "Volume")
+    #if volume is zero, and all other rows are unchanged, delete that row 
+    out <- xx
+    v <- out[, 6]
+    v[is.na(v)] <- 0
+    dout <- cbind(diff(out[,c(1, 3)]), v)
+    align.time(out[index(dout[!rowSums(dout) == 0])], 1)
+}
+
+
+
+#' Convert several files from tick to 1 second
+#'
+#' @param getdir directory to get tick data from
+#' @param savedir directory to save converted data in
+#' @param Symbols names of instruments to convert
+#' @param overwrite TRUE/FALSE. If file already exists in savedir, should it be overwritten?
+#' @return list of files that were converted
+#' @author gsee
+#' @examples
+#' \dontrun{
+#' convert.log <- alltick2sec()
+#' }
+alltick2sec <- function(getdir = '~/TRTH/tick/', 
+                        savedir = '~/TRTH/sec/', 
+                        Symbols=list.files(getdir),
+                        overwrite = FALSE) {
+    if (!file.exists(savedir)) stop(paste("Please create savedir (", savedir, ") first", sep=""))
+    require(foreach)
+    Symbols <- Symbols[!Symbols %in% c("instruments.rda")]
+    gsep <- if(substr(getdir, nchar(getdir), nchar(getdir)) == "/") { "" } else "/"
+    ssep <- if(substr(savedir, nchar(savedir), nchar(savedir)) == "/") {""} else "/"
+    s=NULL    
+    foreach(s = Symbols) %dopar% {
+        cat("converting ", s, ' ...\n')
+        gdir <- paste(getdir, s, sep=gsep)
+        if (file.exists(gdir)) {
+            sdir <- paste(savedir, s, sep=ssep) 
+            if (!file.exists(sdir)) dir.create(sdir) #create dir for symbol if it doesn't exist
+            fls <- list.files(gdir)
+            fls <- fls[!fls %in% c("Bid.Image", "Ask.Image", "Price.Image")]
+            tmpenv <- new.env() 
+            unname(sapply(fls, function(fl) {
+                if (!file.exists(paste(sdir, fl, sep='/')) || overwrite) {
+                    xsym <- try(load(paste(gdir, fl, sep="/")))
+                    if (!inherits(xsym, 'try-error') && !is.null(get(xsym))) {
+                        #x <- to_secBATMV(get(xsym))
+                        x <- try(to_secBATV(get(xsym)), silent=TRUE)
+                        if (!inherits(x, 'try-error')) {
+                            assign(xsym, x, pos=tmpenv)
+                            sfl <- paste(sdir, fl, sep="/")
+                            save(list = xsym, file = sfl, envir = tmpenv)
+                            rm(xsym, pos=tmpenv)
+                            fl
+                        }
+                    }
+                } else warning(paste(sdir, '/', fl, 
+                    " already exists and will not be overwritten. Use overwrite=TRUE to overwrite.", sep=""))
+            }))
+        } else warning(paste(gdir, 'does not exist'))
+    }
+}
+

Modified: pkg/FinancialInstrument/R/load.instruments.R
===================================================================
--- pkg/FinancialInstrument/R/load.instruments.R	2011-12-22 17:43:25 UTC (rev 884)
+++ pkg/FinancialInstrument/R/load.instruments.R	2011-12-22 17:55:21 UTC (rev 885)
@@ -388,7 +388,7 @@
         }
     }) #end loop over Symbols
 
-    if (is.null(unlist(datl)) {
+    if (is.null(unlist(datl))) {
         warning("No data found.")
         return(NULL) 
     }

Modified: pkg/FinancialInstrument/inst/parser/TRTH_BackFill.R
===================================================================
--- pkg/FinancialInstrument/inst/parser/TRTH_BackFill.R	2011-12-22 17:43:25 UTC (rev 884)
+++ pkg/FinancialInstrument/inst/parser/TRTH_BackFill.R	2011-12-22 17:55:21 UTC (rev 885)
@@ -22,7 +22,6 @@
 # any arguments provided in dots will override arguments of same name from config.file
 ##
 #path.output = '~/TRTH/'                # base directory for output
-#Tick2Sec_file = '~/TRTH/Tick2Sec.R'    # path/to/Tick2Sec.R which contains to_secBATV function definition
 #tick_dir = '~/TRTH/tick'               # directory in which to store tick data
 #archive_dir = '~/TRTH/archive'         # directory in which to store downloaded .gz files
 #csv_dir = '~/TRTH/csv'                 # directory in which to store zipped csv files
@@ -69,7 +68,7 @@
 #############################################################################################
 
 
-configureTRTH <- function(config.file, path.output='~/TRTH/', Tick2Sec_file, ...) {
+configureTRTH <- function(config.file, path.output='~/TRTH/', ...) {
     ## Create environment to hold variables that more than one function needs to access    
     .TRTH <- new.env(parent=.GlobalEnv)
     dargs <- list(...)
@@ -103,10 +102,6 @@
 
     #if (!is.null(dargs$path.output)) 
     .TRTH$path.output <- path.output <- addslash(path.output)
-    if (missing(Tick2Sec_file) && !exists('Tick2Sec_file')) 
-        Tick2Sec_file <- paste(path.output, "Tick2Sec.R", sep="") 
-    if (!file.exists(Tick2Sec_file)) stop("Please provide a valid filepath for 'Tick2Sec_file' or move 'Tick2Sec.R' into 'path.output'")
-    .TRTH$Tick2Sec_file <- Tick2Sec_file
 
     .TRTH$archive_dir <- pickDirArg("archive_dir")
     .TRTH$csv_dir <- pickDirArg("csv_dir")
@@ -456,8 +451,6 @@
     # Make sure csv_dir exists since it is where we read the data from
     if (!file.exists(csv_dir)) stop("There is no directory", paste(csv_dir))
     if (!exists('files.xts')) stop("Cannot find 'files.xts' -- Run splitCSV first")
-    if (!exists('Tick2Sec_file')) stop("Cannot find 'Tick2Sec_file' -- Should have been specified when you ran 'configureTRTH'")
-    source(Tick2Sec_file)
     oldTZ <- Sys.getenv("TZ")
     Sys.setenv(TZ='GMT')
 

Added: pkg/FinancialInstrument/man/alltick2sec.Rd
===================================================================
--- pkg/FinancialInstrument/man/alltick2sec.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/alltick2sec.Rd	2011-12-22 17:55:21 UTC (rev 885)
@@ -0,0 +1,33 @@
+\name{alltick2sec}
+\alias{alltick2sec}
+\title{Convert several files from tick to 1 second}
+\usage{
+  alltick2sec(getdir = "~/TRTH/tick/",
+    savedir = "~/TRTH/sec/", Symbols = list.files(getdir),
+    overwrite = FALSE)
+}
+\arguments{
+  \item{getdir}{directory to get tick data from}
+
+  \item{savedir}{directory to save converted data in}
+
+  \item{Symbols}{names of instruments to convert}
+
+  \item{overwrite}{TRUE/FALSE. If file already exists in
+  savedir, should it be overwritten?}
+}
+\value{
+  list of files that were converted
+}
+\description{
+  Convert several files from tick to 1 second
+}
+\examples{
+\dontrun{
+convert.log <- alltick2sec()
+}
+}
+\author{
+  gsee
+}
+

Added: pkg/FinancialInstrument/man/to_secBATV.Rd
===================================================================
--- pkg/FinancialInstrument/man/to_secBATV.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/to_secBATV.Rd	2011-12-22 17:55:21 UTC (rev 885)
@@ -0,0 +1,41 @@
+\name{to_secBATV}
+\alias{to_secBATV}
+\title{convert tick data to one-second data}
+\usage{
+  to_secBATV(x)
+}
+\arguments{
+  \item{x}{the xts series to convert to 1 minute BATMV}
+}
+\value{
+  an xts object of 1 second frequency
+}
+\description{
+  From tick data with columns: \dQuote{Price},
+  \dQuote{Volume}, \dQuote{Bid.Price}, \dQuote{Bid.Size},
+  \dQuote{Ask.Price}, \dQuote{Ask.Size}, to data of one
+  second frequency with columns \dQuote{Bid.Price},
+  \dQuote{Bid.Size}, \dQuote{Ask.Price}, \dQuote{Ask.Size},
+  \dQuote{Trade.Price}, and \dQuote{Volume}
+}
+\details{
+  The primary purpose of this function is to reduce the
+  amount of data on disk so that it will take less time to
+  load the data into memory.
+
+  If there are no trades or bid/ask price updates in a
+  given second, we will not make a row for that timestamp.
+  If there were no trades, but the bid or ask price
+  changed, then we _will_ have a row but the Volume and
+  Trade.Price will be NA.
+}
+\examples{
+\dontrun{
+getSymbols("CLU1")
+system.time(xsec <- to_secBATV(CLU1))
+}
+}
+\author{
+  gsee
+}
+



More information about the Blotter-commits mailing list