Using add.rule() to implement an entry rule
Excellent! You've mastered every element of the rule building process in quantstrat
. While thus far you've added rules step-by-step, now it's time to put it all together and see how well you've been able to absorb the material in this chapter.
The opposite of an exit rule is an enter rule. On enter rules, orderqty
cannot be set to "all"
because there is no initial position on which to act. In this exercise, you will implement an enter rule that references the longentry
signal in your strategy and will buy one share of an asset.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- Specify the arguments in
add.rule()
to implement your new enter rule. - Your rule should be triggered when the
longentry
signal is equal toTRUE
. - Your rule should buy
1
share of an asset as a"market"
order. - Your rule side should be
long
and replace should beFALSE
. - Your rule should buy on the next day's
Open
after observing a signal.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create an entry rule of 1 share when all conditions line up to enter into a position
add.rule(strategy.st, name = "ruleSignal",
# Use the longentry column as the sigcol
arguments=list(sigcol = "___",
# Set sigval to TRUE
sigval = ___,
# Set orderqty to 1
orderqty = ___,
# Use a market type of order
ordertype = "___",
# Take the long orderside
orderside = "___",
# Do not replace other signals
replace = ___,
# Buy at the next day's opening price
prefer = "___"),
# This is an enter type rule, not an exit
type = "___")