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
+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",
)