0

As a variation on a previous question, I would like to know how to implement a holding period using the quantstrat package which prevents portfolio updating between periods of multiple stocks despite signals (here crossing thresholds).

I have attempted two methods, both failed:

  • added a column in the stock data indicating quarters/semesters/years with a 1, used with an add.rule with name="ruleSignal",

  • used add.rule with type="rebalance" and argument relabance_on="quarters".

Here's the code in 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)

Note: I've also used the applyStrategy.rebalance function.

Here's part of the output:

[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"

As you can see, the trades continue to take place between quarters, whereas I wish to implement trades only in new quarters or semesters, etc... Any help appreciated.

Cheers, Mike

Hysterio
  • 11
  • 3

1 Answers1

0

You need to change your exit rules signal. At the moment it is set to exit a trade according to "Higher.dcl.thres" not a time period.

Create another indicator which will give you back a TRUE reading after your specified period of time. Have your time periods start when you get an entry signal.

Use this indicator to create another exit signal and use said signal for your exit rule. I hope this makes sense.

Cameron Giles
  • 72
  • 1
  • 8