Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Short circuit before_trading_start? #fundamentals

I am working on some fundamental-based algorithms and my rebalancing occurs fairly infrequently monthly or annually). So a daily call to before_trading_start is usually not needed and just slows down the algorithm. The problem is that when I try to short circuit the method with a conditional statement, the algorithm seems to fail to run altogether. Is there a good way to do this?

def before_trading_start(context):  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if exchange_time.month != context.rebalance_month:  
        return  
    else:  
        # Query the funadmentals database  
        ...  
        update_universe(context.fundamental_df.columns.values)  
4 responses

Hi Dan, I tried to replicate your error but couldn't given the snippet you gave. I'm able to actually skip it the get_fundamentals and update_universe calls given your snippet and arbitrarily replacing context.rebalance_month with 1 (not sure what you're setting it to).

Given the volume of times handle_data is called during the day (~330 times per day in minutely mode) I wouldn't focus too too much on before_trading_starts and optimizing it.

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 provided a more fleshed out example. I'll attach two for comparison: one where before_trading_starts runs every day and one where I attempt to short circuit it so that it only runs when needed before infrequent rebalancing). I am using the Quantopian example fundamental algorithm.

Given the volume of times handle_data is called during the day (~330 times per day in minutely mode) I wouldn't focus too too much on before_trading_starts and optimizing it.

On this point, all of my algorithm's work is done in the rebalancing function. handle_data just passes, so no work is done. The idea in short circuiting before_trading_starts is that since a lot of work is done here that is only really needed on rebalancing days, if we limit its execution to those days, the backtest runs much faster. Most days are just calculating the return on currently held positions.

The unmodified algorithm is attached. I will attach the modified one in a subsequent post.

Here's the second, modified algorithm with an added short-circuit feature that fails to work. See lines 73 - 80 for the relevant changes.

Last night it occurred to me that it seemed like the behavior was indicating that this error was only showing up when update_universe wasn't called every time before_trading_start was called. So, I changed things up a bit so that I would call update_universe in the short circuit with old data. This seems to work, but feels like a hack.

Another big help in this realm would be a way to query the net scheduled rebalancing day or days. If I added something like this in zipline is it possible that Quantopian would pull that in for the mainline product?

Thanks for your help!