[Blotter-commits] r1567 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 9 01:04:44 CET 2013


Author: bodanker
Date: 2013-11-09 01:04:43 +0100 (Sat, 09 Nov 2013)
New Revision: 1567

Modified:
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
Log:
- updatePortf (and .updatePosPL) gain an 'Interval' argument


Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2013-11-09 00:02:24 UTC (rev 1566)
+++ pkg/blotter/R/updatePortf.R	2013-11-09 00:04:43 UTC (rev 1567)
@@ -11,11 +11,14 @@
 #'
 #' @param Portfolio string identifying a portfolio
 #' @param Symbols character vector identifying symbols to update the portfolio for, default NULL
-#' @param Dates xts-style ISO-8601 time range to run updatePortf over, default NULL (will use times from Prices
+#' @param Dates optional xts-style ISO-8601 time range to run updatePortf over, default NULL (will use times from Prices)
 #' @param Prices optional xts object containing prices and timestamps to mark the book on, default NULL
+#' @param Interval optional character string, containing one of "millisecond" (or "ms"), "microsecond" (or "us"),
+#' "second", "minute", "hour", "day", "week", "month", "quarter", or "year".  This can optionally be preceded by
+#' a positive integer, or followed by "s".
 #' @param \dots any other passthrough parameters
 #' @export
-updatePortf <- function(Portfolio, Symbols=NULL, Dates=NULL, Prices=NULL, ...)
+updatePortf <- function(Portfolio, Symbols=NULL, Dates=NULL, Prices=NULL, Interval=Interval, ...)
 { #' @author Peter Carl, Brian Peterson
      pname<-Portfolio
      Portfolio<-.getPortfolio(pname) # TODO add Date handling
@@ -26,7 +29,7 @@
      }
      for(symbol in Symbols){
        tmp_instr<-try(getInstrument(symbol), silent=TRUE)
-       .updatePosPL(Portfolio=pname, Symbol=as.character(symbol), Dates=Dates, Prices=Prices, ...=...)
+       .updatePosPL(Portfolio=pname, Symbol=as.character(symbol), Dates=Dates, Prices=Prices, Interval=Interval, ...=...)
      }
      
      # Calculate and store portfolio summary table

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2013-11-09 00:02:24 UTC (rev 1566)
+++ pkg/blotter/R/updatePosPL.R	2013-11-09 00:04:43 UTC (rev 1567)
@@ -9,7 +9,7 @@
 #' @return Regular time series of position information and PL 
 #' @author Peter Carl, Brian Peterson
 #' @rdname updatePosPL
-.updatePosPL <- function(Portfolio, Symbol, Dates=NULL, Prices=NULL, ConMult=NULL, ...)
+.updatePosPL <- function(Portfolio, Symbol, Dates=NULL, Prices=NULL, ConMult=NULL, Interval=NULL, ...)
 { # @author Peter Carl, Brian Peterson
   rmfirst=FALSE
   prices=NULL
@@ -41,6 +41,10 @@
             index(prices[paste('/',.parseISO8601(Dates)$last.time,sep='')])
         } else xts:::time.xts(prices[Dates])
     }
+    if(!is.null(Interval)) {
+        ep_args <- .parse_interval(Interval)
+        prices <- prices[endpoints(prices, on=ep_args$on, k=ep_args$k)]
+    }
     
     if(ncol(prices)>1) prices=getPrice(Prices,Symbol)
     
@@ -220,6 +224,27 @@
   #assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
 }
 
+.parse_interval <- function(interval) {
+
+    # taken/modified from xts:::last.xts
+    ip <- gsub("^([[:digit:]]*)([[:alpha:]]+)", "\\1 \\2", interval)
+    ip <- strsplit(ip, " ", fixed = TRUE)[[1]]
+    if (length(ip) > 2 || length(ip) < 1) 
+        stop(paste("incorrectly specified", sQuote("interval")))
+
+    rpu <- ip[length(ip)]
+    rpf <- ifelse(length(ip) > 1, as.numeric(ip[1]), 1)
+    
+    dt.list <- c("milliseconds", "ms", "microseconds", "us", "secs",
+      "mins", "hours", "days", "weeks", "months", "quarters", "years")
+    dt.ind <- pmatch(rpu, dt.list)
+    if(is.na(dt.ind))
+        stop("could not uniquely match '", rpu, "' in '", paste0(dt.list,collapse=",'", "'"))
+    dt <- dt.list[dt.ind]
+
+    list(on=dt, k=rpf)
+}
+
 ###############################################################################
 # Blotter: Tools for transaction-oriented trading systems development
 # for R (see http://r-project.org/) 



More information about the Blotter-commits mailing list