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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 13 21:30:22 CEST 2013


Author: opentrades
Date: 2013-05-13 21:30:22 +0200 (Mon, 13 May 2013)
New Revision: 1467

Added:
   pkg/quantstrat/R/chart.forward.R
   pkg/quantstrat/man/chart.forward.Rd
Removed:
   pkg/quantstrat/R/chart.forward.testing.R
   pkg/quantstrat/man/chart.forward.testing.Rd
Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/NAMESPACE
   pkg/quantstrat/demo/luxor.6.paramset.takeprofit.R
   pkg/quantstrat/demo/luxor.7.exit+risk.R
   pkg/quantstrat/demo/luxor.8.walk.forward.R
   pkg/quantstrat/demo/luxor.include.R
Log:
- chart.forward() improvements
- several small luxor improvements




Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/DESCRIPTION	2013-05-13 19:30:22 UTC (rev 1467)
@@ -22,7 +22,6 @@
 ByteCompile: TRUE
 Collate:
     'applyStrategy.rebalancing.R'
-    'chart.forward.testing.R'
     'chart.forward.training.R'
     'indicators.R'
     'initialize.R'
@@ -43,3 +42,4 @@
     'walk.forward.R'
     'wrapup.R'
     'tradeOrderStats.R'
+    'chart.forward.R'

Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/NAMESPACE	2013-05-13 19:30:22 UTC (rev 1467)
@@ -13,8 +13,8 @@
 export(applySignals)
 export(applyStrategy.rebalancing)
 export(applyStrategy)
-export(chart.forward.testing)
 export(chart.forward.training)
+export(chart.forward)
 export(delete.paramset)
 export(enable.rule)
 export(getOrderBook)

Copied: pkg/quantstrat/R/chart.forward.R (from rev 1466, pkg/quantstrat/R/chart.forward.testing.R)
===================================================================
--- pkg/quantstrat/R/chart.forward.R	                        (rev 0)
+++ pkg/quantstrat/R/chart.forward.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -0,0 +1,64 @@
+#' Chart to analyse walk.forward() objective function
+#'
+#' @param audit.filename name of .audit environment file as produced by walk.forward()
+#'
+#' @export
+
+chart.forward <- function(audit.filename)
+{
+    if(!require(xtsExtra, quietly=TRUE)) stop('The "xtsExtra" package is required to use this function')
+
+    .audit <- NULL
+
+    load(audit.filename)
+
+    # extract all portfolio names from the audit environment, except wfa portfolio
+    portfolios.st = ls(name=.audit, pattern='portfolio.*.[0-9]+')
+    n <- length(portfolios.st)
+
+    # calculate Net.Trading.PL for each portfolio, one xts col per portfolio
+    PL.xts <- xts()
+    for(portfolio.st in portfolios.st)
+    {
+        p <- getPortfolio(portfolio.st, envir=.audit)
+        
+    	from <- index(p$summary[2])
+    	
+        #R <- cumsum(p$summary['2004-01-01/','Net.Trading.PL'])
+        R <- cumsum(p$summary[paste(from, '/', sep=''),'Net.Trading.PL'])
+        names(R) <- portfolio.st
+        
+        PL.xts <- cbind(PL.xts, R)
+    }
+    
+    # now for the result portfolio
+    portfolio.st <- 'portfolio.forex'
+    p <- getPortfolio(portfolio.st, envir=.audit)
+    from <- index(p$summary[2])
+    R <- cumsum(p$summary[paste(from, '/', sep=''),'Net.Trading.PL'])
+    names(R) <- portfolio.st
+
+    #PL.xts <- na.locf(PL.xts)
+    PL.xts <- na.locf(cbind(PL.xts, R))
+
+    # add drawdown columns for all portfolio columns
+    CumMax <- cummax(PL.xts)
+    Drawdowns.xts <- -(CumMax - PL.xts)
+    data.to.plot <- as.xts(cbind(PL.xts, Drawdowns.xts))
+    
+    # now plot it
+    dev.new()
+    plot.xts(
+        data.to.plot,
+        screens=rep(1:2,each=n+1),
+        col=c(rep('grey',n), 'blue'),
+        minor.ticks=FALSE,
+        main=NA
+    )
+    title(
+        main='Walk Forward Analysis',
+        sub=audit.filename
+    )
+    
+    .audit <- NULL
+}

Deleted: pkg/quantstrat/R/chart.forward.testing.R
===================================================================
--- pkg/quantstrat/R/chart.forward.testing.R	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/R/chart.forward.testing.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -1,63 +0,0 @@
-#' Chart to analyse walk.forward() objective function
-#'
-#' @param audit.filename name of .audit environment file as produced by walk.forward()
-#'
-#' @export
-
-chart.forward.testing <- function(audit.filename)
-{
-    if(!require(xtsExtra, quietly=TRUE)) stop('The "xtsExtra" package is required to use this function')
-
-    .audit <- NULL
-
-    load(audit.filename)
-
-    # extract all portfolio names from the audit environment
-    portfolios.st = ls(name=.audit, pattern='portfolio.*')
-    n <- length(portfolios.st)
-
-    # calculate Net.Trading.PL for each portfolio, one xts col per portfolio
-    PL.xts <- xts()
-    for(portfolio.st in portfolios.st)
-    {
-        p <- getPortfolio(portfolio.st, envir=.audit)
-        
-    	from <- index(p$summary[2])
-    	
-        #R <- cumsum(p$summary['2004-01-01/','Net.Trading.PL'])
-        R <- cumsum(p$summary[paste(from, '/', sep=''),'Net.Trading.PL'])
-        names(R) <- portfolio.st
-        
-        PL.xts <- cbind(PL.xts, R)
-    }
-    
-    # add a column for the chosen portfolio, doubling it
-    chosen.one <- .audit$param.combo.nr
-    testing.portfolio.st <- ls(env=.audit, pattern=glob2rx(paste('portfolio', '*', chosen.one, sep='.')))
-
-    R <- PL.xts[,testing.portfolio.st]
-    PL.xts <- cbind(PL.xts, R)
-    
-    PL.xts <- na.locf(PL.xts)
-
-    # add drawdown columns for all portfolio columns
-    CumMax <- cummax(PL.xts)
-    Drawdowns.xts <- -(CumMax - PL.xts)
-    data.to.plot <- as.xts(cbind(PL.xts, Drawdowns.xts))
-    
-    # now plot it
-    dev.new()
-    plot.xts(
-        data.to.plot,
-        screens=rep(1:2,each=n+1),
-        col=c(rep('grey',n), 'blue'),
-        minor.ticks=FALSE,
-        main=NA
-    )
-    title(
-        main='Walk Forward Analysis',
-        sub=audit.filename
-    )
-    
-    .audit <- NULL
-}

Modified: pkg/quantstrat/demo/luxor.6.paramset.takeprofit.R
===================================================================
--- pkg/quantstrat/demo/luxor.6.paramset.takeprofit.R	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/demo/luxor.6.paramset.takeprofit.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -44,7 +44,7 @@
 ### BEGIN uncomment lines to activate StopLoss and/or StopTrailing and/or TakeProfit rules
 
 enable.rule('luxor', 'chain', 'StopLoss')
-enable.rule('luxor', 'chain', 'StopTrailing')
+#enable.rule('luxor', 'chain', 'StopTrailing')
 enable.rule('luxor', 'chain', 'TakeProfit')
 
 ### END uncomment lines to activate StopLoss and/or StopTrailing and/or TakeProfit rules

Modified: pkg/quantstrat/demo/luxor.7.exit+risk.R
===================================================================
--- pkg/quantstrat/demo/luxor.7.exit+risk.R	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/demo/luxor.7.exit+risk.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -34,7 +34,7 @@
 
 ### BEGIN uncomment lines to activate StopLoss and/or StopTrailing and/or TakeProfit rules
 
-enable.rule('luxor', 'chain', 'StopLoss')
+#enable.rule('luxor', 'chain', 'StopLoss')
 #enable.rule('luxor', 'chain', 'StopTrailing')
 #enable.rule('luxor', 'chain', 'TakeProfit')
 

Modified: pkg/quantstrat/demo/luxor.8.walk.forward.R
===================================================================
--- pkg/quantstrat/demo/luxor.8.walk.forward.R	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/demo/luxor.8.walk.forward.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -6,13 +6,9 @@
 #
 # 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
+# Paragraph 3.7 walk forward analysis
 
 source('luxor.include.R')
-initDate <- '2003-01-01'
-.from <- initDate
-.to <- '2003-12-31'
-
 source('luxor.getSymbols.R')
 
 ### foreach and doMC
@@ -46,15 +42,14 @@
 
 ### objective function
 
-ess <- function(a.st, p.st)
+ess <- function(account.st, portfolio.st)
 {
     require(robustbase, quietly=TRUE)
     require(PerformanceAnalytics, quietly=TRUE)
 
-    portfolios.st <- ls(pos=.blotter, pattern=paste('portfolio', p.st, '[0-9]*',sep='.'))
+    portfolios.st <- ls(pos=.blotter, pattern=paste('portfolio', portfolio.st, '[0-9]*',sep='.'))
+    pr <- PortfReturns(Account = account.st, Portfolios=portfolios.st)
 
-    pr <- PortfReturns(Account = a.st, Portfolios=portfolios.st)
-
     my.es <- ES(R=pr, clean='boudt')
 
     return(my.es)
@@ -62,14 +57,18 @@
 
 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))
+    # pick one of the following objective functions (uncomment)
+
+    #return(max(x$tradeStats$Max.Drawdown) == x$tradeStats$Max.Drawdown)
+
+    #return(max(x$tradeStats$Net.Trading.PL) == x$tradeStats$Net.Trading.PL)
+
+    return(max(x$user.func$GBPUSD.DailyEndEq) == x$user.func$GBPUSD.DailyEndEq)
 }
 
 ### 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)
+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('account.st'=account.st, 'portfolio.st'=portfolio.st), audit.prefix='wfa', anchored=FALSE, verbose=TRUE)
 
 ### analyse
 

Modified: pkg/quantstrat/demo/luxor.include.R
===================================================================
--- pkg/quantstrat/demo/luxor.include.R	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/demo/luxor.include.R	2013-05-13 19:30:22 UTC (rev 1467)
@@ -8,13 +8,11 @@
 ###
 
 initDate = '2002-10-21'
-#initDate = '2003-01-01'
 
 .from=initDate
 
-#.to='2008-07-04'
 .to='2002-10-31'
-#.to='2003-07-31'
+.to='2008-07-04'
 
 ###
 
@@ -35,11 +33,9 @@
 .FastSMA = (1:20)
 .SlowSMA = (30:80)
 
-#.StopLoss = seq(0.02, 0.48, length.out=24)/100
-#.StopLoss = seq(0.01, 0.24, length.out=24)/100
-.StopLoss = seq(0.002, 0.048, length.out=24)/100
-.StopTrailing = seq(0.05, 0.4, length.out=8)/100
-.TakeProfit = seq(0.1, 1.0, length.out=10)/100
+.StopLoss = seq(0.05, 2.4, length.out=48)/100
+.StopTrailing = seq(0.05, 2.4, length.out=48)/100
+.TakeProfit = seq(0.1, 4.8, length.out=48)/100
 
 .FastWFA = c(1, 3, 5, 7, 9)
 .SlowWFA = c(42, 44, 46)
@@ -59,10 +55,10 @@
 .fast = 6
 .slow = 44
 
-#.timespan = 'T08:00/T12:00'
+#.timespan = 'T09:00/T13:00'
 .timespan = 'T00:00/T23:59'
 
-.stoploss <- 0.0025
-.stoptrailing <- 0.008
-.takeprofit <- 0.019
+.stoploss <- 0.40/100
+.stoptrailing <- 0.8/100
+.takeprofit <- 2.0/100
 

Added: pkg/quantstrat/man/chart.forward.Rd
===================================================================
--- pkg/quantstrat/man/chart.forward.Rd	                        (rev 0)
+++ pkg/quantstrat/man/chart.forward.Rd	2013-05-13 19:30:22 UTC (rev 1467)
@@ -0,0 +1,14 @@
+\name{chart.forward}
+\alias{chart.forward}
+\title{Chart to analyse walk.forward() objective function}
+\usage{
+  chart.forward(audit.filename)
+}
+\arguments{
+  \item{audit.filename}{name of .audit environment file as
+  produced by walk.forward()}
+}
+\description{
+  Chart to analyse walk.forward() objective function
+}
+

Deleted: pkg/quantstrat/man/chart.forward.testing.Rd
===================================================================
--- pkg/quantstrat/man/chart.forward.testing.Rd	2013-05-10 14:40:03 UTC (rev 1466)
+++ pkg/quantstrat/man/chart.forward.testing.Rd	2013-05-13 19:30:22 UTC (rev 1467)
@@ -1,14 +0,0 @@
-\name{chart.forward.testing}
-\alias{chart.forward.testing}
-\title{Chart to analyse walk.forward() objective function}
-\usage{
-  chart.forward.testing(audit.filename)
-}
-\arguments{
-  \item{audit.filename}{name of .audit environment file as
-  produced by walk.forward()}
-}
-\description{
-  Chart to analyse walk.forward() objective function
-}
-



More information about the Blotter-commits mailing list