backtrader.analyzers.pyfolio module

PyFolio Analyzer Module - PyFolio integration.

This module provides the PyFolio analyzer for collecting data compatible with the pyfolio library for performance analysis.

Classes:

PyFolio: Analyzer that collects data for pyfolio.

示例

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
>>> results = cerebro.run()
>>> pyfolio_data = results[0].analyzers.pyfolio.get_analysis()
class backtrader.analyzers.pyfolio.PyFolio[源代码]

基类:Analyzer

This analyzer uses 4 children analyzers to collect data and transforms it in to a data set compatible with pyfolio

Children Analyzer

  • TimeReturn

    Used to calculate the returns of the global portfolio value

  • PositionsValue

    Used to calculate the value of the positions per data. It sets the headers and cash parameters to True

  • Transactions

    Used to record each transaction on a data (size, price, value). Sets the headers parameter to True

  • GrossLeverage

    Keeps track of the gross leverage (how much the strategy is invested)

Params:

These are passed transparently to the children

  • timeframe (default: bt.TimeFrame.Days)

    If None then the timeframe of the 1st data of the system will be used

  • compression (default: 1`)

    If None then the compression of the 1st data of the system will be used

Both timeframe and compression are set following the default behavior of pyfolio which is working with daily data and upsample it to obtaine values like yearly returns.

- get_analysis

Returns a dictionary with returns as values and the datetime points for each return as keys

params = (('timeframe', 5), ('compression', 1))
__init__(*args, **kwargs)[源代码]

Initialize the PyFolio analyzer.

Creates child analyzers (TimeReturn, PositionsValue, Transactions, GrossLeverage) to collect data for pyfolio integration.

参数:
  • *args -- Positional arguments.

  • **kwargs -- Keyword arguments for analyzer parameters.

stop()[源代码]

Collect results from child analyzers when backtest ends.

Gathers returns, positions, transactions, and gross leverage data from the child analyzers for pyfolio processing.

get_pf_items()[源代码]
Returns a tuple of 4 elements which can be used for further processing with

pyfolio

returns, positions, transactions, gross_leverage

Because the objects are meant to be used as direct input to pyfolio this method makes a local import of pandas to convert the internal backtrader results to pandas DataFrames which is the expected input by, for example, pyfolio.create_full_tear_sheet

The method will break if pandas is not installed