我修改了quantstrat demo bee并试图对指标参数进行优化。但是,我尽可能地尝试简化优化,但我仍然遇到如下错误:
error calling combine function:
<simpleError in fun(result.1, result.2, result.3, result.4, result.5, result.6, result.7, result.8, result.9, result.10): attempt to select less than one element>
Error in .subset2(x, i, exact = exact) : subscript out of bounds
我的sessionInfo
输出:
R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils
[6] datasets methods base
other attached packages:
[1] doMC_1.3.4
[2] iterators_1.0.8
[3] lubridate_1.5.0
[4] dplyr_0.4.3
[5] quantstrat_0.9.1739
[6] foreach_1.4.3
[7] blotter_0.9.1741
[8] PerformanceAnalytics_1.4.4000
[9] FinancialInstrument_1.2.0
[10] quantmod_0.4-5
[11] TTR_0.23-1
[12] xts_0.9-7
[13] zoo_1.7-12
loaded via a namespace (and not attached):
[1] Rcpp_0.12.3 magrittr_1.5
[3] lattice_0.20-33 R6_2.1.2
[5] stringr_1.0.0 tools_3.2.4
[7] grid_3.2.4 DBI_0.3.1
[9] htmltools_0.3 yaml_2.1.13
[11] lazyeval_0.1.10 assertthat_0.1
[13] digest_0.6.9 codetools_0.2-14
[15] rsconnect_0.4.1.4 rmarkdown_0.9.5
[17] stringi_1.0-1 compiler_3.2.4
以下是我的代码,数据将从here下载。谁能帮我弄清楚哪里出错了?感谢
####################################################################
#### Load packages
#####################################################################
library(dplyr)
suppressMessages(require(quantstrat))
####################################################################
#### remove objects
########################################################################
rm(list = ls(all.names = T))
if(!exists(".instrument")) .instrument <<- new.env()
if(!exists(".blotter")) .blotter <<- new.env()
if(!exists(".strategy")) .strategy <- new.env()
########################################################################
#### DEFINE VARIABLES or parameters
########################################################################
initDate = "2000-01-01"
symbol.st = 'CYB_DAY'
portf.st = 'bug'
acct.st = 'colony'
strat.st = 'bee'
initEq = 100000
nFast = 10
nSlow = 30
nSd = 1
########################################################################
#### GET DATA
###############################################################
# set your working directory where the data is stored
setwd("/Users/Natsume/Documents/data_exploration_r/data")
currency('USD') # initiate currency
stock(symbol.st ,currency='USD', multiplier=1) # initiate stock
getSymbols(Symbols = symbol.st, src = "csv")
initPortf(
portf.st,
symbol.st,
initDate=initDate) # initiate portfolio
initAcct(
acct.st,
portf.st,
initEq=initEq,
initDate=initDate) # initiate account
initOrders(
portf.st,
initDate=initDate ) # initiate order_book
bee = strategy(strat.st) # create strategy object
addPosLimit(
portfolio=portf.st,
symbol=symbol.st,
timestamp=initDate,
maxpos=300, longlevels = 3) # only trade in one direction once
bee <- add.indicator(
strategy = strat.st,
name = 'BBands', # TA name
arguments = list(HLC=quote(HLC(mktdata)),
n=nSlow,
sd=nSd),
label = 'BBand')
#### SMA column
bee <- add.indicator(
strategy = strat.st,
name = 'SMA', # TA name
arguments = list(x=quote(Cl(mktdata)),
n=nFast),
label = 'MA' )
bee <- add.signal(
strategy = strat.st,
name = 'sigCrossover',
arguments = list(columns=c('MA','dn'),
relationship='lt'),
label = 'MA.lt.dn')
#### SMA cross over upperBand
bee <- add.signal(
strategy = strat.st,
name = 'sigCrossover',
arguments = list(columns=c('MA','up'),
relationship='gt'),
label = 'MA.gt.up')
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.gt.up',
sigval = TRUE,
replace = F,
orderqty = 100,
ordertype = 'market',
# one of "market","limit","stoplimit", "stoptrailing", or "iceberg"
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterLONG')
#### exitLong when SMA cross under LowerBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.lt.dn',
sigval = TRUE,
replace = F,
orderqty = 'all',
ordertype = 'market',
orderside = 'long'),
type = 'exit',
label = 'ExitLONG')
#### enterShort when SMA cross under LowerBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.lt.dn',
sigval = TRUE,
replace = F,
orderqty = -100,
ordertype = 'market',
orderside = 'short',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterSHORT')
#### exitShort when SMA cross over upperBand
bee <- add.rule(
strategy = strat.st,
name = 'ruleSignal',
arguments = list(sigcol = 'MA.gt.up',
sigval = TRUE,
replace = F,
orderqty = 'all',
ordertype = 'market',
orderside = 'short'),
type = 'exit',
label = 'ExitSHORT')
applyStrategy(
strat.st,
portf.st,
prefer='Open', # why prefer='Open'
verbose=T)
updatePortf(
portf.st) #,
updateAcct(
acct.st) # ,
updateEndEq(
Account = acct.st)#,
### User Set up pf parameter ranges to test
.nFastList = 5:13
.nSlowList = 10:40
.nSdList = 1:3
# number of random samples of the parameter distribution to use for random run
.nsamples = 10
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'BBand', #this is the label given to the indicator in the strat
variable = list(n = .nSlowList),
label = 'BBandMA'
)
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'BBand', #this is the label given to the indicator in the strat
variable = list(sd = .nSdList),
label = 'BBandSD'
)
add.distribution(strat.st,
paramset.label = 'SMA_BBparams',
component.type = 'indicator',
component.label = 'MA', #this is the label given to the indicator in the strat
variable = list(n = .nFastList),
label = 'MAn'
)
add.distribution.constraint(strat.st,
paramset.label = 'SMA_BBparams',
distribution.label.1 = 'BBandMA',
distribution.label.2 = 'MAn',
operator = '>',
label = 'BBandMA>MAn'
)
### parallel computing to speed up
if( Sys.info()['sysname'] == "Windows" )
{
library(doParallel)
# registerDoParallel(cores=detectCores())
registerDoSEQ()
} else {
library(doMC)
registerDoMC(cores=detectCores())
}
results <- apply.paramset(strat.st,
paramset.label='SMA_BBparams',
portfolio.st=portf.st,
account.st=acct.st,
nsamples= .nsamples, # take all options
#.nsamples, only take 10 samples
verbose=TRUE)
results$tradeStats %>% View()