backtrader.errors module

Framework and research exceptions share this module. Research failures derive from ResearchError and BacktraderError. ResearchConfigError keeps structured research diagnostics and ValueError compatibility; the framework ConfigError remains a separate category.

Exception Classes Module - Custom exceptions for backtrader.

This module defines the exception hierarchy used throughout the backtrader framework.

Classes:

BacktraderError: Base exception for all backtrader exceptions. StrategySkipError: Raised to skip a strategy during optimization. ModuleImportError: Raised when a required module cannot be imported. FromModuleImportError: Raised when a from-style import fails. DataError: Raised for data-feed/parsing problems. BrokerError: Raised for broker/execution problems. OrderError: Raised for order-related problems (subclass of BrokerError). ConfigError: Raised for invalid configuration/parameters. ResearchError: Base class for typed native-research failures. ResearchConfigError: Raised for invalid research configuration. DatasetError: Raised for invalid or unsupported research input data. DependencyError: Raised when a research dependency is unavailable. DataQualityError: Raised when a research quality bound is exceeded. FactorComputationError: Raised for unexpected native factor failures. FactorComputationContext: Attributes strict failures to their factor context.

示例

Raising StrategySkipError during optimization: >>> class MyStrategy(bt.Strategy): ... params = (('period', 20),) ... ... def __init__(self): ... if self.p.period < 5: ... raise bt.errors.StrategySkipError()

exception backtrader.errors.BacktraderError[源代码]

基类:Exception

Base exception for all backtrader exceptions.

exception backtrader.errors.StrategySkipError[源代码]

基类:BacktraderError

Requests the platform to skip this strategy for backtesting. To be raised during the initialization (__init__) phase of the instance

exception backtrader.errors.DataError[源代码]

基类:BacktraderError

Raised for data-feed problems: loading, parsing, or alignment failures.

exception backtrader.errors.BrokerError[源代码]

基类:BacktraderError

Raised for broker/execution problems: cash, margin, or matching failures.

exception backtrader.errors.OrderError[源代码]

基类:BrokerError

Raised for order-related problems: invalid size/price or rejected orders.

exception backtrader.errors.ConfigError[源代码]

基类:BacktraderError

Raised for invalid configuration or parameter values.

exception backtrader.errors.ResearchError[源代码]

基类:BacktraderError

Base class for stable, machine-readable research failures.

default_code = 'RESEARCH_ERROR'
__init__(message='', *, code=None, report=None)[源代码]
参数:
返回类型:

None

exception backtrader.errors.ResearchConfigError[源代码]

基类:ResearchError, ValueError

Invalid or unsupported research configuration.

default_code = 'INVALID_CONFIG'
exception backtrader.errors.DatasetError[源代码]

基类:ResearchError, ValueError

Invalid, incomplete, or unsupported research input data.

default_code = 'INVALID_DATASET'
exception backtrader.errors.DependencyError[源代码]

基类:ResearchError, ImportError

A required runtime or public dependency is unavailable.

default_code = 'DEPENDENCY_UNAVAILABLE'
exception backtrader.errors.DataQualityError[源代码]

基类:ResearchError, ValueError

A requested quality bound is exceeded by the collected dataset.

default_code = 'LOSS_LIMIT_EXCEEDED'
exception backtrader.errors.FactorComputationError[源代码]

基类:ResearchError, RuntimeError

An unexpected native factor or line calculation failure.

__cause__ is kept by normal Python exception chaining. Callers should raise the result of FactorComputationContext.wrap() with from the original exception so its type and traceback remain available.

default_code = 'FACTOR_COMPUTATION_FAILED'
__init__(message='factor computation failed', *, factor, asset, phase, code='FACTOR_COMPUTATION_FAILED', report=None)[源代码]
参数:
返回类型:

None

class backtrader.errors.FactorComputationContext[源代码]

基类:object

Duck-typed strict policy context propagated through one native factor.

The core only needs the public phase, asset_for and wrap protocol. Feed and output-line references are retained by identity so an error is attributed only when one asset can be identified unambiguously.

__init__(factor, assets_by_object=None, *, phase='construct')[源代码]
参数:
返回类型:

None

factor
phase
failed
error
bind_object(obj, asset)[源代码]

Bind a feed or concrete output line to its declared asset name.

参数:
返回类型:

None

asset_for(operation=None)[源代码]

Resolve an asset from an operation or its direct data operands.

Cross-sectional operations touching multiple assets intentionally return None; callers must not guess one member of the panel.

参数:

operation (Any)

返回类型:

str | None

wrap(exc, *, phase=None, asset=None)[源代码]

Build a safe structured error; preserve exc via raise from.

参数:
返回类型:

FactorComputationError