[Blotter-commits] r803 - in pkg/quantstrat: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Sep 27 22:41:44 CEST 2011


Author: braverock
Date: 2011-09-27 22:41:44 +0200 (Tue, 27 Sep 2011)
New Revision: 803

Added:
   pkg/quantstrat/R/wrapup.R
   pkg/quantstrat/man/add.init.Rd
   pkg/quantstrat/man/initStrategy.Rd
   pkg/quantstrat/man/rm.strat.Rd
   pkg/quantstrat/man/updateStrategy.Rd
Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/R/initialize.R
   pkg/quantstrat/R/strategy.R
Log:
- more work on initialization and wrapup functionality for applyStrategy, still commented out in applyStrategy pending more testing

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2011-09-27 18:51:50 UTC (rev 802)
+++ pkg/quantstrat/DESCRIPTION	2011-09-27 20:41:44 UTC (rev 803)
@@ -1,7 +1,7 @@
 Package: quantstrat
 Type: Package
 Title: Quantitative Strategy Model Framework
-Version: 0.5.5
+Version: 0.5.6
 Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
     Jeffrey A. Ryan, Joshua Ulrich, Garrett See, Yu Chen
@@ -25,3 +25,4 @@
     'signals.R'
     'strategy.R'
     'traderules.R'
+    'wrapup.R'

Modified: pkg/quantstrat/R/initialize.R
===================================================================
--- pkg/quantstrat/R/initialize.R	2011-09-27 18:51:50 UTC (rev 802)
+++ pkg/quantstrat/R/initialize.R	2011-09-27 20:41:44 UTC (rev 803)
@@ -13,7 +13,7 @@
 #' @param init.Acct TRUE/FALSE, default TRUE: 
 #' @param init.Orders TRUE/FALSE, default TRUE: 
 #' @param unique TRUE/FALSE, default TRUE: 
-#' @param dots any other passtrhrough parameters
+#' @param \dots any other passtrhrough parameters
 #' @author Garrett See, Brian Peterson
 #' @export
 initStrategy <- function(strategy, portfolio, symbols, get.Symbols=TRUE, init.Portf=TRUE, init.Acct=TRUE, init.Orders=TRUE, unique=TRUE,...) {
@@ -87,6 +87,31 @@
     }            
 }
 
+#' add arbitrary initialization functions to a strategy
+#' 
+#' \code{\link{initStrategy}} will run a series of common initialization functions at the 
+#' beginning of an \code{\link{applyStrategy}} call.  This function allows the user to 
+#' add arbitrary initialization functions to the sequence.
+#' 
+#' These arbitrary functions will be added to the \code{init} slot of the strategy object
+#' and when \code{applyStrategy} is evaluated, the arbitrary initialization functions will
+#' be evaluated after the standardized functions.
+#' 
+#' For example, if your strategy uses a synthetic basket instrument, you could use this 
+#' initialization slot to add a custom constructor to build the basket instrument time 
+#' series and modify the symbols slot(s) of the strategy and portfolio.
+#' 
+#' @param strategy an object (or the name of an object) of type 'strategy' to add the init function definition to
+#' @param name name of the init, must correspond to an R function
+#' @param arguments named list of default arguments to be passed to an init function when executed
+#' @param parameters vector of strings naming parameters to be saved for apply-time definition,default NULL, only needed if you need special names to avoid argument collision
+#' @param label arbitrary text label for init output, default NULL
+#' @param ... any other passthru parameters
+#' @param enabled TRUE/FALSE whether the init is enabled for use in applying the strategy, default TRUE
+#' @param indexnum if you are updating a specific init, the index number in the $init list to update
+#' @param store TRUE/FALSE whether to store the strategy in the .strategy environment, or return it.  default FALSE
+#' @return if \code{strategy} was the name of a strategy, the name. It it was a strategy, the updated strategy. 
+#' @export
 add.init <- function(strategy, name, arguments, parameters=NULL, label=NULL, ..., enabled=TRUE, indexnum=NULL, store=FALSE) {
     if(!is.strategy(strategy)) stop("You must pass in a strategy object to manipulate")
     tmp_init<-list()

Modified: pkg/quantstrat/R/strategy.R
===================================================================
--- pkg/quantstrat/R/strategy.R	2011-09-27 18:51:50 UTC (rev 802)
+++ pkg/quantstrat/R/strategy.R	2011-09-27 20:41:44 UTC (rev 803)
@@ -126,7 +126,11 @@
 
 			ret[[portfolio]][[symbol]]<-sret
 		}
-	}
+        
+        # TODO call to updateStrategy will go here!
+        # updateStrategy(strategy, portfolio, Symbols=symbols, ...=...)
+        
+    }
     
     if(verbose) return(ret)
 }

Copied: pkg/quantstrat/R/wrapup.R (from rev 788, pkg/quantstrat/sandbox/init.strat_0.5.3.R)
===================================================================
--- pkg/quantstrat/R/wrapup.R	                        (rev 0)
+++ pkg/quantstrat/R/wrapup.R	2011-09-27 20:41:44 UTC (rev 803)
@@ -0,0 +1,128 @@
+#' Remove objects associated with a strategy
+#'
+#' Remove the order_book, account, and portfolio of given \code{name}.
+#' @param name name of the portfolio/account/order book to clean up
+#' @export
+rm.strat <- function(name='default') {
+    if (is.strategy(name)) name <- name[['name']]
+    try(rm(list=paste("order_book",name,sep="."), pos=.strategy))
+    try(rm(list=paste(c("account", "portfolio"), name, sep="."), pos=.blotter))
+}
+
+
+#' run standard and custom strategy wrapup functions such as updating portfolio, account, and ending equity
+#' 
+#' \code{\link{updateStrategy}} will run a series of common wrapup functions at the 
+#' beginning of an \code{\link{applyStrategy}} call.  This function allows the user to 
+#' add arbitrary wrapup functions to the sequence.
+#' 
+#' These arbitrary functions will be added to the \code{update} slot of the strategy object
+#' and when \code{applyStrategy} is evaluated, the arbitrary wrapup functions will
+#' be evaluated \emph{before} the standardized functions.
+#' 
+#' For example, if you are working with high frequency data, it would be common 
+#' to \empgh{mark the book} on a lower frequency, perhaps minutes, hours, or even days,
+#' rather than tick.  A custom wrapup function could take your high frequency 
+#' data and transform it to lower frequency data before the call to \code{\link{updatePortf}}. 
+#' 
+#' @param portfolio string identifying a portfolio
+#' @param account string identifying an account. Same as \code{portfolio} by default
+#' @param Symbols: character vector of names of symbols whose portfolios will be updated
+#' @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 update.Portf TRUE/FALSE if TRUE (default) a call will be made to \code{updatePortf}
+#' @param update.Acct TRUE/FALSE if TRUE (default) a call will be made to \code{updateAcct}
+#' @param update.EndEq TRUE/FALSE if TRUE (default) a call will be made to \code{updateEndEq}
+#' @param showEq TRUE/FALSE if TRUE (default) ending equity will be printed to the screen
+#' @param chart TRUE/FALSE if TRUE (default) a call will be made to \code{chart.Posn}
+#' @param \dots any other passthrough parameters
+#' @seealso \code{\link{updatePortf}}, \code{\link{updateAcct}}, \code{\link{updateEndEq}}, \code{\link{chart.Posn}}
+#' @author Garrett See, Brian Peterson
+#' @export
+updateStrategy <- 
+function(portfolio='default', 
+         account=portfolio, 
+         Symbols=NULL, 
+         Dates=NULL, 
+         Prices=NULL,
+         update.Portf=TRUE,
+         update.Acct=TRUE,
+         update.EndEq=TRUE,
+         showEq=TRUE,
+         chart=TRUE,
+         ...)
+{
+    
+    #first do whatever the user stuck in this wrapup slot...
+    for (wrapup_o in strategy$wrapup){
+        if(!is.function(get(wrapup_o$name))){
+            message(paste("Skipping wrapup",wrapup_o$name,"because there is no function by that name to call"))
+            next()      
+        }
+        
+        if(!isTRUE(wrapup_o$enabled)) next()
+        
+        # see 'S Programming p. 67 for this matching
+        fun<-match.fun(wrapup_o$name)
+        
+        .formals  <- formals(fun)
+        onames <- names(.formals)
+        
+        pm <- pmatch(names(wrapup_o$arguments), onames, nomatch = 0L)
+        #if (any(pm == 0L))
+        #    warning(paste("some arguments stored for",wrapup_o$name,"do not match"))
+        names(wrapup_o$arguments[pm > 0L]) <- onames[pm]
+        .formals[pm] <- wrapup_o$arguments[pm > 0L]       
+        
+        # now add arguments from parameters
+        if(length(parameters)){
+            pm <- pmatch(names(parameters), onames, nomatch = 0L)
+            names(parameters[pm > 0L]) <- onames[pm]
+            .formals[pm] <- parameters[pm > 0L]
+        }
+        
+        #now add dots
+        if (length(nargs)) {
+            pm <- pmatch(names(nargs), onames, nomatch = 0L)
+            names(nargs[pm > 0L]) <- onames[pm]
+            .formals[pm] <- nargs[pm > 0L]
+        }
+        .formals$... <- NULL
+        
+        do.call(fun,.formals)
+    }            
+    
+    out <- list()
+    if(isTRUE(update.Portf)){
+        out[[paste('portfolio',portfolio,sep='.')]] <- updatePortf(Portfolio=portfolio, Symbols=Symbols, Dates=Dates, Prices=Prices,...=...)
+    }
+    if(isTRUE(update.Acct)){
+        out[[paste('account',portfolio,sep='.')]] <- updateAcct(name=account,Dates=Dates,...=...) 
+    }
+    if(isTRUE(update.EndEq)){
+        updateEndEq(Account=account,Dates=Dates,...=...)
+        if(showEq) cat('EndingEq: ', getEndEq(Account=account,Date=Sys.time()), '\n')
+    }
+    if(isTRUE(chart)){
+        for (symbol in names(getPortfolio(portfolio)$symbols) ){
+            dev.new()
+            chart.Posn(Portfolio=portfolio, Symbol=symbol,...=...)
+        }
+    }
+    
+    if (out[[1]] == out[[2]]) out[[1]]
+    else out
+}
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2011
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, Garrett See, and Joshua Ulrich 
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id$
+#
+###############################################################################
\ No newline at end of file


Property changes on: pkg/quantstrat/R/wrapup.R
___________________________________________________________________
Added: svn:keywords
   + Revision Id Date Author

Added: pkg/quantstrat/man/add.init.Rd
===================================================================
--- pkg/quantstrat/man/add.init.Rd	                        (rev 0)
+++ pkg/quantstrat/man/add.init.Rd	2011-09-27 20:41:44 UTC (rev 803)
@@ -0,0 +1,62 @@
+\name{add.init}
+\alias{add.init}
+\title{add arbitrary initialization functions to a strategy}
+\usage{
+  add.init(strategy, name, arguments, parameters = NULL,
+  label = NULL, ..., enabled = TRUE, indexnum = NULL, store
+  = FALSE)
+}
+\arguments{
+  \item{strategy}{an object (or the name of an object) of
+  type 'strategy' to add the init function definition to}
+
+  \item{name}{name of the init, must correspond to an R
+  function}
+
+  \item{arguments}{named list of default arguments to be
+  passed to an init function when executed}
+
+  \item{parameters}{vector of strings naming parameters to
+  be saved for apply-time definition,default NULL, only
+  needed if you need special names to avoid argument
+  collision}
+
+  \item{label}{arbitrary text label for init output,
+  default NULL}
+
+  \item{...}{any other passthru parameters}
+
+  \item{enabled}{TRUE/FALSE whether the init is enabled for
+  use in applying the strategy, default TRUE}
+
+  \item{indexnum}{if you are updating a specific init, the
+  index number in the $init list to update}
+
+  \item{store}{TRUE/FALSE whether to store the strategy in
+  the .strategy environment, or return it.  default FALSE}
+}
+\value{
+  if \code{strategy} was the name of a strategy, the name.
+  It it was a strategy, the updated strategy.
+}
+\description{
+  \code{\link{initStrategy}} will run a series of common
+  initialization functions at the beginning of an
+  \code{\link{applyStrategy}} call.  This function allows
+  the user to add arbitrary initialization functions to the
+  sequence.
+}
+\details{
+  These arbitrary functions will be added to the
+  \code{init} slot of the strategy object and when
+  \code{applyStrategy} is evaluated, the arbitrary
+  initialization functions will be evaluated after the
+  standardized functions.
+
+  For example, if your strategy uses a synthetic basket
+  instrument, you could use this initialization slot to add
+  a custom constructor to build the basket instrument time
+  series and modify the symbols slot(s) of the strategy and
+  portfolio.
+}
+

Added: pkg/quantstrat/man/initStrategy.Rd
===================================================================
--- pkg/quantstrat/man/initStrategy.Rd	                        (rev 0)
+++ pkg/quantstrat/man/initStrategy.Rd	2011-09-27 20:41:44 UTC (rev 803)
@@ -0,0 +1,35 @@
+\name{initStrategy}
+\alias{initStrategy}
+\title{run standard and custom strategy initialization functions}
+\usage{
+  initStrategy(strategy, portfolio, symbols, get.Symbols =
+  TRUE, init.Portf = TRUE, init.Acct = TRUE, init.Orders =
+  TRUE, unique = TRUE, ...)
+}
+\arguments{
+  \item{strategy}{object of type \code{strategy} to
+  initialize data/containers for}
+
+  \item{portfolio}{}
+
+  \item{symbols}{}
+
+  \item{get.Symbols}{TRUE/FALSE, default TRUE:}
+
+  \item{init.Portf}{TRUE/FALSE, default TRUE:}
+
+  \item{init.Acct}{TRUE/FALSE, default TRUE:}
+
+  \item{init.Orders}{TRUE/FALSE, default TRUE:}
+
+  \item{unique}{TRUE/FALSE, default TRUE:}
+
+  \item{\dots}{any other passtrhrough parameters}
+}
+\description{
+  run standard and custom strategy initialization functions
+}
+\author{
+  Garrett See, Brian Peterson
+}
+

Added: pkg/quantstrat/man/rm.strat.Rd
===================================================================
--- pkg/quantstrat/man/rm.strat.Rd	                        (rev 0)
+++ pkg/quantstrat/man/rm.strat.Rd	2011-09-27 20:41:44 UTC (rev 803)
@@ -0,0 +1,15 @@
+\name{rm.strat}
+\alias{rm.strat}
+\title{Remove objects associated with a strategy}
+\usage{
+  rm.strat(name = "default")
+}
+\arguments{
+  \item{name}{name of the portfolio/account/order book to
+  clean up}
+}
+\description{
+  Remove the order_book, account, and portfolio of given
+  \code{name}.
+}
+

Added: pkg/quantstrat/man/updateStrategy.Rd
===================================================================
--- pkg/quantstrat/man/updateStrategy.Rd	                        (rev 0)
+++ pkg/quantstrat/man/updateStrategy.Rd	2011-09-27 20:41:44 UTC (rev 803)
@@ -0,0 +1,71 @@
+\name{updateStrategy}
+\alias{updateStrategy}
+\title{run standard and custom strategy wrapup functions such as updating portfolio, account, and ending equity}
+\usage{
+  updateStrategy(portfolio = "default", account =
+  portfolio, Symbols = NULL, Dates = NULL, Prices = NULL,
+  update.Portf = TRUE, update.Acct = TRUE, update.EndEq =
+  TRUE, showEq = TRUE, chart = TRUE, ...)
+}
+\arguments{
+  \item{portfolio}{string identifying a portfolio}
+
+  \item{account}{string identifying an account. Same as
+  \code{portfolio} by default}
+
+  \item{Symbols:}{character vector of names of symbols
+  whose portfolios will be updated}
+
+  \item{Dates}{optional xts-style ISO-8601 time range to
+  run updatePortf over, default NULL (will use times from
+  Prices)}
+
+  \item{Prices}{optional xts object containing prices and
+  timestamps to mark the book on, default NULL}
+
+  \item{update.Portf}{TRUE/FALSE if TRUE (default) a call
+  will be made to \code{updatePortf}}
+
+  \item{update.Acct}{TRUE/FALSE if TRUE (default) a call
+  will be made to \code{updateAcct}}
+
+  \item{update.EndEq}{TRUE/FALSE if TRUE (default) a call
+  will be made to \code{updateEndEq}}
+
+  \item{showEq}{TRUE/FALSE if TRUE (default) ending equity
+  will be printed to the screen}
+
+  \item{chart}{TRUE/FALSE if TRUE (default) a call will be
+  made to \code{chart.Posn}}
+
+  \item{\dots}{any other passthrough parameters}
+}
+\description{
+  \code{\link{updateStrategy}} will run a series of common
+  wrapup functions at the beginning of an
+  \code{\link{applyStrategy}} call.  This function allows
+  the user to add arbitrary wrapup functions to the
+  sequence.
+}
+\details{
+  These arbitrary functions will be added to the
+  \code{update} slot of the strategy object and when
+  \code{applyStrategy} is evaluated, the arbitrary wrapup
+  functions will be evaluated \emph{before} the
+  standardized functions.
+
+  For example, if you are working with high frequency data,
+  it would be common to \empgh{mark the book} on a lower
+  frequency, perhaps minutes, hours, or even days, rather
+  than tick.  A custom wrapup function could take your high
+  frequency data and transform it to lower frequency data
+  before the call to \code{\link{updatePortf}}.
+}
+\author{
+  Garrett See, Brian Peterson
+}
+\seealso{
+  \code{\link{updatePortf}}, \code{\link{updateAcct}},
+  \code{\link{updateEndEq}}, \code{\link{chart.Posn}}
+}
+



More information about the Blotter-commits mailing list