Specifying sigcol in add.rule()
Great job! Although the add.rule()
command looks complex, each argument is quite simple. To understand this command, you will explore each argument individually.
First, add.rule()
takes the argument sigcol
, which specifies the signal column in your strategy. Like signals and indicators, all rules reference a column already present in your strategy. Rules relies on signals, and must therefore reference the signal columns in your strategy.
In this exercise, you will supply the add.rule()
call with the sigcol
value, which will be set to filterexit
(to reference the filterexit
signal you created in the previous chapter). Specifically, the filterexit
signal refers to the condition that the 50 day SMA has crossed under the 200 day SMA in your strategy. By creating a rule for this signal, you will be indicating that you wish to exit in this condition, as the market environment is no longer conducive to your position.
As before, strategy.st
is preloaded in your workspace.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- Once again, take a look at the
add.rule()
command in your workspace. - Create an exit rule based on
filterexit
by specifyingsigcol
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Fill in the sigcol argument in add.rule()
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "___", sigval = TRUE, orderqty = "all",
ordertype = "market", orderside = "long",
replace = FALSE, prefer = "Open"),
type = "exit")