quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.71k stars 4.73k forks source link

register_calendar() gives "Inferred time zone not equal to passed time zone" because _all_days in trading_calendar.py has hardcoded tz='UTC' #2395

Open algonitish opened 5 years ago

algonitish commented 5 years ago

Dear Zipline Maintainers,

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

Environment

* Operating System: Windows 10 Pro * Python Version: 3.6.6 * Python Bitness: 64 * How did you install Zipline: pip * Python packages: alabaster==0.7.11 alembic==1.0.0 anaconda-client==1.6.14 anaconda-navigator==1.9.4 anaconda-project==0.8.2 appdirs==1.4.3 archiveis==0.0.6 asn1crypto==0.24.0 astroid==1.6.5 astropy==3.0.3 atomicwrites==1.1.5 attrs==18.1.0 Automat==0.7.0 Babel==2.6.0 backcall==0.1.0 backports.shutil-get-terminal-size==1.0.0 bcolz==0.12.1 beautifulsoup4==4.6.1 bitarray==0.8.3 bkcharts==0.2 blaze==0.11.3 bleach==2.1.3 bokeh==0.13.0 boto==2.49.0 bottle==0.12.16 Bottleneck==1.2.1 breadability==0.1.20 cachetools==2.1.0 certifi==2018.11.29 cffi==1.11.5 chardet==3.0.4 click==6.7 cloudpickle==0.5.3 clyent==1.2.2 colorama==0.3.9 comtypes==1.1.6 conda==4.5.11 conda-build==3.12.0 conda-verify==3.1.0 constantly==15.1.0 contextlib2==0.5.5 cryptography==2.2.2 cssselect==1.0.3 cycler==0.10.0 cyordereddict==1.0.0 Cython==0.28.4 cytoolz==0.9.0.1 dask==0.18.2 datashape==0.5.4 decorator==4.3.0 distributed==1.22.0 docopt==0.6.2 docutils==0.14 EasyProcess==0.2.3 empyrical==0.5.0 entrypoints==0.2.3 et-xmlfile==1.0.1 fake-useragent==0.1.10 fastcache==1.0.2 feedfinder2==0.0.4 feedparser==5.2.1 ffn==0.3.4 filelock==3.0.4 Flask==1.0.2 Flask-Cors==3.0.6 future==0.16.0 gevent==1.3.5 glob2==0.6 google-api-core==1.4.0 google-auth==1.5.1 google-cloud-core==0.28.1 google-cloud-storage==1.12.0 google-resumable-media==0.3.1 googleapis-common-protos==1.5.3 goose-extractor==1.0.25 greenlet==0.4.14 h5py==2.8.0 heapdict==1.0.0 html5lib==1.0.1 hyperlink==18.0.0 ib-insync==0.9.41 ibapi==9.73.7 IbPy2==0.8.0 idna==2.7 imageio==2.3.0 imagesize==1.0.0 incremental==17.5.0 intervaltree==2.1.0 ipykernel==4.8.2 ipython==6.5.0 ipython-genutils==0.2.0 ipywidgets==7.3.1 isort==4.3.4 itsdangerous==0.24 jdcal==1.4 jedi==0.12.1 jieba3k==0.35.1 Jinja2==2.10 jsonschema==2.6.0 jupyter==1.0.0 jupyter-client==5.2.3 jupyter-console==5.2.0 jupyter-core==4.4.0 jupyterlab==0.33.4 jupyterlab-launcher==0.11.2 keyring==13.2.1 Kivy==1.10.1 Kivy-Garden==0.1.4 kiwisolver==1.0.1 lazy-object-proxy==1.3.1 llvmlite==0.24.0 locket==0.2.0 Logbook==1.4.0 lru-dict==1.1.6 lxml==4.2.3 Mako==1.0.7 MarkupSafe==1.0 matplotlib==2.2.2 mccabe==0.6.1 MechanicalSoup==0.11.0 menuinst==1.4.14 mistune==0.8.3 mkl-fft==1.0.4 mkl-random==1.0.1 more-itertools==4.2.0 mpmath==1.0.0 msgpack==0.5.6 msgpack-python==0.5.6 multipledispatch==0.5.0 mysql-connector-python==8.0.12 navigator-updater==0.2.1 nbconvert==5.3.1 nbformat==4.4.0 nest-asyncio==0.9.10 networkx==1.11 newspaper3k==0.2.6 nltk==3.3 nose==1.3.7 notebook==5.6.0 numba==0.39.0 numexpr==2.6.5 numpy==1.15.0 numpydoc==0.8.0 odo==0.5.1 olefile==0.45.1 openpyxl==2.5.4 packaging==17.1 pandas==0.22.0 pandas-compat==0.1.1 pandas-datareader==0.6.0 pandocfilters==1.4.2 parsel==1.5.0 parso==0.3.1 partd==0.3.8 path.py==11.0.1 pathlib2==2.3.2 patsy==0.5.0 pep8==1.7.1 pickleshare==0.7.4 Pillow==5.1.0 pkginfo==1.4.2 pluggy==0.6.0 ply==3.11 praw==5.4.0 prawcore==0.14.0 prometheus-client==0.3.1 prompt-toolkit==1.0.15 protobuf==3.6.1 psutil==5.4.6 py==1.5.4 pyasn1==0.4.4 pyasn1-modules==0.2.2 pycodestyle==2.4.0 pycosat==0.6.3 pycparser==2.18 pycrypto==2.6.1 pycurl==7.43.0.2 PyDispatcher==2.0.5 pyfin==1.0.0a1 pyflakes==2.0.0 pygame==1.9.4 Pygments==2.2.0 PyHamcrest==1.9.0 pylint==1.9.2 pyodbc==4.0.23 pyOpenSSL==18.0.0 pyparsing==2.2.0 PyQt5==5.11.2 PyQt5-sip==4.19.12 PySocks==1.6.8 pytest==3.6.4 pytest-arraydiff==0.2 pytest-astropy==0.4.0 pytest-doctestplus==0.1.3 pytest-openfiles==0.3.0 pytest-remotedata==0.3.0 python-dateutil==2.7.3 python-editor==1.0.3 pytube==9.2.3 pytz==2018.5 PyVirtualDisplay==0.2.1 PyWavelets==0.5.2 pywin32==223 pywinpty==0.5.4 PyYAML==3.13 pyzmq==17.0.0 QDarkStyle==2.6.4 qPython==1.2.2 QtAwesome==0.4.4 qtconsole==4.3.1 QtPy==1.4.2 QuantLib-Python==1.13 queuelib==1.5.0 requests==2.19.1 requests-file==1.4.3 requests-ftp==0.3.1 rope==0.10.7 rsa==4.0 ruamel-yaml==0.15.46 scikit-image==0.14.0 scikit-learn==0.19.1 scipy==1.1.0 Scrapy==1.5.1 seaborn==0.9.0 Send2Trash==1.5.0 service-identity==17.0.0 simplegeneric==0.8.1 simplejson==3.16.0 singledispatch==3.4.0.3 six==1.11.0 snowballstemmer==1.2.1 sortedcollections==1.0.1 sortedcontainers==2.0.4 Sphinx==1.7.6 sphinxcontrib-websupport==1.1.0 spyder==3.3.0 spyder-kernels==0.2.4 SQLAlchemy==1.2.10 statsmodels==0.9.0 sumy==0.7.0 sympy==1.2 tables==3.4.4 tabulate==0.8.2 tblib==1.3.2 terminado==0.8.1 testpath==0.3.1 tldextract==2.2.0 toolz==0.9.0 tornado==5.0.2 trading-calendars==1.1.0 traitlets==4.3.2 Twisted==18.9.0 twitter-text-python==1.1.0 typing==3.6.4 unicodecsv==0.14.1 update-checker==0.16 upstox==1.5.5 urllib3==1.23 w3lib==1.19.0 wcwidth==0.1.7 webencodings==0.5.1 websocket-client==0.40.0 Werkzeug==0.14.1 widgetsnbextension==3.3.1 wikiquote==0.1.7 win-inet-pton==1.0.1 win-unicode-console==0.5 wincertstore==0.2 wrapt==1.10.11 xlrd==1.1.0 XlsxWriter==1.0.5 xlwings==0.11.8 xlwt==1.3.0 yahooweather==0.10 youtube-python==1.0.12 zict==0.1.3 zipline==1.3.0 zope.interface==4.5.0

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

Description of Issue

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "C:\Users\builder\AppData\Roaming\Python\Python36\site-packages\trading_calendars\trading_calendar.py", line 75, in __init__
    _all_days = date_range(start, end, freq=self.day, tz='UTC')
  File "C:\Users\builder\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py", line 2057, in date_range
    closed=closed, **kwargs)
  File "C:\Users\builder\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 118, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\builder\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py", line 324, in __new__
    ambiguous=ambiguous)
  File "C:\Users\builder\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py", line 468, in _generate
    raise AssertionError("Inferred time zone not equal to passed "
AssertionError: Inferred time zone not equal to passed time zone

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

Reproduction Steps

  1. start_session = pd.Timestamp('2018-10-03 09:15:00', tz='Asia/Kolkata')
  2. end_session = pd.Timestamp('2018-12-31 15:30:00', tz='Asia/Kolkata')
register_calendar(
    'NSE',
    NSEExchangeCalendar( #NSEExchangeCalendar(TradingCalendar) is the class in the exchange_calendar_nse.py as required with all Indian holidays and '@property's 
        start=start_session,
        end=end_session
    )
)

What steps have you taken to resolve this already?

The error traces to line 75 of trading_calendar.py with the function: _all_days = date_range(start, end, freq=self.day, tz='UTC') here tz='UTC' is hardcoded, which makes it impossible for any other timezone to be verified. I would have rewritten this part, but the fact that it exists means that there's a workaround or something I am not doing correctly.

Anything else?

As opposed to following the csvdir method I have rolled my own procedure for creating a panel from market data I am getting from my brokers. The end result I am aiming for is running the most basic zipline.run_algorithm() with a trading_calendar=[the above NSE calendar]

Sincerely, algonitish

willianpaixao commented 5 years ago

I'm having a similar issue. While trying to ingest non-USA data, the bundle registration simply fails for not having the same timezone. Leaving as UTC leads to successful ingestion, but I assume if I had intraday data, the timestamps would be wrong. Here my error message when setting a non UTC timezone:

File "zipline/data/bundles/core.py", line 400, in ingest                                                                                 
    end_session,                                                                                                                                                                                                   
  File "zipline/data/us_equity_pricing.py", line 201, in __init__                                                                          
    "Start session %s is invalid!" % start_session                                                                                                                                                                 
ValueError: Start session 2017-01-03 00:00:00-02:00 is invalid!
xiabai84 commented 4 years ago

I have the same problem! zipline works only with utc timezone.