# cbrown35/tradingsystem architecture

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

- Interactive diagram: https://gitdiagram.com/cbrown35/tradingsystem
- Repository: https://github.com/cbrown35/tradingsystem

## Overview

1. **Solution shape**
   - This is a multi-project .NET 8 solution centered on a trading platform, with clear separation between contracts, business logic, persistence, live trading, strategy research, and the host application.
   - The main runtime host appears to be `src/TradingSystem.Console`, while the rest of the projects act as libraries plugged in through dependency injection.

2. **Shared domain boundary: `src/Common`**
   - `Common` is the cross-cutting contract layer. It defines the core interfaces (`ITradingService`, `IExchangeAdapter`, `IBacktester`, `IRiskManager`, repositories, strategy interfaces) and the shared domain models (`Order`, `Trade`, `MarketData`, `Signal`, `BacktestResult`, `RiskMetrics`, etc.).
   - This is the main architectural boundary: other projects implement these interfaces or consume these models, which keeps the trading logic decoupled from infrastructure and exchange specifics.

3. **Application orchestration: `src/Core`**
   - `Core` contains coordination and monitoring-focused business logic.
   - `TradingSystemCoordinator` is the likely top-level orchestrator for running trading workflows.
   - The monitoring subtree is a substantial subsystem of its own: health checks, middleware, auth policy for protected health endpoints, notification/storage services, and a small UI/static asset layer under `wwwroot`.

4. **Live trading runtime: `src/RealTrading`**
   - `RealTrading` contains the execution path for real or simulated trading.
   - Important services include `TradingService`, `RiskManager`, `RiskValidationService`, `MarketDataService`, `MarketDataCacheService`, and adapters for different execution targets: `SimulatedExchangeAdapter`, `TradovateAdapter`, and `TradingViewAdapter`.
   - `PineScriptGenerator` suggests the system can also generate TradingView-compatible strategy artifacts, which bridges strategy logic into external charting/execution tooling.

5. **Strategy research and optimization: `src/StrategySearch`**
   - This project is the research/backtesting engine.
   - It includes `Backtester`, `TheoryGenerator`, `StrategyOptimizer`, `StrategySearchService`, and a dedicated backtesting pipeline with metrics calculation, validation, parameter optimization, and trade execution simulation.
   - The included strategies (`SimpleMovingAverageStrategy`, `TheoryBasedStrategy`) indicate this layer is where strategies are developed, scored, and tuned before being promoted to live trading.

6. **Persistence and data access: `src/Infrastructure`**
   - `Infrastructure` owns the database boundary.
   - It provides EF Core database contexts (`TradingContext`, `DevelopmentTradingContext`), a design-time factory, repository implementations for market data, orders, and trades, and DI wiring.
   - Migrations include explicit TimescaleDB support, which signals that market/time-series data storage is a first-class concern rather than an afterthought.

7. **Trading data flow**
   - The typical live flow is: market data enters through `RealTrading` services/adapters, is cached/validated, passed through strategy and risk logic, then executed via an exchange adapter.
   - Orders and trades are persisted through repository interfaces into the infrastructure database layer.
   - The same shared models are reused by backtesting and monitoring, so performance and health views operate on consistent domain objects.

8. **Backtesting and optimization flow**
   - Historical market data is pulled from the persistence layer through repository abstractions.
   - `StrategySearch` runs strategy logic against this data, computes metrics, validates outcomes, and searches parameter space for better configurations.
   - The result is a feedback loop from research into live trading: strategies are evaluated offline before being used by the execution layer.

9. **Observability and operational monitoring**
   - The repo includes a dedicated monitoring subsystem plus containerized observability assets.
   - Prometheus configuration, alert rules, and templates are stored under `prometheus/`; Grafana dashboards and datasource provisioning live under `grafana/`.
   - This means operational state is intended to be observed externally, with trading health and alerts integrated into metrics/alerting rather than only logs.

10. **Application entrypoint and API surface**
    - `TradingSystem.Console` is not just a CLI shell; it contains controllers and a factory/service wiring layer, so it likely hosts an HTTP API as well.
    - The README documents endpoints for placing orders, closing orders, listing open orders, fetching market data, and checking balances, which are the system’s primary external control surface.

11. **Environment, deployment, and local operations**
    - Docker files and compose definitions indicate the system is designed to run in containers.
    - Setup scripts initialize databases, TimescaleDB support, and secrets, while `config/secrets.template.json` and environment-specific appsettings files separate sensitive runtime configuration from code.
    - The repo also implies supporting services like PostgreSQL and likely Redis for caching/operational state, with config driven setup rather than hardcoded endpoints.

12. **Testing and documentation boundaries**
    - Tests are currently focused in `tests/TradingSystem.Tests`, which suggests API and integration coverage around the host/application surface.
    - The `docs/` and `wiki/` trees are extensive and map directly to architecture, setup, backtesting, risk, and strategy guidance, which helps indicate that the codebase is organized around operational trading workflows rather than a single monolithic app.

Main entry points: [`src/RealTrading/RealTrading.csproj`](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/RealTrading.csproj) (RealTrading), [`src/StrategySearch/StrategySearch.csproj`](https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/StrategySearch.csproj) (StrategySearch), [`src/Core/Monitoring/MonitoringService.cs`](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/MonitoringService.cs) (Monitoring), [`src/Infrastructure/Infrastructure.csproj`](https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Infrastructure.csproj) (Infrastructure)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_shared["Shared Domain"]
  node_node_common["Common<br/>contracts/models<br/>[Common.csproj]"]
  node_node_common_interfaces["Interfaces<br/>contracts<br/>[ITradingService.cs]"]
  node_node_common_models["Models<br/>[Order.cs]"]
end

subgraph group_group_host["Host App"]
  node_node_console["Console Host<br/>api/host<br/>[Program.cs]"]
  node_node_console_controller["Trading API<br/>controller"]
  node_node_console_factory["Factory<br/>composition root"]
end

subgraph group_group_core["Core Orchestration"]
  node_node_coordinator["Coordinator<br/>orchestrator"]
  node_node_monitoring["Monitoring<br/>health subsystem"]
  node_node_health_api["Health API<br/>health controller"]
  node_node_health_checks["Checks"]
  node_node_health_mw["Health Middleware<br/>middleware pipeline"]
end

subgraph group_group_live["Live Trading"]
  node_node_realtrading["RealTrading<br/>execution runtime<br/>[RealTrading.csproj]"]
  node_node_marketdata["Market Data<br/>feed/cache"]
  node_node_risk["Risk Control<br/>risk service<br/>[RiskManager.cs]"]
  node_node_exchange["Exchange Adapters"]
  node_node_pinescript["Pine Script<br/>strategy export"]
end

subgraph group_group_research["Strategy Research"]
  node_node_strategysearch["StrategySearch<br/>research engine"]
  node_node_backtesting["Backtesting<br/>pipeline"]
  node_node_strategies["Strategies<br/>strategy set"]
end

subgraph group_group_data["Persistence"]
  node_node_infrastructure["Infrastructure<br/>data access"]
  node_node_db[("Db Contexts<br/>ef core<br/>[TradingContext.cs]")]
  node_node_repos["Repositories<br/>persistence layer"]
  node_node_migrations["Migrations<br/>schema changes"]
end

subgraph group_group_ops["Operations"]
  node_node_prometheus["Prometheus<br/>metrics/alerts<br/>[prometheus.yml]"]
  node_node_grafana["Grafana<br/>dashboards"]
  node_node_docker["Docker<br/>deployment<br/>[docker-compose.yml]"]
end

node_node_console -->|"composes"| node_node_console_factory
node_node_console_factory -->|"starts"| node_node_coordinator
node_node_console_factory -->|"injects"| node_node_realtrading
node_node_console_factory -->|"injects"| node_node_strategysearch
node_node_console_controller -->|"calls"| node_node_coordinator
node_node_coordinator -->|"orchestrates"| node_node_realtrading
node_node_coordinator -->|"coordinates"| node_node_strategysearch
node_node_realtrading -->|"uses"| node_node_marketdata
node_node_realtrading -->|"guards with"| node_node_risk
node_node_realtrading -->|"executes via"| node_node_exchange
node_node_realtrading -->|"exports"| node_node_pinescript
node_node_realtrading -->|"implements"| node_node_common_interfaces
node_node_strategysearch -->|"runs"| node_node_backtesting
node_node_strategysearch -->|"evaluates"| node_node_strategies
node_node_strategysearch -->|"consumes"| node_node_common_models
node_node_infrastructure -->|"hosts"| node_node_db
node_node_infrastructure -->|"implements"| node_node_repos
node_node_infrastructure -->|"evolves"| node_node_migrations
node_node_repos -->|"implements"| node_node_common_interfaces
node_node_db -->|"persists"| node_node_common_models
node_node_realtrading -->|"persists through"| node_node_infrastructure
node_node_strategysearch -->|"reads via"| node_node_infrastructure
node_node_monitoring -->|"exposes"| node_node_health_api
node_node_monitoring -->|"runs"| node_node_health_checks
node_node_monitoring -->|"protects with"| node_node_health_mw
node_node_monitoring -->|"reports"| node_node_common_models
node_node_console -->|"hosts"| node_node_monitoring
node_node_prometheus -->|"observes"| node_node_monitoring
node_node_grafana -->|"visualizes"| node_node_prometheus
node_node_docker -->|"runs"| node_node_console
node_node_docker -->|"provisions"| node_node_infrastructure

click node_node_common "https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Common.csproj"
click node_node_common_interfaces "https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Interfaces/ITradingService.cs"
click node_node_common_models "https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Models/Order.cs"
click node_node_console "https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Program.cs"
click node_node_console_controller "https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Controllers/TradingController.cs"
click node_node_console_factory "https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Services/TradingSystemFactory.cs"
click node_node_coordinator "https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Services/TradingSystemCoordinator.cs"
click node_node_monitoring "https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/MonitoringService.cs"
click node_node_health_api "https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/Controllers/HealthCheckController.cs"
click node_node_health_checks "https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/HealthChecks/ComponentHealthChecks.cs"
click node_node_health_mw "https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/Middleware/HealthCheckMiddlewarePipeline.cs"
click node_node_realtrading "https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/RealTrading.csproj"
click node_node_marketdata "https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/MarketDataService.cs"
click node_node_risk "https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/RiskManager.cs"
click node_node_exchange "https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/SimulatedExchangeAdapter.cs"
click node_node_pinescript "https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/PineScriptGenerator.cs"
click node_node_strategysearch "https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/StrategySearch.csproj"
click node_node_backtesting "https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/Services/Backtesting/TheoryBacktester.cs"
click node_node_strategies "https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/Strategies/SimpleMovingAverageStrategy.cs"
click node_node_infrastructure "https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Infrastructure.csproj"
click node_node_db "https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Data/TradingContext.cs"
click node_node_repos "https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Repositories/MarketDataRepository.cs"
click node_node_migrations "https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Migrations/20241222053516_AddTimescaleDBSupport.cs"
click node_node_prometheus "https://github.com/cbrown35/tradingsystem/blob/main/prometheus/prometheus.yml"
click node_node_grafana "https://github.com/cbrown35/tradingsystem/blob/main/grafana/provisioning/dashboards/trading_dashboard.json"
click node_node_docker "https://github.com/cbrown35/tradingsystem/blob/main/docker-compose.yml"

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_node_common,node_node_common_interfaces,node_node_common_models,node_node_prometheus,node_node_grafana,node_node_docker toneBlue
class node_node_console,node_node_console_controller,node_node_console_factory toneAmber
class node_node_coordinator,node_node_monitoring,node_node_health_api,node_node_health_checks,node_node_health_mw toneMint
class node_node_realtrading,node_node_marketdata,node_node_risk,node_node_exchange,node_node_pinescript toneRose
class node_node_strategysearch,node_node_backtesting,node_node_strategies toneIndigo
class node_node_infrastructure,node_node_db,node_node_repos,node_node_migrations toneTeal
```

## Components

### Shared Domain

Contracts and shared trading models used across the solution.

- [Common](https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Common.csproj): contracts/models · Shared interfaces and domain models for trading, backtesting, and persistence. · `src/Common/Common.csproj`
- [Interfaces](https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Interfaces/ITradingService.cs): contracts · Core abstractions like trading, exchange, risk, backtesting, and repositories. · `src/Common/Interfaces/ITradingService.cs`
- [Models](https://github.com/cbrown35/tradingsystem/blob/main/src/Common/Models/Order.cs): domain models · Orders, trades, market data, signals, metrics, and health check models. · `src/Common/Models/Order.cs`

### Host App

Entry point and API surface for the trading system.

- [Console Host](https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Program.cs): api/host · Main runtime host with controller surface and application startup. · `src/TradingSystem.Console/Program.cs`
- [Trading API](https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Controllers/TradingController.cs): controller · Primary command surface for orders, market data, and account actions. · `src/TradingSystem.Console/Controllers/TradingController.cs`
- [Factory](https://github.com/cbrown35/tradingsystem/blob/main/src/TradingSystem.Console/Services/TradingSystemFactory.cs): composition root · Builds and wires the trading system runtime. · `src/TradingSystem.Console/Services/TradingSystemFactory.cs`

### Core Orchestration

Trading coordination and monitoring subsystem.

- [Coordinator](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Services/TradingSystemCoordinator.cs): orchestrator · Top-level workflow coordinator for trading execution. · `src/Core/Services/TradingSystemCoordinator.cs`
- [Monitoring](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/MonitoringService.cs): health subsystem · Health checks, notifications, middleware, and UI for operational status. · `src/Core/Monitoring/MonitoringService.cs`
- [Health API](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/Controllers/HealthCheckController.cs): health controller · Protected health-check endpoint surface. · `src/Core/Monitoring/Controllers/HealthCheckController.cs`
- [Checks](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/HealthChecks/ComponentHealthChecks.cs): health checks · Component, market data, backtester, risk, and strategy health checks. · `src/Core/Monitoring/HealthChecks/ComponentHealthChecks.cs`
- [Health Middleware](https://github.com/cbrown35/tradingsystem/blob/main/src/Core/Monitoring/Middleware/HealthCheckMiddlewarePipeline.cs): middleware pipeline · Caching, logging, compression, and rate limiting for health endpoints. · `src/Core/Monitoring/Middleware/HealthCheckMiddlewarePipeline.cs`

### Live Trading

Execution path for market data, risk, and exchange adapters.

- [RealTrading](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/RealTrading.csproj): execution runtime · Live trading services, adapters, caching, and risk enforcement. · `src/RealTrading/RealTrading.csproj`
- [Market Data](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/MarketDataService.cs): feed/cache · Market data ingestion and cache services. · `src/RealTrading/Services/MarketDataService.cs`
- [Risk Control](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/RiskManager.cs): risk service · Risk management and validation for live orders. · `src/RealTrading/Services/RiskManager.cs`
- [Exchange Adapters](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/SimulatedExchangeAdapter.cs): adapters · Execution targets for simulation, Tradovate, and TradingView. · `src/RealTrading/Services/SimulatedExchangeAdapter.cs`
- [Pine Script](https://github.com/cbrown35/tradingsystem/blob/main/src/RealTrading/Services/PineScriptGenerator.cs): strategy export · Generates TradingView-compatible strategy artifacts. · `src/RealTrading/Services/PineScriptGenerator.cs`

### Strategy Research

Backtesting, optimization, and strategy generation.

- [StrategySearch](https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/StrategySearch.csproj): research engine · Backtesting, strategy optimization, and theory generation. · `src/StrategySearch/StrategySearch.csproj`
- [Backtesting](https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/Services/Backtesting/TheoryBacktester.cs): pipeline · Metrics, validation, parameter optimization, and trade execution simulation. · `src/StrategySearch/Services/Backtesting/TheoryBacktester.cs`
- [Strategies](https://github.com/cbrown35/tradingsystem/blob/main/src/StrategySearch/Strategies/SimpleMovingAverageStrategy.cs): strategy set · Reference strategies used for research and optimization. · `src/StrategySearch/Strategies/SimpleMovingAverageStrategy.cs`

### Persistence

Database context, repositories, and migrations.

- [Infrastructure](https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Infrastructure.csproj): data access · EF Core contexts, repositories, and persistence wiring. · `src/Infrastructure/Infrastructure.csproj`
- [Db Contexts](https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Data/TradingContext.cs): ef core · Trading and development database contexts plus factory support. · `src/Infrastructure/Data/TradingContext.cs`
- [Repositories](https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Repositories/MarketDataRepository.cs): persistence layer · Market data, order, and trade repository implementations. · `src/Infrastructure/Repositories/MarketDataRepository.cs`
- [Migrations](https://github.com/cbrown35/tradingsystem/blob/main/src/Infrastructure/Migrations/20241222053516_AddTimescaleDBSupport.cs): schema changes · Database schema evolution including TimescaleDB support. · `src/Infrastructure/Migrations/20241222053516_AddTimescaleDBSupport.cs`

### Operations

Container, metrics, alerting, and deployment assets.

- [Prometheus](https://github.com/cbrown35/tradingsystem/blob/main/prometheus/prometheus.yml): metrics/alerts · Alert rules, recording rules, and notification templates. · `prometheus/prometheus.yml`
- [Grafana](https://github.com/cbrown35/tradingsystem/blob/main/grafana/provisioning/dashboards/trading_dashboard.json): dashboards · Dashboard and datasource provisioning for operational monitoring. · `grafana/provisioning/dashboards/trading_dashboard.json`
- [Docker](https://github.com/cbrown35/tradingsystem/blob/main/docker-compose.yml): deployment · Container build and compose definitions for local and production runs. · `docker-compose.yml`

## Connections

- Console Host → Factory: composes · Host builds the runtime via the factory.
- Factory → Coordinator: starts · Factory wires the main trading coordinator.
- Factory → RealTrading: injects · Live trading services are registered into the host.
- Factory → StrategySearch: injects · Research services are also available through DI.
- Trading API → Coordinator: calls · API requests are routed into orchestration.
- Coordinator → RealTrading: orchestrates · Coordinator drives the live execution runtime.
- Coordinator → StrategySearch: coordinates · Coordinator can invoke research/backtesting flows.
- RealTrading → Market Data: uses · Live trading consumes market data services.
- RealTrading → Risk Control: guards with · Risk checks gate order execution.
- RealTrading → Exchange Adapters: executes via · Orders are sent through exchange adapters.
- RealTrading → Pine Script: exports · Strategy logic can be translated to external artifacts.
- RealTrading → Interfaces: implements · Live services implement shared contracts.
- StrategySearch → Backtesting: runs · Research engine executes the backtesting pipeline.
- StrategySearch → Strategies: evaluates · Strategies are scored and optimized offline.
- StrategySearch → Models: consumes · Uses shared market, trade, and result models.
- Infrastructure → Db Contexts: hosts · Infrastructure owns EF Core contexts.
- Infrastructure → Repositories: implements · Repository implementations live in infrastructure.
- Infrastructure → Migrations: evolves · Schema migrations are managed here.
- Repositories → Interfaces: implements · Repositories satisfy shared persistence abstractions.
- Db Contexts → Models: persists · Database maps shared trading entities.
- RealTrading → Infrastructure: persists through · Live runtime stores orders and trades via repositories.
- StrategySearch → Infrastructure: reads via · Historical data is fetched through repository abstractions.
- Monitoring → Health API: exposes · Monitoring subsystem serves health endpoints.
- Monitoring → Checks: runs · Monitoring aggregates component health checks.
- Monitoring → Health Middleware: protects with · Middleware handles caching, logging, and rate limiting.
- Monitoring → Models: reports · Operational status uses shared health and metric models.
- Console Host → Monitoring: hosts · The host also carries the monitoring surface.
- Prometheus → Monitoring: observes · Metrics and alerts track trading health.
- Grafana → Prometheus: visualizes · Dashboards are built on Prometheus metrics.
- Docker → Console Host: runs · Container setup launches the host application.
- Docker → Infrastructure: provisions · Compose and scripts support the database layer.
