stefan-jansen / zipline-reloaded

Zipline, a Pythonic Algorithmic Trading Library
https://zipline.ml4trading.io
Apache License 2.0
1.12k stars 208 forks source link

AssertionError: All readers must share target trading_calendar #97

Closed RiccaDS closed 2 years ago

RiccaDS commented 2 years ago

Dear Zipline Maintainers,

Before I tell you about my issue, let me describe my environment:

Environment

Now that you know a little about me, let me tell you about the issue I am having:

When trying to create a data portal I receive the following error when passing the trading calendar to the the DataPortal function:

AssertionError: All readers must share target trading_calendar. Reader=<zipline.data.minute_bars.BcolzMinuteBarReader object at 0x7f2756788b50> for type=<class 'zipline.assets._assets.Equity'> uses calendar=<exchange_calendars.exchange_calendar_xnys.XNYSExchangeCalendar object at 0x7f27567513a0> which does not match the desired shared calendar=<exchange_calendars.exchange_calendar_xnys.XNYSExchangeCalendar object at 0x7f27567d1af0>

Here is how you can reproduce this issue on your machine:

Reproduction Steps

  1. Open jupyter notebook in your environment
  2. Copy/paste following code:
    
    import pandas as pd
    from zipline.data import bundles
    from zipline.data.data_portal import DataPortal
    import exchange_calendars as xcals

start_session = pd.Timestamp('2021-8-2', tz='utc') end_session = pd.Timestamp('2022-8-12', tz='utc')

bundles.register( 'il_mio_bundle', bundles.csvdir.csvdir_equities( ['minute'], '/home/riccardo/Scrivania/algo', ), calendar_name='XNYS', # US equities start_session=start_session, end_session=end_session )

start_def = '2021-08-02' end_def = '2021-08-12'

bundle_data = bundles.load('il_mio_bundle')

data_portal = DataPortal(bundle_data.asset_finder, trading_calendar = xcals.get_calendar('XNYS'), #THIS LINE GENERATES THE ERROR first_trading_day = pd.Timestamp(start_def, tz='UTC'), equity_daily_reader = bundle_data.equity_minute_bar_reader, adjustment_reader = bundle_data.adjustment_reader)

4.make sure you have a csv ingested bundle and to point it out in the code. 

## What steps have you taken to resolve this already?
As suggested by another user i commented out the following code in zipline/data/dispatch_bar_reader.py line 49

for t, r in self._readers.items(): assert trading_calendar == r.trading_calendar, ( "All readers must share target trading_calendar. " "Reader={0} for type={1} uses calendar={2} which does not " "match the desired shared calendar={3} ".format( r, t, r.trading_calendar, trading_calendar ) )


This seems to be working, however I would like to understand where I am coding wrong. Zipline is coded fairly intricated for my python skills. 

Sincerely,
Riccardo
MBounouar commented 2 years ago

Hi, First, you shouldn't directly import and use exchange_calendars, it is "incompatible" with zipline-reloaded (+1min delay in exchange opening). I would import get_calendar from zipline, this should ensure that you get the a calendar that is compatible. Apparently, it "works" because you simply commented out a safety check that ensures that the trading calendar is the same. Cheers, M

RiccaDS commented 2 years ago

@MBounouar hi, yes indeed commenting isn't a good solution. Using exchange_calendars was the only solution I found to make the script go along a bit further. Before that I did try I importing get_calendar but I now realize I was importing it from trading_calendar module out of zipline, and this was giving another error. So now following your suggestion I searched better for get_calendar:

from zipline.utils.calendar_utils import get_calendar
data_portal = DataPortal(bundle_data.asset_finder,
                         trading_calendar = get_calendar("XNYS"),
                         first_trading_day = start_aware,
                         equity_daily_reader = bundle_data.equity_minute_bar_reader,
                         adjustment_reader = bundle_data.adjustment_reader)

In this way it works although I still have some issues in my code. Thank you for the help!