backtrader.analyzers.logreturnsrolling module

LogReturnsRolling Analyzer Module - Rolling log returns calculation.

This module provides the LogReturnsRolling analyzer for calculating rolling log returns over a specified timeframe.

Classes:

LogReturnsRolling: Analyzer that calculates rolling log returns.

示例

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.LogReturnsRolling)
class backtrader.analyzers.logreturnsrolling.LogReturnsRolling[源代码]

基类:TimeFrameAnalyzerBase

This analyzer calculates rolling returns for a given timeframe and compression

Params:

  • timeframe (default: None) 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: 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 first data in the system will be used

  • data (default: None)

    Reference asset to track instead of the portfolio value.

    .note: this data must have been added to a cerebro instance with

    addata, resampledata or replaydata

  • firstopen (default: True)

    When tracking the returns of data the following is done when crossing a timeframe boundary, for example, Years:

    • Last close the previous year is used as the reference price to see the return in the current year

    The problem is the first calculation, because the data has** no previous** closing price.As such, and when this parameter is True, the opening price will be used for the first calculation.

    This requires the data feed to have an open price (for close the standard [0] notations will be used without a reference to a field price)

    Else the initial close 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 with returns as values and the datetime points for each return as keys

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

Initialize the LogReturnsRolling 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 the rolling value queue with size controlled by compression parameter.

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

Update current value from fund notification.

参数:
  • cash -- Current cash amount.

  • value -- Current portfolio value.

  • fundvalue -- Current fund value.

  • shares -- Number of fund shares.

on_dt_over()[源代码]

Handle timeframe boundary crossing.

Updates the rolling value queue when entering a new period.

next()[源代码]

Calculate and store the rolling log return for the current period.

Calculates log(current_value / oldest_value) from the rolling window.