Backtrader中文笔记之PyFolio Overview

Note

As of (at least) 2017-07-25 the pyfolio APIs have changed and create_full_tear_sheet no longer has a gross_lev as a named argument.

Consequently the sample for integration doesn’t work

截至(至少)2017-07-25,pyfolio API已经发生了变化,create_full_-tear_sheet不再以总水平作为命名参数。

因此,用于集成的示例不起作用

Quoting from the main pyfolio page at http://quantopian.github.io/pyfolio/:

引用pyfolio主页面http://quantopian.github.io/pyfolio/:

pyfolio is a Python library for performance and risk analysis of financial
portfolios developed by Quantopian Inc. It works well with the Zipline open
source backtesting library

 pyfolio是一个Python库,用于财务性能和风险分析,由Quantopian公司开发的投资组合。它与Zipline open配合良好源代码回溯测试库

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

现在它也很好地与backtrader合作。需要什么:

  • pyfolio obviously

  • 很明显是pyfolio
  • And its dependencies (things like pandas, seaborn …)

  以及它的依附关系(things like pandas, seaborn …)

 

Note

During the integration with version 0.5.1, an update to the most recent packages of the dependencies was needed, like seaborn from the previously installed 0.7.0-dev to 0.7.1, apparently due to the absence of the method swarmplot

在与版本0.5.1的集成过程中,需要对依赖项的最新包进行更新,比如seaborn从以前安装的0.7.0-dev升级到0.7.1,这显然是因为缺少swarmplot方法

 

Usage

Add the PyFolio analyzer to the cerebro mix:

 将PyFolio分析仪添加到大脑中:
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检索分析器。例如:

pyfoliozer = strats.analyzers.getbyname('pyfolio')

 Use the analyzer method get_pf_items to retrieve the 4 components later needed for pyfolio:

 使用analyzer方法获取pyfolio所需的4个组件
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

通过对“pyfolio”可用的测试样本进行集成,复制了相同的头文件(或者没有)

Work with pyfolio (this is already outside of the backtrader ecosystem)

与pyfolio合作(这已经超出了backtrader生态系统)

Some usage notes not directly related to backtrader

 一些与backtrader没有直接关系的使用说明
  • pyfolio automatic plotting works outside of a Jupyter Notebook, but it works best inside

  • pyfolio自动绘图功能在Jupyter笔记本电脑外部工作,但在内部效果最好
  • pyfolio data tables’ output seems to barely work outside of a Jupyter Notebook. It works inside the Notebook

  • pyfolio数据表的输出在Jupyter笔记本之外似乎几乎不起作用。它在笔记本里工作

The conclusion is easy if working with pyfolio is wished: work inside a Jupyter Notebook

如果希望使用pyfolio,那么结论很简单:在Jupyter笔记本中工作

 

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 showtime
import pyfolio as pf
pf.create_full_tear_sheet(
    returns,
    positions=positions,
    transactions=transactions,
    gross_lev=gross_lev,
    live_start_date='2005-05-01',  # This date is sample specific
    round_trips=True)

# At this point tables and chart will show up

 

Reference

Look into the Analyzers Reference for the PyFolio analyzer and which analyzers it uses internally

 查看PyFolio分析器的Analyzers引用,以及它在内部使用哪些分析器
posted @ 2020-09-10 16:47  就是想学习  阅读(2669)  评论(0编辑  收藏  举报