作为前一个问题的变体,我想知道如何使用quantstrat包实现持有期,这可以防止在多个股票期间更新投资组合,尽管有信号(此处超过阈值)。
我尝试了两种方法,都失败了:
在库存数据中添加了一列,表示季度/学期/年,为1,与add.rule一起使用,名称=" ruleSignal",
使用add.rule,类型="重新平衡"和论点 relabance_on ="季度"
这里是quantstrat中的代码:
asda as
## Signal 1: rank inferior to N
stratjt=add.signal(strategy=stratjt, name="sigThreshold",
arguments=list(threshold=N, column="Rank",
relationship="lte",cross=FALSE),
label="Lower.dcl.thres")
## Signal 2: rank superior to N
stratjt <- add.signal(strategy=stratjt, name="sigThreshold",
arguments=list(threshold=N, column="Rank",
relationship="gt", cross=FALSE),
label="Higher.dcl.thres")
# Rule 1: First attempt to add rebalance rule based on extra signal column
#stratjt <- add.rule(strategy=stratjt, name='ruleSignal',
# arguments = list(sigcol="Rebalance_on", sigval=TRUE,
# orderqty=1, ordertype='market',
# orderside='long', pricemethod='market',
# replace=FALSE, osFUN=osMaxPos),
# type='enter', path.dep=TRUE)
# Rule 1: Second attempt to add rebalance rule using the "rebalance" rule type
stratjt <- add.rule(strategy=stratjt, name='ruleSignal',
arguments = list(rebalance_on="quarters"),
type='rebalance', path.dep=TRUE)
# Rule 2: Enter rule when below threshold
stratjt <- add.rule(strategy=stratjt, name='ruleSignal',
arguments = list(sigcol="Lower.dcl.thres", sigval=TRUE,
orderqty=max.size, ordertype='market',
orderside='long', pricemethod='market',
replace=FALSE, osFUN=osMaxPos),
type='enter', path.dep=TRUE)
# Rule 3: add exit rule when above threshold
stratjt <- add.rule(strategy = stratjt, name='ruleSignal',
arguments = list(sigcol="Higher.dcl.thres", sigval=TRUE,
orderqty='all', ordertype='market',
orderside='long', pricemethod='market',
replace=FALSE),
type='exit', path.dep=TRUE)
注意:我还使用了applyStrategy.rebalance函数。
这是输出的一部分:
[1] "2000-04-30 00:00:00 Vivendi -1 @ 99.2256"
[1] "2002-11-30 00:00:00 Vivendi 1 @ 16.2966"
[1] "2003-02-28 00:00:00 Vivendi -1 @ 14.0564"
[1] "2003-11-30 00:00:00 Vivendi 1 @ 22.9647"
[1] "2004-01-31 00:00:00 Vivendi -1 @ 26.3656"
[1] "2004-02-29 00:00:00 Vivendi 1 @ 28.7848"
[1] "2004-03-31 00:00:00 Vivendi -1 @ 26.2421"
[1] "2006-05-31 00:00:00 Vivendi 1 @ 35.8552"
正如您所看到的,交易继续在季度之间进行,而我希望仅在新的季度或学期等实施交易...... 任何帮助表示赞赏。
干杯, 麦克
答案 0 :(得分:0)
您需要更改退出规则信号。目前它根据“Higher.dcl.thres”而不是时间段设置退出交易。
创建另一个指示符,在指定的时间段后返回TRUE读数。当你收到进入信号时,你的时间段就开始了。
使用此指示器创建另一个退出信号,并将所述信号用于退出规则。我希望这是有道理的。