나는 "Mastering pandas for Finance"라는 책을 읽고있다. Zipline 모듈이 포함될 때까지는 모두 매우 부드럽고 재미있었습니다.하지만 지금 Jupyter Notebook에서 책 코드를 다시 작성해야 할 때 Zipline 라이브러리에서 오류가 발생합니다.나는 "Mastering pandas for Finance"라는 코드를 복제 할 수 없다. - Zipline - KeyError : 'Cost'
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'])
를 실행 한 후, 나는 오류의 긴 목록을 얻을 수 있지만, Jupyter 노트북 셀의 마지막 줄은 다음과 같습니다 :
이 책의 코드는
/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'
이 코드는 가정된다 매일 간단한 AAPL을 구입하는 아주 간단한 전략을 실행하지만 작동하지 않습니다. 나는 무언가가 Zipline 내에서 잘못되었다고 생각하고 책이 쓰여진 이후로 뭔가 바뀐다고 생각합니다. 나는 그것을 만들 수 있었지만 거래가 전혀 이루어지지 않았다. 순서가 없기 때문에 OrderApplication 클래스를 인스턴스화하지 않았기 때문에 Order와 관련이없는 데이터를 보여줍니다.
저는 파이썬과 팬더, 그리고 지 플린에 익숙합니다. 그래서 누군가가 이것이 작동하지 않는 이유에 대해 밝히면, 훌륭합니다. 저는 Python 3.4 및 Zipline 1.0.1에 있습니다.