Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to filter pipeline to Market Cap > $50m?

Hi all,

I'm super new to Quantopian, and currently working through a tutorial to set up my first algorithm.

I'm trying to do something super, super simple - set up a Pipeline to screen for companies with market cap > $50m. However, I'm running into trouble and can't find anything in the docs that answers this simple question.

In the research environment I get the following error when I run the code below: "AttributeError: 'Latest' object has no attribute 'factor'"

However, if i replace
& market_cap > 50000000)
with
& market_cap.isnotnull()

the error disappears.

I'm confused. The docs states:
The most common way to construct a Filter is via one of the comparison operators (<, <=, !=, eq, >, >=) of Factor. For example, a natural way to construct a Filter for stocks with a 10-day VWAP less than $20.0 is to first construct a Factor computing 10-day VWAP and compare it to the scalar value 20.0:

from zipline.pipeline.factors import VWAP
vwap_10 = VWAP(window_length=10)
vwaps_under_20 = (vwap_10 <= 20)

market_cap is set up as a factor (a numerical value), so why isn't this working? I'm so confused! Would really appreciate any info the community could share here.

Thanks,

from quantopian.pipeline import Pipeline  
from quantopian.research import run_pipeline  
from quantopian.pipeline.filters.morningstar import Q1500US

from quantopian.pipeline.data import Fundamentals  
from quantopian.pipeline.factors.fundamentals import MarketCap



def make_pipeline():  


    sentiment_factor = sentiment.sentiment_signal.latest  
    market_cap =  Fundamentals.market_cap.latest  


    # Our universe is made up of stocks that have a non-null sentiment signal that was updated in  
    # the last day, are not within 2 days of an earnings announcement, are not announced acquisition  
    # targets, and are in the Q1500US.

    universe = (Q1500US()  
                & sentiment_factor.notnull()  
                & market_cap > 50000000)  


    pipe = Pipeline(  
        columns={  
            'sentiment': sentiment_factor,  
            'longs': (sentiment_factor >=4),  
            'shorts': (sentiment_factor<=2),  
        },  
        screen=universe  
    )  
    return pipe  
2 responses

You have it all right except for one (or maybe two) tiny mistakes.

You may be importing the wrong Sentdex data.

If you want to use the Sentdex data inside a pipeline, whether that pipeline is in the algorithm OR the research environment, then you will need to import the following.

from quantopian.pipeline.data.sentdex import sentiment_free as sentiment

You may be importing the following

from quantopian.interactive.data.sentdex import sentiment_free as sentiment

Notice 'quantopian.pipeline' vs ' quantopian.interactive'.

It's a little misleading in the data documentation (https://www.quantopian.com/data/sentdex/sentiment) because it says "Using the free sample in your pipeline algo...". What it should really say is "Using the free sample in a pipeline (whether the pipeline is used in an algo or the research environment)".

Importing 'quantopian.pipeline.data.sentdex' gets 'datacolumn' objects which each have a 'latest' method. (see the docs https://www.quantopian.com/help#importing-datasets for a description of a dataset object). However, importing 'quantopian.interactive.data.sentdex' gets blaze 'field' objects which don't have 'latest' methods.

Also, one other mistake. There needs to be parentheses around 'market_cap > 50000000'. This is because the python order of precedence for the '&' operation is higher than the ' >' operation. Python tries to do 'sentiment_factor.notnull() & market_cap ' before it does the '>' operation. Use parenthesis to force the '>' operation first. Should be like this.

    universe = (Q1500US()  
                & sentiment_factor.notnull()  
                & (market_cap > 50000000) )  

See the attached notebook showing both imports and the parenthesis fix.

Good luck.

Thank you so much for the quick and clear explanation!

Turns out the parenthesis around the the market cap > 50000000 was the key, such a small thing that i overlooked.

Much appreciated :)

Joe