machine-learning-for-trading icon indicating copy to clipboard operation
machine-learning-for-trading copied to clipboard

Chapter 18. 08_backtesting_with_zipline.ipynb Error

Open silent0506 opened this issue 3 years ago • 2 comments

Describe the bug I checked the following error while running Chapter 18. 08_backtesting_with_zipline.ipynb What should I do?

This book is very helpful to my study. Thank you.

Import&Settings

from pathlib import Path
from collections import defaultdict
from time import time
import warnings

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import pandas_datareader.data as web
from logbook import Logger, StderrHandler, INFO, WARNING

from zipline import run_algorithm
from zipline.api import (attach_pipeline, pipeline_output,
                         date_rules, time_rules, record,
                         schedule_function, commission, slippage,
                         set_slippage, set_commission, set_max_leverage,
                         order_target, order_target_percent,
                         get_open_orders, cancel_order)
from zipline.data import bundles
from zipline.utils.run_algo import load_extensions
from zipline.pipeline import Pipeline, CustomFactor
from zipline.pipeline.data import Column, DataSet
from zipline.pipeline.domain import US_EQUITIES
from zipline.pipeline.filters import StaticAssets
from zipline.pipeline.loaders import USEquityPricingLoader
from zipline.pipeline.loaders.frame import DataFrameLoader
from trading_calendars import get_calendar

import pyfolio as pf
from pyfolio.plotting import plot_rolling_returns, plot_rolling_sharpe
from pyfolio.timeseries import forecast_cone_bootstrap

from alphalens.tears import (create_returns_tear_sheet,
                             create_summary_tear_sheet,
                             create_full_tear_sheet)

from alphalens.performance import mean_return_by_quantile
from alphalens.plotting import plot_quantile_returns_bar
from alphalens.utils import get_clean_factor_and_forward_returns, rate_of_return

Alphalens Analysis

def get_trade_prices(tickers):
    prices = (pd.read_hdf(DATA_STORE, 'quandl/wiki/prices').swaplevel().sort_index())
    prices.index.names = ['symbol', 'date']
    prices = prices.loc[idx[tickers, '2010':'2018'], 'adj_open']
    return (prices
            .unstack('symbol')
            .sort_index()
            .shift(-1)
            .tz_localize('UTC'))

predictions = (pd.read_hdf(results_path / 'predictions.h5', 'predictions')
               .iloc[:, :4]
               .mean(1)
               .to_frame('prediction'))

factor = (predictions
          .unstack('symbol')
          .asfreq('D')
          .dropna(how='all')
          .stack()
          .tz_localize('UTC', level='date')
          .sort_index())
tickers = factor.index.get_level_values('symbol').unique()
trade_prices = get_trade_prices(tickers).dropna()
factor_data = get_clean_factor_and_forward_returns(factor=factor,
                                                   prices=trade_prices,
                                                   quantiles=5,
                                                   max_loss=1.0,
                                                   periods=(1, 5, 10, 21)).sort_index()

Load zipline extensions

load_extensions(default=True,
                extensions=[],
                strict=True,
                environ=None)
log_handler = StderrHandler(format_string='[{record.time:%Y-%m-%d %H:%M:%S.%f}]: ' +
                            '{record.level_name}: {record.func_name}: {record.message}',
                            level=WARNING)
log_handler.push_application()
log = Logger('Algorithm')

Algo Params

N_LONGS = 25
N_SHORTS = 25
MIN_POSITIONS = 10

Load Data

def load_predictions(bundle):
    predictions = (pd.read_hdf(results_path / 'predictions.h5', 'predictions')
                   .iloc[:, :4]
                   .mean(1)
                   .to_frame('prediction'))
    tickers = predictions.index.get_level_values('symbol').unique().tolist()

    assets = bundle.asset_finder.lookup_symbols(tickers, as_of_date=None)
    predicted_sids = pd.Int64Index([asset.sid for asset in assets])
    ticker_map = dict(zip(tickers, predicted_sids))

    return (predictions
            .unstack('symbol')
            .rename(columns=ticker_map)
            .prediction
            .tz_localize('UTC')), assets

Others codes is Same as GitHub example

Initialize Algorithm

def initialize(context):
    context.longs = context.shorts = None
    set_slippage(slippage.FixedSlippage(spread=0.00))
    schedule_function(rebalance,
                      date_rules.every_day(),
                      time_rules.market_open(hours=1, minutes=30))
    schedule_function(record_vars,
                      date_rules.every_day(),
                      time_rules.market_close())

    pipeline = compute_signals()
    attach_pipeline(pipeline, 'signals')

def before_trading_start(context, data):
    output = pipeline_output('signals')
    longs = pipeline_output('signals').longs.astype(int)
    shorts = pipeline_output('signals').shorts.astype(int)
    holdings = context.portfolio.positions.keys()
    if longs.sum() > MIN_POSITIONS and shorts.sum() > MIN_POSITIONS:
        context.longs = longs[longs!=0].index
        context.shorts = shorts[shorts!=0].index
        context.divest = holdings - set(context.longs) - set(context.shorts)
    else:
        context.longs = context.shorts = pd.Index([])
        context.divest = set(holdings)

def rebalance(context, data):
    for symbol, open_orders in get_open_orders().items():
        for open_order in open_orders:
            cancel_order(open_order)       
    for stock in context.divest:
        order_target(stock, target=0)
    if not (context.longs.empty and context.shorts.empty):
        for stock in context.shorts:
            order_target_percent(stock, -1 / len(context.shorts) / 2)
        for stock in context.longs:
            order_target_percent(stock, 1 / len(context.longs))

def record_vars(context, data):
    record(leverage=context.account.leverage,
           longs=context.longs,
           shorts=context.shorts)

Run Algorithm

dates = predictions.index.get_level_values('date')
start_date, end_date = dates.min(), dates.max()
start = time()
results = run_algorithm(start=start_date,
                        end=end_date,
                        initialize=initialize,
                        before_trading_start=before_trading_start,
                        capital_base=1e5,
                        data_frequency='daily',
                        bundle='quandl',
                        custom_loader=signal_loader)  # need to modify zipline

PyFolio Analysis

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
benchmark = web.DataReader('SP500', 'fred', '2011', '2017').squeeze()
benchmark = benchmark.pct_change().tz_localize('UTC')

Custom Plots

LIVE_DATE = '2016-01-01'
fig, axes = plt.subplots(ncols=2, figsize=(16, 5))
plot_rolling_returns(returns,
                     factor_returns=benchmark,
                     live_start_date=LIVE_DATE,
                     logy=False,
                     cone_std=2,
                     legend_loc='best',
                     volatility_match=False,
                     cone_function=forecast_cone_bootstrap,
                     ax=axes[0])
plot_rolling_sharpe(returns, ax=axes[1], rolling_window=63)
axes[0].set_title('Cumulative Returns - In and Out-of-Sample')
axes[1].set_title('Rolling Sharpe Ratio (3 Months)')
fig.tight_layout()
fig.savefig((results_path / 'pyfolio_out_of_sample').as_posix(), dpi=300)
KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: DatetimeIndex(['2011-04-27 00:00:00+00:00', '2011-04-28 00:00:00+00:00',\n               '2011-04-29 00:00:00+00:00', '2011-05-02 00:00:00+00:00',\n               '2011-05-03 00:00:00+00:00',\n               ...\n               '2017-12-21 00:00:00+00:00', '2017-12-22 00:00:00+00:00',\n               '2017-12-26 00:00:00+00:00', '2017-12-27 00:00:00+00:00',\n               '2017-12-28 00:00:00+00:00'],\n              dtype='datetime64[ns, UTC]', length=493, freq=None). See [https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike%22)

error screenshot image

  • OS/Version:[Windows10]

Additional context Add any other context about the problem here.

silent0506 avatar Apr 13 '22 07:04 silent0506

Hi @silent0506, this looks like a version conflict. Which alphalens, pandas etc versions are you using?

stefan-jansen avatar Apr 13 '22 13:04 stefan-jansen

Hi @silent0506, this looks like a version conflict. Which alphalens, pandas etc versions are you using?

This is my library version.

(ch17.deeplearning > 05_backtesting_with_zipline.ipynb is work well. i don't know why this above file is not working)

stefan, thank you for checking it.

_py-xgboost-mutex 2
_pytorch_select 0.1
_tflow_select 2.3.0
abseil-cpp 20210324.2
absl-py 1.0.0
aiohttp 3.8.1
aiosignal 1.2.0
alembic 1.7.5
alphalens-reloaded 0.4.2
anyio 3.5.0
appdirs 1.4.4
arch 4.15
argon2-cffi 21.3.0
argon2-cffi-bindings 21.2.0
arrow-cpp 6.0.1
arviz 0.11.4
astor 0.8.1
asttokens 2.0.5
astunparse 1.6.3
async-timeout 4.0.2
attrs 21.4.0
automat 20.2.0
autopep8 1.6.0
aws-c-cal 0.5.11
aws-c-common 0.6.2
aws-c-event-stream 0.2.7
aws-c-io 0.10.5
aws-checksums 0.1.11
aws-sdk-cpp 1.8.186
babel 2.9.1
backcall 0.2.0
backports 1
backports.functools_lru_ca che 1.6.4
backtrader 1.9.76.123
bcolz-zipline 1.2.4
bcrypt 3.2.0
beautifulsoup4 4.10.0
black 21.12b0
blas 1
bleach 4.1.0
blinker 1.4
blosc 1.21.0
bokeh 2.4.2
bottleneck 1.3.2
box2d-py 2.3.8
bqplot 0.12.32
brotli 1.0.9
brotli-bin 1.0.9
brotlipy 0.7.0
bzip2 1.0.8
c-ares 1.18.1
c-blosc2 2.0.4
ca-certificates 2021.10.26
cachetools 4.2.4
cairo 1.16.0
catalogue 2.0.6
catboost 1.0.4
certifi 2021.10.8
cffi 1.15.0
cfitsio 4.0.0
cftime 1.5.1.1
charls 2.2.0
charset-normalizer 2.0.10
click 8.0.3
cloudpickle 2.0.0
colorama 0.4.4
colorlover 0.3.0
conda 4.11.0
conda-package-handling 1.7.3
constantly 15.1.0
cryptography 36.0.1
cssselect 1.1.0
curl 7.81.0
cvxpy 1.1.18
cycler 0.11.0
cymem 2.0.6
cython 0.29.26
cython-blis 0.7.5
cytoolz 0.11.2
dask-core 2022.1.0
dataclasses 0.8
debugpy 1.5.1
decorator 5.1.1
defusedxml 0.7.1
dill 0.3.4
ecos 2.0.10
empyrical-reloaded 0.5.8
entrypoints 0.3
enum34 1.1.10
et_xmlfile 1.1.0
exchange-calendars 3.3
executing 0.8.2
expat 2.4.2
fastprogress 1.0.0
ffmpeg 4.3.1
filelock 3.4.2
flit-core 3.6.0
font-ttf-dejavu-sans-mono 2.37
font-ttf-inconsolata 3
font-ttf-source-code-pro 2.038
font-ttf-ubuntu 0.83
fontconfig 2.13.1
fonts-conda-ecosystem 1
fonts-conda-forge 1
fonttools 4.28.5
freetype 2.10.4
fribidi 1.0.10
frozenlist 1.2.0
fsspec 2022.1.0
funcy 1.17
future 0.18.2
gast 0.4.0
gensim 3.8.3
getopt-win32 0.1
gettext 0.19.8.1
gflags 2.2.2
giflib 5.2.1
glog 0.5.0
google-auth 2.3.3
google-auth-oauthlib 0.4.6
google-pasta 0.2.0
googleapis-common-protos 1.54.0
graphite2 1.3.13
graphviz 2.47.1
greenlet 1.1.2
grpc-cpp 1.42.0
grpcio 1.43.0
gts 0.7.6
gym 0.21.0
gym-box2d 0.21.0
h2 3.2.0
h5py 2.10.0
harfbuzz 2.8.0
hdbscan 0.8.27
hdf4 4.2.15
hdf5 1.10.6
hpack 3.0.0
html5lib 1.1
hyperframe 5.2.0
hyperlink 21.0.0
icu 68.2
idna 3.3
imagecodecs 2021.11.20
imageio 2.13.5
importlib-metadata 4.10.0
importlib_metadata 4.10.0
importlib_resources 5.4.0
incremental 21.3.0
inflection 0.5.1
intel-openmp 2019.4
intervaltree 3.1.0
ipydatawidgets 4.2.0
ipykernel 6.7.0
ipython 8.0.0
ipython_genutils 0.2.0
ipyvolume 0.6.0a8
ipywebrtc 0.6.0
ipywidgets 7.6.5
iso3166 2.0.2
iso4217 1.6.20180829
itemadapter 0.4.0
itemloaders 1.0.4
jbig 2.1
jedi 0.18.1
jellyfish 0.9.0
jinja2 3.0.3
jmespath 0.10.0
joblib 1.1.0
jpeg 9d
json5 0.9.6
jsonschema 4.4.0
jupyter 1.0.0
jupyter_client 7.1.1
jupyter_console 6.4.0
jupyter_contrib_core 0.3.3
jupyter_contrib_nbextensio ns 0.5.1
jupyter_core 4.9.1
jupyter_highlight_selected _word 0.2.0
jupyter_latex_envs 1.4.6
jupyter_nbextensions_confi gurator 0.4.1
jupyter_server 1.13.3
jupyterlab 3.2.8
jupyterlab_server 2.10.3
jupyterlab_widgets 1.0.2
jxrlib 1.1
keras 2.2.4
keras-applications 1.0.8
keras-preprocessing 1.1.2
kiwisolver 1.3.2
korean_lunar_calendar 0.2.1
krb5 1.19.2
langcodes 3.3.0
lcms2 2.12
lerc 3
libaec 1.0.6
libblas 3.8.0
libbrotlicommon 1.0.9
libbrotlidec 1.0.9
libbrotlienc 1.0.9
libcblas 3.8.0
libclang 11.1.0
libcurl 7.81.0
libdeflate 1.8
libffi 3.4.2
libgd 2.3.3
libglib 2.70.2
libgpuarray 0.7.6
libiconv 1.16
liblapack 3.8.0
libmklml 2019.0.5
libnetcdf 4.8.1
libpng 1.6.37
libprotobuf 3.19.3
libpython 2.1
libsodium 1.0.18
libssh2 1.10.0
libthrift 0.15.0
libtiff 4.3.0
libutf8proc 2.7.0
libwebp 1.2.1
libwebp-base 1.2.1
libxcb 1.13
libxgboost 1.5.0
libxml2 2.9.12
libxslt 1.1.34
libzip 1.8.0
libzlib 1.2.11
libzopfli 1.0.3
lightgbm 3.3.2
linearmodels 4.24
livelossplot 0.5.4
llvmlite 0.36.0
locket 0.2.1
logbook 1.5.3
lru-dict 1.1.7
lxml 4.7.1
lz4-c 1.9.3
m2w64-binutils 2.25.1
m2w64-bzip2 1.0.6
m2w64-crt-git 5.0.0.4636.259
m2w64-gcc 5.3.0
m2w64-gcc-ada 5.3.0
m2w64-gcc-fortran 5.3.0
m2w64-gcc-libgfortran 5.3.0
m2w64-gcc-libs 5.3.0
m2w64-gcc-libs-core 5.3.0
m2w64-gcc-objc 5.3.0
m2w64-gmp 6.1.0
m2w64-headers-git 5.0.0.4636.c0a
m2w64-isl 0.16.1
m2w64-libiconv 1.14
m2w64-libmangle-git 5.0.0.4509.2e5
m2w64-libwinpthread-git 5.0.0.4634.697
m2w64-make 4.1.2351.a80a8
m2w64-mpc 1.0.3
m2w64-mpfr 3.1.4
m2w64-pkg-config 0.29.1
m2w64-toolchain 5.3.0
m2w64-tools-git 5.0.0.4592.90b
m2w64-windows-default-mani fest 6.4
m2w64-winpthreads-git 5.0.0.4634.697
m2w64-zlib 1.2.8
mako 1.1.6
markdown 3.3.6
markupsafe 2.0.1
matplotlib 3.5.1
matplotlib-base 3.5.1
matplotlib-inline 0.1.3
menuinst 1.4.18
mistune 0.8.4
mkl 2019.4
mkl-service 2.3.0
mock 4.0.3
more-itertools 8.12.0
mplfinance 0.12.8b6
mpmath 1.2.1
msys2-conda-epoch 20160418
multidict 5.2.0
multipledispatch 0.6.0
multitasking 0.0.9
munkres 1.1.4
murmurhash 1.0.6
mypy_extensions 0.4.3
nb_conda 2.2.1
nb_conda_kernels 2.3.1
nbclassic 0.3.5
nbconvert 5.6.1
nbformat 5.1.3
nest-asyncio 1.5.4
netcdf4 1.5.7
networkx 2.6.3
ninja 1.10.2
nltk 3.6.7
notebook 6.4.7
numba 0.53.1
numexpr 2.7.3
numpy 1.22.0
oauthlib 3.1.1
olefile 0.46
opencv-python-headless 4.5.4.60
openjpeg 2.4.0
openpyxl 3.0.9
openssl 1.1.1m
opt_einsum 3.3.0
osqp 0.6.2.post5
packaging 21.3
pandas 1.2.5
pandas-datareader 0.10.0
pandoc 2.17
pandocfilters 1.5.0
pango 1.48.4
parquet-cpp 1.5.1
parsel 1.6.0
parso 0.8.3
partd 1.2.0
pathspec 0.9.0
pathy 0.6.1
patsy 0.5.2
pcre 8.45
pickleshare 0.7.5
pillow 8.4.0
pip 21.2.2
pixman 0.40.0
plaidbench 0.7.0
plaidml 0.7.0
plaidml-keras 0.7.0
platformdirs 2.4.0
plotly 5.5.0
preshed 3.0.6
priority 1.3.0
prometheus_client 0.12.0
promise 2.3
prompt-toolkit 3.0.24
prompt_toolkit 3.0.24
property-cached 1.6.4
property_cached 1.6.4
protego 0.1.16
protobuf 3.19.3
pthread-stubs 0.4
pure_eval 0.2.1
py-xgboost 1.5.0
pyarrow 6.0.1
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycodestyle 2.8.0
pycosat 0.6.3
pycparser 2.21
pydantic 1.8.2
pydispatcher 2.0.5
pydot 1.4.2
pyfolio-reloaded 0.9.4
pyglet 1.5.16
pygments 2.11.2
pygpu 0.7.6
pyhdfe 0.1.0
pyjwt 2.3.0
pykalman 0.9.5
pyldavis 3.3.1
pyluach 1.3.0
pymc3 3.11.4
pymdptoolbox 4.0b3
pynndescent 0.5.5
pyopenssl 21.0.0
pyparsing 3.0.6
pyphen 0.12.0
pyportfolioopt 1.5.1
pyqt 5.12.3
pyqt-impl 5.12.3
pyqt5-sip 4.19.18
pyqtchart 5.12
pyqtwebengine 5.12.1
pyreadline 2.1
pyrsistent 0.18.0
pysocks 1.7.1
pytables 3.6.1
python 3.8.12
python-dateutil 2.8.2
python-graphviz 0.19.1
python-interface 1.6.0
python_abi 3.8
pythreejs 2.3.0
pytorch 1.6.0
pytz 2021.3
pyu2f 0.1.5
pywavelets 1.2.0
pywin32 303
pywinpty 0.5.7
pyyaml 6
pyzmq 22.3.0
qdldl 0.1.5.post0
qt 5.12.9
qtconsole 5.2.2
qtconsole-base 5.2.2
qtpy 2.0.0
quandl 3.6.1
queuelib 1.6.2
re2 2021.11.01
regex 2021.11.10
requests 2.27.1
requests-oauthlib 1.3.0
rsa 4.8
ruamel_yaml 0.15.100
scikit-image 0.19.1
scikit-learn 1.0.2
scipy 1.6.3
scrapy 2.5.0
scs 3.1.0
seaborn 0.11.2
seaborn-base 0.11.2
semver 2.13.0
send2trash 1.8.0
service_identity 18.1.0
setuptools 58.0.4
shap 0.40.0
shellingham 1.4.0
six 1.15.0
slicer 0.0.7
smart_open 5.2.1
snappy 1.1.8
sniffio 1.2.0
sortedcontainers 2.4.0
soupsieve 2.3.1
spacy 3.2.1
spacy-legacy 3.0.8
spacy-loggers 1.0.1
sqlalchemy 1.4.29
sqlite 3.37.0
srsly 2.4.2
stack_data 0.1.3
statsmodels 0.13.1
sympy 1.9
ta-lib 0.4.21
tbb 2020.3
tenacity 8.0.1
tensorboard 2.7.0
tensorboard-data-server 0.6.0
tensorboard-plugin-wit 1.8.1
tensorflow 2.3.0
tensorflow-base 2.3.0
tensorflow-datasets 4.3.0
tensorflow-estimator 2.6.0
tensorflow-metadata 0.14.0
termcolor 1.1.0
terminado 0.9.5
testpath 0.5.0
textacy 0.11.0
textblob 0.15.3
theano-pymc 1.1.2
thinc 8.0.13
threadpoolctl 3.0.0
tifffile 2021.11.2
tk 8.6.11
toml 0.10.2
tomli 1.2.2
toolz 0.11.2
tornado 6.1
tqdm 4.62.3
trading-calendars 2.1.1
traitlets 5.1.1
traittypes 0.2.1
twisted 21.7.0
twisted-iocpsupport 1.0.2
typed-ast 1.5.1
typer 0.4.0
typing-extensions 3.10.0.2
typing_extensions 3.10.0.2
umap-learn 0.5.2
unicodedata2 14.0.0
urllib3 1.26.8
vc 14.2
vs2015_runtime 14.27.29016
vs2017_win-64 19.16.27033
vswhere 2.8.4
w3lib 1.22.0
wasabi 0.9.0
wcwidth 0.2.5
webencodings 0.5.1
websocket-client 1.2.3
werkzeug 2.0.2
wheel 0.37.1
widgetsnbextension 3.5.2
win_inet_pton 1.1.0
wincertstore 0.2
winpty 0.4.3
wordcloud 1.8.1
wrapt 1.13.3
xarray 0.20.2
xgboost 1.5.0
xlrd 2.0.1
xorg-kbproto 1.0.7
xorg-libice 1.0.10
xorg-libsm 1.2.3
xorg-libx11 1.7.2
xorg-libxau 1.0.9
xorg-libxdmcp 1.1.3
xorg-libxext 1.3.4
xorg-libxpm 3.5.13
xorg-libxt 1.2.1
xorg-xextproto 7.3.0
xorg-xproto 7.0.31
xz 5.2.5
yaml 0.2.5
yarl 1.7.2
yellowbrick 1.3.post1
yfinance 0.1.69
zeromq 4.3.4
zfp 0.5.5
zipline-reloaded 2.2.0
zipp 3.7.0
zlib 1.2.11
zope.interface 5.4.0
zstd 1.5.1

silent0506 avatar Apr 13 '22 19:04 silent0506

The error states that using .loc[] with dates not present in the target is no longer supported in later pandas versions. Either downgrade pandas or filter the date list beforehand.

stefan-jansen avatar Jan 03 '23 01:01 stefan-jansen