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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 16 17:56:24 CET 2015


Author: bodanker
Date: 2015-11-16 17:56:24 +0100 (Mon, 16 Nov 2015)
New Revision: 1719

Modified:
   pkg/quantstrat/R/chart.forward.R
   pkg/quantstrat/R/chart.forward.training.R
   pkg/quantstrat/man/chart.forward.Rd
   pkg/quantstrat/man/chart.forward.training.Rd
Log:
Make chart.forward.* more robust

Attempt to ensure the audit.filename parameter is pointing to the
correct file generated by walk.forward(). Add file name pattern (as
generated by walk.forward) to the docs. Ensure file exists before
trying to load.

Remove the hard-coded portfolio string in chart.forward, and replace it
with the result portfolio (which doesn't end in a digit) from the
.audit environment.

Only select the first .audit$param.combo.nr, in case there is more than
one in the file.

Roxygenize docs.


Modified: pkg/quantstrat/R/chart.forward.R
===================================================================
--- pkg/quantstrat/R/chart.forward.R	2015-11-15 17:08:44 UTC (rev 1718)
+++ pkg/quantstrat/R/chart.forward.R	2015-11-16 16:56:24 UTC (rev 1719)
@@ -1,20 +1,25 @@
 #' Chart to analyse walk.forward() objective function
 #'
-#' @param audit.filename name of .audit environment file as produced by walk.forward()
+#' @param audit.filename name of .audit environment file as produced by walk.forward().
+#'        Filename will match pattern [audit.prefix].results.RData.
 #'
 #' @export
 
 chart.forward <- function(audit.filename)
 {
-    #.audit <- NULL
-    if(is.null(.audit)) stop ('You need to run a walk forward test first to create the .audit environment')
-  
-    if(!is.null(audit.filename))
-      load(audit.filename)
-    else 
-      stop('You need to provide an audit.filename.')
+    .audit <- NULL  # keep codetools happy
+    # ensure correct file written by walk.forward() is provided
+    if (!grepl("\\.results\\.RData$", audit.filename[1L])) {
+        stop("'audit.filename' should match pattern:\n  [audit.prefix].results.RData")
+    }
+    if (file.exists(audit.filename)) {
+        load(audit.filename)
+    } else {
+        stop("'audit.filename', ", audit.filename, " not found.")
+    }
 
-    # extract all portfolio names from the audit environment, except wfa portfolio
+    # extract all portfolio names from the audit environment,
+    # except result portfolio (which doesn't end with a digit)
     portfolios.st = ls(name=.audit, pattern='portfolio.*.[0-9]+')
     n <- length(portfolios.st)
 
@@ -33,8 +38,8 @@
         PL.xts <- cbind(PL.xts, R)
     }
     
-    # now for the result portfolio
-    portfolio.st <- 'portfolio.forex'
+    # now for the result portfolio (which doesn't end with a digit)
+    portfolio.st <- ls(name=.audit, pattern='portfolio.*[^.0-9]$')
     p <- getPortfolio(portfolio.st, envir=.audit)
     from <- index(p$summary[2])
     R <- cumsum(p$summary[paste(from, '/', sep=''),'Net.Trading.PL'])
@@ -52,6 +57,4 @@
     # set on=NA so it is drawn on a new panel
     p <- lines(Drawdowns.xts, col=c("blue", rep("grey", n-1)), on=NA, main="Drawdowns")
     print(p)
-
-    .audit <- NULL
 }

Modified: pkg/quantstrat/R/chart.forward.training.R
===================================================================
--- pkg/quantstrat/R/chart.forward.training.R	2015-11-15 17:08:44 UTC (rev 1718)
+++ pkg/quantstrat/R/chart.forward.training.R	2015-11-16 16:56:24 UTC (rev 1719)
@@ -1,16 +1,26 @@
 #' Chart to analyse walk.forward() objective function
 #'
-#' @param audit.filename name of .audit environment file as produced by walk.forward()
+#' @param audit.filename name of .audit environment file as produced by walk.forward().
+#'        Filename will match pattern [audit.prefix].[symbol].[start timestamp].[end timestamp].RData.
 #'
 #' @export
 
 chart.forward.training <- function(audit.filename)
 {
-    .audit <- NULL
+    .audit <- NULL  # keep codetools happy
+    # ensure correct file written by walk.forward() is provided
+    datePattern <- "[[:digit:]]{4}.[[:digit:]]{2}.[[:digit:]]{2}(.[[:digit:]]{2}.[[:digit:]]{2}.[[:digit:]]{2})?"
+    if (!grepl(paste0(datePattern, "\\.", datePattern, "\\.RData$"), audit.filename[1L])) {
+        stop("'audit.filename' should match pattern:\n  [audit.prefix].[symbol].[start timestamp].[end timestamp].RData.")
+    }
+    if (file.exists(audit.filename)) {
+        load(audit.filename)
+    } else {
+        stop("'audit.filename', ", audit.filename, " not found.")
+    }
 
-    load(audit.filename)
-
     # extract all portfolio names from the audit environment
+    # NB: training data only has portfolios that end in digits
     portfolios.st = ls(name=.audit, pattern='portfolio.*')
     n <- length(portfolios.st)
 
@@ -30,7 +40,7 @@
     }
     
     # .audit$param.combo.nr contains the rowname of the best portfolio
-    chosen.one <- .audit$param.combo.nr
+    chosen.one <- .audit$param.combo.nr[1L]
     chosen.portfolio.st = ls(name=.audit, pattern=glob2rx(paste('portfolio', '*', chosen.one, sep='.')))
     # add a column for the chosen portfolio, doubling it and
     # making it plot last, so it's not over-plotted by other portfolios
@@ -51,7 +61,4 @@
     # set on=NA so it is drawn on a new panel
     p <- lines(Drawdowns.xts, col=c("blue", rep("grey", n)), on=NA, main="Drawdowns")
     print(p)
-
-    .audit <- NULL
 }
-

Modified: pkg/quantstrat/man/chart.forward.Rd
===================================================================
--- pkg/quantstrat/man/chart.forward.Rd	2015-11-15 17:08:44 UTC (rev 1718)
+++ pkg/quantstrat/man/chart.forward.Rd	2015-11-16 16:56:24 UTC (rev 1719)
@@ -7,7 +7,8 @@
 chart.forward(audit.filename)
 }
 \arguments{
-\item{audit.filename}{name of .audit environment file as produced by walk.forward()}
+\item{audit.filename}{name of .audit environment file as produced by walk.forward()
+Filename will match pattern [audit.prefix].results.RData.}
 }
 \description{
 Chart to analyse walk.forward() objective function

Modified: pkg/quantstrat/man/chart.forward.training.Rd
===================================================================
--- pkg/quantstrat/man/chart.forward.training.Rd	2015-11-15 17:08:44 UTC (rev 1718)
+++ pkg/quantstrat/man/chart.forward.training.Rd	2015-11-16 16:56:24 UTC (rev 1719)
@@ -7,7 +7,8 @@
 chart.forward.training(audit.filename)
 }
 \arguments{
-\item{audit.filename}{name of .audit environment file as produced by walk.forward()}
+\item{audit.filename}{name of .audit environment file as produced by walk.forward().
+Filename will match pattern [audit.prefix].[symbol].[start timestamp].[end timestamp].RData.}
 }
 \description{
 Chart to analyse walk.forward() objective function



More information about the Blotter-commits mailing list