backtrader.linebuffer module¶
LineBuffer Module - Circular buffer storage for time-series data.
This module provides the LineBuffer class which implements a circular buffer for storing time-series data. The buffer allows efficient operations like appending, forwarding, rewinding, and resetting.
- Key Features:
Index 0 always points to the current active value
Positive indices fetch past values (left-hand side)
Negative indices fetch future values (right-hand side)
Automatic memory management with qbuffer
Line bindings for automatic value propagation
- Classes:
LineBuffer: Core circular buffer implementation. LineActions: Base class for line objects with multiple lines. LineActionsMixin: Mixin providing line operations. LineActionsCache: Cache system for performance optimization. PseudoArray: Wrapper for non-array iterables. LinesOperation: Operations on multiple lines. LineOwnOperation: Operations on owned lines.
Example
Basic buffer usage: >>> buf = LineBuffer() >>> buf.home() # Reset to beginning >>> buf.forward() # Move to next position >>> buf[0] = 100.0 # Set current value >>> print(buf[0]) # Get current value 100.0 >>> print(buf[-1]) # Get previous value
- class backtrader.linebuffer.LineBuffer[source]¶
Bases:
LineSingle,LineRootMixinLineBuffer defines an interface to an “array.array” (or list) in which index 0 points to the item which is active for input and output.
Positive indices fetch values from the past (left-hand side) Negative indices fetch values from the future (if the array has been extended on the right-hand side)
With this behavior, no index has to be passed around to entities which have to work with the current value produced by other entities: the value is always reachable at “0”.
Likewise, storing the current value produced by “self” is done at 0.
Additional operations to move the pointer (home, forward, extend, rewind, advance getzero) are provided
The class can also hold “bindings” to other LineBuffers. When a value is set in this class, it will also be set in the binding.
- UnBounded = 0¶
- QBuffer = 1¶
- __init__()[source]¶
Initialize the LineBuffer instance.
Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.
- get_idx()[source]¶
Get the current index position.
- Returns:
The current index in the buffer.
- Return type:
- set_idx(idx, force=False)[source]¶
Set the index position.
- Parameters:
idx – The new index value.
force – If True, force set even in QBuffer mode at lenmark.
Note
In QBuffer mode, when at lenmark, the index stays at 0 unless force is True. This allows resampling operations.
- property idx¶
Get the current index position.
- Returns:
The current index in the buffer.
- Return type:
- qbuffer(savemem=0, extrasize=0)[source]¶
Enable queued buffer mode for memory-efficient storage.
- Parameters:
savemem – Memory saving mode (0=normal, >0=enable cache mode).
extrasize – Extra buffer size for resampling/replay operations.
Note
In QBuffer mode, only the last maxlen values are kept, reducing memory usage for long backtests.
- getindicators()[source]¶
Get list of indicators using this line buffer.
- Returns:
Empty list for base LineBuffer (override in subclasses).
- Return type:
- minbuffer(size)[source]¶
The linebuffer must guarantee the minimum requested size to be available.
In non-dqbuffer mode, this is always true (of course, until data is filled at the beginning, there are fewer values, but minperiod in the framework should account for this.
In dqbuffer mode, the buffer has to be adjusted for this if currently less than requested
- __len__()[source]¶
Return the linebuffer’s length counter.
Performance optimization: Restore master branch’s simple implementation - Directly return self.lencount (pre-calculated length value) - Remove all recursion checks, hasattr calls and complex logic - Performance improvement: from 0.611s to ~0.05s (92% improvement)
- buflen()[source]¶
Real data that can be currently held in the internal buffer
The internal buffer can be longer than the actual stored data to allow for “lookahead” operations. The real amount of data that is held/can be held in the buffer is returned
- __getitem__(ago)[source]¶
Get the value at a specified offset - optimized for hot path.
- Parameters:
ago (int) – Relative offset from current index (0=current, -1=previous, 1=next)
- Returns:
Value at the specified position
- get(ago=0, size=1)[source]¶
Returns a slice of the array relative to ago
- Keyword Arguments:
ago (int) – Point of the array to which size will be added
size (to return the slice) – size of the slice to return,
negative (can be positive or)
If size is positive ago will mark the end of the iterable and vice versa if size is negative
- Returns:
A slice of the underlying buffer
- getzeroval(idx=0)[source]¶
Returns a single value of the array relative to the real zero of the buffer
- getzero(idx=0, size=1)[source]¶
Returns a slice of the array relative to the real zero of the buffer
- __setitem__(ago, value)[source]¶
Sets a value at position “ago” and executes any associated bindings
- Keyword Arguments:
ago (int) – Point of the array to which size will be added to return
slice (the)
value (variable) – value to be set
Performance optimization: Use pre-calculated flags to avoid repeated hasattr and string operations
- set(value, ago=0)[source]¶
Sets a value at position “ago” and executes any associated bindings
- Keyword Arguments:
value (variable) – value to be set
ago (int) – Point of the array to which size will be added to return
slice (the)
PERF: Uses pre-calculated _is_datetime_line and _default_value flags instead of hasattr/isinstance checks on every call.
- home()[source]¶
Rewinds the logical index to the beginning
The underlying buffer remains untouched and the actual len can be found out with buflen
- forward(value=nan, size=1)[source]¶
Moves the logical index forward and enlarges the buffer as much as needed
- Keyword Arguments:
value (variable) – value to be set in new positions
size (int) – How many extra positions to enlarge the buffer
- backwards(size=1, force=False)[source]¶
Moves the logical index backwards and reduces the buffer as much as needed
- safe_backwards(size=1)[source]¶
Safely move the index backwards without raising errors.
- Parameters:
size – Number of positions to move backwards.
- Returns:
True if index is still >= 0 after moving, False otherwise.
- Return type:
- rewind(size=1)[source]¶
Rewind the buffer by decreasing idx and lencount.
- Parameters:
size – Number of positions to rewind.
- extend(value=nan, size=0)[source]¶
Extends the underlying array with positions that the index will not reach
- Keyword Arguments:
value (variable) – value to be set in new positins
size (int) – How many extra positions to enlarge the buffer
The purpose is to allow for lookahead operations or to be able to set values in the buffer “future”
- addbinding(binding)[source]¶
Adds another line binding
- Keyword Arguments:
binding (LineBuffer) – another line that must be set when this line
value (becomes a)
- plot(idx=0, size=None)[source]¶
Returns a slice of the array relative to the real zero of the buffer
- Keyword Arguments:
This is a variant of getzero that unless told otherwise returns the entire buffer, which is usually the idea behind plottint (all must be plotted)
- Returns:
A slice of the underlying buffer
- bind2lines(binding=0)[source]¶
Stores a binding to another line. “Binding” can be an index or a name
- bind2line(binding=0)¶
Stores a binding to another line. “Binding” can be an index or a name
- __call__(ago=None)[source]¶
Returns either the current value (ago=None) or a delayed LineBuffer that fetches the value which is “ago” periods before. Useful to have the closing price 5 bars before: close(-5)
- datetime(ago=0, tz=None, naive=True)[source]¶
Get the datetime value at the specified offset.
- Parameters:
ago – Number of periods to look back (0=current, -1=previous).
tz – Timezone to apply. If None, uses self._tz.
naive – If True, return naive datetime without timezone info.
- Returns:
Datetime object representing the timestamp.
- Return type:
datetime
- Raises:
IndexError – If the requested position is out of bounds for data feeds.
- date(ago=0, tz=None, naive=True)[source]¶
Get the date component of the datetime value at the specified offset.
- Parameters:
ago – Number of periods to look back (0=current, -1=previous).
tz – Timezone to apply. If None, uses self._tz.
naive – If True, return naive date without timezone info.
- Returns:
Date object representing the date portion of the timestamp.
- Return type:
date
- Raises:
IndexError – If the requested position is out of bounds for data feeds.
- time(ago=0, tz=None, naive=True)[source]¶
Get the time component of the datetime value at the specified offset.
- Parameters:
ago – Number of periods to look back (0=current, -1=previous).
tz – Timezone to apply. If None, uses self._tz.
naive – If True, return naive time without timezone info.
- Returns:
Time object representing the time portion of the timestamp.
- Return type:
time
- tm_raw(ago=0)[source]¶
Returns a localtime/gmtime like time.struct_time object which is compatible with strftime formatting.
The time zone of the struct_time is naive
- tm(ago=0)[source]¶
Returns a localtime/gmtime like time.struct_time object which is compatible with strftime formatting.
The time zone of the struct_time is naive
- tm_lt(other, ago=0)[source]¶
Returns True if the time carried by this line’s index “ago” is lower than the time carried by the “other” line
- tm_le(other, ago=0)[source]¶
Returns True if the time carried by this line’s index “ago” is lower than or equal to the time carried by the “other” line
- tm_eq(other, ago=0)[source]¶
Returns True if the time carried by this line’s index “ago” is equal to the time carried by the “other” line
- tm_gt(other, ago=0)[source]¶
Returns True if the time carried by this line’s index “ago” is greater than the time carried by the “other” line
- tm_ge(other, ago=0)[source]¶
Returns True if the time carried by this line’s index “ago” is greater than or equal to the time carried by the “other” line
- class backtrader.linebuffer.LineActionsCache[source]¶
Bases:
objectCache system for LineActions to avoid repetitive calculations
- class backtrader.linebuffer.LineActionsMixin[source]¶
Bases:
objectMixin to provide LineActions functionality without metaclass
- class backtrader.linebuffer.PseudoArray[source]¶
Bases:
objectWrapper for non-array iterables to provide array-like access.
This class wraps iterables (including itertools.repeat) and provides array-like indexing access. It handles cases where the wrapped object doesn’t support direct indexing.
- wrapped¶
The wrapped iterable object.
- _minperiod¶
Minimum period inherited from the wrapped object.
Example
>>> from itertools import repeat >>> pseudo = PseudoArray(repeat(1.0)) >>> print(pseudo[0]) 1.0
- class backtrader.linebuffer.LineActions[source]¶
Bases:
LineBuffer,LineActionsMixin,ParamsMixinBase class for Line Clases with different lines, derived from a LineBuffer
- plotlines = <object object>¶
- static __new__(cls, *args, **kwargs)[source]¶
Handle data processing for indicators and other LineActions objects
- __init__(*args, **kwargs)¶
Initialize the LineBuffer instance.
Sets up all internal attributes including the array storage, index pointer, buffer mode, and performance optimization flags.
- getindicators()[source]¶
Get list of indicators using this line actions object.
- Returns:
Empty list for base LineActions (override in subclasses).
- Return type:
- qbuffer(savemem=0)[source]¶
Enable queued buffer mode for memory-efficient storage.
- Parameters:
savemem – Memory saving mode (0=normal, >0=enable cache mode).
- static arrayize(obj)[source]¶
Convert an object to an array-compatible object.
- Parameters:
obj – Object to convert. Can be a value, iterable, or array-like.
- Returns:
The original object if it has an array attribute, otherwise a LineNum or PseudoArray wrapper.
- frompackages = ()¶
- packages = ()¶
- backtrader.linebuffer.LineDelay(a, ago=0, **kwargs)[source]¶
Create a delayed line object.
- Parameters:
a – Source line object.
ago – Number of periods to delay. Negative for lookback.
**kwargs – Additional keyword arguments.
- Returns:
A delayed line object.
- Return type:
_LineDelay or _LineForward
- backtrader.linebuffer.LineNum(num)[source]¶
Create a constant line from a number.
- Parameters:
num – The constant value.
- Returns:
A line object that always returns the constant value.
- Return type:
_LineDelay
- class backtrader.linebuffer.LinesOperation[source]¶
Bases:
LineActionsOperation between two line objects (binary operations).
This class represents binary operations (addition, subtraction, etc.) between two line objects. The result is a new line that contains the element-wise operation result.
- operation¶
The binary function to apply (e.g., operator.add).
- a¶
First operand (left-hand side).
- b¶
Second operand (right-hand side).
- r¶
If True, reverse operation order.
- _parent_a¶
Parent indicator for operand a.
- _parent_b¶
Parent indicator for operand b.
Example
>>> result = LinesOperation(indicator1, indicator2, operator.sub) >>> # result[0] = indicator1[0] - indicator2[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.
- __getitem__(ago)[source]¶
CRITICAL FIX: Override __getitem__ to compute value dynamically from source operands.
This ensures correct values in runonce mode where LinesOperation’s _idx may not be properly advanced because it’s not registered as IndType.
- next()[source]¶
Calculate and set the operation result for the current bar.
Performs the binary operation on the current values of both operands and stores the result at position 0.
- once(start, end)[source]¶
Calculate operation results in batch mode (runonce).
- Parameters:
start – Starting index.
end – Ending index.
- frompackages = ()¶
- packages = ()¶
- class backtrader.linebuffer.LineOwnOperation[source]¶
Bases:
LineActionsOperation on a single line object (unary operations).
This class represents unary operations (negation, absolute value, etc.) on a single line object. The result is a new line that contains the element-wise operation result.
- operation¶
The unary function to apply (e.g., operator.neg).
- a¶
The operand (line object).
- _parent_a¶
Parent indicator for the operand.
Example
>>> result = LineOwnOperation(indicator, operator.neg) >>> # result[0] = -indicator[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.
- frompackages = ()¶
- packages = ()¶
- __getitem__(ago)[source]¶
CRITICAL FIX: Override __getitem__ to compute value dynamically from source operand.
- next()[source]¶
Calculate and set the unary operation result for the current bar.
Performs the unary operation on the current value of the operand and stores the result at position 0.