braverock / braverock/quantstrat

tradeGraphs will not render if more than two optimizing parameters are in a strategy

Open
#136 5 comments 0 reactions 1 assignee Claimed by @jaymon0703 View on GitHub
enhancement
Dominant language
R
Stars
310
Forks
121
PR merge metrics
No merged PRs in 30d

Description

### Description

Ran a simple optimization of an MACD example using `apply.paramset(.)`, with three paramsets: Fast MA, Slow MA, and Signal MA. When finished, I tried using `tradeGraphs(.)` on pairs of paramsets, but this results in an error saying `Aggregation function missing: defaulting to length`, and a plot showing a plane in the parameter space.

quantstrat version is 0.16.9.

Remark: I have verified that I can generate plots from `tradeGraphs(.)` in cases where there are exactly two paramsets in the model.

### Expected behavior

Expected to be able to view plots of performance measures vs two paramsets chosen from the three that are used in the optimization.

### Minimal, reproducible example

```r
library(quantstrat)
symbol <- 'SPY'
currency("USD")
stock(symbol, currency="USD", multiplier=1)

initDate <- '2000-12-31'
startDate <- '2001-01-01'
endDate <- '2021-03-31'
initEq <- 1000000 # $1M
shs <- 500

Sys.setenv(TZ="UTC")
getSymbols(symbol, from=startDate, to=endDate, index.class="POSIXct", adjust=TRUE)
etfData <- get(symbol)

maType <- "EMA"

stratName <- "macd.ema.opt"
portName <- "macd.ema.opt"
acctName <- "macd.ema.opt"
suppressWarnings(rm.strat(stratName)) # reset

# Distribution setup:
fastRange <- seq(10, 16, by=2)
slowRange <- seq(20, 32, by=4)
sigRange <- seq(5, 20, by=3)

initPortf(name=portName, symbols = symbol, initDate=initDate)
initAcct(name=acctName, portfolios=portName,
initDate=initDate, initEq=initEq)
initOrders(portfolio=portName, initDate=initDate)
strategy(name = stratName, store=TRUE)

add.indicator(strategy = stratName, name = "MACD",
arguments = list(x=quote(Cl(mktdata)),
nFast = 0, nSlow = 0, nSig = 0),
label='macd.osc')

add.signal(strategy = stratName, name="sigThreshold",
arguments=list(column="signal.macd.osc",relationship="gt",threshold=0,cross=TRUE),
label="signal.gt.zero")

add.signal(strategy = stratName, name="sigThreshold",
arguments=list(column="signal.macd.osc",relationship="lt",threshold=0,cross=TRUE),
label="signal.lt.zero")

add.rule(strategy = stratName, name='ruleSignal',
arguments = list(sigcol="signal.gt.zero", sigval=TRUE, orderqty=shs,
ordertype='market',orderside='long'),
type='enter',label='long_entry') # ,storefun=FALSE)

add.rule(strategy = stratName, name='ruleSignal',
arguments = list(sigcol="signal.lt.zero",sigval=TRUE,orderqty='all',
ordertype='market'),
type='exit',label='long_exit')

add.distribution(strategy = stratName,
paramset.label = "macd.emas",
component.type = "indicator",
component.label = "macd.osc",
variable = list( nFast = fastRange),
label = "macd.fast")

add.distribution(strategy = stratName,
paramset.label = "macd.emas",
component.type = "indicator",
component.label = "macd.osc",
variable = list(nSlow = slowRange),
label = "macd.slow")

add.distribution(strategy = stratName,
paramset.label = "macd.emas",
component.type = "indicator",
component.label = "macd.osc",
variable = list(nSig = sigRange),
label = "macd.sig")

results <- apply.paramset(strategy.st = stratName, paramset.label = "macd.emas",
portfolio.st=portName, account.st=acctName, nsamples=0)

library(rgl)
library(reshape2)

tradeGraphs(stats = results$tradeStats,
free.params = c("macd.fast","macd.sig"),
statistics = c("Profit.To.Max.Draw"))
```

### Session Info

```r
Aggregation function missing: defaulting to length
```
![image](https://user-images.githubusercontent.com/3461284/117910518-ee6ba980-b290-11eb-8ed6-73c41c93fb12.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.