PyFolio Overview
Note
As of (at least) 2017-07-25 thepyfolio APIs have changed andcreate_full_tear_sheet no longer has agross_lev as a namedargument.
Consequently the sample for integration doesn’t work
Quoting from the mainpyfolio page athttp://quantopian.github.io/pyfolio/:
pyfolio is a Python library for performance and risk analysis of financialportfolios developed by Quantopian Inc. It works well with the Zipline opensource backtesting libraryAnd now it works also well withbacktrader. What’s needed:
pyfolioobviouslyAnd its dependencies (things like
pandas,seaborn…)Note
During the integration with version
0.5.1, an update to the mostrecent packages of the dependencies was needed, likeseabornfromthe previously installed0.7.0-devto0.7.1, apparently due tothe absence of the methodswarmplot
Usage
Add the
PyFolioanalyzer to thecerebromix:cerebro.addanalyzer(bt.analyzers.PyFolio)Run and retrieve the 1st strategy:
strats=cerebro.run()strat0=strats[0]Retrieve the analyzer using whatever name you gave to it or the default name it will be given to it:
pyfolio. For example:pyfolio=strats.analyzers.getbyname('pyfolio')Use the analyzer method
get_pf_itemsto retrieve the 4 components later needed forpyfolio:returns,positions,transactions,gross_lev=pyfoliozer.get_pf_items()!!! note
The integration was done looking at test samples available with`pyfolio` and the same headers (or absence of) has been replicatedWork with
pyfolio(this is already outside of thebacktrader ecosystem)
Some usage notes not directly related tobacktrader
pyfolioautomatic plotting works outside of aJupyter Notebook, butit works best insidepyfoliodata tables’ output seems to barely work outside of aJupyter Notebook. It works inside theNotebook
The conclusion is easy if working withpyfolio is wished:work inside aJupyter Notebook
Sample Code
The code would look like this:
...cerebro.addanalyzer(bt.analyzers.PyFolio,_name='pyfolio')...results=cerebro.run()strat=results[0]pyfoliozer=strat.analyzers.getbyname('pyfolio')returns,positions,transactions,gross_lev=pyfoliozer.get_pf_items()......# pyfolio showtimeimportpyfolioaspfpf.create_full_tear_sheet(returns,positions=positions,transactions=transactions,gross_lev=gross_lev,live_start_date='2005-05-01',# This date is sample specificround_trips=True)# At this point tables and chart will show upReference
Look into the Analyzers Reference for thePyFolio analyzer and whichanalyzers it uses internally