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引用,以及它在內部使用哪些分析器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM