quantopian / zipline

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

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

Open kmeans27 opened 2 years ago

kmeans27 commented 2 years ago

Dear Zipline Maintainers,

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

Environment

name: ml4t channels:

* 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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

@.***邮箱联系我,谢谢!