Back to Community
What to do if the backtest is too slow?

I cloned and runned the full backtest "long-short-equity" algo from Lecture39, see here.
Although the period is only one year long, I could not finish it in one hour.
Anything I can do at this moment? Thanks.

2 responses

Hmm,

You are right. It's slow.

Try deleting the entire 'handle_data' method (which runs every minute). Move the recording logic somewhere else to only run once a day. Perhaps move it to the 'daily_clean' function.

def daily_clean(context, data):

    for stock in context.portfolio.positions:  
        if stock not in context.security_set and data.can_trade(stock):  
            order_target_percent(stock, 0)

    record(num_positions=len(context.portfolio.positions),  
           exposure=context.account.net_leverage,  
           leverage=context.account.leverage)

Not sure why the recording was done every minute. Generally, limit the use of the 'handle_data' function to keep things fast.

Another thing to try to speed the algorithm up is to replace the 'filter_universe' function with one of the newer Q500US or Q1500US built in filters.

See attached.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58b05edaac1c9e61f3ef3746
There was a runtime error.

@Dan Once again, thanks for your help. It works normal.