9.2 PyFolio Overvies

2020-04-28  本文已影响0人  wanggs66

PyFolio 和 Backtrader 能很好的兼容

Usage

  1. Add the PyFolio analyzer to the cerebro 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 method get_pf_items to retrieve the 4 components later needed for pyfolio:
    returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()

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

[Notes] if working with pyfolio is wished: work inside a Jupyter Notebook

Example:

...
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)

通过以上方法可以利用pyfolio中的工具画出更多可视化的图片,帮助分析

# At this point tables and chart will show up
上一篇下一篇

猜你喜欢

热点阅读