PyFolio 和 Backtrader 能很好的兼容
Usage
-
Add the PyFolio analyzer to the cerebro mix:
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_items to retrieve the 4 components later needed for pyfolio:
returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() -
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
网友评论