Zipline - context vars not saved in joblib file

Hi all.

This is example zipline algo running on daily bias in live mode.

def initialize(context):
    algo.schedule_function(rebalance, algo.date_rules.every_day(), algo.time_rules.market_open())

def rebalance(context, data):
    context.testvar += 1
def before_trading_start(context, data):
    context.testvar = 0

The problem is that the value of context.testvar is saved to jobfile incorrectly.

After the first run, it should be 1, then 2, and so on. But no matter how many times the algorithm is run when viewing joblib file, is always equal to zero. I have tried declaring context variables in initialize. This doesn't help either. Shouldn't all context variables be preloaded from joblib file? Or am I doing something wrong?

import joblib
{'testvar': 0}

Thanks in advance for help.

Zipline persists the context to file after running handle_data (even if you don’t define a handle_data function), but handle_data gets run before any scheduled functions get run. Therefore, any context updates made in scheduled functions miss out on getting persisted to file since that step has already happened.

This will be fixed in the next release. In the meantime, the workaround is to set your context variables in handle_data rather than in your scheduled functions. (If you are running an end-of-day strategy, you don’t need scheduled functions anyway and can just use handle_data instead.)

Yes, it works with handle_data. Thank you. I will use it in such way. But with minute strategies, this is big problem.

To access a patched version of the zipline service prior to the next official release, you can edit your docker-compose.yml and change:

  image: 'quantrocket/zipline:2.7.0'


  image: 'quantrocket/zipline:patch20220418'

then redeploy zipline:

docker compose up -d zipline

This issue is fixed in QuantRocket 2.8.0: