SCM

SCM Repository

[blotter] View of /pkg/quantstrat/demo/luxor.8.walk.forward.R
ViewVC logotype

View of /pkg/quantstrat/demo/luxor.8.walk.forward.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1713 - (download) (annotate)
Wed Nov 11 19:49:55 2015 UTC (3 years, 7 months ago) by bodanker
File size: 2954 byte(s)
Get luxor.8 closer to running

It seems unlikely this demo ever worked as committed. Even after
ensuring robustbase and PerformanceAnalytics are installed, changing
period="days" (since there isn't enough data for period="months"), and
running the luxor.5 demo before running luxor.8, there's still an error
caused by an attempt to calculate a 40+ period SMA on ~20 observations.

I'm not sure how to fix that last issue, but these seem like enough
changes for one commit. :)

See R-Forge issue #6258.

#!/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.7 walk forward analysis

require(quantstrat)

source(paste0(path.package("quantstrat"),"/demo/luxor.include.R"))
source(paste0(path.package("quantstrat"),"/demo/luxor.getSymbols.R"))
source(paste0(path.package("quantstrat"),"/demo/luxor.5.strategy.ordersets.R"))

### foreach and doMC

require(foreach)
require(doMC)
registerDoMC(cores=8)

### robustbase and PerformanceAnalytics

if (!requireNamespace("robustbase", quietly=TRUE))
  stop("package 'robustbase' required, but not installed")
if (!requireNamespace("PerformanceAnalytics", quietly=TRUE))
  stop("package 'PerformanceAnalytics' required, but not installed")

### blotter

initPortf(portfolio.st, symbols='GBPUSD', initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD', initEq=100000)

### 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(account.st, portfolio.st)
{
    require(robustbase, quietly=TRUE)
    require(PerformanceAnalytics, quietly=TRUE)

    portfolios.st <- ls(pos=.blotter, pattern=paste('portfolio', portfolio.st, '[0-9]*',sep='.'))
    pr <- PortfReturns(Account = account.st, Portfolios=portfolios.st)

    my.es <- ES(R=pr, clean='boudt')

    return(my.es)
}

my.obj.func <- function(x)
{
    # 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='days',
                  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

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='.'))


root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge