Archived context note: this file captures a historical implementation prompt.
Canonical runbooks and governance now live inalphaswarm_docs/index.md,alphaswarm_docs/operations/*, andAGENTS.md. Seealphaswarm_docs/archive/README.md.
π Actionable Implementation Prompt: Institutional-Grade AlphaSwarm Refactor
Context: You are an expert quantitative developer tasked with enhancing the AlphaSwarm (AlphaSwarm). The project has already transitioned to a 5-stage framework (Universe -> Alpha -> Portfolio -> Risk -> Execution) and has defined core event types for an event-driven transition.
Objective: Complete the transition to a high-performance, deterministic, and agent-first trading laboratory.
Phase 1: Event-Driven Core Refactor (Action Item 2.1)β
- Target File:
alphaswarm/backtest/engine.py(specificallyEventDrivenBacktester). - Task: Replace the current iterative bar processing with a central
collections.dequeevent bus. - Implementation Details:
- Use
MarketEvent,SignalEvent,OrderEvent_Msg, andFillEvent_Msgfromalphaswarm/core/types.py. - Refactor
_run_implto push initialMarketEvents into the queue and process them in awhile queue:loop. - Ensure
IAlphaModeloutputsSignalEvents, which are then processed by thePortfolioConstructionModel.
- Use
- D.O.D: A backtest can be fully reconstructed by replaying the event log stored in the
BacktestResult.
Phase 2: Columnar Memory & Polars Integration (Action Item 1.1)β
- Target Files:
alphaswarm/data/iceberg_catalog.py,alphaswarm/backtest/engine.py. - Task: Migrate from
pandas.DataFrametopolars.DataFrameandpyarrow. - Implementation Details:
- Update
IcebergCatalog.read_arrowandread_polars(add if missing) to be the primary data retrieval methods. - Implement a
ZeroCopyManagerinalphaswarm/core/memory.pyto handle ArrowRecordBatchesacross process boundaries (Celery workers). - Refactor
_MLBaseAlphainalphaswarm/strategies/ml_alphas.pyto use Polars lazy expressions for feature engineering.
- Update
- D.O.D: All core data paths are free of
pandasserialization overhead.
Phase 3: Advanced Risk & Interception (Action Item 1.3)β
- Target Files:
alphaswarm/strategies/risk_models.py,alphaswarm/backtest/engine.py. - Task: Implement institutional-grade risk management.
- Implementation Details:
- Create
TrailingStopRiskManagementModel: Monitors unrealized PnL per symbol and liquidates if a peak-to-trough threshold is hit. - Create
TVaRInterceptor: A decorator for theIRiskManagementModelthat calculates Tail Value at Risk using the formula:TVaR = mu + sigma * (phi(InvPhi(alpha)) / (1-alpha)).
- Create
- D.O.D: The risk layer can autonomously cancel signals generated by the Alpha layer if they violate tail-risk constraints.
Phase 4: Multi-Agent Orchestration & MCP (Action Item 3.1 & 3.2)β
- Target Files:
alphaswarm/agents/graph/,alphaswarm/agents/runtime.py. - Task: Implement a hierarchical consensus mechanism for spec-driven agents.
- Implementation Details:
- Define MCP tool definitions for
get_historical_volatility,query_portfolio_margin, andsimulate_insight_impact. - Refactor
AgentRuntimeto orchestrate a "Research Debate":- Market Monitor: Detects regime shifts.
- Quant Generator: Drafts Alpha logic.
- Risk Simulator: Runs Monte Carlo stress tests.
- Use
langgraphto manage the state and consensus among these agents.
- Define MCP tool definitions for
- D.O.D: An agent run only progresses to a
SignalEventif the Risk Simulator agent approves the Monte Carlo outcome.
Phase 5: FinOps Governance Enforcement (Action Item 4.1)β
- Target Files:
alphaswarm/config.py,alphaswarm_platform/deploy/k8s/. - Task: Enforce mandatory tagging for all cloud resources.
- Implementation Details:
- Ensure every Celery task dispatch and Kubernetes pod creation injects labels:
project,cost_center,owner,data_classification(sourced fromalphaswarm.config.settings). - Add a
FinOpsAuditTaskthat scans the current runtime and alerts on untagged resources.
- Ensure every Celery task dispatch and Kubernetes pod creation injects labels:
- D.O.D: Every dollar of cloud spend is attributable to a specific
strategy_idoragent_run_id.
β οΈ Constraints & Guardrailsβ
- Hard Rule: Never call
litellmdirectly; userouter_complete. - Hard Rule: All symbols must be
Symbolinstances; useSymbol.parse(). - Hard Rule: No direct order execution from agents; must emit
Signalobjects to the 5-stage pipeline.