QuantConnect: Create an Indicator – Awesome Oscillator

Continuing with the QuantConnect series, we shall turn our attention to indicators. There are actually a lot of cool things we can do with indicators on QuantConnect such as chaining the existing library of 100 indicators together to create some unique results. However, in this tutorial we will focus on creating a new indicator from scratch, importing it into our algorithm and finally plotting the output.

Awesome Oscillator

In the code example for this post, we will create an Awesome Oscillator. This was selected as it is a fairly simple indicator to calculate and we can build it on the back of an excellent example from QuantConnect’s own Alex Catarino.

In short, the Awesome Oscillator aims to measure market momentum by calculating the difference between two simple moving averages. Specifically, it uses 34 Period and 5 Period SMA’s for this purpose.

Awesome Oscillator = 5 Period SMA – 34 Period SMA

Note: That in the example below, we will copy/port the formula from Tradingview’s Awesome Oscillator.

Example Code

Code Commentary

If you have followed other posts in the getting started series, you might notice right away that we have a new import.

from collections import deque

A deque is like a list but it has a fixed size. This means that it is more like a “container”. Once it is full, anything you put into the container will result in something else getting pushed out of the other side. This makes it perfect for storing values needed to calculate our simple moving average value.

For more information see here: https://docs.python.org/3.7/library/collections.html#collections.deque

After the imports, we move onto the Indicator and Algorithm classes. This is actually our first example on QuantConnect to contain two classes. As such, we will break the commentary up into two parts to cover the indicator and algorithm separately. Note that we will be talking a little bit about classes over the following few paragraphs. The jargon will be as light as possible but if you are lost or completely new to programming, it might be an idea to take a look at some introductory Python tutorials on classes.

Indicator

To create a custom indicator on QuantConnect, all we need to do is create a new python class and make sure that it has some specific methods (functions) inside it.

__init__()

The first method needed is __init__(). This is called (run/executed) when a new instance of the indicator is made from the class blueprints. It is during the initialization of the class (when we create the indicator/instance) that we set a few attributes (variables) to be used later. Setting them when the indicator is created means that the attributes exist and have valid values before any data is piped into the indicator.  These attributes we set are:

  • Name: Wich can be used to identify the indicator when debugging or printing the output (more on this later)
  • Time: Will be used for logging the last time that the indicator was updated.  Here we start withdatetime.min. This actually just gives us the earliest valid datetime value possible.
  • Value: Is the actual indicator value to be returned. Here we set it to 0 as we don’t have any data when we first create the indicator.
  • IsReady: This an important attribute. It will be used by many algorithms to detect whether the indicator has enough data to make accurate calculations. For example, if you have a 200-day moving average, IsReadywould be Falseuntil the indicator has 200 days of data to make the calculation.
  • fast_sma_queue and slow_sma_queue: These are two dequecontainers that we will use for storing data. They will allow us to create the SMA values needed to create the final Awesome Oscillator Value.

Introduction to classes: https://www.pythoncentral.io/introduction-to-python-classes/

__repr__()

Next, we have the __repr__() method. This is not absolutely needed. However, it can be used to provide some useful debug information if you try to use self.Debug()on the indicator. In fact, we actually do this in the example algorithm.

update()

Finally, there is the update()method. This method IS absolutely required. It will be called and passed some new data every time a new bar or tick of data comes in. This is where we do our number crunching and arrive at the final indicator value. In our example, we are adding HL2data to each of the queues as new data arrives. Then we simply get the average value of each queue and subtract one average from the other. Once the queue is full, we set IsReady to true.

Algorithm

The algorithm in this example is only used for testing that the indicator is working and to plot the final results. There are more in-depth articles on both of these topics here:

To load our custom indicator into our algorithm, we just need to create an instance of the indicator inside the algorithm’s __init__()method. Then we register the indicator with an asset and data feed (in this case daily SPY).

Once we have done that, our indicator will be fed data which we can access in OnData().  It is here we check to see if our indicator IsReadyand if so, check the Value with self.AO.Value. This can the be used to update the custom plot we created.

Note: Plotting  is covered in the article above.

Second Note: At the time of writing using PlotIndicator()within the Initialize()method does not work. As such, users must manually create a plot and update it in OnData()as shown in the example code. See this GitHub issue for more details.

Running The Algorithm

When running the algorithm, you will be able to see debugs printed thanks to the __repr__method we created.

Awesome Oscillator Terminal Output on QuantConnect

Also, once the results are in, we will be able to plot the final chart.

Awesome Oscillator Plot Output on QuantConnect

Note that we are just representing the chart as a simple line chart. The indicator on Tradingview (and possibly other platforms) uses a histogram and some dynamic coloring for styling. A good followup exercise would be to dive deeper into plotting to replicate the look and feel.