Movatterモバイル変換


[0]ホーム

URL:


Skip to content

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 library

And now it works also well withbacktrader. What’s needed:

  • pyfolio obviously

  • And its dependencies (things likepandas,seaborn …)

    Note

    During the integration with version0.5.1, an update to the mostrecent packages of the dependencies was needed, likeseaborn fromthe previously installed0.7.0-dev to0.7.1, apparently due tothe absence of the methodswarmplot

Usage

  1. Add thePyFolio analyzer to thecerebro mix:

    cerebro.addanalyzer(bt.analyzers.PyFolio)
  2. Run and retrieve the 1st strategy:

    strats=cerebro.run()strat0=strats[0]
  3. 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')
  4. Use the analyzer methodget_pf_items to 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 replicated
  5. Work withpyfolio (this is already outside of thebacktrader ecosystem)

Some usage notes not directly related tobacktrader

  • pyfolio automatic plotting works outside of aJupyter Notebook, butit works best inside

  • pyfolio data 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 up

Reference

Look into the Analyzers Reference for thePyFolio analyzer and whichanalyzers it uses internally


[8]ページ先頭

©2009-2025 Movatter.jp