Back to Community
How to filter "USEquityPricing.close.latest" using QTradableStocksUS()

I've been working through the pipeline tutorial and am now onto building my first pipeline. I have this so far:

    # Base universe  
    base_universe = QTradableStocksUS()  
    # Dollar volume factor  
    dollar_volume = AverageDollarVolume(  

    # High dollar volume filter  
    filter_dollar_volume = dollar_volume > 50000000  

I want to combine this filter with a $ threshold on the last closing price:

    latest_close = USEquityPricing.close.latest  
    filter_minClose = latest_close > 5  

How would I restrict the second filter to only selecting stocks from filter_dollar_volume?

4 responses

Filters can be logically combined using the & | and ~ operators for and or and not operations respectively. So, to restrict the second filter to only selecting stocks from 'filter_dollar_volume' one could do something like this.

    filter_minClose = (latest_close > 5 ) & filter_dollar_volume

    # alternately, all in one statement  
    filter_minClose = (latest_close > 5 ) & (dollar_volume > 50000000)

Good luck!


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Dan!

Just out of interest; how would I restrict USEquityPricing.close.latest to the stocks in QTradableStocksUS()?

The "mask=base_universe" within your AverageDollarVolume function filters for stocks in your "base-universe" which you have assigned QTradableStocksUS() to. So using Dan's code and your own will filter for stocks in QTradableStocksUS(). Another way to do it is to use the "screen" parameter in your pipleline function:

Pipeline(columns = {
'factor1':factorfunction, 'Sector':sector }, screen=QTradableStocksUS()

Thanks Niclas. Sorry - bad question phrasing from me. I'm working through Python tutorials in parallel as someone new to programming. I think I should have asked if there is a method within the .Latest class that can be used as a filter? From the Q docs I don't believe there is. That leaves me with a couple of options as you've pointed out; Dan's solution or screen at the end. Sound about right?