rquantstratblotter

Error implementing a stoplimit order in quantstrat


After adding ordertype=stoplimit rules for stoploss implementation in quantstrat's pair_trade.R demo (only the short side shown below) as,

# stop loss for short order (child)
add.rule(strategy=pairStrat, name = 'ruleSignal', arguments=list(sigcol='short',
    sigval=TRUE,
    replace=FALSE, 
    orderside='short', 
    ordertype='stoplimit', 
    tmult=TRUE, 
    prefer='Close',
    TxnFees='TxnCost',
    threshold=quote(.stoplossPercent), 
    orderqty='all', 
    orderset='ocoshort'),
type='chain', parent='Enter_Short',
label='StopLoss_Short',
enabled=FALSE)

and enabling it via:

enable.rule(pairStrat,'chain',"StopLoss_Long","StopLoss_Short")

I get the error:

Error in if (grepl(label, strategy$rules[[type]][[i]]$label)) strategy$rules[[type]][[i]]$enabled <- enabled :
  argument is of length zero

A very similar approach was nevertheless successful with a single instrument portfolio strategy.

What am I missing here?


Solution

  • Your bitbucket code runs for me if I assign the output of add.rule and enable.rule back to pairStrat (as is done for all the other add.rule calls in the demo).

    # wrong
    add.rule(strategy=pairStrat, ...)
    enable.rule(pairStrat,'chain',"StopLoss_Long","StopLoss_Short")
    # correct
    pairStrat <- add.rule(strategy=pairStrat, ...)
    pairStrat <- enable.rule(pairStrat,'chain',"StopLoss_Long","StopLoss_Short")