[R-SIG-Finance] demo("luxor.8.walk.forward") Error in if (!all(i <= 0)) stop("only zeros may be mixed with negative subscripts") : missing value where TRUE/FALSE needed

Richard Long rlong011 at verizon.net
Tue Jul 22 19:02:55 CEST 2014


Resend of html message

I've been trying to get the Luxor demo to work. I have 1 through 7 working and have modified to my needs and have working, but the walk.forward, which to me is key,
fails running the demo as delivered in the latest release:

demo("luxor.8.walk.forward")
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('account.st'=account.st, 'portfolio.st'=portfolio.st), audit.prefix='wfa', anchored=FALSE, verbose=TRUE)
Error in if (!all(i <= 0)) stop("only zeros may be mixed with negative subscripts") : missing value where TRUE/FALSE needed

I thought the error might be the results of the period being months and k.traing=3 and k.testing=1 and the data only having 10 days so 
I tried period= days. It got in to the WFA and then failed.

demo("luxor.8.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)
[1] "=== training WFA on 2002-10-21 00:30:00/2002-10-23 23:30:00"Error in walk.forward(strategy.st, paramset.label = "WFA", portfolio.st = portfolio.st, : 
 obj.func() returned empty resultIn addition: Warning message:In max(x$user.func$GBPUSD.DailyEndEq) : no non-missing arguments to max; returning -Inf

Using my own data with 6 months of data and period=months:

GBPUSD <- to.daily(readRDS("/dataIII/RData/gbpusd_2014-01-06v.rds"),OHLC=FALSE)[,c(1,2,3,4,9)]GBPUSD = align.time(GBPUSD, 86400)GBPUSD<-GBPUSD["2014-01-02 00:00:00/2014-06-30 00:00:00"]head(GBPUSD) Bid_Open Bid_High Bid_Low Bid_Close Vol2014-01-02 1.65501 1.65501 1.65501 1.65501 1.500002014-01-03 1.64427 1.64427 1.64426 1.64426 4.129992014-01-04 1.64269 1.64269 1.64269 1.64269 2.000002014-01-05 1.64171 1.64171 1.64171 1.64171 1.500002014-01-07 1.64129 1.64129 1.64124 1.64124 15.159992014-01-08 1.64234 1.64234 1.64232 1.64232 9.829982014-01-08 1.64234 1.64234 1.64232 1.64232 9.82998

I get:

### 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('account.st'=account.st, 'portfolio.st'=portfolio.st), audit.prefix='wfa', anchored=FALSE, verbose=TRUE)[1] "=== training WFA on 2014-01-02/2014-03-30"numValues: 15, numResults: 0, stopped: TRUEgot results for task 1numValues: 15, numResults: 1, stopped: TRUEreturning status FALSEgot results for task 2numValues: 15, numResults: 2, stopped: TRUEreturning status FALSEgot results for task 3numValues: 15, numResults: 3, stopped: TRUEreturning status FALSEgot results for task 4numValues: 15, numResults: 4, stopped: TRUEreturning status FALSEgot results for task 5numValues: 15, numResults: 5, stopped: TRUEreturning status FALSEgot results for task 6numValues: 15, numResults: 6, stopped: TRUEreturning status FALSEgot results for task 7numValues: 15, numResults: 7, stopped: TRUEreturning status FALSEgot results for task 8numValues: 15, numResults: 8, stopped: TRUEreturning status FALSEgot results for task 9numValues: 15, numResults: 9, stopped: TRUEreturning status FALSEgot results for task 10numValues: 15, numResults: 10, stopped: TRUEreturning status FALSEgot results for task 11numValues: 15, numResults: 11, stopped: TRUEreturning status FALSEgot results for task 12numValues: 15, numResults: 12, stopped: TRUEreturning status FALSEgot results for task 13numValues: 15, numResults: 13, stopped: TRUEreturning status FALSEgot results for task 14numValues: 15, numResults: 14, stopped: TRUEreturning status FALSEgot results for task 15numValues: 15, numResults: 15, stopped: TRUEfirst call to combine functionevaluating call object to combine results: fun(result.1, result.2, result.3, result.4, result.5, result.6, result.7, result.8, result.9, result.10, result.11, result.12, result.13, result.14, result.15)returning status TRUE[1] "=== testing param.combo 5 on 2014-04-01/2014-04-30" nFAST nSLOW5 9 42Error in runSum(x, n) : Invalid 'n'

The 1st wfa appears to work but it fails in the testing phase and I don't know if the problem is
in my data or my code.

This the demo code alterations highlighted:

#!/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"))
GBPUSD <- to.daily(readRDS("/dataIII/RData/gbpusd_2014-01-06v.rds"),OHLC=FALSE)[,c(1,2,3,4,9)]
GBPUSD = align.time(GBPUSD, 86400)
GBPUSD<-GBPUSD["2014-01-02 00:00:00/2014-06-30 00:00:00"]
head(GBPUSD)

### 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

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

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



More information about the R-SIG-Finance mailing list