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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 17 02:47:36 CET 2013


Author: opentrades
Date: 2013-01-17 02:47:36 +0100 (Thu, 17 Jan 2013)
New Revision: 1366

Modified:
   pkg/quantstrat/R/paramsets.R
   pkg/quantstrat/R/walk.forward.R
   pkg/quantstrat/man/apply.paramset.Rd
Log:
- added feature to apply.paramset() to store all portfolios and orderbooks in a file for later 
audit
- added audit.st parameter to apply.paramset() to specify filename for audit file
- added audit.prefix parameter to walk.forward() to specify a prefix for generation of 
filenames for each step, to be passed on to apply.paramset()



Modified: pkg/quantstrat/R/paramsets.R
===================================================================
--- pkg/quantstrat/R/paramsets.R	2013-01-16 23:24:38 UTC (rev 1365)
+++ pkg/quantstrat/R/paramsets.R	2013-01-17 01:47:36 UTC (rev 1366)
@@ -349,13 +349,14 @@
 #' @param user.func an optional user-supplied function to be run for each param.combo at the end, either on the slave or on the master (see calc)
 #' @param user.args user-supplied list of arguments for user.func
 #' @param calc 'slave' to run updatePortfolio() and tradesStats() on the slave and return all portfolios and orderbooks as a list: higher parallelization but more data transfer between master and slave; 'master' to have updatePortf() and tradeStats() run at the master and return all portfolios and orderbooks in the .blotter and .strategy environments resp: less parallelization but also less data transfer between slave and master; default is 'slave'
+#' @param audit.st optional filename for storage of audit data
 #' @param verbose return full information, in particular the .blotter environment, default FALSE
 #'
 #' @author Jan Humme
 #' @export
 #' @seealso \code{\link{add.constraint}}, \code{\link{add.constraint}}, \code{\link{delete.paramset}}
 
-apply.paramset <- function(strategy.st, paramset.label, portfolio.st, account.st, mktdata, nsamples=0, user.func=NULL, user.args=NULL, calc='slave', verbose=FALSE)
+apply.paramset <- function(strategy.st, paramset.label, portfolio.st, account.st, mktdata, nsamples=0, user.func=NULL, user.args=NULL, calc='slave', audit.st=NULL, verbose=FALSE)
 {
     must.have.args(match.call(), c('strategy.st', 'paramset.label', 'portfolio.st'))
 
@@ -377,6 +378,8 @@
     env.functions <- c('clone.portfolio', 'clone.orderbook', 'install.param.combo')
     env.instrument <- as.list(FinancialInstrument:::.instrument)
 
+    .audit <- new.env()
+
     combine <- function(...)
     {
         args <- list(...)
@@ -387,11 +390,11 @@
             r <- args[[i]]
 
             # move portfolio from slave returned list into .blotter environment
-            put.portfolio(r$portfolio.st, r$portfolio)
+            put.portfolio(r$portfolio.st, r$portfolio, envir=.audit)
             r$portfolio <- NULL
 
             # move orderbook from slave returned list into .strategy environment
-            put.orderbook(r$portfolio.st, r$orderbook)
+            put.orderbook(r$portfolio.st, r$orderbook, envir=.audit)
             r$orderbook <- NULL
 
             if(calc == 'master')
@@ -471,6 +474,9 @@
     results$distributions <- distributions
     results$constraints <- constraints
 
+    if(!is.null(audit.st))
+        save(.audit, file=paste(audit.st, 'RData', sep='.'))
+
     return(results)
 }
 

Modified: pkg/quantstrat/R/walk.forward.R
===================================================================
--- pkg/quantstrat/R/walk.forward.R	2013-01-16 23:24:38 UTC (rev 1365)
+++ pkg/quantstrat/R/walk.forward.R	2013-01-17 01:47:36 UTC (rev 1366)
@@ -35,6 +35,7 @@
 #' @param period the period unit, as a character string, eg. 'days' or 'months'
 #' @param k.training the number of periods to use for training, eg. '3' months
 #' @param nsamples the number of sample param.combos to draw from the paramset for training; 0 means all samples (see also apply.paramset)
+#' @param audit.prefix prefix to generate filenames for storage of audit data
 #' @param k.testing the number of periods to use for testing, eg. '1 month'
 #' @param obj.func a user provided function returning the best param.combo from the paramset, based on training results; defaults to 'max'
 #' @param obj.args a user provided argument to obj.func, defaults to quote(tradeStats.list$Net.Trading.PL)
@@ -49,8 +50,9 @@
 #' @export
 
 walk.forward <- function(strategy.st, paramset.label, portfolio.st, account.st,
-    period, k.training, nsamples=0, k.testing,
-    obj.func=function(x){which(x==max(x))}, obj.args=list(x=quote(tradeStats.list$Net.Trading.PL)),
+    period, k.training, nsamples=0, audit.prefix=NULL, k.testing,
+    obj.func=function(x){which(x==max(x))},
+    obj.args=list(x=quote(tradeStats.list$Net.Trading.PL)),
     ..., verbose=FALSE)
 {
     must.have.args(match.call(), c('portfolio.st', 'strategy.st', 'paramset.label', 'k.training'))
@@ -99,11 +101,14 @@
 
             print(paste('=== training', paramset.label, 'on', training.timespan))
 
+            if(!is.null(audit.prefix))
+                audit.st <- paste(audit.prefix, index(symbol[training.start]), index(symbol[training.end]), sep='.')
+
             # run backtests on training window
             result$apply.paramset <- apply.paramset(strategy.st=strategy.st, paramset.label=paramset.label,
                 portfolio.st=portfolio.st, account.st=account.st,
                 mktdata=symbol[training.timespan], nsamples=nsamples,
-                calc='slave', verbose=verbose, ...=...)
+                calc='slave', audit.st=audit.st, verbose=verbose, ...=...)
 
             tradeStats.list <- result$apply.paramset$tradeStats
 

Modified: pkg/quantstrat/man/apply.paramset.Rd
===================================================================
--- pkg/quantstrat/man/apply.paramset.Rd	2013-01-16 23:24:38 UTC (rev 1365)
+++ pkg/quantstrat/man/apply.paramset.Rd	2013-01-17 01:47:36 UTC (rev 1366)
@@ -4,7 +4,8 @@
 \usage{
   apply.paramset(strategy.st, paramset.label, portfolio.st,
     account.st, mktdata, nsamples = 0, user.func = NULL,
-    user.args = NULL, calc = "slave", verbose = FALSE)
+    user.args = NULL, calc = "slave", audit.st = NULL,
+    verbose = FALSE)
 }
 \arguments{
   \item{strategy.st}{the name of the strategy object}
@@ -39,6 +40,9 @@
   also less data transfer between slave and master; default
   is 'slave'}
 
+  \item{audit.st}{optional filename for storage of audit
+  data}
+
   \item{verbose}{return full information, in particular the
   .blotter environment, default FALSE}
 }



More information about the Blotter-commits mailing list