Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algo disqualified: backtest took too long

Hi everyone,

My algo has been disqualified from the contest because it takes too long to finish running.

I'm wondering if you have any idea about how to optimize an algo.

Thanks!

7 responses

Hi Youness,

I suggest that you use the Pipeline API in your code. With Pipeline, you can make custom factors for market cap and net income to select your universe of stocks. Behind the scenes, Pipeline should be able to help speed up your algo!

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.

Hi Jamie,

I tried to use the Pipline Api, but i'm stuck with this error: IndexError: index 0 is out of bounds for axis 0 with size 0 / There was a runtime error on line 62.

import pandas  
import numpy as np

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.data import morningstar

class AvgDailyDollarVolume(CustomFactor):  
    inputs = [USEquityPricing.close, USEquityPricing.volume]  
    window_length = 30  
    def compute(self, today, assets, out, close_price, volume):  
        dollar_volume = close_price * volume  
        avg_dollar_volume = np.mean(dollar_volume, axis=0)  
        out[:] = avg_dollar_volume  
#class Income_statement(CustomFactor):  
    # Pre-declare inputs and window_length  
    #inputs = [morningstar.income_statement]  
    #window_length = 1  
    # Compute market cap value  
    #def compute(self, today, assets, out, income):  
        #out[:] = income  
class MarketCap(CustomFactor):  
    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding]  
    window_length = 1  
    # Compute market cap value  
    def compute(self, today, assets, out, close, shares):  
        out[:] = close[-1] * shares[-1]  
def initialize(context):  
    context.size = float(context.portfolio.cash / 20)  
    context.shorting = True  
    context.shorts = []

    pipe = Pipeline()  
    attach_pipeline(pipe, 'mypipe')  
    mkt_cap = MarketCap()  
    pipe.add(mkt_cap, 'mkt_cap')  
    #income = Income_statement()  
    #pipe.add(income, 'income')  
    dollar_volume = AvgDailyDollarVolume()  
    pipe.add(dollar_volume, 'dollar_volume')  
    dv_filter = (dollar_volume > 30 * 10**6) & (dollar_volume < 200 * 10**6)  
    mkt_cap_fltr= (mkt_cap > 20*10*9) & (mkt_cap < 200*10*9)  
    pipe.set_screen(mkt_cap_fltr & dv_filter)

def before_trading_start(context, data):  
    # Call pipelive_output to get the output  
    # Note this is a dataframe where the index is the SIDs for all securities to pass my screen  
    # and the colums are the factors which I added to the pipeline  
    context.output = pipeline_output('mypipe')  
    log.info(len(context.output))  
    stocks = context.output

    update_universe(stocks.index)

Hi Youness,

That error tells me that your no securities are passing your filters. Looking at your code, I think you may have typo'd on the mkt_cap filter. you have 200*10*9 as the upper bound when I think you mean 200*(10**9)!

Hi Jamie,

Thank you so much. I'm running it right now, but it doesn't look faster than the first algo. I guess it will take more than 2 hours to complete the 2 years backtest.

If you are doing a fundamentals universe, you'll probably speed up a lot by simply removing the dollar volume universe entirely. I am not really sure how they interact anyway.

Also, you're doing a lot of work in your handle_data, which is called every minute. Since you are working with daily data anyway, you might want to simply do a schedule_function for your ordering logic every morning or whatever.

Timing code example to identify slowdowns made easy.

My algo has been disqualified from the contest because it takes too long to finish running.

There's a 1 minute time-out on handle_data and a 5 minute time-out on before_trading_start but I am not aware of a constraint on the overall runtime of a backtest. So, what is the problem?