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 [11]:
# Get backtest object
bt = get_backtest('56bb3f8d3ce1db11952648c0')

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


Backtest Months: 62
                   Backtest
annual_return          0.33
annual_volatility      0.23
sharpe_ratio           1.37
calmar_ratio           1.74
stability              0.93
max_drawdown          -0.19
omega_ratio            1.28
sortino_ratio          1.96
skewness              -0.54
kurtosis               3.49
information_ratio      0.07
alpha                  0.23
beta                   0.77

Worst Drawdown Periods
   net drawdown in %  peak date valley date recovery date duration
0              19.24 2015-09-17  2016-01-11           NaT      NaN
4              15.53 2012-03-26  2012-05-18    2012-08-13      101
1              14.99 2015-06-23  2015-07-09    2015-08-11       36
2              14.71 2014-07-16  2014-12-16    2015-03-20      178
3              13.39 2013-08-05  2013-08-30    2014-03-24      166


2-sigma returns daily    -0.028
2-sigma returns weekly   -0.050
dtype: float64
Stress Events
                                    mean    min    max
US downgrade/European Debt Crisis  0.001 -0.047  0.020
Fukushima                          0.003 -0.015  0.019
EZB IR Event                       0.000 -0.029  0.033
Apr14                              0.001 -0.019  0.015
Oct14                              0.001 -0.019  0.029
Fall2015                          -0.003 -0.063  0.031
Recovery                           0.002 -0.067  0.053
New Normal                         0.001 -0.090  0.056