R - Quantstrat Issue with prefer and getPrice

405 Views Asked by At

Currently working on a strategy in quantstrat using Quandl futures data. However, when I try to applyStrategy() after adding indicators, signals and order rules, I receive the following error message, Error in getPrice(mktdata, prefer = prefer) : object 'prefer' not found. applyIndicators() and applySignals() works well when debugging so the error is most likely in the processing of the rules. Below is the tail end of the mktdata variable that results after applying the signals in addition to the end part of the code.

mktdata:

enter image description here

Code:

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)

Output of sessionInfo():

enter image description here

0

There are 0 best solutions below