Zipline algorithm analysis example in pyfolio

Here's an example where we run an algorithm with zipline, then produce tear sheets for that algorithm.

Imports

Import pyfolio and zipline, and ingest the pricing data for backtesting.

In [1]:
import sys
sys.path.append('/Users/george/Desktop/pyfolio/')
In [ ]:
import pyfolio as pf
%matplotlib inline

# silence warnings
import warnings
warnings.filterwarnings('ignore')

import zipline
%load_ext zipline
!zipline ingest
Downloading Bundle: quantopian-quandl  [------------------------------------]    0%

Run our zipline algorithm

This algorithm can also be adjusted to execute a modified, or completely different, trading strategy.

In [2]:
%%zipline --start 2004-1-1 --end 2010-1-1 -o results.pickle

# Zipline trading algorithm
# Taken from zipline.examples.olmar

import numpy as np

from zipline.finance import commission, slippage

STOCKS = ['AMD', 'CERN', 'COST', 'DELL', 'GPS', 'INTC', 'MMM']


# On-Line Portfolio Moving Average Reversion

# More info can be found in the corresponding paper:
# http://icml.cc/2012/papers/168.pdf
def initialize(algo, eps=1, window_length=5):
    algo.stocks = STOCKS
    algo.sids = [algo.symbol(symbol) for symbol in algo.stocks]
    algo.m = len(algo.stocks)
    algo.price = {}
    algo.b_t = np.ones(algo.m) / algo.m
    algo.eps = eps
    algo.window_length = window_length

    algo.set_commission(commission.PerShare(cost=0))
    algo.set_slippage(slippage.FixedSlippage(spread=0))


def handle_data(algo, data):
    m = algo.m

    x_tilde = np.zeros(m)
    b = np.zeros(m)

    # find relative moving average price for each asset
    mavgs = data.history(algo.sids, 'price', algo.window_length, '1d').mean()
    for i, sid in enumerate(algo.sids):
        price = data.current(sid, "price")
        # Relative mean deviation
        x_tilde[i] = mavgs[sid] / price

    ###########################
    # Inside of OLMAR (algo 2)
    x_bar = x_tilde.mean()

    # market relative deviation
    mark_rel_dev = x_tilde - x_bar

    # Expected return with current portfolio
    exp_return = np.dot(algo.b_t, x_tilde)
    weight = algo.eps - exp_return
    variability = (np.linalg.norm(mark_rel_dev)) ** 2

    # test for divide-by-zero case
    if variability == 0.0:
        step_size = 0
    else:
        step_size = max(0, weight / variability)

    b = algo.b_t + step_size * mark_rel_dev
    b_norm = simplex_projection(b)
    np.testing.assert_almost_equal(b_norm.sum(), 1)

    rebalance_portfolio(algo, data, b_norm)

    # update portfolio
    algo.b_t = b_norm


def rebalance_portfolio(algo, data, desired_port):
    # rebalance portfolio
    for i, sid in enumerate(algo.sids):
        algo.order_target_percent(sid, desired_port[i])


def simplex_projection(v, b=1):
    """Projection vectors to the simplex domain
    Implemented according to the paper: Efficient projections onto the
    l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
    Implementation Time: 2011 June 17 by [email protected] AT pmail.ntu.edu.sg
    Optimization Problem: min_{w}\| w - v \|_{2}^{2}
    s.t. sum_{i=1}^{m}=z, w_{i}\geq 0
    Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
    Output: Projection vector w
    :Example:
    >>> proj = simplex_projection([.4 ,.3, -.4, .5])
    >>> print(proj)
    array([ 0.33333333, 0.23333333, 0. , 0.43333333])
    >>> print(proj.sum())
    1.0
    Original matlab implementation: John Duchi ([email protected])
    Python-port: Copyright 2013 by Thomas Wiecki ([email protected]).
    """

    v = np.asarray(v)
    p = len(v)

    # Sort v into u in descending order
    v = (v > 0) * v
    u = np.sort(v)[::-1]
    sv = np.cumsum(u)

    rho = np.where(u > (sv - b) / np.arange(1, p + 1))[0][-1]
    theta = np.max([0, (sv[rho] - b) / (rho + 1)])
    w = (v - theta)
    w[w < 0] = 0
    return w
Out[2]:
algo_volatility algorithm_period_return alpha benchmark_period_return benchmark_volatility beta capital_used ending_cash ending_exposure ending_value ... short_exposure short_value shorts_count sortino starting_cash starting_exposure starting_value trading_days transactions treasury_period_return
2004-01-02 21:00:00+00:00 NaN 0.000000e+00 NaN -0.000449 NaN NaN 0.000000e+00 1.000000e+07 0.00 0.00 ... 0 0 0 NaN 1.000000e+07 0.00 0.00 1 [] 0.0438
2004-01-05 21:00:00+00:00 0.000008 -7.000000e-07 -0.000007 0.010424 0.127153 -0.000062 -1.012625e+07 -1.262475e+05 10126240.51 10126240.51 ... 0 0 0 -11.224972 1.000000e+07 0.00 0.00 2 [{u'commission': None, u'amount': 39332, u'sid... 0.0441
2004-01-06 21:00:00+00:00 0.072177 7.874142e-03 0.941443 0.011413 0.097935 -0.292225 1.098407e+05 -1.640676e+04 10095148.18 10095148.18 ... 0 0 0 103096.791454 -1.262475e+05 10126240.51 10126240.51 3 [{u'commission': None, u'amount': 22673, u'sid... 0.0429
2004-01-07 21:00:00+00:00 0.064694 1.390826e-02 1.158059 0.014827 0.080035 -0.305795 3.809134e+04 2.168458e+04 10117398.03 10117398.03 ... 0 0 0 157170.375195 -1.640676e+04 10095148.18 10095148.18 4 [{u'commission': None, u'amount': 4882, u'sid'... 0.0427
2004-01-08 21:00:00+00:00 0.202046 -1.030250e-02 -0.137563 0.018871 0.069343 -0.388318 1.594916e+04 3.763374e+04 9859341.24 9859341.24 ... 0 0 0 -2.978283 2.168458e+04 10117398.03 10117398.03 5 [{u'commission': None, u'amount': 7020, u'sid'... 0.0427
2004-01-09 21:00:00+00:00 0.181404 -9.874269e-03 -0.290170 0.009975 0.101951 -0.266625 -3.549779e+04 2.135950e+03 9899121.36 9899121.36 ... 0 0 0 -2.601355 3.763374e+04 9859341.24 9859341.24 6 [{u'commission': None, u'amount': 12007, u'sid... 0.0411
2004-01-12 21:00:00+00:00 0.169355 -5.602480e-03 -0.118894 0.017344 0.098995 -0.113533 -3.387390e+03 -1.251440e+03 9945226.64 9945226.64 ... 0 0 0 -1.324308 2.135950e+03 9899121.36 9899121.36 7 [{u'commission': None, u'amount': 879, u'sid':... 0.0411
2004-01-13 21:00:00+00:00 0.162138 -1.366743e-02 -0.454475 0.011503 0.102602 0.090323 -5.892000e+02 -1.840640e+03 9865166.35 9865166.35 ... 0 0 0 -2.977973 -1.251440e+03 9945226.64 9945226.64 8 [{u'commission': None, u'amount': 478, u'sid':... 0.0405
2004-01-14 21:00:00+00:00 0.156543 -8.090471e-03 -0.338686 0.020219 0.103200 0.216209 2.099000e+02 -1.630740e+03 9920726.03 9920726.03 ... 0 0 0 -1.621248 -1.840640e+03 9865166.35 9865166.35 9 [{u'commission': None, u'amount': -958, u'sid'... 0.0401
2004-01-15 21:00:00+00:00 0.147732 -1.021672e-02 -0.370917 0.022466 0.097298 0.216356 -2.985980e+03 -4.616720e+03 9902449.48 9902449.48 ... 0 0 0 -1.957694 -1.630740e+03 9920726.03 9920726.03 10 [{u'commission': None, u'amount': -12547, u'si... 0.0399
2004-01-16 21:00:00+00:00 0.150072 -9.720000e-05 -0.152516 0.026510 0.092669 0.265920 4.211040e+03 -4.056800e+02 9999433.68 9999433.68 ... 0 0 0 0.066906 -4.616720e+03 9902449.48 9902449.48 11 [{u'commission': None, u'amount': 29, u'sid': ... 0.0404
2004-01-20 21:00:00+00:00 0.174603 -2.189795e-02 -0.677508 0.026240 0.089193 0.414247 -6.349190e+03 -6.754870e+03 9787775.37 9787775.37 ... 0 0 0 -2.942390 -4.056800e+02 9999433.68 9999433.68 12 [{u'commission': None, u'amount': -8204, u'sid... 0.0408
2004-01-21 21:00:00+00:00 0.167345 -2.194580e-02 -0.682346 0.033879 0.088489 0.408461 3.967860e+03 -2.787010e+03 9783328.99 9783328.99 ... 0 0 0 -2.833401 -6.754870e+03 9787775.37 9787775.37 13 [{u'commission': None, u'amount': 18, u'sid': ... 0.0405
2004-01-22 21:00:00+00:00 0.160815 -2.278829e-02 -0.620203 0.031991 0.087048 0.381335 3.202510e+03 4.155000e+02 9771701.58 9771701.58 ... 0 0 0 -2.838791 -2.787010e+03 9783328.99 9783328.99 14 [{u'commission': None, u'amount': 1177, u'sid'... 0.0399
2004-01-23 21:00:00+00:00 0.156057 -1.995549e-02 -0.466320 0.028307 0.087226 0.294719 -5.584200e+02 -1.429200e+02 9800588.06 9800588.06 ... 0 0 0 -2.386967 4.155000e+02 9771701.58 9771701.58 15 [{u'commission': None, u'amount': -1437, u'sid... 0.0409
2004-01-26 21:00:00+00:00 0.151899 -1.665361e-02 -0.460877 0.041247 0.094376 0.323325 -5.684500e+02 -7.113700e+02 9834175.24 9834175.24 ... 0 0 0 -1.911049 -1.429200e+02 9800588.06 9800588.06 16 [{u'commission': None, u'amount': 1760, u'sid'... 0.0416
2004-01-27 21:00:00+00:00 0.149383 -2.432286e-02 -0.521233 0.030554 0.103850 0.369999 4.884130e+03 4.172760e+03 9752598.61 9752598.61 ... 0 0 0 -2.680537 -7.113700e+02 9834175.24 9834175.24 17 [{u'commission': None, u'amount': 27757, u'sid... 0.0411
2004-01-28 21:00:00+00:00 0.159088 -4.280630e-02 -0.745407 0.018422 0.112811 0.556821 -3.171400e+03 1.001360e+03 9570935.67 9570935.67 ... 0 0 0 -4.088941 4.172760e+03 9752598.61 9752598.61 18 [{u'commission': None, u'amount': 3192, u'sid'... 0.0422
2004-01-29 21:00:00+00:00 0.154753 -4.330959e-02 -0.723066 0.019770 0.109637 0.557354 -1.023840e+03 -2.248000e+01 9566926.58 9566926.58 ... 0 0 0 -4.028374 1.001360e+03 9570935.67 9570935.67 19 [{u'commission': None, u'amount': 740, u'sid':... 0.0422
2004-01-30 21:00:00+00:00 0.151981 -4.003614e-02 -0.641952 0.019770 0.106778 0.550030 -3.302200e+02 -3.527000e+02 9599991.28 9599991.28 ... 0 0 0 -3.616538 -2.248000e+01 9566926.58 9566926.58 20 [{u'commission': None, u'amount': 202, u'sid':... 0.0416
2004-02-02 21:00:00+00:00 0.148849 -3.791101e-02 -0.611549 0.023544 0.104494 0.558118 7.507400e+02 3.980400e+02 9620491.88 9620491.88 ... 0 0 0 -3.333751 -3.527000e+02 9599991.28 9599991.28 21 [{u'commission': None, u'amount': -774, u'sid'... 0.0418
2004-02-03 21:00:00+00:00 0.149505 -2.958549e-02 -0.470647 0.022466 0.102243 0.530207 -3.759100e+02 2.213000e+01 9704122.98 9704122.98 ... 0 0 0 -2.509972 3.980400e+02 9620491.88 9620491.88 22 [{u'commission': None, u'amount': -1805, u'sid... 0.0413
2004-02-04 21:00:00+00:00 0.148908 -3.935433e-02 -0.518102 0.014019 0.104520 0.565794 2.243250e+04 2.245463e+04 9584002.02 9584002.02 ... 0 0 0 -3.200968 2.213000e+01 9704122.98 9704122.98 23 [{u'commission': None, u'amount': -115293, u's... 0.0415
2004-02-05 21:00:00+00:00 0.157685 -2.306599e-02 -0.343928 0.017074 0.102515 0.607067 -2.537802e+04 -2.923390e+03 9772263.47 9772263.47 ... 0 0 0 -1.776034 2.245463e+04 9584002.02 9584002.02 24 [{u'commission': None, u'amount': 28, u'sid': ... 0.0420
2004-02-06 21:00:00+00:00 0.169544 -2.392913e-03 -0.228447 0.028487 0.105744 0.755742 -7.073620e+03 -9.997010e+03 9986067.88 9986067.88 ... 0 0 0 -0.080130 -2.923390e+03 9772263.47 9772263.47 25 [{u'commission': None, u'amount': -5231, u'sid... 0.0412
2004-02-09 21:00:00+00:00 0.166124 -2.006309e-03 -0.220937 0.029206 0.103617 0.755433 4.991920e+03 -5.005090e+03 9984942.00 9984942.00 ... 0 0 0 -0.048764 -9.997010e+03 9986067.88 9986067.88 26 [{u'commission': None, u'amount': 3350, u'sid'... 0.0409
2004-02-10 21:00:00+00:00 0.179042 2.224008e-02 -0.016088 0.032081 0.101732 0.790076 3.909423e+04 3.408914e+04 10188311.63 10188311.63 ... 0 0 0 1.786071 -5.005090e+03 9984942.00 9984942.00 27 [{u'commission': None, u'amount': 20374, u'sid... 0.0413
2004-02-11 21:00:00+00:00 0.175854 2.568422e-02 -0.045677 0.043045 0.103763 0.750971 -9.027760e+04 -5.618846e+04 10313030.69 10313030.69 ... 0 0 0 2.003630 3.408914e+04 10188311.63 10188311.63 28 [{u'commission': None, u'amount': 86138, u'sid... 0.0405
2004-02-12 21:00:00+00:00 0.174331 1.835900e-02 -0.088628 0.039270 0.103016 0.768848 6.841798e+04 1.222952e+04 10171360.46 10171360.46 ... 0 0 0 1.426563 -5.618846e+04 10313030.69 10313030.69 29 [{u'commission': None, u'amount': 60959, u'sid... 0.0410
2004-02-13 21:00:00+00:00 0.174263 7.811894e-03 -0.152515 0.034597 0.102633 0.799374 -8.834620e+03 3.394900e+03 10074724.04 10074724.04 ... 0 0 0 0.651812 1.222952e+04 10171360.46 10171360.46 30 [{u'commission': None, u'amount': -2990, u'sid... 0.0405
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2009-11-18 21:00:00+00:00 0.257581 6.008698e-01 0.099259 -0.000090 0.683428 0.080995 -2.965000e+02 -1.613460e+02 16008859.05 16008859.05 ... 0 0 0 0.642225 1.351540e+02 16099119.48 16099119.48 1482 [{u'commission': None, u'amount': 126, u'sid':... 0.0336
2009-11-19 21:00:00+00:00 0.257584 5.751349e-01 0.096627 -0.013120 0.683220 0.081072 6.384500e+02 4.771040e+02 15750871.82 15750871.82 ... 0 0 0 0.626050 -1.613460e+02 16008859.05 16008859.05 1483 [{u'commission': None, u'amount': -2169, u'sid... 0.0335
2009-11-20 21:00:00+00:00 0.257497 5.760543e-01 0.096710 -0.016625 0.682992 0.081071 -9.618800e+02 -4.847760e+02 15761027.55 15761027.55 ... 0 0 0 0.626402 4.771040e+02 15750871.82 15750871.82 1484 [{u'commission': None, u'amount': 1554, u'sid'... 0.0336
2009-11-23 21:00:00+00:00 0.257412 5.801855e-01 0.096914 -0.004134 0.682780 0.081077 2.498000e+01 -4.597960e+02 15802314.57 15802314.57 ... 0 0 0 0.628718 -4.847760e+02 15761027.55 15761027.55 1485 [{u'commission': None, u'amount': -1472, u'sid... 0.0337
2009-11-24 21:00:00+00:00 0.257339 5.910314e-01 0.097991 -0.002606 0.682550 0.081079 -7.668800e+02 -1.226676e+03 15911540.99 15911540.99 ... 0 0 0 0.635119 -4.597960e+02 15802314.57 15802314.57 1486 [{u'commission': None, u'amount': 3775, u'sid'... 0.0332
2009-11-25 21:00:00+00:00 0.257277 6.056737e-01 0.099435 0.000899 0.682322 0.081087 7.561600e+02 -4.705160e+02 16057207.89 16057207.89 ... 0 0 0 0.643769 -1.226676e+03 15911540.99 15911540.99 1487 [{u'commission': None, u'amount': -18139, u'si... 0.0328
2009-11-27 18:00:00+00:00 0.257296 5.777323e-01 0.096627 -0.015367 0.682128 0.081189 -9.221030e+03 -9.691546e+03 15787014.39 15787014.39 ... 0 0 0 0.626279 -4.705160e+02 16057207.89 16057207.89 1488 [{u'commission': None, u'amount': -108386, u's... 0.0321
2009-11-30 21:00:00+00:00 0.257262 5.583863e-01 0.094443 -0.012042 0.681899 0.081176 9.661970e+03 -2.957600e+01 15583892.62 15583892.62 ... 0 0 0 0.614025 -9.691546e+03 15787014.39 15787014.39 1489 [{u'commission': None, u'amount': -2, u'sid': ... 0.0321
2009-12-01 21:00:00+00:00 0.257202 5.728593e-01 0.095775 0.000180 0.681687 0.081210 -2.880110e+03 -2.909686e+03 15731503.04 15731503.04 ... 0 0 0 0.622743 -2.957600e+01 15583892.62 15583892.62 1490 [{u'commission': None, u'amount': 4666, u'sid'... 0.0328
2009-12-02 21:00:00+00:00 0.257128 5.637940e-01 0.094742 -0.000270 0.681459 0.081212 3.608900e+03 6.992140e+02 15637240.49 15637240.49 ... 0 0 0 0.616942 -2.909686e+03 15731503.04 15731503.04 1491 [{u'commission': None, u'amount': -4907, u'sid... 0.0332
2009-12-03 21:00:00+00:00 0.257153 5.356815e-01 0.091740 -0.008088 0.681239 0.081267 -1.671090e+03 -9.718760e+02 15357787.00 15357787.00 ... 0 0 0 0.598945 6.992140e+02 15637240.49 15637240.49 1492 [{u'commission': None, u'amount': 1667, u'sid'... 0.0339
2009-12-04 21:00:00+00:00 0.257089 5.489262e-01 0.093054 -0.002426 0.681014 0.081282 1.217390e+03 2.455140e+02 15489016.40 15489016.40 ... 0 0 0 0.607016 -9.718760e+02 15357787.00 15357787.00 1493 [{u'commission': None, u'amount': -1387, u'sid... 0.0348
2009-12-07 21:00:00+00:00 0.257030 5.354856e-01 0.091548 -0.003954 0.680786 0.081289 -2.795400e+02 -3.402600e+01 15354889.76 15354889.76 ... 0 0 0 0.598371 2.455140e+02 15489016.40 15489016.40 1494 [{u'commission': None, u'amount': -1961, u'sid... 0.0344
2009-12-08 21:00:00+00:00 0.257055 5.078036e-01 0.088587 -0.015007 0.680576 0.081364 1.316500e+02 9.762400e+01 15077938.34 15077938.34 ... 0 0 0 0.580382 -3.402600e+01 15354889.76 15354889.76 1495 [{u'commission': None, u'amount': -795, u'sid'... 0.0340
2009-12-09 21:00:00+00:00 0.256971 5.048394e-01 0.088146 -0.011323 0.680349 0.081361 -2.696890e+03 -2.599266e+03 15050993.27 15050993.27 ... 0 0 0 0.578300 9.762400e+01 15077938.34 15077938.34 1496 [{u'commission': None, u'amount': -1367, u'sid... 0.0345
2009-12-10 21:00:00+00:00 0.256909 5.181698e-01 0.089499 -0.005751 0.680125 0.081375 2.364150e+03 -2.351160e+02 15181933.08 15181933.08 ... 0 0 0 0.586582 -2.599266e+03 15050993.27 15050993.27 1497 [{u'commission': None, u'amount': 5379, u'sid'... 0.0349
2009-12-11 21:00:00+00:00 0.256836 5.283234e-01 0.090504 -0.001528 0.679899 0.081383 2.523800e+02 1.726400e+01 15283216.38 15283216.38 ... 0 0 0 0.592783 -2.351160e+02 15181933.08 15181933.08 1498 [{u'commission': None, u'amount': -483, u'sid'... 0.0355
2009-12-14 21:00:00+00:00 0.256789 5.454956e-01 0.092235 0.005302 0.679677 0.081406 6.313700e+03 6.330964e+03 15448625.06 15448625.06 ... 0 0 0 0.603328 1.726400e+01 15283216.38 15283216.38 1499 [{u'commission': None, u'amount': -21110, u'si... 0.0356
2009-12-15 21:00:00+00:00 0.256712 5.540960e-01 0.093174 0.000629 0.679454 0.081395 -2.764129e+04 -2.131033e+04 15562270.30 15562270.30 ... 0 0 0 0.608445 6.330964e+03 15448625.06 15448625.06 1500 [{u'commission': None, u'amount': -273861, u's... 0.0360
2009-12-16 21:00:00+00:00 0.256629 5.591566e-01 0.093638 0.002157 0.679227 0.081396 1.440453e+05 1.227349e+05 15468831.23 15468831.23 ... 0 0 0 0.611354 -2.131033e+04 15562270.30 15562270.30 1501 [{u'commission': None, u'amount': 108105, u'si... 0.0361
2009-12-17 21:00:00+00:00 0.256583 5.426112e-01 0.091951 -0.009885 0.679021 0.081442 -1.195010e+05 3.233914e+03 15422877.79 15422877.79 ... 0 0 0 0.600831 1.227349e+05 15468831.23 15468831.23 1502 [{u'commission': None, u'amount': 9112, u'sid'... 0.0350
2009-12-18 21:00:00+00:00 0.256572 5.665010e-01 0.094483 -0.009615 0.678795 0.081440 -4.617290e+03 -1.383376e+03 15666393.17 15666393.17 ... 0 0 0 0.615413 3.233914e+03 15422877.79 15422877.79 1503 [{u'commission': None, u'amount': 2044, u'sid'... 0.0355
2009-12-21 21:00:00+00:00 0.256673 6.045817e-01 0.098342 0.000449 0.678580 0.081520 4.766944e+04 4.628606e+04 15999531.04 15999531.04 ... 0 0 0 0.638404 -1.383376e+03 15666393.17 15666393.17 1504 [{u'commission': None, u'amount': 64288, u'sid... 0.0369
2009-12-22 21:00:00+00:00 0.256588 6.030770e-01 0.098071 0.004044 0.678355 0.081518 -4.947564e+04 -3.189576e+03 16033959.57 16033959.57 ... 0 0 0 0.637296 4.628606e+04 15999531.04 15999531.04 1505 [{u'commission': None, u'amount': 162944, u'si... 0.0376
2009-12-23 21:00:00+00:00 0.256510 6.113661e-01 0.098843 0.006021 0.678130 0.081521 1.820101e+04 1.501143e+04 16098649.18 16098649.18 ... 0 0 0 0.642015 -3.189576e+03 16033959.57 16033959.57 1506 [{u'commission': None, u'amount': 100740, u'si... 0.0377
2009-12-24 18:00:00+00:00 0.256425 6.129331e-01 0.098876 0.010784 0.677907 0.081521 1.411030e+04 2.912173e+04 16100208.93 16100208.93 ... 0 0 0 0.642729 1.501143e+04 16098649.18 16098649.18 1507 [{u'commission': None, u'amount': 61067, u'sid... 0.0382
2009-12-28 21:00:00+00:00 0.256359 6.261147e-01 0.100146 0.012940 0.677682 0.081525 -3.452828e+04 -5.406546e+03 16266553.28 16266553.28 ... 0 0 0 0.650304 2.912173e+04 16100208.93 16100208.93 1508 [{u'commission': None, u'amount': 20274, u'sid... 0.0385
2009-12-29 21:00:00+00:00 0.256290 6.382284e-01 0.101344 0.011503 0.677458 0.081519 -6.974254e+03 -1.238080e+04 16394664.33 16394664.33 ... 0 0 0 0.657185 -5.406546e+03 16266553.28 16266553.28 1509 [{u'commission': None, u'amount': 69484, u'sid... 0.0382
2009-12-30 21:00:00+00:00 0.256206 6.422355e-01 0.101690 0.011143 0.677234 0.081519 -1.493927e+05 -1.617735e+05 16584128.61 16584128.61 ... 0 0 0 0.659296 -1.238080e+04 16394664.33 16394664.33 1510 [{u'commission': None, u'amount': -244304, u's... 0.0380
2009-12-31 21:00:00+00:00 0.256245 6.110285e-01 0.098572 0.001438 0.677023 0.081588 6.987978e+04 -9.189371e+04 16202178.41 16202178.41 ... 0 0 0 0.640360 -1.617735e+05 16584128.61 16584128.61 1511 [{u'commission': None, u'amount': -116692, u's... 0.0385

1511 rows × 37 columns

Extract metrics

Get the returns, positions, and transactions from the zipline backtest object.

In [3]:
import pandas as pd

results = pd.read_pickle('results.pickle')
returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)

Single plot example

Make one plot of the top 5 drawdown periods.

In [4]:
pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')
Out[4]:
<matplotlib.text.Text at 0x1207c8050>

Full tear sheet example

Create a full tear sheet for our algorithm. As an example, set the live start date to something arbitrary.

In [5]:
pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions,
                          live_start_date='2009-10-22', round_trips=True)
/Users/georgeho/pyfolio/pyfolio/utils.py:251: UserWarning: Yahoo Finance read failed: HTTPConnectionPool(host='ichart.finance.yahoo.com', port=80): Max retries exceeded with url: /table.csv?a=0&ignore=.csv&s=SPY&b=1&e=13&d=7&g=d&f=2017&c=1970 (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x12372a750>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)), falling back to Google
  UserWarning)
Entire data start date: 2004-01-02
Entire data end date: 2009-12-31
In-sample months: 69
Out-of-sample months: 2
All In-sample Out-of-sample
Annual return 8.3% 8.1% 14.9%
Cumulative returns 61.1% 56.8% 2.7%
Annual volatility 25.6% 25.7% 22.0%
Sharpe ratio 0.44 0.43 0.74
Calmar ratio 0.14 0.13 2.03
Stability 0.00 0.01 0.04
Max drawdown -60.3% -60.3% -7.3%
Omega ratio 1.08 1.08 1.13
Sortino ratio 0.64 0.63 1.04
Skew 0.21 0.22 -0.29
Kurtosis 4.19 4.24 0.36
Tail ratio 0.97 0.99 1.23
Daily value at risk -3.2% -3.2% -2.7%
Gross leverage 1.00 1.00 1.00
Daily turnover 7.6% 7.5% 9.6%
Alpha 0.09 0.10 -0.03
Beta 0.83 0.82 1.19
Worst drawdown periods Net drawdown in % Peak date Valley date Recovery date Duration
0 60.30 2007-11-06 2008-11-20 NaT NaN
1 23.25 2006-04-06 2006-09-07 2007-05-22 294
2 12.52 2004-11-15 2005-10-12 2006-01-11 303
3 10.90 2004-06-25 2004-08-12 2004-11-04 95
4 9.47 2007-07-16 2007-08-06 2007-09-04 37
/Users/georgeho/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py:4269: RuntimeWarning: Invalid value encountered in percentile
  interpolation=interpolation)
Stress Events mean min max
Lehmann -0.28% -7.41% 4.40%
Aug07 0.35% -2.96% 3.03%
Mar08 -0.43% -3.10% 3.34%
Sept08 -0.68% -7.41% 3.99%
2009Q1 -0.35% -4.98% 3.36%
2009Q2 0.71% -3.78% 6.17%
Low Volatility Bull Market 0.01% -6.11% 6.45%
GFC Crash -0.08% -7.58% 9.71%
Recovery 0.32% -3.78% 6.17%