backtrader.analyzers.positions module

Positions Analyzer Module - Position value tracking.

This module provides the PositionsValue analyzer for tracking the value of positions across all data feeds.

Classes:

PositionsValue: Analyzer that reports position values over time.

Example

>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.PositionsValue, _name='posval')
>>> results = cerebro.run()
>>> print(results[0].analyzers.posval.get_analysis())
class backtrader.analyzers.positions.PositionsValue[source]

Bases: Analyzer

This analyzer reports the value of the positions of the current set of datas

Params:

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

  • 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

  • headers (default: False)

    Add an initial key to the dictionary holding the results with the names of the data ‘Datetime’ as a key

  • cash (default: False)

    Include the actual cash as an extra position (for the header ‘cash’ will be used as name)

- get_analysis

Returns a dictionary with returns as values and the datetime points for each return as keys

params = (('headers', False), ('cash', False))
__init__(*args, **kwargs)[source]

Initialize the PositionsValue analyzer.

Parameters:
  • *args – Positional arguments.

  • **kwargs – Keyword arguments for analyzer parameters.

start()[source]

Initialize the analyzer at the start of the backtest.

Sets up headers for the results dictionary and determines whether to use date or datetime as the key based on the timeframe.

next()[source]

Record position values for the current bar.

Gets the value of positions for each data feed and optionally includes cash. Stores results keyed by date or datetime.