backtrader.lineroot module

LineRoot Module - Base classes for line-based data structures.

This module defines the base class LineRoot and derived classes LineSingle and LineMultiple that provide the foundation and interfaces for all line-based objects in backtrader.

Key Classes:

LineRoot: Common base for all line objects with period management. LineSingle: Base for single-line objects. LineMultiple: Base for multi-line objects. LineRootMixin: Mixin providing owner-finding functionality.

The module provides:
  • Period management (minperiod)

  • Iteration management

  • Operation management (binary/unary operations)

  • Rich comparison operators

示例

Period management: >>> obj.setminperiod(20) # Set minimum period to 20 >>> obj.updateminperiod(30) # Update to max(current, 30)

class backtrader.lineroot.LineRootMixin[源代码]

基类:object

Mixin to provide LineRoot functionality without metaclass

classmethod donew(*args, **kwargs)[源代码]

Create new instance with owner finding logic

class backtrader.lineroot.LineRoot[源代码]

基类:LineRootMixin, BaseMixin

Defines a common base and interfaces for Single and Multiple LineXXX instances

Period management Iteration management Operation (dual/single operand) Management Rich Comparison operator definition

IndType = 0
StratType = 1
ObsType = 2
qbuffer(savemem=0)[源代码]

Change the lines to implement a minimum size qbuffer scheme

minbuffer(size)[源代码]

Receive notification of how large the buffer must at least be

setminperiod(minperiod)[源代码]

Direct minperiod manipulation.It could be used, for example, by a strategy to not wait for all indicators to produce a value

updateminperiod(minperiod)[源代码]

Update the minperiod if needed. The minperiod will have been calculated elsewhere and has to take over if greater that self's

addminperiod(minperiod)[源代码]

Add a minperiod to own ... to be defined by subclasses

incminperiod(minperiod)[源代码]

Increment the minperiod with no considerations

prenext()[源代码]

It will be called during the "minperiod" phase of an iteration.

nextstart()[源代码]

It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next

next()[源代码]

Called to calculate values when the minperiod is over

preonce(start, end)[源代码]

It will be called during the "minperiod" phase of a "once" iteration

oncestart(start, end)[源代码]

It will be called when the minperiod phase is over for the 1st post-minperiod value

Only called once and defaults to automatically calling once

once(start, end)[源代码]

Called to calculate values at "once" when the minperiod is over

size()[源代码]

Return the number of lines in this object

class backtrader.lineroot.LineMultiple[源代码]

基类:LineRoot

Base class for objects containing multiple lines.

LineMultiple is the base class for objects that manage multiple line instances, such as indicators with multiple outputs. It provides common functionality for period management, staging, and operations across all contained lines.

lines

Collection of line objects managed by this instance.

_ltype

Line type indicator (None for base LineMultiple).

_clock

Clock reference for synchronization.

_lineiterators

Dictionary tracking registered lineiterators.

示例

>>> class MyIndicator(LineMultiple):
...     lines = ('output1', 'output2')
__init__()[源代码]

Initialize a LineMultiple instance.

Sets up the internal state for managing multiple lines, including line type indicator, lines collection, clock reference, and line iterator tracking.

Initializes:

_ltype: Line type indicator (None for base LineMultiple). lines: Collection of line objects (creates if not exists). _clock: Clock reference for synchronization. _lineiterators: Dictionary tracking registered lineiterators. _minperiod: Minimum period requirement (defaults to 1).

reset()[源代码]

Reset the line multiple to initial state.

Resets the operation stage to stage 1 and resets all managed lines to their initial state. This is typically called before starting a new backtest run.

addminperiod(minperiod)[源代码]

The passed minperiod is fed to the lines

incminperiod(minperiod)[源代码]

The passed minperiod is fed to the lines

qbuffer(savemem=0)[源代码]

Apply queued buffering to all managed lines.

Changes the lines to implement a minimum size qbuffer scheme to control memory usage during backtesting.

参数:

savemem -- Memory savings mode (0 = normal, higher values reduce memory usage at the cost of performance).

minbuffer(size)[源代码]

Set minimum buffer size for all managed lines.

Receives notification of how large the buffer must at least be and propagates this requirement to all managed lines.

参数:

size -- Minimum buffer size required.

class backtrader.lineroot.LineSingle[源代码]

基类:LineRoot

Base class for single-line objects.

LineSingle is the base class for objects that represent a single line of time-series data. It provides the foundational interface for period management and operations on individual lines.

示例

>>> line = LineSingle()
>>> line.addminperiod(5)  # Require 5 periods before valid
addminperiod(minperiod)[源代码]

Add the minperiod (substracting the overlapping 1 minimum period)

incminperiod(minperiod)[源代码]

Increment the minperiod with no considerations