Strategy Generation & Constraints
Relevant source files
The following files were used as context for generating this wiki page:
The Strategy Generation & Constraints module is responsible for translating detected market regimes into actionable trading parameters and constraints. It serves as a decision-support layer that converts high-level market states (e.g., trend direction, volatility, and transition risk) into specific multipliers for position sizing, leverage permissions, and strategy family allowances.
The system operates through three primary layers: the legacy Strategy Response, Agent Cohort Routing, and Strategy Family Constraints. These layers are reconciled using a "most-restrictive-wins" precedence logic to produce a final set of effective constraints for the trading engine.
System Architecture and Data Flow
The system consumes outputs from various regime classifiers and applies hierarchical logic to determine the final strategy guidance.
The diagram shows the flow from raw regime classification data through the three constraint layers to the final resolved constraints.
Sources: src/regime_detection/strategy_constraints.py:40-66, src/regime_detection/strategy_response.py:65-120
Strategy Response Generation
The StrategyResponse represents the base layer of guidance derived from four main axes (Trend Direction, Trend Character, Volatility State, Breadth State) and a combined Transition Risk State. It determines fundamental trading postures, such as whether to allow trend following or mean reversion.
Key Logic and Modifiers
If data is insufficient or any core axis is "unknown", the system defaults to a conservative posture (position multiplier of 0.75, no leverage). In classified states, various modifiers are applied:
- Bull Healthy Low Vol: Applies when bull trends meet low/normal volatility and healthy breadth.
- Recovery Attempt: Reduces position sizing to 0.5 and disables leverage.
- Crisis: Drastically reduces position sizing to 0.25 and requires hard max loss protections.
Sources: src/regime_detection/strategy_response.py:75-120, src/regime_detection/models.py:596-620
Event Calendar Modifiers
The strategy response is further modified by specific macro or policy events defined in the StrategyEventModifiersConfig. Rules can cap position sizes or require additional confirmation for new trades during windows like "fed_week" or "election_window".
Sources: src/regime_detection/strategy_response.py:10-50
Strategy Family Constraints
This layer manages permissions for specific strategy families (e.g., Trend Following, Mean Reversion, Breakout, Short Vol, Long Vol). It uses an inheritance-based configuration model where specialist cohorts (Agent Cohorts) can override the default_neutral baseline.
The class diagram illustrates the data structure used for defining per-family permissions and their configuration.
Sources: src/regime_detection/models.py:623-644, src/regime_detection/config.py:1013-1055
Resolution Logic
The function resolve_strategy_family_constraints looks up the active agent cohort. If the cohort exists in the configuration's overrides, those values are merged with the default_neutral baseline. If a cohort is unknown, the system falls back to the neutral baseline.
Sources: src/regime_detection/strategy_family_constraints.py:25-50
Effective Constraints Resolution
The final step in the pipeline is the reconciliation of all layers. The resolve_effective_strategy_constraints function applies a "most-restrictive-wins" policy across the Strategy Response, Agent Routing, and Strategy Family Constraints.
Reconciliation Table
| Dimension | Source Precedence | Logic |
|---|---|---|
| Allowed Status | Strategy Response AND Family Constraints AND Agent Routing | A family is blocked if any source marks it as False. |
| Position Multiplier | Strategy Response | Taken directly from the base strategy response. |
| Lookback/Holding Days | Strategy Family Constraints | Taken from the resolved family overrides. |
| Confirmation Flags | Strategy Response OR Family Constraints | If either source requires confirmation, it is enforced. |
Sources: src/regime_detection/strategy_constraints.py:30-66, src/regime_detection/strategy_constraints.py:78-120
Implementation Example: One-Family Resolution
def _resolve_one(
*,
family: str,
strategy_response: StrategyResponse,
agent_routing: AgentRouting | None,
family_constraint: StrategyFamilyConstraint | None,
is_agent_blocked: bool,
) -> EffectiveStrategyConstraint:
# Logic: Start with strategy_response. If family_constraint or
# agent_routing blocks it, 'allowed' flips to False.
Sources: src/regime_detection/strategy_constraints.py:78-95
Data Models
The system relies on Pydantic models to ensure strict wire contracts and type safety across the strategy generation process.
| Class | Purpose | Key Fields |
|---|---|---|
StrategyResponse | Base guidance layer | position_size_multiplier, leverage_allowed, modifiers_applied |
StrategyFamilyConstraint | Per-family parameters | allowed, max_lookback_days, require_volume_confirmation |
EffectiveStrategyConstraint | Final resolved output | family, allowed, sources, blocking_reasons |
AgentRouting | Cohort selection | active_cohort, blocked_strategy_modes |
Sources: src/regime_detection/models.py:596-675
Summary
Strategy Generation & Constraints provides the critical mapping between abstract market regimes and concrete operational limits. By layering base response logic, agent-specific overrides, and family-level configurations, the system ensures that trading behavior remains aligned with the identified market environment while maintaining a safety-first "most restrictive" resolution policy.