目前正在使用 Quandl 期货数据在 quantstrat 中制定策略。但是,当我在添加指标、信号和订单规则后尝试 applyStrategy() 时,我收到以下错误消息,Error in getPrice(mktdata, prefer = prefer) : object 'prefer' not found
. applyIndicators()
并且applySignals()
在调试时运行良好,因此错误最有可能出现在规则的处理中。下面是 mktdata 变量的尾部,该变量在应用信号后产生,以及代码的结尾部分。
mkt 数据:
代码:
initPortf(portfolio.mom, symbols=allInstruments, initDate=initDate, currency='USD')
initAcct(account.mom, portfolios=portfolio.mom, initDate=initDate, currency='USD')
initOrders(portfolio.mom, initDate=initDate)
strategy(strategy.mom, store=TRUE)
#Indicators
strategy.mom <- add.indicator(strategy=strategy.mom, name="tsMOM", arguments=list(futDataRet = quote(Cl(mktdata)), momLookback=momLookback, tradingDays=tradingDays), label="tsMOM")
strategy.mom <- add.indicator(strategy=strategy.mom, name="retVol", arguments=list(futDataRet = quote(Cl(mktdata)), volLookback=volLookback, tradingDays=tradingDays), label="retVol")
#Signals
strategy.mom <- add.signal(strategy.mom,name="sigThreshold",arguments=list(threshold=posTradeThresh, column="tsMOM", relationship="gt", cross=FALSE), label="LongCond")
strategy.mom <- add.signal(strategy.mom,name="sigThreshold",arguments=list(threshold=negTradeThresh, column="tsMOM", relationship="lt", cross=FALSE), label="ShortCond")
strategy.mom <- add.signal(strategy.mom, name="sigFormula", arguments=list(columns=c("tsMOM","LongCond"), formula="((LongCond != 1) & (tsMOM >= 0))", cross=FALSE), label="NeutralPosCond")
strategy.mom <- add.signal(strategy.mom, name="sigFormula", arguments=list(columns=c("tsMOM","ShortCond"), formula="((tsMOM < 0) & (ShortCond != 1))", cross=FALSE), label="NeutralNegCond")
# Entry Rule
strategy.mom <- add.rule(strategy.mom, name="ruleSignal", arguments=list(sigcol="LongCond", sigval=TRUE, orderqty=1000, ordertype="market", orderside="long", prefer = "Close", TxnFees = -100), type="enter")
strategy.mom <- add.rule(strategy.mom, name="ruleSignal", arguments=list(sigcol="ShortCond", sigval=TRUE, orderqty=1000, ordertype="market", orderside="short", prefer = "Close", TxnFees = -100), type="enter")
# Exit Rule
strategy.mom <- add.rule(strategy.mom, name="ruleSignal", arguments=list(sigcol="NeutralPosCond", sigval=TRUE, orderqty="all", ordertype="market", orderside="short", prefer = "Close", TxnFees = -100), type="exit")
strategy.mom <- add.rule(strategy.mom, name="ruleSignal", arguments=list(sigcol="NeutralNegCond", sigval=TRUE, orderqty="all", ordertype="market", orderside="long", prefer = "Close", TxnFees = -100), type="exit")
applyStrategy(strategy=strategy.mom, portfolios=portfolio.mom, prefer="Close")
updatePortf(portfolio.mom)
updateAcct(account.mom)
updateEndEq(account.mom)
输出sessionInfo()
: