In [1]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
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 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.
In [2]:
last_close_price = USEquityPricing.close.latest
close_price_filter = last_close_price > 20
And this example produces a filter that returns True whenever the 10-day mean is below the 30-day mean.
In [3]:
mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)
mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)
mean_crossover_filter = mean_close_10 < mean_close_30
Remember, each security will get its own True
or False
value each day.
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.
In [4]:
last_close_price = USEquityPricing.close.latest
top_close_price_filter = last_close_price.top(200)
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:
In [5]:
from quantopian.pipeline.factors import AverageDollarVolume
And then, let's instantiate our average dollar volume factor.
In [6]:
dollar_volume = AverageDollarVolume(window_length=30)
By default, AverageDollarVolume
uses USEquityPricing.close
and USEquityPricing.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:
In [7]:
high_dollar_volume = (dollar_volume > 10000000)
To see what this filter looks like, let's can add it as a column to the pipeline we defined in the previous lesson.
In [8]:
def make_pipeline():
mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)
mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)
percent_difference = (mean_close_10 - mean_close_30) / mean_close_30
dollar_volume = AverageDollarVolume(window_length=30)
high_dollar_volume = (dollar_volume > 10000000)
return Pipeline(
columns={
'percent_difference': percent_difference,
'high_dollar_volume': high_dollar_volume
}
)
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.
In [9]:
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
result
Out[9]:
By default, a pipeline produces computed values each day for every asset in the Quantopian database. 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:
In [10]:
def make_pipeline():
mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)
mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)
percent_difference = (mean_close_10 - mean_close_30) / mean_close_30
dollar_volume = AverageDollarVolume(window_length=30)
high_dollar_volume = dollar_volume > 10000000
return Pipeline(
columns={
'percent_difference': percent_difference
},
screen=high_dollar_volume
)
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,100 securities
In [11]:
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
print 'Number of securities that passed the filter: %d' % len(result)
result
Out[11]:
In [12]:
low_dollar_volume = ~high_dollar_volume
This will return True
for all securities with an average dollar volume below or equal to $10,000,000 over the last 30 days.
In the next lesson, we will look at combining filters.