Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Zipline: Troubles manually running trading algorithm

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.

4 responses

When I was playing with zipline I have noticed that you need to warm up the data for zipline. You already have that code with the skypping and the add.history variable. My recommendation should be that you use as close as possible your algorithm to run from Quantopian instead of creating classes and one last thing. I have been using and running it via the script runa.py to pass all of the variables and symbols

./runa.py -f YourAlgo.py --data-frequency daily \
  --start 2012-10-15 \  
  --end 2015-01-30   \  
  --capital_base 100000 \  
  --output output \  
  --symbols "SPY"

Hi Grant,
you may check this :https://github.com/florentchandelier/zipline2quantopian
I've written a script to automate the transfer of zipline to quantopian, provided that you respect the structure I define.
That way, you may design locally, with classes, and export quantopian compatible code.

I have implemented a simple strategy as example, using a class ;-) ,and will add another for combining multiple strategies in a single Q-script.

HTH

Hi Grant - also, you may also get additional help & zipline-specific info from the zipline forum:
https://groups.google.com/forum/#!forum/zipline

But, FYI, ideally it's good to use the constructs that both share, like symbol(), and
also you can use the get_environment method to write cross-platform code.
https://www.quantopian.com/help#api-get-environment

Thanks for the suggestions. I'll have to look at it more in depth later, and I'll post any results I find in regards to a working version of what I'm trying to do.