# rohan1090/trading_workstation architecture

> Architecture diagram of the GitHub repository rohan1090/trading_workstation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rohan1090/trading_workstation
- Repository: https://github.com/rohan1090/trading_workstation

## Overview

1. **What this repo is**
   - Trading Workstation is a terminal-first Python trading platform for strategy development, backtesting, paper trading, and live execution.
   - The architecture is centered on one strategy code path that can run unchanged across modes: backtest, paper, and live.

2. **Primary control plane**
   - The main entry point is the `tw` CLI in `src/trading_workstation/cli.py`.
   - `bootstrap.py` wires together configuration, providers, execution adapters, runtime state, persistence, and optional TUI depending on the selected mode.

3. **Core domain boundary**
   - The `models/` package defines broker-agnostic Pydantic types for market data, orders, positions, accounts, signals, and backtest results.
   - This is the main boundary that keeps strategies and runtime logic independent from Schwab, Alpaca, Yahoo, or any fake/test implementation.

4. **Strategy layer**
   - Strategies live under `strategies/` and are dynamically loaded via `src/trading_workstation/strategy/loader.py`.
   - A strategy implements the `BaseStrategy` contract and interacts only through `StrategyContext`, which is read-only and exposes recent bars, latest quotes, positions, account state, and logging.
   - Strategies emit `TargetIntent` objects; they do not submit orders directly.

5. **Runtime orchestration**
   - `runtime/engine.py` is the central orchestrator for live and paper modes.
   - Its cycle is: fetch market data, invoke strategy callbacks, convert intents into orders, submit through the execution adapter, update runtime state, and publish events.
   - `runtime/state.py` holds the mutable trading state, while `runtime/events.py` provides the synchronous event bus used for observability and UI updates.
   - `runtime/scheduler.py` provides timed execution and optional market-hours awareness.

6. **Market data providers**
   - `providers/` abstracts quote, bar, and option-chain retrieval behind a common `MarketDataProvider`.
   - The concrete providers are Schwab OAuth/API, Yahoo Finance fallback, and deterministic fake data for tests/demo.
   - Provider selection is automatic, so architecture-wise the data source is swappable without changing strategy code.

7. **Execution adapters**
   - `execution/` abstracts order submission behind `ExecutionAdapter`.
   - The main adapters are fake simulated fills, Alpaca paper trading, and Schwab live trading.
   - `execution/router.py` chooses the adapter based on mode, while `live_schwab.py` adds safety guardrails such as dry-run defaults, kill switch, and order/session/value limits.

8. **Backtesting subsystem**
   - `backtest/engine.py` runs strategies over historical bars and simulates fills through `fills.py`.
   - `metrics.py` computes performance measures like return, drawdown, Sharpe, win rate, and profit factor.
   - `reports.py` writes backtest artifacts and CLI summaries, typically into `data/artifacts/`.
   - This subsystem is separate from live runtime but reuses the same strategy and model abstractions.

9. **Persistence and audit trail**
   - `persistence/journal.py` provides an append-only JSONL trade journal for intents, orders, and fills.
   - `persistence/sqlite_store.py` stores operational data locally in SQLite, including orders, fills, positions, equity snapshots, and events.
   - Both are fed from the event/state stream, making persistence an observer rather than a trading dependency.

10. **TUI and observability**
   - `tui/app.py` is a Textual-based terminal UI with multiple screens for watchlist, symbol detail, option chain, strategy status, orders, account, and logs.
   - The TUI reads from `RuntimeState` and subscribes to the `EventBus`; it does not call broker APIs directly.
   - Presenters and widgets handle formatting and visualization, while the runtime remains the source of truth.

11. **Configuration and environment**
   - Static configuration lives in YAML under `configs/`, with profile overrides for backtest, paper, and live modes plus a symbols watchlist.
   - Environment variables supply credentials and live-trading settings, especially for Schwab and Alpaca.
   - `config/loader.py` and `config/models.py` validate and normalize this configuration into typed settings.

12. **Safety and testing emphasis**
   - Live trading is explicitly protected by default-dry-run behavior and guardrails in the Schwab executor.
   - The repo includes extensive unit tests for the strategy loader, runtime engine, event bus, execution routing, providers, backtest engine, persistence, and TUI presenters.
   - Architecturally, the tests reinforce the same boundary structure: strategy, runtime, provider, execution, persistence, and UI are all independently verified.

Main entry points: [`src/trading_workstation/bootstrap.py`](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/bootstrap.py) (Bootstrap), [`src/trading_workstation/runtime/engine.py`](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/engine.py) (Runtime engine), [`src/trading_workstation/backtest/engine.py`](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/engine.py) (Backtest engine), [`src/trading_workstation/providers`](https://github.com/rohan1090/trading_workstation/tree/main/src/trading_workstation/providers) (Market providers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_cli["Control plane"]
  node_n_cli["tw CLI<br/>entrypoint<br/>[cli.py]"]
  node_n_bootstrap["Bootstrap<br/>wiring<br/>[bootstrap.py]"]
end

subgraph group_grp_domain["Domain"]
  node_n_models[("Core models<br/>domain types")]
  node_n_strategy_base["Strategy contract<br/>strategy API<br/>[base.py]"]
  node_n_strategy_loader["Strategy loader<br/>plugin loader<br/>[loader.py]"]
  node_n_strategy_examples["Example strategies"]
end

subgraph group_grp_runtime["Runtime"]
  node_n_runtime_engine["Runtime engine<br/>orchestrator<br/>[engine.py]"]
  node_n_runtime_state[("Runtime state<br/>mutable state<br/>[state.py]")]
  node_n_event_bus(("Event bus<br/>events<br/>[events.py]"))
  node_n_scheduler["Scheduler<br/>timing<br/>[scheduler.py]"]
end

subgraph group_grp_data["Data & execution"]
  node_n_providers[("Market providers<br/>data sources")]
  node_n_provider_schwab["Schwab data<br/>market data<br/>[schwab.py]"]
  node_n_provider_yahoo["Yahoo fallback<br/>market data<br/>[yahoo_fallback.py]"]
  node_n_provider_fake["Fake data<br/>market data<br/>[fake.py]"]
  node_n_exec_router["Execution router<br/>adapter selection<br/>[router.py]"]
  node_n_exec_fake["Fake fills<br/>execution<br/>[fake.py]"]
  node_n_exec_paper["Paper adapter<br/>execution<br/>[paper_alpaca.py]"]
  node_n_exec_live["Live Schwab<br/>execution<br/>[live_schwab.py]"]
end

subgraph group_grp_backtest["Backtesting"]
  node_n_backtest_engine["Backtest engine<br/>simulation<br/>[engine.py]"]
  node_n_backtest_metrics["Metrics<br/>analytics<br/>[metrics.py]"]
  node_n_backtest_reports["Reports<br/>reporting<br/>[reports.py]"]
end

subgraph group_grp_persist["Persistence"]
  node_n_journal["Trade journal<br/>audit log<br/>[journal.py]"]
  node_n_sqlite_store[("SQLite store<br/>local store<br/>[sqlite_store.py]")]
end

subgraph group_grp_tui["TUI"]
  node_n_tui_app["Terminal UI<br/>ui shell<br/>[app.py]"]
  node_n_tui_presenters["Presenters<br/>formatting<br/>[formatters.py]"]
end

subgraph group_grp_config["Config"]
  node_n_config_loader["Config loader<br/>settings<br/>[loader.py]"]
  node_n_config_models["Config models<br/>settings schema<br/>[models.py]"]
end

node_n_cli -->|"starts"| node_n_bootstrap
node_n_bootstrap -->|"loads"| node_n_config_loader
node_n_bootstrap -->|"loads strategies"| node_n_strategy_loader
node_n_bootstrap -->|"selects data"| node_n_providers
node_n_bootstrap -->|"selects execution"| node_n_exec_router
node_n_bootstrap -->|"starts runtime"| node_n_runtime_engine
node_n_bootstrap -->|"starts backtest"| node_n_backtest_engine
node_n_bootstrap -->|"optional UI"| node_n_tui_app
node_n_config_loader -->|"validates"| node_n_config_models
node_n_strategy_loader -->|"instantiates"| node_n_strategy_base
node_n_strategy_examples -->|"implements"| node_n_strategy_base
node_n_strategy_base -->|"uses types"| node_n_models
node_n_runtime_engine -->|"fetches market data"| node_n_providers
node_n_runtime_engine -->|"invokes"| node_n_strategy_base
node_n_runtime_engine -->|"updates"| node_n_runtime_state
node_n_runtime_engine -->|"publishes"| node_n_event_bus
node_n_runtime_engine -->|"submits intents"| node_n_exec_router
node_n_runtime_engine -->|"ticks"| node_n_scheduler
node_n_providers -->|"backed by"| node_n_provider_schwab
node_n_providers -->|"fallback"| node_n_provider_yahoo
node_n_providers -->|"test source"| node_n_provider_fake
node_n_exec_router -->|"routes to"| node_n_exec_fake
node_n_exec_router -->|"routes to"| node_n_exec_paper
node_n_exec_router -->|"routes to"| node_n_exec_live
node_n_backtest_engine -->|"reuses types"| node_n_models
node_n_backtest_engine -->|"simulates fills"| node_n_exec_fake
node_n_backtest_engine -->|"computes"| node_n_backtest_metrics
node_n_backtest_engine -->|"writes reports"| node_n_backtest_reports
node_n_runtime_state -->|"observed by"| node_n_journal
node_n_runtime_state -->|"persisted to"| node_n_sqlite_store
node_n_event_bus -->|"feeds UI"| node_n_tui_app
node_n_runtime_state -->|"read by"| node_n_tui_app
node_n_tui_app -->|"formats with"| node_n_tui_presenters
node_n_exec_live -->|"uses types"| node_n_models
node_n_provider_schwab -->|"uses types"| node_n_models

click node_n_cli "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/cli.py"
click node_n_bootstrap "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/bootstrap.py"
click node_n_config_loader "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/config/loader.py"
click node_n_config_models "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/config/models.py"
click node_n_models "https://github.com/rohan1090/trading_workstation/tree/main/src/trading_workstation/models"
click node_n_strategy_base "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/strategy/base.py"
click node_n_strategy_loader "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/strategy/loader.py"
click node_n_strategy_examples "https://github.com/rohan1090/trading_workstation/blob/main/strategies/example_sma_cross.py"
click node_n_runtime_engine "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/engine.py"
click node_n_runtime_state "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/state.py"
click node_n_event_bus "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/events.py"
click node_n_scheduler "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/scheduler.py"
click node_n_providers "https://github.com/rohan1090/trading_workstation/tree/main/src/trading_workstation/providers"
click node_n_provider_schwab "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/schwab.py"
click node_n_provider_yahoo "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/yahoo_fallback.py"
click node_n_provider_fake "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/fake.py"
click node_n_exec_router "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/router.py"
click node_n_exec_fake "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/fake.py"
click node_n_exec_paper "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/paper_alpaca.py"
click node_n_exec_live "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/live_schwab.py"
click node_n_backtest_engine "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/engine.py"
click node_n_backtest_metrics "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/metrics.py"
click node_n_backtest_reports "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/reports.py"
click node_n_journal "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/persistence/journal.py"
click node_n_sqlite_store "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/persistence/sqlite_store.py"
click node_n_tui_app "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/tui/app.py"
click node_n_tui_presenters "https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/tui/presenters/formatters.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_cli,node_n_bootstrap,node_n_tui_app,node_n_tui_presenters toneBlue
class node_n_models,node_n_strategy_base,node_n_strategy_loader,node_n_strategy_examples,node_n_config_loader,node_n_config_models toneAmber
class node_n_runtime_engine,node_n_runtime_state,node_n_event_bus,node_n_scheduler toneMint
class node_n_providers,node_n_provider_schwab,node_n_provider_yahoo,node_n_provider_fake,node_n_exec_router,node_n_exec_fake,node_n_exec_paper,node_n_exec_live toneRose
class node_n_backtest_engine,node_n_backtest_metrics,node_n_backtest_reports toneIndigo
class node_n_journal,node_n_sqlite_store toneTeal
```

## Components

### Control plane

Entry points and bootstrap wiring

- [tw CLI](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/cli.py): entrypoint · Command-line interface for running the workstation in different modes. · `src/trading_workstation/cli.py`
- [Bootstrap](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/bootstrap.py): wiring · Assembles config, providers, execution, runtime state, persistence, and optional UI. · `src/trading_workstation/bootstrap.py`

### Domain

Strategy contracts and shared models

- [Core models](https://github.com/rohan1090/trading_workstation/tree/main/src/trading_workstation/models): domain types · Broker-agnostic data structures for market data, orders, positions, accounts, signals, and results. · `src/trading_workstation/models`
- [Strategy contract](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/strategy/base.py): strategy API · Base strategy interface and read-only context used by strategy code. · `src/trading_workstation/strategy/base.py`
- [Strategy loader](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/strategy/loader.py): plugin loader · Discovers and loads strategy implementations dynamically. · `src/trading_workstation/strategy/loader.py`
- [Example strategies](https://github.com/rohan1090/trading_workstation/blob/main/strategies/example_sma_cross.py): strategies · Sample strategies demonstrating the strategy contract. · `strategies/example_sma_cross.py`

### Runtime

Live/paper orchestration and state

- [Runtime engine](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/engine.py): orchestrator · Coordinates fetch, strategy callback, intent conversion, execution, state updates, and events. · `src/trading_workstation/runtime/engine.py`
- [Runtime state](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/state.py): mutable state · In-memory trading state shared across the runtime and UI. · `src/trading_workstation/runtime/state.py`
- [Event bus](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/events.py): events · Synchronous event stream for observability and UI updates. · `src/trading_workstation/runtime/events.py`
- [Scheduler](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/runtime/scheduler.py): timing · Timed execution and market-hours-aware scheduling. · `src/trading_workstation/runtime/scheduler.py`

### Data & execution

Market data sources and order adapters

- [Market providers](https://github.com/rohan1090/trading_workstation/tree/main/src/trading_workstation/providers): data sources · Swappable market data providers for bars, quotes, and option chains. · `src/trading_workstation/providers`
- [Schwab data](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/schwab.py): market data · Schwab-backed market data provider. · `src/trading_workstation/providers/schwab.py`
- [Yahoo fallback](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/yahoo_fallback.py): market data · Fallback provider for public market data. · `src/trading_workstation/providers/yahoo_fallback.py`
- [Fake data](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/providers/fake.py): market data · Deterministic provider for tests and demos. · `src/trading_workstation/providers/fake.py`
- [Execution router](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/router.py): adapter selection · Chooses the execution backend for backtest, paper, or live mode. · `src/trading_workstation/execution/router.py`
- [Fake fills](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/fake.py): execution · Simulated order fills used for testing and non-live runs. · `src/trading_workstation/execution/fake.py`
- [Paper adapter](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/paper_alpaca.py): execution · Alpaca paper-trading execution adapter. · `src/trading_workstation/execution/paper_alpaca.py`
- [Live Schwab](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/execution/live_schwab.py): execution · Schwab live-trading adapter with guardrails and safety limits. · `src/trading_workstation/execution/live_schwab.py`

### Backtesting

Historical simulation and reporting

- [Backtest engine](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/engine.py): simulation · Runs strategies over historical bars and simulates fills. · `src/trading_workstation/backtest/engine.py`
- [Metrics](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/metrics.py): analytics · Computes performance statistics such as return, drawdown, Sharpe, win rate, and profit factor. · `src/trading_workstation/backtest/metrics.py`
- [Reports](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/backtest/reports.py): reporting · Writes backtest artifacts and CLI summaries. · `src/trading_workstation/backtest/reports.py`

### Persistence

Audit trail and local storage

- [Trade journal](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/persistence/journal.py): audit log · Append-only JSONL journal for intents, orders, and fills. · `src/trading_workstation/persistence/journal.py`
- [SQLite store](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/persistence/sqlite_store.py): local store · Local operational store for orders, fills, positions, equity snapshots, and events. · `src/trading_workstation/persistence/sqlite_store.py`

### TUI

Terminal UI and presentation

- [Terminal UI](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/tui/app.py): ui shell · Textual-based application shell for watchlist, strategy, orders, account, option chain, and logs. · `src/trading_workstation/tui/app.py`
- [Presenters](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/tui/presenters/formatters.py): formatting · Formatting and visualization helpers for UI screens and widgets. · `src/trading_workstation/tui/presenters/formatters.py`

### Config

Typed configuration loading

- [Config loader](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/config/loader.py): settings · Loads and normalizes profile and environment-based configuration. · `src/trading_workstation/config/loader.py`
- [Config models](https://github.com/rohan1090/trading_workstation/blob/main/src/trading_workstation/config/models.py): settings schema · Typed configuration definitions and validation models. · `src/trading_workstation/config/models.py`

## Connections

- tw CLI → Bootstrap: starts · CLI hands off to bootstrap for mode-specific assembly.
- Bootstrap → Config loader: loads · Bootstrap loads configuration before wiring subsystems.
- Bootstrap → Strategy loader: loads strategies · Bootstrap resolves the selected strategy implementation.
- Bootstrap → Market providers: selects data · Bootstrap wires the market data provider.
- Bootstrap → Execution router: selects execution · Bootstrap chooses the execution adapter for the mode.
- Bootstrap → Runtime engine: starts runtime · Bootstrap launches live or paper orchestration.
- Bootstrap → Backtest engine: starts backtest · Bootstrap launches historical simulation when requested.
- Bootstrap → Terminal UI: optional UI · Bootstrap starts the terminal UI when enabled.
- Config loader → Config models: validates · Loaded settings are normalized into typed models.
- Strategy loader → Strategy contract: instantiates · Loaded strategies must conform to the base contract.
- Example strategies → Strategy contract: implements · Example strategies demonstrate the shared strategy interface.
- Strategy contract → Core models: uses types · Strategies exchange domain objects through the shared model boundary.
- Runtime engine → Market providers: fetches market data · Runtime asks the provider for the latest market inputs.
- Runtime engine → Strategy contract: invokes · Runtime calls strategy logic through the standard contract.
- Runtime engine → Runtime state: updates · Runtime mutates the canonical in-memory trading state.
- Runtime engine → Event bus: publishes · Runtime emits events for observability and UI consumers.
- Runtime engine → Execution router: submits intents · Runtime converts intents into orders via the execution layer.
- Runtime engine → Scheduler: ticks · Scheduler drives timed engine cycles.
- Market providers → Schwab data: backed by · Schwab is the primary market data source.
- Market providers → Yahoo fallback: fallback · Yahoo acts as a fallback source.
- Market providers → Fake data: test source · Fake provider supports tests and demo runs.
- Execution router → Fake fills: routes to · Fake execution is used for simulation and tests.
- Execution router → Paper adapter: routes to · Paper mode uses the Alpaca adapter.
- Execution router → Live Schwab: routes to · Live mode uses the Schwab adapter.
- Backtest engine → Core models: reuses types · Backtesting shares the same market and order models.
- Backtest engine → Fake fills: simulates fills · Backtest fill behavior is handled by the simulation layer.
- Backtest engine → Metrics: computes · Backtest output is summarized by metrics calculations.
- Backtest engine → Reports: writes reports · Results are emitted as artifacts and summaries.
- Runtime state → Trade journal: observed by · State and intent streams are journaled for auditability.
- Runtime state → SQLite store: persisted to · Operational snapshots are stored locally in SQLite.
- Event bus → Terminal UI: feeds UI · The UI subscribes to runtime events.
- Runtime state → Terminal UI: read by · The UI renders current trading state without broker access.
- Terminal UI → Presenters: formats with · Screens use presenters for display formatting.
- Live Schwab → Core models: uses types · Execution adapters operate on shared broker-agnostic models.
- Schwab data → Core models: uses types · Provider output is normalized into the shared domain boundary.
