I'm reading the book "Mastering pandas for Finance". Up until where the zipline module is involved all was very smooth and interesting, but now when I need to recreate the books code in the Jupyter Notebook, I'm getting and error from zipline library.
The book's code is:
import zipline as zp
import zipline.utils.factory as zpf
import pandas as pd
import pandas_datareader.data as web
import numpy as np
# dates
from datetime import datetime
# zipline has its own method to load data from Yahoo! Finance
data = zpf.load_from_yahoo(stocks=['AAPL'],
indexes={},
start=datetime(1990, 1, 1),
end=datetime(2014, 1, 1),
adjusted=False)
class BuyApple(zp.TradingAlgorithm):
""" Simple trading algorithm that does nothing
but buy one share of AAPL every trading period.
"""
trace=False
def __init__(self, trace=False):
BuyApple.trace = trace
super(BuyApple, self).__init__()
def initialize(context):
if BuyApple.trace: print("---> initialize")
if BuyApple.trace: print(context)
if BuyApple.trace: print("<--- initialize")
def handle_data(self, context):
if BuyApple.trace: print("---> handle_data")
if BuyApple.trace: print(context)
self.order("AAPL", 1)
if BuyApple.trace: print("<-- handle_data")
result = BuyApple(trace=True).run(data['2000-01-03':'2000-01-07'])
After running it, I get long list of errors, but the last line in the Jupyter Notebook cell is:
/Users/***/anaconda/lib/python3.4/site-packages/zipline/finance/commission.py in __repr__(self)
83 .format(class_name=self.__class__.__name__,
84 cost_per_share=self.cost_per_share,
---> 85 min_trade_cost=self.min_trade_cost)
86
87 def calculate(self, order, transaction):
KeyError: 'cost'
This code is supposed to run a very simple strategy, just buying AAPL every day, but it doesn't work. I think something is amiss within the zipline and something changes since the book was written. I managed to make it run, but with no transactions being made at all. It just shows some data not related to the orders because no order are made and they are not made since I don't instantiate the class BuyApple.
I'm new to Python and to pandas and to zipline as well, so if someone could shed some light as to why this is not working, it's be great. I'm on Python 3.4 ans zipline 1.0.1
Thanks