backtrader.indicator module¶
Backtrader Indicator Module.
This module provides the base Indicator class and related infrastructure for creating and managing technical analysis indicators. It replaces the metaclass-based approach with explicit inheritance and registration.
The Indicator class serves as the foundation for all technical indicators in backtrader, managing line data, minimum periods, and calculation logic.
- class backtrader.indicator.IndicatorRegistry[源代码]¶
基类:
objectRegistry to manage indicator classes and provide caching functionality.
This class replaces the metaclass-based indicator registration and caching mechanism from the original backtrader implementation.
- classmethod register(name, indicator_cls)[源代码]¶
Register an indicator class in the registry.
- 参数:
name -- Name of the indicator class
indicator_cls -- The indicator class to register
- classmethod usecache(onoff)[源代码]¶
Enable or disable indicator caching.
- 参数:
onoff -- If True, enable caching; if False, disable it
- classmethod get_cached_or_create(indicator_cls, *args, **kwargs)[源代码]¶
Get cached indicator instance or create new one.
- 参数:
indicator_cls -- The indicator class to instantiate
*args -- Positional arguments for the indicator
**kwargs -- Keyword arguments for the indicator
- 返回:
Cached indicator instance if available and caching enabled, otherwise a new indicator instance
- class backtrader.indicator.Indicator[源代码]¶
-
Base class for all technical indicators in Backtrader.
This class provides the foundation for creating custom indicators. It manages line data, minimum periods, and calculation logic. Indicators inherit from IndicatorBase and integrate with the LineIterator system for data flow.
- _ltype¶
Line type set to IndType (0) for indicators
- csv¶
Whether to output this indicator to CSV (default: False)
- aliased¶
Whether this indicator has an alias name
- csv = False¶
- __getitem__(ago)[源代码]¶
CRITICAL FIX: Forward item access to the first line (e.g., sma line)
For indicators with named lines like SMA (which has lines.sma), accessing indicator[0] should return the value from the first line, not the indicator's own array.
- aliased = False¶
- classmethod __init_subclass__(**kwargs)[源代码]¶
Handle subclass registration and initialization.
This method is called when a subclass of Indicator is created. It performs: 1. Lines creation using Lines infrastructure 2. Automatic registration in IndicatorRegistry 3. Alias handling for module-level access 4. next/once method setup for calculation modes
- 参数:
**kwargs -- Additional keyword arguments
- advance(size=1)[源代码]¶
Advance indicator lines when data length is less than clock length.
This method supports indicators with data feeds of different lengths (e.g., different timeframes).
- 参数:
size -- Number of steps to advance (default: 1)
- preonce_via_prenext(start, end)[源代码]¶
Implement preonce using prenext for batch calculation.
This is a generic implementation if prenext is overridden but preonce is not. It loops through the range and calls prenext for each step.
- 参数:
start -- Starting index
end -- Ending index
- oncestart_via_nextstart(start, end)[源代码]¶
Implement oncestart using nextstart for batch calculation.
This is used when nextstart is overridden but oncestart is not.
- 参数:
start -- Starting index
end -- Ending index
- once_via_next(start, end)[源代码]¶
Implement once using next for batch calculation.
This is used when next is overridden but once is not. It loops through the range and calls next for each step.
- 参数:
start -- Starting index
end -- Ending index
- frompackages = ()¶
- packages = ()¶