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 [4]:
# Get backtest object
#bt = get_backtest('56bce47e611ec212a436ff8e') #Simon's full backtest
bt = get_backtest('56c9d3e0081e330de4368e8b') 


# Create all tear sheets
bt.create_full_tear_sheet()
100% Time: 0:00:26|###########################################################|
Entire data start date: 2003-03-03
Entire data end date: 2016-02-04


Backtest Months: 155
                   Backtest
annual_return          0.17
annual_volatility      0.18
sharpe_ratio           0.97
calmar_ratio           0.79
stability              0.93
max_drawdown          -0.22
omega_ratio            1.19
sortino_ratio          1.38
skewness              -0.32
kurtosis               2.70
information_ratio      0.04
alpha                  0.13
beta                   0.56

Worst Drawdown Periods
   net drawdown in %  peak date valley date recovery date duration
3              18.63 2007-10-29  2008-02-06    2009-10-14      513
1              17.53 2011-07-07  2011-08-08    2013-03-05      434
4              16.27 2010-04-15  2010-05-20    2010-10-25      138
0              15.01 2015-03-20  2016-02-04           NaT      NaN
2              10.74 2014-03-04  2014-04-11    2014-07-23      102


2-sigma returns daily    -0.022
2-sigma returns weekly   -0.043
dtype: float64
Stress Events
                                    mean    min    max
Lehmann                           -0.001 -0.014  0.005
US downgrade/European Debt Crisis  0.001 -0.061  0.054
Fukushima                          0.003 -0.014  0.021
EZB IR Event                      -0.001 -0.023  0.011
Aug07                              0.001 -0.040  0.040
Mar08                             -0.000 -0.026  0.026
Sept08                            -0.001 -0.005  0.001
2009Q1                             0.000  0.000  0.000
2009Q2                             0.000  0.000  0.000
Flash Crash                        0.001 -0.039  0.070
Apr14                             -0.000 -0.032  0.021
Oct14                             -0.000 -0.028  0.017
Fall2015                          -0.002 -0.028  0.023
Low Volatility Bull Market         0.001 -0.052  0.042
GFC Crash                          0.000 -0.057  0.049
Recovery                           0.000 -0.061  0.070
New Normal                         0.000 -0.032  0.023