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 [3]:
# Get backtest object
backtest = get_backtest('584af2392c655863d9ff9e95')

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


Backtest Months: 47
Performance statistics Backtest
annual_return -0.03
annual_volatility 0.13
sharpe_ratio -0.13
calmar_ratio -0.09
stability_of_timeseries 0.19
max_drawdown -0.29
omega_ratio 0.98
sortino_ratio -0.17
skew -0.90
kurtosis 5.22
tail_ratio 0.98
common_sense_ratio 0.96
information_ratio -0.04
alpha 0.02
beta -0.30
Worst Drawdown Periods net drawdown in % peak date valley date recovery date duration
0 29.12 2015-08-20 2016-12-06 NaT NaN
1 14.11 2015-01-30 2015-05-12 2015-07-24 126
2 7.19 2014-04-28 2014-06-27 2014-09-22 106
3 6.79 2013-04-22 2013-07-22 2013-10-18 130
4 6.13 2014-10-13 2014-10-16 2015-01-13 67

[-0.017 -0.04 ]
Stress Events mean min max
Apr14 0.05% -1.19% 1.46%
Oct14 0.19% -2.55% 1.88%
Fall2015 0.03% -3.61% 1.28%
New Normal -0.01% -5.48% 2.90%
Top 10 long positions of all time max
CRC-48073 2.62%
SIG-9774 2.60%
CJES-48821 2.58%
CYBR-47779 2.58%
SWN-7244 2.53%
PBYI-42689 2.53%
JUNO-48317 2.52%
HOS-26150 2.48%
PTEN-10254 2.47%
AGN-205 2.45%
Top 10 short positions of all time max
UPL-22406 -7.90%
PVA-6258 -4.63%
PTCT-44955 -4.55%
GEVA-42112 -4.41%
CSTM-44780 -4.36%
SGY-9458 -4.10%
EXXI-34443 -3.94%
LINE-27993 -3.74%
VNR-34931 -3.58%
BTU-22660 -3.53%
Top 10 positions of all time max
UPL-22406 7.90%
PVA-6258 4.63%
PTCT-44955 4.55%
GEVA-42112 4.41%
CSTM-44780 4.36%
SGY-9458 4.10%
EXXI-34443 3.94%
LINE-27993 3.74%
VNR-34931 3.58%
BTU-22660 3.53%
All positions ever held max
UPL-22406 7.90%
PVA-6258 4.63%
PTCT-44955 4.55%
GEVA-42112 4.41%
CSTM-44780 4.36%
SGY-9458 4.10%
EXXI-34443 3.94%
LINE-27993 3.74%
VNR-34931 3.58%
BTU-22660 3.53%
MGT-27925 3.51%
DGLY-29117 3.42%
CVEO-46939 3.29%
WLT-13771 3.29%
ARNA-21724 3.27%
ADXS-40992 3.24%
NVAX-14112 3.23%
BCEI-42272 3.21%
LNCO-43513 3.17%
SUNE-13306 3.14%
GNW-26323 3.13%
CJES-48821 3.06%
MHR-32541 3.05%
HERO-27747 3.03%
BBG-26865 3.02%
FXCM-40531 3.01%
ACI-88 2.99%
ANR-27035 2.98%
DNR-15789 2.94%
VRTX-8045 2.91%
... ...
AOS-6949 0.01%
EXC-22114 0.01%
AKR-19185 0.01%
SO-7011 0.01%
WTR-6193 0.01%
OUT-46644 0.01%
WEX-27045 0.01%
AVT-661 0.01%
HBHC-3476 0.01%
CYN-2052 0.01%
SPGI-4849 0.01%
LM-4488 0.01%
CTL-1960 0.01%
NJR-5326 0.01%
PCAR-5787 0.01%
HCN-3488 0.01%
CHD-1482 0.01%
TJX-7457 0.01%
VTR-18821 0.01%
KRC-16374 0.01%
LXFT-44986 0.01%
BOH-1023 0.01%
PB-19509 0.01%
ATO-612 0.01%
ADTN-11718 0.00%
LSCC-4549 0.00%
FBHS-41928 0.00%
TNET-46633 0.00%
WEC-8140 0.00%
LMCA-43919 0.00%

1747 rows × 1 columns