Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Taking forever in Backtest!

I am brand new to Quantopian, my teammate and I at the ChiPy Hackathon came up with this strategy and it is running really really slow. We cloned one of the lecture series code and added our factors to it and did a small regression. Can someone look into it and tell me what could be changed to make it work faster? I am pasting the code here for you guys to view. Appreciate any help!!

5 responses

Hi Smitha, could you clarify what you mean by really slow? Is the backtest timing out, or just taking a long time to execute? You're accessing fundamental factors in a pipeline, and that is going to run really slow until quantopian optimizes their fundamental data retrieval.

Nothing else really stands out, the data histories are of reasonable length... I'm not familiar with Ridge in sklearn, you might consider profiling that.

Sunil

Hi Sunil,

Thank you for looking into this. When I meant really slow, it took 20minutes to finish 1.3% of the backtest. I didn't let it complete the backtest because even after an hour, it was still in the below 10% range.

We have only one fundamental factor, do you think that would affect the strategy's backtest to that extent? Is there any other way we could access the fundamental factors?

Smitha

It's probably the loop in before_trading_starts.

You have six fundamental factors:

     28:    inputs = [morningstar.income_statement.ebit,  
     29:              morningstar.valuation.enterprise_value]  
     48:    inputs = [morningstar.operation_ratios.roe,]  
     62:    inputs = [mstar.income_statement.ebit, mstar.valuation.enterprise_value,  
     63:              mstar.operation_ratios.roic]  

But I don't think that's enough to explain your performance. Simon might be on the right track, this bit of code could probably be written more efficiently using pandas data manipulation operations:

        for stock in yesterday:  
            yesterday[stock]['date'] = today_date  
            yesterday[stock]['pct_change'] = pct_change[stock]  
            context.hist.append(yesterday[stock])  

Other things I've noticed in running backtests, if you're trading without a good liquidity filter, an open order might take a very long time to fill, which would cause it to keep the backtest engine busy. The code you have in your notebook has an initial_screen filter that is used in the notebook. Consider adding the filter to the pipeline screen. You could also rule out this possibility by zero'ing the slippage model.

Sunil

Oh well! In my head all those inputs are used into creating one factor. I am a more Finance person, but yes I get it! I will look into it and work . In the meantime if you find any other changes that could help, please do let me know. Thank you!

  • Smitha