在quantstrat包中添加信号时出错

时间:2016-04-08 21:05:18

标签: r quantstrat

我是R的新手,我正试图创建一个非常简单的交易策略(当它上涨2%时买入VIX,当它下跌2%时卖出它)。我在使用add.signal命令时收到错误:“match.names(column,colnames(data))中的错误:   参数“column”缺失,没有默认值。“ 我到处搜索,似乎无法找到修复或理解R试图告诉我的内容。任何意见都将不胜感激。

require(quantstrat)

options("getSymbols.warning4.0"=FALSE) #suppress warnings
rm(list=ls(.blotter), envir=.blotter) #house cleaning, clears blotter
rm(list=ls(.strategy), envir=.strategy)#clears strategy
rm.strat(qs.portfolio)#clear the portfolio environment
rm.strat(qs.strategy)#clear the strategy environment
rm.strat(qs.account)#clear the account environment

currency("USD")
stock("VIX",currency="USD",multiplier=1)

# system settings
initDate <- '2013-12-31' 
startDate <- '2014-01-01'
endDate <- '2016-04-07'
initEq <- 100000
Sys.setenv(TZ="UTC") #Timezone

getSymbols('^VIX', from=startDate, to=endDate, index.class="POSIXct", adjust=TRUE)
VIX$lagROC <- lag(round(ROC(Cl(VIX)), 4), n=1, type="discrete")
VIX$lagROC[is.na(VIX$lagROC)] <- 0

# initialize portfolio, account, orders
qs.strategy <- "qsJones"
initPortf(qs.strategy,'VIX')
initAcct(qs.strategy,portfolios=qs.strategy, initEq=initEq)
initOrders(portfolio=qs.strategy)
#create a new strategy object
strategy(qs.strategy,store=TRUE)

strat <- getStrategy(qs.strategy) 

thresh1 <- (.02*-1)
thresh2 <- .02

add.indicator(strategy = qs.strategy, name = "ROC",
  arguments = list(x = quote(Cl(mktdata)), n=1), label="lagROC")
test <- applyIndicators(qs.strategy, VIX)

add.signal(qs.strategy, name="sigThreshold",
  arguments=list(column="lagROC", threshold=thresh1, relationship="lte", cross=FALSE),
  label="lt.ROCThresh1")
add.signal(qs.strategy, name="sigThreshold",
  arguments=list(column="lagROC", threshold=thresh2, relationship="gte", cross=FALSE),
  label="gt.ROCThresh2")
test <- applySignals(qs.strategy, test)

# exit when lagROC < .02
add.rule(qs.strategy, name='ruleSignal',
  arguments=list(sigcol="Cl.lt.lagROC", sigval=TRUE, replace=FALSE, orderqty='all',
                   ordertype='market', orderside='long'),
  type='exit', path.dep=TRUE)
# go long when lagROC > .02
add.rule(qs.strategy, name='ruleSignal',
  arguments=list(sigcol="Cl.gt.lagROC", sigval=TRUE, replace=FALSE, orderqty=1500,
                   ordertype='market', orderside='long'),
  type='enter', path.dep=TRUE)

applyStrategy(strategy=qs.strategy , portfolios=qs.strategy)
tail(mktdata)
mktdata["2014"]
getTxns(Portfolio=qs.strategy, Symbol="VIX")

1 个答案:

答案 0 :(得分:1)

您的add.signal来电创建了两列:"lt.ROCThresh1""gt.ROCThresh2",而您的add.rule来电则引用了"Cl.lt.lagROC""Cl.gt.lagROC"列。 #39; t存在)。

您需要:

  1. label=来电中的add.signal参数分别从"lt.ROCThresh1""gt.ROCThresh2"更改为"Cl.lt.lagROC""Cl.gt.lagROC"
  2. sigcol=来电中的add.rule参数分别从"Cl.lt.lagROC""Cl.gt.lagROC"更改为"lt.ROCThresh1""gt.ROCThresh2"