在使用自定义指标时,我正在寻找 Quantstrat 中的参数优化代码示例。我可以在网上找到的大多数示例都使用 SMA、MACD 和其他经典指标。这对我没有多大帮助,因为我主要希望使用自定义指标优化交易系统。
有没有人有相关的链接或材料?
更具体地说,我第一次尝试指标优化选项。我使用的指标是 MACD 森林的动量。我正在尝试评估该指标与通常的 MACD 交叉信号线的有效性。但我不想过多关注这个特定指标,因为我的目标是为我将使用的未来自定义指标提供可行的优化代码。
这是代码,数据是日内 EURUSD 报价(5 分钟蜡烛):
library(quantstrat)
Sys.setenv(TZ = "UTC")
currency(c('EUR', 'USD'))
exchange_rate(c('EURUSD'), tick_size=0.0001)
init_date <- "2018-01-07"
start_date <- "2018-01-08"
end_date <- "2018-01-08"
init_equity <- 1e8 # $100,000,000
adjustment <- FALSE
basic_symbols <- function() {
symbols <- c(
"EUR"
)
}
symbols <- basic_symbols()
portfolio.st <- "Port.Luxor.Opt"
account.st <- "Acct.Luxor.Opt"
strategy.st <- "Strat.Luxor.Opt"
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date,
initEq = init_equity)
initOrders(portfolio = portfolio.st,
symbols = symbols,
initDate = init_date)
strategy(strategy.st, store = TRUE)
fastMA_custom = 12
fastMA_custom2 = 12
slowMA_custom = 26
slowMA_custom2 = 26
signalMA_custom = 9
signalMA_custom2 = 9
maType="EMA"
MAforest = 3
forest <- function(x){
step1 <- EMA(x,fastMA_custom)
step2 <- EMA(x,slowMA_custom)
step3 <- step1-step2
step4 <- EMA(step3,signalMA_custom)
step5 <- step3-step4
return(step5)
}
smaforest <- function(x){
step1 <- EMA(x,fastMA_custom2)
step2 <- EMA(x,slowMA_custom2)
step3 <- step1-step2
step4 <- EMA(step3,signalMA_custom2)
step5 <- step3-step4
step6 <- EMA(step5,MAforest)
return(step6)
}
add.indicator(strategy = strategy.st,
name ="forest",
arguments = list(x=quote(Cl(mktdata))),
label="forest")
add.indicator(strategy=strategy.st,
name ="smaforest",
arguments = list(x=quote(Cl(mktdata))),
label="smaforest")
add.distribution(strategy.st,
paramset.label = "forestopt",
component.type = "indicator",
component.label = "forest",
variable = list(fastMA_custom= 8:14),
label = "fastMA_custom")
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("forest", "smaforest"),
relationship = "gte"),
label = "long")
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("forest", "smaforest"),
relationship = "lte"),
label = "short")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderqty = 100000,
ordertype = "market",
orderside = "long",
TxnFees = -1,
replace = FALSE),
type = "enter",
label = "EnterLONG")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderqty = -100000,
ordertype = "market",
orderside = "short",
replace = FALSE,
TxnFees = -1
),
type = "enter",
label = "EnterSHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
orderside = "long",
ordertype = "market",
orderqty = "all",
TxnFees = -1,
replace = TRUE),
type = "exit",
label = "Exit2SHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
orderside = "short",
ordertype = "market",
orderqty = "all",
TxnFees = -1,
replace = TRUE),
type = "exit",
label = "Exit2LONG")
addPosLimit(portfolio.st, 'EUR', timestamp=initDate, maxpos=500, minpos=0)
resultsopt <- apply.paramset(strategy.st,
paramset.label = "forestopt",
portfolio.st = portfolio.st,
account.st = account.st,
nsamples = 0)
没有优化部分的回测工作完美。这实际上是导致错误的优化位。
这是我得到的错误:
调用组合函数时出错:“有趣的simpleError(result.1,result.2,result.3,result.4,result.5,result.6,result.7):尝试选择少于一个元素”