Zipline Beginner Tutorial

Basics

Zipline is an open-source algorithmic trading simulator written in Python.

The source can be found at: https://github.com/quantopian/zipline

Some benefits include:

  • Realistic: slippage, transaction costs, order delays.
  • Stream-based: Process each event individually, avoids look-ahead bias.
  • Batteries included: Common transforms (moving average) as well as common risk calculations (Sharpe).
  • Developed and continuously updated by Quantopian which provides an easy-to-use web-interface to Zipline, 10 years of minute-resolution historical US stock data, and live-trading capabilities. This tutorial is directed at users wishing to use Zipline without using Quantopian. If you instead want to get started on Quantopian, see here.

This tutorial assumes that you have zipline correctly installed, see the installation instructions if you haven't set up zipline yet.

Every zipline algorithm consists of two functions you have to define:

  • initialize(context)
  • handle_data(context, data)

Before the start of the algorithm, zipline calls the initialize() function and passes in a context variable. context is a persistent namespace for you to store variables you need to access from one algorithm iteration to the next.

After the algorithm has been initialized, zipline calls the handle_data() function once for each event. At every call, it passes the same context variable and an event-frame called data containing the current trading bar with open, high, low, and close (OHLC) prices as well as volume for each stock in your universe. For more information on these functions, see the relevant part of the Quantopian docs.

My first algorithm

Lets take a look at a very simple algorithm from the examples directory, buyapple.py:


In [1]:
# assuming you're running this notebook in zipline/docs/notebooks
import os

if os.name == 'nt':
    # windows doesn't have the cat command, but uses 'type' similarly
    ! type "..\..\zipline\examples\buyapple.py"
else:
    ! cat ../../zipline/examples/buyapple.py


#!/usr/bin/env python
#
# Copyright 2014 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from zipline.api import order, record, symbol
from zipline.finance import commission


def initialize(context):
    context.asset = symbol('AAPL')

    # Explicitly set the commission to the "old" value until we can
    # rebuild example data.
    # github.com/quantopian/zipline/blob/master/tests/resources/
    # rebuild_example_data#L105
    context.set_commission(commission.PerShare(cost=.0075, min_trade_cost=1.0))


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


# Note: this function can be removed if running
# this algorithm on quantopian.com
def analyze(context=None, results=None):
    import matplotlib.pyplot as plt
    # Plot the portfolio and asset data.
    ax1 = plt.subplot(211)
    results.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('Portfolio value (USD)')
    ax2 = plt.subplot(212, sharex=ax1)
    results.AAPL.plot(ax=ax2)
    ax2.set_ylabel('AAPL price (USD)')

    # Show the plot.
    plt.gcf().set_size_inches(18, 8)
    plt.show()


def _test_args():
    """Extra arguments to use when zipline's automated tests run this example.
    """
    import pandas as pd

    return {
        'start': pd.Timestamp('2014-01-01', tz='utc'),
        'end': pd.Timestamp('2014-11-01', tz='utc'),
    }

As you can see, we first have to import some functions we would like to use. All functions commonly used in your algorithm can be found in zipline.api. Here we are using order() which takes two arguments -- a security object, and a number specifying how many stocks you would like to order (if negative, order() will sell/short stocks). In this case we want to order 10 shares of Apple at each iteration. For more documentation on order(), see the Quantopian docs.

Finally, the record() function allows you to save the value of a variable at each iteration. You provide it with a name for the variable together with the variable itself: varname=var. After the algorithm finished running you will have access to each variable value you tracked with record() under the name you provided (we will see this further below). You also see how we can access the current price data of the AAPL stock in the data event frame (for more information see here).

Ingesting data for your algorithm

Before we can run the algorithm, we'll need some historical data for our algorithm to ingest, which we can get through a data bundle. A data bundle is a collection of pricing data, adjustment data, and an asset database. Bundles allow us to preload all of the data we will need to run backtests and store the data for future runs. Quantopian provides a default bundle called quandl which uses the Quandl WIKI Dataset. You'll need a Quandl API Key, and then you can ingest that data by running:


In [ ]:
! QUANDL_API_KEY=<yourkey> zipline ingest -b quandl

For more information on data bundles, such as building custom data bundles, you can look at the zipline docs.

Running the algorithm

To now test this algorithm on financial data, zipline provides two interfaces. A command-line interface and an IPython Notebook interface.

Command line interface

After you installed zipline you should be able to execute the following from your command line (e.g. cmd.exe on Windows, or the Terminal app on OSX):


In [24]:
!zipline run --help


Usage: zipline run [OPTIONS]

  Run a backtest for the given algorithm.

Options:
  -f, --algofile FILENAME         The file that contains the algorithm to run.
  -t, --algotext TEXT             The algorithm script to run.
  -D, --define TEXT               Define a name to be bound in the namespace
                                  before executing the algotext. For example
                                  '-Dname=value'. The value may be any python
                                  expression. These are evaluated in order so
                                  they may refer to previously defined names.
  --data-frequency [minute|daily]
                                  The data frequency of the simulation.
                                  [default: daily]
  --capital-base FLOAT            The starting capital for the simulation.
                                  [default: 10000000.0]
  -b, --bundle BUNDLE-NAME        The data bundle to use for the simulation.
                                  [default: quandl]
  --bundle-timestamp TIMESTAMP    The date to lookup data on or before.
                                  [default: <current-time>]
  -s, --start DATE                The start date of the simulation.
  -e, --end DATE                  The end date of the simulation.
  -o, --output FILENAME           The location to write the perf data. If this
                                  is '-' the perf will be written to stdout.
                                  [default: -]
  --trading-calendar TRADING-CALENDAR
                                  The calendar you want to use e.g. LSE. NYSE
                                  is the default.
  --print-algo / --no-print-algo  Print the algorithm to stdout.
  --help                          Show this message and exit.

Note that you have to omit the preceding '!' when you call run_algo.py, this is only required by the IPython Notebook in which this tutorial was written.

As you can see there are a couple of flags that specify where to find your algorithm (-f) as well as the time-range (--start and --end). Finally, you'll want to save the performance metrics of your algorithm so that you can analyze how it performed. This is done via the --output flag and will cause it to write the performance DataFrame in the pickle Python file format.

Thus, to execute our algorithm from above and save the results to buyapple_out.pickle we would call run_algo.py as follows:


In [25]:
!zipline run -f ../../zipline/examples/buyapple.py --start 2016-1-1 --end 2018-1-1 -o buyapple_out.pickle


[2018-01-03 04:46:19.968831] WARNING: Loader: Refusing to download new benchmark data because a download succeeded at 2018-01-03 04:01:34+00:00.
[2018-01-03 04:46:20.009540] WARNING: Loader: Refusing to download new treasury data because a download succeeded at 2018-01-03 04:01:35+00:00.
[2018-01-03 04:46:21.720073] INFO: Performance: Simulated 503 trading days out of 503.
[2018-01-03 04:46:21.720217] INFO: Performance: first open: 2016-01-04 14:31:00+00:00
[2018-01-03 04:46:21.720308] INFO: Performance: last close: 2017-12-29 21:00:00+00:00
Figure(1440x640)

run_algo.py first outputs the algorithm contents. It then uses historical price and volume data of Apple from the quantopian-quandl bundle in the desired time range, calls the initialize() function, and then streams the historical stock price day-by-day through handle_data(). After each call to handle_data() we instruct zipline to order 10 stocks of AAPL. After the call of the order() function, zipline enters the ordered stock and amount in the order book. After the handle_data() function has finished, zipline looks for any open orders and tries to fill them. If the trading volume is high enough for this stock, the order is executed after adding the commission and applying the slippage model which models the influence of your order on the stock price, so your algorithm will be charged more than just the stock price * 10. (Note, that you can also change the commission and slippage model that zipline uses, see the Quantopian docs for more information).

Note that there is also an analyze() function printed. run_algo.py will try and look for a file with the ending with _analyze.py and the same name of the algorithm (so buyapple_analyze.py) or an analyze() function directly in the script. If an analyze() function is found it will be called after the simulation has finished and passed in the performance DataFrame. (The reason for allowing specification of an analyze() function in a separate file is that this way buyapple.py remains a valid Quantopian algorithm that you can copy&paste to the platform).

Lets take a quick look at the performance DataFrame. For this, we use pandas from inside the IPython Notebook and print the first ten rows. Note that zipline makes heavy usage of pandas, especially for data input and outputting so it's worth spending some time to learn it.


In [26]:
import pandas as pd
perf = pd.read_pickle('buyapple_out.pickle') # read in perf DataFrame
perf.head()


Out[26]:
AAPL algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ... short_exposure short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2016-01-04 21:00:00+00:00 105.35 NaN 0.000000e+00 NaN -0.013983 NaN NaN 0.0 10000000.0 0.0 ... 0 0 0 NaN 10000000.0 0.0 0.0 1 [] 0.0
2016-01-05 21:00:00+00:00 102.71 0.000001 -1.000000e-07 -0.000022 -0.012312 0.175994 -0.000006 -1028.1 9998971.9 1027.1 ... 0 0 0 -11.224972 10000000.0 0.0 0.0 2 [{'dt': 2016-01-05 21:00:00+00:00, 'order_id':... 0.0
2016-01-06 21:00:00+00:00 100.70 0.000019 -2.210000e-06 -0.000073 -0.024771 0.137853 0.000054 -1008.0 9997963.9 2014.0 ... 0 0 0 -9.588756 9998971.9 1027.1 1027.1 3 [{'dt': 2016-01-06 21:00:00+00:00, 'order_id':... 0.0
2016-01-07 21:00:00+00:00 96.45 0.000064 -1.081000e-05 0.000243 -0.048168 0.167868 0.000300 -965.5 9996998.4 2893.5 ... 0 0 0 -9.688947 9997963.9 2014.0 2014.0 4 [{'dt': 2016-01-07 21:00:00+00:00, 'order_id':... 0.0
2016-01-08 21:00:00+00:00 96.96 0.000063 -9.380000e-06 0.000466 -0.058601 0.145654 0.000311 -970.6 9996027.8 3878.4 ... 0 0 0 -7.519659 9996998.4 2893.5 2893.5 5 [{'dt': 2016-01-08 21:00:00+00:00, 'order_id':... 0.0

5 rows × 38 columns

As you can see, there is a row for each trading day, starting on the first business day of 2016. In the columns you can find various information about the state of your algorithm. The very first column AAPL was placed there by the record() function mentioned earlier and allows us to plot the price of apple. For example, we could easily examine now how our portfolio value changed over time compared to the AAPL stock price.


In [27]:
%pylab inline
figsize(12, 12)
import matplotlib.pyplot as plt

ax1 = plt.subplot(211)
perf.portfolio_value.plot(ax=ax1)
ax1.set_ylabel('Portfolio Value')
ax2 = plt.subplot(212, sharex=ax1)
perf.AAPL.plot(ax=ax2)
ax2.set_ylabel('AAPL Stock Price')


Populating the interactive namespace from numpy and matplotlib
Out[27]:
<matplotlib.text.Text at 0x118d64438>

As you can see, our algorithm performance as assessed by the portfolio_value closely matches that of the AAPL stock price. This is not surprising as our algorithm only bought AAPL every chance it got.

IPython Notebook

The IPython Notebook is a very powerful browser-based interface to a Python interpreter (this tutorial was written in it). As it is already the de-facto interface for most quantitative researchers zipline provides an easy way to run your algorithm inside the Notebook without requiring you to use the CLI.

To use it you have to write your algorithm in a cell and let zipline know that it is supposed to run this algorithm. This is done via the %%zipline IPython magic command that is available after you run %load_ext zipline in a separate cell. This magic takes the same arguments as the command line interface described above.


In [28]:
%load_ext zipline


The zipline extension is already loaded. To reload it, use:
  %reload_ext zipline

In [29]:
%%zipline --start 2016-1-1 --end 2018-1-1 -o perf_ipython.pickle

from zipline.api import symbol, order, record

def initialize(context):
    context.asset = symbol('AAPL')

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


Out[29]:
AAPL algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ... short_exposure short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2016-01-04 21:00:00+00:00 105.350 NaN 0.000000e+00 NaN -0.013983 NaN NaN 0.00 10000000.00 0.00 ... 0 0 0 NaN 10000000.00 0.00 0.00 1 [] 0.0
2016-01-05 21:00:00+00:00 102.710 1.122497e-08 -1.000000e-09 -2.247510e-07 -0.012312 0.175994 -6.378047e-08 -1027.11 9998972.89 1027.10 ... 0 0 0 -11.224972 10000000.00 0.00 0.00 2 [{'dt': 2016-01-05 21:00:00+00:00, 'amount': 1... 0.0
2016-01-06 21:00:00+00:00 100.700 1.842654e-05 -2.012000e-06 -4.883861e-05 -0.024771 0.137853 5.744807e-05 -1007.01 9997965.88 2014.00 ... 0 0 0 -9.169708 9998972.89 1027.10 1027.10 3 [{'dt': 2016-01-06 21:00:00+00:00, 'amount': 1... 0.0
2016-01-07 21:00:00+00:00 96.450 6.394658e-05 -1.051300e-05 2.633450e-04 -0.048168 0.167868 3.005102e-04 -964.51 9997001.37 2893.50 ... 0 0 0 -9.552189 9997965.88 2014.00 2014.00 4 [{'dt': 2016-01-07 21:00:00+00:00, 'amount': 1... 0.0
2016-01-08 21:00:00+00:00 96.960 6.275294e-05 -8.984000e-06 4.879306e-04 -0.058601 0.145654 3.118401e-04 -969.61 9996031.76 3878.40 ... 0 0 0 -7.301134 9997001.37 2893.50 2893.50 5 [{'dt': 2016-01-08 21:00:00+00:00, 'amount': 1... 0.0
2016-01-11 21:00:00+00:00 98.530 7.674349e-05 -2.705000e-06 8.837486e-04 -0.057684 0.154953 4.033007e-04 -985.31 9995046.45 4926.50 ... 0 0 0 -2.006727 9996031.76 3878.40 3878.40 6 [{'dt': 2016-01-11 21:00:00+00:00, 'amount': 1... 0.0
2016-01-12 21:00:00+00:00 99.960 8.358973e-05 4.444000e-06 9.120981e-04 -0.050077 0.177554 4.111938e-04 -999.61 9994046.84 5997.60 ... 0 0 0 3.052375 9995046.45 4926.50 4926.50 7 [{'dt': 2016-01-12 21:00:00+00:00, 'amount': 1... 0.0
2016-01-13 21:00:00+00:00 97.390 1.187830e-04 -1.097700e-05 9.520761e-04 -0.073773 0.192029 5.438943e-04 -973.91 9993072.93 6817.30 ... 0 0 0 -3.476065 9994046.84 5997.60 5997.60 8 [{'dt': 2016-01-13 21:00:00+00:00, 'amount': 1... 0.0
2016-01-14 21:00:00+00:00 99.520 1.405986e-04 3.932000e-06 1.065698e-03 -0.058567 0.225894 5.751722e-04 -995.21 9992077.72 7961.60 ... 0 0 0 1.174035 9993072.93 6817.30 6817.30 9 [{'dt': 2016-01-14 21:00:00+00:00, 'amount': 1... 0.0
2016-01-15 21:00:00+00:00 97.130 1.649569e-04 -1.518900e-05 9.532919e-04 -0.078776 0.225683 6.561426e-04 -971.31 9991106.41 8741.70 ... 0 0 0 -2.924499 9992077.72 7961.60 7961.60 10 [{'dt': 2016-01-15 21:00:00+00:00, 'amount': 1... 0.0
2016-01-19 21:00:00+00:00 96.660 1.570293e-04 -1.942000e-05 6.768119e-04 -0.077549 0.218789 6.161130e-04 -966.61 9990139.80 9666.00 ... 0 0 0 -3.519120 9991106.41 8741.70 8741.70 11 [{'dt': 2016-01-19 21:00:00+00:00, 'amount': 1... 0.0
2016-01-20 21:00:00+00:00 96.790 1.503787e-04 -1.812100e-05 7.799722e-04 -0.089371 0.210175 5.988146e-04 -967.91 9989171.89 10646.90 ... 0 0 0 -3.143921 9990139.80 9666.00 9666.00 12 [{'dt': 2016-01-20 21:00:00+00:00, 'amount': 1... 0.0
2016-01-21 21:00:00+00:00 96.300 1.449871e-04 -2.351200e-05 4.337086e-04 -0.084269 0.209564 5.293433e-04 -963.01 9988208.88 11556.00 ... 0 0 0 -3.840063 9989171.89 10646.90 10646.90 13 [{'dt': 2016-01-21 21:00:00+00:00, 'amount': 1... 0.0
2016-01-22 21:00:00+00:00 101.420 3.023445e-04 3.792700e-05 1.842053e-03 -0.065483 0.232034 9.733837e-04 -1014.21 9987194.67 13184.60 ... 0 0 0 5.969375 9988208.88 11556.00 11556.00 14 [{'dt': 2016-01-22 21:00:00+00:00, 'amount': 1... 0.0
2016-01-25 21:00:00+00:00 99.440 3.138152e-04 1.218600e-05 1.618378e-03 -0.079610 0.227613 1.035162e-03 -994.41 9986200.26 13921.60 ... 0 0 0 1.340362 9987194.67 13184.60 13184.60 15 [{'dt': 2016-01-25 21:00:00+00:00, 'amount': 1... 0.0
2016-01-26 21:00:00+00:00 99.990 3.044035e-04 1.988500e-05 1.340071e-03 -0.067053 0.232544 9.638415e-04 -999.91 9985200.35 14998.50 ... 0 0 0 2.117548 9986200.26 13921.60 13921.60 16 [{'dt': 2016-01-26 21:00:00+00:00, 'amount': 1... 0.0
2016-01-27 21:00:00+00:00 93.420 4.842411e-04 -7.866600e-05 1.647133e-04 -0.077206 0.226614 1.143236e-03 -934.21 9984266.14 14947.20 ... 0 0 0 -2.874444 9985200.35 14998.50 14998.50 17 [{'dt': 2016-01-27 21:00:00+00:00, 'amount': 1... 0.0
2016-01-28 21:00:00+00:00 94.090 4.732794e-04 -6.794700e-05 2.339515e-04 -0.072399 0.222902 1.154621e-03 -940.91 9983325.23 15995.30 ... 0 0 0 -2.412770 9984266.14 14947.20 14947.20 18 [{'dt': 2016-01-28 21:00:00+00:00, 'amount': 1... 0.0
2016-01-29 21:00:00+00:00 97.340 5.077018e-04 -1.269800e-05 6.922634e-04 -0.049783 0.240133 1.325918e-03 -973.41 9982351.82 17521.20 ... 0 0 0 -0.438594 9983325.23 15995.30 15995.30 19 [{'dt': 2016-01-29 21:00:00+00:00, 'amount': 1... 0.0
2016-02-01 21:00:00+00:00 96.430 4.972985e-04 -2.907900e-05 4.514561e-04 -0.050130 0.233860 1.316425e-03 -964.31 9981387.51 18321.70 ... 0 0 0 -0.967745 9982351.82 17521.20 17521.20 20 [{'dt': 2016-02-01 21:00:00+00:00, 'amount': 1... 0.0
2016-02-02 21:00:00+00:00 94.480 5.001476e-04 -6.613000e-05 3.115951e-04 -0.067249 0.234222 1.367873e-03 -944.81 9980442.70 18896.00 ... 0 0 0 -2.029144 9981387.51 18321.70 18321.70 21 [{'dt': 2016-02-02 21:00:00+00:00, 'amount': 1... 0.0
2016-02-03 21:00:00+00:00 96.350 5.070214e-04 -2.873100e-05 6.708211e-04 -0.061657 0.230689 1.423254e-03 -963.51 9979479.19 20233.50 ... 0 0 0 -0.861110 9980442.70 18896.00 18896.00 22 [{'dt': 2016-02-03 21:00:00+00:00, 'amount': 1... 0.0
2016-02-04 21:00:00+00:00 96.600 4.958391e-04 -2.348200e-05 6.749304e-04 -0.060185 0.225846 1.423586e-03 -966.01 9978513.18 21252.00 ... 0 0 0 -0.688261 9979479.19 20233.50 20233.50 23 [{'dt': 2016-02-04 21:00:00+00:00, 'amount': 1... 0.0
2016-02-05 21:00:00+00:00 94.020 5.174863e-04 -8.024300e-05 4.250432e-04 -0.078089 0.227224 1.531726e-03 -940.21 9977572.97 21624.60 ... 0 0 0 -2.057677 9978513.18 21252.00 21252.00 24 [{'dt': 2016-02-05 21:00:00+00:00, 'amount': 1... 0.0
2016-02-08 21:00:00+00:00 95.010 5.133303e-04 -5.747400e-05 7.666502e-04 -0.090499 0.224774 1.447049e-03 -950.11 9976622.86 22802.40 ... 0 0 0 -1.443922 9977572.97 21624.60 21624.60 25 [{'dt': 2016-02-08 21:00:00+00:00, 'amount': 1... 0.0
2016-02-09 21:00:00+00:00 94.990 5.029907e-04 -5.795500e-05 7.293430e-04 -0.090450 0.220541 1.444361e-03 -949.91 9975672.95 23747.50 ... 0 0 0 -1.427724 9976622.86 22802.40 22802.40 26 [{'dt': 2016-02-09 21:00:00+00:00, 'amount': 1... 0.0
2016-02-10 21:00:00+00:00 94.270 4.955715e-04 -7.595600e-05 5.368129e-04 -0.091235 0.216414 1.433851e-03 -942.71 9974730.24 24510.20 ... 0 0 0 -1.817949 9975672.95 23747.50 23747.50 27 [{'dt': 2016-02-10 21:00:00+00:00, 'amount': 1... 0.0
2016-02-11 21:00:00+00:00 93.700 4.876403e-04 -9.077700e-05 5.490663e-04 -0.103056 0.214296 1.430667e-03 -937.01 9973793.23 25299.00 ... 0 0 0 -2.119355 9974730.24 24510.20 24510.20 28 [{'dt': 2016-02-11 21:00:00+00:00, 'amount': 1... 0.0
2016-02-12 21:00:00+00:00 93.990 4.799642e-04 -8.294800e-05 2.659222e-04 -0.084564 0.222393 1.328421e-03 -939.91 9972853.32 26317.20 ... 0 0 0 -1.902857 9973793.23 25299.00 25299.00 29 [{'dt': 2016-02-12 21:00:00+00:00, 'amount': 1... 0.0
2016-02-16 21:00:00+00:00 96.640 5.218332e-04 -8.749000e-06 7.873800e-04 -0.069113 0.225953 1.493891e-03 -966.41 9971886.91 28025.60 ... 0 0 0 -0.197002 9972853.32 26317.20 26317.20 30 [{'dt': 2016-02-16 21:00:00+00:00, 'amount': 1... 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2017-11-16 21:00:00+00:00 171.100 7.308922e-03 2.187551e-02 8.840147e-03 0.268553 0.106704 2.036027e-02 -1711.01 9409452.08 809303.00 ... 0 0 0 2.511117 9411163.09 798057.60 798057.60 474 [{'dt': 2017-11-16 21:00:00+00:00, 'amount': 1... 0.0
2017-11-17 21:00:00+00:00 170.150 7.309766e-03 2.142616e-02 8.611247e-03 0.264826 0.106621 2.042734e-02 -1701.51 9407750.57 806511.00 ... 0 0 0 2.451649 9409452.08 809303.00 809303.00 475 [{'dt': 2017-11-17 21:00:00+00:00, 'amount': 1... 0.0
2017-11-20 21:00:00+00:00 169.980 7.302622e-03 2.134558e-02 8.534012e-03 0.266984 0.106512 2.041914e-02 -1699.81 9406050.76 807405.00 ... 0 0 0 2.439790 9407750.57 806511.00 806511.00 476 [{'dt': 2017-11-20 21:00:00+00:00, 'amount': 1... 0.0
2017-11-21 21:00:00+00:00 173.140 7.368124e-03 2.284657e-02 9.172929e-03 0.275273 0.106490 2.078451e-02 -1731.41 9404319.35 824146.40 ... 0 0 0 2.606411 9406050.76 807405.00 807405.00 477 [{'dt': 2017-11-21 21:00:00+00:00, 'amount': 1... 0.0
2017-11-22 21:00:00+00:00 174.960 7.383253e-03 2.371289e-02 9.617252e-03 0.274145 0.106384 2.072980e-02 -1749.61 9402569.74 834559.20 ... 0 0 0 2.701082 9404319.35 824146.40 824146.40 478 [{'dt': 2017-11-22 21:00:00+00:00, 'amount': 1... 0.0
2017-11-24 18:00:00+00:00 174.970 7.375597e-03 2.371766e-02 9.575341e-03 0.277088 0.106280 2.072305e-02 -1749.71 9400820.03 836356.60 ... 0 0 0 2.698796 9402569.74 834559.20 834559.20 479 [{'dt': 2017-11-24 18:00:00+00:00, 'amount': 1... 0.0
2017-11-27 21:00:00+00:00 174.090 7.375427e-03 2.329702e-02 9.342283e-03 0.276451 0.106172 2.074413e-02 -1740.91 9399079.12 833891.10 ... 0 0 0 2.643239 9400820.03 836356.60 836356.60 480 [{'dt': 2017-11-27 21:00:00+00:00, 'amount': 1... 0.0
2017-11-28 21:00:00+00:00 173.070 7.377554e-03 2.280844e-02 9.007340e-03 0.289400 0.106289 2.042091e-02 -1730.71 9397348.41 830736.00 ... 0 0 0 2.578540 9399079.12 833891.10 833891.10 481 [{'dt': 2017-11-28 21:00:00+00:00, 'amount': 1... 0.0
2017-11-29 21:00:00+00:00 169.480 7.475500e-03 2.108524e-02 8.101617e-03 0.288615 0.106182 2.051276e-02 -1694.81 9395653.60 815198.80 ... 0 0 0 2.304045 9397348.41 830736.00 830736.00 482 [{'dt': 2017-11-29 21:00:00+00:00, 'amount': 1... 0.0
2017-11-30 21:00:00+00:00 171.850 7.507854e-03 2.222521e-02 8.524716e-03 0.299897 0.106237 2.085597e-02 -1718.51 9393935.09 828317.00 ... 0 0 0 2.424491 9395653.60 815198.80 815198.80 483 [{'dt': 2017-11-30 21:00:00+00:00, 'amount': 1... 0.0
2017-12-01 21:00:00+00:00 171.050 7.506282e-03 2.183961e-02 8.326902e-03 0.297199 0.106144 2.090085e-02 -1710.51 9392224.58 826171.50 ... 0 0 0 2.376606 9393935.09 828317.00 828317.00 484 [{'dt': 2017-12-01 21:00:00+00:00, 'amount': 1... 0.0
2017-12-04 21:00:00+00:00 169.800 7.512507e-03 2.123586e-02 8.009011e-03 0.295630 0.106042 2.094981e-02 -1698.01 9390526.57 821832.00 ... 0 0 0 2.300123 9392224.58 826171.50 826171.50 485 [{'dt': 2017-12-04 21:00:00+00:00, 'amount': 1... 0.0
2017-12-05 21:00:00+00:00 169.640 7.505249e-03 2.115842e-02 7.991427e-03 0.290970 0.105975 2.095600e-02 -1696.41 9388830.16 822754.00 ... 0 0 0 2.289334 9390526.57 821832.00 821832.00 486 [{'dt': 2017-12-05 21:00:00+00:00, 'amount': 1... 0.0
2017-12-06 21:00:00+00:00 169.010 7.501569e-03 2.085286e-02 7.817362e-03 0.291215 0.105866 2.096153e-02 -1690.11 9387140.05 821388.60 ... 0 0 0 2.252073 9388830.16 822754.00 822754.00 487 [{'dt': 2017-12-06 21:00:00+00:00, 'amount': 1... 0.0
2017-12-07 21:00:00+00:00 169.452 7.494835e-03 2.106768e-02 7.873977e-03 0.295286 0.105774 2.097516e-02 -1694.53 9385445.52 825231.24 ... 0 0 0 2.272642 9387140.05 821388.60 821388.60 488 [{'dt': 2017-12-07 21:00:00+00:00, 'amount': 1... 0.0
2017-12-08 21:00:00+00:00 169.370 7.487383e-03 2.102774e-02 7.784692e-03 0.302350 0.105724 2.093342e-02 -1693.71 9383751.81 826525.60 ... 0 0 0 2.266029 9385445.52 825231.24 825231.24 489 [{'dt': 2017-12-08 21:00:00+00:00, 'amount': 1... 0.0
2017-12-11 21:00:00+00:00 172.670 7.560247e-03 2.263814e-02 8.523554e-03 0.306274 0.105631 2.110096e-02 -1726.71 9382025.10 844356.30 ... 0 0 0 2.434840 9383751.81 826525.60 826525.60 490 [{'dt': 2017-12-11 21:00:00+00:00, 'amount': 1... 0.0
2017-12-12 21:00:00+00:00 171.700 7.561349e-03 2.216381e-02 8.253266e-03 0.308579 0.105526 2.107144e-02 -1717.01 9380308.09 841330.00 ... 0 0 0 2.376243 9382025.10 844356.30 844356.30 491 [{'dt': 2017-12-12 21:00:00+00:00, 'amount': 1... 0.0
2017-12-13 21:00:00+00:00 172.270 7.555414e-03 2.244311e-02 8.378753e-03 0.308432 0.105420 2.106380e-02 -1722.71 9378585.38 845845.70 ... 0 0 0 2.403340 9380308.09 841330.00 841330.00 492 [{'dt': 2017-12-13 21:00:00+00:00, 'amount': 1... 0.0
2017-12-14 21:00:00+00:00 172.220 7.547895e-03 2.241856e-02 8.394349e-03 0.303085 0.105365 2.105762e-02 -1722.21 9376863.17 847322.40 ... 0 0 0 2.398295 9378585.38 845845.70 845845.70 493 [{'dt': 2017-12-14 21:00:00+00:00, 'amount': 1... 0.0
2017-12-15 21:00:00+00:00 173.870 7.559184e-03 2.323036e-02 8.736036e-03 0.307255 0.105275 2.114189e-02 -1738.71 9375124.46 857179.10 ... 0 0 0 2.481452 9376863.17 847322.40 847322.40 494 [{'dt': 2017-12-15 21:00:00+00:00, 'amount': 1... 0.0
2017-12-18 21:00:00+00:00 176.420 7.598478e-03 2.448750e-02 9.234755e-03 0.315544 0.105249 2.142327e-02 -1764.21 9373360.25 871514.80 ... 0 0 0 2.611242 9375124.46 857179.10 857179.10 495 [{'dt': 2017-12-18 21:00:00+00:00, 'amount': 1... 0.0
2017-12-19 21:00:00+00:00 174.540 7.621292e-03 2.355878e-02 8.772408e-03 0.310492 0.105190 2.159785e-02 -1745.41 9371614.84 863973.00 ... 0 0 0 2.487916 9373360.25 871514.80 871514.80 496 [{'dt': 2017-12-19 21:00:00+00:00, 'amount': 1... 0.0
2017-12-20 21:00:00+00:00 174.350 7.614248e-03 2.346473e-02 8.713083e-03 0.309805 0.105087 2.160363e-02 -1743.51 9369871.33 864776.00 ... 0 0 0 2.475394 9371614.84 863973.00 863973.00 497 [{'dt': 2017-12-20 21:00:00+00:00, 'amount': 1... 0.0
2017-12-21 21:00:00+00:00 175.010 7.609064e-03 2.379209e-02 8.832559e-03 0.312503 0.104987 2.162015e-02 -1750.11 9368121.22 869799.70 ... 0 0 0 2.506926 9369871.33 864776.00 864776.00 498 [{'dt': 2017-12-21 21:00:00+00:00, 'amount': 1... 0.0
2017-12-22 21:00:00+00:00 175.010 7.601495e-03 2.379209e-02 8.817554e-03 0.312160 0.104883 2.162127e-02 -1750.11 9366371.11 871549.80 ... 0 0 0 2.504413 9368121.22 869799.70 869799.70 499 [{'dt': 2017-12-22 21:00:00+00:00, 'amount': 1... 0.0
2017-12-26 21:00:00+00:00 170.570 7.753823e-03 2.158097e-02 7.699556e-03 0.310590 0.104785 2.179675e-02 -1705.71 9364665.40 851144.30 ... 0 0 0 2.162710 9366371.11 871549.80 871549.80 500 [{'dt': 2017-12-26 21:00:00+00:00, 'amount': 1... 0.0
2017-12-27 21:00:00+00:00 170.600 7.746091e-03 2.159594e-02 7.686211e-03 0.311228 0.104680 2.179684e-02 -1706.01 9362959.39 853000.00 ... 0 0 0 2.162029 9364665.40 851144.30 851144.30 501 [{'dt': 2017-12-27 21:00:00+00:00, 'amount': 1... 0.0
2017-12-28 21:00:00+00:00 171.080 7.739554e-03 2.183594e-02 7.764757e-03 0.313926 0.104581 2.180779e-02 -1710.81 9361248.58 857110.80 ... 0 0 0 2.183557 9362959.39 853000.00 853000.00 502 [{'dt': 2017-12-28 21:00:00+00:00, 'amount': 1... 0.0
2017-12-29 21:00:00+00:00 169.230 7.761038e-03 2.090909e-02 7.312205e-03 0.308971 0.104522 2.197793e-02 -1692.31 9359556.27 849534.60 ... 0 0 0 2.072850 9361248.58 857110.80 857110.80 503 [{'dt': 2017-12-29 21:00:00+00:00, 'amount': 1... 0.0

503 rows × 38 columns

Note that we did not have to specify an input file as above since the magic will use the contents of the cell and look for your algorithm functions there.


In [30]:
pd.read_pickle('perf_ipython.pickle').head()


Out[30]:
AAPL algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ... short_exposure short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2016-01-04 21:00:00+00:00 105.35 NaN 0.000000e+00 NaN -0.013983 NaN NaN 0.00 10000000.00 0.0 ... 0 0 0 NaN 10000000.00 0.0 0.0 1 [] 0.0
2016-01-05 21:00:00+00:00 102.71 1.122497e-08 -1.000000e-09 -2.247510e-07 -0.012312 0.175994 -6.378047e-08 -1027.11 9998972.89 1027.1 ... 0 0 0 -11.224972 10000000.00 0.0 0.0 2 [{'dt': 2016-01-05 21:00:00+00:00, 'order_id':... 0.0
2016-01-06 21:00:00+00:00 100.70 1.842654e-05 -2.012000e-06 -4.883861e-05 -0.024771 0.137853 5.744807e-05 -1007.01 9997965.88 2014.0 ... 0 0 0 -9.169708 9998972.89 1027.1 1027.1 3 [{'dt': 2016-01-06 21:00:00+00:00, 'order_id':... 0.0
2016-01-07 21:00:00+00:00 96.45 6.394658e-05 -1.051300e-05 2.633450e-04 -0.048168 0.167868 3.005102e-04 -964.51 9997001.37 2893.5 ... 0 0 0 -9.552189 9997965.88 2014.0 2014.0 4 [{'dt': 2016-01-07 21:00:00+00:00, 'order_id':... 0.0
2016-01-08 21:00:00+00:00 96.96 6.275294e-05 -8.984000e-06 4.879306e-04 -0.058601 0.145654 3.118401e-04 -969.61 9996031.76 3878.4 ... 0 0 0 -7.301134 9997001.37 2893.5 2893.5 5 [{'dt': 2016-01-08 21:00:00+00:00, 'order_id':... 0.0

5 rows × 38 columns

Access to previous prices using data.history()

Working example: Dual Moving Average Cross-Over

The Dual Moving Average (DMA) is a classic momentum strategy. It's probably not used by any serious trader anymore but is still very instructive. The basic idea is that we compute two rolling or moving averages (mavg) -- one with a longer window that is supposed to capture long-term trends and one shorter window that is supposed to capture short-term trends. Once the short-mavg crosses the long-mavg from below we assume that the stock price has upwards momentum and long the stock. If the short-mavg crosses from above we exit the positions as we assume the stock to go down further.

As we need to have access to previous prices to implement this strategy we need a new concept: History

data.history() is a convenience function that keeps a rolling window of data for you. The first argument is the asset or iterable of assets you're using, the second argument is the field you're looking for i.e. price, open, volume, the third argument is the number of bars, and the fourth argument is your frequency (either '1d' for '1m' but note that you need to have minute-level data for using 1m).

For a more detailed description of data.history()'s features, see the Quantopian docs. Let's look at the strategy which should make this clear:


In [31]:
%pylab inline
figsize(12, 12)


Populating the interactive namespace from numpy and matplotlib

In [32]:
%%zipline --start 2014-1-1 --end 2018-1-1 -o perf_dma.pickle

from zipline.api import order_target, record, symbol
import numpy as np
import matplotlib.pyplot as plt

def initialize(context):
    context.i = 0
    context.asset = symbol('AAPL')


def handle_data(context, data):
    # Skip first 300 days to get full windows
    context.i += 1
    if context.i < 300:
        return

    # Compute averages
    # data.history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
    long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()

    # Trading logic
    if short_mavg > long_mavg:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(context.asset, 100)
    elif short_mavg < long_mavg:
        order_target(context.asset, 0)

    # Save values for later inspection
    record(AAPL=data.current(context.asset, 'price'),
           short_mavg=short_mavg,
           long_mavg=long_mavg)


def analyze(context, perf):
    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')
    ax1.set_xlabel('time in years')

    ax2 = plt.subplot(212, sharex=ax1)

    perf['AAPL'].plot(ax=ax2)
    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)

    perf_trans = perf.ix[[t != [] for t in perf.transactions]]
    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]
    sells = perf_trans.ix[[t[0]['amount'] < 0 for t in perf_trans.transactions]]
    ax2.plot(buys.index, perf.short_mavg.ix[buys.index], '^', markersize=10, color='m')
    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],'v', markersize=10, color='k')
    ax2.set_ylabel('price in $')
    ax2.set_xlabel('time in years')
    plt.legend(loc=0)
    plt.show()


Out[32]:
AAPL algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ... short_mavg short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2014-01-02 21:00:00+00:00 NaN NaN 0.000000 NaN -0.009584 NaN NaN 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 1 [] 0.0
2014-01-03 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.009773 0.105428 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 2 [] 0.0
2014-01-06 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.012616 0.076806 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 3 [] 0.0
2014-01-07 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.006552 0.103395 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 4 [] 0.0
2014-01-08 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.006335 0.090495 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 5 [] 0.0
2014-01-09 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.005685 0.081883 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 6 [] 0.0
2014-01-10 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.002978 0.077910 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 7 [] 0.0
2014-01-13 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.016271 0.102266 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 8 [] 0.0
2014-01-14 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.005523 0.117689 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 9 [] 0.0
2014-01-15 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.000162 0.114949 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 10 [] 0.0
2014-01-16 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.001462 0.109229 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 11 [] 0.0
2014-01-17 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.005712 0.105864 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 12 [] 0.0
2014-01-21 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.002761 0.102473 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 13 [] 0.0
2014-01-22 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.002112 0.098518 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 14 [] 0.0
2014-01-23 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.010288 0.100518 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 15 [] 0.0
2014-01-24 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.031404 0.127109 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 16 [] 0.0
2014-01-27 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.036169 0.123598 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 17 [] 0.0
2014-01-28 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.030429 0.123670 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 18 [] 0.0
2014-01-29 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.039742 0.123597 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 19 [] 0.0
2014-01-30 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.029563 0.128474 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 20 [] 0.0
2014-01-31 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.035248 0.126142 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 21 [] 0.0
2014-02-03 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.056960 0.141856 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 22 [] 0.0
2014-02-04 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.050382 0.142191 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 23 [] 0.0
2014-02-05 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.051546 0.139101 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 24 [] 0.0
2014-02-06 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.039038 0.144634 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 25 [] 0.0
2014-02-07 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.027127 0.148215 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 26 [] 0.0
2014-02-10 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.025340 0.145597 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 27 [] 0.0
2014-02-11 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.014673 0.147234 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 28 [] 0.0
2014-02-12 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.014186 0.144610 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 29 [] 0.0
2014-02-13 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.009096 0.143024 0.000000 0.0 10000000.0 0.0 ... NaN 0 0 NaN 10000000.0 0.0 0.0 30 [] 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2017-11-16 21:00:00+00:00 171.100 0.000190 0.000486 0.000071 0.400292 0.122557 0.000581 0.0 9987753.7 17110.0 ... 157.284780 0 0 0.969223 9987753.7 16908.0 16908.0 978 [] 0.0
2017-11-17 21:00:00+00:00 170.150 0.000190 0.000477 0.000068 0.396177 0.122506 0.000581 0.0 9987753.7 17015.0 ... 157.533680 0 0 0.949155 9987753.7 17110.0 17110.0 979 [] 0.0
2017-11-20 21:00:00+00:00 169.980 0.000189 0.000475 0.000068 0.398560 0.122445 0.000581 0.0 9987753.7 16998.0 ... 157.802300 0 0 0.945269 9987753.7 17015.0 17015.0 980 [] 0.0
2017-11-21 21:00:00+00:00 173.140 0.000190 0.000507 0.000074 0.407710 0.122423 0.000584 0.0 9987753.7 17314.0 ... 158.099130 0 0 1.007593 9987753.7 16998.0 16998.0 981 [] 0.0
2017-11-22 21:00:00+00:00 174.960 0.000190 0.000525 0.000079 0.406465 0.122362 0.000583 0.0 9987753.7 17496.0 ... 158.419340 0 0 1.043234 9987753.7 17314.0 17314.0 982 [] 0.0
2017-11-24 18:00:00+00:00 174.970 0.000190 0.000525 0.000079 0.409714 0.122304 0.000583 0.0 9987753.7 17497.0 ... 158.733780 0 0 1.042902 9987753.7 17496.0 17496.0 983 [] 0.0
2017-11-27 21:00:00+00:00 174.090 0.000190 0.000516 0.000077 0.409010 0.122242 0.000584 0.0 9987753.7 17409.0 ... 159.052960 0 0 1.024299 9987753.7 17497.0 17497.0 984 [] 0.0
2017-11-28 21:00:00+00:00 173.070 0.000190 0.000506 0.000073 0.423304 0.122280 0.000581 0.0 9987753.7 17307.0 ... 159.347500 0 0 1.002759 9987753.7 17409.0 17409.0 985 [] 0.0
2017-11-29 21:00:00+00:00 169.480 0.000191 0.000470 0.000063 0.422438 0.122219 0.000581 0.0 9987753.7 16948.0 ... 159.597370 0 0 0.922113 9987753.7 17307.0 17307.0 986 [] 0.0
2017-11-30 21:00:00+00:00 171.850 0.000191 0.000494 0.000068 0.434891 0.122230 0.000584 0.0 9987753.7 17185.0 ... 159.866260 0 0 0.968085 9987753.7 16948.0 16948.0 987 [] 0.0
2017-12-01 21:00:00+00:00 171.050 0.000191 0.000486 0.000066 0.431913 0.122175 0.000584 0.0 9987753.7 17105.0 ... 160.125060 0 0 0.951470 9987753.7 17185.0 17185.0 988 [] 0.0
2017-12-04 21:00:00+00:00 169.800 0.000191 0.000473 0.000063 0.430180 0.122115 0.000585 0.0 9987753.7 16980.0 ... 160.351140 0 0 0.925446 9987753.7 17105.0 17105.0 989 [] 0.0
2017-12-05 21:00:00+00:00 169.640 0.000191 0.000472 0.000063 0.425037 0.122070 0.000585 0.0 9987753.7 16964.0 ... 160.562970 0 0 0.921836 9987753.7 16980.0 16980.0 990 [] 0.0
2017-12-06 21:00:00+00:00 169.010 0.000191 0.000465 0.000061 0.425307 0.122009 0.000585 0.0 9987753.7 16901.0 ... 160.763320 0 0 0.908801 9987753.7 16964.0 16964.0 991 [] 0.0
2017-12-07 21:00:00+00:00 169.452 0.000191 0.000470 0.000062 0.429801 0.121955 0.000585 0.0 9987753.7 16945.2 ... 160.962910 0 0 0.916965 9987753.7 16901.0 16901.0 992 [] 0.0
2017-12-08 21:00:00+00:00 169.370 0.000191 0.000469 0.000061 0.437598 0.121920 0.000584 0.0 9987753.7 16937.0 ... 161.152320 0 0 0.914900 9987753.7 16945.2 16945.2 993 [] 0.0
2017-12-11 21:00:00+00:00 172.670 0.000191 0.000502 0.000069 0.441930 0.121866 0.000586 0.0 9987753.7 17267.0 ... 161.381500 0 0 0.978747 9987753.7 16937.0 16937.0 994 [] 0.0
2017-12-12 21:00:00+00:00 171.700 0.000191 0.000492 0.000066 0.444475 0.121807 0.000586 0.0 9987753.7 17170.0 ... 161.601680 0 0 0.958688 9987753.7 17267.0 17267.0 995 [] 0.0
2017-12-13 21:00:00+00:00 172.270 0.000191 0.000498 0.000067 0.444312 0.121746 0.000585 0.0 9987753.7 17227.0 ... 161.809430 0 0 0.969295 9987753.7 17170.0 17170.0 996 [] 0.0
2017-12-14 21:00:00+00:00 172.220 0.000191 0.000498 0.000068 0.438410 0.121705 0.000585 0.0 9987753.7 17222.0 ... 162.010200 0 0 0.967835 9987753.7 17227.0 17227.0 997 [] 0.0
2017-12-15 21:00:00+00:00 173.870 0.000191 0.000514 0.000071 0.443013 0.121652 0.000586 0.0 9987753.7 17387.0 ... 162.220300 0 0 0.999415 9987753.7 17222.0 17222.0 998 [] 0.0
2017-12-18 21:00:00+00:00 176.420 0.000191 0.000540 0.000076 0.452163 0.121628 0.000588 0.0 9987753.7 17642.0 ... 162.484790 0 0 1.048446 9987753.7 17387.0 17387.0 999 [] 0.0
2017-12-19 21:00:00+00:00 174.540 0.000192 0.000521 0.000072 0.446586 0.121586 0.000589 0.0 9987753.7 17454.0 ... 162.741040 0 0 1.008761 9987753.7 17642.0 17642.0 1000 [] 0.0
2017-12-20 21:00:00+00:00 174.350 0.000191 0.000519 0.000072 0.445828 0.121526 0.000589 0.0 9987753.7 17435.0 ... 163.001860 0 0 1.004553 9987753.7 17454.0 17454.0 1001 [] 0.0
2017-12-21 21:00:00+00:00 175.010 0.000191 0.000525 0.000073 0.448806 0.121468 0.000590 0.0 9987753.7 17501.0 ... 163.257330 0 0 1.016818 9987753.7 17435.0 17435.0 1002 [] 0.0
2017-12-22 21:00:00+00:00 175.010 0.000191 0.000525 0.000073 0.448427 0.121408 0.000590 0.0 9987753.7 17501.0 ... 163.442180 0 0 1.016311 9987753.7 17501.0 17501.0 1003 [] 0.0
2017-12-26 21:00:00+00:00 170.570 0.000193 0.000481 0.000062 0.446694 0.121350 0.000591 0.0 9987753.7 17057.0 ... 163.598270 0 0 0.916663 9987753.7 17501.0 17501.0 1004 [] 0.0
2017-12-27 21:00:00+00:00 170.600 0.000192 0.000481 0.000062 0.447398 0.121290 0.000591 0.0 9987753.7 17060.0 ... 163.746493 0 0 0.916778 9987753.7 17057.0 17057.0 1005 [] 0.0
2017-12-28 21:00:00+00:00 171.080 0.000192 0.000486 0.000062 0.450376 0.121232 0.000591 0.0 9987753.7 17108.0 ... 163.899510 0 0 0.925456 9987753.7 17060.0 17060.0 1006 [] 0.0
2017-12-29 21:00:00+00:00 169.230 0.000193 0.000468 0.000058 0.444908 0.121190 0.000592 0.0 9987753.7 16923.0 ... 163.997270 0 0 0.887619 9987753.7 17108.0 17108.0 1007 [] 0.0

1007 rows × 40 columns

Here we are explicitly defining an analyze() function that gets automatically called once the backtest is done (this is not possible on Quantopian currently).

Although it might not be directly apparent, the power of history (pun intended) can not be under-estimated as most algorithms make use of prior market developments in one form or another. You could easily devise a strategy that trains a classifier with scikit-learn which tries to predict future market movements based on past prices (note, that most of the scikit-learn functions require numpy.ndarrays rather than pandas.DataFrames, so you can simply pass the underlying ndarray of a DataFrame via .values).

We also used the order_target() function above. This and other functions like it can make order management and portfolio rebalancing much easier. See the Quantopian documentation on order functions fore more details.

Conclusions

We hope that this tutorial gave you a little insight into the architecture, API, and features of zipline. For next steps, check out some of the examples.

Feel free to ask questions on our mailing list, report problems on our GitHub issue tracker, get involved, and checkout Quantopian.