backtrader.functions module

Functions Module - Common operations on line objects.

This module provides utility functions and classes for performing operations on line objects. It includes arithmetic operations with zero-division protection, logical operations, comparison operations, and mathematical functions.

Classes:

Logic: Base class for logical operations on lines. DivByZero: Division with zero-division protection. DivZeroByZero: Division with zero/zero indetermination protection. And/Or/Not/If/Max/Min/MinN/MaxN: Logical and comparison operations. Sum/Average/StdDev/TSMean: Statistical operations.

Example

Using indicator functions: >>> from backtrader.functions import And, Or >>> condition = And(indicator1 > indicator2, indicator3 > 0)

class backtrader.functions.List[source]

Bases: list

List subclass that uses hash equality for contains checks.

This class overrides __contains__ to check if any element has the same hash value as the target, rather than using identity comparison.

class backtrader.functions.Logic[source]

Bases: LineActions

Base class for logical operations on line objects.

Handles argument conversion to arrays and manages minperiod propagation from operands.

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

frompackages = ()
packages = ()
class backtrader.functions.DivByZero[source]

Bases: Logic

This operation is a Lines object and fills it values by executing a division on the numerator / denominator arguments and avoiding a division by zero exception by checking the denominator

Params:
  • a: numerator (numeric or iterable object … mostly a Lines object)

  • b: denominator (numeric or iterable object … mostly a Lines object)

  • zero (def: 0.0): value to apply if division by zero is raised

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

next()[source]

Calculate the next value with zero-division protection.

once(start, end)[source]

Calculate all values at once with zero-division protection.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.DivZeroByZero[source]

Bases: Logic

This operation is a Lines object and fills it values by executing a division on the numerator / denominator arguments and avoiding a division by zero exception or an indetermination by checking the denominator/numerator pair

Params:
  • a: numerator (numeric or iterable object … mostly a Lines object)

  • b: denominator (numeric or iterable object … mostly a Lines object)

  • single (def: +inf): value to apply if division is x / 0

  • dual (def: 0.0): value to apply if division is 0 / 0

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

next()[source]

Calculate the next value with zero/zero indetermination protection.

once(start, end)[source]

Calculate all values at once with zero/zero indetermination protection.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.Cmp[source]

Bases: Logic

Comparison operation that returns comparison results.

Compares two line objects and returns standard comparison values: -1 if a < b, 0 if a == b, 1 if a > b.

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

next()[source]

Calculate the next comparison value.

once(start, end)[source]

Calculate all comparison values at once.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.CmpEx[source]

Bases: Logic

Extended comparison operation with three possible return values.

Compares two line objects and returns one of three values based on the comparison result: - r1 if a < b - r2 if a == b - r3 if a > b

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

next()[source]

Calculate the next extended comparison value.

once(start, end)[source]

Calculate all extended comparison values at once.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.If[source]

Bases: Logic

Conditional selection operation.

Returns a value from a or b based on a condition: - Returns a if condition is True - Returns b if condition is False

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

next()[source]

Calculate the next conditional value.

once(start, end)[source]

Calculate all conditional values at once.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.MultiLogic[source]

Bases: Logic

Base class for operations that apply a function to multiple arguments.

The flogic attribute should be set to a callable that takes an iterable of values and returns a single result.

next()[source]

Apply the logic function to current values from all arguments.

once(start, end)[source]

Apply the logic function to all values across the specified range.

Parameters:
  • start – Starting index for calculation.

  • end – Ending index for calculation.

frompackages = ()
packages = ()
class backtrader.functions.MultiLogicReduce[source]

Bases: MultiLogic

MultiLogic that uses functools.reduce for cumulative operations.

This class applies a reduction function cumulatively to all arguments, combining them into a single result.

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

frompackages = ()
packages = ()
class backtrader.functions.Reduce[source]

Bases: MultiLogicReduce

Generic reduction operation with a custom function.

Allows any reduction function to be applied to the arguments.

__init__(*args, **kwargs)

Initialize the LineBuffer instance.

Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.

frompackages = ()
packages = ()
class backtrader.functions.And[source]

Bases: MultiLogicReduce

Logical AND operation across all arguments.

Returns True only if all input values are truthy.

static flogic(x, y)

Logical AND operation for reduction.

frompackages = ()
packages = ()
class backtrader.functions.Or[source]

Bases: MultiLogicReduce

Logical OR operation across all arguments.

Returns True if any input value is truthy.

static flogic(x, y)

Logical OR operation for reduction.

frompackages = ()
packages = ()
class backtrader.functions.Max[source]

Bases: MultiLogic

Maximum operation across all arguments.

Returns the maximum value from all input lines.

flogic()

max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument.

frompackages = ()
packages = ()
class backtrader.functions.Min[source]

Bases: MultiLogic

Minimum operation across all arguments.

Returns the minimum value from all input lines.

flogic()

min(iterable, *[, default=obj, key=func]) -> value min(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its smallest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the smallest argument.

frompackages = ()
packages = ()
class backtrader.functions.Sum[source]

Bases: MultiLogic

Sum operation across all arguments.

Returns the sum of all input values using math.fsum for better floating point precision.

flogic(seq, /)

Return an accurate floating point sum of values in the iterable seq.

Assumes IEEE-754 floating point arithmetic.

frompackages = ()
packages = ()
class backtrader.functions.Any[source]

Bases: MultiLogic

Any operation across all arguments.

Returns True if any input value is truthy.

flogic(iterable, /)

Return True if bool(x) is True for any x in the iterable.

If the iterable is empty, return False.

frompackages = ()
packages = ()
class backtrader.functions.All[source]

Bases: MultiLogic

All operation across all arguments.

Returns True only if all input values are truthy.

flogic(iterable, /)

Return True if bool(x) is True for all values x in the iterable.

If the iterable is empty, return True.

frompackages = ()
packages = ()