How cbrown35/tradingsystem works
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: RealTrading.csproj RealTrading · StrategySearch.csproj StrategySearch · MonitoringService.cs Monitoring · Infrastructure.csproj Infrastructure
Components 26
Shared Domain
Contracts and shared trading models used across the solution.
- Common · Shared interfaces and domain models for trading, backtesting, and persistence.
src/Common/Common.csproj - Interfaces · Core abstractions like trading, exchange, risk, backtesting, and repositories.
src/Common/Interfaces/ITradingService.cs - 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 · Main runtime host with controller surface and application startup.
src/TradingSystem.Console/Program.cs - Trading API · Primary command surface for orders, market data, and account actions.
src/TradingSystem.Console/Controllers/TradingController.cs - Factory · Builds and wires the trading system runtime.
src/TradingSystem.Console/Services/TradingSystemFactory.cs
Core Orchestration
Trading coordination and monitoring subsystem.
- Coordinator · Top-level workflow coordinator for trading execution.
src/Core/Services/TradingSystemCoordinator.cs - Monitoring · Health checks, notifications, middleware, and UI for operational status.
src/Core/Monitoring/MonitoringService.cs - Health API · Protected health-check endpoint surface.
src/Core/Monitoring/Controllers/HealthCheckController.cs - Checks · Component, market data, backtester, risk, and strategy health checks.
src/Core/Monitoring/HealthChecks/ComponentHealthChecks.cs - Health Middleware · 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 · Live trading services, adapters, caching, and risk enforcement.
src/RealTrading/RealTrading.csproj - Market Data · Market data ingestion and cache services.
src/RealTrading/Services/MarketDataService.cs - Risk Control · Risk management and validation for live orders.
src/RealTrading/Services/RiskManager.cs - Exchange Adapters · Execution targets for simulation, Tradovate, and TradingView.
src/RealTrading/Services/SimulatedExchangeAdapter.cs - Pine Script · Generates TradingView-compatible strategy artifacts.
src/RealTrading/Services/PineScriptGenerator.cs
Strategy Research
Backtesting, optimization, and strategy generation.
- StrategySearch · Backtesting, strategy optimization, and theory generation.
src/StrategySearch/StrategySearch.csproj - Backtesting · Metrics, validation, parameter optimization, and trade execution simulation.
src/StrategySearch/Services/Backtesting/TheoryBacktester.cs - Strategies · Reference strategies used for research and optimization.
src/StrategySearch/Strategies/SimpleMovingAverageStrategy.cs
Persistence
Database context, repositories, and migrations.
- Infrastructure · EF Core contexts, repositories, and persistence wiring.
src/Infrastructure/Infrastructure.csproj - Db Contexts · Trading and development database contexts plus factory support.
src/Infrastructure/Data/TradingContext.cs - Repositories · Market data, order, and trade repository implementations.
src/Infrastructure/Repositories/MarketDataRepository.cs - Migrations · Database schema evolution including TimescaleDB support.
src/Infrastructure/Migrations/20241222053516_AddTimescaleDBSupport.cs
Operations
Container, metrics, alerting, and deployment assets.
- Prometheus · Alert rules, recording rules, and notification templates.
prometheus/prometheus.yml - Grafana · Dashboard and datasource provisioning for operational monitoring.
grafana/provisioning/dashboards/trading_dashboard.json - Docker · Container build and compose definitions for local and production runs.
docker-compose.yml
Connections 31
- 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.