Tutorial buyapple.py fails in python 2.7.10
running buyapple.py fails in python 2.7.10 but runs in python 3.4.3 for zipline 8.0rc. The part of code that fails is data[symbol('AAPL')] found in
record(AAPL=data[symbol('AAPL')].price)
the error is
Traceback (most recent call last):
File "C:\python2710_64\Scripts\run_algo.py", line 4, in <module>
__import__('pkg_resources').run_script('zipline==0.8.0rc1', 'run_algo.py')
File "C:\Python2710_64\lib\site-packages\pkg_resources\__init__.py", line 729, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "C:\Python2710_64\lib\site-packages\pkg_resources\__init__.py", line 1642, in run_script
exec(code, namespace, namespace)
File "c:\python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\EGG-INFO\scripts\run_algo.py", line 23, in <module>
run_pipeline(print_algo=True, **parsed)
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\cli.py", line 246, in run_pipeline
perf = algo.run(source, overwrite_sim_params=overwrite_sim_params)
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\algorithm.py", line 529, in run
for perf in self.gen:
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.py", line 120, in transform
self.algo.instant_fill,
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.py", line 314, in _process_snapshot
new_orders = self._call_handle_data()
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.py", line 343, in _call_handle_data
self.simulation_dt,
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\events.py", line 194, in handle_data
event.handle_data(context, data, dt)
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\events.py", line 212, in handle_data
self.callback(context, data)
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\algorithm.py", line 306, in handle_data
self._handle_data(self, data)
File "buyapple.py", line 26, in handle_data
record(AAPL=data[symbol('AAPL')].price)
File "C:\Python2710_64\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\protocol.py", line 518, in __getitem__
return self._data[name]
KeyError: Equity(0, symbol='AAPL', asset_name='', exchange='', start_date=None, end_date=None, first_traded=None)
Environment: Windows 7 64 bit, Python 3.4, Python 2.7, zipline 0.8.0rc Appears to be with zipline.protocol.BarData and zipline.assets._assets.Equity
Kind regards
David Bieber
Thank you, David! I am investigating and attempting to reproduce.
Can you provide me with the command that you use to call runalgo.py?
Actually I was running it in a debugger so added main into buyapple.py - here is the code I used
from datetime import datetime
import pytz
from zipline.api import order, record, symbol
from zipline import TradingAlgorithm
from zipline.utils.factory import load_from_yahoo
def initialize(context):
pass
def handle_data(context, data):
order(symbol('AAPL'), 10)
record(AAPL=data[symbol('AAPL')].price)
if __name__ == '__main__':
start = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2010, 1, 1, 0, 0, 0, 0, pytz.utc)
data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,
end=end)
data = data.dropna()
algo = TradingAlgorithm(initialize=initialize,
handle_data=handle_data,
identifiers=['AAPL'])
results = algo.run(data)
results.portfolio_value.plot()
it fails at record(AAPL=data[symbol('AAPL')].price), was trying to debug it but couldn't figure out the inheritance.
Forgot to say in the master 8.0rc there is a missing dependency contextlib2
Ok - I'll look in to the dependency as well. I'll update once I've reproduced.
I've attempted to reproduce, but the code you provided runs fine for me in python 2.7.10 and 3.4.
@twiecki Any ideas on how I should investigate?
Seems like maybe the data isn't correctly loaded?
I have the same error following the tutorial at http://www.zipline.io/tutorial/
code:
%%zipline --start 2000-1-1 --end 2014-1-1 --symbols AAPL -o perf_ipython
import zipline
from zipline.api import order, record, symbol
def initialize(context):
pass
def handle_data(context, data):
order(symbol('AAPL'), 10)
record(AAPL=data[symbol('AAPL')].price)
error message:
AAPL
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-0d07572f6ef6> in <module>()
1
----> 2 get_ipython().run_cell_magic(u'zipline', u'--start 2000-1-1 --end 2014-1-1 --symbols AAPL -o perf_ipython', u"\n\nimport zipline\nfrom zipline.api import order, record, symbol\n\nfrom zipline.api import symbol, order, record\n\ndef initialize(context):\n pass\n\ndef handle_data(context, data):\n order(symbol('AAPL'), 10)\n record(AAPL=data[symbol('AAPL')].price)")
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2262 magic_arg_s = self.var_expand(line, stack_depth)
2263 with self.builtin_trap:
-> 2264 result = fn(magic_arg_s, cell)
2265 return result
2266
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\cli.pyc in parse_cell_magic(line, cell)
138 output_var_name = args.pop('output', None)
139
--> 140 perf = run_pipeline(print_algo=False, algo_text=cell, **args)
141
142 if output_var_name is not None:
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\cli.pyc in run_pipeline(print_algo, **kwargs)
252 end=end)
253
--> 254 perf = algo.run(source, overwrite_sim_params=overwrite_sim_params)
255
256 output_fname = kwargs.get('output', None)
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\algorithm.pyc in run(self, source, overwrite_sim_params, benchmark_return_source)
557 # perf dictionary
558 perfs = []
--> 559 for perf in self.gen:
560 perfs.append(perf)
561
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.pyc in transform(self, stream_in)
118 date,
119 snapshot,
--> 120 self.algo.instant_fill,
121 )
122 # Perf messages are only emitted if the snapshot contained
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.pyc in _process_snapshot(self, dt, snapshot, instant_fill)
296
297 if any_trade_occurred:
--> 298 new_orders = self._call_handle_data()
299 for order in new_orders:
300 perf_process_order(order)
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\gens\tradesimulation.pyc in _call_handle_data(self)
325 self.algo,
326 self.current_data,
--> 327 self.simulation_dt,
328 )
329 orders = self.algo.blotter.new_orders
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\events.pyc in handle_data(self, context, data, dt)
192 def handle_data(self, context, data, dt):
193 for event in self._events:
--> 194 event.handle_data(context, data, dt)
195
196
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\utils\events.pyc in handle_data(self, context, data, dt)
210 """
211 if self.rule.should_trigger(dt):
--> 212 self.callback(context, data)
213
214
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\algorithm.pyc in handle_data(self, data)
334 self.history_container.update(data, self.datetime)
335
--> 336 self._handle_data(self, data)
337
338 # Unlike trading controls which remain constant unless placing an
<string> in handle_data(context, data)
d:\Users\Xingjian Pan\Anaconda\envs\pyfolio\lib\site-packages\zipline-0.8.0rc1-py2.7-win-amd64.egg\zipline\protocol.pyc in __getitem__(self, name)
528
529 def __getitem__(self, name):
--> 530 return self._data[name]
531
532 def __delitem__(self, name):
KeyError: Equity(0, symbol='AAPL', asset_name='', exchange='', start_date=0, end_date=Timestamp('2015-08-29 00:00:00+0000', tz='UTC'), first_traded=None)
@beebeed Can you please pull the latest version of Zipline and let us know whether you still experience problems?
I confirm that I get identical results to Beebeeb and xingjianpan using identical code. Windows 8.1, Python 2.7.10, zipline 0.8.0rc1
None of the examples in the latest build of zipline work with my zipline installation.
@AnthonyFJGarner can you post the command you are using the launch the backtest?
Ok @AnthonyFJGarner I'm assuming you're running the exact same code as @beebeed. I'm still unclear as to why you are getting the KeyError. One thing to note though is that your code needs some additional lines added if it is to plot the results correctly:
import matplotlib.pyplot as plt # put this at the top
...
plt.show() # put this at the end of the main block
I'm using Spyder and the "run" command on byapple.py. Here is the full stack.
Python 2.7.10 |Anaconda 2.3.0 (32-bit)| (default, May 28 2015, 17:02:00) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
In [1]: runfile('C:/Application/github/zipline/zipline/examples/buyapple.py', wdir='C:/Application/github/zipline/zipline/examples')
AAPL
Traceback (most recent call last):
File "<ipython-input-1-2319e3d0f7d4>", line 1, in <module>
runfile('C:/Application/github/zipline/zipline/examples/buyapple.py', wdir='C:/Application/github/zipline/zipline/examples')
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Application/github/zipline/zipline/examples/buyapple.py", line 65, in <module>
results = algo.run(data)
File "c:\users\anthony fj garner\src\zipline\zipline\algorithm.py", line 610, in run
for perf in self.gen:
File "c:\users\anthony fj garner\src\zipline\zipline\gens\tradesimulation.py", line 121, in transform
self.algo.instant_fill,
File "c:\users\anthony fj garner\src\zipline\zipline\gens\tradesimulation.py", line 298, in _process_snapshot
new_orders = self._call_handle_data()
File "c:\users\anthony fj garner\src\zipline\zipline\gens\tradesimulation.py", line 327, in _call_handle_data
self.simulation_dt,
File "c:\users\anthony fj garner\src\zipline\zipline\utils\events.py", line 192, in handle_data
event.handle_data(context, data, dt, context.trading_environment)
File "c:\users\anthony fj garner\src\zipline\zipline\utils\events.py", line 210, in handle_data
self.callback(context, data)
File "c:\users\anthony fj garner\src\zipline\zipline\algorithm.py", line 352, in handle_data
self._handle_data(self, data)
File "C:/Application/github/zipline/zipline/examples/buyapple.py", line 26, in handle_data
record(AAPL=data[symbol('AAPL')].price)
File "c:\users\anthony fj garner\src\zipline\zipline\protocol.py", line 536, in __getitem__
return self._data[name]
KeyError: Equity(0, symbol='AAPL', asset_name='None', exchange='None', start_date=Timestamp('1970-01-01 00:00:00+0000', tz='UTC'), end_date=Timestamp('2116-02-20 23:53:38.427387392+0000', tz='UTC'), first_traded=None)
In [2]:
I get exactly the same Key Error in Spyder with beebeed's code. With xingjianpan's code used in iP notebook again I get exactly the same error.
Have a look at my closing comments on the closed thread #707. It turned out I had the wrong version of sqlite3 in the Python library.
The point is unless you guys specify every single component of every single element in Python / anaconda/ whatever necessary to run the latest version of Zipline, your job is impossible. Even clowns like me are capable of following a simple recipe book. And unfortunately the recipe book needs to be wider than just the zipline dependencies contained in requirement.txt
You Quantopians are not suffering from this Key Error and your back tester is zipline. If, therefore, you gave a more complete recipe of every single piece of software and the correct version (for a given operating system such as Windows 8.1) would it not be simpler to track such issues?
Because at present, is it entirely clear where the issue lies? Within zipline? Or within the wider environment in which zipline operates?
@AnthonyFJGarner thanks for all the very helpful feedback. Did the actions you took in https://github.com/quantopian/zipline/issues/707 also resolve this issue?
Stewart - sorry, I'm new here! Perhaps you are not on the Quantopian team but just trying to kindly help me out? Even so, I do think the Quantopian guys could make life easier if they were more prescise in their set up instructions.
Yes. That particular issue was solved.Now I am faced with a host of other (hopefully less serious) issues. I'm enjoying the process but clearly it is going to take me quite a while to get the comfort I need before using either Zipline or Quantopian for actual trading.
@AnthonyFJGarner No problem. I'm on the Quantopian team and have been trying to get to the bottom of this. Glad this issue in particular has been resolved, and sorry it has taken you some time.
@beebeed @xingjianpan can I suggest you take the action explained here: http://lambdathefirst.blogspot.com/2011/01/how-to-upgrade-sqlite-that-comes-with.html to ensure you have a recent version of SQLite under the hood.
Tried @ssanderson recommendations re generating C files (python setup.py build_ext --inplace) at issue #663 to no avail. Exactly the same problem.