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[源代码]

基类:object

Registry 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 cleancache()[源代码]

Clear the indicator cache.

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[源代码]

基类:IndicatorBase

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

classmethod cleancache()[源代码]

Clear the indicator cache

classmethod usecache(onoff)[源代码]

Enable or disable caching

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 = ()
class backtrader.indicator.LinePlotterIndicatorBase[源代码]

基类:type

Base class for indicators that plot multiple lines.

Note: These classes are not currently used in the project. They are kept for compatibility with the original backtrader.

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

Create a new LinePlotterIndicator instance.

参数:
  • *args -- Positional arguments

  • **kwargs -- Keyword arguments, must include 'name'

返回:

(created_object, args, kwargs)

返回类型:

tuple

class backtrader.indicator.LinePlotterIndicator[源代码]

基类:Indicator, LinePlotterIndicatorBase

Indicator that plots multiple lines.

Note: This class is not currently used in the project.

frompackages = ()
packages = ()