Skip to Content
ProjectsStock Trading Agent Swarm

Stock Trading Agent Swarm

A distributed multi-agent swarm for stock trading simulation — 10 specialized agents coordinating via an async message bus, backtested across 250 trading days on S&P 500 data


⚠️ Disclaimer: This is a simulation project for educational and research purposes only. It does not constitute financial advice. Stock trading involves significant risk. Always consult a qualified financial advisor before making investment decisions.


Problem Statement

We Asked NEO to: Build a distributed multi-agent swarm for stock trading simulation with:

  • Specialized agents for market analysis, trade execution, and risk management
  • An async pub/sub message bus for agent coordination
  • A 250-day backtest on real S&P 500 historical data
  • Dual-layer risk validation — pre-trade approval and post-trade stop-loss monitoring
  • Comprehensive P&L reports with per-agent breakdowns
  • Full Docker containerization for reproducible runs

Solution Overview

NEO built a production-grade multi-agent trading simulation with 10 specialized agents across 4 tiers:

  1. 3 Analyst Agents scan market data and generate BUY/SELL signals using SMA crossovers and volume trend analysis
  2. 4 Trader Agents each manage a $250K portfolio slice — consuming signals, executing trades, and tracking positions independently
  3. 2 Risk Manager Agents handle dual validation — pre-trade order approval and post-trade stop-loss monitoring
  4. 1 Reporter Agent aggregates P&L across all portfolios and generates structured JSON/CSV reports

On $1M capital over 250 trading days, the swarm returned +4.62% ($46,155 profit) with a max drawdown of just 0.46%.


Workflow / Pipeline

StepDescription
1. Data IngestionDataLoader fetches S&P 500 tickers and downloads historical OHLCV data via yfinance — cached as CSVs for reproducible reruns
2. Market Environment InitMarketEnvironment loads historical data, initializes the trading clock, and exposes a day-by-day market feed to all agents via the message bus
3. Signal Generation3 Analyst Agents compute SMA crossovers and volume trends per symbol each day — publishing typed SignalMessage events to the async pub/sub bus
4. Pre-Trade Risk ValidationRisk Managers evaluate each order against position size limits and concentration thresholds before any trade executes — approving or rejecting in real time
5. Trade Execution4 Trader Agents consume approved signals, place orders at current day prices, and update their Portfolio objects with new positions and cash balances
6. Stop-Loss MonitoringAfter each trading day, Risk Managers scan all open positions against STOP_LOSS_PERCENT=0.10 — autonomously closing any that breach the threshold
7. P&L AggregationReporter Agent collects daily snapshots from all 4 Trader portfolios and appends records to reports/daily_pnl.json and reports/trades_history.csv
8. Simulation ReportAt run completion, the Reporter outputs a final summary — total return, max drawdown, trade count, approval rate, and per-agent breakdown

Repository & Artifacts

Generated Artifacts:

  • 10 specialized agent implementations — 3 Analysts, 4 Traders, 2 Risk Managers, 1 Reporter (agents/)
  • Async in-memory pub/sub MessageBus with typed message schemas (core/)
  • MarketEnvironment — day-by-day historical market feed for backtesting
  • Portfolio class with position tracking, cash management, and P&L calculation
  • Pydantic schemas for all message types — SignalMessage, OrderMessage, RiskDecision (core/schemas.py)
  • DataLoader with S&P 500 ticker fetching and yfinance data download (data/)
  • Configurable .env for capital, position limits, stop-loss thresholds, and date ranges
  • Per-agent isolated log files under logs/
  • Structured outputs — daily_pnl.json and trades_history.csv
  • Docker + Docker Compose for one-command reproducible simulation runs
  • 250-day backtest results in DETAILED_SIMULATION_REPORT.md

Technical Details

  • Agent Architecture:

    • All agents extend base_agent.py — shared interface for message subscription, publishing, and lifecycle
    • Each agent runs its own async loop, subscribing only to relevant message types
    • Analyst Agents: configurable SMA windows + volume trend confirmation
    • Trader Agents: signal → order → portfolio update, $250K capital each
    • Risk Managers: pre-trade approval gate + continuous post-trade stop-loss scanning
    • Reporter Agent: daily P&L aggregation + final simulation summary
  • Message Bus:

    • In-memory async pub/sub with typed topic channels
    • Redis-compatible interface — scaling to distributed Redis is a bus swap, not an agent rewrite
    • Message types: MarketData, TradingSignal, OrderRequest, RiskDecision, TradeExecution, PortfolioSnapshot
  • Risk Management:

    • Pre-trade: MAX_POSITION_SIZE=0.5 caps any position at 50% of portfolio value
    • Post-trade: STOP_LOSS_PERCENT=0.10 auto-closes at 10% loss from entry
    • Result: 86.9% approval rate, 26 orders blocked, 20 stop-losses triggered
  • Data Layer:

    • yfinance with configurable ticker list and date range
    • OHLCV data cached as CSVs after first download — no repeated API calls
    • DataLoader auto-fetches S&P 500 constituents (configurable limit)
  • Configuration (.env):

    • INITIAL_CASH — capital per Trader Agent (default: $250,000)
    • MAX_POSITION_SIZE — max single-position fraction (default: 0.5)
    • STOP_LOSS_PERCENT — automatic exit threshold (default: 0.10)
    • DATA_START_DATE / DATA_END_DATE — backtest window

Results

  • Total Return: +4.62% on $1M capital over 250 days ($46,155 net profit)
  • Max Drawdown: 0.46% — risk layer kept downside tightly controlled
  • Trades Executed: 86 across 4 Trader Agents
  • Order Approval Rate: 86.9% — 26 orders blocked for breaching limits
  • Stop-Loss Events: 20 autonomous closures, no manual intervention needed
  • Risk Effectiveness: Zero positions breached the 10% stop-loss threshold

Simulation Summary (250-Day Backtest, $1M Capital)

============================================================
STOCK TRADING AGENT SWARM — SIMULATION RESULTS
============================================================

Capital Configuration:
  Total Capital:          $1,000,000
  Per-Trader Allocation:  $250,000 × 4 agents

Performance Metrics:
  Total Return:           +4.62%  ($46,155 profit)
  Max Drawdown:            0.46%
  Simulation Period:      250 trading days
  Data Source:            S&P 500 historical (yfinance)

Trade Statistics:
  Total Trades Executed:  86
  Order Approval Rate:    86.9%
  Orders Blocked (Risk):  26
  Stop-Losses Triggered:  20

Agent Activity:
  Analyst Agents (×3):   SMA crossovers + volume trend signals
  Trader Agents (×4):    Order execution — $250K portfolios each
  Risk Managers (×2):    Pre-trade approval + stop-loss monitoring
  Reporter Agent (×1):   Daily P&L aggregation + final report
============================================================

Risk Layer Breakdown

Pre-Trade Validation:
  Orders Submitted:     112
  Orders Approved:       86  (86.9%)
  Orders Blocked:        26  (23.2%)
  Block Reasons:         Position size limit (18), Concentration risk (8)

Post-Trade Monitoring:
  Stop-Losses Triggered: 20
  Avg Loss at Trigger:   -8.3%  (within 10% threshold)
  Capital Protected:     Est. $34,000+ in avoided further losses

Best Practices & Lessons Learned

  • Separate analysts from traders — prevents the same logic from generating and executing a signal, which causes overtrading in single-agent setups
  • The message bus is the core abstraction — it’s what makes the swarm genuinely distributed rather than functions called in sequence
  • Two risk layers beat one — pre-trade catches bad orders upfront; post-trade catches position drift that no pre-trade check can predict
  • Isolate logs per agent — a single log file for 10 concurrent agents is unreadable; per-agent files make debugging tractable
  • Cache historical data as CSVs — cuts simulation startup from 30+ seconds to under 2 seconds on reruns
  • Max drawdown proves the risk layer works — early experiments without it saw drawdowns exceeding 8% on the same data
  • Redis-compatible bus costs nothing now — but makes distributed scaling a config change, not a rewrite later
  • Pydantic schemas for every message type — eliminates malformed message bugs at the bus level before they reach agent logic

Next Steps

  • Swap MarketEnvironment for a live broker API (Alpaca, Interactive Brokers) with no agent logic changes
  • Replace SMA crossover signals with LSTM or reinforcement learning-based generation
  • Add a Sentiment Analyst Agent ingesting news feeds and earnings transcripts
  • Scale the message bus to Redis for true multi-machine distributed deployment
  • Extend the symbol universe to crypto or forex — the architecture is data-source agnostic
  • Add portfolio-level risk constraints like sector concentration caps
  • Build a Streamlit or React dashboard for real-time simulation visualization
  • Add walk-forward backtesting to reduce overfitting on the 2023–2024 test period
  • Implement agent communication logging for post-mortem trade decision analysis

References