Rquantstrat apply.paramset和自定义指标
你好!无法确定如何使用apply.paramset函数运行自定义指标和“ sigFormula”规则。 如果将任何自定义指标添加到测试中,它将返回错误。在没有优化的情况下,它适用于applyStrategy。
返回的错误是:
系统: quantstrat_0.16.7 R版本3.5.3(2019-03-11) 平台:x86_64-w64-mingw32 / x64(64位)
测试代码:
library(quantmod)
library(quantstrat)
library(parallel)
if( Sys.info()['sysname'] == "Windows") {
library(doParallel)
registerDoParallel(cores=detectCores())
} else {
library(doMC)
registerDoMC(cores=detectCores())
}
Sys.setenv(TZ = "UTC")
portfolio.st <- "Port.My"
account.st <- "acc.My"
strategy.st <- "Strat.My"
init_date <- '2011-12-31'
start_date = '2012-01-01'
end_date = '2020-10-02'
adjustment <- TRUE
init_equity <- 100
currency('USD')
##########################################################
symbols <- c("AAPL")
pv <- list(
source = source,account_equity = init_equity,init_date = init_date,start_date = start_date,end_date = end_date,adjust = adjustment
)
getSymbols(symbols,src = "yahoo",index.class = c("POSIXt","POSIXct"),from = pv$start_date,to = pv$end_date,adjust = pv$adjust)
for(symbol in symbols) {
stock(symbol,currency = "USD",multiplier = 1)
x <- get(symbol)
tformat(x) <- "%Y-%m-%d"
colnames(x) <- gsub("x",symbol,colnames(x))
assign(symbol,x)
# Set pv$init_date to one day prior to earliest date in all of symbols
if(pv$init_date > min(index(x))) pv$init_date = min(index(x)) - 86400
rm(x)
}
##########################################################
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,initDate = init_date)
strategy(strategy.st,store = TRUE)
##########################################################
#TTR SMA copy for use case
#SMA function name and column name replaced
mySMA <- function (x,n = 10)
{
ma <- runmean(x,n)
if (!is.null(dim(ma))) {
colnames(ma) <- "XSMA"
}
return(ma)
}
add.indicator(strategy = strategy.st,name = "mySMA",arguments = list(x = quote(Cl(mktdata)),n = 20),label = "NFAST"
)
add.indicator(strategy = strategy.st,n = 50),label = "NSLW")
#this way it doesn't work with apply.paramset.
add.signal(strategy = strategy.st,name="sigFormula",arguments = list(columns = c("XSMA.NFAST","XSMA.NSLW"),#only works with XSMA. prefix
formula = "(XSMA.NFAST > XSMA.NSLW )",cross=TRUE),label = "buy")
## This way it works with apply.paramset!
# add.signal(strategy = strategy.st,# name="sigCrossover",# arguments = list(columns = c("NFAST","NSLW"),# relationship = "gt"),# label = "buy")
add.signal(strategy = strategy.st,name="sigCrossover",arguments = list(columns = c("NFAST",relationship = "lt"),label = "sell")
##DEBUG SIGNALS
out_sig_1 <- applySignals(strategy=strategy.st,mktdata=applyIndicators(strategy.st,mktdata=get(symbols)))
#print(summary(out_sig_1))
#####################################################################
add.rule(strategy = strategy.st,name = "ruleSignal",arguments = list(sigcol = "buy",sigval = TRUE,orderqty = 1,ordertype = "market",orderside = "long",prefer = "Open",TxnFees = 0,orderset = "ocolong",replace = FALSE),type = "enter",label = "EnterLONG")
add.rule(strategy.st,arguments = list(sigcol = "sell",orderqty = "all",replace = FALSE
),type = "exit",label = "TargetReached")
#for distribution
.fastSMAd <- (c(6,9))
.slowSMAd <- (c(200))
.nsamples <- 0
add.distribution(strategy.st,paramset.label = "mySMA",component.type = "indicator",component.label = "NFAST",variable = list(n = .fastSMAd),label = "distFAST")
#
add.distribution(strategy.st,component.label = "NSLW",variable = list(n = .slowSMAd),label = "distSLW")
#try distributed
results <- apply.paramset(strategy.st,portfolio.st = portfolio.st,account.st = account.st,nsamples = .nsamples,store = TRUE,verbose = FALSE
)
print(results$error)