[Blotter-commits] r1308 - pkg/quantstrat/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 20 01:18:42 CET 2012


Author: opentrades
Date: 2012-12-20 01:18:42 +0100 (Thu, 20 Dec 2012)
New Revision: 1308

Modified:
   pkg/quantstrat/R/walk.forward.R
Log:
verbose flag added for print



Modified: pkg/quantstrat/R/walk.forward.R
===================================================================
--- pkg/quantstrat/R/walk.forward.R	2012-12-19 23:30:05 UTC (rev 1307)
+++ pkg/quantstrat/R/walk.forward.R	2012-12-20 00:18:42 UTC (rev 1308)
@@ -17,19 +17,23 @@
 
 max.Net.Trading.PL <- function(tradeStats.list)
 {
-	which(max(tradeStats.list$Net.Trading.PL) == tradeStats.list$Net.Trading.PL)
+    which(max(tradeStats.list$Net.Trading.PL) == tradeStats.list$Net.Trading.PL)
 }
 
 ### exported functions ############################################################
 
-#' Rolling walk forward analysis
+#' Rolling Walk Forward Analysis
 #' 
-#' A wrapper for apply.paramset() and applyStrategy(), implementing a Rolling Walk Forward Analysis (WFA). It executes a strategy on a portfolio, while
+#' A wrapper for apply.paramset() and applyStrategy(), implementing a Rolling Walk Forward Analysis (WFA).
+#'
+#' walk.forward executes a strategy on a portfolio, while
 #' rolling a re-optimization of one of the strategies parameter sets during a specified time period (training window), then selecting an optimal
 #' parameter combination from the parameter set using an objective function, then applying the selected parameter combo to the next out-of-sample
 #' time period immediately following the training window (testing window). Once completed,
 #' the training window is shifted forward by a time period equal to the testing window size, and the process is repeated. 
-#' WFA stops when there are insufficient data left for a full testing window. For a complete description, see Jaekle&Tomasini chapter 6.
+#' WFA stops when there are insufficient data left for a full testing window.
+#'
+#' For a complete description, see Jaekle&Tomasini chapter 6.
 #' 
 #' @param portfolio.st the name of the portfolio object
 #' @param strategy.st the name of the strategy object
@@ -41,9 +45,9 @@
 #' @param objective a user provided function returning the best param.combo from the paramset, based on training results; a default function is provided that returns the number of the param.combo that brings the highest Net.Trading.PL
 #' @param verbose dumps a lot of info during the run if set to TRUE, defaults to FALSE
 #'
-#' @return a list consisting of a slot containing detailed results for each training + testing period, as well as the results of tradeStats() on the portfolio for the entire WFA over all testing periods
+#' @return a list consisting of a slot containing detailed results for each training + testing period, as well as the portfolio and the tradeStats() for the portfolio
 #'
-#' @seealso applyStrategy apply.paramset endpoints tradeStats
+#' @seealso \code{\link{applyStrategy}} \code{\link{apply.paramset}} \code{\link{endpoints}} \code{\link{tradeStats}}
 #'
 #' @examples
 #' res <- walk.forward(strategy.st, paramset.label='SMA', portfolio.st=portfolio.st, on='months', k.training=3, k.testing=1, verbose=FALSE)
@@ -54,84 +58,84 @@
 
 walk.forward <- function(portfolio.st, strategy.st, paramset.label, period, k.training, nsamples=0, k.testing, objective=max.Net.Trading.PL, verbose=FALSE)
 {
-	warning('walk.forward() is still under development! expect changes in arguments and results at any time JH')
+    warning('walk.forward() is still under development! expect changes in arguments and results at any time JH')
 
-	must.have.args(match.call(), c('portfolio.st', 'strategy.st', 'paramset.label', 'k.training'))
+    must.have.args(match.call(), c('portfolio.st', 'strategy.st', 'paramset.label', 'k.training'))
 
-	strategy <- must.be.strategy(strategy.st)
-	must.be.paramset(strategy, paramset.label)
+    strategy <- must.be.strategy(strategy.st)
+    must.be.paramset(strategy, paramset.label)
 
-	portfolio <- getPortfolio(portfolio.st)
+    portfolio <- getPortfolio(portfolio.st)
 
-	results <- list()
+    results <- list()
 
-	for(symbol.st in names(portfolio$symbols))
-	{
-		symbol <- get(symbol.st)
+    for(symbol.st in names(portfolio$symbols))
+    {
+        symbol <- get(symbol.st)
 
-		ep <- endpoints(symbol, on=period)
+        ep <- endpoints(symbol, on=period)
 
-		k <- 1; while(TRUE)
-		{
-			result <- list()
+        k <- 1; while(TRUE)
+        {
+            result <- list()
 
-			training.start <- ep[k] + 1
-			training.end   <- ep[k + k.training]
+            training.start <- ep[k] + 1
+            training.end   <- ep[k + k.training]
 
-			if(is.na(training.end))
-				break
+            if(is.na(training.end))
+                break
 
             training.timespan <- paste(index(symbol[training.start]), index(symbol[training.end]), sep='/')
 
-			result$training.timespan <- training.timespan
+            result$training.timespan <- training.timespan
 
-			if(verbose) print(paste('=== training', paramset.label, 'on', training.timespan))
+            if(verbose) print(paste('=== training', paramset.label, 'on', training.timespan))
 
-			result$apply.paramset <- apply.paramset(strategy.st=strategy.st, paramset.label=paramset.label,
+            result$apply.paramset <- apply.paramset(strategy.st=strategy.st, paramset.label=paramset.label,
                 portfolio.st=portfolio.st, mktdata=symbol[training.timespan], nsamples=nsamples, verbose=verbose)
 
-			if(missing(k.testing) || k.testing==0)
-			{
+            if(missing(k.testing) || k.testing==0)
+            {
                 k <- k + 1
             }
             else
             {
-				if(!is.function(objective))
-					stop(paste(objective, 'unknown objective function', sep=': '))
+                if(!is.function(objective))
+                    stop(paste(objective, 'unknown objective function', sep=': '))
 
-				testing.start <- ep[k + k.training] + 1
-				testing.end   <- ep[k + k.training + k.testing]
+                testing.start <- ep[k + k.training] + 1
+                testing.end   <- ep[k + k.training + k.testing]
 
-				if(is.na(testing.end))
-					break
+                if(is.na(testing.end))
+                    break
 
                 testing.timespan <- paste(index(symbol[testing.start]), index(symbol[testing.end]), sep='/')
 
                 tradeStats.list <- result$apply.paramset$tradeStats
 
-				param.combo.nr <- do.call(objective, list('tradeStats.list'=tradeStats.list))
-				param.combo <- tradeStats.list[param.combo.nr, 1:grep('Portfolio', names(tradeStats.list)) - 1]
+                param.combo.nr <- do.call(objective, list('tradeStats.list'=tradeStats.list))
+                param.combo <- tradeStats.list[param.combo.nr, 1:grep('Portfolio', names(tradeStats.list)) - 1]
 
-				strategy <- quantstrat:::install.param.combo(strategy, param.combo, paramset.label)
+                strategy <- quantstrat:::install.param.combo(strategy, param.combo, paramset.label)
 
                 result$testing.timespan <- testing.timespan
-				result$param.combo.nr <- param.combo.nr
-				result$param.combo <- param.combo
-				result$strategy <- strategy
+                result$param.combo.nr <- param.combo.nr
+                result$param.combo <- param.combo
+                result$strategy <- strategy
 
-				print(paste('--- testing param.combo', param.combo.nr, 'on', testing.timespan))
+                if(verbose) print(paste('=== testing param.combo', param.combo.nr, 'on', testing.timespan))
 
-				applyStrategy(strategy, portfolios=portfolio.st, mktdata=symbol[testing.timespan])
-			}
-			results[[k]] <- result
+                applyStrategy(strategy, portfolios=portfolio.st, mktdata=symbol[testing.timespan])
+            }
+            results[[k]] <- result
 
-			k <- k + k.testing
-		}
-	}
-	updatePortf(portfolio.st, Dates=paste('::',as.Date(Sys.time()),sep=''))
+            k <- k + k.testing
+        }
+    }
+    updatePortf(portfolio.st, Dates=paste('::',as.Date(Sys.time()),sep=''))
 
-	results$portfolio <- portfolio
-	results$tradeStats <- tradeStats(portfolio.st)
+    results$portfolio <- portfolio
+    results$tradeStats <- tradeStats(portfolio.st)
 
-	return(results)
+    return(results)
 } 



More information about the Blotter-commits mailing list