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


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


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.

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 [2]:
!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: quantopian-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: -]
  --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 parameters specifying which stock data to load from Yahoo! finance and 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. Note that you can also define a configuration file with these parameters that you can then conveniently pass to the -c option so that you don't have to supply the command line args all the time (see the .conf files in the examples directory).

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


In [3]:
!zipline run -f ../../zipline/examples/buyapple.py --start 2000-1-1 --end 2014-1-1 -o buyapple_out.pickle


[2017-04-24 18:18:46.912285] INFO: Performance: after split: sid: Equity(0 [AAPL]), amount: 2340.0, cost_basis: 55.65, last_sale_price: 101.25
[2017-04-24 18:18:46.912451] INFO: Performance: returning cash: 0.0
[2017-04-24 18:18:54.383697] INFO: Performance: after split: sid: Equity(0 [AAPL]), amount: 28220.0, cost_basis: 15.77, last_sale_price: 88.99
[2017-04-24 18:18:54.383934] INFO: Performance: returning cash: 0.0
[2017-04-24 18:19:08.603957] INFO: Performance: Simulated 3521 trading days out of 3521.
[2017-04-24 18:19:08.604134] INFO: Performance: first open: 2000-01-03 14:31:00+00:00
[2017-04-24 18:19:08.604221] INFO: Performance: last close: 2013-12-31 21:00:00+00:00

run_algo.py first outputs the algorithm contents. It then fetches historical price and volume data of Apple from Yahoo! finance 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 [4]:
import pandas as pd
perf = pd.read_pickle('buyapple_out.pickle') # read in perf DataFrame
perf.head()


Out[4]:
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
2000-01-03 21:00:00+00:00 111.94 NaN 0.000000e+00 NaN -0.009549 NaN NaN 0.0 10000000.0 0.0 ... 0 0 0 NaN 10000000.0 0.0 0.0 1 [] 0.0658
2000-01-04 21:00:00+00:00 102.50 0.000001 -1.000000e-07 0.000008 -0.047528 0.323229 0.000003 -1026.0 9998974.0 1025.0 ... 0 0 0 -11.224972 10000000.0 0.0 0.0 2 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0649
2000-01-05 21:00:00+00:00 104.00 0.000013 1.300000e-06 0.000228 -0.045697 0.329321 0.000031 -1041.0 9997933.0 2080.0 ... 0 0 0 119.146981 9998974.0 1025.0 1025.0 3 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0662
2000-01-06 21:00:00+00:00 95.00 0.000148 -1.680000e-05 -0.001593 -0.044785 0.298325 -0.000189 -951.0 9996982.0 2850.0 ... 0 0 0 -7.367062 9997933.0 2080.0 2080.0 4 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0657
2000-01-07 21:00:00+00:00 99.50 0.000179 -3.400000e-06 -0.000034 -0.018908 0.375301 0.000152 -996.0 9995986.0 3980.0 ... 0 0 0 -1.333453 9996982.0 2850.0 2850.0 5 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0652

5 rows × 39 columns

As you can see, there is a row for each trading day, starting on the first business day of 2000. 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 [5]:
%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[5]:
<matplotlib.text.Text at 0x10c793278>

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 [6]:
%load_ext zipline

In [7]:
%%zipline --start 2000-1-1 --end 2014-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[7]:
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
2000-01-03 21:00:00+00:00 111.940 NaN 0.000000e+00 NaN -0.009549 NaN NaN 0.00 10000000.00 0.00 ... 0 0 0 NaN 10000000.00 0.00 0.00 1 [] 0.0658
2000-01-04 21:00:00+00:00 102.500 0.000001 -1.000000e-07 0.000008 -0.047528 0.323229 0.000003 -1026.00 9998974.00 1025.00 ... 0 0 0 -11.224972 10000000.00 0.00 0.00 2 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0649
2000-01-05 21:00:00+00:00 104.000 0.000013 1.300000e-06 0.000228 -0.045697 0.329321 0.000031 -1041.00 9997933.00 2080.00 ... 0 0 0 119.146981 9998974.00 1025.00 1025.00 3 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0662
2000-01-06 21:00:00+00:00 95.000 0.000148 -1.680000e-05 -0.001593 -0.044785 0.298325 -0.000189 -951.00 9996982.00 2850.00 ... 0 0 0 -7.367062 9997933.00 2080.00 2080.00 4 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0657
2000-01-07 21:00:00+00:00 99.500 0.000179 -3.400000e-06 -0.000034 -0.018908 0.375301 0.000152 -996.00 9995986.00 3980.00 ... 0 0 0 -1.333453 9996982.00 2850.00 2850.00 5 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0652
2000-01-10 21:00:00+00:00 97.750 0.000165 -1.050000e-05 -0.000410 -0.007929 0.349070 0.000108 -978.50 9995007.50 4887.50 ... 0 0 0 -3.499789 9995986.00 3980.00 3980.00 6 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0657
2000-01-11 21:00:00+00:00 92.750 0.000206 -3.560000e-05 -0.001141 -0.020888 0.326609 0.000197 -928.50 9994079.00 5565.00 ... 0 0 0 -6.727599 9995007.50 4887.50 4887.50 7 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0667
2000-01-12 21:00:00+00:00 87.190 0.000248 -6.906000e-05 -0.002014 -0.025183 0.302508 0.000212 -872.90 9993206.10 6103.30 ... 0 0 0 -8.402993 9994079.00 5565.00 5565.00 8 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0672
2000-01-13 21:00:00+00:00 96.750 0.000462 -2.240000e-06 0.000128 -0.013320 0.294168 0.000567 -968.50 9992237.60 7740.00 ... 0 0 0 -0.256575 9993206.10 6103.30 6103.30 9 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0663
2000-01-14 21:00:00+00:00 100.440 0.000460 2.718000e-05 0.000707 -0.002791 0.283818 0.000653 -1005.40 9991232.20 9039.60 ... 0 0 0 2.958348 9992237.60 7740.00 7740.00 10 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-18 21:00:00+00:00 103.940 0.000458 5.858000e-05 0.001451 -0.009604 0.271155 0.000584 -1040.40 9990191.80 10394.00 ... 0 0 0 6.078787 9991232.20 9039.60 9039.60 11 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0675
2000-01-19 21:00:00+00:00 106.560 0.000447 8.468000e-05 0.001873 -0.009086 0.258601 0.000592 -1066.60 9989125.20 11721.60 ... 0 0 0 8.412796 9990191.80 10394.00 10394.00 12 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0673
2000-01-20 21:00:00+00:00 113.500 0.000525 1.609200e-04 0.003246 -0.016117 0.249219 0.000445 -1136.00 9987989.20 13620.00 ... 0 0 0 15.359196 9989125.20 11721.60 11721.60 13 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0679
2000-01-21 21:00:00+00:00 111.310 0.000531 1.345400e-04 0.002570 -0.018982 0.239561 0.000466 -1114.10 9986875.10 14470.30 ... 0 0 0 10.742342 9987989.20 13620.00 13620.00 14 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0679
2000-01-24 21:00:00+00:00 106.250 0.000598 6.866000e-05 0.001836 -0.046092 0.254900 0.000897 -1063.50 9985811.60 14875.00 ... 0 0 0 3.325663 9986875.10 14470.30 14470.30 15 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-25 21:00:00+00:00 112.250 0.000658 1.525600e-04 0.003059 -0.040306 0.248882 0.001061 -1123.50 9984688.10 16837.50 ... 0 0 0 7.154141 9985811.60 14875.00 14875.00 16 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0670
2000-01-26 21:00:00+00:00 110.190 0.000656 1.215600e-04 0.002497 -0.044349 0.241075 0.001079 -1102.90 9983585.20 17630.40 ... 0 0 0 5.193134 9984688.10 16837.50 16837.50 17 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-27 21:00:00+00:00 110.000 0.000637 1.184200e-04 0.002376 -0.048113 0.233934 0.001082 -1101.00 9982484.20 18700.00 ... 0 0 0 4.913498 9983585.20 17630.40 17630.40 18 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0668
2000-01-28 21:00:00+00:00 101.620 0.000824 -2.414000e-05 0.001421 -0.074249 0.244660 0.001754 -1017.20 9981467.00 18291.60 ... 0 0 0 -0.520687 9982484.20 18700.00 18700.00 19 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0666
2000-01-31 21:00:00+00:00 103.750 0.000814 1.410000e-05 0.001235 -0.050904 0.259652 0.001691 -1038.50 9980428.50 19712.50 ... 0 0 0 0.297247 9981467.00 18291.60 18291.60 20 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0668
2000-02-01 21:00:00+00:00 100.250 0.000827 -5.250000e-05 0.000062 -0.040817 0.257119 0.001478 -1003.50 9979425.00 20050.00 ... 0 0 0 -1.002362 9980428.50 19712.50 19712.50 21 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0662
2000-02-02 21:00:00+00:00 98.810 0.000812 -8.140000e-05 -0.000274 -0.040926 0.250992 0.001469 -989.10 9978435.90 20750.10 ... 0 0 0 -1.499773 9979425.00 20050.00 20050.00 22 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0660
2000-02-03 21:00:00+00:00 103.310 0.000857 1.300000e-05 0.000647 -0.030138 0.248983 0.001651 -1034.10 9977401.80 22728.20 ... 0 0 0 0.234903 9978435.90 20750.10 20750.10 23 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0649
2000-02-04 21:00:00+00:00 108.000 0.000902 1.160800e-04 0.001713 -0.030546 0.243523 0.001665 -1081.00 9976320.80 24840.00 ... 0 0 0 2.048842 9977401.80 22728.20 22728.20 24 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0653
2000-02-07 21:00:00+00:00 114.060 0.000980 2.553600e-04 0.003058 -0.030635 0.238421 0.001690 -1141.60 9975179.20 27374.40 ... 0 0 0 4.415192 9976320.80 24840.00 24840.00 25 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0664
2000-02-08 21:00:00+00:00 114.870 0.000961 2.747000e-04 0.002921 -0.018737 0.237304 0.001659 -1149.70 9974029.50 28717.50 ... 0 0 0 4.657246 9975179.20 27374.40 27374.40 26 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0659
2000-02-09 21:00:00+00:00 112.620 0.000964 2.183500e-04 0.002647 -0.039163 0.240737 0.001768 -1127.20 9972902.30 29281.20 ... 0 0 0 3.473224 9974029.50 28717.50 28717.50 27 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0656
2000-02-10 21:00:00+00:00 113.500 0.000947 2.411300e-04 0.002701 -0.035678 0.236711 0.001772 -1136.00 9971766.30 30645.00 ... 0 0 0 3.766345 9972902.30 29281.20 29281.20 28 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0667
2000-02-11 21:00:00+00:00 108.750 0.001014 1.127800e-04 0.001960 -0.055899 0.239648 0.002077 -1088.50 9970677.80 30450.00 ... 0 0 0 1.439843 9971766.30 30645.00 30645.00 29 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0663
2000-02-14 21:00:00+00:00 115.810 0.001144 3.103600e-04 0.003567 -0.053980 0.235752 0.002187 -1159.10 9969518.70 33584.90 ... 0 0 0 3.894316 9970677.80 30450.00 30450.00 30 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0656
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2013-11-18 21:00:00+00:00 518.629 0.133219 2.021722e+00 0.078743 0.219350 0.209347 0.274972 -5187.29 4176861.52 26040362.09 ... 0 0 0 0.958926 4182048.81 26354548.20 26354548.20 3492 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0267
2013-11-19 21:00:00+00:00 519.549 0.133200 2.026342e+00 0.078872 0.216859 0.209318 0.274965 -5196.49 4171665.03 26091750.78 ... 0 0 0 0.959981 4176861.52 26040362.09 26040362.09 3493 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0271
2013-11-20 21:00:00+00:00 515.000 0.133198 2.003496e+00 0.078376 0.212435 0.209291 0.275008 -5151.00 4166514.03 25868450.00 ... 0 0 0 0.953727 4171665.03 26091750.78 26091750.78 3494 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0280
2013-11-21 21:00:00+00:00 521.135 0.133205 2.034312e+00 0.078928 0.222290 0.209272 0.275109 -5212.35 4161301.68 26181822.40 ... 0 0 0 0.961589 4166514.03 25868450.00 25868450.00 3495 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0279
2013-11-22 21:00:00+00:00 519.799 0.133188 2.027600e+00 0.078649 0.228355 0.209246 0.275078 -5198.99 4156102.69 26119899.75 ... 0 0 0 0.959708 4161301.68 26181822.40 26181822.40 3496 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0275
2013-11-25 21:00:00+00:00 523.740 0.133180 2.047404e+00 0.079123 0.226803 0.209216 0.275063 -5238.40 4150864.29 26323172.40 ... 0 0 0 0.964668 4156102.69 26119899.75 26119899.75 3497 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0274
2013-11-26 21:00:00+00:00 533.400 0.133226 2.095955e+00 0.080245 0.226987 0.209186 0.275063 -5335.00 4145529.29 26814018.00 ... 0 0 0 0.976945 4150864.29 26323172.40 26323172.40 3498 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0271
2013-11-27 21:00:00+00:00 545.960 0.133316 2.159094e+00 0.081639 0.230036 0.209157 0.275138 -5460.60 4140068.69 27450868.80 ... 0 0 0 0.992695 4145529.29 26814018.00 26814018.00 3499 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0274
2013-11-29 18:00:00+00:00 556.070 0.133363 2.209927e+00 0.082791 0.229069 0.209128 0.275114 -5561.70 4134506.99 27964760.30 ... 0 0 0 1.005088 4140068.69 27450868.80 27450868.80 3500 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0275
2013-12-02 21:00:00+00:00 551.230 0.133361 2.185586e+00 0.082274 0.225727 0.209099 0.275147 -5513.30 4128993.69 27726869.00 ... 0 0 0 0.998796 4134506.99 27964760.30 27964760.30 3501 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0281
2013-12-03 21:00:00+00:00 566.322 0.133491 2.261499e+00 0.084033 0.221814 0.209071 0.275013 -5664.22 4123329.47 28491659.82 ... 0 0 0 1.017208 4128993.69 27726869.00 27726869.00 3502 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0279
2013-12-04 21:00:00+00:00 565.000 0.133473 2.254848e+00 0.083888 0.220221 0.209042 0.275018 -5651.00 4117678.47 28430800.00 ... 0 0 0 1.015458 4123329.47 28491659.82 28491659.82 3503 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0284
2013-12-05 21:00:00+00:00 567.901 0.133459 2.269446e+00 0.084274 0.214926 0.209015 0.274979 -5680.01 4111998.46 28582457.33 ... 0 0 0 1.018804 4117678.47 28430800.00 28430800.00 3504 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-06 21:00:00+00:00 560.020 0.133482 2.229780e+00 0.083166 0.228579 0.209007 0.274695 -5601.20 4106397.26 28191406.80 ... 0 0 0 1.008591 4111998.46 28582457.33 28582457.33 3505 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-09 21:00:00+00:00 566.430 0.133488 2.262048e+00 0.083824 0.230812 0.208977 0.274720 -5665.30 4100731.96 28519750.50 ... 0 0 0 1.016217 4106397.26 28191406.80 28191406.80 3506 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0286
2013-12-10 21:00:00+00:00 565.550 0.133469 2.257617e+00 0.083765 0.226898 0.208949 0.274724 -5656.50 4095075.46 28481098.00 ... 0 0 0 1.015008 4100731.96 28519750.50 28519750.50 3507 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0281
2013-12-11 21:00:00+00:00 561.360 0.133463 2.236516e+00 0.083496 0.213013 0.208942 0.274794 -5614.60 4089460.86 28275703.20 ... 0 0 0 1.009649 4095075.46 28481098.00 28481098.00 3508 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0286
2013-12-12 21:00:00+00:00 560.540 0.133445 2.232386e+00 0.083455 0.208440 0.208915 0.274798 -5606.40 4083854.46 28240005.20 ... 0 0 0 1.008507 4089460.86 28275703.20 28275703.20 3509 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-13 21:00:00+00:00 554.430 0.133452 2.201604e+00 0.082750 0.208317 0.208885 0.274802 -5545.30 4078309.16 27937727.70 ... 0 0 0 1.000581 4083854.46 28240005.20 28240005.20 3510 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-16 21:00:00+00:00 557.500 0.133438 2.217073e+00 0.082947 0.215954 0.208862 0.274830 -5576.00 4072733.16 28098000.00 ... 0 0 0 1.004191 4078309.16 27937727.70 27937727.70 3511 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-17 21:00:00+00:00 554.990 0.133424 2.204423e+00 0.082702 0.212183 0.208834 0.274848 -5550.90 4067182.26 27977045.90 ... 0 0 0 1.000930 4072733.16 28098000.00 28098000.00 3512 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0285
2013-12-18 21:00:00+00:00 550.770 0.133419 2.183150e+00 0.081886 0.232363 0.208851 0.274535 -5508.70 4061673.56 27769823.40 ... 0 0 0 0.995449 4067182.26 27977045.90 27977045.90 3513 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-19 21:00:00+00:00 544.460 0.133428 2.151335e+00 0.081156 0.231649 0.208822 0.274547 -5445.60 4056227.96 27457117.80 ... 0 0 0 0.987136 4061673.56 27769823.40 27769823.40 3514 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0294
2013-12-20 21:00:00+00:00 549.020 0.133422 2.174331e+00 0.081560 0.237584 0.208796 0.274590 -5491.20 4050736.76 27692568.80 ... 0 0 0 0.992656 4056227.96 27457117.80 27457117.80 3515 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-23 21:00:00+00:00 570.090 0.133698 2.280608e+00 0.083822 0.244165 0.208771 0.274860 -5701.90 4045034.86 28761040.50 ... 0 0 0 1.018482 4050736.76 27692568.80 27692568.80 3516 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0294
2013-12-24 18:00:00+00:00 567.670 0.133683 2.268399e+00 0.083475 0.247793 0.208742 0.274838 -5677.70 4039357.16 28644628.20 ... 0 0 0 1.015392 4045034.86 28761040.50 28761040.50 3517 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0299
2013-12-26 21:00:00+00:00 563.900 0.133674 2.249375e+00 0.082943 0.253714 0.208716 0.274781 -5640.00 4033717.16 28460033.00 ... 0 0 0 1.010591 4039357.16 28644628.20 28644628.20 3518 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0300
2013-12-27 21:00:00+00:00 560.090 0.133666 2.230146e+00 0.082502 0.253293 0.208686 0.274786 -5601.90 4028115.26 28273343.20 ... 0 0 0 1.005713 4033717.16 28460033.00 28460033.00 3519 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0302
2013-12-30 21:00:00+00:00 554.520 0.133669 2.202028e+00 0.081858 0.253068 0.208657 0.274791 -5546.20 4022569.06 27997714.80 ... 0 0 0 0.998507 4028115.26 28273343.20 28273343.20 3520 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0299
2013-12-31 21:00:00+00:00 561.020 0.133676 2.234847e+00 0.082489 0.258030 0.208630 0.274846 -5611.20 4016957.86 28331510.00 ... 0 0 0 1.006304 4022569.06 27997714.80 27997714.80 3521 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0304

3521 rows × 39 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 [8]:
pd.read_pickle('perf_ipython.pickle').head()


Out[8]:
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
2000-01-03 21:00:00+00:00 111.94 NaN 0.000000e+00 NaN -0.009549 NaN NaN 0.0 10000000.0 0.0 ... 0 0 0 NaN 10000000.0 0.0 0.0 1 [] 0.0658
2000-01-04 21:00:00+00:00 102.50 0.000001 -1.000000e-07 0.000008 -0.047528 0.323229 0.000003 -1026.0 9998974.0 1025.0 ... 0 0 0 -11.224972 10000000.0 0.0 0.0 2 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0649
2000-01-05 21:00:00+00:00 104.00 0.000013 1.300000e-06 0.000228 -0.045697 0.329321 0.000031 -1041.0 9997933.0 2080.0 ... 0 0 0 119.146981 9998974.0 1025.0 1025.0 3 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0662
2000-01-06 21:00:00+00:00 95.00 0.000148 -1.680000e-05 -0.001593 -0.044785 0.298325 -0.000189 -951.0 9996982.0 2850.0 ... 0 0 0 -7.367062 9997933.0 2080.0 2080.0 4 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0657
2000-01-07 21:00:00+00:00 99.50 0.000179 -3.400000e-06 -0.000034 -0.018908 0.375301 0.000152 -996.0 9995986.0 3980.0 ... 0 0 0 -1.333453 9996982.0 2850.0 2850.0 5 [{'sid': Equity(0 [AAPL]), 'amount': 10, 'dt':... 0.0652

5 rows × 39 columns

Using Custom Bundles (Advanced)

If you want to use your own custom data bundles using yahoo finance data, you'll first need to find where your zipline root directory is.


In [9]:
from zipline.utils.paths import zipline_root

root = zipline_root()

Once we've found where our root directory is, we'll want to add a file called extension.py to the zipline root directory, which is where we will register our custom yahoo bundle. We'll edit that file with the following code:


In [10]:
ext_path = os.path.join(root, "extension.py")

In [11]:
%%writefile {ext_path}

from zipline.data.bundles import register, yahoo_equities

equities = (
    'AAPL',
    'IBM',
    'MSFT',
)

register(
    'my-bundle', # you can use any name you want                                                                                                                                    
    yahoo_equities(equities),
)


Overwriting /Users/freddiev4/.zipline/extension.py

Now we'll check that our bundle was created by running zipline bundles, and then ingest our bundle for usage using zipline ingest.


In [12]:
! zipline bundles


my-bundle 2017-04-24 18:03:10.059364
quandl <no ingestions>
quantopian-quandl 2017-04-24 18:05:37.111982
quantopian-quandl 2017-04-24 16:45:55.425033

In [13]:
! zipline ingest -b my-bundle


Merging daily equity files:  [---#--------------------------------]  20%             
Merging daily equity files:  [####################################]   
Downloading Yahoo adjustment data:   [####################################]  100%

And now we can re-run the code we wrote above using our custom yahoo bundle.


In [14]:
%%zipline --bundle my-bundle --start 2000-1-1 --end 2014-1-1 -o custom_bundle

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[14]:
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
2000-01-03 21:00:00+00:00 111.937 NaN 0.000000e+00 NaN -0.009549 NaN NaN 0.00 10000000.00 0.00 ... 0 0 0 NaN 10000000.00 0.00 0.00 1 [] 0.0658
2000-01-04 21:00:00+00:00 102.500 0.000001 -1.000000e-07 0.000008 -0.047528 0.323229 0.000003 -1026.00 9998974.00 1025.00 ... 0 0 0 -11.224972 10000000.00 0.00 0.00 2 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0649
2000-01-05 21:00:00+00:00 103.999 0.000013 1.299000e-06 0.000228 -0.045697 0.329321 0.000031 -1040.99 9997933.01 2079.98 ... 0 0 0 119.055329 9998974.00 1025.00 1025.00 3 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0662
2000-01-06 21:00:00+00:00 94.999 0.000148 -1.680100e-05 -0.001593 -0.044785 0.298325 -0.000189 -950.99 9996982.02 2849.97 ... 0 0 0 -7.367500 9997933.01 2079.98 2079.98 4 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0657
2000-01-07 21:00:00+00:00 99.500 0.000179 -3.398000e-06 -0.000034 -0.018908 0.375301 0.000152 -996.00 9995986.02 3980.00 ... 0 0 0 -1.332669 9996982.02 2849.97 2849.97 5 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0652
2000-01-10 21:00:00+00:00 97.750 0.000165 -1.049800e-05 -0.000410 -0.007929 0.349070 0.000108 -978.50 9995007.52 4887.50 ... 0 0 0 -3.499122 9995986.02 3980.00 3980.00 6 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0657
2000-01-11 21:00:00+00:00 92.750 0.000206 -3.559800e-05 -0.001141 -0.020888 0.326609 0.000197 -928.50 9994079.02 5565.00 ... 0 0 0 -6.727221 9995007.52 4887.50 4887.50 7 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0667
2000-01-12 21:00:00+00:00 87.187 0.000248 -6.907600e-05 -0.002014 -0.025183 0.302508 0.000212 -872.87 9993206.15 6103.09 ... 0 0 0 -8.402560 9994079.02 5565.00 5565.00 8 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0672
2000-01-13 21:00:00+00:00 96.749 0.000462 -2.242000e-06 0.000128 -0.013320 0.294168 0.000567 -968.49 9992237.66 7739.92 ... 0 0 0 -0.256732 9993206.15 6103.09 6103.09 9 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0663
2000-01-14 21:00:00+00:00 100.437 0.000460 2.716200e-05 0.000707 -0.002791 0.283818 0.000653 -1005.37 9991232.29 9039.33 ... 0 0 0 2.955552 9992237.66 7739.92 7739.92 10 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-18 21:00:00+00:00 103.937 0.000458 5.856200e-05 0.001451 -0.009604 0.271155 0.000584 -1040.37 9990191.92 10393.70 ... 0 0 0 6.075200 9991232.29 9039.33 9039.33 11 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0675
2000-01-19 21:00:00+00:00 106.562 0.000447 8.471200e-05 0.001874 -0.009086 0.258601 0.000592 -1066.62 9989125.30 11721.82 ... 0 0 0 8.413593 9990191.92 10393.70 10393.70 12 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0673
2000-01-20 21:00:00+00:00 113.499 0.000525 1.609190e-04 0.003246 -0.016117 0.249219 0.000445 -1135.99 9987989.31 13619.88 ... 0 0 0 15.354753 9989125.30 11721.82 11721.82 13 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0679
2000-01-21 21:00:00+00:00 111.312 0.000531 1.345750e-04 0.002570 -0.018982 0.239561 0.000466 -1114.12 9986875.19 14470.56 ... 0 0 0 10.746454 9987989.31 13619.88 13619.88 14 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0679
2000-01-24 21:00:00+00:00 106.250 0.000598 6.866900e-05 0.001836 -0.046092 0.254900 0.000897 -1063.50 9985811.69 14875.00 ... 0 0 0 3.325465 9986875.19 14470.56 14470.56 15 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-25 21:00:00+00:00 112.250 0.000658 1.525690e-04 0.003059 -0.040306 0.248882 0.001062 -1123.50 9984688.19 16837.50 ... 0 0 0 7.153199 9985811.69 14875.00 14875.00 16 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0670
2000-01-26 21:00:00+00:00 110.187 0.000656 1.215240e-04 0.002496 -0.044349 0.241075 0.001079 -1102.87 9983585.32 17629.92 ... 0 0 0 5.189832 9984688.19 16837.50 16837.50 17 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0669
2000-01-27 21:00:00+00:00 110.000 0.000637 1.184320e-04 0.002376 -0.048113 0.233934 0.001082 -1101.00 9982484.32 18700.00 ... 0 0 0 4.912418 9983585.32 17629.92 17629.92 18 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0668
2000-01-28 21:00:00+00:00 101.625 0.000823 -2.404300e-05 0.001422 -0.074249 0.244660 0.001754 -1017.25 9981467.07 18292.50 ... 0 0 0 -0.518767 9982484.32 18700.00 18700.00 19 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0666
2000-01-31 21:00:00+00:00 103.749 0.000814 1.408900e-05 0.001234 -0.050904 0.259652 0.001690 -1038.49 9980428.58 19712.31 ... 0 0 0 0.297114 9981467.07 18292.50 18292.50 20 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0668
2000-02-01 21:00:00+00:00 100.249 0.000827 -5.251100e-05 0.000061 -0.040817 0.257119 0.001477 -1003.49 9979425.09 20049.80 ... 0 0 0 -1.002862 9980428.58 19712.31 19712.31 21 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0662
2000-02-02 21:00:00+00:00 98.812 0.000812 -8.135100e-05 -0.000274 -0.040926 0.250992 0.001468 -989.12 9978435.97 20750.52 ... 0 0 0 -1.499370 9979425.09 20049.80 20049.80 22 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0660
2000-02-03 21:00:00+00:00 103.312 0.000857 1.304900e-05 0.000647 -0.030138 0.248983 0.001650 -1034.12 9977401.85 22728.64 ... 0 0 0 0.235865 9978435.97 20750.52 20750.52 23 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0649
2000-02-04 21:00:00+00:00 108.000 0.000901 1.160850e-04 0.001713 -0.030546 0.243523 0.001664 -1081.00 9976320.85 24840.00 ... 0 0 0 2.049613 9977401.85 22728.64 22728.64 24 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0653
2000-02-07 21:00:00+00:00 114.062 0.000980 2.554110e-04 0.003058 -0.030635 0.238421 0.001690 -1141.62 9975179.23 27374.88 ... 0 0 0 4.417545 9976320.85 24840.00 24840.00 25 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0664
2000-02-08 21:00:00+00:00 114.875 0.000961 2.748230e-04 0.002923 -0.018737 0.237304 0.001658 -1149.75 9974029.48 28718.75 ... 0 0 0 4.660883 9975179.23 27374.88 27374.88 26 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0659
2000-02-09 21:00:00+00:00 112.624 0.000964 2.184480e-04 0.002648 -0.039163 0.240737 0.001767 -1127.24 9972902.24 29282.24 ... 0 0 0 3.475708 9974029.48 28718.75 28718.75 27 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0656
2000-02-10 21:00:00+00:00 113.499 0.000947 2.410980e-04 0.002701 -0.035678 0.236711 0.001772 -1135.99 9971766.25 30644.73 ... 0 0 0 3.766848 9972902.24 29282.24 29282.24 28 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0667
2000-02-11 21:00:00+00:00 108.749 0.001014 1.127480e-04 0.001959 -0.055899 0.239648 0.002077 -1088.49 9970677.76 30449.72 ... 0 0 0 1.439700 9971766.25 30644.73 30644.73 29 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0663
2000-02-14 21:00:00+00:00 115.812 0.001144 3.104120e-04 0.003567 -0.053980 0.235752 0.002186 -1159.12 9969518.64 33585.48 ... 0 0 0 3.895685 9970677.76 30449.72 30449.72 30 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0656
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2013-11-18 21:00:00+00:00 518.629 0.133217 2.021724e+00 0.078742 0.219350 0.209347 0.275010 -5187.29 4176875.94 26040362.09 ... 0 0 0 0.958944 4182063.23 26354498.00 26354498.00 3492 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0267
2013-11-19 21:00:00+00:00 519.549 0.133199 2.026343e+00 0.078870 0.216859 0.209318 0.275003 -5196.49 4171679.45 26091750.78 ... 0 0 0 0.959999 4176875.94 26040362.09 26040362.09 3493 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0271
2013-11-20 21:00:00+00:00 514.999 0.133196 2.003493e+00 0.078374 0.212435 0.209291 0.275046 -5150.99 4166528.46 25868399.77 ... 0 0 0 0.953744 4171679.45 26091750.78 26091750.78 3494 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0280
2013-11-21 21:00:00+00:00 521.139 0.133204 2.034334e+00 0.078927 0.222290 0.209272 0.275147 -5212.39 4161316.07 26182023.36 ... 0 0 0 0.961613 4166528.46 25868399.77 25868399.77 3495 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0279
2013-11-22 21:00:00+00:00 519.799 0.133187 2.027602e+00 0.078647 0.228355 0.209246 0.275116 -5198.99 4156117.08 26119899.75 ... 0 0 0 0.959726 4161316.07 26182023.36 26182023.36 3496 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0275
2013-11-25 21:00:00+00:00 523.739 0.133178 2.047400e+00 0.079122 0.226803 0.209216 0.275101 -5238.39 4150878.69 26323122.14 ... 0 0 0 0.964685 4156117.08 26119899.75 26119899.75 3497 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0274
2013-11-26 21:00:00+00:00 533.399 0.133225 2.095951e+00 0.080244 0.226987 0.209186 0.275101 -5334.99 4145543.70 26813967.73 ... 0 0 0 0.976963 4150878.69 26323122.14 26323122.14 3498 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0271
2013-11-27 21:00:00+00:00 545.959 0.133314 2.159090e+00 0.081638 0.230036 0.209157 0.275176 -5460.59 4140083.11 27450818.52 ... 0 0 0 0.992713 4145543.70 26813967.73 26813967.73 3499 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0274
2013-11-29 18:00:00+00:00 556.069 0.133362 2.209923e+00 0.082789 0.229069 0.209128 0.275152 -5561.69 4134521.42 27964710.01 ... 0 0 0 1.005107 4140083.11 27450818.52 27450818.52 3500 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0275
2013-12-02 21:00:00+00:00 551.230 0.133360 2.185588e+00 0.082273 0.225727 0.209099 0.275185 -5513.30 4129008.12 27726869.00 ... 0 0 0 0.998816 4134521.42 27964710.01 27964710.01 3501 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0281
2013-12-03 21:00:00+00:00 566.319 0.133489 2.261485e+00 0.084032 0.221814 0.209071 0.275051 -5664.19 4123343.93 28491508.89 ... 0 0 0 1.017224 4129008.12 27726869.00 27726869.00 3502 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0279
2013-12-04 21:00:00+00:00 565.000 0.133472 2.254849e+00 0.083887 0.220221 0.209042 0.275056 -5651.00 4117692.93 28430800.00 ... 0 0 0 1.015477 4123343.93 28491508.89 28491508.89 3503 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0284
2013-12-05 21:00:00+00:00 567.899 0.133457 2.269437e+00 0.084272 0.214926 0.209015 0.275017 -5679.99 4112012.94 28582356.67 ... 0 0 0 1.018821 4117692.93 28430800.00 28430800.00 3504 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-06 21:00:00+00:00 560.019 0.133480 2.229777e+00 0.083164 0.228579 0.209007 0.274733 -5601.19 4106411.75 28191356.46 ... 0 0 0 1.008610 4112012.94 28582356.67 28582356.67 3505 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-09 21:00:00+00:00 566.430 0.133486 2.262050e+00 0.083822 0.230812 0.208977 0.274758 -5665.30 4100746.45 28519750.50 ... 0 0 0 1.016237 4106411.75 28191356.46 28191356.46 3506 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0286
2013-12-10 21:00:00+00:00 565.549 0.133468 2.257614e+00 0.083763 0.226898 0.208949 0.274762 -5656.49 4095089.96 28481047.64 ... 0 0 0 1.015026 4100746.45 28519750.50 28519750.50 3507 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0281
2013-12-11 21:00:00+00:00 561.359 0.133461 2.236513e+00 0.083495 0.213013 0.208942 0.274832 -5614.59 4089475.37 28275652.83 ... 0 0 0 1.009668 4095089.96 28481047.64 28481047.64 3508 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0286
2013-12-12 21:00:00+00:00 560.539 0.133443 2.232382e+00 0.083454 0.208440 0.208915 0.274836 -5606.39 4083868.98 28239954.82 ... 0 0 0 1.008525 4089475.37 28275652.83 28275652.83 3509 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-13 21:00:00+00:00 554.429 0.133450 2.201600e+00 0.082748 0.208317 0.208885 0.274840 -5545.29 4078323.69 27937677.31 ... 0 0 0 1.000599 4083868.98 28239954.82 28239954.82 3510 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0288
2013-12-16 21:00:00+00:00 557.500 0.133437 2.217075e+00 0.082946 0.215954 0.208862 0.274868 -5576.00 4072747.69 28098000.00 ... 0 0 0 1.004211 4078323.69 27937677.31 27937677.31 3511 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-17 21:00:00+00:00 554.990 0.133423 2.204424e+00 0.082700 0.212183 0.208834 0.274886 -5550.90 4067196.79 27977045.90 ... 0 0 0 1.000949 4072747.69 28098000.00 28098000.00 3512 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0285
2013-12-18 21:00:00+00:00 550.769 0.133417 2.183146e+00 0.081884 0.232363 0.208851 0.274573 -5508.69 4061688.10 27769772.98 ... 0 0 0 0.995467 4067196.79 27977045.90 27977045.90 3513 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-19 21:00:00+00:00 544.459 0.133427 2.151331e+00 0.081155 0.231649 0.208822 0.274585 -5445.59 4056242.51 27457067.37 ... 0 0 0 0.987153 4061688.10 27769772.98 27769772.98 3514 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0294
2013-12-20 21:00:00+00:00 549.019 0.133421 2.174327e+00 0.081558 0.237584 0.208796 0.274628 -5491.19 4050751.32 27692518.36 ... 0 0 0 0.992674 4056242.51 27457067.37 27457067.37 3515 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0289
2013-12-23 21:00:00+00:00 570.090 0.133696 2.280609e+00 0.083820 0.244165 0.208771 0.274898 -5701.90 4045049.42 28761040.50 ... 0 0 0 1.018501 4050751.32 27692518.36 27692518.36 3516 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0294
2013-12-24 18:00:00+00:00 567.669 0.133682 2.268395e+00 0.083473 0.247793 0.208742 0.274876 -5677.69 4039371.73 28644577.74 ... 0 0 0 1.015410 4045049.42 28761040.50 28761040.50 3517 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0299
2013-12-26 21:00:00+00:00 563.900 0.133673 2.249376e+00 0.082941 0.253714 0.208716 0.274819 -5640.00 4033731.73 28460033.00 ... 0 0 0 1.010611 4039371.73 28644577.74 28644577.74 3518 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0300
2013-12-27 21:00:00+00:00 560.089 0.133664 2.230142e+00 0.082500 0.253293 0.208686 0.274824 -5601.89 4028129.84 28273292.72 ... 0 0 0 1.005731 4033731.73 28460033.00 28460033.00 3519 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0302
2013-12-30 21:00:00+00:00 554.519 0.133668 2.202025e+00 0.081857 0.253068 0.208657 0.274829 -5546.19 4022583.65 27997664.31 ... 0 0 0 0.998525 4028129.84 28273292.72 28273292.72 3520 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0299
2013-12-31 21:00:00+00:00 561.019 0.133675 2.234843e+00 0.082487 0.258030 0.208630 0.274884 -5611.19 4016972.46 28331459.50 ... 0 0 0 1.006322 4022583.65 27997664.31 27997664.31 3521 [{'amount': 10, 'sid': Equity(0 [AAPL]), 'dt':... 0.0304

3521 rows × 39 columns

If you want to use your own custom bundle, that doesn't use yahoo finance data, check out our docs on writing a new bundle

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 [15]:
%%zipline --start 2000-1-1 --end 2014-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):
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')
    ax1.set_xlabel('time in years')

    ax2 = fig.add_subplot(212)

    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[15]:
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
2000-01-03 21:00:00+00:00 NaN NaN 0.000000 NaN -0.009549 NaN NaN 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 1 [] 0.0658
2000-01-04 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.047528 0.323229 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 2 [] 0.0649
2000-01-05 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.045697 0.329321 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 3 [] 0.0662
2000-01-06 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.044785 0.298325 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 4 [] 0.0657
2000-01-07 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.018908 0.375301 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 5 [] 0.0652
2000-01-10 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.007929 0.349070 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 6 [] 0.0657
2000-01-11 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.020888 0.326609 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 7 [] 0.0667
2000-01-12 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.025183 0.302508 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 8 [] 0.0672
2000-01-13 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.013320 0.294168 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 9 [] 0.0663
2000-01-14 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.002791 0.283818 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 10 [] 0.0669
2000-01-18 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.009604 0.271155 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 11 [] 0.0675
2000-01-19 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.009086 0.258601 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 12 [] 0.0673
2000-01-20 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.016117 0.249219 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 13 [] 0.0679
2000-01-21 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.018982 0.239561 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 14 [] 0.0679
2000-01-24 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.046092 0.254900 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 15 [] 0.0669
2000-01-25 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.040306 0.248882 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 16 [] 0.0670
2000-01-26 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.044349 0.241075 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 17 [] 0.0669
2000-01-27 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.048113 0.233934 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 18 [] 0.0668
2000-01-28 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.074249 0.244660 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 19 [] 0.0666
2000-01-31 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.050904 0.259652 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 20 [] 0.0668
2000-02-01 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.040817 0.257119 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 21 [] 0.0662
2000-02-02 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.040926 0.250992 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 22 [] 0.0660
2000-02-03 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.030138 0.248983 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 23 [] 0.0649
2000-02-04 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.030546 0.243523 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 24 [] 0.0653
2000-02-07 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.030635 0.238421 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 25 [] 0.0664
2000-02-08 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.018737 0.237304 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 26 [] 0.0659
2000-02-09 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.039163 0.240737 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 27 [] 0.0656
2000-02-10 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.035678 0.236711 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 28 [] 0.0667
2000-02-11 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.055899 0.239648 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 29 [] 0.0663
2000-02-14 21:00:00+00:00 NaN 0.000000 0.000000 0.000000 -0.053980 0.235752 0.000000 0.0 1.000000e+07 0.0 ... NaN 0 0 NaN 1.000000e+07 0.0 0.0 30 [] 0.0656
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2013-11-18 21:00:00+00:00 518.629 0.000649 0.004024 0.000254 0.219350 0.209347 0.000985 0.0 1.004024e+07 0.0 ... 476.44448 0 0 0.632681 1.004024e+07 0.0 0.0 3492 [] 0.0267
2013-11-19 21:00:00+00:00 519.549 0.000649 0.004024 0.000254 0.216859 0.209318 0.000985 0.0 1.004024e+07 0.0 ... 477.72355 0 0 0.632591 1.004024e+07 0.0 0.0 3493 [] 0.0271
2013-11-20 21:00:00+00:00 515.000 0.000649 0.004024 0.000255 0.212435 0.209291 0.000985 0.0 1.004024e+07 0.0 ... 478.83179 0 0 0.632500 1.004024e+07 0.0 0.0 3494 [] 0.0280
2013-11-21 21:00:00+00:00 521.135 0.000649 0.004024 0.000254 0.222290 0.209272 0.000984 0.0 1.004024e+07 0.0 ... 479.90982 0 0 0.632410 1.004024e+07 0.0 0.0 3495 [] 0.0279
2013-11-22 21:00:00+00:00 519.799 0.000649 0.004024 0.000254 0.228355 0.209246 0.000984 0.0 1.004024e+07 0.0 ... 480.95168 0 0 0.632319 1.004024e+07 0.0 0.0 3496 [] 0.0275
2013-11-25 21:00:00+00:00 523.740 0.000649 0.004024 0.000254 0.226803 0.209216 0.000984 0.0 1.004024e+07 0.0 ... 482.06633 0 0 0.632229 1.004024e+07 0.0 0.0 3497 [] 0.0274
2013-11-26 21:00:00+00:00 533.400 0.000649 0.004024 0.000254 0.226987 0.209186 0.000984 0.0 1.004024e+07 0.0 ... 483.30099 0 0 0.632138 1.004024e+07 0.0 0.0 3498 [] 0.0271
2013-11-27 21:00:00+00:00 545.960 0.000649 0.004024 0.000253 0.230036 0.209157 0.000984 0.0 1.004024e+07 0.0 ... 484.58915 0 0 0.632048 1.004024e+07 0.0 0.0 3499 [] 0.0274
2013-11-29 18:00:00+00:00 556.070 0.000648 0.004024 0.000253 0.229069 0.209128 0.000984 0.0 1.004024e+07 0.0 ... 485.99441 0 0 0.631958 1.004024e+07 0.0 0.0 3500 [] 0.0275
2013-12-02 21:00:00+00:00 551.230 0.000648 0.004024 0.000253 0.225727 0.209099 0.000984 -55124.0 9.985121e+06 55123.0 ... 487.28650 0 0 0.631852 1.004024e+07 0.0 0.0 3501 [{'amount': 100, 'sid': Equity(0 [AAPL]), 'dt'... 0.0281
2013-12-03 21:00:00+00:00 566.322 0.000650 0.004175 0.000264 0.221814 0.209071 0.000984 0.0 9.985121e+06 56632.2 ... 488.73719 0 0 0.655389 9.985121e+06 55123.0 55123.0 3502 [] 0.0279
2013-12-04 21:00:00+00:00 565.000 0.000649 0.004162 0.000263 0.220221 0.209042 0.000984 0.0 9.985121e+06 56500.0 ... 490.16548 0 0 0.653207 9.985121e+06 56632.2 56632.2 3503 [] 0.0284
2013-12-05 21:00:00+00:00 567.901 0.000649 0.004191 0.000266 0.214926 0.209015 0.000983 0.0 9.985121e+06 56790.1 ... 491.59557 0 0 0.657653 9.985121e+06 56500.0 56500.0 3504 [] 0.0288
2013-12-06 21:00:00+00:00 560.020 0.000650 0.004112 0.000259 0.228579 0.209007 0.000982 0.0 9.985121e+06 56002.0 ... 492.94571 0 0 0.644548 9.985121e+06 56790.1 56790.1 3505 [] 0.0288
2013-12-09 21:00:00+00:00 566.430 0.000650 0.004176 0.000264 0.230812 0.208977 0.000982 0.0 9.985121e+06 56643.0 ... 494.34565 0 0 0.654474 9.985121e+06 56002.0 56002.0 3506 [] 0.0286
2013-12-10 21:00:00+00:00 565.550 0.000650 0.004168 0.000263 0.226898 0.208949 0.000982 0.0 9.985121e+06 56555.0 ... 495.80403 0 0 0.652997 9.985121e+06 56643.0 56643.0 3507 [] 0.0281
2013-12-11 21:00:00+00:00 561.360 0.000650 0.004126 0.000261 0.213013 0.208942 0.000982 0.0 9.985121e+06 56136.0 ... 497.20708 0 0 0.646165 9.985121e+06 56555.0 56555.0 3508 [] 0.0286
2013-12-12 21:00:00+00:00 560.540 0.000650 0.004117 0.000260 0.208440 0.208915 0.000983 0.0 9.985121e+06 56054.0 ... 498.67422 0 0 0.644785 9.985121e+06 56136.0 56136.0 3509 [] 0.0289
2013-12-13 21:00:00+00:00 554.430 0.000650 0.004056 0.000256 0.208317 0.208885 0.000983 0.0 9.985121e+06 55443.0 ... 499.86772 0 0 0.634751 9.985121e+06 56054.0 56054.0 3510 [] 0.0288
2013-12-16 21:00:00+00:00 557.500 0.000650 0.004087 0.000258 0.215954 0.208862 0.000983 0.0 9.985121e+06 55750.0 ... 501.11177 0 0 0.639450 9.985121e+06 55443.0 55443.0 3511 [] 0.0289
2013-12-17 21:00:00+00:00 554.990 0.000650 0.004062 0.000256 0.212183 0.208834 0.000983 0.0 9.985121e+06 55499.0 ... 502.30613 0 0 0.635376 9.985121e+06 55750.0 55750.0 3512 [] 0.0285
2013-12-18 21:00:00+00:00 550.770 0.000650 0.004020 0.000252 0.232363 0.208851 0.000981 0.0 9.985121e+06 55077.0 ... 503.39112 0 0 0.628514 9.985121e+06 55499.0 55499.0 3513 [] 0.0289
2013-12-19 21:00:00+00:00 544.460 0.000650 0.003957 0.000247 0.231649 0.208822 0.000981 0.0 9.985121e+06 54446.0 ... 504.35840 0 0 0.618170 9.985121e+06 55077.0 55077.0 3514 [] 0.0294
2013-12-20 21:00:00+00:00 549.020 0.000650 0.004002 0.000250 0.237584 0.208796 0.000982 0.0 9.985121e+06 54902.0 ... 505.37908 0 0 0.625186 9.985121e+06 54446.0 54446.0 3515 [] 0.0289
2013-12-23 21:00:00+00:00 570.090 0.000652 0.004213 0.000265 0.244165 0.208771 0.000983 0.0 9.985121e+06 57009.0 ... 506.56951 0 0 0.657915 9.985121e+06 54902.0 54902.0 3516 [] 0.0294
2013-12-24 18:00:00+00:00 567.670 0.000652 0.004189 0.000263 0.247793 0.208742 0.000983 0.0 9.985121e+06 56767.0 ... 507.67782 0 0 0.653989 9.985121e+06 57009.0 57009.0 3517 [] 0.0299
2013-12-26 21:00:00+00:00 563.900 0.000652 0.004151 0.000260 0.253714 0.208716 0.000983 0.0 9.985121e+06 56390.0 ... 508.68018 0 0 0.647871 9.985121e+06 56767.0 56767.0 3518 [] 0.0300
2013-12-27 21:00:00+00:00 560.090 0.000652 0.004113 0.000257 0.253293 0.208686 0.000983 0.0 9.985121e+06 56009.0 ... 509.68593 0 0 0.641693 9.985121e+06 56390.0 56390.0 3519 [] 0.0302
2013-12-30 21:00:00+00:00 554.520 0.000652 0.004057 0.000253 0.253068 0.208657 0.000983 0.0 9.985121e+06 55452.0 ... 510.63864 0 0 0.632605 9.985121e+06 56009.0 56009.0 3520 [] 0.0299
2013-12-31 21:00:00+00:00 561.020 0.000652 0.004122 0.000257 0.258030 0.208630 0.000983 0.0 9.985121e+06 56102.0 ... 511.66550 0 0 0.642621 9.985121e+06 55452.0 55452.0 3521 [] 0.0304

3521 rows × 41 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.