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:
objectMixin to provide LineRoot functionality without metaclass
- class backtrader.lineroot.LineRoot[source]¶
Bases:
LineRootMixin,BaseMixinDefines 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¶
- 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
- 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
- class backtrader.lineroot.LineMultiple[source]¶
Bases:
LineRootBase 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.
- class backtrader.lineroot.LineSingle[source]¶
Bases:
LineRootBase 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