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