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[源代码]¶
基类:
objectMixin to provide LineRoot functionality without metaclass
- class backtrader.lineroot.LineRoot[源代码]¶
-
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¶
- 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
- 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
- class backtrader.lineroot.LineMultiple[源代码]¶
基类:
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.
示例
>>> 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.
- class backtrader.lineroot.LineSingle[源代码]¶
基类:
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.
示例
>>> line = LineSingle() >>> line.addminperiod(5) # Require 5 periods before valid