braverock / braverock/quantstrat

[R-Forge #2658] error in pair trade demo

Open
#21 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
R
Stars
310
Forks
121
PR merge metrics
No merged PRs in 30d

Description

Submitted by: Robert Schmidt
Assigned to: Nobody
[R-Forge link](http://r-forge.r-project.org/tracker/index.php?func=detail&aid=2658&group_id=316&atid=1269)

First, a big thank you for this wonderful work. I'm in over my head now, LOL. I run R --vanilla in a terminal. Then require(quantstrat) and demo(pair_trade). Here is the output when it gets to applyStrategy:

> out1<-applyStrategy(strategy=pairStrat, portfolios=portfolio1.st)
> Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { :
> missing value where TRUE/FALSE needed
> traceback()
> 16: `[.xts`(data, , colNums[1])
> 15: data[, colNums[1]]
> 14: do.call(opr, list(data[, colNums[1]] + offset1, data[, colNums[2]] +
> offset2))
> 13: sigComparison(label = label, data = data, columns = columns[c(i,
> lng)], relationship = relationship, offset1 = offset1, offset2 = offset2)
> 12: diff(sigComparison(label = label, data = data, columns = columns[c(i,
> lng)], relationship = relationship, offset1 = offset1, offset2 = offset2))
> 11: withCallingHandlers(expr, warning = function(w) invokeRestart('muffleWarning'))
> 10: suppressWarnings(ret_sig | diff(sigComparison(label = label,
> data = data, columns = columns[c(i, lng)], relationship = relationship,
> offset1 = offset1, offset2 = offset2)) == 1)
> 9: function (label, data = mktdata, columns, relationship = c('gt',
> 'lt', 'eq', 'gte', 'lte'), offset1 = 0, offset2 = 0)
> {
> ret_sig = FALSE
> lng <- length(columns)
> for (i in 1:(lng - 1)) {
> ret_sig = suppressWarnings(ret_sig | diff(sigComparison(label = label,
> data = data, columns = columns[c(i, lng)], relationship = relationship,
> offset1 = offset1, offset2 = offset2)) == 1)
> }
> is.na(ret_sig) <- which(!ret_sig)
> colnames(ret_sig) <- label
> return(ret_sig)
> }(label = 'cross.up', data = mktdata, columns = c('Ratio', 'up'
> ), relationship = 'lt', offset1 = 0, offset2 = 0)
> 8: do.call(fun, .formals)
> 7: applySignals(strategy = strategy, mktdata = mktdata, sret$indicators,
> parameters = parameters, ...)
> 6: applyStrategy(strategy = pairStrat, portfolios = portfolio1.st) at pair_trade.R#189
> 5: eval(expr, envir, enclos)
> 4: eval(ei, envir)
> 3: withVisible(eval(ei, envir))
> 2: source(available, echo = echo, max.deparse.length = Inf, keep.source = TRUE)
> 1: demo(pair_trade)
> sessionInfo()
> R version 2.15.3 (2013-03-01)
> Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] quantstrat_0.7.7 foreach_1.4.0 blotter_0.8.13
[4] FinancialInstrument_1.1 quantmod_0.4-0 Defaults_1.1-1
[7] TTR_0.22-0 xts_0.9-3 zoo_1.7-9

loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_2.15.3 iterators_1.0.6 lattice_0.20-13 tools_2.15.3

The column labels for mktdata aren't matching the names in the sigComparison lines. I added label arguments to the add.indicator commands and changed them to match in the 'update' I posted to the message board. I hope this isn't a dumb newbie thing.

Best regards,

Rob

Followups:

Date: 2013-04-02 14:08
Sender: Brian Peterson
It's not you. We changed the way labels worked, as an earlier attempt was a step backwards, not forwards. Hopefully the new way is 'better' than the one that just overwrote the indicator columns entirely.I'll take a look and patch the pair_trade demo, but I'd be lying if I said it was a high priority.If you actually intended to trade pairs, you'd use a spreader. That would mean that in R, for research, you'd construct a single spread series, and backtest _on the spread_, rather than writing the logic to manual leg into the spread into your backtest. As such, I think that the pair_trade demo shows how to do some interesting things, but isn't a realistic representation of how you would typically research and trade a strategy like this.All that aside, I'll review the column labels and code there and patch, and probably refer to your R-SIG-Finance post.Thanks for the contributions to the community! Keep it up!Regards,Brian

Contributor guide

Open the contributing guide

Research direction

Start with demo(pair_trade) and pair_trade.R#189, then inspect the applyStrategy call and the mktdata column labels used by sigComparison. Re-run the demo after checking the indicator labels; done means the pair_trade demo reaches completion without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.