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)