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.
Example
>>> 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[source]¶
Bases:
AnalyzerThis analyzer uses 4 children analyzers to collect data and transforms it in to a data set compatible with
pyfolioChildren Analyzer
TimeReturnUsed to calculate the returns of the global portfolio value
PositionsValueUsed to calculate the value of the positions per data. It sets the
headersandcashparameters toTrueTransactionsUsed to record each transaction on a data (size, price, value). Sets the
headersparameter toTrueGrossLeverageKeeps 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
Nonethen the timeframe of the 1st data of the system will be usedcompression (default: 1`)
If
Nonethen the compression of the 1st data of the system will be used
Both
timeframeandcompressionare set following the default behavior ofpyfoliowhich 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)[source]¶
Initialize the PyFolio analyzer.
Creates child analyzers (TimeReturn, PositionsValue, Transactions, GrossLeverage) to collect data for pyfolio integration.
- Parameters:
*args – Positional arguments.
**kwargs – Keyword arguments for analyzer parameters.
- stop()[source]¶
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()[source]¶
- Returns a tuple of 4 elements which can be used for further processing with
pyfolioreturns, positions, transactions, gross_leverage
Because the objects are meant to be used as direct input to
pyfoliothis method makes a local import ofpandasto convert the internal backtrader results to pandas DataFrames which is the expected input by, for example,pyfolio.create_full_tear_sheetThe method will break if
pandasis not installed