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 [2]:
# Get backtest object
bt = get_backtest('55f6e75c3107830e0b3f57c0')

# Create all tear sheets
bt.create_full_tear_sheet()
100% Time: 0:00:28|###########################################################|
Entire data start date: 2003-01-13 00:00:00+00:00
Entire data end date: 2015-02-05 00:00:00+00:00


Backtest Months: 144
                   Backtest
annual_return          0.28
annual_volatility      0.25
sharpe_ratio           1.11
calmar_ratio           0.76
stability              0.94
max_drawdown          -0.37
omega_ratio            1.23
sortino_ratio          1.63
skewness              -0.26
kurtosis               7.14
alpha                  0.21
beta                   0.87

Worst Drawdown Periods
   net drawdown in %                  peak date                valley date  \
0              37.00  2011-07-07 00:00:00+00:00  2011-08-08 00:00:00+00:00   
4              31.01  2007-12-26 00:00:00+00:00  2009-07-07 00:00:00+00:00   
2              17.65  2011-04-29 00:00:00+00:00  2011-06-16 00:00:00+00:00   
3              12.28  2014-01-15 00:00:00+00:00  2014-02-03 00:00:00+00:00   
1              11.70  2014-09-05 00:00:00+00:00  2014-10-16 00:00:00+00:00   

               recovery date duration  
0  2013-05-07 00:00:00+00:00      479  
4  2009-10-09 00:00:00+00:00      468  
2  2013-05-08 00:00:00+00:00      529  
3  2014-03-04 00:00:00+00:00       35  
1  2014-10-31 00:00:00+00:00       41  


2-sigma returns daily    -0.031
2-sigma returns weekly   -0.059
dtype: float64

Stress Events
                                    mean    min    max
Lehmann                           -0.002 -0.038  0.027
US downgrade/European Debt Crisis  0.002 -0.145  0.117
Fukushima                          0.004 -0.033  0.030
US Housing                         0.000  0.000  0.000
EZB IR Event                      -0.001 -0.018  0.018
Aug07                              0.001 -0.052  0.042
Sept08                            -0.004 -0.038  0.027
2009Q1                            -0.000 -0.014  0.006
2009Q2                             0.000 -0.005  0.012
Flash Crash                       -0.004 -0.055  0.090
Apr14                             -0.001 -0.033  0.018
Oct14                              0.002 -0.032  0.034


Top 10 long positions of all time (and max%)
[u'BIIB' u'FLS' u'AAPL' u'APA' u'AIV' u'BBBY' u'BWA' u'BXP' u'BAX' u'DVN']
[ 0.109  0.103  0.102  0.097  0.094  0.094  0.093  0.09   0.087  0.087]


Top 10 short positions of all time (and max%)
[u'AAPL' u'AKAM' u'FLS' u'ETFC' u'FFIV' u'DO' u'APA' u'CCI' u'CAM' u'BWA']
[-0.065 -0.042 -0.033 -0.024 -0.024 -0.023 -0.022 -0.02  -0.015 -0.014]


Top 10 positions of all time (and max%)
[u'BIIB' u'FLS' u'AAPL' u'APA' u'AIV' u'BBBY' u'BWA' u'BXP' u'BAX' u'DVN']
[ 0.109  0.103  0.102  0.097  0.094  0.094  0.093  0.09   0.087  0.087]


All positions ever held
[u'BIIB' u'FLS' u'AAPL' u'APA' u'AIV' u'BBBY' u'BWA' u'BXP' u'BAX' u'DVN'
 u'DLTR' u'AET' u'BRCM' u'ABT' u'EL' u'BLK' u'CTXS' u'APH' u'ESS' u'CTSH'
 u'CCE' u'AN' u'DHR' u'AMT' u'EXPD' u'ADBE' u'EQT' u'AMG' u'DRI' u'CTAS'
 u'DO' u'FOSL' u'COP' u'CCI' u'CAM' u'AKAM' u'AME' u'CAH' u'ESV' u'CB'
 u'BDX' u'DNB' u'FOXA' u'AMGN' u'EXC' u'COF' u'AA' u'EBAY' u'DOW' u'AON'
 u'DGX' u'FFIV' u'ETFC' u'BK' u'CINF' u'SPY']
[ 0.109  0.103  0.102  0.097  0.094  0.094  0.093  0.09   0.087  0.087
  0.085  0.084  0.084  0.084  0.083  0.083  0.083  0.082  0.082  0.081
  0.081  0.079  0.079  0.078  0.078  0.077  0.076  0.076  0.075  0.075
  0.075  0.075  0.074  0.074  0.073  0.073  0.073  0.073  0.073  0.073
  0.073  0.072  0.071  0.071  0.071  0.071  0.071  0.07   0.069  0.068
  0.068  0.065  0.065  0.057  0.057  0.053]


Interpreting the output

There are many metrics being reported in all the tear sheets above. At the top, there are tables that tell you about summary performance statistics like the Sharpe ratio, Sortino ratio, and worst drawdown periods. The following plots are hopefully pretty self-explanatory, but more information can be found on the pyfolio website.