Back to Community
Sorting and getting top n of rows in Pipeline with Custom Factor

Dear all

Just could not get my head around a seemingly simple issue with pipeline with Custom Factor. testing it in Notebook now with the aim to use in algorithm.

The pipeline uses Custom factor that does regress on prices and extracts slopes of prices. My goal is to return a pipeline in which the result was first sorted descending by SlopeAdj factor and only say top(3) rows are returned. But SlopeAdj.sort() or SlopeAdj.sort().top() spit out errors

The code is below

Loading notebook preview...
3 responses

Use the top method. No need to sort first. The top method first sorts and then takes the 'top n' of that sorted list. Remember that SlopeAdj is a factor object. It's not the actual data like a dataframe or series. It only has specific factor object methods. The method 'sort' is not one of them. Something like this should be what you want

def make_pipeline():

    slope_adj = SlopeAdj(inputs=[USEquityPricing.close],  window_length=100) 

    # Use the 'top' method to get a filter for the highest n equities  
    # No need to sort first. The top method does that for you.  
    # Use a mask to limit the results.  
    positive_slope = slope_adj > 0  
    is_tradable = positive_slope & my_equities  
    slope_adj_top_3 =, mask=is_tradable)

    return Pipeline(  
            'slope_adj': slope_adj},  
        screen = is_tradable & slope_adj_top_3  

It's a good practice, if one will be applying a filter to the results, to use that filter as a mask to the top method. Otherwise, there is the chance that one will fetch the top n securities but then filter them out and perhaps leave none. Maybe this is the desired result but typically not.

Also, it's great you are debugging your custom factor and pipeline logic in a notebook before using it in an algo. It's generally easier to debug, but moreover, one can easily display the results, perhaps plot them, and verify they are what is expected. Keep it up!

Look at the methods that one can use with factors in the docs

See attached notebook for this pipeline in action.

Loading notebook preview...

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.


Try this:

is_tradable =,  mask = my_equities)  

Equity(114 [ADBE]) 0.000913
Equity(5061 [MSFT]) 0.001387
Equity(28016 [CMG]) 0.002009

Thanks a lot to both of you. Indeed very helpful