Get startedGet started for free

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!

This exercise is part of the course

Financial Trading in R

View Course

Exercise instructions

  • Use add.signal() to create a sigFormula signal is true when both longfilter and longthreshold are true.
  • Set cross equal to TRUE.
  • Label this new signal as longentry.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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 = "___")
Edit and Run Code