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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri May 10 16:40:03 CEST 2013


Author: opentrades
Date: 2013-05-10 16:40:03 +0200 (Fri, 10 May 2013)
New Revision: 1466

Added:
   pkg/quantstrat/demo/luxor.8.walk.forward.R
Modified:
   pkg/quantstrat/R/walk.forward.R
   pkg/quantstrat/demo/00Index
   pkg/quantstrat/man/walk.forward.Rd
Log:
- added "anchored" parameter to walk.forward
- included luxor.8.walk.forward.R in the demos



Modified: pkg/quantstrat/R/walk.forward.R
===================================================================
--- pkg/quantstrat/R/walk.forward.R	2013-05-10 13:51:22 UTC (rev 1465)
+++ pkg/quantstrat/R/walk.forward.R	2013-05-10 14:40:03 UTC (rev 1466)
@@ -39,6 +39,7 @@
 #' @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)
+#' @param anchored whether to use a fixed start for the training window (TRUE), or a sliding start (FALSE); defaults to FALSE
 #' @param include.insamples will optionally run a full backtest for each param.combo in the paramset, and add the resulting in-sample portfolios and orderbooks to the file '<prefix>.results.RData'; default TRUE
 #' @param ... optional parameters to pass to apply.paramset()
 #' @param verbose dumps a lot of info during the run if set to TRUE, defaults to FALSE
@@ -55,7 +56,7 @@
     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)),
-    include.insamples=TRUE,
+    anchored=FALSE, include.insamples=TRUE,
     ..., verbose=FALSE)
 {
     must.have.args(match.call(), c('portfolio.st', 'strategy.st', 'paramset.label', 'k.training'))
@@ -76,12 +77,16 @@
     total.start <- ep[1 + k.training] + 1
     total.timespan <- paste(index(symbol[total.start]), '', sep='/')
 
+    if(anchored)
+        training.start <- ep[1] + 1
+
     k <- 1; while(TRUE)
     {
         result <- list()
 
         # start and end of training window
-        training.start <- ep[k] + 1
+        if(!anchored)
+            training.start <- ep[k] + 1
         training.end   <- ep[k + k.training]
 
         # stop if training.end is beyond last data

Modified: pkg/quantstrat/demo/00Index
===================================================================
--- pkg/quantstrat/demo/00Index	2013-05-10 13:51:22 UTC (rev 1465)
+++ pkg/quantstrat/demo/00Index	2013-05-10 14:40:03 UTC (rev 1466)
@@ -17,6 +17,7 @@
 luxor.6.paramset.stoptrailing.R	Jaekle & Tomasini; Sections 3.5: paramset implementation for stop trailing optimization
 luxor.6.paramset.takeprofit.R	Jaekle & Tomasini; Sections 3.5: paramset implementation for take profit optimization
 luxor.7.exit+risk.R	Jaekle & Tomasini; Sections 3.5: running with stoploss and/or stoptrailing and/or takeprofit
+luxor.8.walk.forward.R	Jaekle & Tomasini; Sections 6: walk forward analysis
 luxor.getSymbols.R	Jaekle & Tomasini; reading symbols
 luxor.include.R	Jaekle & Tomasini; Sections constants
 luxor.sample.MAE.stoploss.R	Jaekle & Tomasini; sample MAE stoploss graph

Added: pkg/quantstrat/demo/luxor.8.walk.forward.R
===================================================================
--- pkg/quantstrat/demo/luxor.8.walk.forward.R	                        (rev 0)
+++ pkg/quantstrat/demo/luxor.8.walk.forward.R	2013-05-10 14:40:03 UTC (rev 1466)
@@ -0,0 +1,82 @@
+#!/usr/bin/Rscript --vanilla
+#
+# Jan Humme (@opentrades) - April 2013
+#
+# Tested and found to work correctly using blotter r1457
+#
+# After Jaekle & Tamasini: A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
+#
+# Paragraph 3.5: determination of appropriate exit and risk management
+
+source('luxor.include.R')
+initDate <- '2003-01-01'
+.from <- initDate
+.to <- '2003-12-31'
+
+source('luxor.getSymbols.R')
+
+### foreach and doMC
+
+require(foreach)
+require(doMC)
+registerDoMC(cores=8)
+
+### blotter
+
+initPortf(portfolio.st, symbols='GBPUSD', initDate=initDate, currency='USD')
+initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD', initEq=100000)
+
+### quantstrat
+
+require(quantstrat)
+
+initOrders(portfolio.st, initDate=initDate)
+
+load.strategy(strategy.st)
+
+enable.rule(strategy.st, 'chain', 'StopLoss')
+#enable.rule(strategy.st, 'chain', 'StopTrailing')
+enable.rule(strategy.st, 'chain', 'TakeProfit')
+
+addPosLimit(
+            portfolio=portfolio.st,
+            symbol='GBPUSD',
+            timestamp=initDate,
+            maxpos=.orderqty)
+
+### objective function
+
+ess <- function(a.st, p.st)
+{
+    require(robustbase, quietly=TRUE)
+    require(PerformanceAnalytics, quietly=TRUE)
+
+    portfolios.st <- ls(pos=.blotter, pattern=paste('portfolio', p.st, '[0-9]*',sep='.'))
+
+    pr <- PortfReturns(Account = a.st, Portfolios=portfolios.st)
+
+    my.es <- ES(R=pr, clean='boudt')
+
+    return(my.es)
+}
+
+my.obj.func <- function(x)
+{
+    #return(which(max(x$Net.Trading.PL) == x$Net.Trading.PL))
+    #return(which(max(x$GBPUSD) == x$GBPUSD))
+    return(which(max(x$tradeStats$Net.Trading.PL/x$user.func$GBPUSD) == x$tradeStats$Net.Trading.PL/x$user.func$GBPUSD))
+}
+
+### walk.forward
+
+r <- walk.forward(strategy.st, paramset.label='WFA', portfolio.st=portfolio.st, account.st=account.st, period='months', k.training=3, k.testing=1, obj.func=my.obj.func, obj.args=list(x=quote(result$apply.paramset)), user.func=ess, user.args=list('a.st'=account.st, 'p.st'=portfolio.st), audit.prefix='wfa.ples', anchored=FALSE, verbose=TRUE)
+
+### analyse
+
+pdf(paste('GBPUSD', .from, .to, 'pdf', sep='.'))
+chart.Posn(portfolio.st)
+dev.off()
+
+ts <- tradeStats(portfolio.st)
+save(ts, file=paste('GBPUSD', .from, .to, 'RData', sep='.'))
+


Property changes on: pkg/quantstrat/demo/luxor.8.walk.forward.R
___________________________________________________________________
Added: svn:executable
   + *

Modified: pkg/quantstrat/man/walk.forward.Rd
===================================================================
--- pkg/quantstrat/man/walk.forward.Rd	2013-05-10 13:51:22 UTC (rev 1465)
+++ pkg/quantstrat/man/walk.forward.Rd	2013-05-10 14:40:03 UTC (rev 1466)
@@ -7,7 +7,8 @@
     audit.prefix = NULL, k.testing,
     obj.func = function(x) {     which(x == max(x)) },
     obj.args = list(x = quote(tradeStats.list$Net.Trading.PL)),
-    include.insamples = TRUE, ..., verbose = FALSE)
+    anchored = FALSE, include.insamples = TRUE, ...,
+    verbose = FALSE)
 }
 \arguments{
   \item{portfolio.st}{the name of the portfolio object}
@@ -51,6 +52,10 @@
   \item{obj.args}{a user provided argument to obj.func,
   defaults to quote(tradeStats.list$Net.Trading.PL)}
 
+  \item{anchored}{whether to use a fixed start for the
+  training window (TRUE), or a sliding start (FALSE);
+  defaults to FALSE}
+
   \item{include.insamples}{will optionally run a full
   backtest for each param.combo in the paramset, and add
   the resulting in-sample portfolios and orderbooks to the



More information about the Blotter-commits mailing list