Zipline Error: AttributeError: 'NoneType' object has no attribute 'index'

1.2k views Asked by At

I'd like to automate my manual trading strategies. However, for the beginning, I tried to reproduce Zipline's simple example of buying Apple stocks. I struggled running the algorithm with run_algorithm(). When I was trying to run the 'dual moving average cross', the exact same error came up. I also tried IPython and Terminal but still get that error. I couldn't find anything related to that either in this forum. I would very grateful for any hints. Thank you.

I'm using Python 3.6 on macOS and Zipline version 1.1.1.

That's the code:

import zipline as zl from zipline.api import order, record, symbol


def initialize(context):
    pass


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

zl.run_algorithm(start='2015-1-1', end='2017-1-1', initialize=initialize, capital_base=10000)

That's the traceback:

Traceback (most recent call last): File "/Users/SOL/Desktop/Python/backtest.py", line 13, in zl.run_algorithm(start=2015-1-1, end=2017-1-1, initialize=initialize, capital_base=10000) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/utils/run_algo.py", line 360, in run_algorithm environ=environ, File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/utils/run_algo.py", line 132, in _run env = TradingEnvironment(asset_db_path=connstr, environ=environ) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/finance/trading.py", line 99, in init self.bm_symbol, File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/data/loader.py", line 173, in load_market_data environ, File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/data/loader.py", line 287, in ensure_treasury_data if not has_data_for_dates(data, first_date, last_date): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/zipline/data/loader.py", line 87, in has_data_for_dates dts = series_or_df.index AttributeError: 'NoneType' object has no attribute 'index'

2

There are 2 answers

1
Fejs On

According to docs here, start and end are datetime objects, and not strings. So, You should define those as following:

from datetime import datetime

start = datetime(year=2015, month=1, day=1)
end = datetime(year=2017, month=1, day=1)

and then call

zl.run_algorithm(start=start, end=end, initialize=initialize, capital_base=10000)
0
petros On

FYI, I think the problem has to do with SSL certificates in python 3.6. Unfortunately the error message (especially if you ignore the stack trace) is not very helpful. For mac try: sudo /Applications/Python\ 3.6/Install\ Certificatescommand to reinstall the certificates