Notebook

Portfolio Analysis using pyfolio

There are many ways to evaluate and analyze an algorithm. While we already provide you with some of these measures like a cumulative returns plot in the Quantopian backtester, you may want to dive deeper into what your algorithm is doing. For example, you might want to look at how your portfolio allocation changes over time, or what your exposure to certain risk-factors is.

At Quantopian, we built and open-sourced pyfolio for exactly that purpose. In this notebook you will learn how you can use this library from within the Quantopian research environment (you can also use this library independently, see the pyfolio website for more information on that).

At the core of pyfolio, we have tear sheets that summarize information about a backtest. Each tear sheet returns a number of plots, as well as other information, about a given topic. There are five main ones:

  • Cumulative returns tear sheet
  • Shock event returns tear sheet
  • Positional tear sheet
  • Transactional tear sheet
  • Bayesian tear sheet

We have added an interface to the object returned by get_backtest() to create these various tear sheets. To generate all tear sheets at once, it's as simple as generating a backtest object and calling create_full_tear_sheet on it:

In [1]:
# Get backtest object
bt = get_backtest('56b616d9e5f94912b0285bf6')

# Create all tear sheets
bt.create_full_tear_sheet()
100% Time: 0:00:05|###########################################################|
Entire data start date: 2011-06-01
Entire data end date: 2016-02-04


Backtest Months: 56
                   Backtest
annual_return          0.27
annual_volatility      0.25
sharpe_ratio           1.11
calmar_ratio           1.65
stability              0.93
max_drawdown          -0.17
omega_ratio            1.21
sortino_ratio          1.61
skewness              -0.17
kurtosis               2.25
information_ratio      0.04
alpha                  0.28
beta                  -0.09

Worst Drawdown Periods
   net drawdown in %  peak date valley date recovery date duration
0              15.10 2015-01-30  2015-03-06    2015-07-23      125
1              14.39 2013-05-17  2013-08-30    2013-11-14      130
2              13.28 2014-01-22  2014-02-05    2014-05-02       73
3              11.41 2014-07-03  2014-08-07    2014-12-12      117
4               9.18 2015-08-24  2015-09-16    2016-01-20      108


2-sigma returns daily    -0.030
2-sigma returns weekly   -0.055
dtype: float64
Stress Events
                                    mean    min    max
US downgrade/European Debt Crisis  0.006 -0.081  0.062
EZB IR Event                       0.000 -0.036  0.036
Apr14                              0.002 -0.018  0.016
Oct14                              0.001 -0.014  0.020
Fall2015                          -0.000 -0.031  0.024
Recovery                           0.001 -0.081  0.066
New Normal                         0.001 -0.058  0.050