Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help making algo run faster

I'm trying to test a portfolio with influence from fama french factor "Conservative Minus Aggressive." However I am struggling with timeout errors.

Does anyone have suggestions on how to make this system run a bit faster?

Thank!

http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library/f-f_5_factors_2x3.html
http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library/det_port_form_inv.html

6 responses

The main culprit in the slowdown is

class Asset(CustomFactor):  
    inputs = [morningstar.balance_sheet.total_assets]  
    window_length = 252  
    def compute(self, today, assets, out, close ):  
        out[:] = close[0]/close[-1]  

Your window_length shouldn't be 252, if you only want use the data from the previous two days

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.

Sorry, I should have added comments to the code. I am trying to rank assets based on the change in total assets from 1 year ago not over the previous two days.

No problem, I see now what you are trying to do.

I cloned your algorithm and ran it over a period of 9 months. It finished in decent time - how much faster are you trying to make it?

The performance bottleneck will come from your customfactor computation, as it's loading in a 252 day trailing window of data.

Try reducing the trailing window size. Sometimes cutting things in half means less i/o swapping on the L3 or L4 cache, so the speed improvement could be much greater.

@Steve - The economics of this idea kinda gets diminished by reducing the window size. At a minimum, the window size will need to ensure all stocks tested have data from 2 distinct balance sheets for each stock (minimum ~63 days). This will give a notion of the investment activity of each firm. I believe Fama/French use annual data in their paper. The window size is probably up for debate, by I personally think a year at a minimum is necessary to get true picture of whether the firms are "aggressive" or "conservative".

@Georges - Good idea! Despite the slow nature of this factor, have you thought about getting a little deeper in the morningstar data to possibly better reflect the investments being made by the firms? Maybe something like this for starters:

class Asset(CustomFactor):  
    inputs = [morningstar.balance_sheet.total_assets, morningstar.cash_flow_statement.capital_expenditure]  
    window_length = 252  
    def compute(self, today, assets, out, total_assets, capex):  
        investment_ratio_0 = capex[0] / total_assets[0]  
        investment_ratio_1 = capex[-1] / total_assets[-1]  
        relative_investment_growth = (investment_ratio_1 - investment_ratio_0) / investment_ratio_0  
        out[:] = relative_investment_growth  

(note, I did not test this in an algo yet, might have an error or be really slow)

Also, might even be little better to increase window size to ~260 days. This way if any companies have some lag time between respective annual reporting dates, you will be likely to catch that lag time and be calculating the change in investment using the full year.

@Frank are you using 'capex' for' capital_expenditure' does that really work no where do i remember read that you could use what ever variable as long as you what keep the order straight? is this true is this how it works?