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.
示例
Using indicator functions: >>> from backtrader.functions import And, Or >>> condition = And(indicator1 > indicator2, indicator3 > 0)
- class backtrader.functions.List[源代码]¶
基类:
listList 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[源代码]¶
基类:
LineActionsBase 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[源代码]¶
基类:
LogicThis 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.
- once(start, end)[源代码]¶
Calculate all values at once with zero-division protection.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.DivZeroByZero[源代码]¶
基类:
LogicThis 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.
- once(start, end)[源代码]¶
Calculate all values at once with zero/zero indetermination protection.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.Cmp[源代码]¶
基类:
LogicComparison 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.
- once(start, end)[源代码]¶
Calculate all comparison values at once.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.CmpEx[源代码]¶
基类:
LogicExtended 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.
- once(start, end)[源代码]¶
Calculate all extended comparison values at once.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.If[源代码]¶
基类:
LogicConditional 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.
- once(start, end)[源代码]¶
Calculate all conditional values at once.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.MultiLogic[源代码]¶
基类:
LogicBase 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.
- once(start, end)[源代码]¶
Apply the logic function to all values across the specified range.
- 参数:
start -- Starting index for calculation.
end -- Ending index for calculation.
- frompackages = ()¶
- packages = ()¶
- class backtrader.functions.MultiLogicReduce[源代码]¶
基类:
MultiLogicMultiLogic 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[源代码]¶
-
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[源代码]¶
-
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[源代码]¶
-
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[源代码]¶
基类:
MultiLogicMaximum 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[源代码]¶
基类:
MultiLogicMinimum 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[源代码]¶
基类:
MultiLogicSum 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 = ()¶