Hello and welcome to my very first blog post. In this post I will cover how to install zipline locally using Python 3.5.4. Zipline is in my opinion the best finance backtesting strategy system for Python. While you can use Quantopian for basic strategy backtesting, for free, local zipline offers you a lot of more options. Zipline offers you a lot of different datasets, even your own for example if you need minute data, which is not provided by zipline, the most popular one is probably Quandl. If you ant to do live trading (and potentially lose money), then there’s an option for you called zipline-live. Also if your strategy requires heavy deep learning or you want to do high frequency trading you have to go with a local (or some individually hosted)environment.

So let’s get started with the installation. First installing zipline can be a absolute pain because it requires a lot of other packages and in case one is buggy the whole system could crash. LOL. First for this tutorial I am gonna be using Python 3.5 since it’s currently the most compatible version with all the other packages I use. I would also recommend you using Python 3.5 (as of this writing). Using Python 3.5 we can install the older pandas version, which is required by zipline. This version can’t be installed on Python 3.6. I have zipline installed on Windows and Ubuntu

I am personally using Zipline 1.3.0 on Python 3.5 on Windows at the moment.

Installation on Ubunntu 18.04:

$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install python3-dev

$ sudo apt-get install libatlas-base-dev gfortran pkg-config libfreetype6-dev
$ sudo apt-get install python3-pip

$ pip3 install numpy
$ pip3 install cython
$ pip3 install -U setuptools
$ pip3 install zipline

Installation on Windows:

First on Windows it’s a bit hacky. At the time I am writing this Python 3.5 is the standart for Zipline 1.3.0. A key dependence of Zipline is pandas, here you need pandas 0.18.1 or 0.22.0, which is an older release. To install to Python 3.5, here’s the list of dependences, linking to the unofficial binaries page:

cython
numpy+mkl
sqlalchemy
bcolz
lru dict
wrapt
stats-models
bottleneck
cyordereddict

All of those can be downloaded from Unofficial Windows Binaries for Python site.

Now if that is all done, we can continue installing with pandas. As I mentioned already we need to use either pandas==0.18.1 or pandas==0.22.0. But first we need to do pip3 install zipline. This may probably fail. But then it downloads the other non C++ libs. Now if that is done let’s run pip3 install pandas=0.18.1 or replace 0.18.1 with 0.22.0 if you need it for any reason.

Finally get zipline. I downloaded it from here

The last and fianl step is to install zipline. We need to do pip3 install –no-deps zipline-1.3.0-cp35-cp35m-win_amd64.whl. And then we are done.

Test your installation:

Run python and do a import zipline

Using Zipline

Now first for this you need a Quandl account to get all the financial data. It’s worth mentioning that you need to verify your email adress so that you can use your API key. You should then find your quandl api key under your account. For Windows Users run setx QUANDL_API_KEY “your_api_key”. Now ingest your data using the following command: zipline ingest -b quandl. If you have a good internet connection that shouldn’t take too long.

Code Example

In this case we use the “buy_appl” “strategy”, so we order 10 shares every day.

from zipline.api import order, record, symbol


def initialize(context):
    pass


def handle_data(context, data):
    order(symbol('AAPL'), 10)
    record(AAPL=data.current(symbol('AAPL'), 'price'))

save the file as apple_backtest.py and run it on the command line using

zipline run --bundle quandl -f apple_backtest.py --start 2000-1-1 --end 2018-1-1 --output buyapple_out.pickle 

For more detail about running zipline in an IPython Notebook, checkout the Zipline Doc

**Thanks for reading and maybe we see each other on Twitter @nowsden or reach out to me via hello@leonnowsden.com **

Disclaimer: This is for educational and informational purpose only.