QuantRocket logo

Disclaimer

Filters

A Filter is a function from an asset and a moment in time to a boolean:

F(asset, timestamp) -> boolean

In Pipeline, Filters are used for narrowing down the set of securities included in a computation or in the final output of a pipeline. There are two common ways to create a Filter: comparison operators and Factor/Classifier methods.

Comparison Operators

Comparison operators on Factors and Classifiers produce Filters. Since we haven't looked at Classifiers yet, let's stick to examples using Factors. The following example produces a filter that returns True whenever the latest close price is above $20.

And this example produces a filter that returns True whenever the 10-day mean is below the 30-day mean.

Remember, each security will get its own True or False value each day.

Factor/Classifier Methods

Various methods of the Factor and Classifier classes return Filters. Again, since we haven't yet looked at Classifiers, let's stick to Factor methods for now (we'll look at Classifier methods later). The Factor.top(n) method produces a Filter that returns True for the top n securities of a given Factor. The following example produces a filter that returns True for exactly 200 securities every day, indicating that those securities were in the top 200 by last close price across all known securities.

For a full list of Factor methods that return Filters, see the Factor API Reference.

For a full list of Classifier methods that return Filters, see the Classifier API Reference.

Dollar Volume Filter

As a starting example, let's create a filter that returns True if a security's 30-day average dollar volume is above $10,000,000. To do this, we'll first need to create an AverageDollarVolume factor to compute the 30-day average dollar volume. Let's include the built-in AverageDollarVolume factor in our imports:

And then, let's instantiate our average dollar volume factor.

By default, AverageDollarVolume uses EquityPricing.close and EquityPricing.volume as its inputs, so we don't specify them.

Now that we have a dollar volume factor, we can create a filter with a boolean expression. The following line creates a filter returning True for securities with a dollar_volume greater than 10,000,000:

To see what this filter looks like, let's can add it as a column to the pipeline we defined in the previous lesson.

If we make and run our pipeline, we now have a column high_dollar_volume with a boolean value corresponding to the result of the expression for each security.

Applying a Screen

By default, a pipeline produces computed values each day for every asset in the data bundle. Very often however, we only care about a subset of securities that meet specific criteria (for example, we might only care about securities that have enough daily trading volume to fill our orders quickly). We can tell our Pipeline to ignore securities for which a filter produces False by passing that filter to our Pipeline via the screen keyword.

To screen our pipeline output for securities with a 30-day average dollar volume greater than $10,000,000, we can simply pass our high_dollar_volume filter as the screen argument. This is what our make_pipeline function now looks like:

When we run this, the pipeline output only includes securities that pass the high_dollar_volume filter on a given day. For example, running this pipeline on May 5th, 2015 results in an output for ~2,200 securities

Inverting a Filter

The ~ operator is used to invert a filter, swapping all True values with Falses and vice-versa. For example, we can write the following to filter for low dollar volume securities:

This will return True for all securities with an average dollar volume below or equal to $10,000,000 over the last 30 days.


Next Lesson: Combining Filters