在quantstrat的pair_trade.R演示中添加了ordertype=stoplimit
规则来实现stoploss(仅显示下面的短边)后,
# 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)
并通过启用
enable.rule(pairStrat,'chain',"StopLoss_Long","StopLoss_Short")
我得到错误:
Error in if (grepl(label, strategy$rules[[type]][[i]]$label)) strategy$rules[[type]][[i]]$enabled <- enabled :
argument is of length zero
然而,一种非常类似的方法在单一工具组合战略中取得了成功。
我在这里错过了什么?
如果我将add.rule
和enable.rule
的输出分配回pairStrat
(就像在演示中对所有其他add.rule
调用所做的那样),那么您的比特桶代码将为我运行。
# 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")