Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buyapple.py example: TypeError: 'zipline._protocol.BarData' object is not subscriptable #2830

Open
kmeans27 opened this issue Nov 9, 2021 · 4 comments

Comments

@kmeans27
Copy link

kmeans27 commented Nov 9, 2021

Dear Zipline Maintainers,

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

Environment

name: ml4t
channels:

  • fastai
  • bashtage
  • ml4t
  • conda-forge
  • defaults
  • anaconda
    dependencies:
  • _py-xgboost-mutex=2.0
  • _pytorch_select=0.1
  • _tflow_select=2.3.0
  • abseil-cpp=20210324.2
  • absl-py=0.13.0
  • aiohttp=3.7.4.post0
  • alembic=1.7.1
  • alphalens-reloaded=0.4.2
  • anyio=3.3.0
  • appdirs=1.4.4
  • arch=4.15
  • argon2-cffi=20.1.0
  • arrow-cpp=5.0.0
  • arviz=0.11.2
  • astor=0.8.1
  • astunparse=1.6.3
  • async-timeout=3.0.1
  • attrs=21.2.0
  • automat=20.2.0
  • autopep8=1.5.7
  • 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.0
  • backports.functools_lru_cache=1.6.4
  • bcolz-zipline=1.2.4
  • bcrypt=3.2.0
  • beautifulsoup4=4.10.0
  • blas=1.0
  • bleach=4.1.0
  • blinker=1.4
  • blosc=1.21.0
  • bokeh=2.3.3
  • bottleneck=1.3.2
  • box2d-py=2.3.8
  • bqplot=0.12.30
  • brotli=1.0.9
  • brotli-bin=1.0.9
  • brotlipy=0.7.0
  • bzip2=1.0.8
  • c-ares=1.17.2
  • ca-certificates=2021.5.30
  • cachetools=4.2.2
  • cairo=1.16.0
  • catalogue=2.0.5
  • catboost=0.26.1
  • certifi=2021.5.30
  • cffi=1.14.6
  • cfitsio=3.470
  • cftime=1.5.0
  • chardet=4.0.0
  • charls=2.2.0
  • charset-normalizer=2.0.0
  • click=7.1.2
  • cloudpickle=1.6.0
  • colorama=0.4.4
  • colorlover=0.3.0
  • conda=4.10.3
  • conda-package-handling=1.7.3
  • constantly=15.1.0
  • cryptography=3.4.7
  • cssselect=1.1.0
  • curl=7.78.0
  • cycler=0.10.0
  • cymem=2.0.5
  • cython=0.29.24
  • cython-blis=0.7.4
  • cytoolz=0.11.0
  • dask-core=2021.9.0
  • dataclasses=0.8
  • debugpy=1.4.1
  • decorator=4.4.2
  • defusedxml=0.7.1
  • dill=0.3.4
  • empyrical-reloaded=0.5.8
  • entrypoints=0.3
  • expat=2.4.1
  • fastprogress=1.0.0
  • ffmpeg=4.3.1
  • filelock=3.0.12
  • font-ttf-dejavu-sans-mono=2.37
  • font-ttf-inconsolata=3.000
  • font-ttf-source-code-pro=2.038
  • font-ttf-ubuntu=0.83
  • fontconfig=2.13.1
  • fonts-conda-ecosystem=1
  • fonts-conda-forge=1
  • freetype=2.10.4
  • fribidi=1.0.10
  • fsspec=2021.8.1
  • funcy=1.16
  • 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=1.35.0
  • google-auth-oauthlib=0.4.6
  • google-pasta=0.2.0
  • googleapis-common-protos=1.53.0
  • graphite2=1.3.13
  • graphviz=2.49.0
  • greenlet=1.1.1
  • grpc-cpp=1.39.1
  • grpcio=1.38.1
  • gts=0.7.6
  • gym=0.19.0
  • gym-box2d=0.19.0
  • h2=3.2.0
  • h5py=2.10.0
  • harfbuzz=2.9.1
  • 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.1
  • idna=3.1
  • imagecodecs=2021.6.8
  • imageio=2.9.0
  • importlib-metadata=4.8.1
  • importlib_metadata=4.8.1
  • importlib_resources=5.2.2
  • incremental=21.3.0
  • inflection=0.5.1
  • intel-openmp=2019.4
  • intervaltree=3.0.2
  • ipydatawidgets=4.2.0
  • ipykernel=6.3.1
  • ipython=7.27.0
  • ipython_genutils=0.2.0
  • ipyvolume=0.6.0a8
  • ipywebrtc=0.6.0
  • ipywidgets=7.6.4
  • iso3166=1.0.1
  • iso4217=1.6.20180829
  • itemadapter=0.4.0
  • itemloaders=1.0.4
  • jbig=2.1
  • jedi=0.18.0
  • jellyfish=0.8.2
  • jinja2=3.0.1
  • jmespath=0.10.0
  • joblib=1.0.1
  • jpeg=9d
  • json5=0.9.5
  • jsonschema=3.2.0
  • jupyter=1.0.0
  • jupyter_client=7.0.2
  • jupyter_console=6.4.0
  • jupyter_contrib_core=0.3.3
  • jupyter_contrib_nbextensions=0.5.1
  • jupyter_core=4.7.1
  • jupyter_highlight_selected_word=0.2.0
  • jupyter_latex_envs=1.4.6
  • jupyter_nbextensions_configurator=0.4.1
  • jupyter_server=1.10.2
  • jupyterlab=3.1.11
  • jupyterlab_server=2.8.1
  • jupyterlab_widgets=1.0.1
  • jxrlib=1.1
  • keras-applications=1.0.8
  • keras-preprocessing=1.1.2
  • kiwisolver=1.3.2
  • krb5=1.19.2
  • lcms2=2.12
  • lerc=2.2.1
  • libaec=1.0.5
  • 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.78.0
  • libdeflate=1.7
  • libffi=3.3
  • libgd=2.3.2
  • libglib=2.68.4
  • 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.16.0
  • libpython=2.0
  • libsodium=1.0.18
  • libssh2=1.10.0
  • libthrift=0.14.2
  • libtiff=4.3.0
  • libutf8proc=2.6.1
  • libwebp=1.2.1
  • libwebp-base=1.2.1
  • libxcb=1.13
  • libxgboost=1.4.0
  • libxml2=2.9.12
  • libxslt=1.1.33
  • libzip=1.8.0
  • libzopfli=1.0.3
  • lightgbm=3.2.1
  • linearmodels=4.24
  • llvmlite=0.36.0
  • locket=0.2.0
  • logbook=1.5.3
  • lru-dict=1.1.7
  • lxml=4.6.3
  • lz4-c=1.9.3
  • m2w64-binutils=2.25.1
  • m2w64-bzip2=1.0.6
  • m2w64-crt-git=5.0.0.4636.2595836
  • 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.c0ad18a
  • m2w64-isl=0.16.1
  • m2w64-libiconv=1.14
  • m2w64-libmangle-git=5.0.0.4509.2e5a9a2
  • m2w64-libwinpthread-git=5.0.0.4634.697f757
  • m2w64-make=4.1.2351.a80a8b8
  • 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.90b8472
  • m2w64-windows-default-manifest=6.4
  • m2w64-winpthreads-git=5.0.0.4634.697f757
  • m2w64-zlib=1.2.8
  • mako=1.1.5
  • markdown=3.3.4
  • markupsafe=2.0.1
  • matplotlib=3.4.3
  • matplotlib-base=3.4.3
  • matplotlib-inline=0.1.3
  • menuinst=1.4.17
  • mistune=0.8.4
  • mkl=2019.4
  • mkl-service=2.3.0
  • mock=4.0.3
  • more-itertools=8.9.0
  • mplfinance=0.12.7a17
  • mpmath=1.2.1
  • msys2-conda-epoch=20160418
  • multidict=5.1.0
  • multipledispatch=0.6.0
  • multitasking=0.0.9
  • murmurhash=1.0.5
  • mypy_extensions=0.4.3
  • nb_conda=2.2.1
  • nb_conda_kernels=2.3.1
  • nbclassic=0.3.1
  • nbconvert=5.6.1
  • nbformat=5.1.3
  • nest-asyncio=1.5.1
  • netcdf4=1.5.7
  • networkx=2.6.2
  • ninja=1.10.2
  • nltk=3.6.2
  • notebook=6.4.3
  • numba=0.53.1
  • numexpr=2.7.3
  • numpy=1.21.2
  • oauthlib=3.1.1
  • olefile=0.46
  • opencv-python-headless=4.5.3.56
  • openjpeg=2.4.0
  • openssl=1.1.1l
  • opt_einsum=3.3.0
  • packaging=21.0
  • pandas=1.2.5
  • pandas-datareader=0.10.0
  • pandoc=2.14.2
  • pandocfilters=1.4.2
  • pango=1.48.9
  • parquet-cpp=1.5.1
  • parsel=1.6.0
  • parso=0.8.2
  • partd=1.2.0
  • pathy=0.6.0
  • patsy=0.5.1
  • pcre=8.45
  • pickleshare=0.7.5
  • pillow=8.3.2
  • pip=21.2.4
  • pixman=0.40.0
  • plotly=5.3.1
  • pooch=1.5.1
  • preshed=3.0.5
  • priority=1.3.0
  • prometheus_client=0.11.0
  • promise=2.3
  • prompt-toolkit=3.0.20
  • prompt_toolkit=3.0.20
  • property-cached=1.6.4
  • property_cached=1.6.4
  • protego=0.1.16
  • protobuf=3.16.0
  • pthread-stubs=0.4
  • py-xgboost=1.4.0
  • pyarrow=5.0.0
  • pyasn1=0.4.8
  • pyasn1-modules=0.2.7
  • pycodestyle=2.7.0
  • pycosat=0.6.3
  • pycparser=2.20
  • pydantic=1.8.2
  • pydispatcher=2.0.5
  • pydot=1.4.2
  • pyfolio-reloaded=0.9.4
  • pyglet=1.5.16
  • pygments=2.10.0
  • pygpu=0.7.6
  • pyjwt=2.1.0
  • pykalman=0.9.5
  • pyldavis=3.3.1
  • pymc3=3.11.4
  • pynndescent=0.5.4
  • pyopenssl=20.0.1
  • pyparsing=2.4.7
  • pyphen=0.11.0
  • 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.17.3
  • pysocks=1.7.1
  • pytables=3.6.1
  • python=3.8.10
  • python-dateutil=2.8.2
  • python-graphviz=0.17
  • python-interface=1.6.0
  • python_abi=3.8
  • pythreejs=2.3.0
  • pytorch=1.6.0
  • pytz=2021.1
  • pyu2f=0.1.5
  • pywavelets=1.1.1
  • pywin32=301
  • pywinpty=1.1.4
  • pyyaml=5.4.1
  • pyzmq=22.2.1
  • qt=5.12.9
  • qtconsole=5.1.1
  • qtpy=1.11.0
  • quandl=3.4.6
  • queuelib=1.6.2
  • re2=2021.09.01
  • regex=2021.8.28
  • requests=2.26.0
  • requests-oauthlib=1.3.0
  • requests-unixsocket=0.2.0
  • rsa=4.7.2
  • ruamel_yaml=0.15.80
  • scikit-image=0.18.3
  • scikit-learn=0.24.2
  • scipy=1.7.1
  • scrapy=2.5.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.3
  • shap=0.39.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.0.1
  • spacy=3.1.2
  • spacy-legacy=3.0.8
  • sqlalchemy=1.4.23
  • sqlite=3.36.0
  • srsly=2.4.1
  • statsmodels=0.12.2
  • sympy=1.8
  • ta-lib=0.4.19
  • tbb=2020.2
  • tenacity=8.0.1
  • tensorboard=2.6.0
  • tensorboard-data-server=0.6.0
  • tensorboard-plugin-wit=1.8.0
  • tensorflow=2.3.0
  • tensorflow-base=2.3.0
  • tensorflow-datasets=4.3.0
  • tensorflow-estimator=2.5.0
  • tensorflow-metadata=0.14.0
  • termcolor=1.1.0
  • terminado=0.12.1
  • testpath=0.5.0
  • textacy=0.11.0
  • textblob=0.15.3
  • theano-pymc=1.1.2
  • thinc=8.0.10
  • threadpoolctl=2.2.0
  • tifffile=2021.7.2
  • tk=8.6.11
  • toml=0.10.2
  • toolz=0.11.1
  • tornado=6.1
  • tqdm=4.62.2
  • trading-calendars=2.1.1
  • traitlets=5.1.0
  • traittypes=0.2.1
  • twisted=21.7.0
  • twisted-iocpsupport=1.0.1
  • typer=0.3.2
  • typing-extensions=3.10.0.0
  • typing_extensions=3.10.0.0
  • ucrt=10.0.20348.0
  • umap-learn=0.5.1
  • urllib3=1.26.6
  • vc=14.2
  • vs2015_runtime=14.29.30037
  • vs2017_win-64=19.16.27038
  • vswhere=2.8.4
  • w3lib=1.22.0
  • wasabi=0.8.2
  • wcwidth=0.2.5
  • webencodings=0.5.1
  • websocket-client=0.57.0
  • werkzeug=2.0.1
  • wheel=0.37.0
  • widgetsnbextension=3.5.1
  • win_inet_pton=1.1.0
  • winpty=0.4.3
  • wordcloud=1.8.1
  • wrapt=1.12.1
  • xarray=0.19.0
  • xgboost=1.4.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.6.3
  • yellowbrick=1.3.post1
  • yfinance=0.1.63
  • zeromq=4.3.4
  • zfp=0.5.5
  • zipline-reloaded=2.1.1
  • zipp=3.5.0
  • zlib=1.2.11
  • zope.interface=5.4.0
  • zstd=1.5.0
  • pip:
    • backtrader==1.9.76.123
    • cvxpy==1.1.15
    • ecos==2.0.7.post1
    • livelossplot==0.5.4
    • osqp==0.6.2.post0
    • pymdptoolbox==4.0b3
    • pyportfolioopt==1.4.2
    • qdldl==0.1.5.post0
    • scs==2.1.2
  • Operating System: (Windows Version or $ uname --all)
  • Python Version: $ python --version
  • Python Bitness: $ python -c 'import math, sys;print(int(math.log(sys.maxsize + 1, 2) + 1))'
  • How did you install Zipline: (pip, conda, or other (please explain))
  • Python packages: $ pip freeze or $ conda list

When i try to run the basic zipline example from https://zipline.ml4trading.io/beginner-tutorial.html

%load_ext zipline

%%zipline --start 2016-1-1 --end 2018-1-1 -o buyapple_out.pickle --no-benchmark
from zipline.api import symbol, order, record

def initialize(context):
pass

def handle_data(context, data):
order(symbol('AAPL'), 10)
record(AAPL=data[symbol('AAPL')].price)

I get a TypeError: "zipline._protocol.BarData' object is not subscriptable"
I set my api key & ingested the quandl data.

What am I missing out here?

zipline issue

@mike576
Copy link

mike576 commented Nov 11, 2021

I have the same issue. I have the feeling the tutorial is not for the newest version of zipline 2.2.0. But it is just a guess.

@mike576
Copy link

mike576 commented Nov 11, 2021

so data[] got deprecated in 2.0 then it removed for 2.2.0 As said the doc is not updated. Here is a working version with 2.2.0:

from zipline import run_algorithm
import pandas as pd
import pandas_datareader.data as web
from zipline.assets import Equity
from zipline.api import symbol, order, record

def initialize(context):
    context.i = 0
    context.asset = symbol('AAPL')


def handle_data(context, data):
    order(symbol('AAPL'), 1.0)
    #record(AAPL=data[symbol('AAPL')].price)
    record(data.current(symbol('AAPL'),"close"))
    

start = pd.Timestamp('2014')
end = pd.Timestamp('2018')

sp500 = web.DataReader('SP500', 'fred', start, end).SP500
benchmark_returns = sp500.pct_change()

result = run_algorithm(start=start.tz_localize('UTC'),
                       end=end.tz_localize('UTC'),
                       initialize=initialize,
                       handle_data=handle_data,
                       capital_base=100000,
                       benchmark_returns=benchmark_returns,
                       bundle='quandl',
                       data_frequency='daily')

@russtoku
Copy link

russtoku commented Aug 19, 2022

Confirming that @mike576's fix:

record(data.current(symbol('AAPL'),"close"))

does work in a notebook cell.

The code in the tutorial for what to put in a notebook cell is incorrect.

record(AAPL=data[symbol('AAPL')].price)

The code in the section A simple example is the similar to @mike576's.

record(AAPL=data.current(symbol('AAPL'), 'price'))

I didn't read the tutorial carefully enough and should have compared the code in both sections.

Thanks @mike576 !

@andycwang
Copy link

andycwang commented Aug 19, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants