backtrader.analyzers.periodstats module

Period Statistics Analyzer Module - Basic statistics by period.

This module provides the PeriodStats analyzer for calculating basic statistics (average, standard deviation, etc.) for a given timeframe.

Classes:

PeriodStats: Analyzer that calculates period statistics.

Example

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.PeriodStats, _name='stats')
>>> results = cerebro.run()
>>> print(results[0].analyzers.stats.get_analysis())
class backtrader.analyzers.periodstats.PeriodStats[source]

Bases: Analyzer

Calculates basic statistics for given timeframe

Params:

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

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

  • compression (default: 1)

    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 first data in the system will be used

  • 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 containing the keys:

  • average

  • stddev

  • positive

  • negative

  • nochange

  • best

  • worst

If the parameter zeroispos is set to True, periods with no change will be counted as positive

params = (('timeframe', 8), ('compression', 1), ('zeroispos', False), ('fund', None))
__init__(*args, **kwargs)[source]

Initialize the PeriodStats analyzer.

Parameters:
  • *args – Positional arguments.

  • **kwargs – Keyword arguments for analyzer parameters.

stop()[source]

Calculate period statistics when backtest ends.

Computes average, standard deviation, and count of positive/negative/ zero returns for the specified timeframe period.