stefan-jansen / zipline-reloaded

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

AssertionError: Algorithm should have a utc datetime #233

Closed jasonstrimpel closed 1 week ago

jasonstrimpel commented 10 months ago

Dear Zipline Maintainers,

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

Environment

* Operating System: macOS M2 * Python Version: 3.9.13 * Python Bitness: 64 * How did you install Zipline: pip install zipline-reloaded * Python packages: ### packages in environment at /Users/jason/anaconda3/envs/newsletter: aiodns 3.0.0 pypi_0 pypi aiofiles 22.1.0 pypi_0 pypi aiohttp 3.8.5 pypi_0 pypi aiosignal 1.3.1 pypi_0 pypi aiosqlite 0.19.0 pypi_0 pypi alembic 1.11.1 pypi_0 pypi alpha-vantage 2.3.1 pypi_0 pypi alphalens-reloaded 0.4.3 pypi_0 pypi altair 5.0.1 pypi_0 pypi ansiwrap 0.8.4 pypi_0 pypi anyio 3.7.1 pypi_0 pypi aom 3.5.0 h7ea286d_0 conda-forge appdirs 1.4.4 pypi_0 pypi appnope 0.1.3 pypi_0 pypi arch 5.6.0 pypi_0 pypi argon2-cffi 21.3.0 pypi_0 pypi argon2-cffi-bindings 21.2.0 pypi_0 pypi arrow 1.2.3 pypi_0 pypi ascii-magic 1.6 pypi_0 pypi astor 0.8.1 pypi_0 pypi astropy 5.3.1 pypi_0 pypi asttokens 2.2.1 pypi_0 pypi async-lru 2.0.4 pypi_0 pypi async-timeout 4.0.2 pypi_0 pypi attrs 23.1.0 pypi_0 pypi autoflake 2.2.0 pypi_0 pypi babel 2.12.1 pypi_0 pypi backcall 0.2.0 pypi_0 pypi backoff 2.2.1 pypi_0 pypi base58 2.1.1 pypi_0 pypi bcolz-zipline 1.2.6 pypi_0 pypi beautifulsoup4 4.12.2 pypi_0 pypi black 23.7.0 pypi_0 pypi bleach 6.0.0 pypi_0 pypi blinker 1.6.2 pypi_0 pypi blosc 1.21.4 hc338f07_0 conda-forge blosc2 2.0.0 pypi_0 pypi bottleneck 1.3.7 pypi_0 pypi brotli 1.0.9 h1a8c8d9_9 conda-forge brotli-bin 1.0.9 h1a8c8d9_9 conda-forge brotli-python 1.0.9 py39h23fbdae_9 conda-forge bs4 0.0.1 pypi_0 pypi bt 0.2.9 pypi_0 pypi build 0.10.0 pypi_0 pypi bzip2 1.0.8 h3422bc3_4 conda-forge c-ares 1.19.1 hb547adb_0 conda-forge c-blosc2 2.10.0 h068da5f_0 conda-forge ca-certificates 2023.7.22 hf0a4a13_0 conda-forge cachecontrol 0.12.14 pypi_0 pypi cachetools 5.3.1 pypi_0 pypi cairo 1.16.0 h1e71087_1016 conda-forge catboost 1.1.1 pypi_0 pypi ccxt 4.0.40 pypi_0 pypi certifi 2023.7.22 pyhd8ed1ab_0 conda-forge cffi 1.15.1 pypi_0 pypi charset-normalizer 3.1.0 pypi_0 pypi chroma 0.2.0 pypi_0 pypi chroma-hnswlib 0.7.2 pypi_0 pypi chromadb 0.4.5 pypi_0 pypi cleo 2.0.1 pypi_0 pypi click 8.1.6 pypi_0 pypi cmake 3.26.4 hc0af03a_0 conda-forge colorama 0.4.6 pypi_0 pypi coloredlogs 15.0.1 pypi_0 pypi comm 0.1.3 pypi_0 pypi commonmark 0.9.1 pypi_0 pypi contourpy 1.1.0 pypi_0 pypi convertdate 2.4.0 pypi_0 pypi crashtest 0.4.1 pypi_0 pypi cryptography 41.0.2 pypi_0 pypi cssselect 1.2.0 pypi_0 pypi cssselect2 0.7.0 pypi_0 pypi curl 8.1.1 h80987f9_0 cvxpy 1.3.2 pypi_0 pypi cycler 0.11.0 pyhd8ed1ab_0 conda-forge cython 3.0.0 pypi_0 pypi dataclasses-json 0.5.13 pypi_0 pypi dateparser 1.1.8 pypi_0 pypi datetime 5.2 pypi_0 pypi dav1d 1.2.1 hb547adb_0 conda-forge debugpy 1.6.7 pypi_0 pypi decorator 5.1.1 pypi_0 pypi defusedxml 0.7.1 pypi_0 pypi degiro-connector 2.0.21 pypi_0 pypi detecta 0.0.5 pypi_0 pypi dill 0.3.7 pypi_0 pypi distlib 0.3.7 pypi_0 pypi dnspython 2.4.1 pypi_0 pypi dulwich 0.21.5 pypi_0 pypi ecdsa 0.18.0 pypi_0 pypi ecos 2.0.11 py39h4d8bf0d_0 conda-forge empyrical 0.5.5 pypi_0 pypi empyrical-reloaded 0.5.9 pypi_0 pypi entrypoints 0.4 pypi_0 pypi et-xmlfile 1.1.0 pypi_0 pypi exceptiongroup 1.1.2 pypi_0 pypi exchange-calendars 4.2.8 pypi_0 pypi executing 1.2.0 pypi_0 pypi expat 2.5.0 hb7217d7_1 conda-forge faiss-cpu 1.7.4 pypi_0 pypi falcon 3.1.1 pypi_0 pypi fastapi 0.99.1 pypi_0 pypi fastjsonschema 2.18.0 pypi_0 pypi feedfinder2 0.0.4 pypi_0 pypi feedparser 6.0.10 pypi_0 pypi ffmpeg 6.0.0 gpl_h474fd3e_103 conda-forge ffn 0.3.6 pypi_0 pypi filelock 3.12.2 pypi_0 pypi financedatabase 2.1.0 pypi_0 pypi finnhub-python 2.4.18 pypi_0 pypi finviz 1.4.6 pypi_0 pypi finvizfinance 0.14.7rc1 pypi_0 pypi flake8 6.0.0 pypi_0 pypi flatbuffers 23.5.26 pypi_0 pypi font-ttf-dejavu-sans-mono 2.37 hab24e00_0 conda-forge font-ttf-inconsolata 3.000 h77eed37_0 conda-forge font-ttf-source-code-pro 2.038 h77eed37_0 conda-forge font-ttf-ubuntu 0.83 hab24e00_0 conda-forge fontconfig 2.14.2 h82840c6_0 conda-forge fonts-conda-ecosystem 1 0 conda-forge fonts-conda-forge 1 0 conda-forge fonttools 4.41.1 pypi_0 pypi formulaic 0.6.4 pypi_0 pypi fqdn 1.5.1 pypi_0 pypi fred 3.1 pypi_0 pypi fredapi 0.4.3 pypi_0 pypi freetype 2.12.1 hd633e50_1 conda-forge fribidi 1.0.10 h27ca646_0 conda-forge frozendict 2.3.8 pypi_0 pypi frozenlist 1.4.0 pypi_0 pypi fsspec 2023.6.0 pypi_0 pypi fundamentalanalysis 0.2.14 pypi_0 pypi future 0.18.3 pypi_0 pypi gettext 0.21.1 h0186832_0 conda-forge git 2.39.1 pl5321h7727c56_0 conda-forge gitdb 4.0.10 pypi_0 pypi gitpython 3.1.32 pypi_0 pypi gmp 6.2.1 h9f76cd9_0 conda-forge gnutls 3.7.8 h9f1a10d_0 conda-forge google-search-results 2.4.2 pypi_0 pypi graphite2 1.3.13 h9f76cd9_1001 conda-forge grpcio 1.56.2 pypi_0 pypi h11 0.14.0 pypi_0 pypi h5py 3.9.0 pypi_0 pypi harfbuzz 7.3.0 h46e5fef_0 conda-forge hdf5 1.14.0 nompi_hc28a057_103 conda-forge hijri-converter 2.3.1 pypi_0 pypi holidays 0.14.2 pypi_0 pypi html5lib 1.1 pypi_0 pypi httpcore 1.0.1 pypi_0 pypi httptools 0.6.0 pypi_0 pypi httpx 0.25.1 pypi_0 pypi huggingface-hub 0.16.4 pypi_0 pypi humanfriendly 10.0 pypi_0 pypi icu 72.1 he12128b_0 conda-forge idna 3.4 pyhd8ed1ab_0 conda-forge imageio 2.31.1 pypi_0 pypi importlib-metadata 6.8.0 pypi_0 pypi importlib-resources 6.0.0 pypi_0 pypi inflection 0.5.1 pypi_0 pypi installer 0.6.0 pypi_0 pypi interface-meta 1.3.0 pypi_0 pypi intervaltree 3.1.0 pypi_0 pypi intrinio-sdk 6.24.1 pypi_0 pypi ipykernel 6.25.0 pypi_0 pypi ipympl 0.8.4 pypi_0 pypi ipython 8.11.0 pypi_0 pypi ipython-genutils 0.2.0 pypi_0 pypi ipywidgets 8.0.7 pypi_0 pypi iso3166 2.1.1 pypi_0 pypi iso4217 1.11.20220401 pypi_0 pypi iso8601 0.1.16 pypi_0 pypi isodate 0.6.1 pypi_0 pypi isoduration 20.11.0 pypi_0 pypi isort 5.12.0 pypi_0 pypi jaraco-classes 3.3.0 pypi_0 pypi jedi 0.18.2 pypi_0 pypi jieba3k 0.35.1 pypi_0 pypi jinja2 3.1.2 pypi_0 pypi joblib 1.3.0 pyhd8ed1ab_1 conda-forge json5 0.9.14 pypi_0 pypi jsonpointer 2.4 pypi_0 pypi jsonschema 4.18.4 pypi_0 pypi jsonschema-specifications 2023.7.1 pypi_0 pypi jupyter-client 7.4.1 pypi_0 pypi jupyter-console 6.6.3 pypi_0 pypi jupyter-contrib-core 0.4.2 pypi_0 pypi jupyter-contrib-nbextensions 0.7.0 pypi_0 pypi jupyter-core 5.3.1 pypi_0 pypi jupyter-events 0.6.3 pypi_0 pypi jupyter-highlight-selected-word 0.2.0 pypi_0 pypi jupyter-lsp 2.2.0 pypi_0 pypi jupyter-nbextensions-configurator 0.6.3 pypi_0 pypi jupyter-server 1.23.6 pypi_0 pypi jupyter-server-fileid 0.9.0 pypi_0 pypi jupyter-server-terminals 0.4.4 pypi_0 pypi jupyter-server-ydoc 0.8.0 pypi_0 pypi jupyter-ydoc 0.2.5 pypi_0 pypi jupyterlab 3.6.6 pypi_0 pypi jupyterlab-pygments 0.2.2 pypi_0 pypi jupyterlab-server 2.24.0 pypi_0 pypi jupyterlab-widgets 3.0.8 pypi_0 pypi keyring 23.13.1 pypi_0 pypi kiwisolver 1.4.4 py39haaf3ac1_1 conda-forge korean-lunar-calendar 0.3.1 pypi_0 pypi krb5 1.19.4 h8380606_0 lame 3.100 h1a8c8d9_1003 conda-forge langchain 0.0.266 pypi_0 pypi langsmith 0.0.57 pypi_0 pypi lcms2 2.15 hd835a16_1 conda-forge lerc 4.0.0 h9a09cb3_0 conda-forge libaec 1.0.6 hb7217d7_1 conda-forge libass 0.17.1 h4da34ad_0 conda-forge libblas 3.9.0 17_osxarm64_openblas conda-forge libbrotlicommon 1.0.9 h1a8c8d9_9 conda-forge libbrotlidec 1.0.9 h1a8c8d9_9 conda-forge libbrotlienc 1.0.9 h1a8c8d9_9 conda-forge libcblas 3.9.0 17_osxarm64_openblas conda-forge libcurl 8.1.1 h0f1d93c_0 libcxx 16.0.6 h4653b0c_0 conda-forge libdeflate 1.18 h1a8c8d9_0 conda-forge libedit 3.1.20221030 h80987f9_0 libev 4.33 h642e427_1 conda-forge libexpat 2.5.0 hb7217d7_1 conda-forge libffi 3.4.2 h3422bc3_5 conda-forge libgfortran 5.0.0 12_3_0_hd922786_1 conda-forge libgfortran5 12.3.0 ha3a6a3e_1 conda-forge libglib 2.76.4 h24e9cb9_0 conda-forge libiconv 1.17 he4db4b2_0 conda-forge libidn2 2.3.4 h1a8c8d9_0 conda-forge libjpeg-turbo 2.1.5.1 h1a8c8d9_0 conda-forge liblapack 3.9.0 17_osxarm64_openblas conda-forge libnghttp2 1.51.0 hd184df1_0 conda-forge libopenblas 0.3.23 openmp_hc731615_0 conda-forge libopus 1.3.1 h27ca646_1 conda-forge libosqp 0.6.3 h13dd4ca_0 conda-forge libpng 1.6.39 h76d750c_0 conda-forge libqdldl 0.1.5 hb7217d7_1 conda-forge libsqlite 3.42.0 hb31c410_0 conda-forge libssh2 1.10.0 hb80f160_3 conda-forge libtasn1 4.19.0 h1a8c8d9_0 conda-forge libtiff 4.5.1 h23a1a89_0 conda-forge libunistring 0.9.10 h3422bc3_0 conda-forge libuv 1.44.2 hb547adb_1 conda-forge libvpx 1.13.0 h7ea286d_0 conda-forge libwebp-base 1.3.1 hb547adb_0 conda-forge libxcb 1.15 hf346824_0 conda-forge libxml2 2.11.4 he3bdae6_0 conda-forge libzlib 1.2.13 h53f4e23_5 conda-forge lightgbm 3.3.5 py39h7dbb43e_0 conda-forge linearmodels 4.31 pypi_0 pypi llama-index 0.8.29 pypi_0 pypi llvm-openmp 16.0.6 h1c12783_0 conda-forge llvmlite 0.39.1 pypi_0 pypi lockfile 0.12.2 pypi_0 pypi logbook 1.6.0 pypi_0 pypi loguru 0.7.0 pypi_0 pypi lru-dict 1.2.0 pypi_0 pypi lxml 4.9.3 pypi_0 pypi lz4-c 1.9.4 hb7217d7_0 conda-forge mako 1.2.4 pypi_0 pypi markdownify 0.11.6 pypi_0 pypi markupsafe 2.1.3 pypi_0 pypi marshmallow 3.20.1 pypi_0 pypi matplotlib 3.7.2 pypi_0 pypi matplotlib-base 3.5.3 py39ha500c34_2 conda-forge matplotlib-inline 0.1.6 pypi_0 pypi mccabe 0.7.0 pypi_0 pypi mistune 3.0.1 pypi_0 pypi monotonic 1.6 pypi_0 pypi more-itertools 10.0.0 pypi_0 pypi mplfinance 0.12.9b7 pypi_0 pypi mpmath 1.3.0 pypi_0 pypi msgpack 1.0.5 pypi_0 pypi mstarpy 0.0.4 pypi_0 pypi multidict 6.0.4 pypi_0 pypi multipledispatch 1.0.0 pypi_0 pypi multitasking 0.0.11 pypi_0 pypi munkres 1.1.4 pyh9f0ad1d_0 conda-forge mypy-extensions 1.0.0 pypi_0 pypi nbclassic 1.0.0 pypi_0 pypi nbclient 0.8.0 pypi_0 pypi nbconvert 7.7.3 pypi_0 pypi nbformat 5.9.1 pypi_0 pypi ncurses 6.4 h7ea286d_0 conda-forge nest-asyncio 1.5.6 pypi_0 pypi nettle 3.8.1 h63371fa_1 conda-forge networkx 3.1 pypi_0 pypi newspaper3k 0.2.8 pypi_0 pypi nixtlats 0.1.17 pypi_0 pypi nltk 3.8.1 pypi_0 pypi notebook 6.5.6 pypi_0 pypi notebook-shim 0.2.3 pypi_0 pypi numba 0.56.4 pypi_0 pypi numexpr 2.8.4 py39h6b13a34_1 conda-forge numpy 1.23.4 py39hefdcf20_1 conda-forge oandapyv20 0.6.3 pypi_0 pypi onetimepass 1.0.1 pypi_0 pypi onnxruntime 1.15.1 pypi_0 pypi openai 0.27.8 pypi_0 pypi openapi-schema-pydantic 1.2.4 pypi_0 pypi openbb 3.2.3 pypi_0 pypi openh264 2.3.1 hb7217d7_2 conda-forge openjpeg 2.5.0 hbc2ba62_2 conda-forge openpyxl 3.1.2 pypi_0 pypi openssl 1.1.1v h53f4e23_0 conda-forge osqp 0.6.3 py39h6b13a34_1 conda-forge overrides 7.3.1 pypi_0 pypi p11-kit 0.24.1 h29577a5_0 conda-forge packaging 23.1 pyhd8ed1ab_0 conda-forge pandas 2.1.3 pypi_0 pypi pandas-datareader 0.10.0 pypi_0 pypi pandas-market-calendars 3.2 pypi_0 pypi pandas-ta 0.3.14b0 pypi_0 pypi pandocfilters 1.5.0 pypi_0 pypi papermill 2.4.0 pypi_0 pypi parso 0.8.3 pypi_0 pypi pathspec 0.11.2 pypi_0 pypi patsy 0.5.3 pyhd8ed1ab_0 conda-forge pcre2 10.40 hb34f9b4_0 conda-forge peewee 3.17.0 pypi_0 pypi perl 5.32.1 4_hf2054a2_perl5 conda-forge pexpect 4.8.0 pypi_0 pypi pickleshare 0.7.5 pypi_0 pypi pillow 9.5.0 pypi_0 pypi pip 23.2.1 pyhd8ed1ab_0 conda-forge pixman 0.40.0 h27ca646_0 conda-forge pkginfo 1.9.6 pypi_0 pypi platformdirs 2.6.2 pypi_0 pypi plotly 5.15.0 pypi_0 pypi pmaw 3.0.0 pypi_0 pypi poetry 1.4.0 pypi_0 pypi poetry-core 1.5.1 pypi_0 pypi poetry-plugin-export 1.3.1 pypi_0 pypi pooch 1.7.0 pyha770c72_3 conda-forge posthog 3.0.1 pypi_0 pypi praw 7.7.1 pypi_0 pypi prawcore 2.3.0 pypi_0 pypi prometheus-client 0.17.1 pypi_0 pypi prompt-toolkit 3.0.39 pypi_0 pypi property-cached 1.6.4 pypi_0 pypi protobuf 3.20.1 pypi_0 pypi psutil 5.9.5 pypi_0 pypi psycopg2 2.9.7 pypi_0 pypi pthread-stubs 0.4 h27ca646_1001 conda-forge ptyprocess 0.7.0 pypi_0 pypi pulsar-client 3.2.0 pypi_0 pypi pure-eval 0.2.2 pypi_0 pypi py-cpuinfo 9.0.0 pyhd8ed1ab_0 conda-forge pyarrow 12.0.1 pypi_0 pypi pyasn1 0.5.0 pypi_0 pypi pybind11 2.11.1 py39hbd775c9_0 conda-forge pybind11-global 2.11.1 py39hbd775c9_0 conda-forge pycares 4.3.0 pypi_0 pypi pycodestyle 2.10.0 pypi_0 pypi pycoingecko 3.1.0 pypi_0 pypi pycparser 2.21 pypi_0 pypi pycryptodome 3.18.0 pypi_0 pypi pydantic 1.10.12 pypi_0 pypi pydeck 0.8.0 pypi_0 pypi pyerfa 2.0.0.3 pypi_0 pypi pyflakes 3.0.1 pypi_0 pypi pyfolio-reloaded 0.9.5 pypi_0 pypi pygments 2.15.1 pypi_0 pypi pyhdfe 0.1.2 pypi_0 pypi pyluach 2.2.0 pypi_0 pypi pymeeus 0.5.12 pypi_0 pypi pympler 1.0.1 pypi_0 pypi pyobjc-core 9.2 pypi_0 pypi pyobjc-framework-cocoa 9.2 pypi_0 pypi pyotp 2.8.0 pypi_0 pypi pyparsing 3.0.9 pypi_0 pypi pypdf 3.15.0 pypi_0 pypi pypdf2 3.0.1 pypi_0 pypi pypika 0.48.9 pypi_0 pypi pyprind 2.11.3 pypi_0 pypi pyproject-hooks 1.0.0 pypi_0 pypi pyrsistent 0.19.3 pypi_0 pypi pysabr 0.4.1 pypi_0 pypi pysocks 1.7.1 pyha2e5f31_6 conda-forge pytables 3.8.0 py39h0bfbcdb_1 conda-forge pythclient 0.1.8 pypi_0 pypi python 3.9.13 hc596b02_0_cpython conda-forge python-binance 1.0.17 pypi_0 pypi python-coinmarketcap 0.2 pypi_0 pypi python-dateutil 2.8.2 pyhd8ed1ab_0 conda-forge python-dotenv 1.0.0 pypi_0 pypi python-graphviz 0.20.1 pypi_0 pypi python-i18n 0.3.9 pypi_0 pypi python-interface 1.6.1 pypi_0 pypi python-jose 3.3.0 pypi_0 pypi python-json-logger 2.0.7 pypi_0 pypi python-liquid 1.9.3 pypi_0 pypi python-tzdata 2023.3 pyhd8ed1ab_0 conda-forge python_abi 3.9 3_cp39 conda-forge pytrends 4.9.2 pypi_0 pypi pytz 2023.3 pyhd8ed1ab_0 conda-forge pytz-deprecation-shim 0.1.0.post0 pypi_0 pypi pywry 0.6.2 pypi_0 pypi pyyaml 6.0.1 pypi_0 pypi pyzmq 24.0.1 pypi_0 pypi qdldl 0.1.5.post3 pypi_0 pypi qtconsole 5.4.3 pypi_0 pypi qtpy 2.3.1 pypi_0 pypi quandl 3.7.0 pypi_0 pypi quantstats 0.0.62 pypi_0 pypi rapidfuzz 2.15.1 pypi_0 pypi rdflib 6.3.2 pypi_0 pypi readline 8.2 h92ec313_1 conda-forge referencing 0.30.0 pypi_0 pypi regex 2023.6.3 pypi_0 pypi reportlab 3.6.13 pypi_0 pypi requests 2.31.0 pyhd8ed1ab_0 conda-forge requests-file 1.5.1 pypi_0 pypi requests-futures 1.0.1 pypi_0 pypi requests-toolbelt 0.10.1 pypi_0 pypi retrying 1.3.4 pypi_0 pypi rfc3339-validator 0.1.4 pypi_0 pypi rfc3986-validator 0.1.1 pypi_0 pypi rhash 1.4.3 hb547adb_1 conda-forge rich 12.6.0 pypi_0 pypi riskfolio-lib 3.3.0 pypi_0 pypi robin-stocks 2.1.0 pypi_0 pypi rpds-py 0.9.2 pypi_0 pypi rsa 4.9 pypi_0 pypi ruamel-yaml 0.17.32 pypi_0 pypi ruamel-yaml-clib 0.2.7 pypi_0 pypi safetensors 0.3.2 pypi_0 pypi schedule 1.2.0 pypi_0 pypi scikit-learn 1.3.0 py39hd5c4a62_0 conda-forge scikit-learn-extra 0.3.0 pypi_0 pypi scipy 1.10.1 pypi_0 pypi screeninfo 0.6.7 pypi_0 pypi scs 3.2.3 py39hb0f1628_0 conda-forge seaborn 0.13.0 pypi_0 pypi seaborn-base 0.12.2 pyhd8ed1ab_0 conda-forge send2trash 1.8.2 pypi_0 pypi sentence-transformers 2.2.2 pypi_0 pypi sentencepiece 0.1.99 pypi_0 pypi setproctitle 1.3.2 pypi_0 pypi setuptools 65.4.1 pypi_0 pypi setuptools-scm 7.1.0 pypi_0 pypi sgmllib3k 1.0.0 pypi_0 pypi shellingham 1.5.0.post1 pypi_0 pypi sigtech 0.1.0 pypi_0 pypi six 1.16.0 pyh6c4a22f_0 conda-forge smmap 5.0.0 pypi_0 pypi snappy 1.1.10 h17c5cce_0 conda-forge sniffio 1.3.0 pypi_0 pypi sortedcontainers 2.4.0 pypi_0 pypi soupsieve 2.4.1 pypi_0 pypi sparqlwrapper 2.0.0 pypi_0 pypi sqlalchemy 2.0.19 pypi_0 pypi sqlite 3.42.0 h203b68d_0 conda-forge squarify 0.4.3 pypi_0 pypi stack-data 0.6.2 pypi_0 pypi starlette 0.27.0 pypi_0 pypi statsmodels 0.13.5 pypi_0 pypi stocksera 0.1.21 pypi_0 pypi streamlit 1.25.0 pypi_0 pypi svglib 1.5.1 pypi_0 pypi svt-av1 1.6.0 hb765f3a_0 conda-forge sympy 1.12 pypi_0 pypi ta-lib 0.4.27 pypi_0 pypi tabulate 0.9.0 pypi_0 pypi tenacity 8.2.2 pypi_0 pypi terminado 0.17.1 pypi_0 pypi textwrap3 0.9.2 pypi_0 pypi thepassiveinvestor 1.2.1 pypi_0 pypi threadpoolctl 3.2.0 pyha21a80b_0 conda-forge tiktoken 0.4.0 pypi_0 pypi tinycss2 1.2.1 pypi_0 pypi tinysegmenter 0.3 pypi_0 pypi tk 8.6.12 he1e0b03_0 conda-forge tldextract 5.0.0 pypi_0 pypi tokenize-rt 5.2.0 pypi_0 pypi tokenizers 0.13.3 pypi_0 pypi tokenterminal 1.0.1 pypi_0 pypi toml 0.10.2 pypi_0 pypi tomli 2.0.1 pypi_0 pypi tomlkit 0.12.0 pypi_0 pypi toolz 0.12.0 pypi_0 pypi torch 2.0.1 pypi_0 pypi torchvision 0.15.2 pypi_0 pypi tornado 6.3.2 pypi_0 pypi tqdm 4.65.0 pypi_0 pypi tradingview-ta 3.3.0 pypi_0 pypi traitlets 5.9.0 pypi_0 pypi transformers 4.31.0 pypi_0 pypi trove-classifiers 2023.7.6 pypi_0 pypi typing-extensions 4.7.1 hd8ed1ab_0 conda-forge typing-inspect 0.9.0 pypi_0 pypi typing_extensions 4.7.1 pyha770c72_0 conda-forge tzdata 2023c h71feb2d_0 conda-forge tzlocal 4.3.1 pypi_0 pypi ujson 5.8.0 pypi_0 pypi unicodedata2 15.0.0 py39h02fc5c5_0 conda-forge update-checker 0.18.0 pypi_0 pypi uri-template 1.3.0 pypi_0 pypi urllib3 1.26.16 pypi_0 pypi user-agent 0.1.10 pypi_0 pypi uvicorn 0.23.2 pypi_0 pypi uvloop 0.17.0 pypi_0 pypi vadersentiment 3.3.2 pypi_0 pypi validators 0.20.0 pypi_0 pypi valinvest 0.0.2 pypi_0 pypi vectorbt 0.25.5 pypi_0 pypi virtualenv 20.21.1 pypi_0 pypi watchdog 2.3.1 pypi_0 pypi watchfiles 0.19.0 pypi_0 pypi wcwidth 0.2.6 pypi_0 pypi webcolors 1.13 pypi_0 pypi webencodings 0.5.1 pypi_0 pypi websocket-client 1.6.1 pypi_0 pypi websockets 11.0.3 pypi_0 pypi wheel 0.41.0 pyhd8ed1ab_0 conda-forge widgetsnbextension 4.0.8 pypi_0 pypi wikipedia 1.4.0 pypi_0 pypi wrapt 1.15.0 pypi_0 pypi x264 1!164.3095 h57fd34a_2 conda-forge x265 3.5 hbc6ce65_3 conda-forge xattr 0.10.1 pypi_0 pypi xgboost 1.7.4 pypi_0 pypi xlsxwriter 3.1.2 pypi_0 pypi xorg-libxau 1.0.11 hb547adb_0 conda-forge xorg-libxdmcp 1.1.3 h27ca646_0 conda-forge xz 5.2.6 h57fd34a_0 conda-forge y-py 0.6.0 pypi_0 pypi yahooquery 2.3.2 pypi_0 pypi yarl 1.9.2 pypi_0 pypi yfinance 0.2.31 pypi_0 pypi ypy-websocket 0.8.4 pypi_0 pypi zipline-reloaded 3.0.3 pypi_0 pypi zipp 3.16.2 pypi_0 pypi zlib 1.2.13 h53f4e23_5 conda-forge zlib-ng 2.0.7 h1a8c8d9_0 conda-forge zope-interface 6.0 pypi_0 pypi zstd 1.5.2 h4f39d0f_7 conda-forge

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

Description of Issue


import os
import pandas as pd
import zipline
from zipline import run_algorithm
from zipline.api import (
    date_rules,
    order_target_percent,
    schedule_function,
    symbol,
    time_rules,
)
from zipline.data import bundles
from zipline.finance import commission, slippage

def initialize(context):
    """
    Set up the initial values and settings for the trading algorithm.
    Called once at the start of the algorithm.
    """
    # List of ETFs that the algorithm will consider for trading.
    context.symbols = [
        symbol("FB"),
        symbol("AAPL"),
        symbol("AMZN"),
        symbol("NFLX"),
        symbol("GOOG"),
    ]

    # Number of top-performing assets to trade each month.
    context.traded_count = 3

    # Lookback period: Number of days the algorithm will use to calculate momentum.
    # (12 months * approx. 21 trading days per month)
    context.lookback = 12 * 21

    # Define the model for trading costs.
    context.set_commission(commission.PerShare(cost=0.007, min_trade_cost=1.0))

    # Define the slippage model.
    context.set_slippage(slippage.VolumeShareSlippage())

    # Schedule a function to rebalance the portfolio at the start of each month when the market opens.
    schedule_function(rebalance, date_rules.month_start(), time_rules.market_open())

def rebalance(context, data):
    """
    Adjust the portfolio by selling and buying assets based on their recent performance.
    """
    # Retrieve historical closing prices for our assets.
    hist = data.history(context.symbols, "close", context.lookback, "1d")

    # Calculate the average percentage change of each asset.
    momentum = hist.pct_change().mean()

    # Rank assets by momentum.
    sorted_by_momentum = momentum.sort_values(ascending=False)

    # Select the top-performing assets based on 'traded_count'.
    selected_assets = sorted_by_momentum.index[: context.traded_count]

    # Sell off assets that aren't in our top performers.
    for asset in context.portfolio.positions:
        if asset.symbol not in selected_assets:
            order_target_percent(asset, 0)

    # Buy the top-performing assets.
    for asset in selected_assets:
        order_target_percent(asset, 1 / context.traded_count)

# Time frame for the backtest.
start = pd.Timestamp("2010-01-01")
end = pd.Timestamp("2018-03-28")

# Execute the backtest with the given settings.
perf = run_algorithm(
    start=start,
    end=end,
    initialize=initialize,
    capital_base=100000,
    bundle="quandl",
)

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[12], line 6
      3 end = pd.Timestamp("2018-03-28")
      5 # Execute the backtest with the given settings.
----> 6 perf = run_algorithm(
      7     start=start,
      8     end=end,
      9     initialize=initialize,
     10     capital_base=100000,
     11     bundle="quandl",
     12 )

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/run_algo.py:397, in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, custom_loader, blotter)
    393 load_extensions(default_extension, extensions, strict_extensions, environ)
    395 benchmark_spec = BenchmarkSpec.from_returns(benchmark_returns)
--> 397 return _run(
    398     handle_data=handle_data,
    399     initialize=initialize,
    400     before_trading_start=before_trading_start,
    401     analyze=analyze,
    402     algofile=None,
    403     algotext=None,
    404     defines=(),
    405     data_frequency=data_frequency,
    406     capital_base=capital_base,
    407     bundle=bundle,
    408     bundle_timestamp=bundle_timestamp,
    409     start=start,
    410     end=end,
    411     output=os.devnull,
    412     trading_calendar=trading_calendar,
    413     print_algo=False,
    414     metrics_set=metrics_set,
    415     local_namespace=False,
    416     environ=environ,
    417     blotter=blotter,
    418     custom_loader=custom_loader,
    419     benchmark_spec=benchmark_spec,
    420 )

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/run_algo.py:200, in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, custom_loader, benchmark_spec)
    197         raise _RunAlgoError(str(e))
    199 try:
--> 200     perf = TradingAlgorithm(
    201         namespace=namespace,
    202         data_portal=data,
    203         get_pipeline_loader=choose_loader,
    204         trading_calendar=trading_calendar,
    205         sim_params=SimulationParameters(
    206             start_session=start,
    207             end_session=end,
    208             trading_calendar=trading_calendar,
    209             capital_base=capital_base,
    210             data_frequency=data_frequency,
    211         ),
    212         metrics_set=metrics_set,
    213         blotter=blotter,
    214         benchmark_returns=benchmark_returns,
    215         benchmark_sid=benchmark_sid,
    216         **{
    217             "initialize": initialize,
    218             "handle_data": handle_data,
    219             "before_trading_start": before_trading_start,
    220             "analyze": analyze,
    221         }
    222         if algotext is None
    223         else {
    224             "algo_filename": getattr(algofile, "name", "<algorithm>"),
    225             "script": algotext,
    226         },
    227     ).run()
    228 except NoBenchmark:
    229     raise _RunAlgoError(
    230         (
    231             "No ``benchmark_spec`` was provided, and"
   (...)
    239         ),
    240     )

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:625, in TradingAlgorithm.run(self, data_portal)
    623 try:
    624     perfs = []
--> 625     for perf in self.get_generator():
    626         perfs.append(perf)
    628     # convert perf dict to pandas dataframe

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/gens/tradesimulation.py:225, in AlgorithmSimulator.transform(self)
    223 for dt, action in self.clock:
    224     if action == BAR:
--> 225         for capital_change_packet in every_bar(dt):
    226             yield capital_change_packet
    227     elif action == SESSION_START:

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/gens/tradesimulation.py:141, in AlgorithmSimulator.transform.<locals>.every_bar(dt_to_use, current_data, handle_data)
    138 for commission in new_commissions:
    139     metrics_tracker.process_commission(commission)
--> 141 handle_data(algo, current_data, dt_to_use)
    143 # grab any new orders from the blotter, then clear the list.
    144 # this includes cancelled orders.
    145 new_orders = blotter.new_orders

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/events.py:201, in EventManager.handle_data(self, context, data, dt)
    199 with self._create_context(data):
    200     for event in self._events:
--> 201         event.handle_data(
    202             context,
    203             data,
    204             dt,
    205         )

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/events.py:224, in Event.handle_data(self, context, data, dt)
    220 """
    221 Calls the callable only when the rule is triggered.
    222 """
    223 if self.rule.should_trigger(dt):
--> 224     self.callback(context, data)

Cell In[5], line 24, in rebalance(context, data)
     22 # Buy the top-performing assets.
     23 for asset in selected_assets:
---> 24     order_target_percent(asset, 1 / context.traded_count)

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/api_support.py:55, in api_method.<locals>.wrapped(*args, **kwargs)
     51 if algo_instance is None:
     52     raise RuntimeError(
     53         "zipline api method %s must be called during a simulation." % f.__name__
     54     )
---> 55 return getattr(algo_instance, f.__name__)(*args, **kwargs)

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/utils/api_support.py:132, in disallowed_in_before_trading_start.<locals>.decorator.<locals>.wrapped_method(self, *args, **kwargs)
    130 if self._in_before_trading_start:
    131     raise exception
--> 132 return method(self, *args, **kwargs)

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:1848, in TradingAlgorithm.order_target_percent(self, asset, target, limit_price, stop_price, style)
   1793 @api_method
   1794 @disallowed_in_before_trading_start(OrderInBeforeTradingStart())
   1795 def order_target_percent(
   1796     self, asset, target, limit_price=None, stop_price=None, style=None
   1797 ):
   1798     """Place an order to adjust a position to a target percent of the
   1799     current portfolio value. If the position doesn't already exist, this is
   1800     equivalent to placing a new order. If the position does exist, this is
   (...)
   1846     :func:`zipline.api.order_target_value`
   1847     """
-> 1848     if not self._can_order_asset(asset):
   1849         return None
   1851     amount = self._calculate_order_target_percent_amount(asset, target)

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:1196, in TradingAlgorithm._can_order_asset(self, asset)
   1189     raise UnsupportedOrderParameters(
   1190         msg="Passing non-Asset argument to 'order()' is not supported."
   1191         " Use 'sid()' or 'symbol()' methods to look up an Asset."
   1192     )
   1194 if asset.auto_close_date:
   1195     # TODO FIXME TZ MESS
-> 1196     day = self.trading_calendar.minute_to_session(self.get_datetime())
   1198     if day > min(asset.end_date, asset.auto_close_date):
   1199         # If we are after the asset's end date or auto close date, warn
   1200         # the user that they can't place an order for this asset, and
   1201         # return None.
   1202         log.warning(
   1203             "Cannot place order for {0}, as it has de-listed. "
   1204             "Any existing positions for this asset will be "
   1205             "liquidated on "
   1206             "{1}.".format(asset.symbol, asset.auto_close_date)
   1207         )

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:4, in get_datetime(self, tz)
      1 #
      2 # Copyright 2015 Quantopian, Inc.
      3 #
----> 4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 from collections.abc import Iterable
     16 from collections import namedtuple

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:4, in get_datetime(self, tz)
      1 #
      2 # Copyright 2015 Quantopian, Inc.
      3 #
----> 4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 from collections.abc import Iterable
     16 from collections import namedtuple

File ~/anaconda3/envs/newsletter/lib/python3.9/site-packages/zipline/algorithm.py:1458, in TradingAlgorithm.get_datetime(self, tz)
   1445 """Returns the current simulation datetime.
   1446 
   1447 Parameters
   (...)
   1455     The current simulation datetime converted to ``tz``.
   1456 """
   1457 dt = self.datetime
-> 1458 assert dt.tzinfo == timezone.utc, "Algorithm should have a utc datetime"
   1459 if tz is not None:
   1460     dt = dt.astimezone(tz)

AssertionError: Algorithm should have a utc datetime
stefan-jansen commented 1 week ago

See:

-> 1458 assert dt.tzinfo == timezone.utc, "Algorithm should have a utc datetime"
   1459 if tz is not None:
   1460     dt = dt.astimezone(tz)

AssertionError: Algorithm should have a utc datetime

suggests to use pd.Timestamp [with tzinfo](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timestamp.html.