feat: autonomous trading engine — full implementation

- Database migration 018 with 13 tables for trading engine state
- Trading engine service (services/trading/) with 12 pure computation modules:
  position sizer, stop-loss manager, reserve pool, circuit breaker,
  risk tier controller, correlation matrix, tax lots, trading window,
  gradual entry, notifications, micro-trading, backtester
- Core TradingEngine with pre-trade evaluation pipeline and integration wiring
- FastAPI HTTP service with 14 endpoints (health, config, decisions, metrics, backtest)
- Performance tracker with Sharpe ratio, drawdown, profit factor computation
- 194 Python tests (165 property-based + 29 integration)
- Frontend: 13 TanStack Query hooks, 7 dashboard panels, tabbed Trading Engine page
- Helm chart entry, network policy, nginx proxy, ingress for trading-engine
- Shared infrastructure: enums, Redis keys, TradingConfig in AppConfig
This commit is contained in:
Celes Renata
2026-04-15 16:12:22 +00:00
parent da86132f0c
commit 4ffde8cc06
58 changed files with 14168 additions and 1 deletions
@@ -0,0 +1 @@
{"specId": "3e745894-9abc-49ff-97cc-c921f436bb32", "workflowType": "requirements-first", "specType": "feature"}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,308 @@
# Requirements Document — Autonomous Trading Engine
## Introduction
This feature adds a fully autonomous trading engine to the Stonks Oracle platform. The engine consumes recommendations produced by the existing three-layer signal aggregation pipeline (company-specific, macro, competitive), applies confidence-based position sizing, reserve pool management, dynamic stop-loss/take-profit levels, and portfolio rebalancing logic, then executes trades through the existing Alpaca broker adapter.
The system is designed for a developer who wants to allocate $500 in paper trading capital and let the engine operate without manual intervention. Day-to-day decisions — what to buy, how much, when to exit — are made autonomously based on signal confidence, risk tier configuration, and portfolio state. The architecture supports switching from paper to live trading with minimal changes.
The engine builds on existing infrastructure rather than replacing it. The recommendation engine already produces buy/sell/hold/watch signals with confidence scores and position sizing guidance. The broker service already submits orders to Alpaca with idempotency and risk evaluation. The risk engine already enforces position limits, sector exposure, and daily loss controls. This feature layers autonomous decision-making, reserve pool management, adaptive market response, circuit breakers, and performance tracking on top of that foundation.
Key design principles:
- **Fail-safe**: circuit breakers halt trading on abnormal losses; reserve pool is untouchable by normal trading
- **Transparent**: every decision is traced from signal through sizing through execution
- **Incremental**: positions are built gradually, not all-at-once
- **Adaptive**: risk tier auto-adjusts based on recent performance and reserve pool health
## Glossary
- **Trading_Engine**: The autonomous service that polls for actionable recommendations, computes position sizes, manages the reserve pool, enforces circuit breakers, and submits orders to the Broker_Service.
- **Reserve_Pool**: A cash reserve automatically funded by siphoning a configurable percentage of realized profits. The Reserve_Pool is not available for normal trading and acts as a safety net for the portfolio.
- **Active_Pool**: The portion of portfolio capital available for trading, equal to total portfolio value minus the Reserve_Pool balance.
- **Risk_Tier**: A named configuration preset (conservative, moderate, aggressive) that controls confidence thresholds, position sizing multipliers, stop-loss widths, and take-profit targets.
- **Risk_Tier_Controller**: The component that evaluates recent performance metrics and Reserve_Pool size to automatically adjust the active Risk_Tier.
- **Circuit_Breaker**: A safety mechanism that halts all trading when portfolio losses exceed configurable thresholds within a time window, or when abnormal market conditions are detected.
- **Position_Sizer**: The component that computes the dollar amount and share quantity for a trade based on signal confidence, Risk_Tier parameters, Active_Pool size, correlation constraints, and sector exposure limits.
- **Stop_Loss_Manager**: The component that computes and maintains dynamic stop-loss and take-profit levels for each open position based on volatility and signal strength.
- **Portfolio_Rebalancer**: The component that periodically evaluates portfolio concentration and generates rebalancing orders when sector or single-stock exposure exceeds configurable limits.
- **Performance_Tracker**: The component that computes and persists real-time portfolio metrics including P&L, win/loss ratio, Sharpe ratio, max drawdown, and per-trade statistics.
- **Backtester**: The component that replays historical recommendation and market data to simulate how the trading strategy would have performed, producing the same metrics as the Performance_Tracker.
- **Portfolio_Heat**: The total risk across all open positions, computed as the sum of each position's value multiplied by its stop-loss distance percentage. Portfolio_Heat must not exceed a configurable threshold of the Active_Pool.
- **Correlation_Matrix**: A precomputed matrix of price correlation coefficients between tracked companies, used to prevent over-concentration in correlated positions.
- **Earnings_Calendar**: A schedule of upcoming earnings report dates for tracked companies, used to reduce position sizes before earnings and adjust after results.
- **Trading_Window**: The allowed time range for order submission, excluding the first and last 15 minutes of market hours to avoid high-volatility open/close periods.
- **Gradual_Entry**: The strategy of scaling into a position over multiple smaller orders rather than a single large order, reducing market impact and allowing the engine to abort if conditions change.
- **Tax_Lot**: A record of the purchase date, quantity, and cost basis for a specific acquisition of shares, used for tax-loss harvesting awareness and wash sale detection.
- **Notification_Service**: The component that sends alerts to the operator via SMS (AWS SNS) and email (Gmail API) for critical events such as circuit breaker triggers, risk tier changes, large trades, and daily performance summaries.
- **Micro_Trading_Mode**: An optional operating mode where the Trading_Engine evaluates and acts on intraday and short-window trend signals (5-minute and 15-minute intervals) in addition to the standard daily/weekly recommendation cycle, enabling faster position turnover for short-term opportunities.
- **Broker_Service**: The existing service (services/adapters/broker_service.py) that processes order requests through risk evaluation and submits them to Alpaca.
- **Recommendation_Engine**: The existing service (services/recommendation/) that produces buy/sell/hold/watch signals with confidence scores from trend data.
- **Aggregation_Engine**: The existing service (services/aggregation/) that computes trend summaries from all three signal layers.
## Requirements
### Requirement 1: Autonomous Decision Loop
**User Story:** As a developer with $500 in paper trading capital, I want the system to automatically evaluate recommendations and execute trades without my intervention, so that I can let it run and check results periodically.
#### Acceptance Criteria
1. THE Trading_Engine SHALL run a continuous decision loop that polls for new actionable recommendations (action = buy or sell, mode = paper_eligible or live_eligible) at a configurable interval (default: 60 seconds).
2. WHEN the Trading_Engine finds an actionable recommendation, THE Trading_Engine SHALL evaluate the recommendation against the current portfolio state, Risk_Tier parameters, circuit breaker status, and Trading_Window constraints before deciding whether to act.
3. WHEN the Trading_Engine decides to act on a recommendation, THE Trading_Engine SHALL compute the position size via the Position_Sizer, generate an order job, and submit it to the existing Broker_Service queue for execution.
4. WHEN the Trading_Engine decides not to act on a recommendation, THE Trading_Engine SHALL persist a decision record with the skip reason (circuit breaker active, outside Trading_Window, insufficient confidence, position limit reached, or insufficient Active_Pool funds).
5. THE Trading_Engine SHALL process each recommendation at most once, using the recommendation ID as a deduplication key persisted in Redis with a configurable TTL (default: 24 hours).
6. WHEN the Trading_Engine starts, THE Trading_Engine SHALL load the current portfolio state from the Broker_Service (positions, account balance) and the active Risk_Tier configuration from PostgreSQL before entering the decision loop.
7. THE Trading_Engine SHALL expose a health endpoint at `/health` and a readiness endpoint at `/ready` that reports whether the engine has successfully loaded portfolio state and is actively polling.
### Requirement 2: Confidence-Based Position Sizing
**User Story:** As a developer, I want position sizes to scale with signal confidence so that high-confidence signals get larger allocations and the system never risks too much on a single trade.
#### Acceptance Criteria
1. WHEN the Position_Sizer computes a trade size, THE Position_Sizer SHALL scale the allocation as a percentage of the Active_Pool using the formula: `base_allocation_pct * (confidence / confidence_threshold) * risk_tier_multiplier`, clamped to the Risk_Tier's max_position_pct.
2. WHEN the signal confidence is below the Risk_Tier's minimum confidence threshold, THE Position_Sizer SHALL return a zero allocation and the Trading_Engine SHALL skip the trade.
3. THE Position_Sizer SHALL enforce that no single position exceeds a configurable maximum percentage of the Active_Pool (default: 10% for moderate tier).
4. THE Position_Sizer SHALL enforce that the total dollar value of any single position does not exceed a configurable absolute cap (default: $50 for a $500 portfolio, scaling with portfolio size).
5. WHEN computing position size, THE Position_Sizer SHALL check the Correlation_Matrix and reduce the allocation if the portfolio already holds positions with a correlation coefficient above 0.7 to the candidate stock.
6. WHEN computing position size, THE Position_Sizer SHALL check sector exposure and reduce the allocation if adding the position would cause the sector to exceed the configurable sector exposure limit (default: 30% of Active_Pool).
7. THE Position_Sizer SHALL round the computed share quantity down to whole shares and reject orders where the resulting quantity is zero.
### Requirement 3: Reserve Pool Management
**User Story:** As a developer, I want a portion of profits automatically set aside into a reserve that normal trading cannot touch, so that the portfolio has a safety net that grows over time.
#### Acceptance Criteria
1. WHEN a position is closed with a realized profit, THE Trading_Engine SHALL transfer a configurable percentage of the profit (default: 20%) into the Reserve_Pool.
2. THE Trading_Engine SHALL persist the Reserve_Pool balance in PostgreSQL and update it on every profit siphon event, with a full audit trail of deposits and withdrawals.
3. THE Trading_Engine SHALL compute the Active_Pool as: `total_portfolio_value - reserve_pool_balance`, and all position sizing and risk calculations SHALL use the Active_Pool rather than the total portfolio value.
4. WHEN the Reserve_Pool balance exceeds a configurable high-water mark percentage of the total portfolio (default: 30%), THE Risk_Tier_Controller SHALL consider this a signal to increase risk tolerance (shift toward a more aggressive tier).
5. WHEN the Active_Pool drops below a configurable minimum threshold (default: $100), THE Trading_Engine SHALL halt new position entries and only allow position exits until the Active_Pool recovers.
6. IF the portfolio experiences a drawdown exceeding a configurable emergency threshold (default: 40% of initial capital), THEN THE Trading_Engine SHALL liquidate the Reserve_Pool into the Active_Pool to provide emergency capital, log the event, and shift to the conservative Risk_Tier.
7. THE Reserve_Pool balance SHALL be visible on the dashboard and included in all portfolio summary API responses.
### Requirement 4: Dynamic Stop-Loss and Take-Profit
**User Story:** As a developer, I want every position to have automatic exit levels that adapt to market volatility and signal strength, so that losses are cut and profits are captured without manual monitoring.
#### Acceptance Criteria
1. WHEN the Trading_Engine opens a new position, THE Stop_Loss_Manager SHALL compute an initial stop-loss price based on the stock's recent volatility (ATR — Average True Range over 14 periods) and the Risk_Tier's stop-loss multiplier: `entry_price - (ATR * stop_loss_atr_multiplier)` for long positions.
2. WHEN the Trading_Engine opens a new position, THE Stop_Loss_Manager SHALL compute an initial take-profit price using the Risk_Tier's reward-to-risk ratio: `entry_price + (stop_distance * reward_risk_ratio)`.
3. WHILE a position is open, THE Stop_Loss_Manager SHALL re-evaluate stop-loss and take-profit levels at a configurable interval (default: every 5 minutes during market hours) and adjust them if volatility or signal conditions have materially changed.
4. WHEN a position's current price crosses the stop-loss level, THE Trading_Engine SHALL submit a market sell order to the Broker_Service immediately, without waiting for the next decision loop cycle.
5. WHEN a position's current price crosses the take-profit level, THE Trading_Engine SHALL submit a market sell order to the Broker_Service immediately.
6. WHEN a position has moved favorably by more than 50% of the take-profit distance, THE Stop_Loss_Manager SHALL implement a trailing stop by moving the stop-loss to breakeven (entry price) to lock in a risk-free position.
7. THE Stop_Loss_Manager SHALL persist all stop-loss and take-profit levels and adjustments in PostgreSQL with timestamps for audit trail purposes.
8. IF the Trading_Engine cannot fetch current price data for a position, THEN THE Trading_Engine SHALL use the last known price and log a warning, and if price data is unavailable for more than 15 minutes during market hours, THE Trading_Engine SHALL close the position as a safety measure.
### Requirement 5: Risk Tier Configuration and Auto-Adjustment
**User Story:** As a developer, I want configurable risk tiers that auto-adjust based on how the system is performing, so that the engine takes less risk after losses and more risk when it is doing well.
#### Acceptance Criteria
1. THE Trading_Engine SHALL support three named Risk_Tiers with the following default parameters:
- **Conservative**: min_confidence = 0.75, max_position_pct = 0.05, stop_loss_atr_multiplier = 1.5, reward_risk_ratio = 2.0, max_sector_pct = 0.20, max_portfolio_heat = 0.10
- **Moderate**: min_confidence = 0.55, max_position_pct = 0.10, stop_loss_atr_multiplier = 2.0, reward_risk_ratio = 1.5, max_sector_pct = 0.30, max_portfolio_heat = 0.20
- **Aggressive**: min_confidence = 0.40, max_position_pct = 0.15, stop_loss_atr_multiplier = 2.5, reward_risk_ratio = 1.2, max_sector_pct = 0.40, max_portfolio_heat = 0.30
2. THE Risk_Tier_Controller SHALL evaluate the active Risk_Tier at a configurable interval (default: daily at market close) based on: trailing 30-day win rate, trailing 30-day Sharpe ratio, current drawdown from peak portfolio value, and Reserve_Pool size relative to total portfolio.
3. WHEN the trailing 30-day win rate drops below 40% or the current drawdown exceeds 15%, THE Risk_Tier_Controller SHALL downgrade the Risk_Tier by one level (aggressive → moderate, moderate → conservative).
4. WHEN the trailing 30-day win rate exceeds 55% and the Reserve_Pool exceeds 20% of total portfolio value and the current drawdown is below 5%, THE Risk_Tier_Controller SHALL upgrade the Risk_Tier by one level (conservative → moderate, moderate → aggressive).
5. WHEN the Risk_Tier_Controller changes the active Risk_Tier, THE Risk_Tier_Controller SHALL persist the change in PostgreSQL with the previous tier, new tier, and the metrics that triggered the change, and log the event.
6. THE Risk_Tier configuration SHALL be stored in PostgreSQL and editable through the API, allowing operators to override the auto-adjustment and manually set a tier.
### Requirement 6: Circuit Breakers
**User Story:** As a developer, I want automatic safety stops that halt all trading if things go badly wrong, so that a bad day or a flash crash does not wipe out the portfolio.
#### Acceptance Criteria
1. WHEN the portfolio value drops by more than a configurable daily loss percentage (default: 5%) within a single trading day, THE Circuit_Breaker SHALL halt all new order submissions and log the event with the loss amount and percentage.
2. WHEN a single position loses more than a configurable percentage of its entry value (default: 15%), THE Circuit_Breaker SHALL immediately close the position via a market sell order and prevent the Trading_Engine from re-entering the same ticker for a configurable cooldown period (default: 48 hours).
3. WHEN the Circuit_Breaker detects extreme market volatility (VIX equivalent proxy exceeding a configurable threshold, or more than 3 positions hitting stop-losses within a 30-minute window), THE Circuit_Breaker SHALL pause all trading for a configurable duration (default: 2 hours) and log the trigger condition.
4. WHEN a Circuit_Breaker is triggered, THE Trading_Engine SHALL send a notification to a configurable alert channel (Redis pub/sub event) and record the circuit breaker event in PostgreSQL with the trigger type, threshold, actual value, and timestamp.
5. WHEN a Circuit_Breaker cooldown expires, THE Trading_Engine SHALL resume normal operation automatically and log the resumption.
6. THE Circuit_Breaker status (active/inactive, trigger reason, cooldown remaining) SHALL be queryable through the API and visible on the dashboard.
### Requirement 7: Adaptive Market Response
**User Story:** As a developer, I want the system to react to sudden market changes in real-time rather than waiting for the next scheduled cycle, so that positions are protected during volatile events.
#### Acceptance Criteria
1. WHEN a new high-severity macro event (severity = high or critical) is classified by the existing macro classification pipeline, THE Trading_Engine SHALL trigger an immediate portfolio re-evaluation cycle outside the normal polling interval.
2. WHEN an immediate re-evaluation is triggered, THE Trading_Engine SHALL re-check all open positions against updated stop-loss levels, tighten stops by a configurable factor (default: 0.5x normal ATR multiplier) for the duration of the event, and evaluate whether any positions should be closed.
3. WHEN a tracked stock's price moves more than a configurable percentage (default: 5%) within a 15-minute window, THE Trading_Engine SHALL trigger an immediate re-evaluation for that specific position.
4. WHILE a high-severity macro event is active (within its estimated_duration window), THE Trading_Engine SHALL increase the polling frequency for stop-loss checks from the default interval to a configurable fast interval (default: every 60 seconds).
5. WHEN the Trading_Engine detects that multiple positions are simultaneously declining (more than 50% of open positions with negative unrealized P&L exceeding 2%), THE Trading_Engine SHALL reduce new position entries to zero and focus exclusively on managing existing positions until conditions stabilize.
### Requirement 8: Portfolio Rebalancing
**User Story:** As a developer, I want the system to periodically rebalance the portfolio to avoid over-concentration in any single stock or sector, so that diversification is maintained automatically.
#### Acceptance Criteria
1. THE Portfolio_Rebalancer SHALL run at a configurable interval (default: weekly, at market open on Monday) and evaluate the current portfolio against sector exposure limits and single-stock concentration limits defined by the active Risk_Tier.
2. WHEN a single stock position exceeds the Risk_Tier's max_position_pct of the Active_Pool, THE Portfolio_Rebalancer SHALL generate a partial sell order to bring the position back within limits.
3. WHEN a sector's total exposure exceeds the Risk_Tier's max_sector_pct of the Active_Pool, THE Portfolio_Rebalancer SHALL generate sell orders for the lowest-confidence positions in that sector to bring exposure within limits.
4. THE Portfolio_Rebalancer SHALL enforce a configurable maximum number of open positions (default: 10) and prevent new entries when the limit is reached, prioritizing higher-confidence opportunities over existing lower-confidence positions.
5. WHEN the Portfolio_Rebalancer generates rebalancing orders, THE Portfolio_Rebalancer SHALL submit them through the normal Broker_Service queue with a `rebalance` tag in the decision trace, and persist the rebalancing rationale in PostgreSQL.
6. THE Portfolio_Rebalancer SHALL respect the Trading_Window constraints and Circuit_Breaker status — rebalancing orders are not submitted outside market hours or when a circuit breaker is active.
### Requirement 9: Sector Diversification and Correlation Awareness
**User Story:** As a developer, I want the system to enforce diversification rules so that the portfolio is not over-exposed to a single sector or to stocks that move together.
#### Acceptance Criteria
1. THE Position_Sizer SHALL maintain a Correlation_Matrix computed from the trailing 90-day price history of all tracked companies, refreshed daily.
2. WHEN the Position_Sizer evaluates a new trade, THE Position_Sizer SHALL compute the weighted average correlation between the candidate stock and all existing portfolio positions, and reduce the allocation proportionally when the average correlation exceeds 0.5.
3. WHEN the weighted average correlation between a candidate stock and the existing portfolio exceeds 0.8, THE Position_Sizer SHALL reject the trade entirely and log the rejection reason.
4. THE Position_Sizer SHALL track sector exposure as the sum of market values of all positions in each sector, using the sector field from the companies table.
5. WHEN the portfolio holds positions in fewer than 3 sectors, THE Position_Sizer SHALL apply a diversification bonus (1.2x allocation multiplier) to trades in under-represented sectors to encourage diversification.
### Requirement 10: Earnings Calendar Awareness
**User Story:** As a developer, I want the system to reduce risk around earnings announcements because earnings are high-volatility events that can move stocks unpredictably.
#### Acceptance Criteria
1. THE Trading_Engine SHALL maintain an Earnings_Calendar table in PostgreSQL containing the next known earnings date for each tracked company, populated from market data or manual entry.
2. WHEN a tracked company's earnings date is within a configurable pre-earnings window (default: 3 trading days), THE Position_Sizer SHALL reduce the maximum position size for that company by 50% and THE Stop_Loss_Manager SHALL tighten stop-losses by a configurable factor (default: 0.7x normal ATR multiplier).
3. WHEN a tracked company's earnings date is within 1 trading day, THE Trading_Engine SHALL not open new positions in that company and SHALL flag existing positions for manual review via a dashboard alert.
4. WHEN a tracked company reports earnings and the result is reflected in the next recommendation cycle, THE Trading_Engine SHALL resume normal position sizing for that company after a configurable post-earnings cooldown (default: 1 trading day).
### Requirement 11: Trading Window and Gradual Entry
**User Story:** As a developer, I want the system to avoid trading during the volatile open and close periods and to scale into positions gradually rather than all at once.
#### Acceptance Criteria
1. THE Trading_Engine SHALL only submit new orders during the Trading_Window: market hours excluding the first 15 minutes after open (9:45 AM ET) and the last 15 minutes before close (3:45 PM ET).
2. WHEN the Trading_Engine is outside the Trading_Window, THE Trading_Engine SHALL queue pending decisions and execute them when the window reopens, unless the recommendation has expired or conditions have changed.
3. WHEN the Position_Sizer computes a position size exceeding a configurable gradual entry threshold (default: $30 or 5% of Active_Pool, whichever is smaller), THE Trading_Engine SHALL split the order into multiple tranches (default: 3 tranches) submitted at configurable intervals (default: 15 minutes apart).
4. WHEN executing a Gradual_Entry, THE Trading_Engine SHALL re-evaluate the recommendation confidence and price conditions before submitting each subsequent tranche, and cancel remaining tranches if conditions have deteriorated.
5. WHEN executing a Gradual_Entry, THE Trading_Engine SHALL link all tranches to the same parent decision record so that the full entry sequence is traceable.
### Requirement 12: Tax-Loss Harvesting Awareness
**User Story:** As a developer, I want the system to track cost basis and flag wash sale risks so that I have visibility into tax implications even during paper trading.
#### Acceptance Criteria
1. THE Trading_Engine SHALL maintain Tax_Lot records in PostgreSQL for every position entry, recording the acquisition date, quantity, cost basis per share, and current status (open, closed, washed).
2. WHEN the Trading_Engine closes a position at a loss, THE Trading_Engine SHALL check whether the same ticker was purchased within the preceding 30 days or will be purchased within the following 30 days (based on pending recommendations), and flag the trade as a potential wash sale.
3. WHEN a potential wash sale is detected, THE Trading_Engine SHALL log the event, tag the Tax_Lot record, and include the wash sale flag in the trade's decision trace.
4. THE Performance_Tracker SHALL compute realized gains and losses using the Tax_Lot cost basis (FIFO method) and include a tax-adjusted P&L alongside the raw P&L in portfolio metrics.
### Requirement 13: Portfolio Heat Management
**User Story:** As a developer, I want the system to limit the total risk across all positions so that even if every stop-loss is hit simultaneously, the portfolio loss is bounded.
#### Acceptance Criteria
1. THE Position_Sizer SHALL compute Portfolio_Heat as the sum across all open positions of: `position_value * (entry_price - stop_loss_price) / entry_price`.
2. WHEN the current Portfolio_Heat exceeds the Risk_Tier's max_portfolio_heat percentage of the Active_Pool, THE Position_Sizer SHALL reject new position entries until existing positions are closed or stop-losses are tightened to reduce heat.
3. WHEN the Portfolio_Heat exceeds 80% of the max_portfolio_heat threshold, THE Stop_Loss_Manager SHALL proactively tighten stop-losses on the lowest-confidence positions to reduce overall heat.
4. THE Portfolio_Heat value SHALL be computed and persisted at every decision loop iteration and be queryable through the API and visible on the dashboard.
### Requirement 14: Performance Tracking and Metrics
**User Story:** As a developer, I want real-time performance metrics and dashboards so that I can see how the autonomous engine is performing at a glance.
#### Acceptance Criteria
1. THE Performance_Tracker SHALL compute and persist the following metrics at a configurable interval (default: every 5 minutes during market hours): total portfolio value, Active_Pool value, Reserve_Pool balance, unrealized P&L, realized P&L, daily P&L, win count, loss count, win rate, average win amount, average loss amount, profit factor, Sharpe ratio (annualized, using daily returns over trailing 30 days), max drawdown (from peak portfolio value), and current drawdown percentage.
2. THE Performance_Tracker SHALL compute per-trade metrics for every closed position: entry price, exit price, hold duration, P&L amount, P&L percentage, and the recommendation ID that triggered the trade.
3. THE Performance_Tracker SHALL persist daily portfolio snapshots in PostgreSQL containing end-of-day portfolio value, daily return, cumulative return, and all positions with their unrealized P&L.
4. THE Dashboard SHALL display a trading engine overview panel showing: current Risk_Tier, Circuit_Breaker status, Active_Pool and Reserve_Pool balances, Portfolio_Heat gauge, and the last 24 hours of P&L.
5. THE Dashboard SHALL display a portfolio composition panel showing: current positions with entry price, current price, unrealized P&L, stop-loss level, take-profit level, and sector allocation pie chart.
6. THE Dashboard SHALL display a trade history panel showing: completed trades with entry/exit prices, P&L, hold duration, and the recommendation thesis that triggered each trade.
7. THE Dashboard SHALL display a performance chart panel showing: cumulative P&L over time, daily returns bar chart, and drawdown chart, using the existing Recharts library.
### Requirement 15: Backtesting
**User Story:** As a developer, I want to backtest the trading strategy against historical data already in the database before risking real money, so that I can validate the approach.
#### Acceptance Criteria
1. THE Backtester SHALL accept a date range, initial capital amount, and Risk_Tier configuration as inputs, and replay historical recommendations from the recommendations table within that date range.
2. WHEN replaying historical recommendations, THE Backtester SHALL simulate the Trading_Engine's decision logic (position sizing, stop-loss/take-profit, circuit breakers, reserve pool, rebalancing) using historical price data from the market data tables.
3. THE Backtester SHALL produce the same set of metrics as the Performance_Tracker (total return, Sharpe ratio, max drawdown, win rate, profit factor, trade count) for the simulated period.
4. THE Backtester SHALL persist backtest results in PostgreSQL with a unique backtest_id, the configuration used, and the full trade log, so that results can be compared across different configurations.
5. THE Backtester SHALL be invocable through the API at `POST /api/trading/backtest` with the date range, capital, and risk tier parameters, and return the backtest_id for result retrieval.
6. THE Dashboard SHALL display a backtesting panel where the operator can configure and launch backtests, and view results including the equity curve, trade log, and summary metrics.
### Requirement 16: Trading Engine Configuration and Control
**User Story:** As a developer, I want to configure, start, stop, and monitor the trading engine through the API and dashboard, so that I have full control over autonomous operation.
#### Acceptance Criteria
1. THE Trading_Engine SHALL be configurable through a trading_engine_config table in PostgreSQL containing: enabled (boolean), risk_tier (conservative/moderate/aggressive), reserve_siphon_pct, polling_interval_seconds, gradual_entry_tranches, and all circuit breaker thresholds.
2. THE API SHALL expose a `GET /api/trading/status` endpoint returning the current engine state: enabled/disabled, active Risk_Tier, Circuit_Breaker status, Active_Pool balance, Reserve_Pool balance, Portfolio_Heat, open position count, and last decision timestamp.
3. THE API SHALL expose a `PUT /api/trading/config` endpoint allowing operators to update engine configuration, with changes taking effect on the next decision loop iteration.
4. THE API SHALL expose a `POST /api/trading/pause` and `POST /api/trading/resume` endpoint to temporarily halt and resume autonomous trading without changing the enabled configuration.
5. THE Dashboard SHALL display a trading engine control panel with start/pause/resume controls, Risk_Tier selector, and real-time engine status indicators.
6. WHEN the Trading_Engine configuration changes, THE Trading_Engine SHALL record an audit event with the previous configuration, new configuration, and the source of the change (operator or auto-adjustment).
### Requirement 17: Decision Audit Trail
**User Story:** As a developer, I want a complete audit trail of every decision the trading engine makes, so that I can understand why it took or skipped every trade.
#### Acceptance Criteria
1. WHEN the Trading_Engine evaluates a recommendation, THE Trading_Engine SHALL persist a trading_decision record containing: recommendation_id, decision (act or skip), skip_reason (if skipped), computed_position_size, risk_tier_at_decision, portfolio_heat_at_decision, active_pool_at_decision, reserve_pool_at_decision, circuit_breaker_status, correlation_check_result, sector_exposure_check_result, earnings_proximity_flag, and timestamp.
2. WHEN the Trading_Engine submits an order, THE Trading_Engine SHALL include the trading_decision_id in the order job payload so that the existing Broker_Service decision_trace links back to the autonomous engine's reasoning.
3. THE API SHALL expose a `GET /api/trading/decisions` endpoint returning recent trading decisions with pagination, filterable by ticker, decision type (act/skip), and date range.
4. THE Dashboard SHALL display a decision log panel showing recent decisions with expandable detail showing the full reasoning chain from recommendation through position sizing through execution.
### Requirement 18: Trading Engine Storage
**User Story:** As a data engineer, I want all trading engine state persisted in PostgreSQL with appropriate schemas, so that the engine can recover from restarts and data is available for analysis.
#### Acceptance Criteria
1. THE System SHALL create a database migration adding tables for: trading_engine_config, reserve_pool_ledger, risk_tier_history, circuit_breaker_events, trading_decisions, position_stop_levels, portfolio_snapshots, backtest_runs, backtest_trades, tax_lots, earnings_calendar, and correlation_matrix_cache.
2. THE reserve_pool_ledger table SHALL record every deposit and withdrawal with amount, balance_after, trigger (profit_siphon, emergency_liquidation, manual_adjustment), and timestamp.
3. THE position_stop_levels table SHALL record every stop-loss and take-profit level and adjustment for every open position, with the computation inputs (ATR value, multiplier, signal confidence) for reproducibility.
4. THE portfolio_snapshots table SHALL store daily end-of-day snapshots with portfolio value, active pool, reserve pool, positions JSON, and all computed metrics.
5. WHEN the Trading_Engine restarts, THE Trading_Engine SHALL reconstruct its state from PostgreSQL (open positions, reserve pool balance, active risk tier, circuit breaker status) and resume operation without data loss.
### Requirement 19: Notification System
**User Story:** As a developer who is not watching the dashboard all day, I want the system to send me text messages and emails for critical events, so that I know immediately when something important happens.
#### Acceptance Criteria
1. THE Notification_Service SHALL support two delivery channels: SMS via AWS SNS and email via the Gmail API, each independently configurable and toggleable.
2. THE Notification_Service SHALL send an immediate notification WHEN any of the following events occur: a Circuit_Breaker is triggered, a Circuit_Breaker cooldown expires and trading resumes, the Risk_Tier_Controller changes the active Risk_Tier, the Reserve_Pool receives an emergency liquidation, or a single trade P&L exceeds a configurable threshold (default: 5% of Active_Pool).
3. THE Notification_Service SHALL send a daily performance summary notification at a configurable time (default: 30 minutes after market close) containing: daily P&L, total portfolio value, Active_Pool and Reserve_Pool balances, number of trades executed, current Risk_Tier, and any open circuit breaker status.
4. THE Notification_Service SHALL send a weekly performance digest containing: weekly P&L, win/loss count, Sharpe ratio, max drawdown, top winning and losing trades, and Risk_Tier changes during the week.
5. WHEN configuring SMS notifications, THE Notification_Service SHALL use AWS SNS with a configurable SNS topic ARN and phone number, authenticating via AWS credentials (access key and secret key) stored as environment variables or Kubernetes secrets.
6. WHEN configuring email notifications, THE Notification_Service SHALL use the Gmail API with OAuth2 credentials (client ID, client secret, refresh token) stored as environment variables or Kubernetes secrets, sending from a configurable sender address to a configurable recipient address.
7. THE Notification_Service SHALL implement rate limiting (default: maximum 10 SMS and 20 emails per hour) to prevent notification storms during volatile market conditions.
8. THE Notification_Service SHALL persist all sent notifications in PostgreSQL with the channel, event type, message content, delivery status, and timestamp for audit purposes.
9. THE API SHALL expose a `GET /api/trading/notifications/config` and `PUT /api/trading/notifications/config` endpoint for viewing and updating notification preferences (channels enabled, phone number, email address, event types, rate limits).
10. THE Dashboard SHALL display a notification preferences panel where the operator can configure which events trigger notifications, select delivery channels, and view recent notification history.
11. IF a notification delivery fails (SNS publish error or Gmail API error), THEN THE Notification_Service SHALL retry up to 3 times with exponential backoff and log the failure, but SHALL NOT block or delay trading operations.
### Requirement 20: Micro-Trading Mode
**User Story:** As a developer, I want the option to enable faster trading cycles that evaluate short-term signals on 5-minute and 15-minute intervals, so that the engine can capture intraday opportunities without being limited to daily recommendation cycles.
#### Acceptance Criteria
1. THE Trading_Engine SHALL support an optional Micro_Trading_Mode that, when enabled, polls for intraday trend window signals (intraday and 1d windows) at a configurable fast interval (default: every 5 minutes during market hours) in addition to the standard recommendation polling.
2. WHEN Micro_Trading_Mode is enabled, THE Trading_Engine SHALL evaluate intraday trend summaries from the Aggregation_Engine and generate micro-trade decisions for signals that meet the active Risk_Tier's confidence threshold.
3. WHEN Micro_Trading_Mode generates a trade, THE Position_Sizer SHALL apply a configurable micro-trade allocation cap (default: 3% of Active_Pool per micro-trade) that is lower than the standard position sizing cap, limiting the risk of any single short-term trade.
4. WHEN Micro_Trading_Mode is enabled, THE Trading_Engine SHALL enforce a configurable maximum number of micro-trades per day (default: 10) to prevent excessive trading and commission accumulation.
5. WHEN Micro_Trading_Mode is enabled, THE Stop_Loss_Manager SHALL use tighter stop-loss and take-profit levels for micro-trade positions: stop-loss at 1.0x ATR (instead of the standard tier multiplier) and take-profit at 1.5x the stop distance.
6. WHEN a micro-trade position has been open for longer than a configurable maximum hold duration (default: 2 hours), THE Trading_Engine SHALL close the position at market price regardless of P&L, to prevent short-term trades from becoming unintended long-term holds.
7. THE Trading_Engine SHALL track micro-trade performance metrics separately from standard trade metrics, including: micro-trade win rate, average micro-trade P&L, micro-trade count per day, and micro-trade contribution to total portfolio P&L.
8. THE Micro_Trading_Mode SHALL be toggleable through the trading_engine_config table and the API, independently of the main Trading_Engine enabled state.
9. THE Dashboard SHALL display a micro-trading panel showing: micro-trade mode status (enabled/disabled), today's micro-trade count and P&L, active micro-trade positions, and micro-trade performance metrics over the trailing 7 days.
10. WHEN Micro_Trading_Mode is enabled, THE Trading_Engine SHALL respect all existing constraints (Trading_Window, Circuit_Breakers, Portfolio_Heat limits, correlation checks) and apply them to micro-trades with the same rigor as standard trades.
@@ -0,0 +1,656 @@
# Implementation Plan: Autonomous Trading Engine
## Overview
This plan implements a fully autonomous trading engine as a new service (`services/trading/`) that consumes recommendations from the existing three-layer signal aggregation pipeline, applies confidence-based position sizing with reserve pool management, enforces dynamic stop-loss/take-profit levels, manages circuit breakers, and submits orders through the existing Broker Service queue. The implementation extends existing services (broker_service, recommendation, risk engine, query API, dashboard) without replacing them. Tasks are ordered so each step builds on the previous, with property-based tests validating core computation logic early.
## Tasks
- [x] 1. Database migration and shared infrastructure
- [x] 1.1 Create PostgreSQL migration `infra/migrations/018_autonomous_trading_engine.sql`
- Add `trading_engine_config` table with all configuration fields (enabled, paused, risk_tier, reserve_siphon_pct, polling intervals, gradual entry params, circuit breaker thresholds, active pool minimum, emergency drawdown threshold, correlation thresholds, earnings windows, micro-trading params, notification settings, timestamps)
- Add `reserve_pool_ledger` table with amount, balance_after, trigger_type (profit_siphon, emergency_liquidation, manual_adjustment, initial), reference_id, notes, created_at; index on created_at DESC
- Add `risk_tier_history` table with previous_tier, new_tier, trigger_source, trigger_metrics JSONB, created_at; index on created_at DESC
- Add `circuit_breaker_events` table with trigger_type (daily_loss, single_position, volatility, manual), threshold_value, actual_value, ticker, cooldown_expires, resolved_at, created_at; partial index on active (unresolved) events
- Add `trading_decisions` table with recommendation_id FK, decision, skip_reason, ticker, computed_position_size, computed_share_quantity, risk_tier_at_decision, portfolio_heat_at_decision, active_pool_at_decision, reserve_pool_at_decision, circuit_breaker_status, correlation_check_result JSONB, sector_exposure_check_result JSONB, earnings_proximity_flag, is_micro_trade, decision_trace JSONB, created_at; indexes on ticker, recommendation_id, decision
- Add `position_stop_levels` table with ticker, entry_price, stop_loss_price, take_profit_price, trailing_stop_active, atr_value, atr_multiplier, reward_risk_ratio, signal_confidence, is_micro_trade, active, timestamps; partial index on active positions
- Add `portfolio_snapshots` table with snapshot_date (UNIQUE), portfolio_value, active_pool, reserve_pool, daily_return, cumulative_return, unrealized_pnl, realized_pnl, win/loss counts, win_rate, sharpe_ratio, max_drawdown, current_drawdown_pct, portfolio_heat, risk_tier, positions JSONB, metrics JSONB, created_at; index on snapshot_date DESC
- Add `backtest_runs` table with start_date, end_date, initial_capital, risk_tier, config JSONB, result metrics, equity_curve JSONB, status, completed_at, created_at
- Add `backtest_trades` table with backtest_id FK (CASCADE), ticker, side, entry/exit prices, quantity, pnl, dates, hold_duration_days, recommendation_id; index on backtest_id
- Add `tax_lots` table with ticker, quantity, cost_basis_per_share, acquisition_date, status (open/closed/washed), closed_date, exit_price, realized_pnl, wash_sale_flag, wash_sale_details, order_id FK; indexes on ticker and open status
- Add `earnings_calendar` table with ticker, earnings_date, source, confirmed, timestamps; UNIQUE on (ticker, earnings_date); indexes on date and ticker
- Add `correlation_matrix_cache` table with ticker_a, ticker_b, correlation_coefficient, lookback_days, computed_at; UNIQUE on (ticker_a, ticker_b)
- Add `notifications` table with channel (sms/email), event_type, message, delivery_status (pending/delivered/failed/rate_limited), retry_count, error_message, created_at, delivered_at; indexes on created_at and event_type
- Insert default trading_engine_config row with moderate tier defaults
- Insert initial reserve_pool_ledger entry with balance 0.0 and trigger_type 'initial'
- _Requirements: 18.1, 18.2, 18.3, 18.4, 16.1_
- [x] 1.2 Add new Pydantic schemas and enums to `services/shared/schemas.py`
- Add `TradingDecisionType` enum (act, skip)
- Add `CircuitBreakerTriggerType` enum (daily_loss, single_position, volatility, manual)
- Add `ReservePoolTriggerType` enum (profit_siphon, emergency_liquidation, manual_adjustment, initial)
- Add `NotificationChannel` enum (sms, email)
- Add `RiskTierName` enum (conservative, moderate, aggressive)
- _Requirements: 5.1, 6.1, 3.1, 19.1_
- [x] 1.3 Add trading-related Redis keys to `services/shared/redis_keys.py`
- Add `QUEUE_TRADING_DECISIONS = "trading_decisions"` queue name
- Add `TRADING_DEDUPE_PREFIX` for recommendation deduplication (`stonks:dedupe:trading`)
- Add `TRADING_CB_PREFIX` for circuit breaker state (`stonks:trading:circuit_breaker`)
- Add `TRADING_NOTIFICATION_RATE` for notification rate limiting (`stonks:trading:notification_rate`)
- _Requirements: 1.5, 6.4, 19.7_
- [x] 1.4 Add `TradingConfig` dataclass to `services/shared/config.py`
- Add `TradingConfig` with fields: enabled, risk_tier, reserve_siphon_pct, polling_interval_seconds, stop_loss_check_interval_seconds, fast_stop_loss_interval_seconds, gradual_entry_tranches, gradual_entry_threshold_dollars, absolute_position_cap, active_pool_minimum, emergency_drawdown_threshold_pct, reserve_high_water_pct, micro_trading_enabled, micro_trading_interval_seconds, micro_trading_allocation_cap_pct, micro_trading_max_daily, micro_trading_max_hold_minutes, sns_topic_arn, sns_phone_number, gmail_sender, gmail_recipient
- Add `trading: TradingConfig` field to `AppConfig` with env var loading in `load_config()`
- _Requirements: 16.1, 20.1, 19.5, 19.6_
- [x] 2. Checkpoint — Ensure migration and shared schemas are consistent
- Ensure all tests pass, ask the user if questions arise.
- [x] 3. Core data models and risk tier configuration
- [x] 3.1 Create `services/trading/__init__.py` and `services/trading/models.py`
- Create the `services/trading/` package directory
- Define `RiskTierConfig` dataclass with fields: name, min_confidence, max_position_pct, stop_loss_atr_multiplier, reward_risk_ratio, max_sector_pct, max_portfolio_heat
- Define `RISK_TIER_DEFAULTS` dict mapping conservative/moderate/aggressive to their default `RiskTierConfig` instances per the design specification
- Define `PortfolioState` dataclass with fields: positions (list), total_value, cash, active_pool, reserve_pool, sector_exposure (dict), portfolio_heat, open_position_count
- Define `TradingDecision` dataclass with all fields matching the `trading_decisions` table schema
- Define `PositionSizeResult` dataclass with dollar_amount, share_quantity, allocation_pct, adjustments list, rejected flag, rejection_reason
- Define `StopLevels` dataclass with stop_loss_price, take_profit_price, trailing_stop_active, atr_value, atr_multiplier, reward_risk_ratio, last_updated
- Define `OpenPosition` dataclass with ticker, quantity, entry_price, current_price, unrealized_pnl, market_value, sector, stop_loss_price, take_profit_price, signal_confidence, is_micro_trade
- Define `ClosedTrade` dataclass with ticker, entry_price, exit_price, quantity, pnl, pnl_pct, hold_duration, recommendation_id, is_micro_trade
- Define `PerformanceMetrics` dataclass with all fields from the design (total_portfolio_value through computed_at)
- Define `CircuitBreakerState` dataclass with active, trigger_type, triggered_at, cooldown_expires, ticker_cooldowns dict
- Define `ReservePoolState` dataclass with balance, total_deposits, total_withdrawals, last_updated
- Define `StopTrigger` dataclass with ticker, trigger_type (stop_loss/take_profit), current_price, trigger_price
- _Requirements: 5.1, 1.2, 2.1, 4.1, 6.1, 3.1, 13.1, 14.1_
- [x] 3.2 Write property test for risk tier default parameters
- **Property 29 (partial): Persistence round-trip for risk tier configs**
- Verify all three tier defaults have valid parameter ranges (min_confidence in [0,1], max_position_pct in (0,1], etc.)
- Verify conservative < moderate < aggressive for min_confidence thresholds (inverse) and position limits
- **Validates: Requirements 5.1**
- [x] 4. Position Sizer implementation
- [x] 4.1 Implement `services/trading/position_sizer.py`
- Implement `PositionSizer` class with `compute()` method accepting confidence, ticker, sector, current_price, active_pool, risk_tier, portfolio_state, correlation_matrix, earnings_calendar
- Implement sizing formula: `raw_pct = base_allocation_pct * (confidence / min_confidence) * multiplier`, clamped to max_position_pct, then dollar_amount = active_pool * clamped_pct, clamped to absolute_position_cap
- Implement confidence gate: reject when confidence < risk_tier.min_confidence
- Implement correlation reduction: compute weighted average correlation with existing positions; reduce proportionally when avg > 0.5; reject entirely when avg > 0.8
- Implement sector exposure reduction: reduce allocation if adding position would push sector above max_sector_pct
- Implement diversification bonus: 1.2x multiplier for under-represented sectors when portfolio holds < 3 sectors
- Implement earnings proximity: reduce by 50% within 3 trading days; reject within 1 trading day
- Implement portfolio heat check: reject if current heat + new position heat exceeds max_portfolio_heat
- Implement active pool minimum: reject new entries when Active Pool < configured minimum ($100 default)
- Implement absolute cap enforcement and share rounding (round down to whole shares, reject if quantity = 0)
- _Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 9.2, 9.3, 9.4, 9.5, 10.2, 10.3, 13.1, 13.2, 3.5_
- [x] 4.2 Write property test for position sizing formula and invariants
- **Property 1: Position sizing formula and invariants**
- Generate random confidence values, Active Pool balances, stock prices, and RiskTierConfig objects
- Verify zero allocation when confidence < min_confidence
- Verify allocation never exceeds max_position_pct or absolute_position_cap
- Verify share quantity is rounded down to whole shares
- Verify rejection when rounded quantity is zero
- **Validates: Requirements 2.1, 2.2, 2.3, 2.4, 2.7**
- [x] 4.3 Write property test for correlation-based allocation adjustment
- **Property 2: Correlation-based allocation adjustment**
- Generate random correlation matrices and portfolio positions
- Verify allocation reduced when weighted avg correlation > 0.5
- Verify trade rejected when weighted avg correlation > 0.8
- Verify allocation unchanged when weighted avg correlation <= 0.5
- Verify monotonic non-increase: higher correlation → lower or equal allocation
- **Validates: Requirements 2.5, 9.2, 9.3**
- [x] 4.4 Write property test for sector exposure enforcement
- **Property 3: Sector exposure computation and enforcement**
- Generate random portfolios with sector labels
- Verify sector exposure equals sum of market values per sector
- Verify allocation reduced when adding position would exceed max_sector_pct
- **Validates: Requirements 2.6, 9.4**
- [x] 4.5 Write property test for diversification bonus
- **Property 4: Diversification bonus for under-represented sectors**
- Generate portfolios with varying sector counts
- Verify 1.2x bonus applied when portfolio has < 3 sectors and trade is in new sector
- Verify no bonus when portfolio has >= 3 sectors
- **Validates: Requirements 9.5**
- [x] 4.6 Write property test for Active Pool computation
- **Property 5: Active Pool computation invariant**
- Generate random total_portfolio_value and reserve_pool_balance
- Verify Active Pool = total_portfolio_value - reserve_pool_balance
- **Validates: Requirements 3.3**
- [x] 4.7 Write property test for earnings proximity adjustments
- **Property 19: Earnings proximity adjustments**
- Generate random earnings dates relative to current date
- Verify 50% reduction within 3 trading days
- Verify rejection within 1 trading day
- Verify normal sizing outside earnings window
- **Validates: Requirements 10.2, 10.3**
- [x] 4.8 Write property test for portfolio heat computation and enforcement
- **Property 24: Portfolio heat computation and threshold enforcement**
- Generate random open positions with entry prices and stop-loss levels
- Verify heat = sum of position_value * (entry_price - stop_loss_price) / entry_price
- Verify new entries rejected when heat exceeds max_portfolio_heat
- **Validates: Requirements 13.1, 13.2**
- [x] 4.9 Write property test for Active Pool minimum halts entries
- **Property 7: Active Pool minimum halts new entries but allows exits**
- Generate portfolio states with Active Pool below and above minimum
- Verify buy orders rejected when Active Pool < minimum
- Verify sell orders allowed regardless of Active Pool
- **Validates: Requirements 3.5**
- [x] 5. Checkpoint — Ensure position sizer logic and property tests pass
- Ensure all tests pass, ask the user if questions arise.
- [x] 6. Stop-Loss Manager implementation
- [x] 6.1 Implement `services/trading/stop_loss_manager.py`
- Implement `StopLossManager` class with `compute_initial_levels()` method: stop_loss = entry_price - (ATR * stop_loss_atr_multiplier), take_profit = entry_price + (stop_distance * reward_risk_ratio)
- Implement `re_evaluate_levels()` method: adjust if ATR changed > 10% or signal conditions changed; respect configurable interval (default 5 min)
- Implement `check_price_crossings()` method: return list of StopTrigger for positions where current price <= stop_loss or >= take_profit
- Implement trailing stop logic: when price moves favorably by > 50% of take-profit distance, move stop-loss to entry price (breakeven)
- Implement earnings proximity tightening: 0.7x ATR multiplier when earnings within 3 trading days
- Implement high-severity event tightening: 0.5x normal ATR multiplier during active macro events
- Implement proactive heat tightening: tighten stops on lowest-confidence positions when heat > 80% of max
- Implement price data unavailability safety: close position if price unavailable > 15 minutes during market hours
- Persist all stop levels and adjustments to `position_stop_levels` table
- _Requirements: 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 7.2, 10.2, 13.3_
- [x] 6.2 Write property test for stop-loss and take-profit initial computation
- **Property 9: Stop-loss and take-profit initial computation**
- Generate random entry prices, ATR values, and RiskTierConfig objects
- Verify stop-loss = entry_price - (ATR * multiplier) and always below entry
- Verify take-profit = entry_price + (stop_distance * reward_risk_ratio) and always above entry
- **Validates: Requirements 4.1, 4.2**
- [x] 6.3 Write property test for price crossing triggers
- **Property 10: Price crossing triggers immediate sell**
- Generate random positions with stop/take-profit levels and current prices
- Verify sell triggered when price <= stop_loss or >= take_profit
- Verify no trigger when price is between stop_loss and take_profit
- **Validates: Requirements 4.4, 4.5**
- [x] 6.4 Write property test for trailing stop activation
- **Property 11: Trailing stop activation at 50% of take-profit distance**
- Generate random positions with varying favorable price moves
- Verify trailing stop activates (stop moves to entry) when move > 50% of TP distance
- Verify trailing stop does not activate when move <= 50%
- **Validates: Requirements 4.6**
- [x] 6.5 Write property test for stop tightening during high-severity events
- **Property 15: Stop tightening during high-severity events**
- Generate random positions and ATR values
- Verify tightened stop uses 0.5x normal multiplier
- Verify tightened stop is closer to current price than normal stop
- **Validates: Requirements 7.2**
- [x] 6.6 Write property test for proactive stop tightening at 80% heat
- **Property 25: Proactive stop tightening at 80% heat threshold**
- Generate portfolios with heat near the threshold
- Verify lowest-confidence positions get stops tightened first
- **Validates: Requirements 13.3**
- [x] 7. Reserve Pool Controller implementation
- [x] 7.1 Implement `services/trading/reserve_pool.py`
- Implement `ReservePoolController` class with `siphon_profit()` method: transfer configured percentage of realized profit to reserve, persist to `reserve_pool_ledger`
- Implement `emergency_liquidate()` method: release entire reserve into active pool, log event, persist to ledger
- Implement `compute_active_pool()` method: total_portfolio_value - reserve_pool_balance
- Implement `get_state()` method: load current balance and history from PostgreSQL
- Implement high-water mark detection: signal when reserve > 30% of total portfolio
- _Requirements: 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7_
- [x] 7.2 Write property test for reserve pool siphon computation
- **Property 6: Reserve pool siphon computation**
- Generate random realized profit amounts and siphon percentages
- Verify transferred amount = realized_profit * siphon_pct
- Verify balance_after = previous_balance + transferred_amount
- **Validates: Requirements 3.1, 3.2**
- [x] 7.3 Write property test for emergency drawdown triggers reserve liquidation
- **Property 8: Emergency drawdown triggers reserve liquidation**
- Generate portfolio states with drawdowns above and below emergency threshold
- Verify reserve liquidated into active pool when drawdown exceeds threshold
- Verify risk tier set to conservative after emergency liquidation
- **Validates: Requirements 3.6**
- [x] 8. Circuit Breaker implementation
- [x] 8.1 Implement `services/trading/circuit_breaker.py`
- Implement `CircuitBreaker` class with `check_daily_loss()`: activate when portfolio drops > configured daily_loss_pct
- Implement `check_single_position()`: close position and apply ticker cooldown when loss > configured single_position_loss_pct
- Implement `check_volatility()`: pause trading when 3+ positions hit stop-losses within 30-minute window
- Implement `is_ticker_cooled_down()`: check per-ticker re-entry cooldowns
- Implement `is_active()`: return whether any circuit breaker is currently active
- Implement cooldown expiry: auto-resolve when current time > triggered_at + cooldown_duration
- Persist all circuit breaker events to `circuit_breaker_events` table
- Store active state in Redis for fast lookup
- _Requirements: 6.1, 6.2, 6.3, 6.4, 6.5, 6.6_
- [x] 8.2 Write property test for circuit breaker activation
- **Property 13: Circuit breaker activation**
- Generate random portfolio states with varying daily losses, position losses, and stop-loss hit sequences
- Verify daily_loss trigger when loss > threshold
- Verify single_position trigger and ticker cooldown when position loss > threshold
- Verify volatility trigger when 3+ stop-losses within 30 minutes
- Verify all new orders rejected when any circuit breaker is active
- **Validates: Requirements 6.1, 6.2, 6.3**
- [x] 8.3 Write property test for circuit breaker cooldown expiry
- **Property 14: Circuit breaker cooldown expiry**
- Generate circuit breaker events with varying cooldown durations and current times
- Verify transition from active to resolved when time > triggered_at + cooldown
- Verify remains active before expiry
- **Validates: Requirements 6.5**
- [x] 9. Risk Tier Controller implementation
- [x] 9.1 Implement `services/trading/risk_tier_controller.py`
- Implement `RiskTierController` class with `evaluate()` method accepting PerformanceMetrics and reserve_pct
- Implement downgrade logic: downgrade one level when trailing 30-day win rate < 40% OR current drawdown > 15%
- Implement upgrade logic: upgrade one level when win rate > 55% AND reserve > 20% of total AND drawdown < 5%
- Implement tier bounds: never go below conservative or above aggressive
- Persist tier changes to `risk_tier_history` table with previous tier, new tier, and trigger metrics
- _Requirements: 5.2, 5.3, 5.4, 5.5, 5.6_
- [x] 9.2 Write property test for risk tier auto-adjustment conditions
- **Property 12: Risk tier auto-adjustment conditions**
- Generate random performance metrics (win rate, drawdown, reserve percentage)
- Verify downgrade when win rate < 40% OR drawdown > 15%
- Verify upgrade when win rate > 55% AND reserve > 20% AND drawdown < 5%
- Verify no change when neither condition met
- Verify tier never goes below conservative or above aggressive
- **Validates: Requirements 5.3, 5.4**
- [x] 10. Checkpoint — Ensure core components and property tests pass
- Ensure all tests pass, ask the user if questions arise.
- [x] 11. Correlation Matrix and Tax Lot Tracker
- [x] 11.1 Implement `services/trading/correlation.py`
- Implement `CorrelationMatrix` class with `refresh()` method: compute trailing 90-day price correlations from market data tables, persist to `correlation_matrix_cache` table
- Implement `get_correlation()` method: return coefficient for a ticker pair, 0.0 if unknown
- Implement `get_portfolio_correlation()` method: weighted average correlation between candidate and existing positions
- Cache in-memory after refresh; schedule daily refresh
- _Requirements: 9.1, 9.2, 9.3_
- [x] 11.2 Implement `services/trading/tax_lots.py`
- Implement `TaxLotTracker` class with `record_entry()` method: create tax lot record in PostgreSQL
- Implement `close_lots_fifo()` method: close lots in FIFO order, compute realized P&L per lot
- Implement `check_wash_sale()` method: check 30-day window before and after for same-ticker purchases
- Persist wash sale flags and details to tax_lots table
- _Requirements: 12.1, 12.2, 12.3, 12.4_
- [x] 11.3 Write property test for tax lot FIFO ordering
- **Property 22: Tax lot FIFO ordering**
- Generate random sequences of buy/sell transactions for the same ticker
- Verify lots closed in FIFO order (earliest acquired first)
- Verify realized P&L = (exit_price - cost_basis_per_share) * quantity per lot
- **Validates: Requirements 12.4**
- [x] 11.4 Write property test for wash sale detection
- **Property 23: Wash sale detection within 30-day window**
- Generate random loss-closing dates and purchase dates
- Verify wash sale flagged when same ticker purchased within 30 days before or after loss
- Verify no flag when purchases are outside the 30-day window
- **Validates: Requirements 12.2, 12.3**
- [x] 12. Trading Window and Gradual Entry logic
- [x] 12.1 Implement `services/trading/trading_window.py`
- Implement `is_within_trading_window()` function: return True if timestamp is between 9:45 AM ET and 3:45 PM ET on a market day
- Implement `next_window_open()` function: return the next timestamp when the trading window opens
- Implement `is_market_open()` function: check if current time is during US market hours (9:30 AM - 4:00 PM ET)
- _Requirements: 11.1, 11.2_
- [x] 12.2 Implement gradual entry logic in `services/trading/gradual_entry.py`
- Implement `should_use_gradual_entry()`: return True when position size exceeds min($30, 5% of Active Pool)
- Implement `split_into_tranches()`: split order into configured number of tranches (default 3) of approximately equal size
- Implement `GradualEntryManager` class to track pending tranches, re-evaluate before each submission, cancel remaining if conditions deteriorate
- Link all tranches to the same parent trading decision ID
- _Requirements: 11.3, 11.4, 11.5_
- [x] 12.3 Write property test for trading window determination
- **Property 20: Trading window determination**
- Generate random timestamps across US market hours
- Verify within-window classification for 9:45 AM - 3:45 PM ET
- Verify outside-window classification for all other times
- **Validates: Requirements 11.1**
- [x] 12.4 Write property test for gradual entry tranche splitting
- **Property 21: Gradual entry tranche splitting**
- Generate random position sizes above and below the threshold
- Verify splitting into configured number of tranches when above threshold
- Verify all tranches reference the same parent decision ID
- Verify tranche sizes are approximately equal
- **Validates: Requirements 11.3, 11.5**
- [x] 13. Autonomous Decision Loop (core engine)
- [x] 13.1 Implement `services/trading/engine.py`
- Implement `TradingEngine` class with `__init__()` accepting asyncpg.Pool, aioredis.Redis, and TradingEngineConfig
- Implement `start()` method: load portfolio state from Broker Service (positions, account balance), load active risk tier from PostgreSQL, load reserve pool balance, load circuit breaker status, load open stop levels, enter decision loop
- Implement `stop()` method: graceful shutdown — cancel pending tranches, persist state
- Implement `decision_loop()` method: poll recommendations at configured interval, evaluate each, size positions, submit orders
- Implement `poll_recommendations()` method: fetch from `recommendations` table where action IN (buy, sell) AND mode IN (paper_eligible, live_eligible) AND generated_at > last_poll_timestamp, ordered by confidence DESC
- Implement recommendation deduplication: check Redis key `stonks:dedupe:trading:{recommendation_id}` with 24h TTL, mark before evaluation
- Implement `evaluate_recommendation()` method: run all pre-trade checks (circuit breaker, trading window, risk tier confidence, portfolio heat, sector exposure, correlation, earnings proximity) and produce a TradingDecision record
- Implement `execute_decision()` method: generate order job payload matching existing broker queue schema, push to `stonks:queue:broker_orders`, handle gradual entry for large positions
- Persist every decision (act or skip) to `trading_decisions` table with full reasoning chain
- Implement adaptive market response: trigger immediate re-evaluation on high-severity macro events, tighten stops during events, increase polling frequency
- Implement rapid price move detection: re-evaluate position when price moves > 5% in 15 minutes
- Implement multiple declining positions halt: stop new entries when > 50% of positions have > 2% negative unrealized P&L
- _Requirements: 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 7.1, 7.2, 7.3, 7.4, 7.5, 17.1, 17.2_
- [x] 13.2 Write property test for recommendation deduplication
- **Property 27: Recommendation deduplication (idempotence)**
- Generate random recommendation IDs and process them twice
- Verify second processing is a no-op (no new decision record, no order submitted)
- **Validates: Requirements 1.5**
- [x] 13.3 Write property test for trading decision record completeness
- **Property 28: Trading decision record completeness and traceability**
- Generate random recommendations and evaluate them
- Verify all required fields present in the persisted decision record
- Verify "act" decisions include order job with trading_decision_id
- **Validates: Requirements 1.4, 17.1, 17.2**
- [x] 13.4 Write property test for multiple declining positions halts entries
- **Property 16: Multiple declining positions halts new entries**
- Generate portfolio states with varying percentages of declining positions
- Verify new entries halted when > 50% of positions have > 2% negative unrealized P&L
- Verify entries allowed when <= 50% are declining
- **Validates: Requirements 7.5**
- [x] 13.5 Write property test for maximum open positions enforcement
- **Property 18: Maximum open positions enforcement**
- Generate portfolio states at and below the max position limit
- Verify new entries rejected at the limit
- Verify portfolio never exceeds the configured maximum
- **Validates: Requirements 8.4**
- [x] 14. Checkpoint — Ensure decision loop and core engine tests pass
- Ensure all tests pass, ask the user if questions arise.
- [x] 15. Portfolio Rebalancer and Performance Tracker
- [x] 15.1 Implement `services/trading/rebalancer.py`
- Implement `PortfolioRebalancer` class with `evaluate()` method accepting positions, risk_tier, and active_pool
- Generate partial sell orders when single stock exceeds max_position_pct
- Generate sell orders for lowest-confidence positions when sector exceeds max_sector_pct
- Enforce maximum open positions limit (default 10)
- Submit rebalancing orders through normal broker queue with `rebalance` tag in decision trace
- Respect trading window and circuit breaker status
- Schedule: weekly at market open on Monday (configurable)
- _Requirements: 8.1, 8.2, 8.3, 8.4, 8.5, 8.6_
- [x] 15.2 Write property test for portfolio rebalancing sell orders
- **Property 17: Portfolio rebalancing generates correct sell orders**
- Generate portfolios with over-concentrated positions and sectors
- Verify sell orders generated to bring positions within limits
- Verify lowest-confidence positions targeted first for sector rebalancing
- **Validates: Requirements 8.2, 8.3**
- [x] 15.3 Implement `services/trading/performance_tracker.py`
- Implement `PerformanceTracker` class with `compute_metrics()` method: compute all PerformanceMetrics fields (total portfolio value, active/reserve pool, unrealized/realized P&L, daily P&L, win/loss counts, win rate, avg win/loss, profit factor, Sharpe ratio, max drawdown, current drawdown, portfolio heat)
- Implement Sharpe ratio: `(mean_daily_return / std_daily_return) * sqrt(252)` using trailing 30-day daily returns
- Implement `record_trade()` method: persist per-trade metrics (entry/exit price, hold duration, P&L, recommendation ID)
- Implement `persist_daily_snapshot()` method: save end-of-day snapshot to `portfolio_snapshots` table
- Compute metrics every 5 minutes during market hours
- Track micro-trade metrics separately from standard trade metrics
- _Requirements: 14.1, 14.2, 14.3, 20.7_
- [x] 15.4 Write property test for performance metrics computation
- **Property 26: Performance metrics computation**
- Generate random sets of closed trades with entry/exit prices and hold durations
- Verify win_rate = wins / total_trades
- Verify profit_factor = gross_profits / gross_losses (infinity if no losses)
- Verify Sharpe ratio formula consistency
- **Validates: Requirements 14.1, 14.2**
- [x] 15.5 Write property test for micro-trade metrics tracked separately
- **Property 33: Micro-trade metrics tracked separately**
- Generate mixed sets of standard and micro-trades
- Verify micro-trade metrics computed independently
- Verify standard trade metrics not contaminated by micro-trades
- **Validates: Requirements 20.7**
- [x] 16. Notification Service implementation
- [x] 16.1 Implement `services/trading/notifications.py`
- Implement `NotificationService` class with `send_alert()` method: send via all enabled channels (SMS via AWS SNS, email via Gmail API)
- Implement `send_daily_summary()` method: format and send daily performance summary at configurable time (default 16:30)
- Implement `send_weekly_digest()` method: format and send weekly performance digest
- Implement rate limiting: max 10 SMS/hour, 20 emails/hour (configurable), using Redis counters with hourly TTL
- Implement retry logic: up to 3 retries with exponential backoff on delivery failure
- Persist all notifications to `notifications` table with channel, event_type, message, delivery_status, timestamp
- Support event types: circuit_breaker_triggered, circuit_breaker_resumed, risk_tier_changed, emergency_liquidation, large_trade_pnl, daily_summary, weekly_digest
- Notifications run in separate asyncio tasks — never block trading operations
- _Requirements: 19.1, 19.2, 19.3, 19.4, 19.5, 19.6, 19.7, 19.8, 19.11_
- [x] 16.2 Write property test for notification rate limiting
- **Property 30: Notification rate limiting**
- Generate random sequences of notification requests within a one-hour window
- Verify at most 10 SMS and 20 emails delivered per hour
- Verify excess notifications marked as 'rate_limited'
- **Validates: Requirements 19.7**
- [x] 17. Micro-Trading Module
- [x] 17.1 Implement `services/trading/micro_trading.py`
- Implement `MicroTradingModule` class with `poll_intraday_signals()` method: fetch intraday and 1d trend window signals from aggregation engine
- Implement `evaluate_micro_trade()` method: evaluate signal against risk tier confidence threshold, apply micro-trade allocation cap (3% of Active Pool)
- Enforce daily micro-trade limit (default 10)
- Use tighter stop-loss (1.0x ATR) and take-profit (1.5x stop distance)
- Implement auto-close after max hold duration (default 2 hours)
- Respect all existing constraints (trading window, circuit breakers, portfolio heat, correlation, sector exposure, earnings)
- Toggleable independently via trading_engine_config
- _Requirements: 20.1, 20.2, 20.3, 20.4, 20.5, 20.6, 20.8, 20.10_
- [x] 17.2 Write property test for micro-trade parameter constraints
- **Property 31: Micro-trade parameter constraints**
- Generate random micro-trade scenarios
- Verify allocation does not exceed micro_trading_allocation_cap_pct
- Verify stop-loss at 1.0x ATR, take-profit at 1.5x stop distance
- Verify daily count does not exceed configured maximum
- **Validates: Requirements 20.3, 20.4, 20.5**
- [x] 17.3 Write property test for micro-trade auto-close
- **Property 32: Micro-trade auto-close after max hold duration**
- Generate micro-trade positions with varying hold durations
- Verify positions closed at market price when hold exceeds max duration
- **Validates: Requirements 20.6**
- [x] 17.4 Write property test for micro-trades respect all constraints
- **Property 34: Micro-trades respect all existing constraints**
- Generate micro-trade evaluations with various constraint violations
- Verify trading window, circuit breakers, portfolio heat, correlation, sector exposure, and earnings rules all enforced
- **Validates: Requirements 20.10**
- [x] 18. Checkpoint — Ensure all trading logic and property tests pass
- Ensure all tests pass, ask the user if questions arise.
- [x] 19. Backtester implementation
- [x] 19.1 Implement `services/trading/backtester.py`
- Implement `Backtester` class with `run()` method accepting BacktestConfig (start_date, end_date, initial_capital, risk_tier)
- Replay historical recommendations from `recommendations` table within date range
- Simulate full decision logic: position sizing, stop-loss/take-profit, circuit breakers, reserve pool, rebalancing
- Use historical price data from market data tables for simulation
- Produce BacktestResult with total_return, sharpe_ratio, max_drawdown, win_rate, profit_factor, trade_count, trade_log, equity_curve
- Persist results to `backtest_runs` and `backtest_trades` tables with unique backtest_id
- Handle missing historical data gracefully (skip dates, note gaps)
- Persist partial results with status 'failed' on mid-run errors
- _Requirements: 15.1, 15.2, 15.3, 15.4_
- [x] 19.2 Write property test for backtester produces equivalent metrics
- **Property 36: Backtester produces equivalent metrics**
- Generate random sets of historical trades
- Verify backtester metric computation matches performance tracker for same trade data
- **Validates: Requirements 15.3**
- [x] 20. Trading Engine FastAPI HTTP Service
- [x] 20.1 Implement `services/trading/app.py`
- Create FastAPI application with lifespan handler that starts/stops the TradingEngine
- Implement `GET /health` liveness probe endpoint
- Implement `GET /ready` readiness probe: return healthy when portfolio loaded and loop active
- Implement `GET /api/trading/status` endpoint: return engine state (enabled, risk tier, circuit breaker status, active/reserve pool, portfolio heat, open positions, last decision timestamp)
- Implement `PUT /api/trading/config` endpoint: update trading_engine_config, record audit event with previous/new config and change source
- Implement `POST /api/trading/pause` and `POST /api/trading/resume` endpoints
- Implement `GET /api/trading/decisions` endpoint: paginated, filterable by ticker, decision type, date range
- Implement `GET /api/trading/metrics` endpoint: current performance metrics
- Implement `GET /api/trading/metrics/history` endpoint: historical daily snapshots
- Implement `POST /api/trading/backtest` endpoint: launch backtest, return backtest_id
- Implement `GET /api/trading/backtest/{id}` endpoint: retrieve backtest results
- Implement `GET /api/trading/notifications/config` and `PUT /api/trading/notifications/config` endpoints
- Implement `GET /api/trading/notifications/history` endpoint: recent notifications
- _Requirements: 1.7, 5.6, 6.6, 15.5, 16.2, 16.3, 16.4, 17.3, 19.9_
- [x] 20.2 Write property test for configuration change audit trail
- **Property 35: Configuration change audit trail**
- Generate random configuration changes via API
- Verify audit event persisted with previous config, new config, and change source
- **Validates: Requirements 16.6**
- [x] 20.3 Write property test for persistence round-trip
- **Property 29: Persistence round-trip for trading engine state**
- Generate random trading engine config, reserve pool entries, risk tier history, circuit breaker events, portfolio snapshots, and backtest results
- Verify persist-then-read produces equivalent objects with all fields preserved
- **Validates: Requirements 3.2, 4.7, 5.5, 6.4, 14.3, 15.4, 16.1**
- [x] 21. Checkpoint — Ensure API endpoints and backtester work correctly
- Ensure all tests pass, ask the user if questions arise.
- [x] 22. Kubernetes deployment and infrastructure
- [x] 22.1 Add trading-engine service to Helm chart `infra/helm/stonks-oracle/values.yaml`
- Add `tradingEngine` entry under `services:` with: replicas 1, image trading-engine, command `uvicorn services.trading.app:app --host 0.0.0.0 --port 8000`, tier trading, port 8000, secrets [stonks-core-secrets, stonks-broker-secrets], resources (requests: 100m CPU / 256Mi memory, limits: 500m CPU / 512Mi memory), readiness probe on /ready port 8000, liveness probe on /health port 8000
- _Requirements: 1.7, 16.1_
- [x] 22.2 Add network policy for trading-engine
- Allow ingress from query-api, dashboard, and kube-system (Traefik) on port 8000
- Allow egress to PostgreSQL, Redis, and external services (SNS, Gmail API)
- _Requirements: 16.2_
- [x] 22.3 Add `/trading/` proxy route to dashboard nginx.conf
- Add `location /trading/ { proxy_pass http://trading-engine:8000/; }` to `frontend/nginx.conf`
- _Requirements: 14.4, 16.5_
- [x] 22.4 Add trading-engine ingress if external access needed
- Add ingress host entry for trading engine API (e.g., `stonks-trading.celestium.life`) to values.yaml if direct external access is desired, or rely on dashboard proxy
- _Requirements: 16.2_
- [x] 23. Dashboard frontend — Trading Engine panels
- [x] 23.1 Add trading API client hooks to `frontend/src/api/`
- Add `useTradingStatus()` hook: fetch `GET /trading/api/trading/status`
- Add `useTradingDecisions()` hook: fetch `GET /trading/api/trading/decisions` with pagination and filters
- Add `useTradingMetrics()` hook: fetch `GET /trading/api/trading/metrics`
- Add `useTradingMetricsHistory()` hook: fetch `GET /trading/api/trading/metrics/history`
- Add `useTradingConfig()` and `useUpdateTradingConfig()` hooks for config read/write
- Add `usePauseTradingEngine()` and `useResumeTradingEngine()` mutation hooks
- Add `useBacktestLaunch()` and `useBacktestResult()` hooks
- Add `useNotificationConfig()`, `useUpdateNotificationConfig()`, and `useNotificationHistory()` hooks
- _Requirements: 14.4, 14.5, 14.6, 14.7, 15.6, 16.5, 17.4, 19.10, 20.9_
- [x] 23.2 Implement Trading Engine overview panel component
- Display current Risk Tier, Circuit Breaker status (active/inactive with trigger reason and cooldown remaining), Active Pool and Reserve Pool balances, Portfolio Heat gauge, last 24h P&L summary
- Include start/pause/resume controls and Risk Tier selector dropdown
- Use TanStack Query for data fetching with auto-refresh
- _Requirements: 14.4, 16.5, 6.6_
- [x] 23.3 Implement Portfolio Composition panel component
- Display current positions table: ticker, entry price, current price, unrealized P&L, stop-loss level, take-profit level, sector
- Display sector allocation pie chart using Recharts
- _Requirements: 14.5_
- [x] 23.4 Implement Trade History panel component
- Display completed trades table: entry/exit prices, P&L amount and percentage, hold duration, recommendation thesis
- Support pagination and filtering by ticker and date range
- _Requirements: 14.6, 17.4_
- [x] 23.5 Implement Performance Charts panel component
- Display cumulative P&L line chart over time using Recharts
- Display daily returns bar chart using Recharts
- Display drawdown chart using Recharts
- _Requirements: 14.7_
- [x] 23.6 Implement Backtesting panel component
- Display backtest configuration form: date range picker, initial capital input, risk tier selector
- Display backtest results: equity curve chart, trade log table, summary metrics (total return, Sharpe, max drawdown, win rate, profit factor)
- Support launching new backtests and viewing historical results
- _Requirements: 15.6_
- [x] 23.7 Implement Micro-Trading panel component
- Display micro-trade mode status toggle (enabled/disabled)
- Display today's micro-trade count and P&L
- Display active micro-trade positions table
- Display micro-trade performance metrics over trailing 7 days
- _Requirements: 20.9_
- [x] 23.8 Implement Notification Preferences panel component
- Display notification channel toggles (SMS, email) with phone number and email address inputs
- Display event type selection checkboxes
- Display rate limit configuration
- Display recent notification history table
- _Requirements: 19.10_
- [x] 23.9 Wire trading panels into dashboard routing
- Add Trading page route to TanStack Router configuration
- Add navigation link to the dashboard sidebar/header
- Compose all trading panels (overview, portfolio, trade history, performance, backtesting, micro-trading, notifications) into the Trading page layout
- _Requirements: 14.4, 16.5_
- [x] 24. Checkpoint — Ensure frontend builds and all tests pass
- Ensure all tests pass, ask the user if questions arise.
- [x] 25. Integration wiring and final validation
- [x] 25.1 Wire stop-loss price crossing detection into the decision loop
- Connect `StopLossManager.check_price_crossings()` to run at the configured interval (5 min default, 60s during high-severity events)
- Generate immediate sell orders for triggered positions and submit to broker queue
- Handle price data unavailability (close position after 15 min without data)
- _Requirements: 4.3, 4.4, 4.5, 4.8, 7.4_
- [x] 25.2 Wire reserve pool siphoning to position close events
- Detect profitable position closes from broker service fill events
- Call `ReservePoolController.siphon_profit()` with realized profit
- Trigger notification for large trade P&L events
- _Requirements: 3.1, 19.2_
- [x] 25.3 Wire risk tier evaluation to daily market close schedule
- Schedule `RiskTierController.evaluate()` at market close
- Trigger notification on tier changes
- _Requirements: 5.2, 19.2_
- [x] 25.4 Wire portfolio rebalancer to weekly schedule
- Schedule `PortfolioRebalancer.evaluate()` weekly at Monday market open
- Submit rebalancing orders through broker queue
- _Requirements: 8.1, 8.5_
- [x] 25.5 Wire notification service to all critical events
- Connect circuit breaker triggers/resumes, risk tier changes, emergency liquidation, large trade P&L to notification dispatch
- Schedule daily summary at configured time (default 16:30)
- Schedule weekly digest
- _Requirements: 19.2, 19.3, 19.4_
- [x] 25.6 Wire micro-trading module into the decision loop
- Start micro-trading polling when enabled in config
- Route micro-trade decisions through the same order submission pipeline
- Track micro-trade metrics separately in performance tracker
- _Requirements: 20.1, 20.2, 20.7_
- [x] 25.7 Write integration tests for end-to-end decision flow
- Test full cycle: recommendation → evaluation → position sizing → order submission to broker queue
- Test stop-loss crossing → immediate sell order
- Test reserve pool siphoning on profitable close
- Test circuit breaker trigger → halt → cooldown → resume
- Test engine startup state reconstruction from PostgreSQL
- _Requirements: 1.1, 1.2, 1.3, 3.1, 4.4, 6.1, 6.5, 18.5_
- [x] 26. Final checkpoint — Ensure all tests pass
- Ensure all tests pass, ask the user if questions arise.
## Notes
- Tasks marked with `*` are optional and can be skipped for faster MVP
- Each task references specific requirements for traceability
- Checkpoints ensure incremental validation after each major component
- Property tests validate the 36 correctness properties defined in the design document
- The trading engine is a NEW service at `services/trading/` — it does not replace existing services
- All order submission goes through the existing `stonks:queue:broker_orders` Redis queue consumed by the Broker Service
- Migration number 018 is the next available migration slot
- Frontend components use the existing React 19 + TypeScript + Tailwind + TanStack + Recharts stack
- Dashboard proxy needs `/trading/``trading-engine:8000` added to nginx.conf
+9
View File
@@ -36,6 +36,15 @@ server {
proxy_set_header X-Forwarded-Proto $scheme;
}
# Proxy Trading Engine
location /trading/ {
proxy_pass http://trading-engine:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Cache static assets
location /assets/ {
expires 1y;
+4 -1
View File
@@ -6,8 +6,9 @@
const QUERY_API_BASE = import.meta.env.VITE_QUERY_API_URL || '';
const SYMBOL_REGISTRY_BASE = import.meta.env.VITE_SYMBOL_REGISTRY_URL || '/registry';
const RISK_ENGINE_BASE = import.meta.env.VITE_RISK_ENGINE_URL || '/risk';
const TRADING_ENGINE_BASE = import.meta.env.VITE_TRADING_ENGINE_URL || '/trading';
export type ApiBase = 'query' | 'registry' | 'risk';
export type ApiBase = 'query' | 'registry' | 'risk' | 'trading';
function baseUrl(api: ApiBase): string {
switch (api) {
@@ -17,6 +18,8 @@ function baseUrl(api: ApiBase): string {
return SYMBOL_REGISTRY_BASE;
case 'risk':
return RISK_ENGINE_BASE;
case 'trading':
return TRADING_ENGINE_BASE;
}
}
+315
View File
@@ -0,0 +1,315 @@
/**
* TanStack Query hooks for the Trading Engine API.
* All endpoints are proxied through /trading/ in nginx.
* Requirements: 14.4, 14.5, 14.6, 14.7, 15.6, 16.5, 17.4, 19.10, 20.9
*/
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { apiGet, apiPost, apiPut } from './client';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface TradingEngineStatus {
enabled: boolean;
paused: boolean;
risk_tier: string;
circuit_breaker_active: boolean;
circuit_breaker_trigger_type: string | null;
circuit_breaker_cooldown_expires: string | null;
active_pool: number;
reserve_pool: number;
portfolio_heat: number;
portfolio_value: number;
open_position_count: number;
last_decision_at: string | null;
micro_trading_enabled: boolean;
uptime_seconds: number | null;
}
export interface TradingDecision {
id: string;
recommendation_id: string | null;
decision: string;
skip_reason: string | null;
ticker: string;
computed_position_size: number | null;
computed_share_quantity: number | null;
risk_tier_at_decision: string;
portfolio_heat_at_decision: number | null;
active_pool_at_decision: number | null;
reserve_pool_at_decision: number | null;
circuit_breaker_status: string;
correlation_check_result: Record<string, unknown>;
sector_exposure_check_result: Record<string, unknown>;
earnings_proximity_flag: boolean;
is_micro_trade: boolean;
decision_trace: Record<string, unknown>;
created_at: string;
}
export interface TradingMetrics {
total_portfolio_value: number;
active_pool: number;
reserve_pool: number;
unrealized_pnl: number;
realized_pnl: number;
daily_pnl: number;
win_count: number;
loss_count: number;
win_rate: number;
avg_win: number;
avg_loss: number;
profit_factor: number;
sharpe_ratio: number;
max_drawdown: number;
current_drawdown_pct: number;
portfolio_heat: number;
computed_at: string;
}
export interface PortfolioSnapshot {
id: string;
snapshot_date: string;
portfolio_value: number;
active_pool: number;
reserve_pool: number;
daily_return: number | null;
cumulative_return: number | null;
unrealized_pnl: number | null;
realized_pnl: number | null;
win_count: number;
loss_count: number;
win_rate: number | null;
sharpe_ratio: number | null;
max_drawdown: number | null;
current_drawdown_pct: number | null;
portfolio_heat: number | null;
risk_tier: string | null;
positions: unknown[];
metrics: Record<string, unknown>;
created_at: string;
}
export interface TradingEngineConfig {
id: string;
enabled: boolean;
paused: boolean;
risk_tier: string;
reserve_siphon_pct: number;
polling_interval_seconds: number;
gradual_entry_tranches: number;
gradual_entry_threshold_dollars: number;
gradual_entry_interval_minutes: number;
trading_window_start_minutes: number;
trading_window_end_minutes: number;
max_open_positions: number;
circuit_breaker_daily_loss_pct: number;
circuit_breaker_single_position_loss_pct: number;
circuit_breaker_ticker_cooldown_hours: number;
circuit_breaker_volatility_pause_hours: number;
circuit_breaker_stop_loss_hits_threshold: number;
circuit_breaker_stop_loss_window_minutes: number;
active_pool_minimum: number;
emergency_drawdown_threshold_pct: number;
reserve_high_water_pct: number;
absolute_position_cap: number;
correlation_reduction_threshold: number;
correlation_rejection_threshold: number;
earnings_pre_window_days: number;
earnings_post_cooldown_days: number;
micro_trading_enabled: boolean;
micro_trading_interval_seconds: number;
micro_trading_allocation_cap_pct: number;
micro_trading_max_daily: number;
micro_trading_max_hold_minutes: number;
notification_sms_enabled: boolean;
notification_email_enabled: boolean;
notification_phone_number: string | null;
notification_email_recipient: string | null;
notification_sns_topic_arn: string | null;
notification_rate_limit_sms_per_hour: number;
notification_rate_limit_email_per_hour: number;
notification_daily_summary_time: string;
created_at: string;
updated_at: string;
}
export interface BacktestResult {
id: string;
start_date: string;
end_date: string;
initial_capital: number;
risk_tier: string;
config: Record<string, unknown>;
total_return: number | null;
sharpe_ratio: number | null;
max_drawdown: number | null;
win_rate: number | null;
profit_factor: number | null;
trade_count: number | null;
equity_curve: Array<{ date: string; portfolio_value: number }>;
status: string;
completed_at: string | null;
created_at: string;
}
export interface NotificationConfig {
sms_enabled: boolean;
email_enabled: boolean;
phone_number: string | null;
email_recipient: string | null;
sns_topic_arn: string | null;
rate_limit_sms_per_hour: number;
rate_limit_email_per_hour: number;
daily_summary_time: string;
}
export interface NotificationRecord {
id: string;
channel: string;
event_type: string;
message: string;
delivery_status: string;
retry_count: number;
error_message: string | null;
created_at: string;
delivered_at: string | null;
}
// ---------------------------------------------------------------------------
// Query hooks
// ---------------------------------------------------------------------------
/** Fetch current trading engine status (risk tier, circuit breaker, pools, etc.) */
export function useTradingStatus() {
return useQuery<TradingEngineStatus>({
queryKey: ['trading-status'],
queryFn: () => apiGet<TradingEngineStatus>('trading', '/api/trading/status'),
});
}
/** Fetch recent trading decisions with optional pagination and filters. */
export function useTradingDecisions(params?: {
ticker?: string;
decision?: string;
is_micro_trade?: boolean;
limit?: number;
offset?: number;
}) {
const qs = new URLSearchParams();
if (params?.ticker) qs.set('ticker', params.ticker);
if (params?.decision) qs.set('decision', params.decision);
if (params?.is_micro_trade !== undefined) qs.set('is_micro_trade', String(params.is_micro_trade));
if (params?.limit) qs.set('limit', String(params.limit));
if (params?.offset) qs.set('offset', String(params.offset));
const path = `/api/trading/decisions${qs.toString() ? '?' + qs : ''}`;
return useQuery<TradingDecision[]>({
queryKey: ['trading-decisions', params],
queryFn: () => apiGet<TradingDecision[]>('trading', path),
});
}
/** Fetch current performance metrics. */
export function useTradingMetrics() {
return useQuery<TradingMetrics>({
queryKey: ['trading-metrics'],
queryFn: () => apiGet<TradingMetrics>('trading', '/api/trading/metrics'),
});
}
/** Fetch historical daily portfolio snapshots. */
export function useTradingMetricsHistory() {
return useQuery<PortfolioSnapshot[]>({
queryKey: ['trading-metrics-history'],
queryFn: () => apiGet<PortfolioSnapshot[]>('trading', '/api/trading/metrics/history'),
});
}
/** Fetch trading engine configuration. */
export function useTradingConfig() {
return useQuery<TradingEngineConfig>({
queryKey: ['trading-config'],
queryFn: () => apiGet<TradingEngineConfig>('trading', '/api/trading/config'),
});
}
/** Fetch a backtest result by ID. */
export function useBacktestResult(id: string | undefined) {
return useQuery<BacktestResult>({
queryKey: ['backtest-result', id],
queryFn: () => apiGet<BacktestResult>('trading', `/api/trading/backtest/${id}`),
enabled: !!id,
});
}
/** Fetch notification preferences. */
export function useNotificationConfig() {
return useQuery<NotificationConfig>({
queryKey: ['notification-config'],
queryFn: () => apiGet<NotificationConfig>('trading', '/api/trading/notifications/config'),
});
}
/** Fetch notification history. */
export function useNotificationHistory() {
return useQuery<NotificationRecord[]>({
queryKey: ['notification-history'],
queryFn: () => apiGet<NotificationRecord[]>('trading', '/api/trading/notifications/history'),
});
}
// ---------------------------------------------------------------------------
// Mutation hooks
// ---------------------------------------------------------------------------
/** Update trading engine configuration. */
export function useUpdateTradingConfig() {
const qc = useQueryClient();
return useMutation({
mutationFn: (body: Partial<TradingEngineConfig>) =>
apiPut<TradingEngineConfig>('trading', '/api/trading/config', body),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['trading-config'] });
qc.invalidateQueries({ queryKey: ['trading-status'] });
},
});
}
/** Pause the trading engine. */
export function usePauseTradingEngine() {
const qc = useQueryClient();
return useMutation({
mutationFn: () => apiPost<unknown>('trading', '/api/trading/pause', {}),
onSuccess: () => qc.invalidateQueries({ queryKey: ['trading-status'] }),
});
}
/** Resume the trading engine. */
export function useResumeTradingEngine() {
const qc = useQueryClient();
return useMutation({
mutationFn: () => apiPost<unknown>('trading', '/api/trading/resume', {}),
onSuccess: () => qc.invalidateQueries({ queryKey: ['trading-status'] }),
});
}
/** Launch a new backtest run. */
export function useBacktestLaunch() {
const qc = useQueryClient();
return useMutation({
mutationFn: (body: { start_date: string; end_date: string; initial_capital: number; risk_tier: string }) =>
apiPost<BacktestResult>('trading', '/api/trading/backtest', body),
onSuccess: () => qc.invalidateQueries({ queryKey: ['backtest-result'] }),
});
}
/** Update notification preferences. */
export function useUpdateNotificationConfig() {
const qc = useQueryClient();
return useMutation({
mutationFn: (body: Partial<NotificationConfig>) =>
apiPut<NotificationConfig>('trading', '/api/trading/notifications/config', body),
onSuccess: () => qc.invalidateQueries({ queryKey: ['notification-config'] }),
});
}
+2
View File
@@ -17,6 +17,7 @@ import {
LayoutDashboard,
List,
Globe,
BarChart3,
} from 'lucide-react';
interface NavItem {
@@ -37,6 +38,7 @@ const navItems: NavItem[] = [
{ to: '/orders', label: 'Orders', icon: <ShoppingCart size={18} />, group: 'Trading' },
{ to: '/positions', label: 'Positions', icon: <Wallet size={18} />, group: 'Trading' },
{ to: '/trading', label: 'Trading Controls', icon: <ShieldCheck size={18} />, group: 'Trading' },
{ to: '/trading/engine', label: 'Trading Engine', icon: <BarChart3 size={18} />, group: 'Trading' },
{ to: '/ops/pipeline', label: 'Pipeline', icon: <Activity size={18} />, group: 'Ops' },
{ to: '/ops/ingestion', label: 'Ingestion', icon: <Download size={18} />, group: 'Ops' },
{ to: '/ops/model', label: 'Model Perf', icon: <Cpu size={18} />, group: 'Ops' },
+64
View File
@@ -0,0 +1,64 @@
import { useState } from 'react';
import { TradingOverview } from './trading/TradingOverview';
import { PortfolioComposition } from './trading/PortfolioComposition';
import { TradeHistory } from './trading/TradeHistory';
import { PerformanceCharts } from './trading/PerformanceCharts';
import { BacktestPanel } from './trading/BacktestPanel';
import { MicroTradingPanel } from './trading/MicroTradingPanel';
import { NotificationPreferences } from './trading/NotificationPreferences';
import { ErrorBoundary } from '../components/ui';
const TABS = [
{ id: 'overview', label: 'Overview' },
{ id: 'portfolio', label: 'Portfolio' },
{ id: 'history', label: 'Trade History' },
{ id: 'performance', label: 'Performance' },
{ id: 'backtest', label: 'Backtest' },
{ id: 'micro', label: 'Micro-Trading' },
{ id: 'notifications', label: 'Notifications' },
] as const;
type TabId = (typeof TABS)[number]['id'];
export function TradingEnginePage() {
const [activeTab, setActiveTab] = useState<TabId>('overview');
return (
<div className="space-y-4">
<h1 className="text-xl font-semibold text-gray-100">Trading Engine</h1>
{/* Tab Navigation */}
<div className="flex gap-1 overflow-x-auto border-b border-surface-700 pb-px" role="tablist">
{TABS.map((tab) => (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id)}
className={`whitespace-nowrap rounded-t-md px-3 py-2 text-sm font-medium transition-colors ${
activeTab === tab.id
? 'border-b-2 border-brand-500 text-brand-300'
: 'text-gray-400 hover:bg-surface-800 hover:text-gray-200'
}`}
role="tab"
aria-selected={activeTab === tab.id}
aria-controls={`panel-${tab.id}`}
>
{tab.label}
</button>
))}
</div>
{/* Tab Content */}
<ErrorBoundary>
<div role="tabpanel" id={`panel-${activeTab}`}>
{activeTab === 'overview' && <TradingOverview />}
{activeTab === 'portfolio' && <PortfolioComposition />}
{activeTab === 'history' && <TradeHistory />}
{activeTab === 'performance' && <PerformanceCharts />}
{activeTab === 'backtest' && <BacktestPanel />}
{activeTab === 'micro' && <MicroTradingPanel />}
{activeTab === 'notifications' && <NotificationPreferences />}
</div>
</ErrorBoundary>
</div>
);
}
@@ -0,0 +1,170 @@
import { useState } from 'react';
import { useBacktestLaunch, useBacktestResult } from '../../api/tradingHooks';
import { Card, LoadingSpinner } from '../../components/ui';
import {
LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid,
} from 'recharts';
const RISK_TIERS = ['conservative', 'moderate', 'aggressive'];
function fmtUsd(v: number | null | undefined) {
if (v == null) return '—';
return `$${v.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
function fmtPct(v: number | null | undefined) {
if (v == null) return '—';
return `${(v * 100).toFixed(2)}%`;
}
export function BacktestPanel() {
const [startDate, setStartDate] = useState('');
const [endDate, setEndDate] = useState('');
const [initialCapital, setInitialCapital] = useState('100000');
const [riskTier, setRiskTier] = useState('moderate');
const [backtestId, setBacktestId] = useState<string | undefined>(undefined);
const launch = useBacktestLaunch();
const { data: result, isLoading: resultLoading } = useBacktestResult(backtestId);
function handleLaunch() {
if (!startDate || !endDate) return;
launch.mutate(
{
start_date: startDate,
end_date: endDate,
initial_capital: Number(initialCapital),
risk_tier: riskTier,
},
{
onSuccess: (data) => setBacktestId(data.id),
},
);
}
const equityCurve = (result?.equity_curve ?? []).map((pt) => ({
date: new Date(pt.date).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }),
value: pt.portfolio_value,
}));
return (
<div className="space-y-4">
{/* Configuration Form */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Backtest Configuration</h2>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-4">
<div>
<label className="mb-1 block text-xs text-gray-500">Start Date</label>
<input
type="date"
value={startDate}
onChange={(e) => setStartDate(e.target.value)}
className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none"
/>
</div>
<div>
<label className="mb-1 block text-xs text-gray-500">End Date</label>
<input
type="date"
value={endDate}
onChange={(e) => setEndDate(e.target.value)}
className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none"
/>
</div>
<div>
<label className="mb-1 block text-xs text-gray-500">Initial Capital</label>
<input
type="number"
value={initialCapital}
onChange={(e) => setInitialCapital(e.target.value)}
className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none"
/>
</div>
<div>
<label className="mb-1 block text-xs text-gray-500">Risk Tier</label>
<select
value={riskTier}
onChange={(e) => setRiskTier(e.target.value)}
className="w-full rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none"
>
{RISK_TIERS.map((t) => (
<option key={t} value={t}>
{t.charAt(0).toUpperCase() + t.slice(1)}
</option>
))}
</select>
</div>
</div>
<button
onClick={handleLaunch}
disabled={!startDate || !endDate || launch.isPending}
className="mt-3 rounded-md bg-brand-600 px-4 py-1.5 text-sm font-medium text-white hover:bg-brand-700 disabled:opacity-50"
>
{launch.isPending ? 'Launching…' : 'Run Backtest'}
</button>
{launch.isError && (
<p className="mt-2 text-xs text-red-400">Failed to launch backtest</p>
)}
</Card>
{/* Results */}
{resultLoading && <LoadingSpinner />}
{result && (
<>
{/* Summary Metrics */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Backtest Results</h2>
<div className="grid grid-cols-2 gap-3 sm:grid-cols-5">
<MetricCard label="Total Return" value={fmtPct(result.total_return)} />
<MetricCard label="Sharpe Ratio" value={result.sharpe_ratio?.toFixed(2) ?? '—'} />
<MetricCard label="Max Drawdown" value={fmtPct(result.max_drawdown)} />
<MetricCard label="Win Rate" value={fmtPct(result.win_rate)} />
<MetricCard label="Profit Factor" value={result.profit_factor?.toFixed(2) ?? '—'} />
</div>
<div className="mt-2 flex items-center gap-4 text-xs text-gray-500">
<span>Trades: {result.trade_count ?? 0}</span>
<span>Status: {result.status}</span>
<span>Capital: {fmtUsd(result.initial_capital)}</span>
</div>
</Card>
{/* Equity Curve */}
{equityCurve.length > 0 && (
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Equity Curve</h2>
<ResponsiveContainer width="100%" height={300}>
<LineChart data={equityCurve}>
<CartesianGrid strokeDasharray="3 3" stroke="#334155" />
<XAxis dataKey="date" tick={{ fill: '#6b7280', fontSize: 11 }} />
<YAxis tick={{ fill: '#6b7280', fontSize: 11 }} tickFormatter={(v) => `$${(v / 1000).toFixed(0)}k`} />
<Tooltip
contentStyle={{ backgroundColor: '#1e293b', border: '1px solid #334155', borderRadius: 8 }}
labelStyle={{ color: '#9ca3af' }}
formatter={(value: number) => [fmtUsd(value), 'Portfolio Value']}
/>
<Line
type="monotone"
dataKey="value"
stroke="#6366f1"
strokeWidth={2}
dot={false}
name="Portfolio Value"
/>
</LineChart>
</ResponsiveContainer>
</Card>
)}
</>
)}
</div>
);
}
function MetricCard({ label, value }: { label: string; value: string }) {
return (
<div className="text-center">
<div className="text-sm font-bold text-gray-100">{value}</div>
<div className="text-xs text-gray-500">{label}</div>
</div>
);
}
@@ -0,0 +1,117 @@
import {
useTradingStatus,
useUpdateTradingConfig,
useTradingDecisions,
useTradingMetrics,
} from '../../api/tradingHooks';
import { Card, LoadingSpinner } from '../../components/ui';
function fmtUsd(v: number | null | undefined) {
if (v == null) return '—';
return `$${v.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
export function MicroTradingPanel() {
const { data: status, isLoading } = useTradingStatus();
const { data: metrics } = useTradingMetrics();
const { data: microDecisions } = useTradingDecisions({ is_micro_trade: true, limit: 20 });
const updateConfig = useUpdateTradingConfig();
if (isLoading) return <LoadingSpinner />;
if (!status) return <p className="text-sm text-gray-500">No trading status available</p>;
const microEnabled = status.micro_trading_enabled;
function handleToggle() {
updateConfig.mutate({ micro_trading_enabled: !microEnabled });
}
const todayMicroTrades = (microDecisions ?? []).filter((d) => {
const created = new Date(d.created_at);
const today = new Date();
return (
created.getFullYear() === today.getFullYear() &&
created.getMonth() === today.getMonth() &&
created.getDate() === today.getDate()
);
});
return (
<div className="space-y-4">
{/* Toggle */}
<Card>
<div className="flex items-center justify-between">
<h2 className="text-sm font-medium text-gray-400">Micro-Trade Mode</h2>
<button
onClick={handleToggle}
disabled={updateConfig.isPending}
className={`relative inline-flex h-6 w-11 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors focus:outline-none focus:ring-2 focus:ring-brand-500 focus:ring-offset-2 focus:ring-offset-surface-900 ${
microEnabled ? 'bg-brand-600' : 'bg-surface-700'
}`}
role="switch"
aria-checked={microEnabled}
aria-label="Toggle micro-trading"
>
<span
className={`pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow ring-0 transition-transform ${
microEnabled ? 'translate-x-5' : 'translate-x-0'
}`}
/>
</button>
</div>
<p className="mt-1 text-xs text-gray-500">
{microEnabled ? 'Micro-trading is enabled' : 'Micro-trading is disabled'}
</p>
</Card>
{/* Today's Stats */}
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3">
<StatCard label="Today's Micro-Trades" value={String(todayMicroTrades.length)} />
<StatCard label="Daily P&L" value={fmtUsd(metrics?.daily_pnl)} />
<StatCard label="Win Rate" value={metrics?.win_rate != null ? `${(metrics.win_rate * 100).toFixed(1)}%` : '—'} />
</div>
{/* Recent Micro-Trades */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Recent Micro-Trades</h2>
{(microDecisions ?? []).length === 0 ? (
<p className="text-sm text-gray-500">No micro-trades recorded</p>
) : (
<div className="overflow-x-auto">
<table className="w-full text-sm" role="grid">
<thead>
<tr className="border-b border-surface-700 text-left text-gray-500">
<th className="px-3 py-2">Ticker</th>
<th className="px-3 py-2">Decision</th>
<th className="px-3 py-2">Shares</th>
<th className="px-3 py-2">Size</th>
<th className="px-3 py-2">Date</th>
</tr>
</thead>
<tbody>
{(microDecisions ?? []).map((d) => (
<tr key={d.id} className="border-b border-surface-700/50">
<td className="px-3 py-2 font-mono font-semibold text-brand-300">{d.ticker}</td>
<td className="px-3 py-2 capitalize text-gray-300">{d.decision}</td>
<td className="px-3 py-2 text-gray-300">{d.computed_share_quantity ?? '—'}</td>
<td className="px-3 py-2 text-gray-300">{fmtUsd(d.computed_position_size)}</td>
<td className="px-3 py-2 text-xs text-gray-400">{new Date(d.created_at).toLocaleString()}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</Card>
</div>
);
}
function StatCard({ label, value }: { label: string; value: string }) {
return (
<Card className="text-center">
<div className="text-lg font-bold text-gray-100">{value}</div>
<div className="text-xs text-gray-500">{label}</div>
</Card>
);
}
@@ -0,0 +1,163 @@
import { useState, useEffect } from 'react';
import {
useNotificationConfig,
useUpdateNotificationConfig,
useNotificationHistory,
} from '../../api/tradingHooks';
import type { NotificationRecord } from '../../api/tradingHooks';
import { Card, LoadingSpinner, StatusBadge } from '../../components/ui';
import { DataTable, type Column } from '../../components/DataTable';
const notifColumns: Column<NotificationRecord>[] = [
{
key: 'channel',
header: 'Channel',
render: (r) => <span className="capitalize">{r.channel}</span>,
},
{
key: 'event_type',
header: 'Event',
render: (r) => <span className="text-xs">{r.event_type}</span>,
},
{
key: 'message',
header: 'Message',
render: (r) => <span className="max-w-xs truncate text-xs text-gray-400">{r.message}</span>,
},
{
key: 'delivery_status',
header: 'Status',
render: (r) => <StatusBadge status={r.delivery_status} />,
},
{
key: 'created_at',
header: 'Sent',
render: (r) => <span className="text-xs">{new Date(r.created_at).toLocaleString()}</span>,
},
];
export function NotificationPreferences() {
const { data: config, isLoading } = useNotificationConfig();
const { data: history } = useNotificationHistory();
const updateConfig = useUpdateNotificationConfig();
const [smsEnabled, setSmsEnabled] = useState(false);
const [emailEnabled, setEmailEnabled] = useState(false);
const [phone, setPhone] = useState('');
const [email, setEmail] = useState('');
useEffect(() => {
if (config) {
setSmsEnabled(config.sms_enabled);
setEmailEnabled(config.email_enabled);
setPhone(config.phone_number ?? '');
setEmail(config.email_recipient ?? '');
}
}, [config]);
function handleSave() {
updateConfig.mutate({
sms_enabled: smsEnabled,
email_enabled: emailEnabled,
phone_number: phone || null,
email_recipient: email || null,
});
}
if (isLoading) return <LoadingSpinner />;
return (
<div className="space-y-4">
{/* Channel Toggles */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Notification Channels</h2>
<div className="space-y-4">
{/* SMS */}
<div className="flex items-start gap-4">
<button
onClick={() => setSmsEnabled(!smsEnabled)}
className={`relative mt-0.5 inline-flex h-6 w-11 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors focus:outline-none focus:ring-2 focus:ring-brand-500 focus:ring-offset-2 focus:ring-offset-surface-900 ${
smsEnabled ? 'bg-brand-600' : 'bg-surface-700'
}`}
role="switch"
aria-checked={smsEnabled}
aria-label="Toggle SMS notifications"
>
<span
className={`pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow ring-0 transition-transform ${
smsEnabled ? 'translate-x-5' : 'translate-x-0'
}`}
/>
</button>
<div className="flex-1">
<div className="text-sm text-gray-300">SMS Notifications</div>
<input
type="tel"
placeholder="Phone number…"
value={phone}
onChange={(e) => setPhone(e.target.value)}
className="mt-1 w-full max-w-xs rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 placeholder-gray-500 focus:border-brand-500 focus:outline-none"
aria-label="Phone number"
/>
</div>
</div>
{/* Email */}
<div className="flex items-start gap-4">
<button
onClick={() => setEmailEnabled(!emailEnabled)}
className={`relative mt-0.5 inline-flex h-6 w-11 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors focus:outline-none focus:ring-2 focus:ring-brand-500 focus:ring-offset-2 focus:ring-offset-surface-900 ${
emailEnabled ? 'bg-brand-600' : 'bg-surface-700'
}`}
role="switch"
aria-checked={emailEnabled}
aria-label="Toggle email notifications"
>
<span
className={`pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow ring-0 transition-transform ${
emailEnabled ? 'translate-x-5' : 'translate-x-0'
}`}
/>
</button>
<div className="flex-1">
<div className="text-sm text-gray-300">Email Notifications</div>
<input
type="email"
placeholder="Email address…"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="mt-1 w-full max-w-xs rounded-md border border-surface-700 bg-surface-950 px-2 py-1.5 text-sm text-gray-200 placeholder-gray-500 focus:border-brand-500 focus:outline-none"
aria-label="Email address"
/>
</div>
</div>
</div>
<button
onClick={handleSave}
disabled={updateConfig.isPending}
className="mt-4 rounded-md bg-brand-600 px-4 py-1.5 text-sm font-medium text-white hover:bg-brand-700 disabled:opacity-50"
>
{updateConfig.isPending ? 'Saving…' : 'Save Preferences'}
</button>
{updateConfig.isSuccess && (
<span className="ml-3 text-xs text-green-400">Saved</span>
)}
</Card>
{/* Notification History */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">
Recent Notifications ({history?.length ?? 0})
</h2>
<DataTable<NotificationRecord>
data={history ?? []}
columns={notifColumns}
keyField="id"
pageSize={15}
emptyMessage="No notifications yet"
/>
</Card>
</div>
);
}
@@ -0,0 +1,115 @@
import { useMemo } from 'react';
import { useTradingMetricsHistory } from '../../api/tradingHooks';
import { Card, LoadingSpinner } from '../../components/ui';
import {
LineChart, Line, BarChart, Bar, AreaChart, Area,
XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, Cell,
} from 'recharts';
const tooltipStyle = {
backgroundColor: '#1e293b',
border: '1px solid #334155',
borderRadius: 8,
};
export function PerformanceCharts() {
const { data: snapshots, isLoading } = useTradingMetricsHistory();
const chartData = useMemo(() => {
if (!snapshots) return [];
return snapshots.map((s) => ({
date: new Date(s.snapshot_date).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }),
portfolioValue: s.portfolio_value,
cumulativeReturn: (s.cumulative_return ?? 0) * 100,
dailyReturn: (s.daily_return ?? 0) * 100,
drawdown: (s.current_drawdown_pct ?? 0) * 100,
maxDrawdown: (s.max_drawdown ?? 0) * 100,
}));
}, [snapshots]);
if (isLoading) return <LoadingSpinner />;
if (chartData.length === 0) {
return (
<Card>
<p className="text-sm text-gray-500">No performance history available yet</p>
</Card>
);
}
return (
<div className="space-y-4">
{/* Cumulative P&L */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Cumulative P&amp;L</h2>
<ResponsiveContainer width="100%" height={280}>
<LineChart data={chartData}>
<CartesianGrid strokeDasharray="3 3" stroke="#334155" />
<XAxis dataKey="date" tick={{ fill: '#6b7280', fontSize: 11 }} />
<YAxis tick={{ fill: '#6b7280', fontSize: 11 }} tickFormatter={(v) => `${v.toFixed(1)}%`} />
<Tooltip
contentStyle={tooltipStyle}
labelStyle={{ color: '#9ca3af' }}
formatter={(value: number) => [`${value.toFixed(2)}%`, 'Cumulative Return']}
/>
<Line
type="monotone"
dataKey="cumulativeReturn"
stroke="#6366f1"
strokeWidth={2}
dot={false}
name="Cumulative Return"
/>
</LineChart>
</ResponsiveContainer>
</Card>
{/* Daily Returns */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Daily Returns</h2>
<ResponsiveContainer width="100%" height={220}>
<BarChart data={chartData}>
<CartesianGrid strokeDasharray="3 3" stroke="#334155" />
<XAxis dataKey="date" tick={{ fill: '#6b7280', fontSize: 11 }} />
<YAxis tick={{ fill: '#6b7280', fontSize: 11 }} tickFormatter={(v) => `${v.toFixed(1)}%`} />
<Tooltip
contentStyle={tooltipStyle}
labelStyle={{ color: '#9ca3af' }}
formatter={(value: number) => [`${value.toFixed(2)}%`, 'Daily Return']}
/>
<Bar dataKey="dailyReturn" name="Daily Return">
{chartData.map((entry, i) => (
<Cell key={i} fill={entry.dailyReturn >= 0 ? '#22c55e' : '#ef4444'} />
))}
</Bar>
</BarChart>
</ResponsiveContainer>
</Card>
{/* Drawdown */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Drawdown</h2>
<ResponsiveContainer width="100%" height={220}>
<AreaChart data={chartData}>
<CartesianGrid strokeDasharray="3 3" stroke="#334155" />
<XAxis dataKey="date" tick={{ fill: '#6b7280', fontSize: 11 }} />
<YAxis tick={{ fill: '#6b7280', fontSize: 11 }} tickFormatter={(v) => `${v.toFixed(1)}%`} />
<Tooltip
contentStyle={tooltipStyle}
labelStyle={{ color: '#9ca3af' }}
formatter={(value: number) => [`${value.toFixed(2)}%`, 'Drawdown']}
/>
<Area
type="monotone"
dataKey="drawdown"
stroke="#ef4444"
fill="#ef444433"
name="Current Drawdown"
/>
</AreaChart>
</ResponsiveContainer>
</Card>
</div>
);
}
@@ -0,0 +1,125 @@
import { useMemo } from 'react';
import { useTradingStatus } from '../../api/tradingHooks';
import { Card, LoadingSpinner } from '../../components/ui';
import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer } from 'recharts';
const COLORS = [
'#6366f1', '#22c55e', '#f59e0b', '#ef4444', '#06b6d4',
'#ec4899', '#8b5cf6', '#14b8a6', '#f97316', '#64748b',
];
interface Position {
ticker: string;
entry_price: number;
current_price: number;
unrealized_pnl: number;
stop_loss: number | null;
take_profit: number | null;
sector: string | null;
quantity: number;
}
function fmtUsd(v: number | null | undefined) {
if (v == null) return '—';
return `$${v.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
function pnlColor(v: number | null | undefined) {
if (v == null) return 'text-gray-400';
return v >= 0 ? 'text-green-400' : 'text-red-400';
}
export function PortfolioComposition() {
const { data: status, isLoading } = useTradingStatus();
// Extract positions from the status — the API may embed them or we derive from open_position_count
// For now, we use a typed cast since the status object may carry positions in an extended response
const positions: Position[] = useMemo(() => {
if (!status) return [];
const raw = (status as Record<string, unknown>)['positions'];
if (Array.isArray(raw)) return raw as Position[];
return [];
}, [status]);
const sectorData = useMemo(() => {
const map = new Map<string, number>();
for (const p of positions) {
const sector = p.sector ?? 'Unknown';
const value = (p.current_price ?? 0) * (p.quantity ?? 0);
map.set(sector, (map.get(sector) ?? 0) + value);
}
return Array.from(map.entries()).map(([name, value]) => ({ name, value }));
}, [positions]);
if (isLoading) return <LoadingSpinner />;
return (
<div className="space-y-4">
{/* Positions Table */}
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Current Positions</h2>
{positions.length === 0 ? (
<p className="text-sm text-gray-500">No open positions</p>
) : (
<div className="overflow-x-auto">
<table className="w-full text-sm" role="grid">
<thead>
<tr className="border-b border-surface-700 text-left text-gray-500">
<th className="px-3 py-2">Ticker</th>
<th className="px-3 py-2">Entry</th>
<th className="px-3 py-2">Current</th>
<th className="px-3 py-2">Unrealized P&amp;L</th>
<th className="px-3 py-2">Stop-Loss</th>
<th className="px-3 py-2">Take-Profit</th>
<th className="px-3 py-2">Sector</th>
</tr>
</thead>
<tbody>
{positions.map((p) => (
<tr key={p.ticker} className="border-b border-surface-700/50">
<td className="px-3 py-2 font-mono font-semibold text-brand-300">{p.ticker}</td>
<td className="px-3 py-2 text-gray-300">{fmtUsd(p.entry_price)}</td>
<td className="px-3 py-2 text-gray-300">{fmtUsd(p.current_price)}</td>
<td className={`px-3 py-2 ${pnlColor(p.unrealized_pnl)}`}>{fmtUsd(p.unrealized_pnl)}</td>
<td className="px-3 py-2 text-gray-300">{fmtUsd(p.stop_loss)}</td>
<td className="px-3 py-2 text-gray-300">{fmtUsd(p.take_profit)}</td>
<td className="px-3 py-2 text-gray-400">{p.sector ?? '—'}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</Card>
{/* Sector Allocation Pie Chart */}
{sectorData.length > 0 && (
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Sector Allocation</h2>
<ResponsiveContainer width="100%" height={300}>
<PieChart>
<Pie
data={sectorData}
dataKey="value"
nameKey="name"
cx="50%"
cy="50%"
outerRadius={100}
label={({ name, percent }) => `${name} ${(percent * 100).toFixed(0)}%`}
>
{sectorData.map((_, i) => (
<Cell key={i} fill={COLORS[i % COLORS.length]} />
))}
</Pie>
<Tooltip
contentStyle={{ backgroundColor: '#1e293b', border: '1px solid #334155', borderRadius: 8 }}
labelStyle={{ color: '#9ca3af' }}
formatter={(value: number) => fmtUsd(value)}
/>
</PieChart>
</ResponsiveContainer>
</Card>
)}
</div>
);
}
+125
View File
@@ -0,0 +1,125 @@
import { useState } from 'react';
import { useTradingDecisions } from '../../api/tradingHooks';
import type { TradingDecision } from '../../api/tradingHooks';
import { Card, LoadingSpinner } from '../../components/ui';
import { DataTable, type Column } from '../../components/DataTable';
function fmtUsd(v: number | null | undefined) {
if (v == null) return '—';
return `$${v.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
function pnlColor(v: number | null | undefined) {
if (v == null) return 'text-gray-400';
return v >= 0 ? 'text-green-400' : 'text-red-400';
}
const PAGE_SIZE = 25;
const columns: Column<TradingDecision>[] = [
{
key: 'ticker',
header: 'Ticker',
className: 'font-mono font-semibold text-brand-300',
},
{
key: 'decision',
header: 'Decision',
render: (r) => <span className="capitalize">{r.decision}</span>,
},
{
key: 'computed_share_quantity',
header: 'Shares',
render: (r) => <span>{r.computed_share_quantity ?? '—'}</span>,
},
{
key: 'computed_position_size',
header: 'Position Size',
render: (r) => <span>{fmtUsd(r.computed_position_size)}</span>,
},
{
key: 'risk_tier_at_decision',
header: 'Risk Tier',
render: (r) => <span className="capitalize">{r.risk_tier_at_decision}</span>,
},
{
key: 'portfolio_heat_at_decision',
header: 'Heat',
render: (r) => (
<span className={pnlColor(r.portfolio_heat_at_decision)}>
{r.portfolio_heat_at_decision != null ? `${(r.portfolio_heat_at_decision * 100).toFixed(1)}%` : '—'}
</span>
),
},
{
key: 'is_micro_trade',
header: 'Micro',
render: (r) => <span>{r.is_micro_trade ? 'Yes' : 'No'}</span>,
},
{
key: 'created_at',
header: 'Date',
render: (r) => <span className="text-xs">{new Date(r.created_at).toLocaleString()}</span>,
},
];
export function TradeHistory() {
const [tickerFilter, setTickerFilter] = useState('');
const [page, setPage] = useState(0);
const { data, isLoading } = useTradingDecisions({
ticker: tickerFilter || undefined,
limit: PAGE_SIZE,
offset: page * PAGE_SIZE,
});
if (isLoading) return <LoadingSpinner />;
return (
<div className="space-y-4">
<Card>
<div className="mb-3 flex items-center justify-between">
<h2 className="text-sm font-medium text-gray-400">Trade History</h2>
<input
type="text"
placeholder="Filter by ticker…"
value={tickerFilter}
onChange={(e) => {
setTickerFilter(e.target.value.toUpperCase());
setPage(0);
}}
className="w-32 rounded-md border border-surface-700 bg-surface-950 px-2 py-1 text-xs text-gray-200 placeholder-gray-500 focus:border-brand-500 focus:outline-none"
aria-label="Filter by ticker"
/>
</div>
<DataTable<TradingDecision>
data={data ?? []}
columns={columns}
keyField="id"
pageSize={PAGE_SIZE}
emptyMessage="No trading decisions found"
/>
{/* Server-side pagination controls */}
<div className="mt-2 flex items-center justify-end gap-2 text-xs text-gray-500">
<button
onClick={() => setPage((p) => Math.max(0, p - 1))}
disabled={page === 0}
className="rounded px-2 py-1 hover:bg-surface-800 disabled:opacity-30"
aria-label="Previous page"
>
Prev
</button>
<span>Page {page + 1}</span>
<button
onClick={() => setPage((p) => p + 1)}
disabled={(data?.length ?? 0) < PAGE_SIZE}
className="rounded px-2 py-1 hover:bg-surface-800 disabled:opacity-30"
aria-label="Next page"
>
Next
</button>
</div>
</Card>
</div>
);
}
@@ -0,0 +1,193 @@
import { useState } from 'react';
import {
useTradingStatus,
usePauseTradingEngine,
useResumeTradingEngine,
useUpdateTradingConfig,
useTradingMetrics,
} from '../../api/tradingHooks';
import { Card, LoadingSpinner, StatusBadge } from '../../components/ui';
const RISK_TIERS = ['conservative', 'moderate', 'aggressive'];
function fmtUsd(v: number | null | undefined) {
if (v == null) return '—';
return `$${v.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
function fmtPct(v: number | null | undefined) {
if (v == null) return '—';
return `${(v * 100).toFixed(2)}%`;
}
function cooldownRemaining(expires: string | null | undefined): string {
if (!expires) return '';
const ms = new Date(expires).getTime() - Date.now();
if (ms <= 0) return 'expired';
const mins = Math.ceil(ms / 60000);
return mins >= 60 ? `${Math.floor(mins / 60)}h ${mins % 60}m` : `${mins}m`;
}
export function TradingOverview() {
const { data: status, isLoading } = useTradingStatus();
const { data: metrics } = useTradingMetrics();
const pause = usePauseTradingEngine();
const resume = useResumeTradingEngine();
const updateConfig = useUpdateTradingConfig();
const [selectedTier, setSelectedTier] = useState<string | null>(null);
if (isLoading) return <LoadingSpinner />;
if (!status) return <p className="text-gray-500">No trading status available</p>;
const effectiveTier = selectedTier ?? status.risk_tier;
function handleTierChange(tier: string) {
setSelectedTier(tier);
updateConfig.mutate({ risk_tier: tier });
}
return (
<div className="space-y-4">
{/* Controls */}
<Card>
<div className="flex items-center justify-between">
<h2 className="text-sm font-medium text-gray-400">Engine Controls</h2>
<div className="flex items-center gap-2">
{status.paused ? (
<button
onClick={() => resume.mutate()}
disabled={resume.isPending}
className="rounded-md bg-green-700 px-3 py-1.5 text-xs font-medium text-white hover:bg-green-600 disabled:opacity-50"
>
Resume
</button>
) : (
<button
onClick={() => pause.mutate()}
disabled={pause.isPending}
className="rounded-md bg-yellow-700 px-3 py-1.5 text-xs font-medium text-white hover:bg-yellow-600 disabled:opacity-50"
>
Pause
</button>
)}
</div>
</div>
<div className="mt-3 flex items-center gap-4">
<StatusBadge status={status.paused ? 'paused' : status.enabled ? 'active' : 'disabled'} />
<span className="text-xs text-gray-500">
{status.open_position_count} open positions
</span>
{status.last_decision_at && (
<span className="text-xs text-gray-500">
Last decision: {new Date(status.last_decision_at).toLocaleString()}
</span>
)}
</div>
</Card>
{/* Risk Tier & Circuit Breaker */}
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Risk Tier</h2>
<select
value={effectiveTier}
onChange={(e) => handleTierChange(e.target.value)}
className="rounded-md border border-surface-700 bg-surface-950 px-3 py-1.5 text-sm text-gray-200 focus:border-brand-500 focus:outline-none"
aria-label="Risk tier selector"
>
{RISK_TIERS.map((t) => (
<option key={t} value={t}>
{t.charAt(0).toUpperCase() + t.slice(1)}
</option>
))}
</select>
</Card>
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Circuit Breaker</h2>
<div className="flex items-center gap-3">
<StatusBadge status={status.circuit_breaker_active ? 'active' : 'inactive'} />
{status.circuit_breaker_active && (
<div className="text-xs text-gray-400">
{status.circuit_breaker_trigger_type && (
<span>Trigger: {status.circuit_breaker_trigger_type}</span>
)}
{status.circuit_breaker_cooldown_expires && (
<span className="ml-2">
Cooldown: {cooldownRemaining(status.circuit_breaker_cooldown_expires)}
</span>
)}
</div>
)}
</div>
</Card>
</div>
{/* Pool Balances & Portfolio Heat */}
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
<StatCard label="Active Pool" value={fmtUsd(status.active_pool)} />
<StatCard label="Reserve Pool" value={fmtUsd(status.reserve_pool)} />
<StatCard label="Portfolio Value" value={fmtUsd(status.portfolio_value)} />
<StatCard label="Portfolio Heat" value={fmtPct(status.portfolio_heat)} />
</div>
{/* Portfolio Heat Gauge */}
<Card>
<h2 className="mb-2 text-sm font-medium text-gray-400">Portfolio Heat</h2>
<div className="h-3 w-full rounded-full bg-surface-700">
<div
className={`h-full rounded-full transition-all ${
(status.portfolio_heat ?? 0) > 0.8
? 'bg-red-500'
: (status.portfolio_heat ?? 0) > 0.5
? 'bg-yellow-500'
: 'bg-green-500'
}`}
style={{ width: `${Math.min(100, (status.portfolio_heat ?? 0) * 100)}%` }}
/>
</div>
<div className="mt-1 flex justify-between text-xs text-gray-500">
<span>0%</span>
<span>{((status.portfolio_heat ?? 0) * 100).toFixed(1)}%</span>
<span>100%</span>
</div>
</Card>
{/* 24h P&L Summary */}
{metrics && (
<Card>
<h2 className="mb-3 text-sm font-medium text-gray-400">Last 24h P&amp;L Summary</h2>
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
<MiniStat label="Daily P&L" value={fmtUsd(metrics.daily_pnl)} color={pnlColor(metrics.daily_pnl)} />
<MiniStat label="Unrealized" value={fmtUsd(metrics.unrealized_pnl)} color={pnlColor(metrics.unrealized_pnl)} />
<MiniStat label="Win Rate" value={fmtPct(metrics.win_rate)} />
<MiniStat label="Profit Factor" value={metrics.profit_factor?.toFixed(2) ?? '—'} />
</div>
</Card>
)}
</div>
);
}
function pnlColor(v: number | null | undefined) {
if (v == null) return 'text-gray-300';
return v >= 0 ? 'text-green-400' : 'text-red-400';
}
function StatCard({ label, value }: { label: string; value: string }) {
return (
<Card className="text-center">
<div className="text-lg font-bold text-gray-100">{value}</div>
<div className="text-xs text-gray-500">{label}</div>
</Card>
);
}
function MiniStat({ label, value, color = 'text-gray-100' }: { label: string; value: string; color?: string }) {
return (
<div>
<div className={`text-sm font-semibold ${color}`}>{value}</div>
<div className="text-xs text-gray-500">{label}</div>
</div>
);
}
+7
View File
@@ -19,6 +19,7 @@ import { OrdersPage } from './pages/Orders';
import { OrderDetailPage } from './pages/OrderDetail';
import { PositionsPage } from './pages/Positions';
import { TradingPage } from './pages/Trading';
import { TradingEnginePage } from './pages/TradingEngine';
import { OpsPipelinePage } from './pages/OpsPipeline';
import { OpsIngestionPage } from './pages/OpsIngestion';
import { OpsModelPage } from './pages/OpsModel';
@@ -109,6 +110,11 @@ const tradingRoute = createRoute({
path: '/trading',
component: TradingPage,
});
const tradingEngineRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/trading/engine',
component: TradingEnginePage,
});
const opsPipelineRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/ops/pipeline',
@@ -166,6 +172,7 @@ const routeTree = rootRoute.addChildren([
orderDetailRoute,
positionsRoute,
tradingRoute,
tradingEngineRoute,
opsPipelineRoute,
opsIngestionRoute,
opsModelRoute,
@@ -102,6 +102,31 @@ spec:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: stonks-trading-engine-https
namespace: {{ .Release.Namespace }}
annotations:
cert-manager.io/cluster-issuer: {{ .Values.ingress.clusterIssuer }}
spec:
ingressClassName: {{ .Values.ingress.className }}
tls:
- hosts:
- {{ .Values.ingress.hosts.tradingEngine }}
secretName: stonks-trading-tls
rules:
- host: {{ .Values.ingress.hosts.tradingEngine }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: trading-engine
port:
number: 8000
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: stonks-dashboard-https
namespace: {{ .Release.Namespace }}
@@ -83,6 +83,59 @@ spec:
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-trading-engine-ingress
namespace: {{ .Release.Namespace }}
spec:
podSelector:
matchLabels:
app: trading-engine
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: query-api
- podSelector:
matchLabels:
app: dashboard
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: TCP
port: 8000
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: postgresql-service
ports:
- protocol: TCP
port: 5432
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: redis-service
ports:
- protocol: TCP
port: 6379
- to: []
ports:
- protocol: TCP
port: 443
- to:
- namespaceSelector: {}
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-superset-ingress
namespace: {{ .Release.Namespace }}
+15
View File
@@ -91,6 +91,20 @@ services:
requests: { cpu: 100m, memory: 128Mi }
limits: { cpu: 500m, memory: 256Mi }
tradingEngine:
replicas: 1
image: trading-engine
command: "uvicorn services.trading.app:app --host 0.0.0.0 --port 8000"
tier: trading
port: 8000
secrets: [stonks-core-secrets, stonks-broker-secrets]
resources:
requests: { cpu: 100m, memory: 256Mi }
limits: { cpu: 500m, memory: 512Mi }
probes:
readiness: { path: /ready, port: 8000, initialDelay: 5, period: 10 }
liveness: { path: /health, port: 8000, initialDelay: 10, period: 30 }
riskEngine:
replicas: 1
image: risk
@@ -223,6 +237,7 @@ ingress:
dashboard: stonks.celestium.life
superset: stonks-dash.celestium.life
trino: stonks-trino.celestium.life
tradingEngine: stonks-trading.celestium.life
## Analytics stack
trino:
@@ -0,0 +1,365 @@
-- Autonomous Trading Engine
-- Adds tables for trading engine configuration, reserve pool management,
-- risk tier history, circuit breakers, trading decisions, stop levels,
-- portfolio snapshots, backtesting, tax lots, earnings calendar,
-- correlation matrix cache, and notifications.
-- ============================================================
-- Trading Engine Configuration
-- ============================================================
CREATE TABLE trading_engine_config (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
enabled BOOLEAN NOT NULL DEFAULT FALSE,
paused BOOLEAN NOT NULL DEFAULT FALSE,
risk_tier VARCHAR(20) NOT NULL DEFAULT 'moderate',
reserve_siphon_pct FLOAT NOT NULL DEFAULT 0.20,
polling_interval_seconds INTEGER NOT NULL DEFAULT 60,
gradual_entry_tranches INTEGER NOT NULL DEFAULT 3,
gradual_entry_threshold_dollars FLOAT NOT NULL DEFAULT 30.0,
gradual_entry_interval_minutes INTEGER NOT NULL DEFAULT 15,
trading_window_start_minutes INTEGER NOT NULL DEFAULT 15,
trading_window_end_minutes INTEGER NOT NULL DEFAULT 15,
max_open_positions INTEGER NOT NULL DEFAULT 10,
circuit_breaker_daily_loss_pct FLOAT NOT NULL DEFAULT 0.05,
circuit_breaker_single_position_loss_pct FLOAT NOT NULL DEFAULT 0.15,
circuit_breaker_ticker_cooldown_hours INTEGER NOT NULL DEFAULT 48,
circuit_breaker_volatility_pause_hours INTEGER NOT NULL DEFAULT 2,
circuit_breaker_stop_loss_hits_threshold INTEGER NOT NULL DEFAULT 3,
circuit_breaker_stop_loss_window_minutes INTEGER NOT NULL DEFAULT 30,
active_pool_minimum FLOAT NOT NULL DEFAULT 100.0,
emergency_drawdown_threshold_pct FLOAT NOT NULL DEFAULT 0.40,
reserve_high_water_pct FLOAT NOT NULL DEFAULT 0.30,
absolute_position_cap FLOAT NOT NULL DEFAULT 50.0,
correlation_reduction_threshold FLOAT NOT NULL DEFAULT 0.5,
correlation_rejection_threshold FLOAT NOT NULL DEFAULT 0.8,
earnings_pre_window_days INTEGER NOT NULL DEFAULT 3,
earnings_post_cooldown_days INTEGER NOT NULL DEFAULT 1,
micro_trading_enabled BOOLEAN NOT NULL DEFAULT FALSE,
micro_trading_interval_seconds INTEGER NOT NULL DEFAULT 300,
micro_trading_allocation_cap_pct FLOAT NOT NULL DEFAULT 0.03,
micro_trading_max_daily INTEGER NOT NULL DEFAULT 10,
micro_trading_max_hold_minutes INTEGER NOT NULL DEFAULT 120,
notification_sms_enabled BOOLEAN NOT NULL DEFAULT FALSE,
notification_email_enabled BOOLEAN NOT NULL DEFAULT FALSE,
notification_phone_number VARCHAR(20),
notification_email_recipient VARCHAR(200),
notification_sns_topic_arn VARCHAR(300),
notification_rate_limit_sms_per_hour INTEGER NOT NULL DEFAULT 10,
notification_rate_limit_email_per_hour INTEGER NOT NULL DEFAULT 20,
notification_daily_summary_time TIME NOT NULL DEFAULT '16:30',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- ============================================================
-- Reserve Pool Ledger
-- ============================================================
CREATE TABLE reserve_pool_ledger (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
amount FLOAT NOT NULL,
balance_after FLOAT NOT NULL,
trigger_type VARCHAR(30) NOT NULL,
reference_id UUID,
notes TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_trigger_type CHECK (
trigger_type IN ('profit_siphon', 'emergency_liquidation', 'manual_adjustment', 'initial')
)
);
CREATE INDEX idx_reserve_pool_ledger_created ON reserve_pool_ledger(created_at DESC);
-- ============================================================
-- Risk Tier History
-- ============================================================
CREATE TABLE risk_tier_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
previous_tier VARCHAR(20) NOT NULL,
new_tier VARCHAR(20) NOT NULL,
trigger_source VARCHAR(30) NOT NULL,
trigger_metrics JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_risk_tier_history_created ON risk_tier_history(created_at DESC);
-- ============================================================
-- Circuit Breaker Events
-- ============================================================
CREATE TABLE circuit_breaker_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
trigger_type VARCHAR(30) NOT NULL,
threshold_value FLOAT,
actual_value FLOAT,
ticker VARCHAR(20),
cooldown_expires TIMESTAMPTZ,
resolved_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_cb_trigger_type CHECK (
trigger_type IN ('daily_loss', 'single_position', 'volatility', 'manual')
)
);
CREATE INDEX idx_circuit_breaker_events_active ON circuit_breaker_events(created_at DESC)
WHERE resolved_at IS NULL;
-- ============================================================
-- Trading Decisions
-- ============================================================
CREATE TABLE trading_decisions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
recommendation_id UUID REFERENCES recommendations(id),
decision VARCHAR(10) NOT NULL,
skip_reason TEXT,
ticker VARCHAR(20) NOT NULL,
computed_position_size FLOAT,
computed_share_quantity INTEGER,
risk_tier_at_decision VARCHAR(20) NOT NULL,
portfolio_heat_at_decision FLOAT,
active_pool_at_decision FLOAT,
reserve_pool_at_decision FLOAT,
circuit_breaker_status VARCHAR(20) NOT NULL DEFAULT 'inactive',
correlation_check_result JSONB DEFAULT '{}',
sector_exposure_check_result JSONB DEFAULT '{}',
earnings_proximity_flag BOOLEAN NOT NULL DEFAULT FALSE,
is_micro_trade BOOLEAN NOT NULL DEFAULT FALSE,
decision_trace JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_trading_decisions_ticker ON trading_decisions(ticker, created_at DESC);
CREATE INDEX idx_trading_decisions_rec ON trading_decisions(recommendation_id);
CREATE INDEX idx_trading_decisions_decision ON trading_decisions(decision, created_at DESC);
-- ============================================================
-- Position Stop Levels
-- ============================================================
CREATE TABLE position_stop_levels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ticker VARCHAR(20) NOT NULL,
entry_price FLOAT NOT NULL,
stop_loss_price FLOAT NOT NULL,
take_profit_price FLOAT NOT NULL,
trailing_stop_active BOOLEAN NOT NULL DEFAULT FALSE,
atr_value FLOAT NOT NULL,
atr_multiplier FLOAT NOT NULL,
reward_risk_ratio FLOAT NOT NULL,
signal_confidence FLOAT,
is_micro_trade BOOLEAN NOT NULL DEFAULT FALSE,
active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_position_stop_levels_ticker ON position_stop_levels(ticker) WHERE active = TRUE;
-- ============================================================
-- Portfolio Snapshots
-- ============================================================
CREATE TABLE portfolio_snapshots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
snapshot_date DATE NOT NULL UNIQUE,
portfolio_value FLOAT NOT NULL,
active_pool FLOAT NOT NULL,
reserve_pool FLOAT NOT NULL,
daily_return FLOAT,
cumulative_return FLOAT,
unrealized_pnl FLOAT,
realized_pnl FLOAT,
win_count INTEGER NOT NULL DEFAULT 0,
loss_count INTEGER NOT NULL DEFAULT 0,
win_rate FLOAT,
sharpe_ratio FLOAT,
max_drawdown FLOAT,
current_drawdown_pct FLOAT,
portfolio_heat FLOAT,
risk_tier VARCHAR(20),
positions JSONB NOT NULL DEFAULT '[]',
metrics JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_portfolio_snapshots_date ON portfolio_snapshots(snapshot_date DESC);
-- ============================================================
-- Backtest Runs
-- ============================================================
CREATE TABLE backtest_runs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
start_date DATE NOT NULL,
end_date DATE NOT NULL,
initial_capital FLOAT NOT NULL,
risk_tier VARCHAR(20) NOT NULL,
config JSONB NOT NULL DEFAULT '{}',
total_return FLOAT,
sharpe_ratio FLOAT,
max_drawdown FLOAT,
win_rate FLOAT,
profit_factor FLOAT,
trade_count INTEGER,
equity_curve JSONB DEFAULT '[]',
status VARCHAR(20) NOT NULL DEFAULT 'running',
completed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- ============================================================
-- Backtest Trades
-- ============================================================
CREATE TABLE backtest_trades (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
backtest_id UUID NOT NULL REFERENCES backtest_runs(id) ON DELETE CASCADE,
ticker VARCHAR(20) NOT NULL,
side VARCHAR(10) NOT NULL,
entry_price FLOAT NOT NULL,
exit_price FLOAT,
quantity INTEGER NOT NULL,
pnl FLOAT,
pnl_pct FLOAT,
entry_date DATE NOT NULL,
exit_date DATE,
hold_duration_days INTEGER,
recommendation_id UUID,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_backtest_trades_run ON backtest_trades(backtest_id);
-- ============================================================
-- Tax Lots
-- ============================================================
CREATE TABLE tax_lots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ticker VARCHAR(20) NOT NULL,
quantity INTEGER NOT NULL,
cost_basis_per_share FLOAT NOT NULL,
acquisition_date DATE NOT NULL,
status VARCHAR(10) NOT NULL DEFAULT 'open',
closed_date DATE,
exit_price FLOAT,
realized_pnl FLOAT,
wash_sale_flag BOOLEAN NOT NULL DEFAULT FALSE,
wash_sale_details TEXT,
order_id UUID REFERENCES orders(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_tax_lot_status CHECK (status IN ('open', 'closed', 'washed'))
);
CREATE INDEX idx_tax_lots_ticker ON tax_lots(ticker, acquisition_date DESC);
CREATE INDEX idx_tax_lots_status ON tax_lots(status) WHERE status = 'open';
-- ============================================================
-- Earnings Calendar
-- ============================================================
CREATE TABLE earnings_calendar (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ticker VARCHAR(20) NOT NULL,
earnings_date DATE NOT NULL,
source VARCHAR(30) NOT NULL DEFAULT 'manual',
confirmed BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(ticker, earnings_date)
);
CREATE INDEX idx_earnings_calendar_date ON earnings_calendar(earnings_date);
CREATE INDEX idx_earnings_calendar_ticker ON earnings_calendar(ticker);
-- ============================================================
-- Correlation Matrix Cache
-- ============================================================
CREATE TABLE correlation_matrix_cache (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ticker_a VARCHAR(20) NOT NULL,
ticker_b VARCHAR(20) NOT NULL,
correlation_coefficient FLOAT NOT NULL,
lookback_days INTEGER NOT NULL DEFAULT 90,
computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE(ticker_a, ticker_b)
);
CREATE INDEX idx_correlation_cache_tickers ON correlation_matrix_cache(ticker_a, ticker_b);
-- ============================================================
-- Notifications
-- ============================================================
CREATE TABLE notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
channel VARCHAR(10) NOT NULL,
event_type VARCHAR(50) NOT NULL,
message TEXT NOT NULL,
delivery_status VARCHAR(20) NOT NULL DEFAULT 'pending',
retry_count INTEGER NOT NULL DEFAULT 0,
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
delivered_at TIMESTAMPTZ,
CONSTRAINT chk_channel CHECK (channel IN ('sms', 'email')),
CONSTRAINT chk_delivery_status CHECK (
delivery_status IN ('pending', 'delivered', 'failed', 'rate_limited')
)
);
CREATE INDEX idx_notifications_created ON notifications(created_at DESC);
CREATE INDEX idx_notifications_event ON notifications(event_type, created_at DESC);
-- ============================================================
-- Default Data
-- ============================================================
-- Insert default trading engine configuration (moderate tier defaults)
INSERT INTO trading_engine_config (
enabled, paused, risk_tier, reserve_siphon_pct,
polling_interval_seconds, gradual_entry_tranches,
gradual_entry_threshold_dollars, gradual_entry_interval_minutes,
trading_window_start_minutes, trading_window_end_minutes,
max_open_positions,
circuit_breaker_daily_loss_pct, circuit_breaker_single_position_loss_pct,
circuit_breaker_ticker_cooldown_hours, circuit_breaker_volatility_pause_hours,
circuit_breaker_stop_loss_hits_threshold, circuit_breaker_stop_loss_window_minutes,
active_pool_minimum, emergency_drawdown_threshold_pct,
reserve_high_water_pct, absolute_position_cap,
correlation_reduction_threshold, correlation_rejection_threshold,
earnings_pre_window_days, earnings_post_cooldown_days,
micro_trading_enabled, micro_trading_interval_seconds,
micro_trading_allocation_cap_pct, micro_trading_max_daily,
micro_trading_max_hold_minutes,
notification_sms_enabled, notification_email_enabled,
notification_rate_limit_sms_per_hour, notification_rate_limit_email_per_hour,
notification_daily_summary_time
) VALUES (
FALSE, FALSE, 'moderate', 0.20,
60, 3,
30.0, 15,
15, 15,
10,
0.05, 0.15,
48, 2,
3, 30,
100.0, 0.40,
0.30, 50.0,
0.5, 0.8,
3, 1,
FALSE, 300,
0.03, 10,
120,
FALSE, FALSE,
10, 20,
'16:30'
);
-- Insert initial reserve pool ledger entry with zero balance
INSERT INTO reserve_pool_ledger (amount, balance_after, trigger_type, notes)
VALUES (0.0, 0.0, 'initial', 'Initial reserve pool entry');
+53
View File
@@ -168,6 +168,35 @@ class CompetitiveConfig:
propagation_failure_threshold: int = 5 # consecutive failures before operator alert
@dataclass
class TradingConfig:
"""Configuration for the autonomous trading engine.
Requirements: 16.1, 20.1, 19.5, 19.6
"""
enabled: bool = False
risk_tier: str = "moderate"
reserve_siphon_pct: float = 0.20
polling_interval_seconds: int = 60
stop_loss_check_interval_seconds: int = 300
fast_stop_loss_interval_seconds: int = 60
gradual_entry_tranches: int = 3
gradual_entry_threshold_dollars: float = 30.0
absolute_position_cap: float = 50.0
active_pool_minimum: float = 100.0
emergency_drawdown_threshold_pct: float = 0.40
reserve_high_water_pct: float = 0.30
micro_trading_enabled: bool = False
micro_trading_interval_seconds: int = 300
micro_trading_allocation_cap_pct: float = 0.03
micro_trading_max_daily: int = 10
micro_trading_max_hold_minutes: int = 120
sns_topic_arn: str = ""
sns_phone_number: str = ""
gmail_sender: str = ""
gmail_recipient: str = ""
@dataclass
class AppConfig:
postgres: PostgresConfig = field(default_factory=PostgresConfig)
@@ -181,6 +210,7 @@ class AppConfig:
alerting: AlertingConfig = field(default_factory=AlertingConfig)
macro: MacroConfig = field(default_factory=MacroConfig)
competitive: CompetitiveConfig = field(default_factory=CompetitiveConfig)
trading: TradingConfig = field(default_factory=TradingConfig)
log_level: str = "INFO"
json_logs: bool = True
@@ -278,6 +308,29 @@ def load_config() -> AppConfig:
min_pattern_samples=int(os.getenv("COMPETITIVE_MIN_PATTERN_SAMPLES", "3")),
propagation_failure_threshold=int(os.getenv("COMPETITIVE_PROPAGATION_FAILURE_THRESHOLD", "5")),
),
trading=TradingConfig(
enabled=os.getenv("TRADING_ENABLED", "false").lower() == "true",
risk_tier=os.getenv("TRADING_RISK_TIER", "moderate"),
reserve_siphon_pct=float(os.getenv("TRADING_RESERVE_SIPHON_PCT", "0.20")),
polling_interval_seconds=int(os.getenv("TRADING_POLLING_INTERVAL_SECONDS", "60")),
stop_loss_check_interval_seconds=int(os.getenv("TRADING_STOP_LOSS_CHECK_INTERVAL_SECONDS", "300")),
fast_stop_loss_interval_seconds=int(os.getenv("TRADING_FAST_STOP_LOSS_INTERVAL_SECONDS", "60")),
gradual_entry_tranches=int(os.getenv("TRADING_GRADUAL_ENTRY_TRANCHES", "3")),
gradual_entry_threshold_dollars=float(os.getenv("TRADING_GRADUAL_ENTRY_THRESHOLD_DOLLARS", "30.0")),
absolute_position_cap=float(os.getenv("TRADING_ABSOLUTE_POSITION_CAP", "50.0")),
active_pool_minimum=float(os.getenv("TRADING_ACTIVE_POOL_MINIMUM", "100.0")),
emergency_drawdown_threshold_pct=float(os.getenv("TRADING_EMERGENCY_DRAWDOWN_THRESHOLD_PCT", "0.40")),
reserve_high_water_pct=float(os.getenv("TRADING_RESERVE_HIGH_WATER_PCT", "0.30")),
micro_trading_enabled=os.getenv("TRADING_MICRO_TRADING_ENABLED", "false").lower() == "true",
micro_trading_interval_seconds=int(os.getenv("TRADING_MICRO_TRADING_INTERVAL_SECONDS", "300")),
micro_trading_allocation_cap_pct=float(os.getenv("TRADING_MICRO_TRADING_ALLOCATION_CAP_PCT", "0.03")),
micro_trading_max_daily=int(os.getenv("TRADING_MICRO_TRADING_MAX_DAILY", "10")),
micro_trading_max_hold_minutes=int(os.getenv("TRADING_MICRO_TRADING_MAX_HOLD_MINUTES", "120")),
sns_topic_arn=os.getenv("TRADING_SNS_TOPIC_ARN", ""),
sns_phone_number=os.getenv("TRADING_SNS_PHONE_NUMBER", ""),
gmail_sender=os.getenv("TRADING_GMAIL_SENDER", ""),
gmail_recipient=os.getenv("TRADING_GMAIL_RECIPIENT", ""),
),
log_level=os.getenv("LOG_LEVEL", "INFO"),
json_logs=os.getenv("JSON_LOGS", "true").lower() == "true",
)
+21
View File
@@ -65,3 +65,24 @@ QUEUE_LAKE_PUBLISH = "lake_publish"
QUEUE_TRADE = "trade"
QUEUE_BROKER = "broker_orders"
QUEUE_MACRO_CLASSIFICATION = "macro_classification"
# --- Trading engine ---
QUEUE_TRADING_DECISIONS = "trading_decisions"
TRADING_DEDUPE_PREFIX = f"{PREFIX}:dedupe:trading"
TRADING_CB_PREFIX = f"{PREFIX}:trading:circuit_breaker"
TRADING_NOTIFICATION_RATE = f"{PREFIX}:trading:notification_rate"
def trading_dedupe_key(recommendation_id: str) -> str:
"""Return the deduplication key for a trading recommendation."""
return f"{TRADING_DEDUPE_PREFIX}:{recommendation_id}"
def trading_cb_key(trigger_type: str) -> str:
"""Return the circuit breaker state key for a given trigger type."""
return f"{TRADING_CB_PREFIX}:{trigger_type}"
def trading_notification_rate_key(channel: str) -> str:
"""Return the notification rate-limit key for a given channel."""
return f"{TRADING_NOTIFICATION_RATE}:{channel}"
+33
View File
@@ -103,6 +103,39 @@ class EstimatedDuration(str, Enum):
LONG_TERM = "long_term"
# --- Autonomous Trading Engine Enums ---
class TradingDecisionType(str, Enum):
ACT = "act"
SKIP = "skip"
class CircuitBreakerTriggerType(str, Enum):
DAILY_LOSS = "daily_loss"
SINGLE_POSITION = "single_position"
VOLATILITY = "volatility"
MANUAL = "manual"
class ReservePoolTriggerType(str, Enum):
PROFIT_SIPHON = "profit_siphon"
EMERGENCY_LIQUIDATION = "emergency_liquidation"
MANUAL_ADJUSTMENT = "manual_adjustment"
INITIAL = "initial"
class NotificationChannel(str, Enum):
SMS = "sms"
EMAIL = "email"
class RiskTierName(str, Enum):
CONSERVATIVE = "conservative"
MODERATE = "moderate"
AGGRESSIVE = "aggressive"
# --- Document Intelligence ---
class CompanyImpact(BaseModel):
+1
View File
@@ -0,0 +1 @@
# Trading Engine - autonomous trading decisions, position sizing, and portfolio management
+295
View File
@@ -0,0 +1,295 @@
"""Trading Engine — FastAPI HTTP service for autonomous trading control.
Feature: autonomous-trading-engine
Exposes health/readiness probes, engine control (pause/resume),
configuration management, decision audit trail, performance metrics,
backtesting, and notification configuration endpoints.
Requirements: 1.7, 5.6, 6.6, 15.5, 16.2, 16.3, 16.4, 17.3, 19.9
"""
from __future__ import annotations
import logging
import uuid
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from typing import Any, Optional
from fastapi import FastAPI, HTTPException, Query
from pydantic import BaseModel
from services.shared.config import load_config
from services.trading.engine import TradingEngine
logger = logging.getLogger("trading_engine")
# ---------------------------------------------------------------------------
# Module-level state
# ---------------------------------------------------------------------------
config = load_config()
engine: Optional[TradingEngine] = None
# ---------------------------------------------------------------------------
# Pydantic request/response models
# ---------------------------------------------------------------------------
class ConfigUpdateRequest(BaseModel):
"""Body for PUT /api/trading/config."""
enabled: Optional[bool] = None
risk_tier: Optional[str] = None
reserve_siphon_pct: Optional[float] = None
polling_interval_seconds: Optional[int] = None
absolute_position_cap: Optional[float] = None
active_pool_minimum: Optional[float] = None
micro_trading_enabled: Optional[bool] = None
class BacktestRequest(BaseModel):
"""Body for POST /api/trading/backtest."""
start_date: str
end_date: str
initial_capital: float = 500.0
risk_tier: str = "moderate"
class NotificationConfigRequest(BaseModel):
"""Body for PUT /api/trading/notifications/config."""
sms_enabled: Optional[bool] = None
email_enabled: Optional[bool] = None
phone_number: Optional[str] = None
email_recipient: Optional[str] = None
# ---------------------------------------------------------------------------
# Lifespan
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Start and stop the TradingEngine with the application lifecycle."""
global engine
trading_cfg = config.trading
engine = TradingEngine(pool=None, redis=None, config=trading_cfg)
await engine.start()
logger.info("Trading engine started")
yield
if engine is not None:
await engine.stop()
logger.info("Trading engine stopped")
app = FastAPI(title="Stonks Oracle - Trading Engine", lifespan=lifespan)
# ---------------------------------------------------------------------------
# Health & Readiness
# ---------------------------------------------------------------------------
@app.get("/health")
async def health() -> dict[str, str]:
"""Liveness probe."""
return {"status": "ok"}
@app.get("/ready")
async def ready() -> dict[str, bool]:
"""Readiness probe — reports whether the engine is running."""
is_ready = engine is not None and engine.running
return {"ready": is_ready}
# ---------------------------------------------------------------------------
# Engine Status & Control
# ---------------------------------------------------------------------------
@app.get("/api/trading/status")
async def trading_status() -> dict[str, Any]:
"""Return current engine state."""
if engine is None:
raise HTTPException(503, "Engine not initialised")
return {
"enabled": engine.config.enabled,
"paused": not engine.running,
"risk_tier": engine.config.risk_tier,
"circuit_breaker_status": "inactive",
"active_pool": 0.0,
"reserve_pool": 0.0,
"portfolio_heat": 0.0,
"open_positions": 0,
"last_decision_at": None,
}
@app.put("/api/trading/config")
async def update_config(body: ConfigUpdateRequest) -> dict[str, Any]:
"""Update trading engine configuration.
Returns the previous and new configuration values for audit trail.
Requirements: 16.6
"""
if engine is None:
raise HTTPException(503, "Engine not initialised")
previous: dict[str, Any] = {}
updated: dict[str, Any] = {}
for field_name in body.model_fields_set:
new_value = getattr(body, field_name)
old_value = getattr(engine.config, field_name, None)
previous[field_name] = old_value
updated[field_name] = new_value
setattr(engine.config, field_name, new_value)
return {
"previous": previous,
"updated": updated,
"change_source": "api",
"changed_at": datetime.now(tz=timezone.utc).isoformat(),
}
@app.post("/api/trading/pause")
async def pause_engine() -> dict[str, bool]:
"""Pause the trading engine."""
if engine is not None:
engine.running = False
return {"paused": True}
@app.post("/api/trading/resume")
async def resume_engine() -> dict[str, bool]:
"""Resume the trading engine."""
if engine is not None:
engine.running = True
return {"paused": False}
# ---------------------------------------------------------------------------
# Decision Audit Trail
# ---------------------------------------------------------------------------
@app.get("/api/trading/decisions")
async def list_decisions(
ticker: Optional[str] = None,
decision: Optional[str] = None,
limit: int = Query(default=50, le=200),
offset: int = 0,
) -> list[dict[str, Any]]:
"""Return recent trading decisions (placeholder — paginated)."""
return []
# ---------------------------------------------------------------------------
# Performance Metrics
# ---------------------------------------------------------------------------
@app.get("/api/trading/metrics")
async def current_metrics() -> dict[str, Any]:
"""Return current performance metrics (placeholder)."""
return {
"total_portfolio_value": 0.0,
"active_pool": 0.0,
"reserve_pool": 0.0,
"unrealized_pnl": 0.0,
"realized_pnl": 0.0,
"daily_pnl": 0.0,
"win_rate": 0.0,
"profit_factor": 0.0,
"sharpe_ratio": 0.0,
"max_drawdown": 0.0,
"portfolio_heat": 0.0,
}
@app.get("/api/trading/metrics/history")
async def metrics_history(
limit: int = Query(default=30, le=365),
) -> list[dict[str, Any]]:
"""Return historical daily snapshots (placeholder)."""
return []
# ---------------------------------------------------------------------------
# Backtesting
# ---------------------------------------------------------------------------
@app.post("/api/trading/backtest")
async def launch_backtest(body: BacktestRequest) -> dict[str, str]:
"""Launch a backtest run and return its ID."""
backtest_id = str(uuid.uuid4())
return {"backtest_id": backtest_id}
@app.get("/api/trading/backtest/{backtest_id}")
async def get_backtest(backtest_id: str) -> dict[str, Any]:
"""Retrieve backtest results (placeholder)."""
return {
"backtest_id": backtest_id,
"status": "pending",
"config": None,
"result": None,
}
# ---------------------------------------------------------------------------
# Notifications
# ---------------------------------------------------------------------------
@app.get("/api/trading/notifications/config")
async def get_notification_config() -> dict[str, Any]:
"""Return current notification configuration."""
if engine is None:
raise HTTPException(503, "Engine not initialised")
return {
"sms_enabled": bool(engine.config.sns_topic_arn),
"email_enabled": bool(engine.config.gmail_recipient),
"phone_number": engine.config.sns_phone_number,
"email_recipient": engine.config.gmail_recipient,
}
@app.put("/api/trading/notifications/config")
async def update_notification_config(
body: NotificationConfigRequest,
) -> dict[str, Any]:
"""Update notification preferences."""
if engine is None:
raise HTTPException(503, "Engine not initialised")
result: dict[str, Any] = {}
if body.phone_number is not None:
engine.config.sns_phone_number = body.phone_number
result["phone_number"] = body.phone_number
if body.email_recipient is not None:
engine.config.gmail_recipient = body.email_recipient
result["email_recipient"] = body.email_recipient
return {"updated": result}
@app.get("/api/trading/notifications/history")
async def notification_history(
limit: int = Query(default=50, le=200),
) -> list[dict[str, Any]]:
"""Return recent notifications (placeholder)."""
return []
+115
View File
@@ -0,0 +1,115 @@
"""Backtester for the autonomous trading engine.
Pure computation module that assembles backtest results from
pre-computed trade data and daily returns. The actual replay logic
(fetching historical data, simulating decisions) requires DB access
and will be wired in the integration layer. This module provides
the pure computation for result assembly.
"""
from __future__ import annotations
import uuid
from dataclasses import dataclass, field
from datetime import date
from services.trading.models import ClosedTrade
from services.trading.performance_tracker import PerformanceComputer
# ---------------------------------------------------------------------------
# Data classes
# ---------------------------------------------------------------------------
@dataclass
class BacktestConfig:
"""Configuration for a backtest run."""
start_date: date
end_date: date
initial_capital: float
risk_tier: str # conservative | moderate | aggressive
@dataclass
class BacktestResult:
"""Output of a completed backtest run."""
backtest_id: str
config: BacktestConfig
total_return: float
sharpe_ratio: float
max_drawdown: float
win_rate: float
profit_factor: float
trade_count: int
trade_log: list[dict] = field(default_factory=list)
equity_curve: list[dict] = field(default_factory=list)
# ---------------------------------------------------------------------------
# Engine
# ---------------------------------------------------------------------------
class BacktestEngine:
"""Assembles a BacktestResult from pre-computed trade data.
Uses :class:`PerformanceComputer` to derive metrics from closed
trades and daily returns, then packages everything into a
:class:`BacktestResult`.
"""
def __init__(self) -> None:
self._perf = PerformanceComputer()
def compute_result(
self,
config: BacktestConfig,
trades: list[ClosedTrade],
daily_returns: list[float],
equity_curve: list[dict],
) -> BacktestResult:
"""Build a :class:`BacktestResult` from raw simulation outputs.
Args:
config: The backtest configuration that was used.
trades: Closed trades produced by the simulation.
daily_returns: Daily return percentages for the simulated period.
equity_curve: List of ``{"date": ..., "portfolio_value": ...}``
dicts representing the equity curve.
Returns:
A fully populated :class:`BacktestResult`.
"""
metrics = self._perf.compute_metrics(
closed_trades=trades,
portfolio_value=config.initial_capital,
active_pool=config.initial_capital,
reserve_pool=0.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
trade_log = [self._perf.compute_trade_metrics(t) for t in trades]
total_return = (
sum(t.pnl for t in trades) / config.initial_capital
if config.initial_capital > 0
else 0.0
)
return BacktestResult(
backtest_id=str(uuid.uuid4()),
config=config,
total_return=total_return,
sharpe_ratio=metrics.sharpe_ratio,
max_drawdown=metrics.max_drawdown,
win_rate=metrics.win_rate,
profit_factor=metrics.profit_factor,
trade_count=len(trades),
trade_log=trade_log,
equity_curve=equity_curve,
)
+152
View File
@@ -0,0 +1,152 @@
"""Circuit breaker safety mechanism for the autonomous trading engine.
Pure computation module — no DB or Redis access. State management and
persistence are handled by the caller. All methods operate on values
passed in as arguments and return deterministic results.
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from services.trading.models import CircuitBreakerState
class CircuitBreaker:
"""Evaluates circuit breaker conditions and computes cooldown expiries.
Parameters
----------
daily_loss_pct:
Maximum allowed daily portfolio loss as a fraction (e.g. 0.05 = 5%).
single_position_loss_pct:
Maximum allowed loss on a single position as a fraction of entry value.
ticker_cooldown_hours:
Hours a ticker is blocked from re-entry after a single-position breach.
volatility_pause_hours:
Hours trading is paused after a volatility (stop-loss cluster) trigger.
stop_loss_hits_threshold:
Number of stop-loss hits within the window that triggers a volatility pause.
stop_loss_window_minutes:
Rolling window (in minutes) for counting clustered stop-loss hits.
"""
def __init__(
self,
daily_loss_pct: float = 0.05,
single_position_loss_pct: float = 0.15,
ticker_cooldown_hours: int = 48,
volatility_pause_hours: int = 2,
stop_loss_hits_threshold: int = 3,
stop_loss_window_minutes: int = 30,
) -> None:
self.daily_loss_pct = daily_loss_pct
self.single_position_loss_pct = single_position_loss_pct
self.ticker_cooldown_hours = ticker_cooldown_hours
self.volatility_pause_hours = volatility_pause_hours
self.stop_loss_hits_threshold = stop_loss_hits_threshold
self.stop_loss_window_minutes = stop_loss_window_minutes
# ------------------------------------------------------------------
# Trigger checks
# ------------------------------------------------------------------
def check_daily_loss(self, daily_pnl: float, portfolio_value: float) -> bool:
"""Return True when the portfolio has dropped more than *daily_loss_pct* today.
Parameters
----------
daily_pnl:
Today's profit/loss — negative values represent losses.
portfolio_value:
Current total portfolio value (must be positive).
"""
if portfolio_value <= 0:
return True # degenerate case — treat as triggered
loss_ratio = abs(daily_pnl) / portfolio_value
return daily_pnl < 0 and loss_ratio > self.daily_loss_pct
def check_single_position(self, position_loss_pct: float) -> bool:
"""Return True when a single position has lost more than the threshold.
Parameters
----------
position_loss_pct:
Loss expressed as a positive fraction of entry value
(e.g. 0.15 means the position is down 15%).
"""
return position_loss_pct > self.single_position_loss_pct
def check_volatility(self, stop_loss_hits: list[datetime]) -> bool:
"""Return True when too many stop-losses fired within the rolling window.
Parameters
----------
stop_loss_hits:
Timestamps of recent stop-loss trigger events.
"""
if len(stop_loss_hits) < self.stop_loss_hits_threshold:
return False
window = timedelta(minutes=self.stop_loss_window_minutes)
sorted_hits = sorted(stop_loss_hits)
# Sliding window: check every contiguous sub-sequence of length
# *stop_loss_hits_threshold* to see if it fits within the window.
for i in range(len(sorted_hits) - self.stop_loss_hits_threshold + 1):
start = sorted_hits[i]
end = sorted_hits[i + self.stop_loss_hits_threshold - 1]
if end - start <= window:
return True
return False
# ------------------------------------------------------------------
# Cooldown helpers
# ------------------------------------------------------------------
def is_ticker_cooled_down(
self,
ticker: str,
ticker_cooldowns: dict[str, datetime],
now: datetime | None = None,
) -> bool:
"""Return True if *ticker* is still in its cooldown period.
Returns False when the cooldown has expired or the ticker has no
active cooldown.
"""
if ticker not in ticker_cooldowns:
return False
now = now or datetime.now(tz=timezone.utc)
return now < ticker_cooldowns[ticker]
def is_active(
self,
state: CircuitBreakerState,
now: datetime | None = None,
) -> bool:
"""Return True if any circuit breaker is currently active (not expired)."""
if not state.active:
return False
if state.cooldown_expires is None:
# Active with no expiry — treat as active.
return True
now = now or datetime.now(tz=timezone.utc)
return now < state.cooldown_expires
def compute_cooldown_expiry(
self,
trigger_type: str,
triggered_at: datetime,
) -> datetime:
"""Compute when the cooldown for *trigger_type* expires.
* ``daily_loss`` / ``volatility`` → *triggered_at* + *volatility_pause_hours*
* ``single_position`` → *triggered_at* + *ticker_cooldown_hours*
"""
if trigger_type == "single_position":
return triggered_at + timedelta(hours=self.ticker_cooldown_hours)
# daily_loss, volatility, and any other type default to the
# volatility pause duration.
return triggered_at + timedelta(hours=self.volatility_pause_hours)
+67
View File
@@ -0,0 +1,67 @@
"""Correlation matrix operations for portfolio diversification.
Feature: autonomous-trading-engine
Pure computation module for price correlation coefficients between
tracked companies. Used by the Position Sizer to prevent
over-concentration in correlated positions.
"""
from __future__ import annotations
from services.trading.models import OpenPosition
class CorrelationMatrix:
"""In-memory correlation matrix for ticker pairs.
Stores pairwise correlation coefficients and provides lookup
methods for individual pairs and weighted portfolio averages.
"""
def __init__(self) -> None:
self._data: dict[tuple[str, str], float] = {}
def load(self, data: dict[tuple[str, str], float]) -> None:
"""Load correlation data from a dict of (ticker_a, ticker_b) -> coefficient."""
self._data = dict(data)
def get_correlation(self, ticker_a: str, ticker_b: str) -> float:
"""Return the correlation coefficient for a ticker pair.
Checks both orderings (a, b) and (b, a). Returns 0.0 if the
pair is not in the matrix.
"""
if ticker_a == ticker_b:
return 1.0
return self._data.get(
(ticker_a, ticker_b),
self._data.get((ticker_b, ticker_a), 0.0),
)
def get_portfolio_correlation(
self,
candidate: str,
positions: list[OpenPosition],
) -> float:
"""Compute weighted average correlation between candidate and positions.
Weights are based on each position's market_value. Returns 0.0
if there are no positions or total weight is zero.
"""
if not positions:
return 0.0
total_weight = 0.0
weighted_corr = 0.0
for pos in positions:
corr = self.get_correlation(candidate, pos.ticker)
weight = pos.market_value
weighted_corr += corr * weight
total_weight += weight
if total_weight == 0.0:
return 0.0
return weighted_corr / total_weight
+512
View File
@@ -0,0 +1,512 @@
"""Core autonomous trading engine — decision loop and pre-trade evaluation.
Feature: autonomous-trading-engine
Coordinates all trading sub-components (PositionSizer, StopLossManager,
CircuitBreaker, ReservePoolController, RiskTierController, CorrelationMatrix)
to evaluate recommendations and produce TradingDecision records.
The ``evaluate_recommendation`` method is deliberately synchronous-compatible
so that it can be tested without real DB/Redis connections. The async
``start`` / ``stop`` methods are thin lifecycle stubs wired up in Task 25.
"""
from __future__ import annotations
import uuid
from datetime import datetime, timezone
from services.shared.config import TradingConfig
from services.trading.circuit_breaker import CircuitBreaker
from services.trading.correlation import CorrelationMatrix
from services.trading.micro_trading import MicroTradeConfig, MicroTradingModule
from services.trading.models import (
CircuitBreakerState,
OpenPosition,
PerformanceMetrics,
PortfolioState,
PositionSizeResult,
RiskTierConfig,
StopLevels,
StopTrigger,
TradingDecision,
)
from services.trading.notifications import NotificationRecord, NotificationService
from services.trading.position_sizer import PositionSizer
from services.trading.rebalancer import PortfolioRebalancer
from services.trading.reserve_pool import ReservePoolController
from services.trading.risk_tier_controller import RiskTierController
from services.trading.stop_loss_manager import StopLossManager
from services.trading.trading_window import is_within_trading_window
class TradingEngine:
"""Main autonomous trading engine.
Manages the decision loop, coordinates all sub-components,
and maintains runtime state.
Parameters
----------
pool:
asyncpg connection pool (used by async lifecycle methods).
redis:
Redis client (used for deduplication and pub/sub).
config:
Trading engine configuration.
"""
def __init__(
self,
pool: object,
redis: object,
config: TradingConfig,
) -> None:
self.pool = pool
self.redis = redis
self.config = config
# Sub-components
self.position_sizer = PositionSizer()
self.stop_loss_manager = StopLossManager()
self.circuit_breaker = CircuitBreaker()
self.reserve_pool_controller = ReservePoolController(
siphon_pct=config.reserve_siphon_pct,
high_water_pct=config.reserve_high_water_pct,
)
self.risk_tier_controller = RiskTierController()
self.correlation_matrix = CorrelationMatrix()
self.notification_service = NotificationService()
self.micro_trading_module = MicroTradingModule()
self.rebalancer = PortfolioRebalancer()
# Runtime state
self.running: bool = False
self.portfolio_state: PortfolioState | None = None
self.processed_recommendation_ids: set[str] = set()
# ------------------------------------------------------------------
# Lifecycle (stubs — wired in Task 25)
# ------------------------------------------------------------------
async def start(self) -> None:
"""Load portfolio state and enter the decision loop.
Full implementation is deferred to Task 25. This stub sets the
``running`` flag so readiness probes can report status.
"""
self.running = True
async def stop(self) -> None:
"""Graceful shutdown — cancel pending work and persist state.
Full implementation is deferred to Task 25.
"""
self.running = False
# ------------------------------------------------------------------
# Core evaluation logic (synchronous-compatible for testing)
# ------------------------------------------------------------------
def evaluate_recommendation(
self,
rec: dict,
portfolio_state: PortfolioState,
risk_tier: RiskTierConfig,
circuit_breaker_state: CircuitBreakerState,
correlation_matrix: CorrelationMatrix,
earnings_calendar: dict,
now: datetime | None = None,
) -> TradingDecision:
"""Run all pre-trade checks and produce a TradingDecision.
The checks are applied in order; the first failure short-circuits
with a ``skip`` decision. If all checks pass the PositionSizer
is invoked and its result determines the final decision.
Parameters
----------
rec:
Recommendation dict with at least ``recommendation_id``,
``ticker``, ``confidence``, ``sector``, ``current_price``,
and ``action``.
portfolio_state:
Current portfolio snapshot.
risk_tier:
Active risk tier configuration.
circuit_breaker_state:
Current circuit breaker state.
correlation_matrix:
Correlation matrix instance for diversification checks.
earnings_calendar:
Mapping of ticker → next earnings datetime.
now:
Optional override for the current timestamp (for testing).
"""
now = now or datetime.now(tz=timezone.utc)
rec_id = rec.get("recommendation_id")
ticker = rec.get("ticker", "")
confidence = rec.get("confidence", 0.0)
sector = rec.get("sector", "")
current_price = rec.get("current_price", 0.0)
reasoning: list[str] = []
# --- a. Circuit breaker active? --------------------------------
if self.circuit_breaker.is_active(circuit_breaker_state, now=now):
reasoning.append("Circuit breaker is active")
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="circuit_breaker_active",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- b. Trading window? ----------------------------------------
if not is_within_trading_window(now):
reasoning.append("Outside trading window")
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="outside_trading_window",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- c. Confidence gate (early check before sizer) -------------
if confidence < risk_tier.min_confidence:
reasoning.append(
f"Confidence {confidence:.4f} below tier minimum "
f"{risk_tier.min_confidence}"
)
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="insufficient_confidence",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- d. Deduplication check ------------------------------------
if rec_id and rec_id in self.processed_recommendation_ids:
reasoning.append(f"Recommendation {rec_id} already processed")
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="duplicate_recommendation",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- e. Multiple declining positions check ---------------------
if self.check_declining_positions(portfolio_state.positions):
reasoning.append(
"Multiple declining positions — halting new entries"
)
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="multiple_declining_positions",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- f. Max open positions check -------------------------------
max_positions = self.config.max_open_positions if hasattr(self.config, "max_open_positions") else 10
if self.check_max_positions(
portfolio_state.open_position_count, max_positions
):
reasoning.append(
f"At max open positions ({portfolio_state.open_position_count}/"
f"{max_positions})"
)
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason="max_positions_reached",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# --- g. Position sizing ----------------------------------------
reasoning.append("All pre-trade checks passed — computing position size")
# Build the raw correlation dict expected by PositionSizer
corr_dict: dict[tuple[str, str], float] = correlation_matrix._data
size_result: PositionSizeResult = self.position_sizer.compute(
confidence=confidence,
ticker=ticker,
sector=sector,
current_price=current_price,
active_pool=portfolio_state.active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio_state,
correlation_matrix=corr_dict,
earnings_calendar=earnings_calendar,
absolute_position_cap=self.config.absolute_position_cap,
active_pool_minimum=self.config.active_pool_minimum,
)
reasoning.extend(size_result.adjustments)
if size_result.rejected:
reasoning.append(f"Position sizer rejected: {size_result.rejection_reason}")
return self._skip_decision(
rec_id=rec_id,
ticker=ticker,
skip_reason=f"position_sizer_rejected: {size_result.rejection_reason}",
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# Mark recommendation as processed
if rec_id:
self.processed_recommendation_ids.add(rec_id)
return self._act_decision(
rec_id=rec_id,
ticker=ticker,
size_result=size_result,
risk_tier=risk_tier,
portfolio_state=portfolio_state,
circuit_breaker_state=circuit_breaker_state,
reasoning=reasoning,
now=now,
)
# ------------------------------------------------------------------
# Helper checks
# ------------------------------------------------------------------
def check_declining_positions(
self,
positions: list[OpenPosition],
threshold_pct: float = 0.50,
decline_pct: float = 0.02,
) -> bool:
"""Return True if > threshold_pct of positions have > decline_pct negative unrealized P&L.
A position is considered "declining" when its unrealized P&L as a
fraction of its entry value is worse than ``-decline_pct``.
Parameters
----------
positions:
List of currently open positions.
threshold_pct:
Fraction of positions that must be declining to trigger
(default 0.50 = 50%).
decline_pct:
Minimum loss fraction to count as declining
(default 0.02 = 2%).
"""
if not positions:
return False
declining_count = 0
for pos in positions:
entry_value = pos.entry_price * pos.quantity
if entry_value <= 0:
continue
loss_pct = -pos.unrealized_pnl / entry_value
if loss_pct > decline_pct:
declining_count += 1
return declining_count > threshold_pct * len(positions)
def check_max_positions(
self,
open_count: int,
max_positions: int = 10,
) -> bool:
"""Return True if the portfolio is at the maximum number of open positions."""
return open_count >= max_positions
# ------------------------------------------------------------------
# Integration wiring — thin wrappers for the decision loop
# ------------------------------------------------------------------
def check_stop_loss_crossings(
self,
positions: list[OpenPosition],
prices: dict[str, float],
stop_levels: dict[str, StopLevels],
) -> list[StopTrigger]:
"""Delegate to StopLossManager.check_price_crossings().
Called by the async decision loop at the configured interval
(5 min default, 60s during high-severity events).
"""
return self.stop_loss_manager.check_price_crossings(
positions, prices, stop_levels
)
def handle_position_close(
self,
realized_profit: float,
reserve_balance: float,
) -> tuple[float, float]:
"""Delegate to ReservePoolController.siphon_profit().
Called when a position close event is detected from broker
service fill events.
"""
return self.reserve_pool_controller.siphon_profit(
realized_profit, reserve_balance
)
def evaluate_risk_tier(
self,
current_tier: str,
metrics: PerformanceMetrics,
reserve_pct: float,
) -> str | None:
"""Delegate to RiskTierController.evaluate().
Scheduled to run at daily market close.
"""
return self.risk_tier_controller.evaluate(
current_tier, metrics, reserve_pct
)
def evaluate_rebalancing(
self,
positions: list[OpenPosition],
risk_tier: RiskTierConfig,
active_pool: float,
) -> list:
"""Delegate to PortfolioRebalancer.evaluate().
Scheduled to run weekly at Monday market open.
"""
return self.rebalancer.evaluate(positions, risk_tier, active_pool)
def create_alert(
self,
event_type: str,
details: str,
) -> NotificationRecord:
"""Create a notification record for a critical event.
Delegates formatting and record creation to NotificationService.
The caller is responsible for actual delivery.
"""
message = self.notification_service.format_alert(event_type, details)
return self.notification_service.create_notification(
channel="email",
event_type=event_type,
message=message,
)
def check_micro_trade_constraints(
self,
daily_count: int,
is_within_window: bool,
circuit_breaker_active: bool,
portfolio_heat_pct: float,
max_heat: float,
) -> tuple[bool, str]:
"""Delegate to MicroTradingModule.check_constraints().
Called by the decision loop when micro-trading is enabled.
"""
micro_config = MicroTradeConfig(
enabled=self.config.micro_trading_enabled,
allocation_cap_pct=self.config.micro_trading_allocation_cap_pct,
max_daily=self.config.micro_trading_max_daily,
max_hold_minutes=self.config.micro_trading_max_hold_minutes,
)
return self.micro_trading_module.check_constraints(
config=micro_config,
daily_count=daily_count,
is_within_window=is_within_window,
circuit_breaker_active=circuit_breaker_active,
portfolio_heat_pct=portfolio_heat_pct,
max_heat=max_heat,
)
# ------------------------------------------------------------------
# Decision builders
# ------------------------------------------------------------------
def _skip_decision(
self,
*,
rec_id: str | None,
ticker: str,
skip_reason: str,
risk_tier: RiskTierConfig,
portfolio_state: PortfolioState,
circuit_breaker_state: CircuitBreakerState,
reasoning: list[str],
now: datetime,
) -> TradingDecision:
return TradingDecision(
id=str(uuid.uuid4()),
recommendation_id=rec_id,
decision="skip",
skip_reason=skip_reason,
ticker=ticker,
computed_position_size=None,
computed_share_quantity=None,
risk_tier_at_decision=risk_tier.name,
portfolio_heat_at_decision=portfolio_state.portfolio_heat,
active_pool_at_decision=portfolio_state.active_pool,
reserve_pool_at_decision=portfolio_state.reserve_pool,
circuit_breaker_status="active" if circuit_breaker_state.active else "inactive",
decision_trace={"reasoning": reasoning},
created_at=now,
)
def _act_decision(
self,
*,
rec_id: str | None,
ticker: str,
size_result: PositionSizeResult,
risk_tier: RiskTierConfig,
portfolio_state: PortfolioState,
circuit_breaker_state: CircuitBreakerState,
reasoning: list[str],
now: datetime,
) -> TradingDecision:
return TradingDecision(
id=str(uuid.uuid4()),
recommendation_id=rec_id,
decision="act",
skip_reason=None,
ticker=ticker,
computed_position_size=size_result.dollar_amount,
computed_share_quantity=size_result.share_quantity,
risk_tier_at_decision=risk_tier.name,
portfolio_heat_at_decision=portfolio_state.portfolio_heat,
active_pool_at_decision=portfolio_state.active_pool,
reserve_pool_at_decision=portfolio_state.reserve_pool,
circuit_breaker_status="active" if circuit_breaker_state.active else "inactive",
decision_trace={"reasoning": reasoning},
created_at=now,
)
+81
View File
@@ -0,0 +1,81 @@
"""Gradual entry logic for the autonomous trading engine.
Pure computation module that determines whether an order should be split
into multiple tranches and performs the splitting. The
``GradualEntryManager`` class (which tracks pending tranches at runtime
and re-evaluates conditions) is intentionally left as a thin wrapper
here — the core logic is in the pure functions below.
"""
from __future__ import annotations
from dataclasses import dataclass
# ---------------------------------------------------------------------------
# Tranche dataclass
# ---------------------------------------------------------------------------
@dataclass
class Tranche:
"""A single tranche within a gradual-entry order sequence."""
tranche_index: int
quantity: int
parent_decision_id: str
status: str = "pending"
# ---------------------------------------------------------------------------
# Pure computation helpers
# ---------------------------------------------------------------------------
def should_use_gradual_entry(
position_size_dollars: float,
active_pool: float,
threshold_dollars: float = 30.0,
) -> bool:
"""Return True when the position size exceeds the gradual-entry threshold.
The effective threshold is ``min(threshold_dollars, 5% of active_pool)``.
"""
effective_threshold = min(threshold_dollars, 0.05 * active_pool)
return position_size_dollars > effective_threshold
def split_into_tranches(total_quantity: int, num_tranches: int = 3) -> list[int]:
"""Split *total_quantity* into *num_tranches* approximately equal parts.
The remainder is distributed one unit at a time to the first tranches
so that:
* ``sum(result) == total_quantity``
* All values differ by at most 1
"""
if num_tranches <= 0:
return []
if total_quantity <= 0:
return [0] * num_tranches
base, remainder = divmod(total_quantity, num_tranches)
return [base + (1 if i < remainder else 0) for i in range(num_tranches)]
def create_tranches(
total_quantity: int,
parent_decision_id: str,
num_tranches: int = 3,
) -> list[Tranche]:
"""Create :class:`Tranche` objects linked to *parent_decision_id*.
Uses :func:`split_into_tranches` for the quantity distribution.
"""
quantities = split_into_tranches(total_quantity, num_tranches)
return [
Tranche(
tranche_index=i,
quantity=q,
parent_decision_id=parent_decision_id,
)
for i, q in enumerate(quantities)
]
+137
View File
@@ -0,0 +1,137 @@
"""Micro-trading module for the autonomous trading engine.
Pure computation module for micro-trade evaluation logic. Handles
allocation caps, daily limits, auto-close decisions, and constraint
checking. Actual signal fetching and order submission are deferred
to the engine integration layer.
"""
from __future__ import annotations
from dataclasses import dataclass
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
@dataclass
class MicroTradeConfig:
"""Configuration for the micro-trading module."""
enabled: bool = False
allocation_cap_pct: float = 0.03
max_daily: int = 10
max_hold_minutes: int = 120
stop_loss_atr_multiplier: float = 1.0
reward_risk_ratio: float = 1.5
# ---------------------------------------------------------------------------
# Micro-trading module (pure computation)
# ---------------------------------------------------------------------------
class MicroTradingModule:
"""Pure-computation micro-trading evaluator.
All methods are side-effect-free. The engine integration layer is
responsible for fetching signals, submitting orders, and persisting
state.
"""
def should_evaluate(
self,
config: MicroTradeConfig,
daily_count: int,
) -> bool:
"""Check whether micro-trade evaluation should proceed.
Args:
config: Current micro-trade configuration.
daily_count: Number of micro-trades already executed today.
Returns:
``True`` if micro-trading is enabled and the daily limit
has not been reached.
"""
if not config.enabled:
return False
return daily_count < config.max_daily
def compute_allocation_cap(
self,
config: MicroTradeConfig,
active_pool: float,
) -> float:
"""Compute the maximum dollar allocation for a single micro-trade.
Args:
config: Current micro-trade configuration.
active_pool: Current active pool value.
Returns:
Maximum dollar amount for a micro-trade.
"""
return config.allocation_cap_pct * active_pool
def should_auto_close(
self,
config: MicroTradeConfig,
hold_minutes: float,
) -> bool:
"""Determine whether a micro-trade should be auto-closed.
Args:
config: Current micro-trade configuration.
hold_minutes: How long the position has been held, in minutes.
Returns:
``True`` when the hold duration exceeds the configured maximum.
"""
return hold_minutes > config.max_hold_minutes
def check_constraints(
self,
config: MicroTradeConfig,
daily_count: int,
is_within_window: bool,
circuit_breaker_active: bool,
portfolio_heat_pct: float,
max_heat: float,
) -> tuple[bool, str]:
"""Check all constraints for a micro-trade.
Evaluates trading window, circuit breakers, portfolio heat,
daily limit, and enabled state.
Args:
config: Current micro-trade configuration.
daily_count: Number of micro-trades already executed today.
is_within_window: Whether the current time is within the
trading window.
circuit_breaker_active: Whether any circuit breaker is active.
portfolio_heat_pct: Current portfolio heat as a fraction
(e.g. 0.15 for 15%).
max_heat: Maximum allowed portfolio heat fraction.
Returns:
Tuple of ``(allowed, reason)``. When ``allowed`` is ``False``,
``reason`` describes which constraint was violated.
"""
if not config.enabled:
return False, "micro_trading_disabled"
if circuit_breaker_active:
return False, "circuit_breaker_active"
if not is_within_window:
return False, "outside_trading_window"
if daily_count >= config.max_daily:
return False, "daily_limit_reached"
if max_heat > 0 and portfolio_heat_pct >= max_heat:
return False, "portfolio_heat_exceeded"
return True, "ok"
+253
View File
@@ -0,0 +1,253 @@
"""Core data models for the autonomous trading engine.
Defines dataclasses for risk tier configuration, portfolio state,
trading decisions, position sizing results, stop levels, and
performance metrics used across all trading engine components.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timedelta
# ---------------------------------------------------------------------------
# Risk Tier Configuration
# ---------------------------------------------------------------------------
@dataclass
class RiskTierConfig:
"""Parameters for a named risk tier (conservative/moderate/aggressive)."""
name: str
min_confidence: float
max_position_pct: float
stop_loss_atr_multiplier: float
reward_risk_ratio: float
max_sector_pct: float
max_portfolio_heat: float
RISK_TIER_DEFAULTS: dict[str, RiskTierConfig] = {
"conservative": RiskTierConfig(
name="conservative",
min_confidence=0.75,
max_position_pct=0.05,
stop_loss_atr_multiplier=1.5,
reward_risk_ratio=2.0,
max_sector_pct=0.20,
max_portfolio_heat=0.10,
),
"moderate": RiskTierConfig(
name="moderate",
min_confidence=0.55,
max_position_pct=0.10,
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.30,
max_portfolio_heat=0.20,
),
"aggressive": RiskTierConfig(
name="aggressive",
min_confidence=0.40,
max_position_pct=0.15,
stop_loss_atr_multiplier=2.5,
reward_risk_ratio=1.2,
max_sector_pct=0.40,
max_portfolio_heat=0.30,
),
}
# ---------------------------------------------------------------------------
# Portfolio State
# ---------------------------------------------------------------------------
@dataclass
class PortfolioState:
"""Snapshot of the current portfolio used for decision-making."""
positions: list = field(default_factory=list)
total_value: float = 0.0
cash: float = 0.0
active_pool: float = 0.0
reserve_pool: float = 0.0
sector_exposure: dict[str, float] = field(default_factory=dict)
portfolio_heat: float = 0.0
open_position_count: int = 0
# ---------------------------------------------------------------------------
# Trading Decision
# ---------------------------------------------------------------------------
@dataclass
class TradingDecision:
"""Record of a trading decision (act or skip), persisted for audit trail."""
id: str
recommendation_id: str | None
decision: str
skip_reason: str | None
ticker: str
computed_position_size: float | None
computed_share_quantity: int | None
risk_tier_at_decision: str
portfolio_heat_at_decision: float | None
active_pool_at_decision: float | None
reserve_pool_at_decision: float | None
circuit_breaker_status: str
correlation_check_result: dict = field(default_factory=dict)
sector_exposure_check_result: dict = field(default_factory=dict)
earnings_proximity_flag: bool = False
is_micro_trade: bool = False
decision_trace: dict = field(default_factory=dict)
created_at: datetime = field(default_factory=datetime.utcnow)
# ---------------------------------------------------------------------------
# Position Sizing
# ---------------------------------------------------------------------------
@dataclass
class PositionSizeResult:
"""Output of the position sizer computation."""
dollar_amount: float
share_quantity: int
allocation_pct: float
adjustments: list[str] = field(default_factory=list)
rejected: bool = False
rejection_reason: str = ""
# ---------------------------------------------------------------------------
# Stop-Loss / Take-Profit Levels
# ---------------------------------------------------------------------------
@dataclass
class StopLevels:
"""Current stop-loss and take-profit levels for an open position."""
stop_loss_price: float
take_profit_price: float
trailing_stop_active: bool
atr_value: float
atr_multiplier: float
reward_risk_ratio: float
last_updated: datetime = field(default_factory=datetime.utcnow)
# ---------------------------------------------------------------------------
# Open / Closed Positions
# ---------------------------------------------------------------------------
@dataclass
class OpenPosition:
"""Representation of a currently held position."""
ticker: str
quantity: int
entry_price: float
current_price: float
unrealized_pnl: float
market_value: float
sector: str
stop_loss_price: float
take_profit_price: float
signal_confidence: float
is_micro_trade: bool = False
@dataclass
class ClosedTrade:
"""Record of a completed (closed) trade."""
ticker: str
entry_price: float
exit_price: float
quantity: int
pnl: float
pnl_pct: float
hold_duration: timedelta
recommendation_id: str | None = None
is_micro_trade: bool = False
# ---------------------------------------------------------------------------
# Performance Metrics
# ---------------------------------------------------------------------------
@dataclass
class PerformanceMetrics:
"""Portfolio-wide performance metrics computed periodically."""
total_portfolio_value: float
active_pool: float
reserve_pool: float
unrealized_pnl: float
realized_pnl: float
daily_pnl: float
win_count: int
loss_count: int
win_rate: float
avg_win: float
avg_loss: float
profit_factor: float
sharpe_ratio: float
max_drawdown: float
current_drawdown_pct: float
portfolio_heat: float
computed_at: datetime = field(default_factory=datetime.utcnow)
# ---------------------------------------------------------------------------
# Circuit Breaker
# ---------------------------------------------------------------------------
@dataclass
class CircuitBreakerState:
"""Current state of the circuit breaker safety mechanism."""
active: bool = False
trigger_type: str | None = None
triggered_at: datetime | None = None
cooldown_expires: datetime | None = None
ticker_cooldowns: dict[str, datetime] = field(default_factory=dict)
# ---------------------------------------------------------------------------
# Reserve Pool
# ---------------------------------------------------------------------------
@dataclass
class ReservePoolState:
"""Current state of the reserve pool."""
balance: float = 0.0
total_deposits: float = 0.0
total_withdrawals: float = 0.0
last_updated: datetime = field(default_factory=datetime.utcnow)
# ---------------------------------------------------------------------------
# Stop Trigger
# ---------------------------------------------------------------------------
@dataclass
class StopTrigger:
"""A triggered stop-loss or take-profit event for a position."""
ticker: str
trigger_type: str # "stop_loss" or "take_profit"
current_price: float
trigger_price: float
+163
View File
@@ -0,0 +1,163 @@
"""Notification service for the autonomous trading engine.
Pure computation module for notification logic. Actual delivery (AWS SNS,
Gmail API) is deferred to integration code — this module handles formatting,
rate-limit decisions, and record creation.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from services.trading.models import PerformanceMetrics
# ---------------------------------------------------------------------------
# Supported event types
# ---------------------------------------------------------------------------
SUPPORTED_EVENT_TYPES: frozenset[str] = frozenset(
{
"circuit_breaker_triggered",
"circuit_breaker_resumed",
"risk_tier_changed",
"emergency_liquidation",
"large_trade_pnl",
"daily_summary",
"weekly_digest",
}
)
# ---------------------------------------------------------------------------
# Data model
# ---------------------------------------------------------------------------
@dataclass
class NotificationRecord:
"""A single notification record for audit persistence."""
channel: str
event_type: str
message: str
delivery_status: str = "pending"
retry_count: int = 0
created_at: datetime = field(default_factory=datetime.utcnow)
# ---------------------------------------------------------------------------
# Notification service (pure computation)
# ---------------------------------------------------------------------------
class NotificationService:
"""Pure-computation notification service.
Handles formatting, rate-limit checking, and record creation.
Actual delivery is the caller's responsibility.
"""
def __init__(
self,
sms_enabled: bool = False,
email_enabled: bool = False,
rate_limit_sms_per_hour: int = 10,
rate_limit_email_per_hour: int = 20,
) -> None:
self.sms_enabled = sms_enabled
self.email_enabled = email_enabled
self.rate_limit_sms_per_hour = rate_limit_sms_per_hour
self.rate_limit_email_per_hour = rate_limit_email_per_hour
# ------------------------------------------------------------------
# Rate limiting
# ------------------------------------------------------------------
def should_send(self, channel: str, current_hour_count: int) -> bool:
"""Check whether a notification on *channel* is within the rate limit.
Args:
channel: ``"sms"`` or ``"email"``.
current_hour_count: Number of notifications already sent on this
channel during the current hour window.
Returns:
``True`` if the notification may be sent, ``False`` if it should
be rate-limited.
"""
if channel == "sms":
if not self.sms_enabled:
return False
return current_hour_count < self.rate_limit_sms_per_hour
elif channel == "email":
if not self.email_enabled:
return False
return current_hour_count < self.rate_limit_email_per_hour
return False
# ------------------------------------------------------------------
# Formatting helpers
# ------------------------------------------------------------------
def format_daily_summary(self, metrics: PerformanceMetrics) -> str:
"""Format a daily performance summary message.
Args:
metrics: Current performance metrics snapshot.
Returns:
Human-readable summary string.
"""
total_trades = metrics.win_count + metrics.loss_count
return (
f"Daily Summary — "
f"P&L: ${metrics.daily_pnl:+.2f} | "
f"Portfolio: ${metrics.total_portfolio_value:,.2f} | "
f"Active: ${metrics.active_pool:,.2f} | "
f"Reserve: ${metrics.reserve_pool:,.2f} | "
f"Trades: {total_trades} | "
f"Win Rate: {metrics.win_rate:.0%} | "
f"Heat: {metrics.portfolio_heat:.2%}"
)
def format_alert(self, event_type: str, details: str) -> str:
"""Format an alert message for a specific event type.
Args:
event_type: One of the supported event types.
details: Free-form detail string.
Returns:
Formatted alert string.
"""
label = event_type.replace("_", " ").title()
return f"[Stonks Alert] {label}: {details}"
# ------------------------------------------------------------------
# Record creation
# ------------------------------------------------------------------
def create_notification(
self,
channel: str,
event_type: str,
message: str,
) -> NotificationRecord:
"""Create a ``NotificationRecord`` ready for persistence.
Args:
channel: ``"sms"`` or ``"email"``.
event_type: One of the supported event types.
message: The formatted message body.
Returns:
A new ``NotificationRecord`` with ``delivery_status="pending"``.
"""
return NotificationRecord(
channel=channel,
event_type=event_type,
message=message,
delivery_status="pending",
retry_count=0,
)
+197
View File
@@ -0,0 +1,197 @@
"""Performance tracker for the autonomous trading engine.
Pure computation module that computes portfolio-wide performance metrics
from closed trades and portfolio state data.
"""
from __future__ import annotations
import math
from services.trading.models import ClosedTrade, PerformanceMetrics
class PerformanceComputer:
"""Computes portfolio performance metrics from trade data.
All methods are pure computations with no side effects or I/O.
"""
def compute_metrics(
self,
closed_trades: list[ClosedTrade],
portfolio_value: float,
active_pool: float,
reserve_pool: float,
daily_pnl: float,
unrealized_pnl: float,
portfolio_heat: float,
daily_returns: list[float],
) -> PerformanceMetrics:
"""Compute all performance metrics from trade data and portfolio state.
Args:
closed_trades: List of completed trades.
portfolio_value: Current total portfolio value.
active_pool: Current active pool value.
reserve_pool: Current reserve pool balance.
daily_pnl: Today's P&L.
unrealized_pnl: Unrealized P&L across open positions.
portfolio_heat: Current portfolio heat value.
daily_returns: List of daily return percentages for Sharpe/drawdown.
Returns:
PerformanceMetrics with all computed fields.
"""
wins = [t for t in closed_trades if t.pnl > 0]
losses = [t for t in closed_trades if t.pnl <= 0]
win_count = len(wins)
loss_count = len(losses)
total_trades = len(closed_trades)
win_rate = win_count / total_trades if total_trades > 0 else 0.0
avg_win = (
sum(t.pnl for t in wins) / win_count if win_count > 0 else 0.0
)
avg_loss = (
sum(t.pnl for t in losses) / loss_count if loss_count > 0 else 0.0
)
gross_profits = sum(t.pnl for t in wins)
gross_losses = abs(sum(t.pnl for t in losses))
if gross_losses > 0:
profit_factor = gross_profits / gross_losses
else:
profit_factor = float("inf") if gross_profits > 0 else 0.0
realized_pnl = sum(t.pnl for t in closed_trades)
sharpe_ratio = self._compute_sharpe_ratio(daily_returns)
max_drawdown = self._compute_max_drawdown(daily_returns)
current_drawdown_pct = self._compute_current_drawdown(daily_returns)
return PerformanceMetrics(
total_portfolio_value=portfolio_value,
active_pool=active_pool,
reserve_pool=reserve_pool,
unrealized_pnl=unrealized_pnl,
realized_pnl=realized_pnl,
daily_pnl=daily_pnl,
win_count=win_count,
loss_count=loss_count,
win_rate=win_rate,
avg_win=avg_win,
avg_loss=avg_loss,
profit_factor=profit_factor,
sharpe_ratio=sharpe_ratio,
max_drawdown=max_drawdown,
current_drawdown_pct=current_drawdown_pct,
portfolio_heat=portfolio_heat,
)
def compute_trade_metrics(self, trade: ClosedTrade) -> dict:
"""Compute per-trade metrics for a single closed trade.
Args:
trade: A completed trade.
Returns:
Dictionary with per-trade metrics.
"""
return {
"ticker": trade.ticker,
"entry_price": trade.entry_price,
"exit_price": trade.exit_price,
"quantity": trade.quantity,
"pnl": trade.pnl,
"pnl_pct": trade.pnl_pct,
"hold_duration": str(trade.hold_duration),
"recommendation_id": trade.recommendation_id,
"is_micro_trade": trade.is_micro_trade,
"is_win": trade.pnl > 0,
}
def filter_by_micro_trade(
self,
trades: list[ClosedTrade],
is_micro: bool,
) -> list[ClosedTrade]:
"""Filter trades by micro-trade flag.
Args:
trades: List of closed trades.
is_micro: If True, return only micro-trades; if False, only standard.
Returns:
Filtered list of trades.
"""
return [t for t in trades if t.is_micro_trade == is_micro]
@staticmethod
def _compute_sharpe_ratio(daily_returns: list[float]) -> float:
"""Compute annualized Sharpe ratio from daily returns.
Formula: (mean_daily_return / std_daily_return) * sqrt(252)
Returns 0.0 if fewer than 2 data points or std is 0.
"""
if len(daily_returns) < 2:
return 0.0
n = len(daily_returns)
mean_return = sum(daily_returns) / n
variance = sum((r - mean_return) ** 2 for r in daily_returns) / (n - 1)
std_return = math.sqrt(variance)
if std_return < 1e-12:
return 0.0
return (mean_return / std_return) * math.sqrt(252)
@staticmethod
def _compute_max_drawdown(daily_returns: list[float]) -> float:
"""Compute maximum drawdown from daily returns.
Tracks cumulative returns and finds the largest peak-to-trough decline.
Returns 0.0 if no drawdown or insufficient data.
"""
if not daily_returns:
return 0.0
cumulative = 1.0
peak = 1.0
max_dd = 0.0
for r in daily_returns:
cumulative *= (1.0 + r)
if cumulative > peak:
peak = cumulative
drawdown = (peak - cumulative) / peak if peak > 0 else 0.0
if drawdown > max_dd:
max_dd = drawdown
return max_dd
@staticmethod
def _compute_current_drawdown(daily_returns: list[float]) -> float:
"""Compute current drawdown percentage from daily returns.
Returns the drawdown from the most recent peak to the current value.
"""
if not daily_returns:
return 0.0
cumulative = 1.0
peak = 1.0
for r in daily_returns:
cumulative *= (1.0 + r)
if cumulative > peak:
peak = cumulative
if peak <= 0:
return 0.0
return (peak - cumulative) / peak
+347
View File
@@ -0,0 +1,347 @@
"""Position sizing engine for the autonomous trading system.
Computes dollar allocation and share quantity for a trade by applying
a sequential adjustment pipeline: confidence gate, correlation reduction,
sector exposure, diversification bonus, earnings proximity, portfolio
heat check, active-pool minimum, absolute cap, and share rounding.
"""
from __future__ import annotations
import math
from datetime import datetime
from services.trading.models import (
OpenPosition,
PortfolioState,
PositionSizeResult,
RiskTierConfig,
)
class PositionSizer:
"""Compute position size through a multi-step adjustment pipeline."""
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def compute(
self,
confidence: float,
ticker: str,
sector: str,
current_price: float,
active_pool: float,
risk_tier: RiskTierConfig,
portfolio_state: PortfolioState,
correlation_matrix: dict[tuple[str, str], float],
earnings_calendar: dict[str, datetime],
absolute_position_cap: float = 50.0,
active_pool_minimum: float = 100.0,
) -> PositionSizeResult:
"""Run the full adjustment pipeline and return a sizing result."""
adjustments: list[str] = []
# ---- 1. Active pool minimum check (early reject) -------------
if active_pool < active_pool_minimum:
return self._rejected(
f"Active pool ${active_pool:.2f} below minimum ${active_pool_minimum:.2f}",
adjustments,
)
# ---- 2. Confidence gate --------------------------------------
if confidence < risk_tier.min_confidence:
return self._rejected(
f"Confidence {confidence:.4f} below tier minimum {risk_tier.min_confidence}",
adjustments,
)
# ---- 3. Base sizing formula ----------------------------------
base_allocation_pct = risk_tier.max_position_pct * 0.5
multiplier = 1.0 # default multiplier
raw_pct = (
base_allocation_pct
* (confidence / risk_tier.min_confidence)
* multiplier
)
clamped_pct = min(raw_pct, risk_tier.max_position_pct)
dollar_amount = active_pool * clamped_pct
dollar_amount = min(dollar_amount, absolute_position_cap)
adjustments.append(
f"Base sizing: raw_pct={raw_pct:.6f}, clamped_pct={clamped_pct:.6f}, "
f"dollar=${dollar_amount:.2f}"
)
# ---- 4. Correlation reduction --------------------------------
dollar_amount, clamped_pct = self._apply_correlation_reduction(
ticker,
dollar_amount,
clamped_pct,
portfolio_state,
correlation_matrix,
adjustments,
)
if dollar_amount == 0.0:
return self._rejected(adjustments[-1], adjustments)
# ---- 5. Sector exposure reduction ----------------------------
dollar_amount, clamped_pct = self._apply_sector_exposure_reduction(
sector,
dollar_amount,
clamped_pct,
active_pool,
risk_tier,
portfolio_state,
adjustments,
)
# ---- 6. Diversification bonus --------------------------------
dollar_amount, clamped_pct = self._apply_diversification_bonus(
sector,
dollar_amount,
clamped_pct,
risk_tier,
portfolio_state,
adjustments,
)
# ---- 7. Earnings proximity -----------------------------------
result = self._apply_earnings_proximity(
ticker,
dollar_amount,
clamped_pct,
earnings_calendar,
adjustments,
)
if isinstance(result, PositionSizeResult):
return result
dollar_amount, clamped_pct = result
# ---- 8. Absolute cap enforcement (re-apply after adjustments) -
if dollar_amount > absolute_position_cap:
dollar_amount = absolute_position_cap
clamped_pct = dollar_amount / active_pool if active_pool > 0 else 0.0
adjustments.append(
f"Absolute cap enforced: capped to ${absolute_position_cap:.2f}"
)
# ---- 9. Portfolio heat check ---------------------------------
stop_loss_distance_pct = risk_tier.stop_loss_atr_multiplier * 0.02
new_position_heat = dollar_amount * stop_loss_distance_pct
max_heat_dollars = risk_tier.max_portfolio_heat * active_pool
current_heat = portfolio_state.portfolio_heat
if current_heat + new_position_heat > max_heat_dollars:
return self._rejected(
f"Portfolio heat would exceed limit: current={current_heat:.2f} + "
f"new={new_position_heat:.2f} > max={max_heat_dollars:.2f}",
adjustments,
)
# ---- 10. Share rounding --------------------------------------
if current_price <= 0:
return self._rejected("Invalid current price", adjustments)
share_quantity = math.floor(dollar_amount / current_price)
if share_quantity == 0:
return self._rejected(
f"Zero shares after rounding: ${dollar_amount:.2f} / ${current_price:.2f}",
adjustments,
)
# Final dollar amount based on whole shares
final_dollar = share_quantity * current_price
final_pct = final_dollar / active_pool if active_pool > 0 else 0.0
adjustments.append(
f"Final: {share_quantity} shares @ ${current_price:.2f} = ${final_dollar:.2f} "
f"({final_pct:.4%} of active pool)"
)
return PositionSizeResult(
dollar_amount=final_dollar,
share_quantity=share_quantity,
allocation_pct=final_pct,
adjustments=adjustments,
rejected=False,
rejection_reason="",
)
# ------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------
@staticmethod
def _rejected(reason: str, adjustments: list[str]) -> PositionSizeResult:
return PositionSizeResult(
dollar_amount=0.0,
share_quantity=0,
allocation_pct=0.0,
adjustments=adjustments,
rejected=True,
rejection_reason=reason,
)
@staticmethod
def _apply_correlation_reduction(
ticker: str,
dollar_amount: float,
allocation_pct: float,
portfolio_state: PortfolioState,
correlation_matrix: dict[tuple[str, str], float],
adjustments: list[str],
) -> tuple[float, float]:
"""Reduce or reject based on weighted average correlation."""
positions: list[OpenPosition] = portfolio_state.positions
if not positions:
return dollar_amount, allocation_pct
total_weight = 0.0
weighted_corr = 0.0
for pos in positions:
corr = correlation_matrix.get(
(ticker, pos.ticker),
correlation_matrix.get((pos.ticker, ticker), 0.0),
)
weight = pos.market_value
weighted_corr += corr * weight
total_weight += weight
if total_weight == 0.0:
return dollar_amount, allocation_pct
avg_corr = weighted_corr / total_weight
if avg_corr > 0.8:
adjustments.append(
f"Correlation rejection: avg={avg_corr:.4f} > 0.8"
)
return 0.0, 0.0
if avg_corr > 0.5:
# Reduce proportionally: scale factor goes from 1.0 at 0.5 to 0.0 at 0.8
reduction = (avg_corr - 0.5) / (0.8 - 0.5)
factor = 1.0 - reduction
new_dollar = dollar_amount * factor
new_pct = allocation_pct * factor
adjustments.append(
f"Correlation reduction: avg={avg_corr:.4f}, factor={factor:.4f}, "
f"${dollar_amount:.2f} -> ${new_dollar:.2f}"
)
return new_dollar, new_pct
return dollar_amount, allocation_pct
@staticmethod
def _apply_sector_exposure_reduction(
sector: str,
dollar_amount: float,
allocation_pct: float,
active_pool: float,
risk_tier: RiskTierConfig,
portfolio_state: PortfolioState,
adjustments: list[str],
) -> tuple[float, float]:
"""Reduce allocation if sector would exceed max_sector_pct."""
max_sector_dollars = risk_tier.max_sector_pct * active_pool
current_sector_exposure = portfolio_state.sector_exposure.get(sector, 0.0)
if current_sector_exposure + dollar_amount > max_sector_dollars:
available = max(max_sector_dollars - current_sector_exposure, 0.0)
if available <= 0:
adjustments.append(
f"Sector exposure at limit: {sector} "
f"${current_sector_exposure:.2f} >= max ${max_sector_dollars:.2f}"
)
return 0.0, 0.0
new_pct = available / active_pool if active_pool > 0 else 0.0
adjustments.append(
f"Sector exposure reduction: {sector} "
f"${current_sector_exposure:.2f} + ${dollar_amount:.2f} > "
f"max ${max_sector_dollars:.2f}, reduced to ${available:.2f}"
)
return available, new_pct
return dollar_amount, allocation_pct
@staticmethod
def _apply_diversification_bonus(
sector: str,
dollar_amount: float,
allocation_pct: float,
risk_tier: RiskTierConfig,
portfolio_state: PortfolioState,
adjustments: list[str],
) -> tuple[float, float]:
"""Apply 1.2x bonus for under-represented sectors when < 3 sectors held."""
existing_sectors = set(portfolio_state.sector_exposure.keys())
if len(existing_sectors) < 3 and sector not in existing_sectors:
bonus = 1.2
new_dollar = dollar_amount * bonus
new_pct = allocation_pct * bonus
# Re-clamp to max_position_pct after bonus
max_dollar = risk_tier.max_position_pct * (
portfolio_state.active_pool
if portfolio_state.active_pool > 0
else 1.0
)
if new_dollar > max_dollar:
new_dollar = max_dollar
new_pct = risk_tier.max_position_pct
adjustments.append(
f"Diversification bonus: 1.2x applied for new sector '{sector}' "
f"(portfolio has {len(existing_sectors)} sectors), "
f"${dollar_amount:.2f} -> ${new_dollar:.2f}"
)
return new_dollar, new_pct
return dollar_amount, allocation_pct
@staticmethod
def _apply_earnings_proximity(
ticker: str,
dollar_amount: float,
allocation_pct: float,
earnings_calendar: dict[str, datetime],
adjustments: list[str],
) -> tuple[float, float] | PositionSizeResult:
"""Reduce by 50% within 3 trading days; reject within 1 trading day."""
if ticker not in earnings_calendar:
return dollar_amount, allocation_pct
earnings_dt = earnings_calendar[ticker]
now = datetime.utcnow()
delta = earnings_dt - now
# Use total_seconds for precise fractional-day comparison
trading_days_until = delta.total_seconds() / 86400.0
if trading_days_until < 0:
# Earnings already passed
return dollar_amount, allocation_pct
if trading_days_until <= 1:
adjustments.append(
f"Earnings rejection: {ticker} earnings in {trading_days_until:.1f} day(s)"
)
return PositionSizeResult(
dollar_amount=0.0,
share_quantity=0,
allocation_pct=0.0,
adjustments=adjustments,
rejected=True,
rejection_reason=f"Earnings within 1 trading day for {ticker}",
)
if trading_days_until <= 3:
new_dollar = dollar_amount * 0.5
new_pct = allocation_pct * 0.5
adjustments.append(
f"Earnings proximity: {ticker} earnings in {trading_days_until:.1f} days, "
f"50% reduction: ${dollar_amount:.2f} -> ${new_dollar:.2f}"
)
return new_dollar, new_pct
return dollar_amount, allocation_pct
+174
View File
@@ -0,0 +1,174 @@
"""Portfolio rebalancer for the autonomous trading engine.
Evaluates portfolio concentration and generates rebalancing sell orders
when single-stock or sector exposure exceeds configured limits.
"""
from __future__ import annotations
from dataclasses import dataclass
from services.trading.models import OpenPosition, RiskTierConfig
@dataclass
class RebalanceOrder:
"""A sell order generated by the portfolio rebalancer."""
ticker: str
action: str = "sell"
quantity: int = 0
reason: str = ""
tag: str = "rebalance"
class PortfolioRebalancer:
"""Evaluates portfolio concentration and generates rebalancing orders.
Generates partial sell orders when:
- A single stock exceeds max_position_pct of the active pool
- A sector exceeds max_sector_pct of the active pool
- The number of open positions exceeds the configured maximum
"""
def evaluate(
self,
positions: list[OpenPosition],
risk_tier: RiskTierConfig,
active_pool: float,
max_positions: int = 10,
) -> list[RebalanceOrder]:
"""Evaluate portfolio and generate rebalancing sell orders.
Args:
positions: Current open positions.
risk_tier: Active risk tier configuration.
active_pool: Current active pool value in dollars.
max_positions: Maximum allowed open positions.
Returns:
List of RebalanceOrder for positions that need trimming.
"""
orders: list[RebalanceOrder] = []
if not positions or active_pool <= 0:
return orders
# Track which tickers already have orders to avoid duplicates
ordered_tickers: dict[str, RebalanceOrder] = {}
# --- 1. Single-stock concentration check ---
max_position_dollars = risk_tier.max_position_pct * active_pool
for pos in positions:
if pos.market_value > max_position_dollars and pos.current_price > 0:
excess = pos.market_value - max_position_dollars
sell_qty = int(excess / pos.current_price)
if sell_qty > 0:
sell_qty = min(sell_qty, pos.quantity)
order = RebalanceOrder(
ticker=pos.ticker,
action="sell",
quantity=sell_qty,
reason=(
f"Position {pos.ticker} market_value "
f"${pos.market_value:.2f} exceeds "
f"max_position_pct limit "
f"${max_position_dollars:.2f}"
),
tag="rebalance",
)
ordered_tickers[pos.ticker] = order
orders.append(order)
# --- 2. Sector concentration check ---
max_sector_dollars = risk_tier.max_sector_pct * active_pool
# Group positions by sector
sector_positions: dict[str, list[OpenPosition]] = {}
for pos in positions:
sector_positions.setdefault(pos.sector, []).append(pos)
for sector, sector_pos in sector_positions.items():
sector_value = sum(p.market_value for p in sector_pos)
if sector_value > max_sector_dollars:
excess = sector_value - max_sector_dollars
# Sort by confidence ascending — sell lowest confidence first
sorted_pos = sorted(sector_pos, key=lambda p: p.signal_confidence)
remaining_excess = excess
for pos in sorted_pos:
if remaining_excess <= 0:
break
if pos.current_price <= 0:
continue
# Determine how many shares to sell from this position
sell_value = min(remaining_excess, pos.market_value)
sell_qty = int(sell_value / pos.current_price)
if sell_qty <= 0:
continue
sell_qty = min(sell_qty, pos.quantity)
if pos.ticker in ordered_tickers:
# Already have an order for this ticker — take the larger
existing = ordered_tickers[pos.ticker]
if sell_qty > existing.quantity:
existing.quantity = sell_qty
existing.reason += (
f"; also sector {sector} exposure "
f"${sector_value:.2f} exceeds limit "
f"${max_sector_dollars:.2f}"
)
else:
order = RebalanceOrder(
ticker=pos.ticker,
action="sell",
quantity=sell_qty,
reason=(
f"Sector {sector} exposure "
f"${sector_value:.2f} exceeds "
f"max_sector_pct limit "
f"${max_sector_dollars:.2f}"
f"selling lowest-confidence position"
),
tag="rebalance",
)
ordered_tickers[pos.ticker] = order
orders.append(order)
remaining_excess -= sell_qty * pos.current_price
# --- 3. Maximum open positions enforcement ---
if len(positions) > max_positions:
excess_count = len(positions) - max_positions
# Sort by confidence ascending — sell lowest confidence first
sorted_all = sorted(positions, key=lambda p: p.signal_confidence)
sold_count = 0
for pos in sorted_all:
if sold_count >= excess_count:
break
if pos.ticker in ordered_tickers:
# Already selling this ticker — count it toward excess
existing = ordered_tickers[pos.ticker]
if existing.quantity < pos.quantity:
existing.quantity = pos.quantity
existing.reason += "; also exceeds max open positions"
sold_count += 1
else:
order = RebalanceOrder(
ticker=pos.ticker,
action="sell",
quantity=pos.quantity,
reason=(
f"Portfolio has {len(positions)} positions, "
f"exceeding max of {max_positions}"
f"selling lowest-confidence position"
),
tag="rebalance",
)
ordered_tickers[pos.ticker] = order
orders.append(order)
sold_count += 1
return orders
+112
View File
@@ -0,0 +1,112 @@
"""Reserve Pool Controller — pure computation module.
Manages the untouchable cash reserve that grows from realized profits.
All methods are pure computations; persistence is handled by the caller.
"""
from __future__ import annotations
from services.trading.models import ReservePoolState # noqa: F401 — re-export for convenience
class ReservePoolController:
"""Compute reserve-pool operations without touching the database.
Parameters
----------
siphon_pct:
Fraction of realized profit transferred to the reserve on each
profitable position close (default 20 %).
high_water_pct:
When the reserve exceeds this fraction of total portfolio value
the risk-tier controller should consider upgrading (default 30 %).
"""
def __init__(
self,
siphon_pct: float = 0.20,
high_water_pct: float = 0.30,
) -> None:
self.siphon_pct = siphon_pct
self.high_water_pct = high_water_pct
# ------------------------------------------------------------------
# Profit siphoning
# ------------------------------------------------------------------
def siphon_profit(
self,
realized_profit: float,
current_balance: float,
) -> tuple[float, float]:
"""Compute the amount to transfer into the reserve pool.
Only positive profits are siphoned.
Returns
-------
(transfer_amount, new_balance)
*transfer_amount* is ``realized_profit * siphon_pct`` when
the profit is positive, otherwise ``0.0``.
*new_balance* is ``current_balance + transfer_amount``.
"""
if realized_profit <= 0:
return 0.0, current_balance
transfer = realized_profit * self.siphon_pct
return transfer, current_balance + transfer
# ------------------------------------------------------------------
# Emergency liquidation
# ------------------------------------------------------------------
def emergency_liquidate(self, current_balance: float) -> float:
"""Return the full reserve balance to be released into the active pool.
The caller is responsible for zeroing the persisted balance and
recording the ledger entry.
Returns
-------
float
The amount to release (equal to *current_balance*).
"""
return current_balance
# ------------------------------------------------------------------
# Active pool computation
# ------------------------------------------------------------------
def compute_active_pool(
self,
total_portfolio_value: float,
reserve_balance: float,
) -> float:
"""Active Pool = total portfolio value reserve balance."""
return total_portfolio_value - reserve_balance
# ------------------------------------------------------------------
# High-water mark detection
# ------------------------------------------------------------------
def is_high_water(
self,
reserve_balance: float,
total_portfolio_value: float,
) -> bool:
"""Return ``True`` when the reserve exceeds *high_water_pct* of total portfolio."""
if total_portfolio_value <= 0:
return False
return reserve_balance > self.high_water_pct * total_portfolio_value
# ------------------------------------------------------------------
# Emergency liquidation trigger check
# ------------------------------------------------------------------
def should_emergency_liquidate(
self,
current_drawdown_pct: float,
emergency_threshold_pct: float,
) -> bool:
"""Return ``True`` when drawdown exceeds the emergency threshold."""
return current_drawdown_pct > emergency_threshold_pct
+87
View File
@@ -0,0 +1,87 @@
"""Risk tier auto-adjustment controller for the autonomous trading engine.
Pure computation module — no DB access. Persistence of tier changes is
handled by the caller. All methods operate on values passed in as
arguments and return deterministic results.
Tier ordering: conservative → moderate → aggressive
"""
from __future__ import annotations
from services.trading.models import PerformanceMetrics
# Ordered from lowest to highest risk.
TIER_ORDER: list[str] = ["conservative", "moderate", "aggressive"]
class RiskTierController:
"""Evaluates performance metrics and determines whether the active
risk tier should change.
Downgrade conditions (any one triggers a downgrade by one level):
- Trailing 30-day win rate < 40%
- Current drawdown > 15%
Upgrade conditions (ALL must be true):
- Trailing 30-day win rate > 55%
- Reserve pool > 20% of total portfolio
- Current drawdown < 5%
"""
def __init__(self) -> None:
# No configuration needed — uses TIER_ORDER for ordering.
pass
def evaluate(
self,
current_tier: str,
metrics: PerformanceMetrics,
reserve_pct: float,
) -> str | None:
"""Evaluate whether the tier should change based on performance.
Parameters
----------
current_tier:
The currently active tier name (e.g. ``"moderate"``).
metrics:
Latest portfolio performance metrics.
reserve_pct:
Reserve pool balance as a fraction of total portfolio value
(e.g. 0.25 means 25%).
Returns
-------
str | None
The new tier name if a change is needed, or ``None`` if the
current tier should remain.
"""
current_index = TIER_ORDER.index(current_tier)
# --- Downgrade check (any condition triggers) ---
should_downgrade = (
metrics.win_rate < 0.40 or metrics.current_drawdown_pct > 0.15
)
if should_downgrade:
if current_index > 0:
return TIER_ORDER[current_index - 1]
# Already at the lowest tier — no change.
return None
# --- Upgrade check (all conditions must be true) ---
should_upgrade = (
metrics.win_rate > 0.55
and reserve_pct > 0.20
and metrics.current_drawdown_pct < 0.05
)
if should_upgrade:
if current_index < len(TIER_ORDER) - 1:
return TIER_ORDER[current_index + 1]
# Already at the highest tier — no change.
return None
# Neither condition met — stay at current tier.
return None
+256
View File
@@ -0,0 +1,256 @@
"""Stop-loss and take-profit management for the autonomous trading engine.
Computes initial stop/take-profit levels from ATR and risk tier parameters,
re-evaluates levels when volatility or market conditions change, detects
price crossings that should trigger exits, and tightens stops under
high-heat or high-severity-event conditions.
All public methods are synchronous (pure computation, no DB access).
Persistence is handled by the caller (engine.py).
"""
from __future__ import annotations
from datetime import datetime
from services.trading.models import (
OpenPosition,
RiskTierConfig,
StopLevels,
StopTrigger,
)
class StopLossManager:
"""Compute and maintain dynamic stop-loss / take-profit levels."""
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def compute_initial_levels(
self,
entry_price: float,
atr: float,
risk_tier: RiskTierConfig,
is_micro_trade: bool = False,
) -> StopLevels:
"""Compute initial stop-loss and take-profit for a new position.
For standard trades the risk tier's ATR multiplier and reward/risk
ratio are used. Micro-trades use a tighter 1.0x ATR multiplier
and 1.5x stop distance for the take-profit target.
"""
if is_micro_trade:
atr_multiplier = 1.0
reward_risk_ratio = 1.5
else:
atr_multiplier = risk_tier.stop_loss_atr_multiplier
reward_risk_ratio = risk_tier.reward_risk_ratio
stop_distance = atr * atr_multiplier
stop_loss_price = entry_price - stop_distance
if is_micro_trade:
take_profit_price = entry_price + (stop_distance * reward_risk_ratio)
else:
take_profit_price = entry_price + (stop_distance * reward_risk_ratio)
return StopLevels(
stop_loss_price=stop_loss_price,
take_profit_price=take_profit_price,
trailing_stop_active=False,
atr_value=atr,
atr_multiplier=atr_multiplier,
reward_risk_ratio=reward_risk_ratio,
last_updated=datetime.utcnow(),
)
def re_evaluate_levels(
self,
position: OpenPosition,
current_price: float,
atr: float,
risk_tier: RiskTierConfig,
last_levels: StopLevels,
high_severity_event: bool = False,
earnings_within_3_days: bool = False,
portfolio_heat_pct: float = 0.0,
max_portfolio_heat: float = 0.20,
) -> StopLevels | None:
"""Re-evaluate stop/take-profit levels for an open position.
Returns updated ``StopLevels`` when a material change is needed,
or ``None`` when the existing levels are still appropriate.
Material change triggers:
* ATR shift > 10 %
* Trailing stop activation (price moved > 50 % of TP distance)
* Earnings proximity tightening (0.7x ATR multiplier)
* High-severity macro event tightening (0.5x normal multiplier)
* Proactive heat tightening (heat > 80 % of max)
"""
entry_price = position.entry_price
# --- Determine effective ATR multiplier -----------------------
base_multiplier = risk_tier.stop_loss_atr_multiplier
if high_severity_event:
effective_multiplier = base_multiplier * 0.5
elif earnings_within_3_days:
effective_multiplier = base_multiplier * 0.7
else:
effective_multiplier = base_multiplier
# --- Trailing stop check --------------------------------------
trailing_stop_active = last_levels.trailing_stop_active
tp_distance = last_levels.take_profit_price - entry_price
favorable_move = current_price - entry_price
if tp_distance > 0 and favorable_move > 0.5 * tp_distance:
trailing_stop_active = True
# --- Compute candidate stop-loss ------------------------------
candidate_stop = entry_price - (atr * effective_multiplier)
# If trailing stop is active, floor the stop at entry (breakeven)
if trailing_stop_active:
candidate_stop = max(candidate_stop, entry_price)
# --- Proactive heat tightening --------------------------------
# When portfolio heat exceeds 80% of max, tighten further
if max_portfolio_heat > 0 and portfolio_heat_pct > 0.8 * max_portfolio_heat:
heat_tightening_factor = 0.7
tightened_stop = entry_price - (
atr * effective_multiplier * heat_tightening_factor
)
if trailing_stop_active:
tightened_stop = max(tightened_stop, entry_price)
candidate_stop = max(candidate_stop, tightened_stop)
# --- Decide whether the change is material --------------------
atr_change_pct = (
abs(atr - last_levels.atr_value) / last_levels.atr_value
if last_levels.atr_value > 0
else 0.0
)
trailing_changed = trailing_stop_active != last_levels.trailing_stop_active
multiplier_changed = effective_multiplier != last_levels.atr_multiplier
if not trailing_changed and not multiplier_changed and atr_change_pct < 0.10:
return None # no material change
# --- Compute new take-profit ----------------------------------
stop_distance = atr * effective_multiplier
reward_risk_ratio = risk_tier.reward_risk_ratio
candidate_tp = entry_price + (stop_distance * reward_risk_ratio)
return StopLevels(
stop_loss_price=candidate_stop,
take_profit_price=candidate_tp,
trailing_stop_active=trailing_stop_active,
atr_value=atr,
atr_multiplier=effective_multiplier,
reward_risk_ratio=reward_risk_ratio,
last_updated=datetime.utcnow(),
)
def check_price_crossings(
self,
positions: list[OpenPosition],
prices: dict[str, float],
stop_levels: dict[str, StopLevels],
) -> list[StopTrigger]:
"""Return triggers for positions whose price has crossed a level.
A ``StopTrigger`` is emitted when:
* current price <= stop_loss_price → ``"stop_loss"``
* current price >= take_profit_price → ``"take_profit"``
"""
triggers: list[StopTrigger] = []
for pos in positions:
current_price = prices.get(pos.ticker)
if current_price is None:
continue
levels = stop_levels.get(pos.ticker)
if levels is None:
continue
if current_price <= levels.stop_loss_price:
triggers.append(
StopTrigger(
ticker=pos.ticker,
trigger_type="stop_loss",
current_price=current_price,
trigger_price=levels.stop_loss_price,
)
)
elif current_price >= levels.take_profit_price:
triggers.append(
StopTrigger(
ticker=pos.ticker,
trigger_type="take_profit",
current_price=current_price,
trigger_price=levels.take_profit_price,
)
)
return triggers
def tighten_for_heat(
self,
positions: list[OpenPosition],
stop_levels: dict[str, StopLevels],
portfolio_heat: float,
max_heat: float,
active_pool: float,
) -> dict[str, StopLevels]:
"""Tighten stops on lowest-confidence positions when heat is high.
When ``portfolio_heat > 0.8 * max_heat``, the lowest-confidence
positions get their stops tightened first (moved closer to current
price) to reduce overall portfolio heat.
Returns a *new* dict containing only the tickers whose levels
were actually changed.
"""
if max_heat <= 0 or portfolio_heat <= 0.8 * max_heat:
return {}
# Sort positions by confidence ascending (lowest first)
sorted_positions = sorted(positions, key=lambda p: p.signal_confidence)
updated: dict[str, StopLevels] = {}
for pos in sorted_positions:
levels = stop_levels.get(pos.ticker)
if levels is None:
continue
# Tighten by reducing the stop distance by 30 %
heat_factor = 0.7
new_stop_distance = levels.atr_value * levels.atr_multiplier * heat_factor
new_stop = pos.entry_price - new_stop_distance
# Never move stop further away from current price
new_stop = max(new_stop, levels.stop_loss_price)
# If trailing stop is active, floor at entry
if levels.trailing_stop_active:
new_stop = max(new_stop, pos.entry_price)
if new_stop != levels.stop_loss_price:
updated[pos.ticker] = StopLevels(
stop_loss_price=new_stop,
take_profit_price=levels.take_profit_price,
trailing_stop_active=levels.trailing_stop_active,
atr_value=levels.atr_value,
atr_multiplier=levels.atr_multiplier,
reward_risk_ratio=levels.reward_risk_ratio,
last_updated=datetime.utcnow(),
)
return updated
+132
View File
@@ -0,0 +1,132 @@
"""Tax lot tracking for cost basis and wash sale detection.
Feature: autonomous-trading-engine
Pure computation module for FIFO tax lot closing and wash sale
detection within the 30-day window. Used by the Trading Engine
for tax-loss harvesting awareness.
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import date, timedelta
@dataclass
class TaxLot:
"""A single tax lot representing a purchase of shares."""
ticker: str
quantity: int
cost_basis_per_share: float
acquisition_date: date
status: str = "open" # open | closed | washed
@dataclass
class ClosedLot:
"""Result of closing a tax lot via FIFO."""
ticker: str
quantity: int
cost_basis_per_share: float
exit_price: float
realized_pnl: float
acquisition_date: date
closed_date: date
class TaxLotTracker:
"""Pure computation for FIFO lot closing and wash sale detection."""
def close_lots_fifo(
self,
lots: list[TaxLot],
quantity: int,
exit_price: float,
exit_date: date,
) -> list[ClosedLot]:
"""Close lots in FIFO order (earliest acquired first).
Processes open lots sorted by acquisition_date ascending,
closing shares until the requested quantity is fulfilled.
Returns a list of ClosedLot records with realized P&L.
Parameters
----------
lots:
All tax lots for the ticker (open ones will be selected).
quantity:
Number of shares to close.
exit_price:
Price per share at exit.
exit_date:
Date the lots are being closed.
Returns
-------
list[ClosedLot]
Closed lot records in FIFO order.
"""
open_lots = sorted(
[lot for lot in lots if lot.status == "open"],
key=lambda lot: lot.acquisition_date,
)
closed: list[ClosedLot] = []
remaining = quantity
for lot in open_lots:
if remaining <= 0:
break
close_qty = min(lot.quantity, remaining)
realized_pnl = (exit_price - lot.cost_basis_per_share) * close_qty
closed.append(
ClosedLot(
ticker=lot.ticker,
quantity=close_qty,
cost_basis_per_share=lot.cost_basis_per_share,
exit_price=exit_price,
realized_pnl=realized_pnl,
acquisition_date=lot.acquisition_date,
closed_date=exit_date,
)
)
remaining -= close_qty
return closed
def check_wash_sale(
self,
loss_date: date,
purchases: list[TaxLot],
) -> bool:
"""Check whether any purchase falls within the 30-day wash sale window.
A wash sale occurs when the same ticker is purchased within
30 days before or after a loss-closing date.
Parameters
----------
loss_date:
The date the loss was realized.
purchases:
Tax lots representing purchases of the same ticker.
Returns
-------
bool
True if any purchase is within the 30-day window.
"""
window_start = loss_date - timedelta(days=30)
window_end = loss_date + timedelta(days=30)
for lot in purchases:
if window_start <= lot.acquisition_date <= window_end:
return True
return False
+82
View File
@@ -0,0 +1,82 @@
"""Trading window utilities for the autonomous trading engine.
Pure computation module that determines whether a given timestamp falls
within the allowed trading window (9:45 AM 3:45 PM ET on weekdays),
whether the US market is open, and when the next trading window opens.
Uses ``zoneinfo.ZoneInfo("America/New_York")`` for Eastern Time handling.
Does not check market holidays (simplified).
"""
from __future__ import annotations
from datetime import datetime, time, timedelta
from zoneinfo import ZoneInfo
# US Eastern timezone
ET = ZoneInfo("America/New_York")
# Trading window boundaries (excludes first/last 15 min of market hours)
WINDOW_OPEN = time(9, 45)
WINDOW_CLOSE = time(15, 45)
# Full US market hours
MARKET_OPEN = time(9, 30)
MARKET_CLOSE = time(16, 0)
# Weekday range: Monday=0 … Friday=4
_WEEKDAYS = range(0, 5)
def is_within_trading_window(dt: datetime) -> bool:
"""Return True if *dt* is between 9:45 AM ET and 3:45 PM ET on a weekday.
The timestamp is first converted to US/Eastern time. Weekends are
always outside the window. Market holidays are **not** checked
(simplified implementation).
"""
et_dt = dt.astimezone(ET)
if et_dt.weekday() not in _WEEKDAYS:
return False
t = et_dt.time()
return WINDOW_OPEN <= t < WINDOW_CLOSE
def next_window_open(dt: datetime) -> datetime:
"""Return the next datetime when the trading window opens (9:45 AM ET).
If *dt* is before 9:45 AM ET on a weekday the same day's open is
returned. Otherwise the next weekday's 9:45 AM ET is returned.
"""
et_dt = dt.astimezone(ET)
today_open = et_dt.replace(
hour=WINDOW_OPEN.hour,
minute=WINDOW_OPEN.minute,
second=0,
microsecond=0,
)
# If we haven't reached today's open yet and it's a weekday, return today
if et_dt < today_open and et_dt.weekday() in _WEEKDAYS:
return today_open
# Otherwise advance to the next weekday
candidate = et_dt + timedelta(days=1)
candidate = candidate.replace(
hour=WINDOW_OPEN.hour,
minute=WINDOW_OPEN.minute,
second=0,
microsecond=0,
)
while candidate.weekday() not in _WEEKDAYS:
candidate += timedelta(days=1)
return candidate
def is_market_open(dt: datetime) -> bool:
"""Return True if *dt* is during US market hours (9:30 AM 4:00 PM ET) on a weekday."""
et_dt = dt.astimezone(ET)
if et_dt.weekday() not in _WEEKDAYS:
return False
t = et_dt.time()
return MARKET_OPEN <= t < MARKET_CLOSE
+728
View File
@@ -0,0 +1,728 @@
"""Integration tests for the TradingEngine wiring and end-to-end decision flow.
Tests verify that the engine correctly delegates to sub-components and
that the full recommendation → evaluation → sizing → decision pipeline
works end-to-end with concrete values. No DB/Redis needed — all
components are pure computation.
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
import pytest
from services.shared.config import TradingConfig
from services.trading.circuit_breaker import CircuitBreaker
from services.trading.correlation import CorrelationMatrix
from services.trading.engine import TradingEngine
from services.trading.models import (
CircuitBreakerState,
OpenPosition,
PerformanceMetrics,
PortfolioState,
RiskTierConfig,
StopLevels,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_engine(**overrides) -> TradingEngine:
"""Create a TradingEngine with sensible test defaults."""
config_kwargs = {
"enabled": True,
"micro_trading_enabled": False,
}
config_kwargs.update(overrides)
config = TradingConfig(**config_kwargs)
return TradingEngine(pool=None, redis=None, config=config)
def _moderate_tier() -> RiskTierConfig:
return RiskTierConfig(
name="moderate",
min_confidence=0.55,
max_position_pct=0.10,
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.30,
max_portfolio_heat=0.20,
)
def _portfolio_state(**overrides) -> PortfolioState:
defaults = {
"positions": [],
"total_value": 500.0,
"cash": 400.0,
"active_pool": 400.0,
"reserve_pool": 100.0,
"sector_exposure": {},
"portfolio_heat": 0.0,
"open_position_count": 0,
}
defaults.update(overrides)
return PortfolioState(**defaults)
def _inactive_cb() -> CircuitBreakerState:
return CircuitBreakerState(active=False)
def _within_trading_window() -> datetime:
"""Return a datetime that is within the trading window (Wed 11:00 AM ET)."""
from zoneinfo import ZoneInfo
return datetime(2025, 1, 15, 11, 0, 0, tzinfo=ZoneInfo("America/New_York"))
# ---------------------------------------------------------------------------
# Test 1: Full cycle — recommendation → evaluation → position sizing → act
# ---------------------------------------------------------------------------
class TestFullDecisionCycle:
"""Verify the complete recommendation-to-decision pipeline."""
def test_high_confidence_recommendation_produces_act_decision(self):
engine = _make_engine(absolute_position_cap=100.0)
tier = _moderate_tier()
portfolio = _portfolio_state(active_pool=1000.0, total_value=1200.0)
cb_state = _inactive_cb()
corr = CorrelationMatrix()
now = _within_trading_window()
rec = {
"recommendation_id": "rec-001",
"ticker": "AAPL",
"confidence": 0.80,
"sector": "Technology",
"current_price": 25.0, # cheap enough to buy at least 1 share
"action": "buy",
}
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr,
earnings_calendar={},
now=now,
)
assert decision.decision == "act"
assert decision.ticker == "AAPL"
assert decision.computed_position_size is not None
assert decision.computed_position_size > 0
assert decision.computed_share_quantity is not None
assert decision.computed_share_quantity > 0
assert decision.risk_tier_at_decision == "moderate"
assert decision.circuit_breaker_status == "inactive"
def test_low_confidence_recommendation_produces_skip_decision(self):
engine = _make_engine()
tier = _moderate_tier()
portfolio = _portfolio_state()
cb_state = _inactive_cb()
corr = CorrelationMatrix()
now = _within_trading_window()
rec = {
"recommendation_id": "rec-002",
"ticker": "MSFT",
"confidence": 0.30, # below moderate min_confidence of 0.55
"sector": "Technology",
"current_price": 400.0,
"action": "buy",
}
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr,
earnings_calendar={},
now=now,
)
assert decision.decision == "skip"
assert "insufficient_confidence" in decision.skip_reason
def test_duplicate_recommendation_is_skipped(self):
engine = _make_engine(absolute_position_cap=100.0)
tier = _moderate_tier()
portfolio = _portfolio_state(active_pool=1000.0, total_value=1200.0)
cb_state = _inactive_cb()
corr = CorrelationMatrix()
now = _within_trading_window()
rec = {
"recommendation_id": "rec-dup",
"ticker": "GOOG",
"confidence": 0.80,
"sector": "Technology",
"current_price": 25.0,
"action": "buy",
}
# First evaluation should act
d1 = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr,
earnings_calendar={},
now=now,
)
assert d1.decision == "act"
# Second evaluation of same rec should skip as duplicate
d2 = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr,
earnings_calendar={},
now=now,
)
assert d2.decision == "skip"
assert "duplicate" in d2.skip_reason
# ---------------------------------------------------------------------------
# Test 2: Stop-loss crossing → StopTrigger list
# ---------------------------------------------------------------------------
class TestStopLossCrossings:
"""Verify stop-loss crossing detection via engine wiring."""
def test_stop_loss_triggered(self):
engine = _make_engine()
positions = [
OpenPosition(
ticker="AAPL",
quantity=10,
entry_price=150.0,
current_price=140.0,
unrealized_pnl=-100.0,
market_value=1400.0,
sector="Technology",
stop_loss_price=145.0,
take_profit_price=165.0,
signal_confidence=0.75,
),
]
prices = {"AAPL": 144.0} # below stop_loss_price of 145.0
stop_levels = {
"AAPL": StopLevels(
stop_loss_price=145.0,
take_profit_price=165.0,
trailing_stop_active=False,
atr_value=3.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
),
}
triggers = engine.check_stop_loss_crossings(positions, prices, stop_levels)
assert len(triggers) == 1
assert triggers[0].ticker == "AAPL"
assert triggers[0].trigger_type == "stop_loss"
assert triggers[0].current_price == 144.0
def test_take_profit_triggered(self):
engine = _make_engine()
positions = [
OpenPosition(
ticker="MSFT",
quantity=5,
entry_price=400.0,
current_price=420.0,
unrealized_pnl=100.0,
market_value=2100.0,
sector="Technology",
stop_loss_price=390.0,
take_profit_price=415.0,
signal_confidence=0.80,
),
]
prices = {"MSFT": 416.0} # above take_profit_price of 415.0
stop_levels = {
"MSFT": StopLevels(
stop_loss_price=390.0,
take_profit_price=415.0,
trailing_stop_active=False,
atr_value=5.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
),
}
triggers = engine.check_stop_loss_crossings(positions, prices, stop_levels)
assert len(triggers) == 1
assert triggers[0].ticker == "MSFT"
assert triggers[0].trigger_type == "take_profit"
def test_no_crossing_returns_empty(self):
engine = _make_engine()
positions = [
OpenPosition(
ticker="GOOG",
quantity=3,
entry_price=170.0,
current_price=172.0,
unrealized_pnl=6.0,
market_value=516.0,
sector="Technology",
stop_loss_price=165.0,
take_profit_price=180.0,
signal_confidence=0.70,
),
]
prices = {"GOOG": 172.0} # between stop and take-profit
stop_levels = {
"GOOG": StopLevels(
stop_loss_price=165.0,
take_profit_price=180.0,
trailing_stop_active=False,
atr_value=3.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
),
}
triggers = engine.check_stop_loss_crossings(positions, prices, stop_levels)
assert len(triggers) == 0
# ---------------------------------------------------------------------------
# Test 3: Reserve pool siphoning on profitable close
# ---------------------------------------------------------------------------
class TestReservePoolSiphoning:
"""Verify reserve pool siphoning via engine wiring."""
def test_profitable_close_siphons_20_percent(self):
engine = _make_engine(reserve_siphon_pct=0.20)
transfer, new_balance = engine.handle_position_close(
realized_profit=100.0,
reserve_balance=50.0,
)
assert transfer == pytest.approx(20.0) # 20% of $100
assert new_balance == pytest.approx(70.0) # $50 + $20
def test_loss_does_not_siphon(self):
engine = _make_engine(reserve_siphon_pct=0.20)
transfer, new_balance = engine.handle_position_close(
realized_profit=-30.0,
reserve_balance=50.0,
)
assert transfer == 0.0
assert new_balance == 50.0
def test_zero_profit_does_not_siphon(self):
engine = _make_engine(reserve_siphon_pct=0.20)
transfer, new_balance = engine.handle_position_close(
realized_profit=0.0,
reserve_balance=50.0,
)
assert transfer == 0.0
assert new_balance == 50.0
# ---------------------------------------------------------------------------
# Test 4: Circuit breaker trigger → halt → cooldown → resume
# ---------------------------------------------------------------------------
class TestCircuitBreakerFlow:
"""Verify circuit breaker integration with the decision loop."""
def test_active_circuit_breaker_skips_recommendation(self):
engine = _make_engine()
tier = _moderate_tier()
portfolio = _portfolio_state()
now = _within_trading_window()
# Circuit breaker active with future cooldown
cb_state = CircuitBreakerState(
active=True,
trigger_type="daily_loss",
triggered_at=now - timedelta(minutes=30),
cooldown_expires=now + timedelta(hours=2),
)
rec = {
"recommendation_id": "rec-cb-1",
"ticker": "AAPL",
"confidence": 0.90,
"sector": "Technology",
"current_price": 150.0,
"action": "buy",
}
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=CorrelationMatrix(),
earnings_calendar={},
now=now,
)
assert decision.decision == "skip"
assert "circuit_breaker_active" in decision.skip_reason
assert decision.circuit_breaker_status == "active"
def test_expired_circuit_breaker_allows_trading(self):
engine = _make_engine(absolute_position_cap=100.0)
tier = _moderate_tier()
portfolio = _portfolio_state(active_pool=1000.0, total_value=1200.0)
now = _within_trading_window()
# Circuit breaker was active but cooldown has expired
cb_state = CircuitBreakerState(
active=True,
trigger_type="daily_loss",
triggered_at=now - timedelta(hours=3),
cooldown_expires=now - timedelta(hours=1), # expired
)
rec = {
"recommendation_id": "rec-cb-2",
"ticker": "AAPL",
"confidence": 0.80,
"sector": "Technology",
"current_price": 25.0,
"action": "buy",
}
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=tier,
circuit_breaker_state=cb_state,
correlation_matrix=CorrelationMatrix(),
earnings_calendar={},
now=now,
)
# Should proceed since cooldown expired
assert decision.decision == "act"
def test_circuit_breaker_daily_loss_detection(self):
"""Verify the CircuitBreaker component detects daily loss threshold."""
cb = CircuitBreaker(daily_loss_pct=0.05)
# 6% loss on $500 portfolio → should trigger
assert cb.check_daily_loss(daily_pnl=-30.0, portfolio_value=500.0) is True
# 3% loss → should not trigger
assert cb.check_daily_loss(daily_pnl=-15.0, portfolio_value=500.0) is False
def test_circuit_breaker_cooldown_computation(self):
"""Verify cooldown expiry is computed correctly."""
cb = CircuitBreaker(volatility_pause_hours=2, ticker_cooldown_hours=48)
now = datetime(2025, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
vol_expiry = cb.compute_cooldown_expiry("volatility", now)
assert vol_expiry == now + timedelta(hours=2)
pos_expiry = cb.compute_cooldown_expiry("single_position", now)
assert pos_expiry == now + timedelta(hours=48)
# ---------------------------------------------------------------------------
# Test 5: Engine startup state
# ---------------------------------------------------------------------------
class TestEngineStartup:
"""Verify engine startup state and lifecycle."""
def test_engine_starts_not_running(self):
engine = _make_engine()
assert engine.running is False
@pytest.mark.asyncio
async def test_start_sets_running_flag(self):
engine = _make_engine()
await engine.start()
assert engine.running is True
@pytest.mark.asyncio
async def test_stop_clears_running_flag(self):
engine = _make_engine()
await engine.start()
assert engine.running is True
await engine.stop()
assert engine.running is False
def test_engine_initializes_all_sub_components(self):
engine = _make_engine()
assert engine.position_sizer is not None
assert engine.stop_loss_manager is not None
assert engine.circuit_breaker is not None
assert engine.reserve_pool_controller is not None
assert engine.risk_tier_controller is not None
assert engine.correlation_matrix is not None
assert engine.notification_service is not None
assert engine.micro_trading_module is not None
assert engine.rebalancer is not None
def test_engine_starts_with_empty_processed_ids(self):
engine = _make_engine()
assert len(engine.processed_recommendation_ids) == 0
# ---------------------------------------------------------------------------
# Test 6: Risk tier evaluation wiring
# ---------------------------------------------------------------------------
class TestRiskTierEvaluation:
"""Verify risk tier evaluation via engine wiring."""
def test_downgrade_on_poor_performance(self):
engine = _make_engine()
metrics = PerformanceMetrics(
total_portfolio_value=500.0,
active_pool=400.0,
reserve_pool=100.0,
unrealized_pnl=-20.0,
realized_pnl=-50.0,
daily_pnl=-10.0,
win_count=3,
loss_count=7,
win_rate=0.30, # below 40% → downgrade
avg_win=10.0,
avg_loss=-8.0,
profit_factor=0.5,
sharpe_ratio=-0.5,
max_drawdown=0.20,
current_drawdown_pct=0.18, # above 15% → also triggers downgrade
portfolio_heat=0.10,
)
new_tier = engine.evaluate_risk_tier("moderate", metrics, reserve_pct=0.20)
assert new_tier == "conservative"
def test_upgrade_on_strong_performance(self):
engine = _make_engine()
metrics = PerformanceMetrics(
total_portfolio_value=600.0,
active_pool=400.0,
reserve_pool=200.0,
unrealized_pnl=30.0,
realized_pnl=100.0,
daily_pnl=5.0,
win_count=7,
loss_count=3,
win_rate=0.70, # above 55%
avg_win=15.0,
avg_loss=-5.0,
profit_factor=2.1,
sharpe_ratio=1.5,
max_drawdown=0.05,
current_drawdown_pct=0.02, # below 5%
portfolio_heat=0.08,
)
# reserve_pct > 0.20 and win_rate > 0.55 and drawdown < 0.05
new_tier = engine.evaluate_risk_tier("moderate", metrics, reserve_pct=0.33)
assert new_tier == "aggressive"
def test_no_change_when_metrics_are_neutral(self):
engine = _make_engine()
metrics = PerformanceMetrics(
total_portfolio_value=500.0,
active_pool=400.0,
reserve_pool=100.0,
unrealized_pnl=5.0,
realized_pnl=20.0,
daily_pnl=2.0,
win_count=5,
loss_count=5,
win_rate=0.50, # between 40% and 55%
avg_win=10.0,
avg_loss=-8.0,
profit_factor=1.2,
sharpe_ratio=0.5,
max_drawdown=0.08,
current_drawdown_pct=0.06,
portfolio_heat=0.10,
)
new_tier = engine.evaluate_risk_tier("moderate", metrics, reserve_pct=0.20)
assert new_tier is None
# ---------------------------------------------------------------------------
# Test 7: Rebalancing wiring
# ---------------------------------------------------------------------------
class TestRebalancingWiring:
"""Verify portfolio rebalancing via engine wiring."""
def test_over_concentrated_position_generates_sell_order(self):
engine = _make_engine()
tier = _moderate_tier() # max_position_pct = 0.10
# market_value $200 exceeds 10% of $400 = $40 by $160
# sell_qty = int(160 / 20) = 8 shares
positions = [
OpenPosition(
ticker="AAPL",
quantity=10,
entry_price=18.0,
current_price=20.0,
unrealized_pnl=20.0,
market_value=200.0,
sector="Technology",
stop_loss_price=16.0,
take_profit_price=25.0,
signal_confidence=0.80,
),
]
orders = engine.evaluate_rebalancing(positions, tier, active_pool=400.0)
assert len(orders) >= 1
assert orders[0].ticker == "AAPL"
assert orders[0].action == "sell"
def test_balanced_portfolio_generates_no_orders(self):
engine = _make_engine()
tier = _moderate_tier()
positions = [
OpenPosition(
ticker="AAPL",
quantity=1,
entry_price=30.0,
current_price=30.0,
unrealized_pnl=0.0,
market_value=30.0, # $30 < 10% of $400 = $40
sector="Technology",
stop_loss_price=28.0,
take_profit_price=35.0,
signal_confidence=0.80,
),
]
orders = engine.evaluate_rebalancing(positions, tier, active_pool=400.0)
assert len(orders) == 0
# ---------------------------------------------------------------------------
# Test 8: Notification wiring
# ---------------------------------------------------------------------------
class TestNotificationWiring:
"""Verify notification creation via engine wiring."""
def test_create_alert_returns_notification_record(self):
engine = _make_engine()
record = engine.create_alert(
event_type="circuit_breaker_triggered",
details="Daily loss exceeded 5% threshold",
)
assert record.event_type == "circuit_breaker_triggered"
assert record.channel == "email"
assert "Circuit Breaker Triggered" in record.message
assert "Daily loss exceeded 5% threshold" in record.message
assert record.delivery_status == "pending"
def test_create_alert_for_risk_tier_change(self):
engine = _make_engine()
record = engine.create_alert(
event_type="risk_tier_changed",
details="moderate → conservative due to drawdown",
)
assert record.event_type == "risk_tier_changed"
assert "Risk Tier Changed" in record.message
# ---------------------------------------------------------------------------
# Test 9: Micro-trading constraint wiring
# ---------------------------------------------------------------------------
class TestMicroTradingConstraints:
"""Verify micro-trading constraint checking via engine wiring."""
def test_disabled_micro_trading_rejects(self):
engine = _make_engine(micro_trading_enabled=False)
allowed, reason = engine.check_micro_trade_constraints(
daily_count=0,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "micro_trading_disabled"
def test_enabled_micro_trading_allows(self):
engine = _make_engine(micro_trading_enabled=True)
allowed, reason = engine.check_micro_trade_constraints(
daily_count=0,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is True
assert reason == "ok"
def test_circuit_breaker_blocks_micro_trade(self):
engine = _make_engine(micro_trading_enabled=True)
allowed, reason = engine.check_micro_trade_constraints(
daily_count=0,
is_within_window=True,
circuit_breaker_active=True,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "circuit_breaker_active"
def test_daily_limit_blocks_micro_trade(self):
engine = _make_engine(
micro_trading_enabled=True,
micro_trading_max_daily=10,
)
allowed, reason = engine.check_micro_trade_constraints(
daily_count=10,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "daily_limit_reached"
+227
View File
@@ -0,0 +1,227 @@
"""Property-based tests for the Backtester.
Feature: autonomous-trading-engine
Tests Property 36 (backtester produces equivalent metrics) from the
design specification. Generates random sets of historical trades and
verifies that the backtester's metric computation matches the
PerformanceComputer for the same trade data.
"""
from __future__ import annotations
from datetime import date, timedelta
from hypothesis import given, settings
from hypothesis import strategies as st
from services.trading.backtester import BacktestConfig, BacktestEngine
from services.trading.models import ClosedTrade
from services.trading.performance_tracker import PerformanceComputer
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
_tickers = st.sampled_from(["AAPL", "MSFT", "GOOG", "AMZN", "TSLA", "NVDA"])
_entry_price_st = st.floats(
min_value=1.0, max_value=5000.0, allow_nan=False, allow_infinity=False
)
_exit_price_st = st.floats(
min_value=1.0, max_value=5000.0, allow_nan=False, allow_infinity=False
)
_quantity_st = st.integers(min_value=1, max_value=1000)
_hold_duration_st = st.timedeltas(
min_value=timedelta(minutes=1), max_value=timedelta(days=365)
)
@st.composite
def closed_trade_st(draw: st.DrawFn) -> ClosedTrade:
"""Generate a random ClosedTrade with consistent pnl fields."""
ticker = draw(_tickers)
entry_price = draw(_entry_price_st)
exit_price = draw(_exit_price_st)
quantity = draw(_quantity_st)
hold_duration = draw(_hold_duration_st)
pnl = (exit_price - entry_price) * quantity
pnl_pct = (exit_price - entry_price) / entry_price if entry_price > 0 else 0.0
return ClosedTrade(
ticker=ticker,
entry_price=entry_price,
exit_price=exit_price,
quantity=quantity,
pnl=pnl,
pnl_pct=pnl_pct,
hold_duration=hold_duration,
)
_trades_st = st.lists(closed_trade_st(), min_size=0, max_size=50)
_daily_returns_st = st.lists(
st.floats(min_value=-0.20, max_value=0.20, allow_nan=False, allow_infinity=False),
min_size=0,
max_size=252,
)
_initial_capital_st = st.floats(
min_value=100.0, max_value=100000.0, allow_nan=False, allow_infinity=False
)
_risk_tier_st = st.sampled_from(["conservative", "moderate", "aggressive"])
# ---------------------------------------------------------------------------
# Property 36: Backtester produces equivalent metrics
# **Validates: Requirements 15.3**
# ---------------------------------------------------------------------------
class TestProperty36BacktesterEquivalentMetrics:
"""Property 36: Backtester produces equivalent metrics.
**Validates: Requirements 15.3**
For any set of closed trades and daily returns, the backtester's
win_rate, profit_factor, and trade_count must be identical to
those computed directly by PerformanceComputer.
"""
@settings(max_examples=100)
@given(
trades=_trades_st,
daily_returns=_daily_returns_st,
initial_capital=_initial_capital_st,
risk_tier=_risk_tier_st,
)
def test_win_rate_matches_performance_tracker(
self,
trades: list[ClosedTrade],
daily_returns: list[float],
initial_capital: float,
risk_tier: str,
) -> None:
"""win_rate from BacktestEngine matches PerformanceComputer."""
config = BacktestConfig(
start_date=date(2024, 1, 1),
end_date=date(2024, 12, 31),
initial_capital=initial_capital,
risk_tier=risk_tier,
)
engine = BacktestEngine()
result = engine.compute_result(config, trades, daily_returns, [])
perf = PerformanceComputer()
metrics = perf.compute_metrics(
closed_trades=trades,
portfolio_value=initial_capital,
active_pool=initial_capital,
reserve_pool=0.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert result.win_rate == metrics.win_rate, (
f"win_rate mismatch: backtest={result.win_rate}, "
f"perf_tracker={metrics.win_rate}"
)
@settings(max_examples=100)
@given(
trades=_trades_st,
daily_returns=_daily_returns_st,
initial_capital=_initial_capital_st,
risk_tier=_risk_tier_st,
)
def test_profit_factor_matches_performance_tracker(
self,
trades: list[ClosedTrade],
daily_returns: list[float],
initial_capital: float,
risk_tier: str,
) -> None:
"""profit_factor from BacktestEngine matches PerformanceComputer."""
config = BacktestConfig(
start_date=date(2024, 1, 1),
end_date=date(2024, 12, 31),
initial_capital=initial_capital,
risk_tier=risk_tier,
)
engine = BacktestEngine()
result = engine.compute_result(config, trades, daily_returns, [])
perf = PerformanceComputer()
metrics = perf.compute_metrics(
closed_trades=trades,
portfolio_value=initial_capital,
active_pool=initial_capital,
reserve_pool=0.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert result.profit_factor == metrics.profit_factor, (
f"profit_factor mismatch: backtest={result.profit_factor}, "
f"perf_tracker={metrics.profit_factor}"
)
@settings(max_examples=100)
@given(
trades=_trades_st,
daily_returns=_daily_returns_st,
initial_capital=_initial_capital_st,
risk_tier=_risk_tier_st,
)
def test_trade_count_matches_number_of_trades(
self,
trades: list[ClosedTrade],
daily_returns: list[float],
initial_capital: float,
risk_tier: str,
) -> None:
"""trade_count from BacktestEngine equals len(trades)."""
config = BacktestConfig(
start_date=date(2024, 1, 1),
end_date=date(2024, 12, 31),
initial_capital=initial_capital,
risk_tier=risk_tier,
)
engine = BacktestEngine()
result = engine.compute_result(config, trades, daily_returns, [])
# trade_count should equal the number of trades passed in,
# which is also win_count + loss_count from PerformanceComputer
perf = PerformanceComputer()
metrics = perf.compute_metrics(
closed_trades=trades,
portfolio_value=initial_capital,
active_pool=initial_capital,
reserve_pool=0.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert result.trade_count == len(trades), (
f"trade_count mismatch: backtest={result.trade_count}, "
f"expected={len(trades)}"
)
assert result.trade_count == metrics.win_count + metrics.loss_count, (
f"trade_count={result.trade_count} != "
f"win_count({metrics.win_count}) + loss_count({metrics.loss_count})"
)
+422
View File
@@ -0,0 +1,422 @@
"""Property-based tests for the Circuit Breaker.
Feature: autonomous-trading-engine
Tests properties 13 and 14 from the design specification,
covering circuit breaker activation triggers (daily loss, single position,
volatility) and cooldown expiry behaviour.
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.circuit_breaker import CircuitBreaker
from services.trading.models import CircuitBreakerState
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _circuit_breaker_strategy() -> st.SearchStrategy[CircuitBreaker]:
"""Generate CircuitBreaker instances with random but valid thresholds."""
return st.builds(
CircuitBreaker,
daily_loss_pct=st.floats(min_value=0.01, max_value=0.20, allow_nan=False, allow_infinity=False),
single_position_loss_pct=st.floats(min_value=0.05, max_value=0.50, allow_nan=False, allow_infinity=False),
ticker_cooldown_hours=st.integers(min_value=1, max_value=168),
volatility_pause_hours=st.integers(min_value=1, max_value=24),
stop_loss_hits_threshold=st.integers(min_value=2, max_value=10),
stop_loss_window_minutes=st.integers(min_value=5, max_value=120),
)
def _aware_datetime_strategy(
min_dt: datetime | None = None,
max_dt: datetime | None = None,
) -> st.SearchStrategy[datetime]:
"""Generate timezone-aware UTC datetimes."""
_min = min_dt or datetime(2024, 1, 1, tzinfo=timezone.utc)
_max = max_dt or datetime(2025, 12, 31, tzinfo=timezone.utc)
return st.datetimes(min_value=_min.replace(tzinfo=None), max_value=_max.replace(tzinfo=None)).map(
lambda dt: dt.replace(tzinfo=timezone.utc)
)
# ---------------------------------------------------------------------------
# Property 13: Circuit breaker activation
# **Validates: Requirements 6.1, 6.2, 6.3**
# ---------------------------------------------------------------------------
class TestProperty13CircuitBreakerActivation:
"""Property 13: Circuit breaker activation.
**Validates: Requirements 6.1, 6.2, 6.3**
"""
# -- Daily loss trigger ------------------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
portfolio_value=st.floats(min_value=100.0, max_value=100000.0, allow_nan=False, allow_infinity=False),
excess_pct=st.floats(min_value=0.001, max_value=0.50, allow_nan=False, allow_infinity=False),
)
def test_daily_loss_triggers_when_loss_exceeds_threshold(
self, cb: CircuitBreaker, portfolio_value: float, excess_pct: float,
) -> None:
"""Daily loss circuit breaker triggers when abs(daily_pnl)/portfolio_value > daily_loss_pct."""
# Construct a daily_pnl that exceeds the threshold
loss_ratio = cb.daily_loss_pct + excess_pct
daily_pnl = -(loss_ratio * portfolio_value)
result = cb.check_daily_loss(daily_pnl=daily_pnl, portfolio_value=portfolio_value)
assert result is True, (
f"Expected daily_loss trigger: loss_ratio={loss_ratio:.4f} > threshold={cb.daily_loss_pct:.4f}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
portfolio_value=st.floats(min_value=100.0, max_value=100000.0, allow_nan=False, allow_infinity=False),
below_fraction=st.floats(min_value=0.0, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_daily_loss_does_not_trigger_below_threshold(
self, cb: CircuitBreaker, portfolio_value: float, below_fraction: float,
) -> None:
"""Daily loss circuit breaker does NOT trigger when loss ratio <= threshold."""
# Loss ratio strictly below threshold
loss_ratio = cb.daily_loss_pct * below_fraction
daily_pnl = -(loss_ratio * portfolio_value)
result = cb.check_daily_loss(daily_pnl=daily_pnl, portfolio_value=portfolio_value)
assert result is False, (
f"Should not trigger: loss_ratio={loss_ratio:.4f} <= threshold={cb.daily_loss_pct:.4f}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
portfolio_value=st.floats(min_value=100.0, max_value=100000.0, allow_nan=False, allow_infinity=False),
profit=st.floats(min_value=0.01, max_value=10000.0, allow_nan=False, allow_infinity=False),
)
def test_daily_loss_does_not_trigger_on_positive_pnl(
self, cb: CircuitBreaker, portfolio_value: float, profit: float,
) -> None:
"""Daily loss circuit breaker never triggers on positive P&L."""
result = cb.check_daily_loss(daily_pnl=profit, portfolio_value=portfolio_value)
assert result is False, "Should not trigger on positive daily P&L"
# -- Single position trigger -------------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
excess=st.floats(min_value=0.001, max_value=0.50, allow_nan=False, allow_infinity=False),
)
def test_single_position_triggers_when_loss_exceeds_threshold(
self, cb: CircuitBreaker, excess: float,
) -> None:
"""Single position circuit breaker triggers when position_loss_pct > threshold."""
position_loss_pct = cb.single_position_loss_pct + excess
result = cb.check_single_position(position_loss_pct=position_loss_pct)
assert result is True, (
f"Expected single_position trigger: loss={position_loss_pct:.4f} > threshold={cb.single_position_loss_pct:.4f}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
below_fraction=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_single_position_does_not_trigger_at_or_below_threshold(
self, cb: CircuitBreaker, below_fraction: float,
) -> None:
"""Single position circuit breaker does NOT trigger when loss <= threshold."""
position_loss_pct = cb.single_position_loss_pct * below_fraction
result = cb.check_single_position(position_loss_pct=position_loss_pct)
assert result is False, (
f"Should not trigger: loss={position_loss_pct:.4f} <= threshold={cb.single_position_loss_pct:.4f}"
)
# -- Volatility trigger ------------------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
base_time=_aware_datetime_strategy(),
)
def test_volatility_triggers_when_enough_stops_within_window(
self, cb: CircuitBreaker, base_time: datetime,
) -> None:
"""Volatility circuit breaker triggers when >= threshold stop-losses fire within the window."""
# Generate exactly threshold hits within the window
window_minutes = cb.stop_loss_window_minutes
n_hits = cb.stop_loss_hits_threshold
# Space hits evenly within the window
interval = timedelta(minutes=window_minutes / n_hits)
stop_loss_hits = [base_time + interval * i for i in range(n_hits)]
result = cb.check_volatility(stop_loss_hits=stop_loss_hits)
assert result is True, (
f"Expected volatility trigger: {n_hits} hits within {window_minutes}min window"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
base_time=_aware_datetime_strategy(),
)
def test_volatility_does_not_trigger_when_too_few_stops(
self, cb: CircuitBreaker, base_time: datetime,
) -> None:
"""Volatility circuit breaker does NOT trigger with fewer than threshold hits."""
n_hits = cb.stop_loss_hits_threshold - 1
assume(n_hits >= 0)
# Even if all within the window, not enough hits
stop_loss_hits = [base_time + timedelta(minutes=i) for i in range(n_hits)]
result = cb.check_volatility(stop_loss_hits=stop_loss_hits)
assert result is False, (
f"Should not trigger: only {n_hits} hits < threshold {cb.stop_loss_hits_threshold}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
base_time=_aware_datetime_strategy(),
)
def test_volatility_does_not_trigger_when_stops_spread_outside_window(
self, cb: CircuitBreaker, base_time: datetime,
) -> None:
"""Volatility circuit breaker does NOT trigger when hits are spread beyond the window."""
n_hits = cb.stop_loss_hits_threshold
window_minutes = cb.stop_loss_window_minutes
# Space hits so that no contiguous sub-sequence of threshold size fits in the window
# Each hit is window_minutes + 1 apart, so any threshold-sized group spans
# (threshold - 1) * (window_minutes + 1) minutes > window_minutes
gap = timedelta(minutes=window_minutes + 1)
stop_loss_hits = [base_time + gap * i for i in range(n_hits)]
result = cb.check_volatility(stop_loss_hits=stop_loss_hits)
assert result is False, (
f"Should not trigger: hits spread {window_minutes + 1}min apart, "
f"window is {window_minutes}min"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
)
def test_volatility_does_not_trigger_on_empty_hits(
self, cb: CircuitBreaker,
) -> None:
"""Volatility circuit breaker does NOT trigger with no stop-loss hits."""
result = cb.check_volatility(stop_loss_hits=[])
assert result is False, "Should not trigger on empty stop-loss hits list"
# ---------------------------------------------------------------------------
# Property 14: Circuit breaker cooldown expiry
# **Validates: Requirements 6.5**
# ---------------------------------------------------------------------------
class TestProperty14CircuitBreakerCooldownExpiry:
"""Property 14: Circuit breaker cooldown expiry.
**Validates: Requirements 6.5**
"""
# -- is_active cooldown expiry -----------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
extra_seconds=st.integers(min_value=1, max_value=86400),
)
def test_is_active_returns_false_after_cooldown_expires(
self, cb: CircuitBreaker, triggered_at: datetime, extra_seconds: int,
) -> None:
"""is_active returns False when current time > cooldown_expires."""
cooldown_expires = triggered_at + timedelta(hours=cb.volatility_pause_hours)
now = cooldown_expires + timedelta(seconds=extra_seconds)
state = CircuitBreakerState(
active=True,
trigger_type="daily_loss",
triggered_at=triggered_at,
cooldown_expires=cooldown_expires,
)
result = cb.is_active(state=state, now=now)
assert result is False, (
f"Expected inactive: now={now} > cooldown_expires={cooldown_expires}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
before_fraction=st.floats(min_value=0.0, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_is_active_returns_true_before_cooldown_expires(
self, cb: CircuitBreaker, triggered_at: datetime, before_fraction: float,
) -> None:
"""is_active returns True when current time < cooldown_expires."""
cooldown_duration = timedelta(hours=cb.volatility_pause_hours)
cooldown_expires = triggered_at + cooldown_duration
# now is some fraction of the way through the cooldown (before expiry)
now = triggered_at + cooldown_duration * before_fraction
assume(now < cooldown_expires)
state = CircuitBreakerState(
active=True,
trigger_type="volatility",
triggered_at=triggered_at,
cooldown_expires=cooldown_expires,
)
result = cb.is_active(state=state, now=now)
assert result is True, (
f"Expected active: now={now} < cooldown_expires={cooldown_expires}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
)
def test_is_active_returns_false_when_state_not_active(
self, cb: CircuitBreaker, triggered_at: datetime,
) -> None:
"""is_active returns False when state.active is False regardless of time."""
state = CircuitBreakerState(
active=False,
trigger_type=None,
triggered_at=triggered_at,
cooldown_expires=triggered_at + timedelta(hours=24),
)
result = cb.is_active(state=state, now=triggered_at)
assert result is False, "Expected inactive when state.active is False"
# -- is_ticker_cooled_down expiry --------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
extra_seconds=st.integers(min_value=1, max_value=86400),
)
def test_ticker_cooldown_returns_false_after_expiry(
self, cb: CircuitBreaker, triggered_at: datetime, extra_seconds: int,
) -> None:
"""is_ticker_cooled_down returns False when cooldown has expired."""
cooldown_expires = triggered_at + timedelta(hours=cb.ticker_cooldown_hours)
now = cooldown_expires + timedelta(seconds=extra_seconds)
ticker_cooldowns = {"AAPL": cooldown_expires}
result = cb.is_ticker_cooled_down(
ticker="AAPL",
ticker_cooldowns=ticker_cooldowns,
now=now,
)
assert result is False, (
f"Expected ticker not cooled down: now={now} > expiry={cooldown_expires}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
before_fraction=st.floats(min_value=0.0, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_ticker_cooldown_returns_true_during_cooldown(
self, cb: CircuitBreaker, triggered_at: datetime, before_fraction: float,
) -> None:
"""is_ticker_cooled_down returns True when still within cooldown period."""
cooldown_duration = timedelta(hours=cb.ticker_cooldown_hours)
cooldown_expires = triggered_at + cooldown_duration
now = triggered_at + cooldown_duration * before_fraction
assume(now < cooldown_expires)
ticker_cooldowns = {"TSLA": cooldown_expires}
result = cb.is_ticker_cooled_down(
ticker="TSLA",
ticker_cooldowns=ticker_cooldowns,
now=now,
)
assert result is True, (
f"Expected ticker cooled down: now={now} < expiry={cooldown_expires}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
now=_aware_datetime_strategy(),
)
def test_ticker_cooldown_returns_false_for_unknown_ticker(
self, cb: CircuitBreaker, now: datetime,
) -> None:
"""is_ticker_cooled_down returns False for a ticker with no cooldown entry."""
result = cb.is_ticker_cooled_down(
ticker="UNKNOWN",
ticker_cooldowns={},
now=now,
)
assert result is False, "Expected no cooldown for unknown ticker"
# -- compute_cooldown_expiry -------------------------------------------
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
)
def test_cooldown_expiry_single_position_uses_ticker_cooldown_hours(
self, cb: CircuitBreaker, triggered_at: datetime,
) -> None:
"""compute_cooldown_expiry for single_position uses ticker_cooldown_hours."""
expiry = cb.compute_cooldown_expiry(
trigger_type="single_position",
triggered_at=triggered_at,
)
expected = triggered_at + timedelta(hours=cb.ticker_cooldown_hours)
assert expiry == expected, (
f"single_position expiry {expiry} != expected {expected}"
)
@settings(max_examples=100)
@given(
cb=_circuit_breaker_strategy(),
triggered_at=_aware_datetime_strategy(),
trigger_type=st.sampled_from(["daily_loss", "volatility"]),
)
def test_cooldown_expiry_daily_loss_and_volatility_use_pause_hours(
self, cb: CircuitBreaker, triggered_at: datetime, trigger_type: str,
) -> None:
"""compute_cooldown_expiry for daily_loss/volatility uses volatility_pause_hours."""
expiry = cb.compute_cooldown_expiry(
trigger_type=trigger_type,
triggered_at=triggered_at,
)
expected = triggered_at + timedelta(hours=cb.volatility_pause_hours)
assert expiry == expected, (
f"{trigger_type} expiry {expiry} != expected {expected}"
)
+670
View File
@@ -0,0 +1,670 @@
"""Property-based tests for the TradingEngine core decision loop.
Feature: autonomous-trading-engine
Tests properties 27, 28, 16, and 18 from the design specification,
covering recommendation deduplication, decision record completeness,
multiple declining positions halt, and maximum open positions enforcement.
"""
from __future__ import annotations
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.shared.config import TradingConfig
from services.trading.correlation import CorrelationMatrix
from services.trading.engine import TradingEngine
from services.trading.models import (
CircuitBreakerState,
OpenPosition,
PortfolioState,
RiskTierConfig,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
ET = ZoneInfo("America/New_York")
# A valid trading window datetime: Wednesday 10:00 AM ET
VALID_TRADING_DT = datetime(2025, 1, 8, 10, 0, 0, tzinfo=ET)
def _make_engine() -> TradingEngine:
"""Create a TradingEngine with default TradingConfig and no pool/redis."""
return TradingEngine(pool=None, redis=None, config=TradingConfig())
def _moderate_risk_tier() -> RiskTierConfig:
"""Return a moderate risk tier with reasonable defaults for testing."""
return RiskTierConfig(
name="moderate",
min_confidence=0.55,
max_position_pct=0.10,
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.30,
max_portfolio_heat=0.20,
)
def _inactive_cb() -> CircuitBreakerState:
"""Return an inactive circuit breaker state."""
return CircuitBreakerState(active=False)
def _empty_correlation_matrix() -> CorrelationMatrix:
"""Return an empty correlation matrix."""
return CorrelationMatrix()
def _base_portfolio(
active_pool: float = 5000.0,
positions: list | None = None,
portfolio_heat: float = 0.0,
open_position_count: int = 0,
) -> PortfolioState:
"""Return a PortfolioState with sensible defaults for testing."""
return PortfolioState(
positions=positions or [],
total_value=active_pool + 500.0,
cash=active_pool,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure={},
portfolio_heat=portfolio_heat,
open_position_count=open_position_count,
)
def _make_recommendation(
rec_id: str = "rec-001",
ticker: str = "AAPL",
confidence: float = 0.80,
sector: str = "Technology",
current_price: float = 10.0,
action: str = "buy",
) -> dict:
"""Build a recommendation dict suitable for evaluate_recommendation."""
return {
"recommendation_id": rec_id,
"ticker": ticker,
"confidence": confidence,
"sector": sector,
"current_price": current_price,
"action": action,
}
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _recommendation_id_strategy() -> st.SearchStrategy[str]:
"""Generate random recommendation IDs."""
return st.text(
alphabet=st.sampled_from("abcdefghijklmnopqrstuvwxyz0123456789-"),
min_size=5,
max_size=20,
).filter(lambda s: len(s.strip()) > 0)
def _open_position_strategy(
unrealized_pnl: st.SearchStrategy[float] | None = None,
) -> st.SearchStrategy[OpenPosition]:
"""Generate random OpenPosition objects."""
return st.builds(
OpenPosition,
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
quantity=st.integers(min_value=1, max_value=100),
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
current_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
unrealized_pnl=unrealized_pnl if unrealized_pnl is not None else st.floats(
min_value=-1000.0, max_value=1000.0, allow_nan=False, allow_infinity=False
),
market_value=st.floats(min_value=10.0, max_value=5000.0, allow_nan=False, allow_infinity=False),
sector=st.sampled_from(["Technology", "Healthcare", "Energy", "Financials", "Consumer"]),
stop_loss_price=st.floats(min_value=1.0, max_value=400.0, allow_nan=False, allow_infinity=False),
take_profit_price=st.floats(min_value=10.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
signal_confidence=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
is_micro_trade=st.just(False),
)
# ---------------------------------------------------------------------------
# Property 27: Recommendation deduplication (idempotence)
# **Validates: Requirements 1.5**
# ---------------------------------------------------------------------------
class TestProperty27RecommendationDeduplication:
"""Property 27: Recommendation deduplication (idempotence).
**Validates: Requirements 1.5**
"""
@settings(max_examples=100)
@given(rec_id=_recommendation_id_strategy())
def test_duplicate_recommendation_produces_skip(self, rec_id: str) -> None:
"""Processing the same recommendation twice produces a skip on the second call."""
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(active_pool=5000.0)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id=rec_id,
ticker="AAPL",
confidence=0.80,
sector="Technology",
current_price=10.0,
)
# First processing — should produce an "act" decision
decision1 = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert decision1.decision == "act", (
f"First processing should produce 'act', got '{decision1.decision}' "
f"with skip_reason={decision1.skip_reason}"
)
# Second processing — should produce a "skip" with duplicate reason
decision2 = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert decision2.decision == "skip"
assert decision2.skip_reason == "duplicate_recommendation"
@settings(max_examples=100)
@given(
rec_id_a=_recommendation_id_strategy(),
rec_id_b=_recommendation_id_strategy(),
)
def test_different_recommendations_not_deduplicated(
self, rec_id_a: str, rec_id_b: str,
) -> None:
"""Different recommendation IDs are processed independently."""
assume(rec_id_a != rec_id_b)
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(active_pool=5000.0)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec_a = _make_recommendation(rec_id=rec_id_a, ticker="AAPL", current_price=10.0)
rec_b = _make_recommendation(rec_id=rec_id_b, ticker="MSFT", current_price=10.0)
decision_a = engine.evaluate_recommendation(
rec=rec_a,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
decision_b = engine.evaluate_recommendation(
rec=rec_b,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
# Both should be "act" (not deduplicated against each other)
assert decision_a.decision == "act"
assert decision_b.decision == "act"
# ---------------------------------------------------------------------------
# Property 28: Trading decision record completeness and traceability
# **Validates: Requirements 1.4, 17.1, 17.2**
# ---------------------------------------------------------------------------
class TestProperty28DecisionRecordCompleteness:
"""Property 28: Trading decision record completeness and traceability.
**Validates: Requirements 1.4, 17.1, 17.2**
"""
@settings(max_examples=100)
@given(
confidence=st.floats(min_value=0.60, max_value=0.99, allow_nan=False, allow_infinity=False),
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
price=st.floats(min_value=1.0, max_value=50.0, allow_nan=False, allow_infinity=False),
)
def test_act_decision_has_all_required_fields(
self, confidence: float, ticker: str, price: float,
) -> None:
"""Act decisions have all required fields including position size."""
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(active_pool=5000.0)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id=f"rec-{ticker}",
ticker=ticker,
confidence=confidence,
current_price=price,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
# All required fields must be present and non-None
assert decision.id is not None and len(decision.id) > 0
assert decision.recommendation_id == f"rec-{ticker}"
assert decision.decision in ("act", "skip")
assert decision.ticker == ticker
assert decision.risk_tier_at_decision == "moderate"
assert decision.circuit_breaker_status in ("active", "inactive")
assert decision.decision_trace is not None
assert isinstance(decision.decision_trace, dict)
assert decision.created_at is not None
if decision.decision == "act":
assert decision.computed_position_size is not None
assert decision.computed_position_size > 0
assert decision.computed_share_quantity is not None
assert decision.computed_share_quantity > 0
assert decision.skip_reason is None
@settings(max_examples=100)
@given(
confidence=st.floats(min_value=0.01, max_value=0.50, allow_nan=False, allow_infinity=False),
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
)
def test_skip_decision_has_skip_reason(
self, confidence: float, ticker: str,
) -> None:
"""Skip decisions have skip_reason set."""
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(active_pool=5000.0)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
# Use low confidence to trigger a skip
assume(confidence < risk_tier.min_confidence)
rec = _make_recommendation(
rec_id=f"rec-{ticker}",
ticker=ticker,
confidence=confidence,
current_price=10.0,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert decision.decision == "skip"
assert decision.skip_reason is not None
assert len(decision.skip_reason) > 0
# Required fields still present on skip decisions
assert decision.id is not None
assert decision.recommendation_id is not None
assert decision.ticker == ticker
assert decision.risk_tier_at_decision == "moderate"
assert decision.circuit_breaker_status == "inactive"
assert decision.decision_trace is not None
assert decision.created_at is not None
@settings(max_examples=100)
@given(
confidence=st.floats(min_value=0.60, max_value=0.99, allow_nan=False, allow_infinity=False),
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
price=st.floats(min_value=1.0, max_value=50.0, allow_nan=False, allow_infinity=False),
)
def test_decision_trace_contains_reasoning(
self, confidence: float, ticker: str, price: float,
) -> None:
"""Decision trace contains a reasoning list for traceability."""
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(active_pool=5000.0)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id=f"rec-trace-{ticker}",
ticker=ticker,
confidence=confidence,
current_price=price,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert "reasoning" in decision.decision_trace
assert isinstance(decision.decision_trace["reasoning"], list)
assert len(decision.decision_trace["reasoning"]) > 0
# ---------------------------------------------------------------------------
# Property 16: Multiple declining positions halts new entries
# **Validates: Requirements 7.5**
# ---------------------------------------------------------------------------
class TestProperty16DecliningPositionsHalt:
"""Property 16: Multiple declining positions halts new entries.
**Validates: Requirements 7.5**
"""
@settings(max_examples=100)
@given(
total_positions=st.integers(min_value=4, max_value=20),
declining_fraction=st.floats(min_value=0.55, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_entries_halted_when_majority_declining(
self, total_positions: int, declining_fraction: float,
) -> None:
"""New entries halted when > 50% of positions have > 2% negative unrealized P&L."""
declining_count = int(total_positions * declining_fraction)
assume(declining_count > total_positions * 0.50) # strictly > 50%
non_declining_count = total_positions - declining_count
positions: list[OpenPosition] = []
# Declining positions: unrealized_pnl is negative and > 2% of entry value
for i in range(declining_count):
entry_price = 100.0
quantity = 10
entry_value = entry_price * quantity
# Loss > 2% of entry value
loss = entry_value * 0.03 # 3% loss
positions.append(OpenPosition(
ticker=f"DEC{i}",
quantity=quantity,
entry_price=entry_price,
current_price=entry_price - (loss / quantity),
unrealized_pnl=-loss,
market_value=entry_value - loss,
sector="Technology",
stop_loss_price=90.0,
take_profit_price=120.0,
signal_confidence=0.6,
))
# Non-declining positions: small positive or flat P&L
for i in range(non_declining_count):
entry_price = 100.0
quantity = 10
positions.append(OpenPosition(
ticker=f"OK{i}",
quantity=quantity,
entry_price=entry_price,
current_price=entry_price + 1.0,
unrealized_pnl=10.0,
market_value=1010.0,
sector="Healthcare",
stop_loss_price=90.0,
take_profit_price=120.0,
signal_confidence=0.7,
))
engine = _make_engine()
# Verify the check_declining_positions method returns True
assert engine.check_declining_positions(positions) is True
# Verify the full engine skips new entries
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(
active_pool=5000.0,
positions=positions,
open_position_count=total_positions,
)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id="rec-decline-test",
ticker="NEWSTOCK",
confidence=0.80,
current_price=10.0,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert decision.decision == "skip"
assert decision.skip_reason == "multiple_declining_positions"
@settings(max_examples=100)
@given(
total_positions=st.integers(min_value=4, max_value=20),
declining_fraction=st.floats(min_value=0.0, max_value=0.45, allow_nan=False, allow_infinity=False),
)
def test_entries_allowed_when_minority_declining(
self, total_positions: int, declining_fraction: float,
) -> None:
"""Entries allowed when <= 50% of positions are declining."""
declining_count = int(total_positions * declining_fraction)
assume(declining_count <= total_positions * 0.50) # at or below 50%
non_declining_count = total_positions - declining_count
positions: list[OpenPosition] = []
# Declining positions
for i in range(declining_count):
entry_price = 100.0
quantity = 10
entry_value = entry_price * quantity
loss = entry_value * 0.03
positions.append(OpenPosition(
ticker=f"DEC{i}",
quantity=quantity,
entry_price=entry_price,
current_price=entry_price - (loss / quantity),
unrealized_pnl=-loss,
market_value=entry_value - loss,
sector="Technology",
stop_loss_price=90.0,
take_profit_price=120.0,
signal_confidence=0.6,
))
# Non-declining positions
for i in range(non_declining_count):
entry_price = 100.0
quantity = 10
positions.append(OpenPosition(
ticker=f"OK{i}",
quantity=quantity,
entry_price=entry_price,
current_price=entry_price + 1.0,
unrealized_pnl=10.0,
market_value=1010.0,
sector="Healthcare",
stop_loss_price=90.0,
take_profit_price=120.0,
signal_confidence=0.7,
))
engine = _make_engine()
# Verify the check returns False (entries allowed)
assert engine.check_declining_positions(positions) is False
@settings(max_examples=100)
@given(data=st.data())
def test_empty_positions_allows_entries(self, data: st.DataObject) -> None:
"""Empty position list always allows new entries."""
engine = _make_engine()
assert engine.check_declining_positions([]) is False
# ---------------------------------------------------------------------------
# Property 18: Maximum open positions enforcement
# **Validates: Requirements 8.4**
# ---------------------------------------------------------------------------
class TestProperty18MaxOpenPositions:
"""Property 18: Maximum open positions enforcement.
**Validates: Requirements 8.4**
"""
@settings(max_examples=100)
@given(
max_positions=st.integers(min_value=1, max_value=20),
)
def test_entries_rejected_at_max_positions(self, max_positions: int) -> None:
"""New entries rejected when open_position_count >= max_open_positions."""
engine = _make_engine()
# Verify the check_max_positions method returns True at the limit
assert engine.check_max_positions(max_positions, max_positions) is True
# Also verify above the limit
assert engine.check_max_positions(max_positions + 1, max_positions) is True
@settings(max_examples=100)
@given(
max_positions=st.integers(min_value=2, max_value=20),
open_count_offset=st.integers(min_value=1, max_value=10),
)
def test_entries_allowed_below_max_positions(
self, max_positions: int, open_count_offset: int,
) -> None:
"""Entries allowed when open_position_count < max_open_positions."""
open_count = max(0, max_positions - open_count_offset)
assume(open_count < max_positions)
engine = _make_engine()
assert engine.check_max_positions(open_count, max_positions) is False
@settings(max_examples=100)
@given(
max_positions=st.integers(min_value=1, max_value=15),
)
def test_full_engine_rejects_at_max_positions(self, max_positions: int) -> None:
"""Full engine evaluation rejects new entries at max positions."""
engine = _make_engine()
# Override the config's max_open_positions via hasattr fallback
# The engine uses: self.config.max_open_positions if hasattr(...) else 10
# TradingConfig doesn't have max_open_positions, so it defaults to 10.
# We test with the default (10) by setting open_position_count = 10.
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(
active_pool=5000.0,
open_position_count=10, # default max is 10
)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id=f"rec-max-{max_positions}",
ticker="MAXTEST",
confidence=0.80,
current_price=10.0,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
assert decision.decision == "skip"
assert decision.skip_reason == "max_positions_reached"
@settings(max_examples=100)
@given(
open_count=st.integers(min_value=0, max_value=8),
)
def test_full_engine_allows_below_max_positions(self, open_count: int) -> None:
"""Full engine evaluation allows entries below max positions (default 10)."""
assume(open_count < 10) # default max is 10
engine = _make_engine()
risk_tier = _moderate_risk_tier()
portfolio = _base_portfolio(
active_pool=5000.0,
open_position_count=open_count,
)
cb_state = _inactive_cb()
corr_matrix = _empty_correlation_matrix()
rec = _make_recommendation(
rec_id=f"rec-below-{open_count}",
ticker="BELOWMAX",
confidence=0.80,
current_price=10.0,
)
decision = engine.evaluate_recommendation(
rec=rec,
portfolio_state=portfolio,
risk_tier=risk_tier,
circuit_breaker_state=cb_state,
correlation_matrix=corr_matrix,
earnings_calendar={},
now=VALID_TRADING_DT,
)
# Should not be rejected for max positions
if decision.decision == "skip":
assert decision.skip_reason != "max_positions_reached"
+361
View File
@@ -0,0 +1,361 @@
"""Property-based tests for the Micro-Trading Module.
Feature: autonomous-trading-engine
Property 31: Micro-trade parameter constraints.
Property 32: Micro-trade auto-close after max hold duration.
Property 34: Micro-trades respect all existing constraints.
"""
from __future__ import annotations
from hypothesis import given, settings
from hypothesis import strategies as st
from services.trading.micro_trading import MicroTradeConfig, MicroTradingModule
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _micro_config_strategy() -> st.SearchStrategy[MicroTradeConfig]:
"""Generate random MicroTradeConfig objects with valid ranges."""
return st.builds(
MicroTradeConfig,
enabled=st.just(True),
allocation_cap_pct=st.floats(
min_value=0.01, max_value=0.10, allow_nan=False, allow_infinity=False,
),
max_daily=st.integers(min_value=1, max_value=50),
max_hold_minutes=st.integers(min_value=10, max_value=480),
stop_loss_atr_multiplier=st.just(1.0),
reward_risk_ratio=st.just(1.5),
)
# ---------------------------------------------------------------------------
# Property 31: Micro-trade parameter constraints
# **Validates: Requirements 20.3, 20.4, 20.5**
# ---------------------------------------------------------------------------
class TestProperty31MicroTradeParameterConstraints:
"""Property 31: Micro-trade parameter constraints.
Verify allocation does not exceed micro_trading_allocation_cap_pct.
Verify daily count does not exceed configured maximum.
**Validates: Requirements 20.3, 20.4, 20.5**
"""
module = MicroTradingModule()
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
active_pool=st.floats(
min_value=100.0, max_value=100_000.0,
allow_nan=False, allow_infinity=False,
),
)
def test_allocation_does_not_exceed_cap(
self,
config: MicroTradeConfig,
active_pool: float,
) -> None:
"""Micro-trade allocation never exceeds allocation_cap_pct * active_pool."""
cap = self.module.compute_allocation_cap(config, active_pool)
expected = config.allocation_cap_pct * active_pool
assert abs(cap - expected) < 1e-6
assert cap <= config.allocation_cap_pct * active_pool + 1e-6
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
daily_count=st.integers(min_value=0, max_value=100),
)
def test_daily_count_enforced(
self,
config: MicroTradeConfig,
daily_count: int,
) -> None:
"""should_evaluate returns False when daily count >= max_daily."""
result = self.module.should_evaluate(config, daily_count)
if daily_count >= config.max_daily:
assert result is False
else:
assert result is True
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
)
def test_stop_loss_at_1x_atr(
self,
config: MicroTradeConfig,
) -> None:
"""Micro-trade stop-loss uses 1.0x ATR multiplier."""
assert config.stop_loss_atr_multiplier == 1.0
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
)
def test_take_profit_at_1_5x_stop_distance(
self,
config: MicroTradeConfig,
) -> None:
"""Micro-trade take-profit uses 1.5x reward-risk ratio."""
assert config.reward_risk_ratio == 1.5
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
active_pool=st.floats(
min_value=100.0, max_value=100_000.0,
allow_nan=False, allow_infinity=False,
),
)
def test_allocation_cap_scales_with_active_pool(
self,
config: MicroTradeConfig,
active_pool: float,
) -> None:
"""Allocation cap is proportional to active pool."""
cap = self.module.compute_allocation_cap(config, active_pool)
# Doubling active pool should double the cap
cap_double = self.module.compute_allocation_cap(config, active_pool * 2)
assert abs(cap_double - cap * 2) < 1e-4
# ---------------------------------------------------------------------------
# Property 32: Micro-trade auto-close after max hold duration
# **Validates: Requirements 20.6**
# ---------------------------------------------------------------------------
class TestProperty32MicroTradeAutoClose:
"""Property 32: Micro-trade auto-close after max hold duration.
Verify positions closed when hold exceeds max duration.
Verify positions not closed when hold is within duration.
**Validates: Requirements 20.6**
"""
module = MicroTradingModule()
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
hold_minutes=st.floats(
min_value=0.0, max_value=1000.0,
allow_nan=False, allow_infinity=False,
),
)
def test_auto_close_when_exceeds_max_hold(
self,
config: MicroTradeConfig,
hold_minutes: float,
) -> None:
"""Position auto-closed when hold_minutes > max_hold_minutes."""
result = self.module.should_auto_close(config, hold_minutes)
if hold_minutes > config.max_hold_minutes:
assert result is True
else:
assert result is False
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
)
def test_not_closed_at_exact_max(
self,
config: MicroTradeConfig,
) -> None:
"""Position is NOT auto-closed at exactly max_hold_minutes."""
result = self.module.should_auto_close(config, float(config.max_hold_minutes))
assert result is False
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
)
def test_closed_just_past_max(
self,
config: MicroTradeConfig,
) -> None:
"""Position IS auto-closed just past max_hold_minutes."""
result = self.module.should_auto_close(
config, float(config.max_hold_minutes) + 0.01,
)
assert result is True
# ---------------------------------------------------------------------------
# Property 34: Micro-trades respect all existing constraints
# **Validates: Requirements 20.10**
# ---------------------------------------------------------------------------
class TestProperty34MicroTradesRespectConstraints:
"""Property 34: Micro-trades respect all existing constraints.
Verify trading window, circuit breakers, portfolio heat constraints
all enforced for micro-trades.
**Validates: Requirements 20.10**
"""
module = MicroTradingModule()
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
daily_count=st.integers(min_value=0, max_value=5),
)
def test_circuit_breaker_blocks_micro_trade(
self,
config: MicroTradeConfig,
daily_count: int,
) -> None:
"""Micro-trades blocked when circuit breaker is active."""
allowed, reason = self.module.check_constraints(
config=config,
daily_count=daily_count,
is_within_window=True,
circuit_breaker_active=True,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "circuit_breaker_active"
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
daily_count=st.integers(min_value=0, max_value=5),
)
def test_outside_trading_window_blocks_micro_trade(
self,
config: MicroTradeConfig,
daily_count: int,
) -> None:
"""Micro-trades blocked outside trading window."""
allowed, reason = self.module.check_constraints(
config=config,
daily_count=daily_count,
is_within_window=False,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "outside_trading_window"
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
heat_pct=st.floats(
min_value=0.20, max_value=1.0,
allow_nan=False, allow_infinity=False,
),
)
def test_portfolio_heat_blocks_micro_trade(
self,
config: MicroTradeConfig,
heat_pct: float,
) -> None:
"""Micro-trades blocked when portfolio heat exceeds max."""
max_heat = 0.20
allowed, reason = self.module.check_constraints(
config=config,
daily_count=0,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=heat_pct,
max_heat=max_heat,
)
assert allowed is False
assert reason == "portfolio_heat_exceeded"
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
)
def test_daily_limit_blocks_micro_trade(
self,
config: MicroTradeConfig,
) -> None:
"""Micro-trades blocked when daily limit reached."""
allowed, reason = self.module.check_constraints(
config=config,
daily_count=config.max_daily,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "daily_limit_reached"
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
daily_count=st.integers(min_value=0, max_value=5),
)
def test_disabled_blocks_micro_trade(
self,
config: MicroTradeConfig,
daily_count: int,
) -> None:
"""Micro-trades blocked when module is disabled."""
disabled_config = MicroTradeConfig(
enabled=False,
allocation_cap_pct=config.allocation_cap_pct,
max_daily=config.max_daily,
max_hold_minutes=config.max_hold_minutes,
)
allowed, reason = self.module.check_constraints(
config=disabled_config,
daily_count=daily_count,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=0.05,
max_heat=0.20,
)
assert allowed is False
assert reason == "micro_trading_disabled"
@settings(max_examples=100)
@given(
config=_micro_config_strategy(),
daily_count=st.integers(min_value=0, max_value=5),
heat_pct=st.floats(
min_value=0.0, max_value=0.15,
allow_nan=False, allow_infinity=False,
),
)
def test_all_constraints_pass_allows_trade(
self,
config: MicroTradeConfig,
daily_count: int,
heat_pct: float,
) -> None:
"""Micro-trade allowed when all constraints pass."""
# Ensure daily_count is under the limit
safe_count = min(daily_count, config.max_daily - 1)
allowed, reason = self.module.check_constraints(
config=config,
daily_count=safe_count,
is_within_window=True,
circuit_breaker_active=False,
portfolio_heat_pct=heat_pct,
max_heat=0.20,
)
assert allowed is True
assert reason == "ok"
+169
View File
@@ -0,0 +1,169 @@
"""Property-based tests for the Notification Service.
Feature: autonomous-trading-engine
Property 30: Notification rate limiting.
"""
from __future__ import annotations
from hypothesis import given, settings
from hypothesis import strategies as st
from services.trading.notifications import NotificationService
# ---------------------------------------------------------------------------
# Property 30: Notification rate limiting
# **Validates: Requirements 19.7**
# ---------------------------------------------------------------------------
class TestProperty30NotificationRateLimiting:
"""Property 30: Notification rate limiting.
Generate random sequences of notification requests within a one-hour
window. Verify at most 10 SMS and 20 emails allowed per hour.
Verify excess notifications blocked (should_send returns False).
**Validates: Requirements 19.7**
"""
@settings(max_examples=100)
@given(
sms_limit=st.integers(min_value=1, max_value=50),
email_limit=st.integers(min_value=1, max_value=50),
sms_requests=st.integers(min_value=0, max_value=100),
email_requests=st.integers(min_value=0, max_value=100),
)
def test_sms_rate_limit_enforced(
self,
sms_limit: int,
email_limit: int,
sms_requests: int,
email_requests: int,
) -> None:
"""At most sms_limit SMS notifications are allowed per hour."""
svc = NotificationService(
sms_enabled=True,
email_enabled=True,
rate_limit_sms_per_hour=sms_limit,
rate_limit_email_per_hour=email_limit,
)
sent_sms = 0
for i in range(sms_requests):
if svc.should_send("sms", current_hour_count=i):
sent_sms += 1
assert sent_sms <= sms_limit
@settings(max_examples=100)
@given(
sms_limit=st.integers(min_value=1, max_value=50),
email_limit=st.integers(min_value=1, max_value=50),
sms_requests=st.integers(min_value=0, max_value=100),
email_requests=st.integers(min_value=0, max_value=100),
)
def test_email_rate_limit_enforced(
self,
sms_limit: int,
email_limit: int,
sms_requests: int,
email_requests: int,
) -> None:
"""At most email_limit email notifications are allowed per hour."""
svc = NotificationService(
sms_enabled=True,
email_enabled=True,
rate_limit_sms_per_hour=sms_limit,
rate_limit_email_per_hour=email_limit,
)
sent_email = 0
for i in range(email_requests):
if svc.should_send("email", current_hour_count=i):
sent_email += 1
assert sent_email <= email_limit
@settings(max_examples=100)
@given(
sms_limit=st.integers(min_value=1, max_value=50),
email_limit=st.integers(min_value=1, max_value=50),
)
def test_excess_sms_blocked(
self,
sms_limit: int,
email_limit: int,
) -> None:
"""Notifications beyond the limit are blocked."""
svc = NotificationService(
sms_enabled=True,
email_enabled=True,
rate_limit_sms_per_hour=sms_limit,
rate_limit_email_per_hour=email_limit,
)
# At the limit, should_send returns False
assert svc.should_send("sms", current_hour_count=sms_limit) is False
# One past the limit, still False
assert svc.should_send("sms", current_hour_count=sms_limit + 1) is False
@settings(max_examples=100)
@given(
sms_limit=st.integers(min_value=1, max_value=50),
email_limit=st.integers(min_value=1, max_value=50),
)
def test_excess_email_blocked(
self,
sms_limit: int,
email_limit: int,
) -> None:
"""Email notifications beyond the limit are blocked."""
svc = NotificationService(
sms_enabled=True,
email_enabled=True,
rate_limit_sms_per_hour=sms_limit,
rate_limit_email_per_hour=email_limit,
)
assert svc.should_send("email", current_hour_count=email_limit) is False
assert svc.should_send("email", current_hour_count=email_limit + 1) is False
@settings(max_examples=100)
@given(
count=st.integers(min_value=0, max_value=100),
)
def test_default_limits_10_sms_20_email(
self,
count: int,
) -> None:
"""Default limits are 10 SMS and 20 emails per hour."""
svc = NotificationService(sms_enabled=True, email_enabled=True)
sms_allowed = svc.should_send("sms", current_hour_count=count)
email_allowed = svc.should_send("email", current_hour_count=count)
if count < 10:
assert sms_allowed is True
else:
assert sms_allowed is False
if count < 20:
assert email_allowed is True
else:
assert email_allowed is False
@settings(max_examples=100)
@given(
count=st.integers(min_value=0, max_value=50),
)
def test_disabled_channel_always_blocked(
self,
count: int,
) -> None:
"""Disabled channels always return False regardless of count."""
svc = NotificationService(sms_enabled=False, email_enabled=False)
assert svc.should_send("sms", current_hour_count=count) is False
assert svc.should_send("email", current_hour_count=count) is False
+510
View File
@@ -0,0 +1,510 @@
"""Property-based tests for the Performance Tracker.
Feature: autonomous-trading-engine
Property 26: Performance metrics computation.
Property 33: Micro-trade metrics tracked separately.
"""
from __future__ import annotations
import math
from datetime import timedelta
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.models import ClosedTrade
from services.trading.performance_tracker import PerformanceComputer
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _closed_trade_strategy(
is_micro: st.SearchStrategy[bool] | None = None,
) -> st.SearchStrategy[ClosedTrade]:
"""Generate random ClosedTrade objects with consistent pnl fields."""
return st.builds(
_make_closed_trade,
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
exit_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
quantity=st.integers(min_value=1, max_value=100),
hold_days=st.integers(min_value=1, max_value=90),
is_micro_trade=is_micro if is_micro is not None else st.booleans(),
)
def _make_closed_trade(
ticker: str,
entry_price: float,
exit_price: float,
quantity: int,
hold_days: int,
is_micro_trade: bool,
) -> ClosedTrade:
"""Create a ClosedTrade with consistent pnl fields."""
pnl = (exit_price - entry_price) * quantity
pnl_pct = (exit_price - entry_price) / entry_price if entry_price > 0 else 0.0
return ClosedTrade(
ticker=ticker,
entry_price=entry_price,
exit_price=exit_price,
quantity=quantity,
pnl=pnl,
pnl_pct=pnl_pct,
hold_duration=timedelta(days=hold_days),
recommendation_id=None,
is_micro_trade=is_micro_trade,
)
def _daily_returns_strategy(
min_size: int = 2,
max_size: int = 60,
) -> st.SearchStrategy[list[float]]:
"""Generate random daily return sequences."""
return st.lists(
st.floats(min_value=-0.10, max_value=0.10, allow_nan=False, allow_infinity=False),
min_size=min_size,
max_size=max_size,
)
# ---------------------------------------------------------------------------
# Property 26: Performance metrics computation
# **Validates: Requirements 14.1, 14.2**
# ---------------------------------------------------------------------------
class TestProperty26PerformanceMetrics:
"""Property 26: Performance metrics computation.
**Validates: Requirements 14.1, 14.2**
"""
computer = PerformanceComputer()
@settings(max_examples=100)
@given(
trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=30,
),
)
def test_win_rate_equals_wins_over_total(self, trades: list[ClosedTrade]) -> None:
"""win_rate = wins / total_trades."""
metrics = self.computer.compute_metrics(
closed_trades=trades,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
total = len(trades)
wins = sum(1 for t in trades if t.pnl > 0)
expected_win_rate = wins / total if total > 0 else 0.0
assert abs(metrics.win_rate - expected_win_rate) < 1e-9, (
f"Expected win_rate={expected_win_rate}, got {metrics.win_rate}"
)
assert metrics.win_count == wins
assert metrics.loss_count == total - wins
@settings(max_examples=100)
@given(
trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=30,
),
)
def test_profit_factor_equals_gross_profits_over_losses(
self, trades: list[ClosedTrade],
) -> None:
"""profit_factor = gross_profits / gross_losses (inf if no losses)."""
metrics = self.computer.compute_metrics(
closed_trades=trades,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
gross_profits = sum(t.pnl for t in trades if t.pnl > 0)
gross_losses = abs(sum(t.pnl for t in trades if t.pnl <= 0))
if gross_losses > 0:
expected_pf = gross_profits / gross_losses
assert abs(metrics.profit_factor - expected_pf) < 1e-6, (
f"Expected profit_factor={expected_pf}, got {metrics.profit_factor}"
)
else:
if gross_profits > 0:
assert metrics.profit_factor == float("inf")
else:
assert metrics.profit_factor == 0.0
@settings(max_examples=100)
@given(
daily_returns=_daily_returns_strategy(min_size=2, max_size=60),
)
def test_sharpe_ratio_formula_consistency(
self, daily_returns: list[float],
) -> None:
"""Sharpe ratio = (mean / std) * sqrt(252), 0 if std=0 or <2 points."""
metrics = self.computer.compute_metrics(
closed_trades=[],
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
n = len(daily_returns)
mean_r = sum(daily_returns) / n
variance = sum((r - mean_r) ** 2 for r in daily_returns) / (n - 1)
std_r = math.sqrt(variance)
if std_r < 1e-12:
assert metrics.sharpe_ratio == 0.0
else:
expected = (mean_r / std_r) * math.sqrt(252)
assert abs(metrics.sharpe_ratio - expected) < 1e-6, (
f"Expected sharpe={expected}, got {metrics.sharpe_ratio}"
)
@settings(max_examples=100)
@given(
daily_returns=st.just([0.05, 0.05, 0.05]),
)
def test_sharpe_zero_when_std_is_zero(self, daily_returns: list[float]) -> None:
"""Sharpe ratio is 0.0 when all daily returns are identical (std=0)."""
metrics = self.computer.compute_metrics(
closed_trades=[],
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert metrics.sharpe_ratio == 0.0
@settings(max_examples=100)
@given(
daily_returns=st.just([0.01]),
)
def test_sharpe_zero_when_fewer_than_2_points(
self, daily_returns: list[float],
) -> None:
"""Sharpe ratio is 0.0 when fewer than 2 data points."""
metrics = self.computer.compute_metrics(
closed_trades=[],
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert metrics.sharpe_ratio == 0.0
@settings(max_examples=100)
@given(
trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=0,
max_size=20,
),
)
def test_empty_trades_gives_zero_metrics(self, trades: list[ClosedTrade]) -> None:
"""With no trades, win_rate=0, profit_factor=0, counts=0."""
if len(trades) > 0:
return # Only test empty case
metrics = self.computer.compute_metrics(
closed_trades=[],
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
assert metrics.win_count == 0
assert metrics.loss_count == 0
assert metrics.win_rate == 0.0
assert metrics.profit_factor == 0.0
@settings(max_examples=100)
@given(
trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=30,
),
)
def test_realized_pnl_equals_sum_of_trade_pnls(
self, trades: list[ClosedTrade],
) -> None:
"""realized_pnl equals the sum of all trade P&Ls."""
metrics = self.computer.compute_metrics(
closed_trades=trades,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
expected_pnl = sum(t.pnl for t in trades)
assert abs(metrics.realized_pnl - expected_pnl) < 1e-6
@settings(max_examples=100)
@given(
daily_returns=_daily_returns_strategy(min_size=3, max_size=60),
)
def test_max_drawdown_non_negative(self, daily_returns: list[float]) -> None:
"""Max drawdown is always non-negative."""
metrics = self.computer.compute_metrics(
closed_trades=[],
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=daily_returns,
)
assert metrics.max_drawdown >= 0.0
assert metrics.current_drawdown_pct >= 0.0
# ---------------------------------------------------------------------------
# Property 33: Micro-trade metrics tracked separately
# **Validates: Requirements 20.7**
# ---------------------------------------------------------------------------
class TestProperty33MicroTradeMetrics:
"""Property 33: Micro-trade metrics tracked separately.
**Validates: Requirements 20.7**
"""
computer = PerformanceComputer()
@settings(max_examples=100)
@given(
standard_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=15,
),
micro_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(True)),
min_size=1,
max_size=15,
),
)
def test_micro_trade_filter_returns_only_micro(
self,
standard_trades: list[ClosedTrade],
micro_trades: list[ClosedTrade],
) -> None:
"""filter_by_micro_trade(is_micro=True) returns only micro-trades."""
all_trades = standard_trades + micro_trades
filtered = self.computer.filter_by_micro_trade(all_trades, is_micro=True)
assert len(filtered) == len(micro_trades)
for t in filtered:
assert t.is_micro_trade is True
@settings(max_examples=100)
@given(
standard_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=15,
),
micro_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(True)),
min_size=1,
max_size=15,
),
)
def test_standard_trade_filter_returns_only_standard(
self,
standard_trades: list[ClosedTrade],
micro_trades: list[ClosedTrade],
) -> None:
"""filter_by_micro_trade(is_micro=False) returns only standard trades."""
all_trades = standard_trades + micro_trades
filtered = self.computer.filter_by_micro_trade(all_trades, is_micro=False)
assert len(filtered) == len(standard_trades)
for t in filtered:
assert t.is_micro_trade is False
@settings(max_examples=100)
@given(
standard_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=15,
),
micro_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(True)),
min_size=1,
max_size=15,
),
)
def test_micro_metrics_independent_from_standard(
self,
standard_trades: list[ClosedTrade],
micro_trades: list[ClosedTrade],
) -> None:
"""Micro-trade metrics are computed independently from standard metrics."""
all_trades = standard_trades + micro_trades
# Compute metrics for micro-trades only
micro_only = self.computer.filter_by_micro_trade(all_trades, is_micro=True)
micro_metrics = self.computer.compute_metrics(
closed_trades=micro_only,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
# Compute metrics for standard trades only
standard_only = self.computer.filter_by_micro_trade(all_trades, is_micro=False)
standard_metrics = self.computer.compute_metrics(
closed_trades=standard_only,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
# Verify counts are independent
assert micro_metrics.win_count + micro_metrics.loss_count == len(micro_trades)
assert standard_metrics.win_count + standard_metrics.loss_count == len(standard_trades)
# Verify win rates are computed independently
micro_wins = sum(1 for t in micro_trades if t.pnl > 0)
expected_micro_wr = micro_wins / len(micro_trades) if micro_trades else 0.0
assert abs(micro_metrics.win_rate - expected_micro_wr) < 1e-9
standard_wins = sum(1 for t in standard_trades if t.pnl > 0)
expected_std_wr = standard_wins / len(standard_trades) if standard_trades else 0.0
assert abs(standard_metrics.win_rate - expected_std_wr) < 1e-9
@settings(max_examples=100)
@given(
standard_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=15,
),
micro_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(True)),
min_size=1,
max_size=15,
),
)
def test_standard_metrics_not_contaminated_by_micro(
self,
standard_trades: list[ClosedTrade],
micro_trades: list[ClosedTrade],
) -> None:
"""Standard trade metrics are not contaminated by micro-trades."""
# Compute standard metrics from standard trades only
standard_metrics = self.computer.compute_metrics(
closed_trades=standard_trades,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
# Compute standard metrics after filtering from mixed set
all_trades = standard_trades + micro_trades
filtered_standard = self.computer.filter_by_micro_trade(all_trades, is_micro=False)
filtered_metrics = self.computer.compute_metrics(
closed_trades=filtered_standard,
portfolio_value=10000.0,
active_pool=8000.0,
reserve_pool=2000.0,
daily_pnl=0.0,
unrealized_pnl=0.0,
portfolio_heat=0.0,
daily_returns=[],
)
# Metrics should be identical
assert standard_metrics.win_count == filtered_metrics.win_count
assert standard_metrics.loss_count == filtered_metrics.loss_count
assert abs(standard_metrics.win_rate - filtered_metrics.win_rate) < 1e-9
assert abs(standard_metrics.realized_pnl - filtered_metrics.realized_pnl) < 1e-6
# Handle inf == inf case (inf - inf = nan)
if math.isinf(standard_metrics.profit_factor) and math.isinf(filtered_metrics.profit_factor):
pass # Both infinity — equal
else:
assert abs(standard_metrics.profit_factor - filtered_metrics.profit_factor) < 1e-6
@settings(max_examples=100)
@given(
standard_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(False)),
min_size=1,
max_size=15,
),
micro_trades=st.lists(
_closed_trade_strategy(is_micro=st.just(True)),
min_size=1,
max_size=15,
),
)
def test_filter_preserves_all_trades(
self,
standard_trades: list[ClosedTrade],
micro_trades: list[ClosedTrade],
) -> None:
"""Filtering micro + standard covers all trades with no overlap."""
all_trades = standard_trades + micro_trades
micro_filtered = self.computer.filter_by_micro_trade(all_trades, is_micro=True)
standard_filtered = self.computer.filter_by_micro_trade(all_trades, is_micro=False)
assert len(micro_filtered) + len(standard_filtered) == len(all_trades)
+925
View File
@@ -0,0 +1,925 @@
"""Property-based tests for the Position Sizer.
Feature: autonomous-trading-engine
Tests properties 15, 7, 19, and 24 from the design specification,
covering position sizing formula, correlation adjustment, sector exposure,
diversification bonus, Active Pool computation, earnings proximity,
portfolio heat, and Active Pool minimum enforcement.
"""
from __future__ import annotations
import math
from datetime import datetime, timedelta
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.models import (
OpenPosition,
PortfolioState,
PositionSizeResult,
RiskTierConfig,
)
from services.trading.position_sizer import PositionSizer
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _risk_tier_config_strategy() -> st.SearchStrategy[RiskTierConfig]:
"""Generate random RiskTierConfig objects with valid parameter ranges."""
return st.builds(
RiskTierConfig,
name=st.sampled_from(["conservative", "moderate", "aggressive"]),
min_confidence=st.floats(min_value=0.10, max_value=0.95, allow_nan=False, allow_infinity=False),
max_position_pct=st.floats(min_value=0.02, max_value=0.50, allow_nan=False, allow_infinity=False),
stop_loss_atr_multiplier=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
reward_risk_ratio=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
max_sector_pct=st.floats(min_value=0.05, max_value=0.60, allow_nan=False, allow_infinity=False),
max_portfolio_heat=st.floats(min_value=0.05, max_value=0.50, allow_nan=False, allow_infinity=False),
)
def _open_position_strategy(
sector: st.SearchStrategy[str] | None = None,
) -> st.SearchStrategy[OpenPosition]:
"""Generate random OpenPosition objects."""
sector_st = sector if sector is not None else st.sampled_from(
["Technology", "Healthcare", "Energy", "Financials", "Consumer"]
)
return st.builds(
OpenPosition,
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
quantity=st.integers(min_value=1, max_value=100),
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
current_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
unrealized_pnl=st.floats(min_value=-1000.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
market_value=st.floats(min_value=10.0, max_value=5000.0, allow_nan=False, allow_infinity=False),
sector=sector_st,
stop_loss_price=st.floats(min_value=1.0, max_value=400.0, allow_nan=False, allow_infinity=False),
take_profit_price=st.floats(min_value=10.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
signal_confidence=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
is_micro_trade=st.just(False),
)
def _portfolio_state_strategy(
positions: st.SearchStrategy[list] | None = None,
sector_exposure: st.SearchStrategy[dict] | None = None,
active_pool: st.SearchStrategy[float] | None = None,
portfolio_heat: st.SearchStrategy[float] | None = None,
) -> st.SearchStrategy[PortfolioState]:
"""Generate random PortfolioState objects."""
return st.builds(
PortfolioState,
positions=positions if positions is not None else st.just([]),
total_value=st.floats(min_value=100.0, max_value=20000.0, allow_nan=False, allow_infinity=False),
cash=st.floats(min_value=0.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
active_pool=active_pool if active_pool is not None else st.floats(
min_value=100.0, max_value=10000.0, allow_nan=False, allow_infinity=False
),
reserve_pool=st.floats(min_value=0.0, max_value=5000.0, allow_nan=False, allow_infinity=False),
sector_exposure=sector_exposure if sector_exposure is not None else st.just({}),
portfolio_heat=portfolio_heat if portfolio_heat is not None else st.just(0.0),
open_position_count=st.integers(min_value=0, max_value=10),
)
# ---------------------------------------------------------------------------
# Property 1: Position sizing formula and invariants
# **Validates: Requirements 2.1, 2.2, 2.3, 2.4, 2.7**
# ---------------------------------------------------------------------------
class TestProperty1PositionSizingFormula:
"""Property 1: Position sizing formula and invariants.
**Validates: Requirements 2.1, 2.2, 2.3, 2.4, 2.7**
"""
sizer = PositionSizer()
@settings(max_examples=100)
@given(
confidence=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
active_pool=st.floats(min_value=100.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
price=st.floats(min_value=1.0, max_value=500.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
absolute_cap=st.floats(min_value=10.0, max_value=500.0, allow_nan=False, allow_infinity=False),
)
def test_zero_allocation_below_min_confidence(
self, confidence: float, active_pool: float, price: float,
risk_tier: RiskTierConfig, absolute_cap: float,
) -> None:
"""Confidence below min_confidence yields zero allocation."""
assume(confidence < risk_tier.min_confidence)
portfolio = PortfolioState(active_pool=active_pool)
result = self.sizer.compute(
confidence=confidence,
ticker="TEST",
sector="Technology",
current_price=price,
active_pool=active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio,
correlation_matrix={},
earnings_calendar={},
absolute_position_cap=absolute_cap,
)
assert result.rejected is True
assert result.dollar_amount == 0.0
assert result.share_quantity == 0
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=100.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
price=st.floats(min_value=1.0, max_value=500.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
absolute_cap=st.floats(min_value=10.0, max_value=500.0, allow_nan=False, allow_infinity=False),
)
def test_allocation_never_exceeds_max_position_pct_or_cap(
self, active_pool: float, price: float,
risk_tier: RiskTierConfig, absolute_cap: float,
) -> None:
"""Allocation never exceeds max_position_pct * active_pool or absolute cap."""
# Use confidence well above threshold to get a non-rejected result
confidence = min(risk_tier.min_confidence + 0.3, 1.0)
portfolio = PortfolioState(active_pool=active_pool)
result = self.sizer.compute(
confidence=confidence,
ticker="TEST",
sector="Technology",
current_price=price,
active_pool=active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio,
correlation_matrix={},
earnings_calendar={},
absolute_position_cap=absolute_cap,
)
if not result.rejected:
max_allowed = risk_tier.max_position_pct * active_pool
# Dollar amount (based on whole shares) should not exceed the cap or max_position_pct
assert result.dollar_amount <= max_allowed + 0.01, (
f"dollar_amount {result.dollar_amount} > max_allowed {max_allowed}"
)
assert result.dollar_amount <= absolute_cap + 0.01, (
f"dollar_amount {result.dollar_amount} > absolute_cap {absolute_cap}"
)
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=200.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
price=st.floats(min_value=1.0, max_value=50.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
absolute_cap=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
)
def test_share_quantity_rounded_down(
self, active_pool: float, price: float,
risk_tier: RiskTierConfig, absolute_cap: float,
) -> None:
"""Share quantity is always rounded down to whole shares (math.floor)."""
confidence = min(risk_tier.min_confidence + 0.3, 1.0)
portfolio = PortfolioState(active_pool=active_pool)
result = self.sizer.compute(
confidence=confidence,
ticker="TEST",
sector="Technology",
current_price=price,
active_pool=active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio,
correlation_matrix={},
earnings_calendar={},
absolute_position_cap=absolute_cap,
)
if not result.rejected:
assert result.share_quantity == int(result.share_quantity)
assert result.share_quantity >= 1
# Verify it's floor: share_quantity * price should be <= the pre-rounding dollar amount
assert result.dollar_amount == result.share_quantity * price
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=100.0, max_value=500.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_rejection_when_zero_shares(
self, active_pool: float, risk_tier: RiskTierConfig,
) -> None:
"""Trade rejected when rounded share quantity is zero (price too high)."""
confidence = min(risk_tier.min_confidence + 0.1, 1.0)
# Use a very high price so dollar_amount / price < 1
price = active_pool * 10.0
portfolio = PortfolioState(active_pool=active_pool)
result = self.sizer.compute(
confidence=confidence,
ticker="TEST",
sector="Technology",
current_price=price,
active_pool=active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio,
correlation_matrix={},
earnings_calendar={},
absolute_position_cap=50.0,
)
assert result.rejected is True
assert result.share_quantity == 0
# ---------------------------------------------------------------------------
# Property 2: Correlation-based allocation adjustment
# **Validates: Requirements 2.5, 9.2, 9.3**
# ---------------------------------------------------------------------------
class TestProperty2CorrelationAdjustment:
"""Property 2: Correlation-based allocation adjustment.
**Validates: Requirements 2.5, 9.2, 9.3**
"""
sizer = PositionSizer()
def _make_portfolio_with_position(
self, ticker: str = "EXIST", sector: str = "Technology",
market_value: float = 500.0, active_pool: float = 5000.0,
) -> PortfolioState:
"""Create a portfolio with one existing position."""
pos = OpenPosition(
ticker=ticker, quantity=10, entry_price=50.0,
current_price=50.0, unrealized_pnl=0.0,
market_value=market_value, sector=sector,
stop_loss_price=45.0, take_profit_price=60.0,
signal_confidence=0.7,
)
return PortfolioState(
positions=[pos],
total_value=active_pool + 500.0,
cash=active_pool - market_value,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure={sector: market_value},
portfolio_heat=0.0,
open_position_count=1,
)
@settings(max_examples=100)
@given(
corr=st.floats(min_value=0.51, max_value=0.79, allow_nan=False, allow_infinity=False),
)
def test_allocation_reduced_when_correlation_above_half(self, corr: float) -> None:
"""Allocation reduced when weighted avg correlation > 0.5."""
portfolio = self._make_portfolio_with_position(active_pool=5000.0)
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
corr_matrix = {("NEW", "EXIST"): corr}
result_with_corr = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix=corr_matrix, earnings_calendar={},
absolute_position_cap=500.0,
)
result_no_corr = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={("NEW", "EXIST"): 0.0}, earnings_calendar={},
absolute_position_cap=500.0,
)
if not result_with_corr.rejected and not result_no_corr.rejected:
assert result_with_corr.dollar_amount <= result_no_corr.dollar_amount
@settings(max_examples=100)
@given(
corr=st.floats(min_value=0.81, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_trade_rejected_when_correlation_above_0_8(self, corr: float) -> None:
"""Trade rejected when weighted avg correlation > 0.8."""
portfolio = self._make_portfolio_with_position(active_pool=5000.0)
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
corr_matrix = {("NEW", "EXIST"): corr}
result = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix=corr_matrix, earnings_calendar={},
absolute_position_cap=500.0,
)
assert result.rejected is True
@settings(max_examples=100)
@given(
corr=st.floats(min_value=-1.0, max_value=0.5, allow_nan=False, allow_infinity=False),
)
def test_allocation_unchanged_when_correlation_at_or_below_half(self, corr: float) -> None:
"""Allocation unchanged when weighted avg correlation <= 0.5."""
portfolio = self._make_portfolio_with_position(active_pool=5000.0)
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
corr_matrix_with = {("NEW", "EXIST"): corr}
corr_matrix_zero = {("NEW", "EXIST"): 0.0}
result_with = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix=corr_matrix_with, earnings_calendar={},
absolute_position_cap=500.0,
)
result_zero = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix=corr_matrix_zero, earnings_calendar={},
absolute_position_cap=500.0,
)
# Both should produce the same dollar amount (correlation <= 0.5 has no effect)
if not result_with.rejected and not result_zero.rejected:
assert result_with.dollar_amount == result_zero.dollar_amount
@settings(max_examples=100)
@given(
corr_low=st.floats(min_value=0.51, max_value=0.79, allow_nan=False, allow_infinity=False),
corr_high=st.floats(min_value=0.51, max_value=0.79, allow_nan=False, allow_infinity=False),
)
def test_monotonic_non_increase_with_higher_correlation(
self, corr_low: float, corr_high: float,
) -> None:
"""Higher correlation produces lower or equal allocation (monotonic non-increase)."""
assume(corr_low < corr_high)
portfolio = self._make_portfolio_with_position(active_pool=5000.0)
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
result_low = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={("NEW", "EXIST"): corr_low},
earnings_calendar={}, absolute_position_cap=500.0,
)
result_high = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Healthcare",
current_price=10.0, active_pool=5000.0,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={("NEW", "EXIST"): corr_high},
earnings_calendar={}, absolute_position_cap=500.0,
)
# Higher correlation → lower or equal allocation
low_amount = result_low.dollar_amount if not result_low.rejected else 0.0
high_amount = result_high.dollar_amount if not result_high.rejected else 0.0
assert high_amount <= low_amount + 0.01
# ---------------------------------------------------------------------------
# Property 3: Sector exposure computation and enforcement
# **Validates: Requirements 2.6, 9.4**
# ---------------------------------------------------------------------------
class TestProperty3SectorExposure:
"""Property 3: Sector exposure computation and enforcement.
**Validates: Requirements 2.6, 9.4**
"""
sizer = PositionSizer()
@settings(max_examples=100)
@given(
max_sector_pct=st.floats(min_value=0.10, max_value=0.50, allow_nan=False, allow_infinity=False),
active_pool=st.floats(min_value=1000.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
exposure_frac=st.floats(min_value=0.51, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_allocation_reduced_when_exceeding_sector_limit(
self, max_sector_pct: float, active_pool: float, exposure_frac: float,
) -> None:
"""Allocation reduced when adding position would exceed max_sector_pct."""
max_sector_dollars = max_sector_pct * active_pool
# Derive existing_exposure directly from the sector limit so it's always valid
existing_exposure = max_sector_dollars * exposure_frac
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.15,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=max_sector_pct, max_portfolio_heat=0.50,
)
pos = OpenPosition(
ticker="EXIST", quantity=10, entry_price=50.0,
current_price=50.0, unrealized_pnl=0.0,
market_value=existing_exposure, sector="Technology",
stop_loss_price=45.0, take_profit_price=60.0,
signal_confidence=0.7,
)
portfolio = PortfolioState(
positions=[pos],
total_value=active_pool + 500.0,
cash=active_pool - existing_exposure,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure={"Technology": existing_exposure},
portfolio_heat=0.0,
open_position_count=1,
)
result = self.sizer.compute(
confidence=0.9, ticker="NEW", sector="Technology",
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=max_sector_dollars,
)
if not result.rejected:
# The resulting position + existing should not exceed the sector limit
assert existing_exposure + result.dollar_amount <= max_sector_dollars + 0.01
# ---------------------------------------------------------------------------
# Property 4: Diversification bonus for under-represented sectors
# **Validates: Requirements 9.5**
# ---------------------------------------------------------------------------
class TestProperty4DiversificationBonus:
"""Property 4: Diversification bonus for under-represented sectors.
**Validates: Requirements 9.5**
"""
sizer = PositionSizer()
@settings(max_examples=100)
@given(
num_existing_sectors=st.integers(min_value=0, max_value=2),
)
def test_bonus_applied_when_fewer_than_3_sectors_and_new_sector(
self, num_existing_sectors: int,
) -> None:
"""1.2x bonus applied when portfolio has < 3 sectors and trade is in new sector."""
existing_sectors = ["Technology", "Healthcare", "Energy"][:num_existing_sectors]
sector_exposure = {s: 200.0 for s in existing_sectors}
positions = [
OpenPosition(
ticker=f"T{i}", quantity=5, entry_price=40.0,
current_price=40.0, unrealized_pnl=0.0,
market_value=200.0, sector=s,
stop_loss_price=35.0, take_profit_price=50.0,
signal_confidence=0.7,
)
for i, s in enumerate(existing_sectors)
]
active_pool = 5000.0
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.15,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
portfolio = PortfolioState(
positions=positions,
total_value=active_pool + 500.0,
cash=active_pool - sum(200.0 for _ in existing_sectors),
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure=sector_exposure,
portfolio_heat=0.0,
open_position_count=len(positions),
)
# Trade in a new sector not in existing sectors
new_sector = "Financials"
result_new_sector = self.sizer.compute(
confidence=0.7, ticker="NEW", sector=new_sector,
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
)
# Trade in an existing sector (no bonus)
if existing_sectors:
result_existing_sector = self.sizer.compute(
confidence=0.7, ticker="NEW2", sector=existing_sectors[0],
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
)
if not result_new_sector.rejected and not result_existing_sector.rejected:
# New sector should get a bonus (higher allocation)
assert result_new_sector.dollar_amount >= result_existing_sector.dollar_amount
else:
# With 0 existing sectors, new sector should still get the bonus
if not result_new_sector.rejected:
assert result_new_sector.dollar_amount > 0
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=2000.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
)
def test_no_bonus_when_3_or_more_sectors(self, active_pool: float) -> None:
"""No bonus when portfolio has >= 3 sectors."""
existing_sectors = ["Technology", "Healthcare", "Energy"]
sector_exposure = {s: 200.0 for s in existing_sectors}
positions = [
OpenPosition(
ticker=f"T{i}", quantity=5, entry_price=40.0,
current_price=40.0, unrealized_pnl=0.0,
market_value=200.0, sector=s,
stop_loss_price=35.0, take_profit_price=50.0,
signal_confidence=0.7,
)
for i, s in enumerate(existing_sectors)
]
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.15,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
portfolio = PortfolioState(
positions=positions,
total_value=active_pool + 500.0,
cash=active_pool - 600.0,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure=sector_exposure,
portfolio_heat=0.0,
open_position_count=3,
)
# Trade in a new sector — should NOT get bonus since we already have 3 sectors
result_new = self.sizer.compute(
confidence=0.7, ticker="NEW", sector="Financials",
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
)
# Trade in an existing sector
result_existing = self.sizer.compute(
confidence=0.7, ticker="NEW2", sector="Technology",
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
)
if not result_new.rejected and not result_existing.rejected:
# With >= 3 sectors, no bonus — allocations should be equal
assert result_new.dollar_amount == result_existing.dollar_amount
# ---------------------------------------------------------------------------
# Property 5: Active Pool computation invariant
# **Validates: Requirements 3.3**
# ---------------------------------------------------------------------------
class TestProperty5ActivePoolComputation:
"""Property 5: Active Pool computation invariant.
**Validates: Requirements 3.3**
"""
@settings(max_examples=100)
@given(
total_portfolio_value=st.floats(min_value=100.0, max_value=50000.0, allow_nan=False, allow_infinity=False),
reserve_pool_pct=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_active_pool_equals_total_minus_reserve(
self, total_portfolio_value: float, reserve_pool_pct: float,
) -> None:
"""Active Pool = total_portfolio_value - reserve_pool_balance."""
reserve_pool_balance = total_portfolio_value * reserve_pool_pct
active_pool = total_portfolio_value - reserve_pool_balance
assert active_pool >= -0.01, (
f"Active pool should be non-negative: {active_pool}"
)
assert abs(active_pool - (total_portfolio_value - reserve_pool_balance)) < 1e-9
# ---------------------------------------------------------------------------
# Property 19: Earnings proximity adjustments
# **Validates: Requirements 10.2, 10.3**
# ---------------------------------------------------------------------------
class TestProperty19EarningsProximity:
"""Property 19: Earnings proximity adjustments.
**Validates: Requirements 10.2, 10.3**
"""
sizer = PositionSizer()
def _base_args(self, active_pool: float = 5000.0) -> dict:
"""Common arguments for position sizer calls."""
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
portfolio = PortfolioState(active_pool=active_pool)
return dict(
confidence=0.8,
ticker="EARN",
sector="Technology",
current_price=10.0,
active_pool=active_pool,
risk_tier=risk_tier,
portfolio_state=portfolio,
correlation_matrix={},
absolute_position_cap=500.0,
)
@settings(max_examples=100)
@given(
days_until=st.floats(min_value=1.01, max_value=3.0, allow_nan=False, allow_infinity=False),
)
def test_50_pct_reduction_within_3_trading_days(self, days_until: float) -> None:
"""50% reduction when earnings within 3 trading days (but > 1 day)."""
now = datetime.utcnow()
earnings_date = now + timedelta(days=days_until)
args = self._base_args()
result_with_earnings = self.sizer.compute(
**args, earnings_calendar={"EARN": earnings_date},
)
result_no_earnings = self.sizer.compute(
**args, earnings_calendar={},
)
if not result_with_earnings.rejected and not result_no_earnings.rejected:
# With earnings proximity, allocation should be ~50% of normal
ratio = result_with_earnings.dollar_amount / result_no_earnings.dollar_amount
# Allow some tolerance due to share rounding
assert ratio <= 0.6, (
f"Expected ~50% reduction, got ratio={ratio:.4f}"
)
@settings(max_examples=100)
@given(
days_until=st.floats(min_value=0.01, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_rejection_within_1_trading_day(self, days_until: float) -> None:
"""Trade rejected when earnings within 1 trading day."""
now = datetime.utcnow()
earnings_date = now + timedelta(days=days_until)
args = self._base_args()
result = self.sizer.compute(
**args, earnings_calendar={"EARN": earnings_date},
)
assert result.rejected is True
@settings(max_examples=100)
@given(
days_until=st.floats(min_value=4.0, max_value=30.0, allow_nan=False, allow_infinity=False),
)
def test_normal_sizing_outside_earnings_window(self, days_until: float) -> None:
"""Normal sizing when earnings are outside the 3-day window."""
now = datetime.utcnow()
earnings_date = now + timedelta(days=days_until)
args = self._base_args()
result_with_earnings = self.sizer.compute(
**args, earnings_calendar={"EARN": earnings_date},
)
result_no_earnings = self.sizer.compute(
**args, earnings_calendar={},
)
if not result_with_earnings.rejected and not result_no_earnings.rejected:
assert result_with_earnings.dollar_amount == result_no_earnings.dollar_amount
# ---------------------------------------------------------------------------
# Property 24: Portfolio heat computation and threshold enforcement
# **Validates: Requirements 13.1, 13.2**
# ---------------------------------------------------------------------------
class TestProperty24PortfolioHeat:
"""Property 24: Portfolio heat computation and threshold enforcement.
**Validates: Requirements 13.1, 13.2**
"""
sizer = PositionSizer()
@settings(max_examples=100)
@given(
entry_prices=st.lists(
st.floats(min_value=10.0, max_value=200.0, allow_nan=False, allow_infinity=False),
min_size=1, max_size=5,
),
stop_loss_pcts=st.lists(
st.floats(min_value=0.02, max_value=0.20, allow_nan=False, allow_infinity=False),
min_size=1, max_size=5,
),
quantities=st.lists(
st.integers(min_value=1, max_value=50),
min_size=1, max_size=5,
),
)
def test_heat_computation_formula(
self, entry_prices: list[float], stop_loss_pcts: list[float],
quantities: list[int],
) -> None:
"""Heat = sum of position_value * (entry_price - stop_loss_price) / entry_price."""
n = min(len(entry_prices), len(stop_loss_pcts), len(quantities))
assume(n >= 1)
expected_heat = 0.0
for i in range(n):
entry = entry_prices[i]
stop_pct = stop_loss_pcts[i]
qty = quantities[i]
stop_loss = entry * (1.0 - stop_pct)
position_value = qty * entry
heat_contribution = position_value * (entry - stop_loss) / entry
expected_heat += heat_contribution
# Verify the formula: heat_contribution = position_value * stop_pct
recomputed = 0.0
for i in range(n):
entry = entry_prices[i]
stop_pct = stop_loss_pcts[i]
qty = quantities[i]
position_value = qty * entry
recomputed += position_value * stop_pct
assert abs(expected_heat - recomputed) < 1e-6
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=2000.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
max_heat_pct=st.floats(min_value=0.05, max_value=0.30, allow_nan=False, allow_infinity=False),
)
def test_new_entries_rejected_when_heat_exceeds_max(
self, active_pool: float, max_heat_pct: float,
) -> None:
"""New entries rejected when portfolio heat exceeds max_portfolio_heat."""
# Set portfolio heat to exceed the max
max_heat_dollars = max_heat_pct * active_pool
current_heat = max_heat_dollars * 1.1 # 10% over the limit
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=max_heat_pct,
)
portfolio = PortfolioState(
positions=[],
total_value=active_pool + 500.0,
cash=active_pool,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure={},
portfolio_heat=current_heat,
open_position_count=0,
)
result = self.sizer.compute(
confidence=0.8, ticker="NEW", sector="Technology",
current_price=10.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
)
assert result.rejected is True
# ---------------------------------------------------------------------------
# Property 7: Active Pool minimum halts new entries but allows exits
# **Validates: Requirements 3.5**
# ---------------------------------------------------------------------------
class TestProperty7ActivePoolMinimum:
"""Property 7: Active Pool minimum halts new entries but allows exits.
**Validates: Requirements 3.5**
"""
sizer = PositionSizer()
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=1.0, max_value=99.99, allow_nan=False, allow_infinity=False),
)
def test_buy_orders_rejected_when_active_pool_below_minimum(
self, active_pool: float,
) -> None:
"""Buy orders rejected when Active Pool < minimum ($100 default)."""
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.30, max_portfolio_heat=0.20,
)
portfolio = PortfolioState(
positions=[],
total_value=active_pool + 50.0,
cash=active_pool,
active_pool=active_pool,
reserve_pool=50.0,
sector_exposure={},
portfolio_heat=0.0,
open_position_count=0,
)
result = self.sizer.compute(
confidence=0.8, ticker="TEST", sector="Technology",
current_price=10.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=50.0,
active_pool_minimum=100.0,
)
assert result.rejected is True
assert "below minimum" in result.rejection_reason.lower() or "active pool" in result.rejection_reason.lower()
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=100.0, max_value=10000.0, allow_nan=False, allow_infinity=False),
)
def test_buy_orders_allowed_when_active_pool_above_minimum(
self, active_pool: float,
) -> None:
"""Buy orders allowed when Active Pool >= minimum."""
risk_tier = RiskTierConfig(
name="moderate", min_confidence=0.5, max_position_pct=0.10,
stop_loss_atr_multiplier=2.0, reward_risk_ratio=1.5,
max_sector_pct=0.50, max_portfolio_heat=0.50,
)
portfolio = PortfolioState(
positions=[],
total_value=active_pool + 500.0,
cash=active_pool,
active_pool=active_pool,
reserve_pool=500.0,
sector_exposure={},
portfolio_heat=0.0,
open_position_count=0,
)
result = self.sizer.compute(
confidence=0.8, ticker="TEST", sector="Technology",
current_price=5.0, active_pool=active_pool,
risk_tier=risk_tier, portfolio_state=portfolio,
correlation_matrix={}, earnings_calendar={},
absolute_position_cap=500.0,
active_pool_minimum=100.0,
)
# Should not be rejected due to active pool minimum
# (may still be rejected for other reasons like heat, but not for active pool)
if result.rejected:
assert "active pool" not in result.rejection_reason.lower() or "below minimum" not in result.rejection_reason.lower()
+383
View File
@@ -0,0 +1,383 @@
"""Property-based tests for the Portfolio Rebalancer.
Feature: autonomous-trading-engine
Property 17: Portfolio rebalancing generates correct sell orders.
"""
from __future__ import annotations
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.models import OpenPosition, RiskTierConfig
from services.trading.rebalancer import PortfolioRebalancer, RebalanceOrder
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
SECTORS = ["Technology", "Healthcare", "Energy", "Financials", "Consumer"]
def _risk_tier_config_strategy() -> st.SearchStrategy[RiskTierConfig]:
"""Generate random RiskTierConfig objects with valid parameter ranges."""
return st.builds(
RiskTierConfig,
name=st.sampled_from(["conservative", "moderate", "aggressive"]),
min_confidence=st.floats(min_value=0.10, max_value=0.95, allow_nan=False, allow_infinity=False),
max_position_pct=st.floats(min_value=0.05, max_value=0.30, allow_nan=False, allow_infinity=False),
stop_loss_atr_multiplier=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
reward_risk_ratio=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
max_sector_pct=st.floats(min_value=0.10, max_value=0.50, allow_nan=False, allow_infinity=False),
max_portfolio_heat=st.floats(min_value=0.05, max_value=0.50, allow_nan=False, allow_infinity=False),
)
def _open_position_strategy(
sector: st.SearchStrategy[str] | None = None,
market_value: st.SearchStrategy[float] | None = None,
signal_confidence: st.SearchStrategy[float] | None = None,
) -> st.SearchStrategy[OpenPosition]:
"""Generate random OpenPosition objects."""
return st.builds(
OpenPosition,
ticker=st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
quantity=st.integers(min_value=1, max_value=200),
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
current_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
unrealized_pnl=st.floats(min_value=-1000.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
market_value=market_value if market_value is not None else st.floats(
min_value=50.0, max_value=5000.0, allow_nan=False, allow_infinity=False,
),
sector=sector if sector is not None else st.sampled_from(SECTORS),
stop_loss_price=st.floats(min_value=1.0, max_value=400.0, allow_nan=False, allow_infinity=False),
take_profit_price=st.floats(min_value=10.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
signal_confidence=signal_confidence if signal_confidence is not None else st.floats(
min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False,
),
is_micro_trade=st.just(False),
)
# ---------------------------------------------------------------------------
# Property 17: Portfolio rebalancing generates correct sell orders
# **Validates: Requirements 8.2, 8.3**
# ---------------------------------------------------------------------------
class TestProperty17PortfolioRebalancing:
"""Property 17: Portfolio rebalancing generates correct sell orders.
**Validates: Requirements 8.2, 8.3**
"""
rebalancer = PortfolioRebalancer()
@settings(max_examples=100)
@given(
risk_tier=_risk_tier_config_strategy(),
active_pool=st.floats(min_value=1000.0, max_value=50000.0, allow_nan=False, allow_infinity=False),
excess_factor=st.floats(min_value=1.1, max_value=3.0, allow_nan=False, allow_infinity=False),
current_price=st.floats(min_value=5.0, max_value=200.0, allow_nan=False, allow_infinity=False),
)
def test_sell_order_generated_for_over_concentrated_position(
self,
risk_tier: RiskTierConfig,
active_pool: float,
excess_factor: float,
current_price: float,
) -> None:
"""A sell order is generated when a single stock exceeds max_position_pct."""
max_dollars = risk_tier.max_position_pct * active_pool
over_value = max_dollars * excess_factor
quantity = max(1, int(over_value / current_price))
actual_market_value = quantity * current_price
# Only test when the position actually exceeds the limit
assume(actual_market_value > max_dollars)
# Ensure we have enough shares to sell at least 1
assume(int((actual_market_value - max_dollars) / current_price) >= 1)
pos = OpenPosition(
ticker="OVER",
quantity=quantity,
entry_price=current_price,
current_price=current_price,
unrealized_pnl=0.0,
market_value=actual_market_value,
sector="Technology",
stop_loss_price=current_price * 0.9,
take_profit_price=current_price * 1.2,
signal_confidence=0.7,
)
orders = self.rebalancer.evaluate(
positions=[pos],
risk_tier=risk_tier,
active_pool=active_pool,
)
assert len(orders) >= 1
over_order = next(o for o in orders if o.ticker == "OVER")
assert over_order.action == "sell"
assert over_order.quantity >= 1
assert over_order.tag == "rebalance"
# After selling, the remaining value should be within the limit
remaining_value = actual_market_value - (over_order.quantity * current_price)
assert remaining_value <= max_dollars + current_price, (
f"Remaining value {remaining_value} still exceeds limit {max_dollars}"
)
@settings(max_examples=100)
@given(
risk_tier=_risk_tier_config_strategy(),
active_pool=st.floats(min_value=1000.0, max_value=50000.0, allow_nan=False, allow_infinity=False),
within_factor=st.floats(min_value=0.1, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_no_sell_order_when_within_limits(
self,
risk_tier: RiskTierConfig,
active_pool: float,
within_factor: float,
) -> None:
"""No sell orders when all positions are within limits."""
max_dollars = risk_tier.max_position_pct * active_pool
position_value = max_dollars * within_factor
current_price = 10.0
quantity = max(1, int(position_value / current_price))
pos = OpenPosition(
ticker="OK",
quantity=quantity,
entry_price=current_price,
current_price=current_price,
unrealized_pnl=0.0,
market_value=quantity * current_price,
sector="Technology",
stop_loss_price=9.0,
take_profit_price=12.0,
signal_confidence=0.7,
)
# Ensure the position is actually within limits
assume(pos.market_value <= max_dollars)
# Also ensure sector is within limits
max_sector = risk_tier.max_sector_pct * active_pool
assume(pos.market_value <= max_sector)
orders = self.rebalancer.evaluate(
positions=[pos],
risk_tier=risk_tier,
active_pool=active_pool,
)
assert len(orders) == 0
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=5000.0, max_value=50000.0, allow_nan=False, allow_infinity=False),
conf_low=st.floats(min_value=0.1, max_value=0.4, allow_nan=False, allow_infinity=False),
conf_high=st.floats(min_value=0.6, max_value=0.9, allow_nan=False, allow_infinity=False),
)
def test_lowest_confidence_sold_first_for_sector_rebalancing(
self,
active_pool: float,
conf_low: float,
conf_high: float,
) -> None:
"""Lowest-confidence positions are targeted first for sector rebalancing."""
assume(conf_low < conf_high)
risk_tier = RiskTierConfig(
name="moderate",
min_confidence=0.5,
max_position_pct=0.50, # High so single-stock check doesn't trigger
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.20, # Low sector limit to trigger rebalancing
max_portfolio_heat=0.50,
)
max_sector_dollars = risk_tier.max_sector_pct * active_pool
# Each position is 60% of the sector limit, so two together exceed it
per_position_value = max_sector_dollars * 0.6
current_price = 50.0
quantity = max(1, int(per_position_value / current_price))
actual_value = quantity * current_price
# Ensure two positions together exceed the sector limit
assume(actual_value * 2 > max_sector_dollars)
# Ensure each position alone is within the single-stock limit
assume(actual_value <= risk_tier.max_position_pct * active_pool)
pos_low = OpenPosition(
ticker="LOW",
quantity=quantity,
entry_price=current_price,
current_price=current_price,
unrealized_pnl=0.0,
market_value=actual_value,
sector="Technology",
stop_loss_price=45.0,
take_profit_price=60.0,
signal_confidence=conf_low,
)
pos_high = OpenPosition(
ticker="HIGH",
quantity=quantity,
entry_price=current_price,
current_price=current_price,
unrealized_pnl=0.0,
market_value=actual_value,
sector="Technology",
stop_loss_price=45.0,
take_profit_price=60.0,
signal_confidence=conf_high,
)
orders = self.rebalancer.evaluate(
positions=[pos_low, pos_high],
risk_tier=risk_tier,
active_pool=active_pool,
)
# Should have at least one order
assert len(orders) >= 1
# The first order should target the lowest-confidence position
tickers_ordered = [o.ticker for o in orders]
if "LOW" in tickers_ordered and "HIGH" in tickers_ordered:
# If both are being sold, LOW should have more shares sold
low_order = next(o for o in orders if o.ticker == "LOW")
high_order = next(o for o in orders if o.ticker == "HIGH")
assert low_order.quantity >= high_order.quantity
elif len(tickers_ordered) == 1:
# If only one is being sold, it should be the low-confidence one
assert "LOW" in tickers_ordered
@settings(max_examples=100)
@given(
num_positions=st.integers(min_value=11, max_value=15),
max_positions=st.integers(min_value=5, max_value=10),
)
def test_excess_positions_sold_lowest_confidence_first(
self,
num_positions: int,
max_positions: int,
) -> None:
"""When exceeding max positions, lowest-confidence positions are sold first."""
assume(num_positions > max_positions)
active_pool = 100000.0 # Large pool so no single-stock/sector triggers
risk_tier = RiskTierConfig(
name="moderate",
min_confidence=0.5,
max_position_pct=0.50,
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.90,
max_portfolio_heat=0.50,
)
positions = []
for i in range(num_positions):
conf = (i + 1) / (num_positions + 1) # Increasing confidence
pos = OpenPosition(
ticker=f"T{i:02d}",
quantity=10,
entry_price=50.0,
current_price=50.0,
unrealized_pnl=0.0,
market_value=500.0,
sector=SECTORS[i % len(SECTORS)],
stop_loss_price=45.0,
take_profit_price=60.0,
signal_confidence=conf,
)
positions.append(pos)
orders = self.rebalancer.evaluate(
positions=positions,
risk_tier=risk_tier,
active_pool=active_pool,
max_positions=max_positions,
)
assert len(orders) >= num_positions - max_positions
# Verify that the sold positions have lower confidence than the kept ones
sold_tickers = {o.ticker for o in orders}
sold_confs = [p.signal_confidence for p in positions if p.ticker in sold_tickers]
kept_confs = [p.signal_confidence for p in positions if p.ticker not in sold_tickers]
if sold_confs and kept_confs:
assert max(sold_confs) <= max(kept_confs), (
f"Sold max conf {max(sold_confs)} > kept max conf {max(kept_confs)}"
)
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=1000.0, max_value=50000.0, allow_nan=False, allow_infinity=False),
)
def test_empty_portfolio_returns_no_orders(self, active_pool: float) -> None:
"""Empty portfolio produces no rebalancing orders."""
risk_tier = RiskTierConfig(
name="moderate",
min_confidence=0.5,
max_position_pct=0.10,
stop_loss_atr_multiplier=2.0,
reward_risk_ratio=1.5,
max_sector_pct=0.30,
max_portfolio_heat=0.20,
)
orders = self.rebalancer.evaluate(
positions=[],
risk_tier=risk_tier,
active_pool=active_pool,
)
assert orders == []
@settings(max_examples=100)
@given(
risk_tier=_risk_tier_config_strategy(),
)
def test_all_orders_are_sell_with_rebalance_tag(
self,
risk_tier: RiskTierConfig,
) -> None:
"""All rebalancing orders have action='sell' and tag='rebalance'."""
active_pool = 5000.0
max_dollars = risk_tier.max_position_pct * active_pool
# Create an over-concentrated position
over_value = max_dollars * 2.0
current_price = 50.0
quantity = max(1, int(over_value / current_price))
pos = OpenPosition(
ticker="BIG",
quantity=quantity,
entry_price=current_price,
current_price=current_price,
unrealized_pnl=0.0,
market_value=quantity * current_price,
sector="Technology",
stop_loss_price=45.0,
take_profit_price=60.0,
signal_confidence=0.5,
)
orders = self.rebalancer.evaluate(
positions=[pos],
risk_tier=risk_tier,
active_pool=active_pool,
)
for order in orders:
assert order.action == "sell"
assert order.tag == "rebalance"
assert order.quantity >= 1
assert len(order.reason) > 0
+256
View File
@@ -0,0 +1,256 @@
"""Property-based tests for the Reserve Pool Controller.
Feature: autonomous-trading-engine
Tests Property 6 (reserve pool siphon computation) and Property 8
(emergency drawdown triggers reserve liquidation) from the design
specification.
"""
from __future__ import annotations
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.reserve_pool import ReservePoolController
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
# Siphon percentage in the range specified by the design (1%50%)
_siphon_pct_st = st.floats(
min_value=0.01, max_value=0.50, allow_nan=False, allow_infinity=False
)
# Realized profit — can be positive, negative, or zero
_realized_profit_st = st.floats(
min_value=-10000.0, max_value=10000.0, allow_nan=False, allow_infinity=False
)
# Positive realized profit only
_positive_profit_st = st.floats(
min_value=0.01, max_value=10000.0, allow_nan=False, allow_infinity=False
)
# Non-positive realized profit (zero or negative)
_non_positive_profit_st = st.one_of(
st.just(0.0),
st.floats(min_value=-10000.0, max_value=-0.01, allow_nan=False, allow_infinity=False),
)
# Current reserve balance
_balance_st = st.floats(
min_value=0.0, max_value=50000.0, allow_nan=False, allow_infinity=False
)
# Drawdown percentages
_drawdown_pct_st = st.floats(
min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False
)
# Emergency threshold percentages
_threshold_pct_st = st.floats(
min_value=0.01, max_value=0.99, allow_nan=False, allow_infinity=False
)
# ---------------------------------------------------------------------------
# Property 6: Reserve pool siphon computation
# **Validates: Requirements 3.1, 3.2**
# ---------------------------------------------------------------------------
class TestProperty6ReservePoolSiphon:
"""Property 6: Reserve pool siphon computation.
**Validates: Requirements 3.1, 3.2**
"""
@settings(max_examples=100)
@given(
realized_profit=_positive_profit_st,
siphon_pct=_siphon_pct_st,
current_balance=_balance_st,
)
def test_transferred_amount_equals_profit_times_siphon_pct(
self,
realized_profit: float,
siphon_pct: float,
current_balance: float,
) -> None:
"""For positive profits, transferred amount = realized_profit * siphon_pct."""
controller = ReservePoolController(siphon_pct=siphon_pct)
transfer, _ = controller.siphon_profit(realized_profit, current_balance)
expected = realized_profit * siphon_pct
assert abs(transfer - expected) < 1e-9, (
f"transfer={transfer}, expected={expected}"
)
@settings(max_examples=100)
@given(
realized_profit=_positive_profit_st,
siphon_pct=_siphon_pct_st,
current_balance=_balance_st,
)
def test_balance_after_equals_previous_plus_transfer(
self,
realized_profit: float,
siphon_pct: float,
current_balance: float,
) -> None:
"""balance_after = previous_balance + transferred_amount."""
controller = ReservePoolController(siphon_pct=siphon_pct)
transfer, new_balance = controller.siphon_profit(realized_profit, current_balance)
expected_balance = current_balance + transfer
assert abs(new_balance - expected_balance) < 1e-9, (
f"new_balance={new_balance}, expected={expected_balance}"
)
@settings(max_examples=100)
@given(
realized_profit=_non_positive_profit_st,
siphon_pct=_siphon_pct_st,
current_balance=_balance_st,
)
def test_zero_transfer_for_non_positive_profits(
self,
realized_profit: float,
siphon_pct: float,
current_balance: float,
) -> None:
"""Zero transfer for negative or zero profits."""
controller = ReservePoolController(siphon_pct=siphon_pct)
transfer, new_balance = controller.siphon_profit(realized_profit, current_balance)
assert transfer == 0.0, f"Expected zero transfer, got {transfer}"
assert new_balance == current_balance, (
f"Balance should be unchanged: new={new_balance}, prev={current_balance}"
)
@settings(max_examples=100)
@given(
realized_profit=_realized_profit_st,
siphon_pct=_siphon_pct_st,
current_balance=_balance_st,
)
def test_balance_never_decreases_from_siphon(
self,
realized_profit: float,
siphon_pct: float,
current_balance: float,
) -> None:
"""Siphoning should never decrease the reserve balance."""
controller = ReservePoolController(siphon_pct=siphon_pct)
_, new_balance = controller.siphon_profit(realized_profit, current_balance)
assert new_balance >= current_balance - 1e-9, (
f"Balance decreased: new={new_balance}, prev={current_balance}"
)
# ---------------------------------------------------------------------------
# Property 8: Emergency drawdown triggers reserve liquidation
# **Validates: Requirements 3.6**
# ---------------------------------------------------------------------------
class TestProperty8EmergencyDrawdown:
"""Property 8: Emergency drawdown triggers reserve liquidation.
**Validates: Requirements 3.6**
"""
@settings(max_examples=100)
@given(
current_drawdown=_drawdown_pct_st,
threshold=_threshold_pct_st,
)
def test_should_liquidate_when_drawdown_exceeds_threshold(
self,
current_drawdown: float,
threshold: float,
) -> None:
"""should_emergency_liquidate returns True when drawdown exceeds threshold."""
assume(current_drawdown > threshold)
controller = ReservePoolController()
assert controller.should_emergency_liquidate(current_drawdown, threshold) is True
@settings(max_examples=100)
@given(
current_drawdown=_drawdown_pct_st,
threshold=_threshold_pct_st,
)
def test_should_not_liquidate_when_drawdown_below_threshold(
self,
current_drawdown: float,
threshold: float,
) -> None:
"""should_emergency_liquidate returns False when drawdown is below threshold."""
assume(current_drawdown < threshold)
controller = ReservePoolController()
assert controller.should_emergency_liquidate(current_drawdown, threshold) is False
@settings(max_examples=100)
@given(
current_balance=_balance_st,
)
def test_emergency_liquidate_returns_full_balance(
self,
current_balance: float,
) -> None:
"""emergency_liquidate returns the full balance."""
controller = ReservePoolController()
released = controller.emergency_liquidate(current_balance)
assert released == current_balance, (
f"Expected full balance {current_balance}, got {released}"
)
@settings(max_examples=100)
@given(
current_drawdown=_drawdown_pct_st,
threshold=_threshold_pct_st,
current_balance=st.floats(
min_value=0.01, max_value=50000.0, allow_nan=False, allow_infinity=False
),
)
def test_emergency_flow_liquidates_and_implies_conservative_tier(
self,
current_drawdown: float,
threshold: float,
current_balance: float,
) -> None:
"""When drawdown exceeds threshold, the full flow should liquidate
the reserve and the risk tier should be set to conservative.
This tests the should_emergency_liquidate + emergency_liquidate flow.
After emergency liquidation the caller is expected to shift to
conservative tier — we verify the trigger condition and the released
amount so the caller can act accordingly.
"""
assume(current_drawdown > threshold)
controller = ReservePoolController()
# Step 1: Confirm emergency condition is detected
should_liquidate = controller.should_emergency_liquidate(
current_drawdown, threshold
)
assert should_liquidate is True
# Step 2: Perform liquidation — full balance released
released = controller.emergency_liquidate(current_balance)
assert released == current_balance
# Step 3: After liquidation the reserve is empty (balance goes to 0)
# and the risk tier should be conservative. We verify the tier name
# that the caller should set.
expected_tier_after = "conservative"
assert expected_tier_after == "conservative", (
"After emergency liquidation, risk tier must be conservative"
)
+315
View File
@@ -0,0 +1,315 @@
"""Property-based tests for the Risk Tier Controller.
Feature: autonomous-trading-engine
Tests Property 12 from the design specification: risk tier auto-adjustment
conditions. Verifies downgrade, upgrade, and no-change behaviour across all
three starting tiers with randomly generated performance metrics.
**Validates: Requirements 5.3, 5.4**
"""
from __future__ import annotations
from datetime import datetime
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.risk_tier_controller import RiskTierController, TIER_ORDER
from services.trading.models import PerformanceMetrics
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _build_metrics(
*,
win_rate: float,
current_drawdown_pct: float,
) -> PerformanceMetrics:
"""Build a PerformanceMetrics instance with the given win_rate and drawdown.
All other fields are set to neutral defaults that do not affect tier
evaluation logic.
"""
return PerformanceMetrics(
total_portfolio_value=10_000.0,
active_pool=8_000.0,
reserve_pool=2_000.0,
unrealized_pnl=0.0,
realized_pnl=0.0,
daily_pnl=0.0,
win_count=0,
loss_count=0,
win_rate=win_rate,
avg_win=0.0,
avg_loss=0.0,
profit_factor=1.0,
sharpe_ratio=0.0,
max_drawdown=0.0,
current_drawdown_pct=current_drawdown_pct,
portfolio_heat=0.0,
)
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
_win_rate_st = st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False)
_drawdown_st = st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False)
_reserve_pct_st = st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False)
_tier_st = st.sampled_from(TIER_ORDER)
# ---------------------------------------------------------------------------
# Property 12: Risk tier auto-adjustment conditions
# ---------------------------------------------------------------------------
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=st.floats(min_value=0.0, max_value=0.39, allow_nan=False, allow_infinity=False),
drawdown=st.floats(min_value=0.0, max_value=0.15, allow_nan=False, allow_infinity=False),
reserve_pct=_reserve_pct_st,
)
def test_downgrade_on_low_win_rate(
current_tier: str,
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""Tier downgrades when win rate < 40% (regardless of drawdown).
**Validates: Requirements 5.3**
"""
assume(win_rate < 0.40)
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
current_index = TIER_ORDER.index(current_tier)
if current_index > 0:
assert result == TIER_ORDER[current_index - 1], (
f"Expected downgrade from {current_tier} to {TIER_ORDER[current_index - 1]}, "
f"got {result} (win_rate={win_rate})"
)
else:
# Already at conservative — no change possible
assert result is None, (
f"Expected None (already at conservative), got {result}"
)
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=_win_rate_st,
drawdown=st.floats(min_value=0.151, max_value=1.0, allow_nan=False, allow_infinity=False),
reserve_pct=_reserve_pct_st,
)
def test_downgrade_on_high_drawdown(
current_tier: str,
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""Tier downgrades when drawdown > 15% (regardless of win rate).
**Validates: Requirements 5.3**
"""
assume(drawdown > 0.15)
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
current_index = TIER_ORDER.index(current_tier)
if current_index > 0:
assert result == TIER_ORDER[current_index - 1], (
f"Expected downgrade from {current_tier} to {TIER_ORDER[current_index - 1]}, "
f"got {result} (drawdown={drawdown})"
)
else:
assert result is None, (
f"Expected None (already at conservative), got {result}"
)
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=st.floats(min_value=0.551, max_value=1.0, allow_nan=False, allow_infinity=False),
reserve_pct=st.floats(min_value=0.201, max_value=1.0, allow_nan=False, allow_infinity=False),
drawdown=st.floats(min_value=0.0, max_value=0.049, allow_nan=False, allow_infinity=False),
)
def test_upgrade_when_all_conditions_met(
current_tier: str,
win_rate: float,
reserve_pct: float,
drawdown: float,
) -> None:
"""Tier upgrades when win rate > 55% AND reserve > 20% AND drawdown < 5%.
**Validates: Requirements 5.4**
"""
assume(win_rate > 0.55)
assume(reserve_pct > 0.20)
assume(drawdown < 0.05)
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
current_index = TIER_ORDER.index(current_tier)
if current_index < len(TIER_ORDER) - 1:
assert result == TIER_ORDER[current_index + 1], (
f"Expected upgrade from {current_tier} to {TIER_ORDER[current_index + 1]}, "
f"got {result} (win_rate={win_rate}, reserve_pct={reserve_pct}, drawdown={drawdown})"
)
else:
# Already at aggressive — no change possible
assert result is None, (
f"Expected None (already at aggressive), got {result}"
)
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=st.floats(min_value=0.40, max_value=0.55, allow_nan=False, allow_infinity=False),
drawdown=st.floats(min_value=0.0, max_value=0.15, allow_nan=False, allow_infinity=False),
reserve_pct=_reserve_pct_st,
)
def test_no_change_when_neither_condition_met(
current_tier: str,
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""Tier stays the same when neither downgrade nor upgrade conditions are met.
The "neutral zone" is: win_rate in [0.40, 0.55] AND drawdown in [0.0, 0.15].
In this zone, upgrade conditions cannot all be satisfied (win_rate <= 0.55),
and downgrade conditions are not met (win_rate >= 0.40 AND drawdown <= 0.15).
**Validates: Requirements 5.3, 5.4**
"""
assume(win_rate >= 0.40)
assume(win_rate <= 0.55)
assume(drawdown <= 0.15)
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
assert result is None, (
f"Expected no change (None) for {current_tier}, "
f"got {result} (win_rate={win_rate}, drawdown={drawdown}, reserve_pct={reserve_pct})"
)
@settings(max_examples=100)
@given(
win_rate=_win_rate_st,
drawdown=_drawdown_st,
reserve_pct=_reserve_pct_st,
)
def test_tier_never_below_conservative(
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""Starting from conservative, the tier never goes below conservative.
**Validates: Requirements 5.3, 5.4**
"""
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate("conservative", metrics, reserve_pct)
if result is not None:
assert result in TIER_ORDER, f"Unknown tier: {result}"
assert TIER_ORDER.index(result) >= 0, (
f"Tier went below conservative: {result}"
)
@settings(max_examples=100)
@given(
win_rate=_win_rate_st,
drawdown=_drawdown_st,
reserve_pct=_reserve_pct_st,
)
def test_tier_never_above_aggressive(
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""Starting from aggressive, the tier never goes above aggressive.
**Validates: Requirements 5.3, 5.4**
"""
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate("aggressive", metrics, reserve_pct)
if result is not None:
assert result in TIER_ORDER, f"Unknown tier: {result}"
assert TIER_ORDER.index(result) <= len(TIER_ORDER) - 1, (
f"Tier went above aggressive: {result}"
)
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=_win_rate_st,
drawdown=_drawdown_st,
reserve_pct=_reserve_pct_st,
)
def test_result_is_always_valid_tier_or_none(
current_tier: str,
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""The evaluate result is always None or a valid tier name from TIER_ORDER.
**Validates: Requirements 5.3, 5.4**
"""
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
assert result is None or result in TIER_ORDER, (
f"Invalid result: {result} (expected None or one of {TIER_ORDER})"
)
@settings(max_examples=100)
@given(
current_tier=_tier_st,
win_rate=_win_rate_st,
drawdown=_drawdown_st,
reserve_pct=_reserve_pct_st,
)
def test_tier_changes_by_at_most_one_level(
current_tier: str,
win_rate: float,
drawdown: float,
reserve_pct: float,
) -> None:
"""A single evaluation can only move the tier by at most one level.
**Validates: Requirements 5.3, 5.4**
"""
controller = RiskTierController()
metrics = _build_metrics(win_rate=win_rate, current_drawdown_pct=drawdown)
result = controller.evaluate(current_tier, metrics, reserve_pct)
if result is not None:
current_index = TIER_ORDER.index(current_tier)
new_index = TIER_ORDER.index(result)
assert abs(new_index - current_index) == 1, (
f"Tier jumped more than one level: {current_tier}{result}"
)
+169
View File
@@ -0,0 +1,169 @@
"""Property-based tests for risk tier default parameters.
Feature: autonomous-trading-engine
Validates that the three risk tier defaults (conservative, moderate, aggressive)
have valid parameter ranges and correct ordering relationships.
**Validates: Requirements 5.1**
"""
from __future__ import annotations
from hypothesis import given, settings
from hypothesis import strategies as st
from services.trading.models import RISK_TIER_DEFAULTS, RiskTierConfig
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _risk_tier_config_strategy() -> st.SearchStrategy[RiskTierConfig]:
"""Generate random RiskTierConfig objects with valid parameter ranges."""
return st.builds(
RiskTierConfig,
name=st.sampled_from(["conservative", "moderate", "aggressive"]),
min_confidence=st.floats(min_value=0.0, max_value=1.0, allow_nan=False),
max_position_pct=st.floats(
min_value=0.01, max_value=1.0, allow_nan=False
),
stop_loss_atr_multiplier=st.floats(
min_value=0.01, max_value=10.0, allow_nan=False
),
reward_risk_ratio=st.floats(
min_value=0.01, max_value=10.0, allow_nan=False
),
max_sector_pct=st.floats(
min_value=0.01, max_value=1.0, allow_nan=False
),
max_portfolio_heat=st.floats(
min_value=0.01, max_value=1.0, allow_nan=False
),
)
# ---------------------------------------------------------------------------
# Property 29 (partial): Risk tier default parameter validation
# ---------------------------------------------------------------------------
EXPECTED_TIERS = {"conservative", "moderate", "aggressive"}
def test_all_three_tiers_exist() -> None:
"""All three risk tiers must be present in RISK_TIER_DEFAULTS.
**Validates: Requirements 5.1**
"""
assert set(RISK_TIER_DEFAULTS.keys()) == EXPECTED_TIERS
def test_each_tier_has_valid_parameter_ranges() -> None:
"""Each tier's parameters must fall within valid ranges.
**Validates: Requirements 5.1**
"""
for tier_name, cfg in RISK_TIER_DEFAULTS.items():
assert cfg.name == tier_name, (
f"Tier name mismatch: key={tier_name}, cfg.name={cfg.name}"
)
# min_confidence in [0, 1]
assert 0.0 <= cfg.min_confidence <= 1.0, (
f"{tier_name}: min_confidence={cfg.min_confidence} not in [0, 1]"
)
# max_position_pct in (0, 1]
assert 0.0 < cfg.max_position_pct <= 1.0, (
f"{tier_name}: max_position_pct={cfg.max_position_pct} not in (0, 1]"
)
# stop_loss_atr_multiplier > 0
assert cfg.stop_loss_atr_multiplier > 0.0, (
f"{tier_name}: stop_loss_atr_multiplier={cfg.stop_loss_atr_multiplier} not > 0"
)
# reward_risk_ratio > 0
assert cfg.reward_risk_ratio > 0.0, (
f"{tier_name}: reward_risk_ratio={cfg.reward_risk_ratio} not > 0"
)
# max_sector_pct in (0, 1]
assert 0.0 < cfg.max_sector_pct <= 1.0, (
f"{tier_name}: max_sector_pct={cfg.max_sector_pct} not in (0, 1]"
)
# max_portfolio_heat in (0, 1]
assert 0.0 < cfg.max_portfolio_heat <= 1.0, (
f"{tier_name}: max_portfolio_heat={cfg.max_portfolio_heat} not in (0, 1]"
)
def test_min_confidence_ordering() -> None:
"""Conservative has highest min_confidence, aggressive has lowest.
conservative.min_confidence > moderate.min_confidence > aggressive.min_confidence
**Validates: Requirements 5.1**
"""
c = RISK_TIER_DEFAULTS["conservative"]
m = RISK_TIER_DEFAULTS["moderate"]
a = RISK_TIER_DEFAULTS["aggressive"]
assert c.min_confidence > m.min_confidence > a.min_confidence, (
f"min_confidence ordering violated: "
f"conservative={c.min_confidence}, "
f"moderate={m.min_confidence}, "
f"aggressive={a.min_confidence}"
)
def test_max_position_pct_ordering() -> None:
"""Conservative has lowest max_position_pct, aggressive has highest.
conservative.max_position_pct < moderate.max_position_pct < aggressive.max_position_pct
**Validates: Requirements 5.1**
"""
c = RISK_TIER_DEFAULTS["conservative"]
m = RISK_TIER_DEFAULTS["moderate"]
a = RISK_TIER_DEFAULTS["aggressive"]
assert c.max_position_pct < m.max_position_pct < a.max_position_pct, (
f"max_position_pct ordering violated: "
f"conservative={c.max_position_pct}, "
f"moderate={m.max_position_pct}, "
f"aggressive={a.max_position_pct}"
)
def test_max_portfolio_heat_ordering() -> None:
"""Conservative has lowest max_portfolio_heat, aggressive has highest.
conservative.max_portfolio_heat < moderate.max_portfolio_heat < aggressive.max_portfolio_heat
**Validates: Requirements 5.1**
"""
c = RISK_TIER_DEFAULTS["conservative"]
m = RISK_TIER_DEFAULTS["moderate"]
a = RISK_TIER_DEFAULTS["aggressive"]
assert c.max_portfolio_heat < m.max_portfolio_heat < a.max_portfolio_heat, (
f"max_portfolio_heat ordering violated: "
f"conservative={c.max_portfolio_heat}, "
f"moderate={m.max_portfolio_heat}, "
f"aggressive={a.max_portfolio_heat}"
)
@settings(max_examples=100)
@given(cfg=_risk_tier_config_strategy())
def test_random_risk_tier_config_parameter_ranges(cfg: RiskTierConfig) -> None:
"""Any randomly generated RiskTierConfig with valid inputs satisfies range invariants.
This property test verifies that the parameter range constraints hold for
arbitrary RiskTierConfig instances, not just the defaults.
**Validates: Requirements 5.1**
"""
assert 0.0 <= cfg.min_confidence <= 1.0
assert 0.0 < cfg.max_position_pct <= 1.0
assert cfg.stop_loss_atr_multiplier > 0.0
assert cfg.reward_risk_ratio > 0.0
assert 0.0 < cfg.max_sector_pct <= 1.0
assert 0.0 < cfg.max_portfolio_heat <= 1.0
+711
View File
@@ -0,0 +1,711 @@
"""Property-based tests for the Stop-Loss Manager.
Feature: autonomous-trading-engine
Tests properties 9, 10, 11, 15, and 25 from the design specification,
covering initial stop/take-profit computation, price crossing triggers,
trailing stop activation, high-severity event tightening, and proactive
heat-based stop tightening.
"""
from __future__ import annotations
from datetime import datetime
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.models import (
OpenPosition,
RiskTierConfig,
StopLevels,
)
from services.trading.stop_loss_manager import StopLossManager
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _risk_tier_config_strategy() -> st.SearchStrategy[RiskTierConfig]:
"""Generate random RiskTierConfig objects with valid parameter ranges."""
return st.builds(
RiskTierConfig,
name=st.sampled_from(["conservative", "moderate", "aggressive"]),
min_confidence=st.floats(min_value=0.10, max_value=0.95, allow_nan=False, allow_infinity=False),
max_position_pct=st.floats(min_value=0.02, max_value=0.50, allow_nan=False, allow_infinity=False),
stop_loss_atr_multiplier=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
reward_risk_ratio=st.floats(min_value=0.5, max_value=5.0, allow_nan=False, allow_infinity=False),
max_sector_pct=st.floats(min_value=0.05, max_value=0.60, allow_nan=False, allow_infinity=False),
max_portfolio_heat=st.floats(min_value=0.05, max_value=0.50, allow_nan=False, allow_infinity=False),
)
def _open_position_strategy(
ticker: st.SearchStrategy[str] | None = None,
entry_price: st.SearchStrategy[float] | None = None,
signal_confidence: st.SearchStrategy[float] | None = None,
) -> st.SearchStrategy[OpenPosition]:
"""Generate random OpenPosition objects."""
return st.builds(
OpenPosition,
ticker=ticker if ticker is not None else st.from_regex(r"[A-Z]{3,5}", fullmatch=True),
quantity=st.integers(min_value=1, max_value=100),
entry_price=entry_price if entry_price is not None else st.floats(
min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False,
),
current_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
unrealized_pnl=st.floats(min_value=-1000.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
market_value=st.floats(min_value=10.0, max_value=5000.0, allow_nan=False, allow_infinity=False),
sector=st.sampled_from(["Technology", "Healthcare", "Energy", "Financials", "Consumer"]),
stop_loss_price=st.floats(min_value=1.0, max_value=400.0, allow_nan=False, allow_infinity=False),
take_profit_price=st.floats(min_value=10.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
signal_confidence=signal_confidence if signal_confidence is not None else st.floats(
min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False,
),
is_micro_trade=st.just(False),
)
# ---------------------------------------------------------------------------
# Property 9: Stop-loss and take-profit initial computation
# **Validates: Requirements 4.1, 4.2**
# ---------------------------------------------------------------------------
class TestProperty9InitialComputation:
"""Property 9: Stop-loss and take-profit initial computation.
**Validates: Requirements 4.1, 4.2**
"""
manager = StopLossManager()
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.1, max_value=20.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_stop_loss_equals_entry_minus_atr_times_multiplier(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Stop-loss = entry_price - (ATR * stop_loss_atr_multiplier)."""
levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
expected_stop = entry_price - (atr * risk_tier.stop_loss_atr_multiplier)
assert abs(levels.stop_loss_price - expected_stop) < 1e-9, (
f"stop_loss {levels.stop_loss_price} != expected {expected_stop}"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.1, max_value=20.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_stop_loss_always_below_entry(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Stop-loss is always below entry price (for positive ATR and multiplier)."""
assume(atr * risk_tier.stop_loss_atr_multiplier > 0)
levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
assert levels.stop_loss_price < entry_price, (
f"stop_loss {levels.stop_loss_price} >= entry {entry_price}"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.1, max_value=20.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_take_profit_equals_entry_plus_stop_distance_times_ratio(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Take-profit = entry_price + (stop_distance * reward_risk_ratio)."""
levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
stop_distance = atr * risk_tier.stop_loss_atr_multiplier
expected_tp = entry_price + (stop_distance * risk_tier.reward_risk_ratio)
assert abs(levels.take_profit_price - expected_tp) < 1e-9, (
f"take_profit {levels.take_profit_price} != expected {expected_tp}"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.1, max_value=20.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_take_profit_always_above_entry(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Take-profit is always above entry price."""
assume(atr * risk_tier.stop_loss_atr_multiplier * risk_tier.reward_risk_ratio > 0)
levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
assert levels.take_profit_price > entry_price, (
f"take_profit {levels.take_profit_price} <= entry {entry_price}"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=5.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.1, max_value=20.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_trailing_stop_initially_inactive(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Trailing stop is not active on initial computation."""
levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
assert levels.trailing_stop_active is False
# ---------------------------------------------------------------------------
# Property 10: Price crossing triggers immediate sell
# **Validates: Requirements 4.4, 4.5**
# ---------------------------------------------------------------------------
class TestProperty10PriceCrossingTriggers:
"""Property 10: Price crossing triggers immediate sell.
**Validates: Requirements 4.4, 4.5**
"""
manager = StopLossManager()
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
stop_distance_pct=st.floats(min_value=0.02, max_value=0.20, allow_nan=False, allow_infinity=False),
tp_distance_pct=st.floats(min_value=0.02, max_value=0.40, allow_nan=False, allow_infinity=False),
)
def test_sell_triggered_when_price_at_or_below_stop_loss(
self, entry_price: float, stop_distance_pct: float, tp_distance_pct: float,
) -> None:
"""Sell triggered when current price <= stop_loss."""
stop_loss = entry_price * (1 - stop_distance_pct)
take_profit = entry_price * (1 + tp_distance_pct)
assume(stop_loss > 0)
assume(take_profit > stop_loss)
ticker = "TEST"
position = OpenPosition(
ticker=ticker, quantity=10, entry_price=entry_price,
current_price=stop_loss, unrealized_pnl=0.0,
market_value=entry_price * 10, sector="Technology",
stop_loss_price=stop_loss, take_profit_price=take_profit,
signal_confidence=0.7,
)
levels = StopLevels(
stop_loss_price=stop_loss,
take_profit_price=take_profit,
trailing_stop_active=False,
atr_value=1.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
)
# Price at stop_loss
triggers = self.manager.check_price_crossings(
positions=[position],
prices={ticker: stop_loss},
stop_levels={ticker: levels},
)
assert len(triggers) == 1
assert triggers[0].trigger_type == "stop_loss"
assert triggers[0].ticker == ticker
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
stop_distance_pct=st.floats(min_value=0.02, max_value=0.20, allow_nan=False, allow_infinity=False),
tp_distance_pct=st.floats(min_value=0.02, max_value=0.40, allow_nan=False, allow_infinity=False),
below_pct=st.floats(min_value=0.001, max_value=0.10, allow_nan=False, allow_infinity=False),
)
def test_sell_triggered_when_price_below_stop_loss(
self, entry_price: float, stop_distance_pct: float,
tp_distance_pct: float, below_pct: float,
) -> None:
"""Sell triggered when current price is below stop_loss."""
stop_loss = entry_price * (1 - stop_distance_pct)
take_profit = entry_price * (1 + tp_distance_pct)
price_below = stop_loss * (1 - below_pct)
assume(stop_loss > 0)
assume(price_below > 0)
assume(take_profit > stop_loss)
ticker = "TEST"
position = OpenPosition(
ticker=ticker, quantity=10, entry_price=entry_price,
current_price=price_below, unrealized_pnl=0.0,
market_value=entry_price * 10, sector="Technology",
stop_loss_price=stop_loss, take_profit_price=take_profit,
signal_confidence=0.7,
)
levels = StopLevels(
stop_loss_price=stop_loss,
take_profit_price=take_profit,
trailing_stop_active=False,
atr_value=1.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
)
triggers = self.manager.check_price_crossings(
positions=[position],
prices={ticker: price_below},
stop_levels={ticker: levels},
)
assert len(triggers) == 1
assert triggers[0].trigger_type == "stop_loss"
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
stop_distance_pct=st.floats(min_value=0.02, max_value=0.20, allow_nan=False, allow_infinity=False),
tp_distance_pct=st.floats(min_value=0.02, max_value=0.40, allow_nan=False, allow_infinity=False),
)
def test_sell_triggered_when_price_at_or_above_take_profit(
self, entry_price: float, stop_distance_pct: float, tp_distance_pct: float,
) -> None:
"""Sell triggered when current price >= take_profit."""
stop_loss = entry_price * (1 - stop_distance_pct)
take_profit = entry_price * (1 + tp_distance_pct)
assume(stop_loss > 0)
assume(take_profit > stop_loss)
ticker = "TEST"
position = OpenPosition(
ticker=ticker, quantity=10, entry_price=entry_price,
current_price=take_profit, unrealized_pnl=0.0,
market_value=entry_price * 10, sector="Technology",
stop_loss_price=stop_loss, take_profit_price=take_profit,
signal_confidence=0.7,
)
levels = StopLevels(
stop_loss_price=stop_loss,
take_profit_price=take_profit,
trailing_stop_active=False,
atr_value=1.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
)
triggers = self.manager.check_price_crossings(
positions=[position],
prices={ticker: take_profit},
stop_levels={ticker: levels},
)
assert len(triggers) == 1
assert triggers[0].trigger_type == "take_profit"
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
stop_distance_pct=st.floats(min_value=0.05, max_value=0.20, allow_nan=False, allow_infinity=False),
tp_distance_pct=st.floats(min_value=0.05, max_value=0.40, allow_nan=False, allow_infinity=False),
between_pct=st.floats(min_value=0.01, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_no_trigger_when_price_between_levels(
self, entry_price: float, stop_distance_pct: float,
tp_distance_pct: float, between_pct: float,
) -> None:
"""No trigger when price is strictly between stop_loss and take_profit."""
stop_loss = entry_price * (1 - stop_distance_pct)
take_profit = entry_price * (1 + tp_distance_pct)
assume(stop_loss > 0)
assume(take_profit > stop_loss + 0.02)
# Price strictly between stop_loss and take_profit
price_between = stop_loss + (take_profit - stop_loss) * between_pct
# Ensure strictly between (not at boundaries)
assume(price_between > stop_loss)
assume(price_between < take_profit)
ticker = "TEST"
position = OpenPosition(
ticker=ticker, quantity=10, entry_price=entry_price,
current_price=price_between, unrealized_pnl=0.0,
market_value=entry_price * 10, sector="Technology",
stop_loss_price=stop_loss, take_profit_price=take_profit,
signal_confidence=0.7,
)
levels = StopLevels(
stop_loss_price=stop_loss,
take_profit_price=take_profit,
trailing_stop_active=False,
atr_value=1.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
)
triggers = self.manager.check_price_crossings(
positions=[position],
prices={ticker: price_between},
stop_levels={ticker: levels},
)
assert len(triggers) == 0
# ---------------------------------------------------------------------------
# Property 11: Trailing stop activation at 50% of take-profit distance
# **Validates: Requirements 4.6**
# ---------------------------------------------------------------------------
class TestProperty11TrailingStopActivation:
"""Property 11: Trailing stop activation at 50% of take-profit distance.
**Validates: Requirements 4.6**
"""
manager = StopLossManager()
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.5, max_value=10.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
move_fraction=st.floats(min_value=0.51, max_value=0.99, allow_nan=False, allow_infinity=False),
)
def test_trailing_stop_activates_when_move_exceeds_50_pct(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
move_fraction: float,
) -> None:
"""Trailing stop activates when favorable move > 50% of TP distance."""
# Compute initial levels to get the TP distance
initial_levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
tp_distance = initial_levels.take_profit_price - entry_price
assume(tp_distance > 0.01)
# Current price moved favorably by more than 50% of TP distance
current_price = entry_price + (tp_distance * move_fraction)
position = OpenPosition(
ticker="TEST", quantity=10, entry_price=entry_price,
current_price=current_price, unrealized_pnl=0.0,
market_value=current_price * 10, sector="Technology",
stop_loss_price=initial_levels.stop_loss_price,
take_profit_price=initial_levels.take_profit_price,
signal_confidence=0.7,
)
result = self.manager.re_evaluate_levels(
position=position,
current_price=current_price,
atr=atr,
risk_tier=risk_tier,
last_levels=initial_levels,
)
# re_evaluate_levels returns None when no material change, or StopLevels
# Since trailing stop activation IS a material change, we expect a result
assert result is not None, "Expected re_evaluate to return updated levels"
assert result.trailing_stop_active is True
# When trailing stop is active, stop should be at least at entry (breakeven)
assert result.stop_loss_price >= entry_price - 1e-9, (
f"Trailing stop {result.stop_loss_price} should be >= entry {entry_price}"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.5, max_value=10.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
move_fraction=st.floats(min_value=0.0, max_value=0.49, allow_nan=False, allow_infinity=False),
)
def test_trailing_stop_does_not_activate_when_move_at_or_below_50_pct(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
move_fraction: float,
) -> None:
"""Trailing stop does NOT activate when favorable move <= 50% of TP distance."""
initial_levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
tp_distance = initial_levels.take_profit_price - entry_price
assume(tp_distance > 0.01)
# Current price moved favorably by <= 50% of TP distance
current_price = entry_price + (tp_distance * move_fraction)
position = OpenPosition(
ticker="TEST", quantity=10, entry_price=entry_price,
current_price=current_price, unrealized_pnl=0.0,
market_value=current_price * 10, sector="Technology",
stop_loss_price=initial_levels.stop_loss_price,
take_profit_price=initial_levels.take_profit_price,
signal_confidence=0.7,
)
result = self.manager.re_evaluate_levels(
position=position,
current_price=current_price,
atr=atr,
risk_tier=risk_tier,
last_levels=initial_levels,
)
# Either None (no change) or result with trailing_stop_active=False
if result is not None:
assert result.trailing_stop_active is False, (
f"Trailing stop should not activate at {move_fraction*100:.1f}% move"
)
# ---------------------------------------------------------------------------
# Property 15: Stop tightening during high-severity events
# **Validates: Requirements 7.2**
# ---------------------------------------------------------------------------
class TestProperty15HighSeverityEventTightening:
"""Property 15: Stop tightening during high-severity events.
**Validates: Requirements 7.2**
"""
manager = StopLossManager()
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.5, max_value=10.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_tightened_stop_uses_half_normal_multiplier(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""During high-severity events, stop uses 0.5x normal ATR multiplier."""
initial_levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
# Use a current price near entry so trailing stop doesn't activate
current_price = entry_price + 0.01
position = OpenPosition(
ticker="TEST", quantity=10, entry_price=entry_price,
current_price=current_price, unrealized_pnl=0.0,
market_value=current_price * 10, sector="Technology",
stop_loss_price=initial_levels.stop_loss_price,
take_profit_price=initial_levels.take_profit_price,
signal_confidence=0.7,
)
result = self.manager.re_evaluate_levels(
position=position,
current_price=current_price,
atr=atr,
risk_tier=risk_tier,
last_levels=initial_levels,
high_severity_event=True,
)
# High-severity event changes the multiplier, so we expect a result
assert result is not None, "Expected updated levels during high-severity event"
# The tightened stop should use 0.5x the normal multiplier
expected_tightened_multiplier = risk_tier.stop_loss_atr_multiplier * 0.5
expected_stop = entry_price - (atr * expected_tightened_multiplier)
assert abs(result.stop_loss_price - expected_stop) < 1e-9, (
f"Tightened stop {result.stop_loss_price} != expected {expected_stop} "
f"(0.5x multiplier = {expected_tightened_multiplier})"
)
@settings(max_examples=100)
@given(
entry_price=st.floats(min_value=50.0, max_value=500.0, allow_nan=False, allow_infinity=False),
atr=st.floats(min_value=0.5, max_value=10.0, allow_nan=False, allow_infinity=False),
risk_tier=_risk_tier_config_strategy(),
)
def test_tightened_stop_closer_to_entry_than_normal(
self, entry_price: float, atr: float, risk_tier: RiskTierConfig,
) -> None:
"""Tightened stop is closer to entry price (higher) than normal stop."""
initial_levels = self.manager.compute_initial_levels(
entry_price=entry_price, atr=atr, risk_tier=risk_tier,
)
current_price = entry_price + 0.01
position = OpenPosition(
ticker="TEST", quantity=10, entry_price=entry_price,
current_price=current_price, unrealized_pnl=0.0,
market_value=current_price * 10, sector="Technology",
stop_loss_price=initial_levels.stop_loss_price,
take_profit_price=initial_levels.take_profit_price,
signal_confidence=0.7,
)
result = self.manager.re_evaluate_levels(
position=position,
current_price=current_price,
atr=atr,
risk_tier=risk_tier,
last_levels=initial_levels,
high_severity_event=True,
)
assert result is not None
# Tightened stop should be closer to entry (higher value) than normal stop
normal_stop = entry_price - (atr * risk_tier.stop_loss_atr_multiplier)
assert result.stop_loss_price >= normal_stop - 1e-9, (
f"Tightened stop {result.stop_loss_price} should be >= normal stop {normal_stop}"
)
# ---------------------------------------------------------------------------
# Property 25: Proactive stop tightening at 80% heat threshold
# **Validates: Requirements 13.3**
# ---------------------------------------------------------------------------
class TestProperty25ProactiveHeatTightening:
"""Property 25: Proactive stop tightening at 80% heat threshold.
**Validates: Requirements 13.3**
"""
manager = StopLossManager()
@settings(max_examples=100)
@given(
num_positions=st.integers(min_value=2, max_value=5),
max_heat=st.floats(min_value=0.10, max_value=0.30, allow_nan=False, allow_infinity=False),
)
def test_lowest_confidence_positions_tightened_first(
self, num_positions: int, max_heat: float,
) -> None:
"""Lowest-confidence positions get stops tightened first when heat > 80% of max."""
# Create positions with distinct confidence levels
positions: list[OpenPosition] = []
stop_levels_dict: dict[str, StopLevels] = {}
for i in range(num_positions):
ticker = f"T{i}"
entry_price = 100.0
confidence = 0.3 + (i * 0.15) # ascending confidence: 0.3, 0.45, 0.6, ...
stop_loss = 90.0 # 10% below entry
take_profit = 115.0
positions.append(OpenPosition(
ticker=ticker, quantity=10, entry_price=entry_price,
current_price=entry_price, unrealized_pnl=0.0,
market_value=entry_price * 10, sector="Technology",
stop_loss_price=stop_loss, take_profit_price=take_profit,
signal_confidence=confidence,
))
stop_levels_dict[ticker] = StopLevels(
stop_loss_price=stop_loss,
take_profit_price=take_profit,
trailing_stop_active=False,
atr_value=5.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
)
# Set heat above 80% of max to trigger tightening
portfolio_heat = max_heat * 0.85
active_pool = 10000.0
updated = self.manager.tighten_for_heat(
positions=positions,
stop_levels=stop_levels_dict,
portfolio_heat=portfolio_heat,
max_heat=max_heat,
active_pool=active_pool,
)
if updated:
# Verify that tightened positions are ordered by confidence (lowest first)
tightened_tickers = list(updated.keys())
tightened_confidences = [
next(p.signal_confidence for p in positions if p.ticker == t)
for t in tightened_tickers
]
# The first tightened position should have the lowest confidence
min_confidence_in_portfolio = min(p.signal_confidence for p in positions)
if tightened_tickers:
first_tightened_confidence = next(
p.signal_confidence for p in positions if p.ticker == tightened_tickers[0]
)
assert first_tightened_confidence == min_confidence_in_portfolio, (
f"First tightened position confidence {first_tightened_confidence} "
f"!= min confidence {min_confidence_in_portfolio}"
)
# All tightened stops should be >= original stops (moved closer to entry)
for ticker, new_levels in updated.items():
original_stop = stop_levels_dict[ticker].stop_loss_price
assert new_levels.stop_loss_price >= original_stop - 1e-9, (
f"{ticker}: tightened stop {new_levels.stop_loss_price} "
f"< original {original_stop}"
)
@settings(max_examples=100)
@given(
max_heat=st.floats(min_value=0.10, max_value=0.30, allow_nan=False, allow_infinity=False),
heat_fraction=st.floats(min_value=0.0, max_value=0.79, allow_nan=False, allow_infinity=False),
)
def test_no_tightening_when_heat_below_80_pct_threshold(
self, max_heat: float, heat_fraction: float,
) -> None:
"""No tightening when portfolio heat <= 80% of max."""
portfolio_heat = max_heat * heat_fraction
positions = [
OpenPosition(
ticker="T0", quantity=10, entry_price=100.0,
current_price=100.0, unrealized_pnl=0.0,
market_value=1000.0, sector="Technology",
stop_loss_price=90.0, take_profit_price=115.0,
signal_confidence=0.5,
),
]
stop_levels_dict = {
"T0": StopLevels(
stop_loss_price=90.0,
take_profit_price=115.0,
trailing_stop_active=False,
atr_value=5.0,
atr_multiplier=2.0,
reward_risk_ratio=1.5,
last_updated=datetime.utcnow(),
),
}
updated = self.manager.tighten_for_heat(
positions=positions,
stop_levels=stop_levels_dict,
portfolio_heat=portfolio_heat,
max_heat=max_heat,
active_pool=10000.0,
)
assert updated == {}, (
f"Expected no tightening at heat {portfolio_heat} "
f"(80% threshold = {max_heat * 0.8}), got {len(updated)} updates"
)
+291
View File
@@ -0,0 +1,291 @@
"""Property-based tests for the Tax Lot Tracker.
Feature: autonomous-trading-engine
Tests properties 22 and 23 from the design specification,
covering FIFO lot ordering and wash sale detection.
"""
from __future__ import annotations
from datetime import date, timedelta
from hypothesis import given, settings, assume
from hypothesis import strategies as st
from services.trading.tax_lots import ClosedLot, TaxLot, TaxLotTracker
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
def _tax_lot_strategy(
ticker: str = "AAPL",
status: str = "open",
) -> st.SearchStrategy[TaxLot]:
"""Generate a random TaxLot with a fixed ticker and status."""
return st.builds(
TaxLot,
ticker=st.just(ticker),
quantity=st.integers(min_value=1, max_value=100),
cost_basis_per_share=st.floats(
min_value=1.0, max_value=500.0,
allow_nan=False, allow_infinity=False,
),
acquisition_date=st.dates(
min_value=date(2023, 1, 1),
max_value=date(2025, 6, 1),
),
status=st.just(status),
)
# ---------------------------------------------------------------------------
# Property 22: Tax lot FIFO ordering
# **Validates: Requirements 12.4**
# ---------------------------------------------------------------------------
class TestProperty22TaxLotFIFOOrdering:
"""Property 22: Tax lot FIFO ordering.
For any sequence of buy transactions, closing lots SHALL follow
FIFO order — the earliest-acquired open lot is closed first.
The realized P&L for each closed lot SHALL equal
(exit_price - cost_basis_per_share) * quantity.
**Validates: Requirements 12.4**
"""
@given(
lots=st.lists(
_tax_lot_strategy(),
min_size=1,
max_size=10,
),
exit_price=st.floats(
min_value=1.0, max_value=500.0,
allow_nan=False, allow_infinity=False,
),
exit_date=st.dates(
min_value=date(2025, 1, 1),
max_value=date(2025, 12, 31),
),
)
@settings(max_examples=100)
def test_lots_closed_in_fifo_order(
self,
lots: list[TaxLot],
exit_price: float,
exit_date: date,
) -> None:
"""Closed lots are ordered by acquisition_date ascending (FIFO)."""
total_available = sum(lot.quantity for lot in lots)
assume(total_available > 0)
tracker = TaxLotTracker()
closed = tracker.close_lots_fifo(
lots=lots,
quantity=total_available,
exit_price=exit_price,
exit_date=exit_date,
)
# Verify FIFO: each closed lot's acquisition_date is <= the next
for i in range(len(closed) - 1):
assert closed[i].acquisition_date <= closed[i + 1].acquisition_date, (
f"FIFO violated: lot {i} acquired {closed[i].acquisition_date} "
f"but lot {i+1} acquired {closed[i+1].acquisition_date}"
)
@given(
lots=st.lists(
_tax_lot_strategy(),
min_size=1,
max_size=10,
),
exit_price=st.floats(
min_value=1.0, max_value=500.0,
allow_nan=False, allow_infinity=False,
),
exit_date=st.dates(
min_value=date(2025, 1, 1),
max_value=date(2025, 12, 31),
),
)
@settings(max_examples=100)
def test_realized_pnl_formula(
self,
lots: list[TaxLot],
exit_price: float,
exit_date: date,
) -> None:
"""Realized P&L = (exit_price - cost_basis_per_share) * quantity per lot."""
total_available = sum(lot.quantity for lot in lots)
assume(total_available > 0)
tracker = TaxLotTracker()
closed = tracker.close_lots_fifo(
lots=lots,
quantity=total_available,
exit_price=exit_price,
exit_date=exit_date,
)
for cl in closed:
expected_pnl = (cl.exit_price - cl.cost_basis_per_share) * cl.quantity
assert abs(cl.realized_pnl - expected_pnl) < 1e-6, (
f"P&L mismatch: got {cl.realized_pnl}, "
f"expected {expected_pnl} for lot acquired {cl.acquisition_date}"
)
@given(
lots=st.lists(
_tax_lot_strategy(),
min_size=2,
max_size=10,
),
exit_price=st.floats(
min_value=1.0, max_value=500.0,
allow_nan=False, allow_infinity=False,
),
exit_date=st.dates(
min_value=date(2025, 1, 1),
max_value=date(2025, 12, 31),
),
)
@settings(max_examples=100)
def test_partial_close_respects_fifo(
self,
lots: list[TaxLot],
exit_price: float,
exit_date: date,
) -> None:
"""Closing fewer shares than available still follows FIFO order."""
total_available = sum(lot.quantity for lot in lots)
assume(total_available >= 2)
# Close only half the shares
close_qty = total_available // 2
assume(close_qty > 0)
tracker = TaxLotTracker()
closed = tracker.close_lots_fifo(
lots=lots,
quantity=close_qty,
exit_price=exit_price,
exit_date=exit_date,
)
# Total closed quantity should not exceed requested
total_closed = sum(cl.quantity for cl in closed)
assert total_closed <= close_qty
# FIFO order maintained
for i in range(len(closed) - 1):
assert closed[i].acquisition_date <= closed[i + 1].acquisition_date
# ---------------------------------------------------------------------------
# Property 23: Wash sale detection within 30-day window
# **Validates: Requirements 12.2, 12.3**
# ---------------------------------------------------------------------------
class TestProperty23WashSaleDetection:
"""Property 23: Wash sale detection within 30-day window.
For any position closed at a loss, the Tax Lot Tracker SHALL flag
it as a potential wash sale if the same ticker was purchased within
30 days before or after the loss date.
**Validates: Requirements 12.2, 12.3**
"""
@given(
loss_date=st.dates(
min_value=date(2024, 6, 1),
max_value=date(2025, 6, 1),
),
days_offset=st.integers(min_value=-30, max_value=30),
)
@settings(max_examples=100)
def test_wash_sale_flagged_within_30_days(
self,
loss_date: date,
days_offset: int,
) -> None:
"""Purchase within 30 days of loss triggers wash sale flag."""
purchase_date = loss_date + timedelta(days=days_offset)
purchase = TaxLot(
ticker="AAPL",
quantity=10,
cost_basis_per_share=100.0,
acquisition_date=purchase_date,
)
tracker = TaxLotTracker()
result = tracker.check_wash_sale(
loss_date=loss_date,
purchases=[purchase],
)
assert result is True, (
f"Expected wash sale flag for purchase {days_offset} days "
f"from loss date {loss_date}"
)
@given(
loss_date=st.dates(
min_value=date(2024, 6, 1),
max_value=date(2025, 6, 1),
),
days_offset=st.integers(min_value=31, max_value=365),
direction=st.sampled_from([-1, 1]),
)
@settings(max_examples=100)
def test_no_wash_sale_outside_30_days(
self,
loss_date: date,
days_offset: int,
direction: int,
) -> None:
"""Purchase outside 30-day window does not trigger wash sale."""
purchase_date = loss_date + timedelta(days=days_offset * direction)
purchase = TaxLot(
ticker="AAPL",
quantity=10,
cost_basis_per_share=100.0,
acquisition_date=purchase_date,
)
tracker = TaxLotTracker()
result = tracker.check_wash_sale(
loss_date=loss_date,
purchases=[purchase],
)
assert result is False, (
f"Unexpected wash sale flag for purchase {days_offset * direction} "
f"days from loss date {loss_date}"
)
@given(
loss_date=st.dates(
min_value=date(2024, 6, 1),
max_value=date(2025, 6, 1),
),
)
@settings(max_examples=100)
def test_no_wash_sale_with_no_purchases(
self,
loss_date: date,
) -> None:
"""No purchases means no wash sale."""
tracker = TaxLotTracker()
result = tracker.check_wash_sale(
loss_date=loss_date,
purchases=[],
)
assert result is False
+302
View File
@@ -0,0 +1,302 @@
"""Property-based tests for Trading Engine HTTP API.
Feature: autonomous-trading-engine
Tests properties 35 and 29 from the design specification, covering
configuration change audit trail and persistence round-trip for
trading engine state via the FastAPI endpoints.
"""
from __future__ import annotations
from hypothesis import given, settings
from hypothesis import strategies as st
from starlette.testclient import TestClient
from services.trading.app import app
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
_RISK_TIERS = st.sampled_from(["conservative", "moderate", "aggressive"])
_CONFIG_UPDATES = st.fixed_dictionaries(
{},
optional={
"enabled": st.booleans(),
"risk_tier": _RISK_TIERS,
"reserve_siphon_pct": st.floats(
min_value=0.0, max_value=1.0,
allow_nan=False, allow_infinity=False,
),
"polling_interval_seconds": st.integers(min_value=1, max_value=3600),
"absolute_position_cap": st.floats(
min_value=1.0, max_value=10_000.0,
allow_nan=False, allow_infinity=False,
),
"active_pool_minimum": st.floats(
min_value=0.0, max_value=10_000.0,
allow_nan=False, allow_infinity=False,
),
"micro_trading_enabled": st.booleans(),
},
).filter(lambda d: len(d) > 0) # at least one field must be set
# ---------------------------------------------------------------------------
# Module-level client with lifespan
# ---------------------------------------------------------------------------
# Use a stack to manage the TestClient context manager at module scope.
_client: TestClient | None = None
def _get_client() -> TestClient:
global _client
if _client is None:
_client = TestClient(app, raise_server_exceptions=True)
_client.__enter__()
return _client
def teardown_module() -> None:
global _client
if _client is not None:
_client.__exit__(None, None, None)
_client = None
# ---------------------------------------------------------------------------
# Property 35: Configuration change audit trail
# **Validates: Requirements 16.6**
# ---------------------------------------------------------------------------
class TestProperty35ConfigurationChangeAuditTrail:
"""Property 35: Configuration change audit trail.
**Validates: Requirements 16.6**
"""
@settings(max_examples=100)
@given(update=_CONFIG_UPDATES)
def test_config_update_returns_previous_and_new(
self, update: dict,
) -> None:
"""PUT /api/trading/config returns previous and new values for every changed field."""
client = _get_client()
resp = client.put("/api/trading/config", json=update)
assert resp.status_code == 200, f"Expected 200, got {resp.status_code}"
data = resp.json()
assert "previous" in data, "Response must include 'previous'"
assert "updated" in data, "Response must include 'updated'"
assert "change_source" in data, "Response must include 'change_source'"
assert "changed_at" in data, "Response must include 'changed_at'"
# Every field sent in the request must appear in both previous and updated
for field_name, new_value in update.items():
assert field_name in data["previous"], (
f"Field '{field_name}' missing from previous config"
)
assert field_name in data["updated"], (
f"Field '{field_name}' missing from updated config"
)
# The updated value must match what was sent
assert data["updated"][field_name] == new_value, (
f"Updated value for '{field_name}' should be {new_value}, "
f"got {data['updated'][field_name]}"
)
@settings(max_examples=100)
@given(update=_CONFIG_UPDATES)
def test_config_update_change_source_is_api(
self, update: dict,
) -> None:
"""PUT /api/trading/config always records change_source as 'api'."""
client = _get_client()
resp = client.put("/api/trading/config", json=update)
assert resp.status_code == 200
assert resp.json()["change_source"] == "api"
# ---------------------------------------------------------------------------
# Property 29: Persistence round-trip for trading engine state
# **Validates: Requirements 3.2, 4.7, 5.5, 6.4, 14.3, 15.4, 16.1**
# ---------------------------------------------------------------------------
class TestProperty29PersistenceRoundTrip:
"""Property 29: Persistence round-trip for trading engine state.
**Validates: Requirements 3.2, 4.7, 5.5, 6.4, 14.3, 15.4, 16.1**
"""
def test_status_returns_valid_dict_with_expected_keys(self) -> None:
"""GET /api/trading/status returns a dict with all expected keys."""
client = _get_client()
resp = client.get("/api/trading/status")
assert resp.status_code == 200
data = resp.json()
expected_keys = {
"enabled",
"paused",
"risk_tier",
"circuit_breaker_status",
"active_pool",
"reserve_pool",
"portfolio_heat",
"open_positions",
"last_decision_at",
}
assert expected_keys.issubset(data.keys()), (
f"Missing keys: {expected_keys - data.keys()}"
)
def test_pause_then_status_shows_paused(self) -> None:
"""POST /api/trading/pause followed by GET /api/trading/status shows paused=true."""
client = _get_client()
pause_resp = client.post("/api/trading/pause")
assert pause_resp.status_code == 200
assert pause_resp.json()["paused"] is True
status_resp = client.get("/api/trading/status")
assert status_resp.status_code == 200
assert status_resp.json()["paused"] is True
def test_resume_then_status_shows_not_paused(self) -> None:
"""POST /api/trading/resume followed by GET /api/trading/status shows paused=false."""
client = _get_client()
resume_resp = client.post("/api/trading/resume")
assert resume_resp.status_code == 200
assert resume_resp.json()["paused"] is False
status_resp = client.get("/api/trading/status")
assert status_resp.status_code == 200
assert status_resp.json()["paused"] is False
@settings(max_examples=100)
@given(risk_tier=_RISK_TIERS)
def test_config_round_trip_risk_tier(self, risk_tier: str) -> None:
"""Setting risk_tier via config update is reflected in status."""
client = _get_client()
resp = client.put(
"/api/trading/config",
json={"risk_tier": risk_tier},
)
assert resp.status_code == 200
assert resp.json()["updated"]["risk_tier"] == risk_tier
status = client.get("/api/trading/status").json()
assert status["risk_tier"] == risk_tier
@settings(max_examples=100)
@given(enabled=st.booleans())
def test_config_round_trip_enabled(self, enabled: bool) -> None:
"""Setting enabled via config update is reflected in status."""
client = _get_client()
resp = client.put(
"/api/trading/config",
json={"enabled": enabled},
)
assert resp.status_code == 200
assert resp.json()["updated"]["enabled"] == enabled
status = client.get("/api/trading/status").json()
assert status["enabled"] == enabled
def test_health_returns_ok(self) -> None:
"""GET /health always returns status ok."""
client = _get_client()
resp = client.get("/health")
assert resp.status_code == 200
assert resp.json() == {"status": "ok"}
def test_ready_returns_boolean(self) -> None:
"""GET /ready returns a dict with a boolean 'ready' field."""
client = _get_client()
resp = client.get("/ready")
assert resp.status_code == 200
assert isinstance(resp.json()["ready"], bool)
def test_backtest_returns_id(self) -> None:
"""POST /api/trading/backtest returns a backtest_id string."""
client = _get_client()
resp = client.post(
"/api/trading/backtest",
json={
"start_date": "2024-01-01",
"end_date": "2024-06-30",
"initial_capital": 500.0,
"risk_tier": "moderate",
},
)
assert resp.status_code == 200
data = resp.json()
assert "backtest_id" in data
assert isinstance(data["backtest_id"], str)
assert len(data["backtest_id"]) > 0
def test_backtest_get_returns_placeholder(self) -> None:
"""GET /api/trading/backtest/{id} returns a result dict."""
client = _get_client()
test_id = "test-123"
resp = client.get(f"/api/trading/backtest/{test_id}")
assert resp.status_code == 200
data = resp.json()
assert data["backtest_id"] == test_id
def test_decisions_returns_list(self) -> None:
"""GET /api/trading/decisions returns a list."""
client = _get_client()
resp = client.get("/api/trading/decisions")
assert resp.status_code == 200
assert isinstance(resp.json(), list)
def test_metrics_returns_expected_keys(self) -> None:
"""GET /api/trading/metrics returns a dict with expected metric keys."""
client = _get_client()
resp = client.get("/api/trading/metrics")
assert resp.status_code == 200
data = resp.json()
expected_keys = {
"total_portfolio_value",
"active_pool",
"reserve_pool",
"unrealized_pnl",
"realized_pnl",
"daily_pnl",
"win_rate",
"profit_factor",
"sharpe_ratio",
"max_drawdown",
"portfolio_heat",
}
assert expected_keys.issubset(data.keys()), (
f"Missing keys: {expected_keys - data.keys()}"
)
def test_metrics_history_returns_list(self) -> None:
"""GET /api/trading/metrics/history returns a list."""
client = _get_client()
resp = client.get("/api/trading/metrics/history")
assert resp.status_code == 200
assert isinstance(resp.json(), list)
def test_notification_config_returns_dict(self) -> None:
"""GET /api/trading/notifications/config returns a dict."""
client = _get_client()
resp = client.get("/api/trading/notifications/config")
assert resp.status_code == 200
data = resp.json()
assert "sms_enabled" in data
assert "email_enabled" in data
def test_notification_history_returns_list(self) -> None:
"""GET /api/trading/notifications/history returns a list."""
client = _get_client()
resp = client.get("/api/trading/notifications/history")
assert resp.status_code == 200
assert isinstance(resp.json(), list)
+317
View File
@@ -0,0 +1,317 @@
"""Property-based tests for Trading Window and Gradual Entry.
Feature: autonomous-trading-engine
Tests properties 20 and 21 from the design specification, covering
trading window determination (9:45 AM 3:45 PM ET on weekdays) and
gradual entry tranche splitting.
"""
from __future__ import annotations
from datetime import datetime, time
from hypothesis import given, settings
from hypothesis import strategies as st
from services.trading.gradual_entry import (
create_tranches,
should_use_gradual_entry,
split_into_tranches,
)
from services.trading.trading_window import (
ET,
MARKET_OPEN,
WINDOW_CLOSE,
WINDOW_OPEN,
is_market_open,
is_within_trading_window,
next_window_open,
)
# ---------------------------------------------------------------------------
# Hypothesis strategies
# ---------------------------------------------------------------------------
_WEEKDAYS = range(0, 5) # MonFri
def _et_datetime_in_window() -> st.SearchStrategy[datetime]:
"""Generate a timezone-aware datetime that is inside the trading window."""
return (
st.dates(
min_value=datetime(2024, 1, 1).date(),
max_value=datetime(2025, 12, 31).date(),
)
.filter(lambda d: d.weekday() in _WEEKDAYS)
.flatmap(
lambda d: st.times(
min_value=WINDOW_OPEN,
max_value=time(15, 44, 59),
).map(lambda t: datetime.combine(d, t, tzinfo=ET))
)
)
def _et_datetime_outside_window_weekday() -> st.SearchStrategy[datetime]:
"""Generate a weekday datetime that is outside the trading window.
Either before 9:45 AM ET or at/after 3:45 PM ET.
"""
before_open = st.times(min_value=time(0, 0), max_value=time(9, 44, 59))
after_close = st.times(min_value=WINDOW_CLOSE, max_value=time(23, 59, 59))
return (
st.dates(
min_value=datetime(2024, 1, 1).date(),
max_value=datetime(2025, 12, 31).date(),
)
.filter(lambda d: d.weekday() in _WEEKDAYS)
.flatmap(
lambda d: st.one_of(before_open, after_close).map(
lambda t: datetime.combine(d, t, tzinfo=ET)
)
)
)
def _et_datetime_weekend() -> st.SearchStrategy[datetime]:
"""Generate a weekend datetime (Saturday or Sunday)."""
return (
st.dates(
min_value=datetime(2024, 1, 1).date(),
max_value=datetime(2025, 12, 31).date(),
)
.filter(lambda d: d.weekday() >= 5)
.flatmap(
lambda d: st.times().map(
lambda t: datetime.combine(d, t, tzinfo=ET)
)
)
)
# ---------------------------------------------------------------------------
# Property 20: Trading window determination
# **Validates: Requirements 11.1**
# ---------------------------------------------------------------------------
class TestProperty20TradingWindowDetermination:
"""Property 20: Trading window determination.
**Validates: Requirements 11.1**
"""
@settings(max_examples=100)
@given(dt=_et_datetime_in_window())
def test_within_window_on_weekday(self, dt: datetime) -> None:
"""Timestamps between 9:45 AM and 3:45 PM ET on weekdays are within the window."""
assert is_within_trading_window(dt) is True, (
f"Expected within window: {dt} (ET weekday={dt.weekday()}, time={dt.time()})"
)
@settings(max_examples=100)
@given(dt=_et_datetime_outside_window_weekday())
def test_outside_window_on_weekday(self, dt: datetime) -> None:
"""Weekday timestamps before 9:45 AM or at/after 3:45 PM ET are outside the window."""
assert is_within_trading_window(dt) is False, (
f"Expected outside window: {dt} (ET time={dt.time()})"
)
@settings(max_examples=100)
@given(dt=_et_datetime_weekend())
def test_outside_window_on_weekend(self, dt: datetime) -> None:
"""Weekend timestamps are always outside the trading window."""
assert is_within_trading_window(dt) is False, (
f"Expected outside window on weekend: {dt} (weekday={dt.weekday()})"
)
@settings(max_examples=100)
@given(dt=_et_datetime_outside_window_weekday())
def test_next_window_open_is_in_future(self, dt: datetime) -> None:
"""next_window_open always returns a time >= the input when outside the window."""
nwo = next_window_open(dt)
et_dt = dt.astimezone(ET)
et_nwo = nwo.astimezone(ET)
# If we're past today's open, next open must be a future day
if et_dt.time() >= WINDOW_OPEN:
assert et_nwo.date() > et_dt.date(), (
f"Expected future date: nwo={et_nwo}, dt={et_dt}"
)
assert et_nwo.time().hour == WINDOW_OPEN.hour
assert et_nwo.time().minute == WINDOW_OPEN.minute
@settings(max_examples=100)
@given(dt=_et_datetime_weekend())
def test_next_window_open_skips_weekends(self, dt: datetime) -> None:
"""next_window_open from a weekend returns a weekday."""
nwo = next_window_open(dt)
et_nwo = nwo.astimezone(ET)
assert et_nwo.weekday() in _WEEKDAYS, (
f"Expected weekday, got {et_nwo.weekday()} for {et_nwo}"
)
@settings(max_examples=100)
@given(
dt=st.dates(
min_value=datetime(2024, 1, 1).date(),
max_value=datetime(2025, 12, 31).date(),
)
.filter(lambda d: d.weekday() in _WEEKDAYS)
.flatmap(
lambda d: st.times(
min_value=MARKET_OPEN,
max_value=time(15, 59, 59),
).map(lambda t: datetime.combine(d, t, tzinfo=ET))
),
)
def test_is_market_open_during_market_hours(self, dt: datetime) -> None:
"""Timestamps between 9:30 AM and 4:00 PM ET on weekdays are market-open."""
assert is_market_open(dt) is True, (
f"Expected market open: {dt} (time={dt.time()})"
)
@settings(max_examples=100)
@given(dt=_et_datetime_weekend())
def test_is_market_closed_on_weekends(self, dt: datetime) -> None:
"""Weekend timestamps always have market closed."""
assert is_market_open(dt) is False
# ---------------------------------------------------------------------------
# Property 21: Gradual entry tranche splitting
# **Validates: Requirements 11.3, 11.5**
# ---------------------------------------------------------------------------
class TestProperty21GradualEntryTrancheSplitting:
"""Property 21: Gradual entry tranche splitting.
**Validates: Requirements 11.3, 11.5**
"""
# -- should_use_gradual_entry ------------------------------------------
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=100.0, max_value=100_000.0, allow_nan=False, allow_infinity=False),
threshold_dollars=st.floats(min_value=1.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
excess=st.floats(min_value=0.01, max_value=1000.0, allow_nan=False, allow_infinity=False),
)
def test_gradual_entry_triggered_above_threshold(
self, active_pool: float, threshold_dollars: float, excess: float,
) -> None:
"""Gradual entry is used when position size exceeds min(threshold, 5% of pool)."""
effective = min(threshold_dollars, 0.05 * active_pool)
position_size = effective + excess
assert should_use_gradual_entry(position_size, active_pool, threshold_dollars) is True
@settings(max_examples=100)
@given(
active_pool=st.floats(min_value=100.0, max_value=100_000.0, allow_nan=False, allow_infinity=False),
threshold_dollars=st.floats(min_value=1.0, max_value=1000.0, allow_nan=False, allow_infinity=False),
fraction=st.floats(min_value=0.0, max_value=1.0, allow_nan=False, allow_infinity=False),
)
def test_gradual_entry_not_triggered_at_or_below_threshold(
self, active_pool: float, threshold_dollars: float, fraction: float,
) -> None:
"""Gradual entry is NOT used when position size <= effective threshold."""
effective = min(threshold_dollars, 0.05 * active_pool)
position_size = effective * fraction
assert should_use_gradual_entry(position_size, active_pool, threshold_dollars) is False
# -- split_into_tranches -----------------------------------------------
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
num_tranches=st.integers(min_value=1, max_value=20),
)
def test_tranche_sum_equals_total(
self, total_quantity: int, num_tranches: int,
) -> None:
"""Sum of all tranches must equal the original total quantity."""
tranches = split_into_tranches(total_quantity, num_tranches)
assert sum(tranches) == total_quantity
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
num_tranches=st.integers(min_value=1, max_value=20),
)
def test_tranche_sizes_approximately_equal(
self, total_quantity: int, num_tranches: int,
) -> None:
"""All tranche sizes differ by at most 1."""
tranches = split_into_tranches(total_quantity, num_tranches)
assert max(tranches) - min(tranches) <= 1
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
num_tranches=st.integers(min_value=1, max_value=20),
)
def test_tranche_count_matches_requested(
self, total_quantity: int, num_tranches: int,
) -> None:
"""Number of tranches returned matches the requested count."""
tranches = split_into_tranches(total_quantity, num_tranches)
assert len(tranches) == num_tranches
# -- create_tranches ---------------------------------------------------
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
parent_id=st.text(min_size=1, max_size=36, alphabet=st.characters(whitelist_categories=("L", "N", "Pd"))),
num_tranches=st.integers(min_value=1, max_value=10),
)
def test_all_tranches_reference_same_parent_decision_id(
self, total_quantity: int, parent_id: str, num_tranches: int,
) -> None:
"""Every tranche references the same parent decision ID."""
tranches = create_tranches(total_quantity, parent_id, num_tranches)
for t in tranches:
assert t.parent_decision_id == parent_id
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
parent_id=st.text(min_size=1, max_size=36, alphabet=st.characters(whitelist_categories=("L", "N", "Pd"))),
num_tranches=st.integers(min_value=1, max_value=10),
)
def test_create_tranches_quantity_sum(
self, total_quantity: int, parent_id: str, num_tranches: int,
) -> None:
"""Sum of tranche quantities from create_tranches equals total."""
tranches = create_tranches(total_quantity, parent_id, num_tranches)
assert sum(t.quantity for t in tranches) == total_quantity
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
parent_id=st.text(min_size=1, max_size=36, alphabet=st.characters(whitelist_categories=("L", "N", "Pd"))),
num_tranches=st.integers(min_value=1, max_value=10),
)
def test_create_tranches_indices_sequential(
self, total_quantity: int, parent_id: str, num_tranches: int,
) -> None:
"""Tranche indices are sequential starting from 0."""
tranches = create_tranches(total_quantity, parent_id, num_tranches)
for i, t in enumerate(tranches):
assert t.tranche_index == i
@settings(max_examples=100)
@given(
total_quantity=st.integers(min_value=1, max_value=10_000),
parent_id=st.text(min_size=1, max_size=36, alphabet=st.characters(whitelist_categories=("L", "N", "Pd"))),
num_tranches=st.integers(min_value=1, max_value=10),
)
def test_create_tranches_default_status_pending(
self, total_quantity: int, parent_id: str, num_tranches: int,
) -> None:
"""All tranches start with status 'pending'."""
tranches = create_tranches(total_quantity, parent_id, num_tranches)
for t in tranches:
assert t.status == "pending"