我一直在关注以下教程,以学习如何在Quantstrat中测试不同的变量。
https://timtrice.github.io/backtesting-strategies/parameter-optimization.html
本教程的有关优化的部分位于此链接的第7部分中,但这取决于前面的部分。
输入apply.paramset
后,R不返回任何内容。
如果我输入tradeStats(portfolio.st,'SPY')
,它将返回此内容;
[1] Portfolio Symbol Num.Txns Num.Trades
[5] Total.Net.Profit Avg.Trade.PL Med.Trade.PL Std.Err.Trade.PL
[9] Largest.Winner Largest.Loser Gross.Profits Gross.Losses
[13] Std.Dev.Trade.PL Percent.Positive Percent.Negative Profit.Factor
[17] Avg.Win.Trade Med.Win.Trade Avg.Losing.Trade Med.Losing.Trade
[21] Avg.Daily.PL Med.Daily.PL Std.Dev.Daily.PL Std.Err.Daily.PL
[25] Ann.Sharpe Max.Drawdown Profit.To.Max.Draw Avg.WinLoss.Ratio
[29] Med.WinLoss.Ratio Max.Equity Min.Equity End.Equity
<0 rows> (or 0-length row.names)
此数据框的格式符合我的预期,但没有任何值。我希望得到的结果将为我显示慢速和快速SMA值的每种组合的值。
我尝试运行apply.strategy
,但这给了我相同的结果。
我的理解是apply.paramset
与apply.strategy
具有相似的功能。
我尝试在没有此部分的情况下运行它,结果相同。
library(parallel)
if( Sys.info()['sysname'] == "Windows") {
library(doParallel)
registerDoParallel(cores=detectCores())
} else {
library(doMC)
registerDoMC(cores=detectCores())
}
直接从本教程中获取的代码如下。
我这里缺少什么吗?我完成了本教程的第12部分,并且毫无困难地查看了第5部分的结果,该部分不包含发行版。但是,我看不到优化后查看结果的任何信息。
#教程第5部分
library(quantstrat)
library(blotter)
Sys.setenv(TZ = "UTC")
currency('USD')
init_date <- "2007-12-31"
start_date <- "2008-01-01"
end_date <- "2009-12-31"
init_equity <- 1e4 # $10,000
adjustment <- TRUE
basic_symbols <- function() {
symbols <- c(
"IWM",# iShares Russell 2000 Index ETF
"qqQ",# PowerShares qqQ TRust,Series 1 ETF
"SPY" # SPDR S&P 500 ETF Trust
)
}
symbols <- basic_symbols()
getSymbols(Symbols = symbols,src = "yahoo",index.class = "POSIXct",from = start_date,to = end_date,adjust = adjustment)
stock(symbols,currency = "USD",multiplier = 1)
portfolio.st <- "Port.Luxor"
account.st <- "acct.Luxor"
strategy.st <- "Strat.Luxor"
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)
add.indicator(strategy = strategy.st,name = "SMA",arguments = list(x = quote(Cl(mktdata)),n = 10),label = "nFast")
add.indicator(strategy = strategy.st,n = 30),label = "nSlow")
add.signal(strategy = strategy.st,name="sigCrossover",arguments = list(columns = c("nFast","nSlow"),relationship = "gte"),label = "long")
add.signal(strategy = strategy.st,relationship = "lt"),label = "short")
add.rule(strategy = strategy.st,name = "ruleSignal",arguments = list(sigcol = "long",sigval = TRUE,orderqty = 100,ordertype = "stoplimit",orderside = "long",threshold = 0.0005,prefer = "High",TxnFees = -10,replace = FALSE),type = "enter",label = "EnterLONG")
add.rule(strategy.st,arguments = list(sigcol = "short",orderqty = -100,threshold = -0.005,orderside = "short",replace = FALSE,prefer = "Low"),label = "EnterSHORT")
add.rule(strategy.st,ordertype = "market",orderqty = "all",replace = TRUE),type = "exit",label = "Exit2SHORT")
add.rule(strategy.st,label = "Exit2LONG")
cwd <- getwd()
setwd("C:\\Users\\NEW USER\\Desktop\\RYO\\R Working Directory")
results_file <- paste("results",strategy.st,"RData",sep = ".")
if( file.exists(results_file) ) {
load(results_file)
} else {
results <- applyStrategy(strategy.st,portfolios = portfolio.st)
updatePortf(portfolio.st)
updateacct(account.st)
updateEndEq(account.st)
if(checkBlotterupdate(portfolio.st,account.st,verbose = TRUE)) {
save(list = "results",file = results_file)
save.strategy(strategy.st)
}
}
setwd(cwd)
#Tutorial,Part 7
.fastSMA <- (1:30)
.slowSMA <- (20:80)
.nsamples <- 5
portfolio.st <- "Port.Luxor.MA.Opt"
account.st <- "acct.Luxor.MA.Opt"
strategy.st <- "Strat.Luxor.MA.Opt"
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st,initDate = init_date)
initacct(name = account.st,initEq = init_equity)
initOrders(portfolio = portfolio.st,initDate = init_date)
strategy(strategy.st,store = TRUE)
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st,initDate = init_date)
initOrders(portfolio = portfolio.st,initDate = init_date)
add.distribution(strategy.st,paramset.label = "SMA",component.type = "indicator",component.label = "nFast",variable = list(n = .fastSMA),label = "nFAST")
add.distribution(strategy.st,component.label = "nSlow",variable = list(n = .slowSMA),label = "nSLOW")
add.distribution.constraint(strategy.st,distribution.label.1 = "nFAST",distribution.label.2 = "nSLOW",operator = "<",label = "SMA.Constraint")
library(parallel)
if( Sys.info()['sysname'] == "Windows") {
library(doParallel)
registerDoParallel(cores=detectCores())
} else {
library(doMC)
registerDoMC(cores=detectCores())
}
cwd <- getwd()
setwd("C:\\Users\\NEW USER\\Desktop\\RYO\\R Working Directory")
results_file <- paste("results",sep = ".")
if( file.exists(results_file) ) {
load(results_file)
} else {
results <- apply.paramset(strategy.st,portfolio.st = portfolio.st,account.st = account.st,nsamples = .nsamples)
if(checkBlotterupdate(portfolio.st,file = results_file)
save.strategy(strategy.st)
}
}
setwd(cwd)
applyStrategy(strategy.st,portfolios = portfolio.st)
checkBlotterupdate(portfolio.st,verbose = TRUE)
updatePortf(portfolio.st)
updateacct(account.st)
updateacct(account.st)