Get startedGet started for free

Introduction to signals

1. Introduction to signals

Welcome to the chapter on signals.

2. What are signals?

Signals occur from the combination of market data and indicators, or indicators with other indicators. Simply, signals are events that may tell you at which point in time you may wish to buy or sell an asset. For instance, a trend-following strategy may signal to buy an asset when a moving average with a shorter lookback period crosses over one with a longer lookback period, such as a 50 day moving average crossing above a 200 day moving average. A reversion strategy may signal to buy when a certain oscillator crosses under 20, on a scale from 0 to 100. Still, other signals can occur only when multiple conditions are met.

3. Using add.signal()

In this chapter, we will investigate all such functionality I just mentioned. While this may sound intimidating at first, when you break it down into its pieces, you will find that signals are actually similar to, but simpler than indicators. Like indicators, they take in a signal function, its arguments, and need a label. Unlike indicators, which are infinitely customizable, the few signal functions in quantstrat can cover almost all phenomena found in trading, and careful signal specification can go very far in terms of specifying a strong and logical strategy. Essentially, they work similarly in structure to the add-dot-indicator function in that they call the add-dot-signal function, the name of the strategy, the function name of the signal function, the arguments to the signal function, and finally, a label. Again, like the add-dot-indicator function, this is also similar to the apply family of functions from R in that one specifies a function within an apply call, and provides its arguments.

4. Four types of signals

If this still seems a little bit hazy to you, in this chapter, you will get an intuitive feel for the functionality of some of these signals. In fact, there are only four types of signals: sigComparison -- which is a signal permanently comparing two differing quantities (such as two moving averages), sigCrossover -- which is identical, except only evaluates to TRUE on the day of the condition changing from FALSE to TRUE, sigThreshold, which states whether or not a dynamic quantity is larger (or smaller) than a static number, with both the option of evaluating it in the context of a sigComparison or sigCrossover signal, but against flat quantities instead. Lastly, the sigFormula signal function allows for the creation of complex signals by combining simpler ones.

5. Examples

To get a visual idea of what signals do, we will have a look at two diagrams. This first diagram visualized the principles of sigCrossover and sigComparison. The sigCrossover and sigComparison signals are very useful when dealing with moving averages and other such filters. The sigCrossover signal is concerned only when one moving averages crosses another, while the sigComparison signal is concerned about the relationship as a whole.

6. Examples

With sigThreshold, the same idea applies except with an indicator related to static quantities, such as an less than 20. However, rather than need two separate signal functions, sigThreshold simply makes use of a cross argument, having the function mimic sigCrossover when TRUE and sigComparison when FALSE.

7. Let's practice!

In the following exercises, you'll become more acquainted with signals. Good luck.