I have been trying to run a simple TradingAlgorithm manually from another script using zipline, but I've had no luck. My test TradingAlgorithm is in a seperate file, which contains
from zipline.algorithm import TradingAlgorithm
from zipline.api import order_target, record, symbol, history, add_history
class TradingStrategy(TradingAlgorithm):
def initialize(self):
self.__skipping = 10
self.add_history(10, '1d', 'price')
def handle_data(self, data):
# Skip first 10 days
if self.__skipping:
self.__skipping -= 1
return
# print self.__skipping
hist = self.history(10, '1d', 'price')
and the script attempting to run this TradingAlgorithm is
import TradingStrategy as ts
import pytz
from datetime import datetime
from zipline.utils.factory import load_bars_from_yahoo
ticker = 'SPY'
startDate = datetime(2014, 9, 1, 0, 0, 0, 0, pytz.utc)
endDate = datetime(2015, 1, 31, 0, 0, 0, 0, pytz.utc)
data = load_bars_from_yahoo(stocks=[ticker], start=startDate, end=endDate)
algo = ts.MyStrategy()
results = algo.run(data)
The problem seems to arise when calling the history function. I get the following error:
IndexError: index 0 is out of bounds for axis 0 with size 0
and if I change the 10 to a 9 in the history function call, I get
KeyError: '9:1d:price:True'
I have no idea what could be wrong, since I think this is pretty darn straight forward (i.e., pull existing historical values). I'm assuming I'm missing something blatently obvious to a more proficient zipline user, but I simply cannot see it. What could be wrong?
I should note that where I import zipline.api, the items order_target, record, history, and add_history have "unresolved references", even though I have installed zipline on my machine. I'm editing via PyCharm.
Thanks in advance.