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

Example

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

class backtrader.lineroot.LineRootMixin[source]

Bases: object

Mixin to provide LineRoot functionality without metaclass

classmethod donew(*args, **kwargs)[source]

Create new instance with owner finding logic

class backtrader.lineroot.LineRoot[source]

Bases: 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)[source]

Change the lines to implement a minimum size qbuffer scheme

minbuffer(size)[source]

Receive notification of how large the buffer must at least be

setminperiod(minperiod)[source]

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

updateminperiod(minperiod)[source]

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

addminperiod(minperiod)[source]

Add a minperiod to own … to be defined by subclasses

incminperiod(minperiod)[source]

Increment the minperiod with no considerations

prenext()[source]

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

nextstart()[source]

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()[source]

Called to calculate values when the minperiod is over

preonce(start, end)[source]

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

oncestart(start, end)[source]

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)[source]

Called to calculate values at “once” when the minperiod is over

size()[source]

Return the number of lines in this object

class backtrader.lineroot.LineMultiple[source]

Bases: 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.

Example

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

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()[source]

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)[source]

The passed minperiod is fed to the lines

incminperiod(minperiod)[source]

The passed minperiod is fed to the lines

qbuffer(savemem=0)[source]

Apply queued buffering to all managed lines.

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

Parameters:

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

minbuffer(size)[source]

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.

Parameters:

size – Minimum buffer size required.

class backtrader.lineroot.LineSingle[source]

Bases: 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.

Example

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

Add the minperiod (substracting the overlapping 1 minimum period)

incminperiod(minperiod)[source]

Increment the minperiod with no considerations