Skip to content

Architecture

Sergey Gerasimov edited this page Oct 22, 2026 · 1 revision

Architecture

DerivLab is a local-first Streamlit application with a deterministic simulation core and an optional LLM orchestration layer.


System map

flowchart LR
    UI["Streamlit UI"] --> DS["Data service<br/>cache · seed · yfinance"]
    UI --> BT["Backtest controller"]
    BT --> BTB["Backtrader engine"]
    BT --> LEDGER["Private portfolio ledger"]
    BT --> METRICS["Metrics<br/>NAV · DD · Sharpe · Sortino"]
    BT --> BRIDGE["AIAgentBridge<br/>breach · assignment"]
    DS --> SYNTH["Synthetic option chain<br/>Black-Scholes · Greeks"]
    SYNTH --> BT
    UI --> ARENA["AI Arena orchestrator"]
    ARENA --> BT
    BRIDGE --> ARENA
    ARENA --> LLM["Optional LLM layer<br/>OpenAI-compatible / Anthropic"]
    ARENA --> REPORT["AlphaVanguard report"]
    ARENA --> NEWS["NewsWire"]
    REPORT --> UI
    NEWS --> UI
Loading

Components

Component Responsibility
derivlab/ui.py Six screens, navigation, session state, live game fragment
derivlab/backtest_engine.py Backtrader wrapper, controller, ledger, NAV, events
derivlab/strategies.py Strategy configuration, position plans, synthetic option legs, Greeks
derivlab/data_fetcher.py yfinance downloads, Parquet cache, bundled seed, synthetic fallback
derivlab/game_mode.py Scenario slicing, alerts, action state machine, scoring
derivlab/metrics.py Return, CAGR, drawdown, Sharpe, Sortino, win rate, profit factor
derivlab/charts.py Plotly payoff, equity, drawdown, live path, arena race
derivlab/theme.py Cyberpunk theme, layout primitives, cards, translation guard
derivlab/agents/ Personas, arena, LLM client, bridge, reports, news, parsing
tests/ Offline pytest, Streamlit AppTest, policy/LLM/bridge/arena tests

Design principles

  1. Offline first. The seed and deterministic fallback make every screen useful without a network or API key.
  2. One accounting truth. The user, the AI desks, and the game all use the same simulation engine and ledger.
  3. No look-ahead. AI decisions use only bars before the scenario start.
  4. Additive AI. An LLM failure never breaks a screen; local output takes over.
  5. Stable frontend. Fixed Plotly identities and no full reruns inside live fragments protect the React tree.

Event flow

  1. run_backtest slices history to the requested lookback.
  2. The controller builds a synthetic option chain and opens a position.
  3. Backtrader calls next() every bar; the controller marks the book, checks expiry, triggers alerts, and records NAV.
  4. Short strike breaches and assignments are emitted to AIAgentBridge.
  5. At the end, metrics and trades are assembled into a BacktestResult.
  6. The AI Arena ranks four results, extracts triggers, builds a JSON payload, and optionally calls the LLM.

Deployment shape

  • One Streamlit process
  • One Parquet cache volume
  • No database
  • No broker connection
  • Optional outbound HTTPS to Yahoo Finance and the configured LLM provider

See Backtest Engine, AI Agents, and Deployment.

Clone this wiki locally