backtrader.parameters module¶
New Parameter System for Backtrader
This module implements a modern parameter system that replaces the metaclass-based parameter handling with descriptor-based approach. This provides better type safety, validation, and maintainability while maintaining backward compatibility.
Key Components: - ParameterDescriptor: Core descriptor for parameter handling - ParameterManager: Parameter storage and management - ParameterizedBase: Base class for parameterized objects (without metaclass) - Type checking and validation mechanisms - Python 3.6+ __set_name__ support
- class backtrader.parameters.ParameterDescriptor[source]¶
Bases:
objectAdvanced parameter descriptor with type checking and validation.
This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:
Automatic type checking and conversion
Value validation
Default value handling
Documentation support
Python 3.6+ __set_name__ support
- __init__(default=None, type_=None, validator=None, doc=None, name=None, required=False)[source]¶
Initialize parameter descriptor.
- Parameters:
default (Any) – Default value for the parameter
type – Expected type for the parameter (enables type checking)
validator (Callable[[Any], bool] | None) – Function to validate parameter values
doc (str | None) – Documentation string for the parameter
name (str | None) – Parameter name (usually set by __set_name__)
required (bool) – Whether this parameter is required (no default allowed)
type_ (Type | None)
- __set_name__(owner, name)[source]¶
Called when the descriptor is assigned to a class attribute. This is a Python 3.6+ feature that automatically sets the parameter name.
- class backtrader.parameters.ParameterManager[source]¶
Bases:
objectEnhanced Parameter storage and management system.
This class manages parameter values for an object, replacing the functionality of AutoInfoClass. It provides efficient storage, inheritance support, batch operations, and advanced features like change tracking, callbacks, and transactional updates.
New Features in Day 32-33: - Parameter change history and tracking - Change callbacks and notifications - Parameter locking mechanism - Parameter groups for organization - Advanced inheritance with conflict resolution - Lazy default value evaluation - Transactional batch updates
- __init__(descriptors, initial_values=None, enable_history=True, enable_callbacks=True)[source]¶
Initialize parameter manager.
- get(name, default=None, _MISSING=<object object>)[source]¶
Get parameter value with optimized caching and lazy evaluation support.
- set(name, value, force=False, trigger_callbacks=True, skip_validation=False)[source]¶
Set parameter value with validation and dependency updates.
- copy()[source]¶
Create a copy of this parameter manager.
- Returns:
New ParameterManager instance with same values
- Return type:
- inherit_from(parent, strategy='merge', conflict_resolution='parent', selective=None)[source]¶
Inherit parameters from another ParameterManager.
- Parameters:
parent (ParameterManager) – Parent ParameterManager to inherit from
strategy (str) – Inheritance strategy (‘merge’, ‘replace’, ‘add_only’, ‘selective’)
conflict_resolution (str) – How to resolve conflicts (‘parent’, ‘child’, ‘error’, ‘raise’)
selective (List[str] | None) – Only inherit specific parameters (list of names)
- Return type:
None
- lock_parameter(name)[source]¶
Lock a parameter to prevent modification.
- Parameters:
name (str)
- Return type:
None
- unlock_parameter(name)[source]¶
Unlock a parameter to allow modification.
- Parameters:
name (str)
- Return type:
None
- clear_lazy_default(name)[source]¶
Clear lazy default for a parameter.
- Parameters:
name (str)
- Return type:
None
- add_change_callback(callback, param_name=None)[source]¶
Add a callback function that will be called when parameters change.
- clear_history(name=None)[source]¶
Clear change history.
- Parameters:
name (str | None) – Specific parameter name, or None to clear all history
- Return type:
None
- add_dependency(param_name, dependent_param)[source]¶
Add a dependency relationship between parameters.
- class backtrader.parameters.ParameterAccessor[source]¶
Bases:
objectParameter accessor that provides dict-like and attribute-like access to parameters.
This class serves as a bridge between the new parameter system and the old MetaParams-style parameter access patterns. It provides backward compatibility by supporting both attribute access (obj.p.param_name) and dict-like access.
- __init__(param_manager)[source]¶
Initialize with a parameter manager.
NOTE: Originally attempted to pre-create instance attributes for performance, but this causes parameter synchronization issues. When parameters are modified through other means (like broker.set_cash()), instance attributes won’t update. Therefore maintain dynamic lookup to ensure latest values are always retrieved.
- Parameters:
param_manager (ParameterManager)
- class backtrader.parameters.ParameterizedBase[source]¶
Bases:
objectEnhanced base class for objects with parameters - without metaclass.
This class provides the modern parameter system interface while maintaining backward compatibility with the old MetaParams-based system. It uses regular class mechanisms instead of metaclass.
- classmethod __init_subclass__(**kwargs)[source]¶
Called when a class is subclassed. Replaces metaclass functionality.
This method sets up the class for lazy parameter descriptor resolution to avoid inheritance contamination issues.
- set_param(name, value, validate=True)[source]¶
Set parameter value with optional validation.
- Parameters:
- Raises:
AttributeError – If parameter manager not initialized
ValueError – If validation fails
- Return type:
None
- reset_param(name)[source]¶
Reset parameter to its default value.
- Parameters:
name (str) – Parameter name
- Raises:
AttributeError – If parameter manager not initialized
ValueError – If parameter doesn’t exist or is locked
- Return type:
None
- copy_params_from(other, param_names=None, exclude=None)[source]¶
Copy parameters from another ParameterizedBase instance.
- Parameters:
other (ParameterizedBase) – Source object to copy parameters from
param_names (List[str] | None) – Specific parameter names to copy (None for all)
exclude (List[str] | None) – Parameter names to exclude from copying
- Return type:
None
- backtrader.parameters.Int(min_val=None, max_val=None)[source]¶
Create an integer validator function.
- backtrader.parameters.FloatParam(default=None, min_val=None, max_val=None, doc=None)[source]¶
Create a float parameter descriptor with validation.
- Parameters:
- Return type:
- backtrader.parameters.BoolParam(default=None, doc=None)[source]¶
Create a boolean parameter descriptor.
- Parameters:
doc (str)
- Return type:
- backtrader.parameters.StringParam(default=None, min_length=None, max_length=None, doc=None)[source]¶
Create a string parameter descriptor with length validation.
- Parameters:
- Return type:
- backtrader.parameters.String(min_length=None, max_length=None)[source]¶
Create a string validator function.
- backtrader.parameters.OneOf(*choices)[source]¶
Create a validator that checks if value is one of the given choices.
- backtrader.parameters.create_param_descriptor(name, default=None, doc=None)[source]¶
Create a basic parameter descriptor.
- Parameters:
- Returns:
ParameterDescriptor instance
- Return type:
- backtrader.parameters.derive_params(base_params, new_params, other_base_params=None)[source]¶
Derive parameters by combining base parameters with new ones.
- Parameters:
base_params – Base parameter descriptors or tuples
new_params – New parameter descriptors or tuples
other_base_params – Additional base parameters
- Returns:
Dictionary of combined parameter descriptors
- class backtrader.parameters.ParamsBridge[source]¶
Bases:
objectBridge class for transitioning from MetaParams to new parameter system.
This class provides utilities for converting between the old MetaParams system and the new descriptor-based system during the migration period.
- static extract_params_from_metaparams_class(cls)[source]¶
Extract parameter descriptors from a MetaParams-based class.
- Parameters:
cls – Class with MetaParams-based parameters
- Returns:
Dictionary of parameter descriptors
- Return type:
- exception backtrader.parameters.ParameterValidationError[source]¶
Bases:
ValueErrorSpecific exception for parameter validation errors.
- exception backtrader.parameters.ParameterAccessError[source]¶
Bases:
AttributeErrorSpecific exception for parameter access errors.