Combining signals - II
In the previous exercise, you approximated a sigFormula signal by comparing the value of two other signals. In this final exercise, you will take this one step further by using the sigFormula() function to generate a sigFormula signal.
The goal of this exercise is simple. You want to enter into a position when both longfilter and longthreshold become true at the same time. The idea is this: You don't want to keep entering into a position for as long as conditions hold true, but you do want to hold a position when there's a pullback in an uptrending environment.
Writing a sigFormula function is as simple as writing the argument of an "if statement" in base R inside the formula() function. In this case, you want to create a signal labeled longentry, which is true when both longfilter and longthreshold cross over to true at the same time.
Once you complete this exercise, you will have a complete survey of how signals work in quantstrat!
Diese Übung ist Teil des Kurses
Financial Trading in R
Anleitung zur Übung
- Use
add.signal()to create asigFormulasignal is true when bothlongfilterandlongthresholdare true. - Set
crossequal toTRUE. - Label this new signal as
longentry.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Add a sigFormula signal to your code specifying that both longfilter and longthreshold must be TRUE, label it longentry
add.signal(strategy.st, name = "___",
# Specify that longfilter and longthreshold must be TRUE
arguments = list(formula = "___ & ___",
# Specify that cross must be TRUE
cross = ___),
# Label it longentry
label = "___")