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'
According to docs here,
start
andend
aredatetime
objects, and not strings. So, You should define those as following:and then call