backtrader.analyzers.drawdown module

DrawDown Analyzer Module - Drawdown statistics calculation.

This module provides analyzers for calculating drawdown statistics including current drawdown, maximum drawdown, and drawdown duration.

Classes:

DrawDown: Analyzer that calculates drawdown statistics. TimeDrawDown: Time-frame based drawdown analyzer.

示例

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.DrawDown, _name='dd')
>>> results = cerebro.run()
>>> print(results[0].analyzers.dd.get_analysis())
class backtrader.analyzers.drawdown.DrawDown[源代码]

基类:Analyzer

This analyzer calculates trading system drawdowns stats such as drawdown values in %s and in dollars, max drawdown in %s and in dollars, drawdown length and drawdown max length

Params:

  • fund (default: None)

    If None, the actual mode of the broker (fundmode - True/False) will be autodetected to decide if the returns are based on the total net asset value or on the fund value. See set_fundmode in the broker documentation

    Set it to True or False for a specific behavior

- ``get_analysis``

Returns a dictionary (with . notation support and subdctionaries) with drawdown stats as values, the following keys/attributes are available:

  • drawdown - drawdown value in 0.xx %

  • moneydown - drawdown value in monetary units

  • len - drawdown length

  • max.drawdown - max drawdown value in 0.xx %

  • max.moneydown - max drawdown value in monetary units

  • max.len - max drawdown length

params = (('fund', None),)
start()[源代码]

Initialize the analyzer at the start of the backtest.

Sets the fund mode based on parameters or broker settings.

create_analysis()[源代码]

Create the analysis result data structure.

Initializes the results dictionary with all drawdown metrics set to zero.

stop()[源代码]

Finalize the analysis when backtest ends.

Closes the results dictionary to prevent further modifications.

notify_fund(cash, value, fundvalue, shares)[源代码]

Update drawdown calculation with current fund values.

参数:
  • cash -- Current cash amount.

  • value -- Current portfolio value.

  • fundvalue -- Current fund value.

  • shares -- Number of fund shares.

next()[源代码]

Calculate drawdown for the current period.

Updates current and maximum drawdown values and lengths.

class backtrader.analyzers.drawdown.TimeDrawDown[源代码]

基类:TimeFrameAnalyzerBase

This analyzer calculates trading system drawdowns on the chosen timeframe which can be different from the one used in the underlying data Params:

  • timeframe (default: None) If None the timeframe of the 1st data in the system will be used

    Pass TimeFrame.NoTimeFrame to consider the entire dataset with no time constraints

  • compression (default: None)

    Only used for sub-day timeframes to, for example, work on an hourly timeframe by specifying "TimeFrame.Minutes" and 60 as compression

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

  • None

  • fund (default: None)

    If None, the actual mode of the broker (fundmode - True/False) will be autodetected to decide if the returns are based on the total net asset value or on the fund value. See set_fundmode in the broker documentation

    Set it to True or False for a specific behavior

- ``get_analysis``

Returns a dictionary (with . notation support and subdctionaries) with drawdown stats as values, the following keys/attributes are available:

  • drawdown - drawdown value in 0.xx %

  • maxdrawdown - drawdown value in monetary units

  • maxdrawdownperiod - drawdown length

- Those are available during runs as attributes
  • dd

  • maxdd

  • maxddlen

params = (('fund', None),)
__init__(*args, **kwargs)[源代码]

Initialize the TimeDrawDown analyzer.

参数:
  • *args -- Positional arguments.

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

start()[源代码]

Initialize the analyzer at the start of the backtest.

Sets the fund mode and initializes drawdown tracking variables.

on_dt_over()[源代码]

Called when a datetime period is over.

Updates drawdown calculations for the timeframe period.

stop()[源代码]

Finalize the analysis when backtest ends.

Stores the maximum drawdown and maximum drawdown period.