Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
help needed: Real slow pipeline

Hi All,

I seem to have a performance problem with a very simple pipeline code. The code is very straightforward but it takes a loooong time. This is really annoying even when using the pipeline only once per quarter. Here it goes:


#Factors definition  
class PriceToBook(CustomFactor):  
    window_length = 1  
    inputs = [morningstar.valuation_ratios.pb_ratio]  
    def compute(self, today, assets, out, pb_ratio):  
        out[:] = pb_ratio[-1]

class ReturnOnEquity(CustomFactor):  
    window_length = 1  
    inputs = [morningstar.operation_ratios.roe]  
    def compute(self, today, assets, out, roe):  
        out[:] = roe[-1]  

class MarketCap(CustomFactor):  
    inputs = [morningstar.valuation.shares_outstanding, USEquityPricing.close]  
    window_length = 1  
    def compute(self, today, assets, out, shares, close_price):  
        # Shares * price/share = total price = market cap  
        out[:] = shares[-1] * close_price[-1]  
def initialize(context):  
    attach_pipeline(make_pipeline(context), 'my_pipeline')

def make_pipeline(context):  
    pipe = Pipeline()  
    pb_ratio=PriceToBook()  
    pipe.add(pb_ratio,'pb')  
    roe = ReturnOnEquity()  
    pipe.add(roe,'roe')  
    mkt_cap = MarketCap()  
    mkt_cap_rank = mkt_cap.rank(ascending=False)  
    mid_cap = (500 < mkt_cap_rank) & (1000 >= mkt_cap_rank) 

    pb_rank = pb_ratio.rank(mask=mid_cap,  ascending=True)  
    pipe.add(pb_rank, 'pb_rank')

    roe_rank = roe.rank(mask=mid_cap,  ascending=False)  
    pipe.add(roe_rank, 'roe_rank')

    combo_raw = (pb_rank+roe_rank)/2  
    pipe.add(combo_raw, 'combo_raw')  
    pipe.add(combo_raw.rank(mask=mid_cap,  ascending=True).top(50), 'combo_rank')  
    pipe.set_screen(mid_cap)  
    return pipe

def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  
    context.security_list = context.output.sort(  
        ['combo_rank'],  
        ascending=True  
        ).iloc[-20:].index.tolist()  

Please help me optimize this code !

5 responses

Hi Lionel,

Thanks for posting your code. I've looked through it and tried running it myself. It looks like your code is already well-written, and I don't see any obvious optimizations that can be made. The speed at which this pipeline runs is pretty typical for one using Morningstar data. Unfortunately, accessing this fundamental data is slow; this is something that we are working on improving, but for now these results are normal.

Disclaimer

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 for your time Nathan.

I really hope you can optimize this. I realize this is a somehow not trivial engineering feat.

Keep up the good work!

Any update on speeding up pipeline for fundamental data? Even if the memory usage for backtesting was increased, testing with fundamentals is stil very, very slow!

Jamie provided an update a few days ago on a different thread: https://www.quantopian.com/posts/access-to-fundamental-data-from-previous-quarters-slash-years#594d292ca407ef000db36c41

We're definitely working on it.

Thanks
Josh

Disclaimer

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.

Great to hear it! And any update on the historical fundamentals? Why don't discuss with the community the definition of the API?

Thanks and keep the good work going on!