apply.paramset找不到投资组合

时间:2019-02-14 14:09:44

标签: quantstrat

好吧,当我尝试运行以下代码时,我正在跟踪Quantstrat的策略示例:效果很好,但是.....当我尝试更新Portfolio / Account或仅从投资组合中获取订单时,它说“ Portfolio ....未找到”。无论如何,我的完整代码(从作者复制):

Sys.setenv(TZ= "UTC")
currency('USD')
init_date <- "2007-12-31"
start_date <- "2008-01-01"
end_date <- "2009-12-31"
init_equity <- 1e4 
adjustment <- TRUE

symbols <- c("IWM","QQQ","SPY")
getSymbols(Symbols = symbols,src = "yahoo", index.class = "POSIXct",
       from = start_date,to = end_date, adjust = adjustment)
stock(symbols, currency = "USD",multiplier = 1)

.fast <- 10
.slow <- 30
.threshold <- 0.0005
.orderqty <- 100
.txnfees <- -10
.stoploss <- 3e-3

.StopLoss<- seq(0.05,0.6,length.out=48)/100
strategy.st <- "Luxor.Stop.Loss.Opt."
portfolio.st <- "Luxor.Stop.Loss.Opt."
account.st  <- "Luxor.Stop.Loss.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)
initOrders(portfolio = portfolio.st,initDate = init_date)
strategy(strategy.st, store = TRUE)

add.indicator(strategy.st, name = "SMA",arguments = list(x = quote(Cl(mktdata)),
          n = .fast),label = "nFast")
add.indicator(strategy.st, name = "SMA",arguments = list(x = quote(Cl(mktdata)),
          n = .slow),label = "nSlow")

add.signal(strategy.st, name = "sigCrossover",arguments = 
       list(columns = c("nFast", "nSlow"),relationship = "gte"),
       label = "long")
add.signal(strategy.st, name = "sigCrossover",arguments = 
       list(columns = c("nFast", "nSlow"),relationship = "lt"),
       label = "short")

add.rule(strategy.st, name = "ruleSignal",arguments = list(sigcol = "long" , 
  sigval = TRUE,replace = FALSE,orderside = "long" ,ordertype = "stoplimit",
  prefer = "High",threshold = .threshold,TxnFees = .txnfees,
  orderqty = +.orderqty,osFUN = osMaxPos,orderset = "ocolong"),
  type = "enter",label = "EnterLONG")

add.rule(strategy.st, name = "ruleSignal",arguments = list(sigcol = "short", 
  sigval = TRUE,replace = FALSE,orderside = "short",ordertype = "stoplimit",
  prefer = "Low",threshold = .threshold,TxnFees = .txnfees,
  orderqty = -.orderqty,osFUN = osMaxPos,orderset = "ocoshort"),
  type = "enter",label = "EnterSHORT")

add.rule(strategy.st,name = "ruleSignal",arguments = list(sigcol = "short", 
  sigval = TRUE,replace = TRUE,orderside = "long" ,ordertype = "market",
  TxnFees = .txnfees,orderqty = "all",orderset = "ocolong"),type = "exit",
  label = "Exit2SHORT")

add.rule(strategy.st, name = "ruleSignal",arguments = list(sigcol = "long", 
  sigval = TRUE,replace = TRUE,orderside = "short",ordertype = "market",
  TxnFees = .txnfees,orderqty = "all",orderset = "ocoshort"),type = "exit",
  label = "Exit2LONG")

add.rule(strategy.st, name = "ruleSignal",arguments = list(sigcol = "long" , 
sigval = TRUE,replace = FALSE,orderside = "long",ordertype = "stoplimit",
tmult = TRUE,threshold = quote(.stoploss),TxnFees = .txnfees,
orderqty = "all",orderset = "ocolong"),type = "chain", parent = "EnterLONG",
label = "StopLossLONG",enabled = FALSE)

add.rule(strategy.st, name = "ruleSignal",arguments = list(sigcol = "short", 
sigval = TRUE,replace = FALSE,orderside = "short",ordertype = "stoplimit",
tmult = TRUE,threshold = quote(.stoploss),TxnFees = .txnfees,
orderqty = "all",orderset = "ocoshort"),type = "chain", parent = "EnterSHORT",
label = "StopLossSHORT",enabled = FALSE)

for(symbol in symbols){
addPosLimit(portfolio = portfolio.st,symbol = symbol,timestamp = init_date,
          maxpos = .orderqty)

}

最后是参数/约束

add.distribution(strategy.st,
             paramset.label = "StopLoss",
             component.type = "chain",
             component.label = "StopLossLONG",
             variable = list(threshold = .StopLoss),
             label = "StopLossLONG")
add.distribution(strategy.st,
             paramset.label = "StopLoss",
             component.type = "chain",
             component.label = "StopLossSHORT",
             variable = list(threshold = .StopLoss),
             label = "StopLossSHORT")
add.distribution.constraint(strategy.st,
             paramset.label = "StopLoss",
             distribution.label.1 = "StopLossLONG",
             distribution.label.2 = "StopLossSHORT",
             operator = "==",
             label = "StopLoss")

enable.rule(strategy.st, 'chain', 'StopLoss')

 results <- apply.paramset(strategy.st, 
                        paramset.label = "StopLoss", 
                        portfolio.st = portfolio.st, 
                        account.st = account.st, 
                        nsamples = .nsamples, 
                        verbose = TRUE)

我认为还可以。 但是现在开始出现问题:每当我尝试执行以下任一操作时:a)获取投资组合,b)投资组合的订单簿,c)更新Portfolio.st/account.st,或d)仅按“ chart.Posn”绘图总是得到相同的错误消息:

Error in .getPortfolio(Portfolio, envir = envir) : 
Portfolio Luxor.Stop.Loss.Opt. not found, use initPortf() to create a new    portfolio

为什么????我是Quantstrat的新手,所以这里有很多要了解的地方。但是到目前为止,我设法运行了除此部分以外的所有内容。这里有天使可以帮助我吗?

0 个答案:

没有答案