Enums#
Enumerations used across the backtest engine to express order side, price type and rebalance status with type-safe values.
Trading Enums#
Trading enumerations module.
This module defines type-safe enumerations for trading operations used throughout the backtesting engine. These enums provide compile-time type safety and consistent naming conventions.
All enums inherit from StrEnum for string serialization compatibility.
- class OrderSide(*values)#
Bases:
StrEnumEnumeration representing the side of a trade order.
This enum distinguishes between buy and sell operations in the backtesting engine. It is used by TradeOrder and ExecutionBroker to track trade direction.
- Variables:
BUY (str) – Represents a purchase order that increases position size. Value is “buy”.
SELL (str) – Represents a sale order that decreases position size. Value is “sell”.
Examples
Creating and using OrderSide:
>>> side = OrderSide.BUY >>> side.value 'buy' >>> side == OrderSide.BUY True
Using in conditionals:
>>> if side == OrderSide.SELL: ... print("Selling position")
Notes
As a StrEnum, OrderSide values can be directly compared with strings and used as dictionary keys or in JSON serialization.
- class PriceType(*values)#
Bases:
StrEnumEnumeration for different price sources used in backtesting.
The backtesting engine uses multiple price types for different purposes. Values for the three standard price types are derived from
StandardFieldso thatfield_definitions.pyremains the single source of truth.- Variables:
EXECUTION (str) – Price used for trade execution. This is the configurable price source selected by the user. Value is
"execution".COMMISSION_PRICE (str) – Price used for commission calculation (share count for fees). Value is derived from
StandardField.COMMISSION_PRICE.TRADE_EXECUTION_PRICE (str) – Price used as the default trade execution price (typically split/dividend-adjusted VWAP). Value is derived from
StandardField.TRADE_EXECUTION_PRICE.MARK_TO_MARKET_PRICE (str) – Price used for daily portfolio valuation. Value is derived from
StandardField.MARK_TO_MARKET_PRICE.
Examples
Using as cache keys:
>>> price_cache: dict[str, pa.Table] = {} >>> price_cache[PriceType.EXECUTION.value] = execution_table >>> price_cache[PriceType.COMMISSION_PRICE.value] = commission_table
Notes
The EXECUTION price type is special — it represents whatever price source was configured for trade execution, which could be any of the standard price columns.
- class RebalanceStatus(*values)#
Bases:
StrEnumEnumeration representing the outcome of a rebalancing operation.
Each rebalancing event results in one of these statuses, which is captured in RebalanceResult. This enables proper error handling and logging of backtest issues.
- Variables:
SUCCESS (str) – Rebalancing completed successfully without errors. All target positions were achieved. Value is “success”.
PARTIAL (str) – Rebalancing partially completed. Some positions may not have been filled. Value is “partial”.
FAILED (str) – Rebalancing failed due to critical error. No trades were executed. Value is “failed”.
INSUFFICIENT_CASH (str) – Rebalancing failed due to negative cash position. This indicates the strategy requires more capital. Value is “insufficient_cash”.
Examples
Checking rebalance outcome:
>>> result = broker.execute_rebalance(...) >>> if result.status == RebalanceStatus.SUCCESS: ... print("Rebalancing completed") >>> elif result.status == RebalanceStatus.INSUFFICIENT_CASH: ... print(f"Not enough cash: {result.error_message}")
Using in backtest flow:
>>> if result.status != RebalanceStatus.SUCCESS: ... logger.warning("Rebalancing issue: %s", result.status) ... if result.status == RebalanceStatus.INSUFFICIENT_CASH: ... return False # Stop backtest
Notes
INSUFFICIENT_CASH typically indicates the strategy is too aggressive for the initial capital, or commission/slippage costs are higher than expected.