stefan-jansen / zipline-reloaded

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

Trouble With Dates #240

Open JohnSparks2024 opened 3 months ago

JohnSparks2024 commented 3 months ago

Dear Zipline Maintainers,

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

Environment

* Operating System: Windows * Python Version: `3.9.7` * Python Bitness: 64 * How did you install Zipline:`conda` * Python packages: `$ pip freeze` or `$ conda list` (base) PS C:\Users\JSparks> pip freeze WARNING: Ignoring invalid distribution -ables (c:\users\jsparks\anaconda3\lib\site-packages) adjustText==0.7.3 alabaster @ file:///home/ktietz/src/ci/alabaster_1611921544520/work alembic==1.13.1 anaconda-client @ file:///C:/ci/anaconda-client_1635342725944/work anaconda-navigator==2.1.1 anaconda-project @ file:///tmp/build/80754af9/anaconda-project_1626085644852/work analytics-python==1.4.0 anyio @ file:///C:/ci/anyio_1620153135622/work/dist appdirs==1.4.4 argh==0.26.2 argon2-cffi @ file:///C:/ci/argon2-cffi_1613037869401/work arrow @ file:///C:/ci/arrow_1617738834352/work asn1crypto @ file:///tmp/build/80754af9/asn1crypto_1596577642040/work astroid @ file:///C:/ci/astroid_1628063282661/work astropy @ file:///C:/ci/astropy_1629829318700/work async-generator @ file:///home/ktietz/src/ci/async_generator_1611927993394/work atomicwrites==1.4.0 attrs @ file:///tmp/build/80754af9/attrs_1620827162558/work autopep8 @ file:///tmp/build/80754af9/autopep8_1620866417880/work Babel @ file:///tmp/build/80754af9/babel_1620871417480/work backcall @ file:///home/ktietz/src/ci/backcall_1611930011877/work backoff==1.10.0 backports.functools-lru-cache @ file:///tmp/build/80754af9/backports.functools_lru_cache_1618170165463/work backports.shutil-get-terminal-size @ file:///tmp/build/80754af9/backports.shutil_get_terminal_size_1608222128777/work backports.tempfile @ file:///home/linux1/recipes/ci/backports.tempfile_1610991236607/work backports.weakref==1.0.post1 bcolz-zipline==1.2.6 bcrypt @ file:///C:/ci/bcrypt_1607022693089/work beautifulsoup4==4.12.3 binaryornot @ file:///tmp/build/80754af9/binaryornot_1617751525010/work bitarray @ file:///C:/ci/bitarray_1629133068652/work bkcharts==0.2 black==19.10b0 bleach @ file:///tmp/build/80754af9/bleach_1628110601003/work blosc2==2.5.1 bokeh @ file:///C:/ci/bokeh_1635306491714/work boto==2.49.0 Bottleneck @ file:///C:/ci/bottleneck_1607557040328/work brotlipy==0.7.0 bs4==0.0.1 cached-property @ file:///tmp/build/80754af9/cached-property_1600785575025/work certifi==2021.10.8 cffi @ file:///C:/ci/cffi_1625831756778/work chardet @ file:///C:/ci/chardet_1607706937985/work charset-normalizer @ file:///tmp/build/80754af9/charset-normalizer_1630003229654/work click==8.0.3 cloudpickle @ file:///tmp/build/80754af9/cloudpickle_1632508026186/work clyent==1.2.2 colorama @ file:///tmp/build/80754af9/colorama_1607707115595/work comtypes==1.1.10 conda==4.10.3 conda-build==3.21.6 conda-content-trust @ file:///tmp/build/80754af9/conda-content-trust_1617045594566/work conda-pack @ file:///tmp/build/80754af9/conda-pack_1611163042455/work conda-package-handling @ file:///C:/ci/conda-package-handling_1618262410900/work conda-repo-cli @ file:///tmp/build/80754af9/conda-repo-cli_1620168426516/work conda-token @ file:///tmp/build/80754af9/conda-token_1620076980546/work conda-verify==3.4.2 contextlib2 @ file:///Users/ktietz/demo/mc3/conda-bld/contextlib2_1630668244042/work cookiecutter @ file:///tmp/build/80754af9/cookiecutter_1617748928239/work cryptography @ file:///C:/ci/cryptography_1633520531101/work cycler==0.10.0 Cython @ file:///C:/ci/cython_1636018292912/work cytoolz==0.11.0 daal4py==2021.3.0 dask==2021.10.0 debugpy @ file:///C:/ci/debugpy_1629222819322/work decorator @ file:///tmp/build/80754af9/decorator_1632776554403/work defusedxml @ file:///tmp/build/80754af9/defusedxml_1615228127516/work diff-match-patch @ file:///Users/ktietz/demo/mc3/conda-bld/diff-match-patch_1630511840874/work distributed @ file:///C:/ci/distributed_1635968318313/work docutils @ file:///C:/ci/docutils_1620828264669/work empyrical==0.5.5 empyrical-reloaded==0.5.9 entrypoints==0.3 et-xmlfile==1.1.0 exchange_calendars==4.5.3 fastcache @ file:///C:/ci/fastcache_1607571310570/work feather-format==0.4.1 ffn==0.3.6 filelock @ file:///tmp/build/80754af9/filelock_1635402558181/work flake8 @ file:///tmp/build/80754af9/flake8_1620776156532/work Flask @ file:///home/ktietz/src/ci/flask_1611932660458/work fonttools==4.25.0 frozendict==2.4.0 fsspec @ file:///tmp/build/80754af9/fsspec_1636116461911/work future @ file:///C:/ci/future_1607568713721/work gevent @ file:///C:/ci/gevent_1628273776273/work glob2 @ file:///home/linux1/recipes/ci/glob2_1610991677669/work greenlet @ file:///C:/ci/greenlet_1628888275363/work h5py @ file:///C:/ci/h5py_1622088609188/work HeapDict @ file:///Users/ktietz/demo/mc3/conda-bld/heapdict_1630598515714/work html5lib @ file:///Users/ktietz/demo/mc3/conda-bld/html5lib_1629144453894/work idna @ file:///tmp/build/80754af9/idna_1622654382723/work imagecodecs @ file:///C:/ci/imagecodecs_1635511087451/work imageio @ file:///tmp/build/80754af9/imageio_1617700267927/work imagesize @ file:///Users/ktietz/demo/mc3/conda-bld/imagesize_1628863108022/work importlib-metadata @ file:///C:/ci/importlib-metadata_1631916826748/work inflection==0.5.1 iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work intervaltree @ file:///Users/ktietz/demo/mc3/conda-bld/intervaltree_1630511889664/work ipykernel @ file:///C:/ci/ipykernel_1633545585502/work/dist/ipykernel-6.4.1-py3-none-any.whl ipython @ file:///C:/ci/ipython_1635944283918/work ipython-genutils @ file:///tmp/build/80754af9/ipython_genutils_1606773439826/work ipywidgets @ file:///tmp/build/80754af9/ipywidgets_1634143127070/work iso3166==2.1.1 iso4217==1.11.20220401 isort @ file:///tmp/build/80754af9/isort_1628603791788/work itsdangerous @ file:///tmp/build/80754af9/itsdangerous_1621432558163/work jdcal @ file:///Users/ktietz/demo/mc3/conda-bld/jdcal_1630584345063/work jedi @ file:///C:/ci/jedi_1611341083684/work Jinja2 @ file:///tmp/build/80754af9/jinja2_1612213139570/work jinja2-time @ file:///tmp/build/80754af9/jinja2-time_1617751524098/work joblib @ file:///tmp/build/80754af9/joblib_1635411271373/work json5 @ file:///tmp/build/80754af9/json5_1624432770122/work jsonschema @ file:///Users/ktietz/demo/mc3/conda-bld/jsonschema_1630511932244/work jupyter @ file:///C:/ci/jupyter_1607685287094/work jupyter-client @ file:///tmp/build/80754af9/jupyter_client_1616770841739/work jupyter-console @ file:///tmp/build/80754af9/jupyter_console_1616615302928/work jupyter-core @ file:///C:/ci/jupyter_core_1633420716440/work jupyter-server @ file:///C:/ci/jupyter_server_1616084298419/work jupyterlab @ file:///tmp/build/80754af9/jupyterlab_1635799997693/work jupyterlab-pygments @ file:///tmp/build/80754af9/jupyterlab_pygments_1601490720602/work jupyterlab-server @ file:///tmp/build/80754af9/jupyterlab_server_1633419203660/work jupyterlab-widgets @ file:///tmp/build/80754af9/jupyterlab_widgets_1609884341231/work keyring @ file:///C:/ci/keyring_1629321702436/work kiwisolver @ file:///C:/ci/kiwisolver_1612282555033/work korean-lunar-calendar==0.3.1 lazy-object-proxy @ file:///C:/ci/lazy-object-proxy_1616529288960/work libarchive-c @ file:///tmp/build/80754af9/python-libarchive-c_1617780486945/work llvmlite==0.37.0 locket==0.2.1 Logbook==1.5.3 lru-dict==1.3.0 lxml==5.1.0 Mako==1.3.2 MarkupSafe @ file:///C:/ci/markupsafe_1607027406824/work matplotlib @ file:///C:/ci/matplotlib-suite_1634667159685/work matplotlib-inline @ file:///tmp/build/80754af9/matplotlib-inline_1628242447089/work mccabe==0.6.1 menuinst @ file:///C:/ci/menuinst_1631733438520/work mistune @ file:///C:/ci/mistune_1607359457024/work mitoinstaller==0.0.132 mitosheet==0.1.400 mkl-fft==1.3.1 mkl-random @ file:///C:/ci/mkl_random_1626186184308/work mkl-service==2.4.0 mock @ file:///tmp/build/80754af9/mock_1607622725907/work monotonic==1.6 more-itertools @ file:///tmp/build/80754af9/more-itertools_1635423142362/work mpmath==1.2.1 msgpack @ file:///C:/ci/msgpack-python_1612287350784/work multipledispatch @ file:///C:/ci/multipledispatch_1607574329826/work multitasking==0.0.11 munkres==1.1.4 mypy-extensions==0.4.3 navigator-updater==0.2.1 nbclassic @ file:///tmp/build/80754af9/nbclassic_1616085367084/work nbclient @ file:///tmp/build/80754af9/nbclient_1614364831625/work nbconvert @ file:///C:/ci/nbconvert_1624479160025/work nbformat @ file:///tmp/build/80754af9/nbformat_1617383369282/work ndindex==1.8 nest-asyncio @ file:///tmp/build/80754af9/nest-asyncio_1613680548246/work networkx @ file:///tmp/build/80754af9/networkx_1633639043937/work nltk==3.6.5 norgatedata==1.0.60 nose @ file:///tmp/build/80754af9/nose_1606773131901/work notebook @ file:///C:/ci/notebook_1635393701545/work numba @ file:///C:/ci/numba_1635186062888/work numexpr @ file:///C:/ci/numexpr_1618856728739/work numpy==1.22.4 numpydoc @ file:///tmp/build/80754af9/numpydoc_1605117425582/work olefile @ file:///Users/ktietz/demo/mc3/conda-bld/olefile_1629805411829/work openpyxl @ file:///tmp/build/80754af9/openpyxl_1632777717936/work packaging @ file:///tmp/build/80754af9/packaging_1625611678980/work pandas==2.2.1 pandas-datareader==0.10.0 pandasql==0.7.3 pandocfilters @ file:///C:/ci/pandocfilters_1605114832805/work paramiko @ file:///tmp/build/80754af9/paramiko_1598886428689/work parso @ file:///tmp/build/80754af9/parso_1617223946239/work partd @ file:///tmp/build/80754af9/partd_1618000087440/work path @ file:///C:/ci/path_1624287837534/work pathlib2 @ file:///C:/ci/pathlib2_1625585796814/work pathspec==0.7.0 patsy==0.5.2 peewee==3.17.1 pep8==1.7.1 pexpect @ file:///tmp/build/80754af9/pexpect_1605563209008/work pickleshare @ file:///tmp/build/80754af9/pickleshare_1606932040724/work Pillow==8.4.0 pkginfo==1.7.1 plotly==5.3.0 pluggy @ file:///C:/ci/pluggy_1615976440052/work ply==3.11 poyo @ file:///tmp/build/80754af9/poyo_1617751526755/work prometheus-client @ file:///tmp/build/80754af9/prometheus_client_1623189609245/work prompt-toolkit @ file:///tmp/build/80754af9/prompt-toolkit_1633440160888/work psutil @ file:///C:/ci/psutil_1612298199233/work ptyprocess @ file:///tmp/build/80754af9/ptyprocess_1609355006118/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl py @ file:///tmp/build/80754af9/py_1607971587848/work py-cpuinfo==9.0.0 pyarrow==12.0.1 pycodestyle @ file:///tmp/build/80754af9/pycodestyle_1615748559966/work pycosat==0.6.3 pycparser @ file:///tmp/build/80754af9/pycparser_1594388511720/work pycurl==7.44.1 pydocstyle @ file:///tmp/build/80754af9/pydocstyle_1621600989141/work pyerfa @ file:///C:/ci/pyerfa_1621560974055/work pyfinance==1.3.0 pyflakes @ file:///tmp/build/80754af9/pyflakes_1617200973297/work pyfolio==0.9.2 Pygments @ file:///tmp/build/80754af9/pygments_1629234116488/work PyJWT @ file:///C:/ci/pyjwt_1619682721924/work pykalman-py311-update==0.9.6 pylint @ file:///C:/ci/pylint_1627536884966/work pyls-spyder==0.4.0 pyluach==2.2.0 PyNaCl @ file:///C:/ci/pynacl_1607612759007/work pyodbc===4.0.0-unsupported pyOpenSSL @ file:///tmp/build/80754af9/pyopenssl_1635333100036/work pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work pyreadline==2.1 pyreadr==0.4.6 pyrsistent @ file:///C:/ci/pyrsistent_1636093225342/work PySocks @ file:///C:/ci/pysocks_1605307512533/work pytest==6.2.4 python-dateutil @ file:///tmp/build/80754af9/python-dateutil_1626374649649/work python-interface==1.6.1 python-lsp-black @ file:///tmp/build/80754af9/python-lsp-black_1634232156041/work python-lsp-jsonrpc==1.0.0 python-lsp-server==1.2.4 python-slugify @ file:///tmp/build/80754af9/python-slugify_1620405669636/work pytz==2024.1 PyWavelets @ file:///C:/ci/pywavelets_1607645631519/work pywin32==228 pywin32-ctypes @ file:///C:/ci/pywin32-ctypes_1607553594546/work pywinpty @ file:///C:/ci/pywinpty_1607419945780/work PyYAML==6.0 pyzmq @ file:///C:/ci/pyzmq_1628276105793/work QDarkStyle @ file:///tmp/build/80754af9/qdarkstyle_1617386714626/work qstylizer @ file:///tmp/build/80754af9/qstylizer_1617713584600/work/dist/qstylizer-0.1.10-py2.py3-none-any.whl QtAwesome @ file:///tmp/build/80754af9/qtawesome_1615991616277/work qtconsole @ file:///tmp/build/80754af9/qtconsole_1632739723211/work QtPy @ file:///tmp/build/80754af9/qtpy_1629397026935/work quantconnect==0.1.0 regex @ file:///C:/ci/regex_1628063427816/work requests==2.31.0 rope @ file:///tmp/build/80754af9/rope_1623703006312/work Rtree @ file:///C:/ci/rtree_1618421015405/work ruamel-yaml-conda @ file:///C:/ci/ruamel_yaml_1616016898638/work scikit-image==0.18.3 scikit-learn @ file:///C:/ci/scikit-learn_1622739499047/work scikit-learn-intelex==2021.20210714.120553 scipy @ file:///C:/ci/scipy_1630606917240/work seaborn @ file:///tmp/build/80754af9/seaborn_1629307859561/work Send2Trash @ file:///tmp/build/80754af9/send2trash_1632406701022/work simpledbf==0.2.6 simplegeneric==0.8.1 singledispatch @ file:///tmp/build/80754af9/singledispatch_1629321204894/work sip==4.19.13 six @ file:///tmp/build/80754af9/six_1623709665295/work sniffio @ file:///C:/ci/sniffio_1614030527509/work snowballstemmer @ file:///tmp/build/80754af9/snowballstemmer_1611258885636/work sortedcollections @ file:///tmp/build/80754af9/sortedcollections_1611172717284/work sortedcontainers @ file:///tmp/build/80754af9/sortedcontainers_1623949099177/work soupsieve @ file:///tmp/build/80754af9/soupsieve_1616183228191/work Sphinx==4.2.0 sphinxcontrib-applehelp @ file:///home/ktietz/src/ci/sphinxcontrib-applehelp_1611920841464/work sphinxcontrib-devhelp @ file:///home/ktietz/src/ci/sphinxcontrib-devhelp_1611920923094/work sphinxcontrib-htmlhelp @ file:///tmp/build/80754af9/sphinxcontrib-htmlhelp_1623945626792/work sphinxcontrib-jsmath @ file:///home/ktietz/src/ci/sphinxcontrib-jsmath_1611920942228/work sphinxcontrib-qthelp @ file:///home/ktietz/src/ci/sphinxcontrib-qthelp_1611921055322/work sphinxcontrib-serializinghtml @ file:///tmp/build/80754af9/sphinxcontrib-serializinghtml_1624451540180/work sphinxcontrib-websupport @ file:///tmp/build/80754af9/sphinxcontrib-websupport_1597081412696/work spyder @ file:///C:/ci/spyder_1636480369575/work spyder-kernels @ file:///C:/ci/spyder-kernels_1634237096710/work SQLAlchemy==2.0.27 statsmodels==0.12.2 sympy @ file:///C:/ci/sympy_1635219088507/work TA-Lib==0.4.28 tables==3.9.2 tabulate==0.8.10 TBB==0.2 tblib @ file:///Users/ktietz/demo/mc3/conda-bld/tblib_1629402031467/work tenacity==8.0.1 termcolor==1.1.0 terminado==0.9.4 testpath @ file:///tmp/build/80754af9/testpath_1624638946665/work text-unidecode @ file:///Users/ktietz/demo/mc3/conda-bld/text-unidecode_1629401354553/work textdistance @ file:///tmp/build/80754af9/textdistance_1612461398012/work threadpoolctl @ file:///Users/ktietz/demo/mc3/conda-bld/threadpoolctl_1629802263681/work three-merge @ file:///tmp/build/80754af9/three-merge_1607553261110/work tifffile @ file:///tmp/build/80754af9/tifffile_1627275862826/work tinycss @ file:///tmp/build/80754af9/tinycss_1617713798712/work toml @ file:///tmp/build/80754af9/toml_1616166611790/work toolz @ file:///home/linux1/recipes/ci/toolz_1610987900194/work tornado @ file:///C:/ci/tornado_1606924294691/work tqdm @ file:///tmp/build/80754af9/tqdm_1635330843403/work traitlets @ file:///tmp/build/80754af9/traitlets_1632522747050/work typed-ast @ file:///C:/ci/typed-ast_1624953797214/work typing_extensions==4.10.0 tzdata==2024.1 ujson @ file:///C:/ci/ujson_1611259568517/work unicodecsv==0.14.1 Unidecode @ file:///tmp/build/80754af9/unidecode_1614712377438/work urllib3==1.26.7 watchdog @ file:///C:/ci/watchdog_1624955113064/work wcwidth @ file:///Users/ktietz/demo/mc3/conda-bld/wcwidth_1629357192024/work webencodings==0.5.1 Werkzeug @ file:///tmp/build/80754af9/werkzeug_1635505089296/work whichcraft @ file:///tmp/build/80754af9/whichcraft_1617751293875/work widgetsnbextension @ file:///C:/ci/widgetsnbextension_1607531582688/work win-inet-pton @ file:///C:/ci/win_inet_pton_1605306162074/work win-unicode-console==0.5 wincertstore==0.2 wrapt @ file:///C:/ci/wrapt_1607574570428/work xlrd @ file:///tmp/build/80754af9/xlrd_1608072521494/work XlsxWriter @ file:///tmp/build/80754af9/xlsxwriter_1628603415431/work xlwings==0.24.9 xlwt==1.3.0 xmltodict @ file:///Users/ktietz/demo/mc3/conda-bld/xmltodict_1629301980723/work yapf @ file:///tmp/build/80754af9/yapf_1615749224965/work yfinance==0.2.37 zict==2.0.0 zipline-reloaded==3.0.3 zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work zope.event==4.5.0 zope.interface @ file:///C:/ci/zope.interface_1625036252485/work

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

I am trying to run the momentum program for Trading Evolved (Chapter 12 – Momentum/Momentum Model.ipynb), contained below. I am having a terrible time with the dates. Running the code as it was originally provided gives me the error AttributeError: 'UTC' object has no attribute 'key' After a large amount of google searching I was able to get around this error for a different program (First Zipleine Backtest.ipynb) by changing start_date = datetime(1996, 1, 1, tzinfo=pytz.UTC) end_date = datetime(2017, 12, 31, tzinfo=pytz.UTC)

to

start_date=pd.to_datetime('1996-01-01') end_date =pd.to_datetime('2017-12-31')

However, when I make this change to the Momentum Model program changing

start = datetime(1997, 1, 1, 8, 15, 12, 0, pytz.UTC) end = datetime(2017, 12, 31, 8, 15, 12, 0, pytz.UTC)

which initially produces the error

NameError: name 'datetime' is not defined

So I change the format of the start and end to

start=pd.to_datetime('1996-01-01') end =pd.to_datetime('2017-12-31')

I get the error

TypeError: Invalid comparison between dtype=datetime64[ns] and Timestamp

After searching the program for references to ‘start’, I came across

    date_rule=date_rules.month_start(),

Now, I know that dates in python can take a couple of different forms and after I made the change, the form of start is

type(start) Out[21]: pandas._libs.tslibs.timestamps.Timestamp But I don’t know the format of the date in

    date_rule=date_rules.month_start(),

So I don’t know how to make the comparison legitimate, if this indeed the source of the error.

I am trying to develop these programs into teaching material at the university at which I am employed but can’t do that if I can’t get passed the sticking point of the date formats. I would really, really, appreciate some help. Thanks.

--John Sparks

Program is below

%reset -f %clear

import zipline from zipline.api import order_target_percent, symbol, \ set_commission, set_slippage, schedule_function, \ date_rules, time_rules, attach_pipeline, pipeline_output, set_benchmark from pandas import Timestamp import matplotlib.pyplot as plt import pyfolio as pf import pandas as pd import numpy as np
from scipy import stats
from zipline.finance.commission import PerDollar from zipline.finance.slippage import VolumeShareSlippage, FixedSlippage

from zipline_norgatedata.pipelines import NorgateDataIndexConstituent

from zipline.pipeline import Pipeline

import norgatedata

intial_portfolio = 10000000 momentum_window = 125 minimum_momentum = 40 portfolio_size = 30 vola_window = 20

""" Commission and Slippage Settings """ enable_commission = True commission_pct = 0.001 enable_slippage = True slippage_volume_limit = 0.25 slippage_impact = 0.1

""" Helper functions. """ def momentum_score(ts): """ Input: Price time series. Output: Annualized exponential regression slope, multiplied by the R2 """

Make a list of consecutive numbers

x = np.arange(len(ts)) 
# Get logs
log_ts = np.log(ts) 
# Calculate regression values
slope, intercept, r_value, p_value, std_err = stats.linregress(x, log_ts)
# Annualize percent
annualized_slope = (np.power(np.exp(slope), 252) - 1) * 100
#Adjust for fitness
score = annualized_slope * (r_value ** 2)
return score

def volatility(ts): return ts.pct_change().rolling(vola_window).std().iloc[-1]

def output_progress(context): """ Output some performance numbers during backtest run This code just prints out the past month's performance so that we have something to look at while the backtest runs. """

# Get today's date
today = zipline.api.get_datetime().date()

# Calculate percent difference since last month
perf_pct = (context.portfolio.portfolio_value / context.last_month) - 1

# Print performance, format as percent with two decimals.
print("{} - Last Month Result: {:.2%}".format(today, perf_pct))

# Remember today's portfolio value for next month's calculation
context.last_month = context.portfolio.portfolio_value

""" Initialization and trading logic """ def initialize(context):

# Set commission and slippage.
if enable_commission:
    comm_model = PerDollar(cost=commission_pct)
else:
    comm_model = PerDollar(cost=0.0)
set_commission(comm_model)

if enable_slippage:
    slippage_model=VolumeShareSlippage(volume_limit=slippage_volume_limit, price_impact=slippage_impact)
else:
    slippage_model=FixedSlippage(spread=0.0)   
set_slippage(slippage_model)    

# Used only for progress output.
context.last_month = intial_portfolio

# Fetch and store index membership
context.index_members = pd.read_csv('C:/StockData/NewCourse/TradingEvolvedCode/data/index_members/sp500.csv', index_col=0, parse_dates=[0])

#Schedule rebalance monthly.
schedule_function(
    func=rebalance,
    date_rule=date_rules.month_start(),
    time_rule=time_rules.market_open()
)

def rebalance(context, data):

Write some progress output during the backtest

output_progress(context)

# Ok, let's find which stocks can be traded today.

# First, get today's date
today = zipline.api.get_datetime()

# Second, get the index makeup for all days prior to today.
all_prior = context.index_members.loc[context.index_members.index < today]

# Now let's snag the first column of the last, i.e. latest, entry.
latest_day = all_prior.iloc[-1,0]

# Split the text string with tickers into a list
list_of_tickers = latest_day.split(',')

# Finally, get the Zipline symbols for the tickers
todays_universe = [symbol(ticker) for ticker in list_of_tickers]

# There's your daily universe. But we could of course have done this in one go.
"""       

# This line below does the same thing,
# using the same logic to fetch today's stocks.

todays_universe = [
    symbol(ticker) for ticker in 
    context.index_members.loc[context.index_members.index < today].iloc[-1,0].split(',')
]
"""    

# Get historical data
hist = data.history(todays_universe, "close", momentum_window, "1d")

# Make momentum ranking table
ranking_table = hist.apply(momentum_score).sort_values(ascending=False)  

"""
Sell Logic

First we check if any existing position should be sold.
* Sell if stock is no longer part of index.
* Sell if stock has too low momentum value.
"""
kept_positions = list(context.portfolio.positions.keys())
for security in context.portfolio.positions:
    if (security not in todays_universe):
        order_target_percent(security, 0.0)
        kept_positions.remove(security)          
    elif ranking_table[security] < minimum_momentum:
        order_target_percent(security, 0.0)
        kept_positions.remove(security)         

"""
Stock Selection Logic

Check how many stocks we are keeping from last month.
Fill from top of ranking list, until we reach the
desired total number of portfolio holdings.
"""
replacement_stocks = portfolio_size - len(kept_positions)
buy_list = ranking_table.loc[
    ~ranking_table.index.isin(kept_positions)][:replacement_stocks]

new_portfolio = pd.concat(
    (buy_list, 
     ranking_table.loc[ranking_table.index.isin(kept_positions)])
)

"""
Calculate inverse volatility for stocks, 
and make target position weights.
"""
vola_table = hist[new_portfolio.index].apply(volatility)
inv_vola_table = 1 / vola_table 
sum_inv_vola = np.sum(inv_vola_table)         
vola_target_weights = inv_vola_table / sum_inv_vola

for security, rank in new_portfolio.iteritems():
    weight = vola_target_weights[security]
    if security in kept_positions:
        order_target_percent(security, weight)
    else:
        if ranking_table[security] > minimum_momentum:
            order_target_percent(security, weight)

def analyze(context, perf):

perf['max'] = perf.portfolio_value.cummax()
perf['dd'] = (perf.portfolio_value / perf['max']) - 1
maxdd = perf['dd'].min()

ann_ret = (np.power((perf.portfolio_value.iloc[-1] / perf.portfolio_value.iloc[0]),(252 / len(perf)))) - 1

print("Annualized Return: {:.2%} Max Drawdown: {:.2%}".format(ann_ret, maxdd))

return   

start=Timestamp('1996-01-01')

end =Timestamp('2017-12-31')

start = datetime(1997, 1, 1, 8, 15, 12, 0, pytz.UTC)

end = datetime(2017, 12, 31, 8, 15, 12, 0, pytz.UTC)

start=pd.to_datetime('1996-01-01') end =pd.to_datetime('2017-12-31')

perf = zipline.run_algorithm( start=start, end=end, initialize=initialize, analyze=analyze, capital_base=intial_portfolio,
data_frequency = 'daily', bundle='quandl' )

print(start) type(start)

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

Reproduction Steps

  1. Run the momentum program as is provided in the Trading Evolved website (https://www.dropbox.com/s/tj85sufbsi820ya/Trading%20Evolved.zip?dl=0) under Chapter 12 Momentum / Momentum Model.ipynb
  2. ...

What steps have you taken to resolve this already?

Changed the format of the date ...

Anything else?

...

Sincerely, John J. Sparks, Ph.D.

gnzsnz commented 2 months ago

please try with this https://github.com/stefan-jansen/zipline-reloaded/issues/227#issuecomment-1791995323

tzinfo=None in datetime should do the trick

JohnSparks2024 commented 2 months ago

Regrettably

start=np.datetime64('1996-01-01',tzinfo=None) end =np.datetime64('2017-12-31',tzinfo=None)

gets the error

TypeError: 'tzinfo' is an invalid keyword argument for this function

Any guidance would be appreciated.

--JJS


From: gnzsnz @.> Sent: Tuesday, April 23, 2024 5:35 AM To: stefan-jansen/zipline-reloaded @.> Cc: JohnSparks2024 @.>; Author @.> Subject: Re: [stefan-jansen/zipline-reloaded] Trouble With Dates (Issue #240)

please try with this #227 (comment)https://github.com/stefan-jansen/zipline-reloaded/issues/227#issuecomment-1791995323

tzinfo=None in datetime should do the trick

— Reply to this email directly, view it on GitHubhttps://github.com/stefan-jansen/zipline-reloaded/issues/240#issuecomment-2071966359, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BFDCQCDDWTQOSIPZW2GBPA3Y6Y2OPAVCNFSM6AAAAABFFP4T22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZRHE3DMMZVHE. You are receiving this because you authored the thread.Message ID: @.***>