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.
Example
>>> 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[source]¶
Bases:
AnalyzerThis 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. Seeset_fundmodein the broker documentationSet it to
TrueorFalsefor 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 unitslen- drawdown lengthmax.drawdown- max drawdown value in 0.xx %max.moneydown- max drawdown value in monetary unitsmax.len- max drawdown length
- params = (('fund', None),)¶
- start()[source]¶
Initialize the analyzer at the start of the backtest.
Sets the fund mode based on parameters or broker settings.
- create_analysis()[source]¶
Create the analysis result data structure.
Initializes the results dictionary with all drawdown metrics set to zero.
- stop()[source]¶
Finalize the analysis when backtest ends.
Closes the results dictionary to prevent further modifications.
- class backtrader.analyzers.drawdown.TimeDrawDown[source]¶
Bases:
TimeFrameAnalyzerBaseThis 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) IfNonethetimeframeof the 1st data in the system will be usedPass
TimeFrame.NoTimeFrameto consider the entire dataset with no time constraintscompression(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. Seeset_fundmodein the broker documentationSet it to
TrueorFalsefor 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 unitsmaxdrawdownperiod- drawdown length
- - Those are available during runs as attributes
ddmaxddmaxddlen
- params = (('fund', None),)¶
- __init__(*args, **kwargs)[source]¶
Initialize the TimeDrawDown analyzer.
- Parameters:
*args – Positional arguments.
**kwargs – Keyword arguments for analyzer parameters.
- start()[source]¶
Initialize the analyzer at the start of the backtest.
Sets the fund mode and initializes drawdown tracking variables.