Wrong value received in the parameter for `start` although I passed valid date

zipline bundles output:

(BackTesting) $ zipline bundles
csvdir
custom-data-bundle 2023-10-22 15:03:39.651317
quandl
quantopian-quandl
(BackTesting) $

zipline run:

(BackTesting) $ zipline run -f src/ma_strategy_crossover.py --capital-base 100000.0 -b custom-data-bundle -s 2021-1-1 -e 2023-9-29 --no-benchmark --trading-calendar XBOM --data-frequency daily
Traceback (most recent call last):
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/utils/memoize.py”, line 56, in get
return self._cache[instance]
File “/usr/lib/python3.10/weakref.py”, line 416, in getitem
return self.data[ref(key)]
KeyError: <weakref at 0x7f77bc9ba840; to ‘BcolzDailyBarReader’ at 0x7f77bc98e560>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/mysys/StockMarkets/BackTesting/bin/zipline”, line 8, in
sys.exit(main())
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/core.py”, line 1157, in call
return self.main(*args, **kwargs)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/core.py”, line 1078, in main
rv = self.invoke(ctx)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/core.py”, line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/core.py”, line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/core.py”, line 783, in invoke
return __callback(*args, **kwargs)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/main.py”, line 112, in _
return f(*args, **kwargs)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/click/decorators.py”, line 33, in new_func
return f(get_current_context(), *args, **kwargs)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/main.py”, line 306, in run
return _run(
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/utils/run_algo.py”, line 163, in _run
data = DataPortal(
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/data/data_portal.py”, line 167, in init
last_sessions = [
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/data/data_portal.py”, line 168, in
reader.last_available_dt
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/data/bcolz_daily_bars.py”, line 493, in last_available_dt
return self.sessions[-1]
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/utils/memoize.py”, line 58, in get
self._cache[instance] = val = self.func(instance)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/zipline/data/bcolz_daily_bars.py”, line 452, in sessions
sessions = cal.sessions_in_range(start_session, end_session)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py”, line 2199, in sessions_in_range
slc = self._get_sessions_slice(start, end, _parse)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py”, line 2176, in _get_sessions_slice
start, end = self._parse_start_end_dates(start, end, _parse)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/exchange_calendars/exchange_calendar.py”, line 2172, in _parse_start_end_dates
return parse_date(start, “start”, self), parse_date(end, “end”, self)
File “/home/mysys/StockMarkets/BackTesting/lib/python3.10/site-packages/exchange_calendars/calendar_helpers.py”, line 393, in parse_date
raise errors.DateOutOfBounds(calendar, ts, param_name)
exchange_calendars.errors.DateOutOfBounds: Parameter start receieved as ‘2003-10-22 00:00:00’ although cannot be earlier than the first session of calendar ‘XBOM’ (‘2003-10-23 00:00:00’).

Even though, I pass start parameter as value of -s 2021-1-1, still it is converted to 2003-10-22 00:00:00. How to resolve this issue?

If I remove that data bundle, ingest data again then it’s working fine. Any caching issue?

Hey,

this is quite old, but I am currently having the same problems.
Did you ever figure out what the problem was/is?

I feel this happens when I use a different calendar than default, ingest bundle, then wait a day. So somehow the lazyval or cache is responsible for this I guess, but couldn’t yet figure exactly where this goes wrong…

In case anybody ever has the same problem again. The issue is that the exchange_calendar package has a default parameter set for the start date, which is the later of 20 years from today and the official start of the calendar, which zipline only overwrites for US exchanges.

I personally get around this by changing the environment variable of the exchange_calendar module:

  • within utils/calendar_utils.py, I added…
from exchange_calendars import exchange_calendar
exchange_calendar.GLOBAL_DEFAULT_START = pd.Timestamp("1980-01-01")

and changed the get_calendar method to the following:

@wrap_with_signature(inspect.signature(ec_get_calendar))
def get_calendar(name, *args, **kwargs):
    return ec_get_calendar(name, *args, side="right", **kwargs)

but there are other ways of fixing this obviously.

Please use at own risk and make sure that this doesn’t interfer with any other of your logic… :wink: