QuantRocket logo

Disclaimer

Custom Factors

When we first looked at factors, we explored the set of built-in factors. Frequently, a desired computation isn't included as a built-in factor. One of the most powerful features of the Pipeline API is that it allows us to define our own custom factors. When a desired computation doesn't exist as a built-in, we define a custom factor.

Conceptually, a custom factor is identical to a built-in factor. It accepts inputs, window_length, and mask as constructor arguments, and returns a Factor object each day.

Let's take an example of a computation that doesn't exist as a built-in: standard deviation. To create a factor that computes the standard deviation over a trailing window, we can subclass zipline.pipeline.CustomFactor and implement a compute method whose signature is:

def compute(self, today, asset_ids, out, *inputs):
    ...

Of these, *inputs and out are most commonly used.

An instance of CustomFactor that has been added to a pipeline will have its compute method called every day. For example, let's define a custom factor that computes the standard deviation of the close price over the last 5 days. To start, let's add CustomFactor and numpy to our import statements.

Next, let's define our custom factor to calculate the standard deviation over a trailing window using numpy.nanstd:

Finally, let's instantiate our factor in make_pipeline():

When this pipeline is run, StdDev.compute() will be called every day with data as follows:

Default Inputs

When writing a custom factor, we can set default inputs and window_length in our CustomFactor subclass. For example, let's define the TenDayMeanDifference custom factor to compute the mean difference between two data columns over a trailing window using numpy.nanmean. Let's set the default inputs to [EquityPricing.close, EquityPricing.open] and the default window_length to 10:

Remember in this case that `close` and `open` are each 10 x ~8000 2D numpy arrays.

If we call TenDayMeanDifference without providing any arguments, it will use the defaults.

The defaults can be manually overridden by specifying arguments in the constructor call.

Further Example

Let's take another example where we build a momentum custom factor and use it to create a filter. We will then use that filter as a screen for our pipeline.

Let's start by defining a Momentum factor to be the division of the most recent close price by the close price from n days ago where n is the window_length.

Now, let's instantiate our Momentum factor (twice) to create a 10-day momentum factor and a 20-day momentum factor. Let's also create a positive_momentum filter returning True for securities with both a positive 10-day momentum and a positive 20-day momentum.

Next, let's add our momentum factors and our positive_momentum filter to make_pipeline. Let's also pass positive_momentum as a screen to our pipeline.

Running this pipeline outputs the standard deviation and each of our momentum computations for securities with positive 10-day and 20-day momentum.

Custom factors allow us to define custom computations in a pipeline. They are frequently the best way to perform computations on multiple data columns. The full documentation for CustomFactors is available in the API Reference.


Next Lesson: Creating the TradableStocksUS Universe