Using sigCrossover
While having a long filter is necessary, it is not sufficient to put on a trade for this strategy. However, the moment the condition does not hold, the strategy should not hold any position whatsoever. For this exercise, you will implement the opposite of the rule specified above using the sigCrossover()
function.
As opposed to sigComparison()
, which will always state whether or not a condition holds, sigCrossover()
only gives a positive the moment the signal first occurs, and then not again. This is useful for a signal that will be used to initiate a transaction, as you only want one transaction in most cases, rather than having transactions fire again and again.
In this case, you will implement the sigCrossover() function specifying that the SMA50
crosses under the SMA200
. You will label this signal filterexit
, as it will exit your position when the moving average filter states that the environment is not conducive for the strategy to hold a position.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- Use
add.signal()
to add asigCrossover
specifying that theSMA50
crosses under theSMA200
. - Label this signal
filterexit
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Add a sigCrossover which specifies that the SMA50 is less than the SMA200 and label it filterexit
add.signal(strategy.st, name = "___",
# We're interested in the relationship between the SMA50 and the SMA200
arguments = list(columns = c("___", "___"),
# The relationship is that the SMA50 crosses under the SMA200
relationship = "___"),
# Label it filterexit
label = "___")