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.
Cet exercice fait partie du cours
Financial Trading in R
Instructions
- Use
add.signal()to add asigCrossoverspecifying that theSMA50crosses under theSMA200. - Label this signal
filterexit.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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 = "___")