# mbiskho/stocksharp architecture

> Architecture diagram of the GitHub repository mbiskho/stocksharp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mbiskho/stocksharp
- Repository: https://github.com/mbiskho/stocksharp

## Overview

##### Project type and purpose
- “StockSharp” is a pluggable, full-stack algorithmic‐trading framework.
- It exposes a C# API for building strategies, ships core libraries for message processing, connects to external exchanges via connectors, stores/exports data, loads scripts (C#/F#/Python), and provides sample UIs.

##### File structure
- Top-level “Algo”, “BusinessEntities”, “Messages”, “Charting.Interfaces” represent the core engine, domain objects, messaging bus and UI contracts.
- “Connectors” contains one project per exchange, each implementing message adapters for market data/orders via HTTP/WebSocket.
- “Storages”, “Algo.Export”, “Algo.Analytics.*” are plug-in modules for persistence, export and custom analytics.
- “Configuration” and “ServicesRegistry” handle dependency injection and component registration.
- “Samples” provides WPF demo front-ends.
- Build scripts, .props files and the root solution file glue it all together.

##### README for insights
- High-level components: Designer (visual strategy builder), Hydra (data loader/storage), Terminal (charting/trading UI), Shell (generic UI framework), API (C# library).
- Over 50 exchange connectors for crypto, stocks, futures, options, forex.
- Features: event-driven architecture, plugin extensibility, scriptable analytics, high-performance HFT support.

##### Main components & interactions
a. UI Layer (blue)
– Designer / Terminal / Shell sample apps (WPF)
– Samples folder (WPF and console demos)
⮕ Call into API Layer to create connectors, subscribe to data, display charts, send orders.
b. API Layer (light-green)
– StockSharp.API, BusinessEntities, Charting.Interfaces
– Exposes domain models (CandleSeries, Order, Position, Portfolio) and interfaces (IConnector, IMarketDataProvider).
c. Core Engine (green)
– Algo (Strategy base, message adapters, processors)
– Messages (IMessage, IMessageAdapter, MessageChannel, MessageProcessor)
– Configuration & ServicesRegistry (DI container, component lookup)
– Patterns: pub/sub via message bus, Strategy pattern for trading logic, Factory/Provider for connectors and storages.
d. Infrastructure & Plugins (yellow)
– Connectors (per-exchange message adapters over HTTP/WebSocket)
– Storages (CSV, binary, remote, cache layers)
– Exporters (Excel, JSON, XML, DB)
– Analytics scripts (Algo.Analytics.CSharp/FSharp/Python)
e. External Systems (gray)
– Exchanges and brokers (Binance, Coinbase, IB, FIX, MT4/5, etc.)
– Databases or file systems for persistence.

Interactions:
- Connectors ingest market data / trades / account events → push IMessage into MessageChannel.
- Core Engine dispatches messages to subscribers: Strategies (Algo), Storages, Exporters, Analytics.
- Strategies generate order messages → routed back through MessageChannel → Connectors → external exchanges.
- UI apps subscribe to business entities and charting interfaces to render data and control strategies.
- Configuration/ServicesRegistry wires up all modules at startup.

##### Architectural patterns & design principles
- Modular plugin architecture: each connector, storage, exporter, analytics script is discovered via ServicesRegistry.
- Pub/sub asynchronous message bus (MessageChannel, AsyncMessageAdapter, MessageProcessor).
- Dependency injection via custom ServicesRegistry (ConfigurationServicesRegistry).
- Strategy pattern for trading logic (Strategy base class).
- Factory/provider for connectors, storages, commission/risk rules.
- Separation of concerns: UI ↔ API ↔ Core ↔ Infrastructure ↔ External.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI Layer
    subgraph "UI Layer" 
        direction TB
        Designer["Designer App"]:::ui
        Terminal["Terminal App"]:::ui
        Shell["Shell App"]:::ui
        Samples["Samples"]:::ui
    end

    %% API/Domain Layer
    subgraph "API & Domain Layer"
        direction TB
        BusinessEntities["BusinessEntities"]:::api
        ChartingInterfaces["Charting.Interfaces"]:::api
    end

    %% Core Engine Layer
    subgraph "Core Engine"
        direction TB
        StrategyEngine["Strategy & Processing (Algo)"]:::core
        MessageBus["Message Bus (Messages)"]:::core
        Configuration["Configuration"]:::core
        ConfigurationServicesRegistry["ConfigurationServicesRegistry"]:::core
        ServicesRegistry["ServicesRegistry"]:::core
    end

    %% Plugins Layer
    subgraph "Infrastructure & Plugins"
        direction TB
        Connectors["Connectors"]:::plugin
        Storages["Storages"]:::plugin
        Exporters["Data Exporters"]:::plugin
        subgraph "Analytics Plugins"
            direction TB
            AnalyticsFramework["Analytics Framework"]:::plugin
            CSharpScripts["C# Analytics Scripts"]:::plugin
            FSharpScripts["F# Analytics Scripts"]:::plugin
            PythonScripts["Python Analytics Scripts"]:::plugin
        end
        subgraph "Localization Plugins"
            direction TB
            LocalizationCore["Localization"]:::plugin
            LocalizationLangs["Localization.Langs"]:::plugin
            LocalizationGen["Localization.Generator"]:::plugin
        end
        subgraph "Media Plugins"
            direction TB
            MediaCore["Media"]:::plugin
            MediaNames["Media.Names"]:::plugin
            MediaGen["Media.Generator"]:::plugin
        end
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        Exchanges["Exchanges & Brokers"]:::external
        Persistence["File System / Databases"]:::external
    end

    %% Connections UI -> API -> Core
    Designer -->|uses| BusinessEntities
    Designer -->|uses| ChartingInterfaces
    Terminal -->|uses| BusinessEntities
    Terminal -->|uses| ChartingInterfaces
    Shell -->|uses| BusinessEntities
    Shell -->|uses| ChartingInterfaces
    Samples -->|uses| BusinessEntities
    Samples -->|uses| ChartingInterfaces

    BusinessEntities -->|domain models| StrategyEngine
    ChartingInterfaces -->|UI contracts| StrategyEngine

    %% Core messaging and config
    Connectors -->|ingests data| MessageBus
    MessageBus -->|dispatches| StrategyEngine
    StrategyEngine -->|publishes orders| MessageBus
    MessageBus -->|routes orders| Connectors

    %% Plugins interactions
    MessageBus -->|store events| Storages
    MessageBus -->|export data| Exporters
    MessageBus -->|run analytics| AnalyticsFramework
    AnalyticsFramework -->|scripts| CSharpScripts
    AnalyticsFramework -->|scripts| FSharpScripts
    AnalyticsFramework -->|scripts| PythonScripts
    MessageBus -->|localize| LocalizationCore
    LocalizationCore -->|langs| LocalizationLangs
    LocalizationCore -->|generate| LocalizationGen
    MessageBus -->|media resources| MediaCore
    MediaCore -->|names| MediaNames
    MediaCore -->|generate| MediaGen

    %% Configuration wiring
    Configuration -->|initializes| StrategyEngine
    Configuration -->|wires| MessageBus
    Configuration -->|registers plugins| Connectors
    Configuration -->|registers plugins| Storages
    Configuration -->|registers plugins| Exporters
    Configuration -->|registers plugins| AnalyticsFramework
    Configuration -->|registers plugins| LocalizationCore
    Configuration -->|registers plugins| MediaCore
    ConfigurationServicesRegistry -->|DI container| StrategyEngine
    ConfigurationServicesRegistry -->|DI container| Plugins

    %% External persistence
    Storages -->|persists| Persistence
    Exporters -->|writes| Persistence

    %% UI data flows
    StrategyEngine -->|pushes updates| BusinessEntities
    StrategyEngine -->|pushes updates| ChartingInterfaces
    BusinessEntities -->|shown in| Designer
    ChartingInterfaces -->|shown in| Designer

    %% Layers interactions summary
    StrategyEngine -->|controls| Connectors
    Exchanges -->|market data & executions| Connectors

    %% Click Events
    click Samples "https://github.com/mbiskho/stocksharp/tree/master/Samples/"
    click BusinessEntities "https://github.com/mbiskho/stocksharp/tree/master/BusinessEntities/"
    click ChartingInterfaces "https://github.com/mbiskho/stocksharp/tree/master/Charting.Interfaces/"
    click StrategyEngine "https://github.com/mbiskho/stocksharp/tree/master/Algo/"
    click MessageBus "https://github.com/mbiskho/stocksharp/tree/master/Messages/"
    click ConfigurationServicesRegistry "https://github.com/mbiskho/stocksharp/blob/master/Configuration/ConfigurationServicesRegistry.cs"
    click Configuration "https://github.com/mbiskho/stocksharp/tree/master/Configuration/"
    click ServicesRegistry "https://github.com/mbiskho/stocksharp/blob/master/Algo/ServicesRegistry.cs"
    click Connectors "https://github.com/mbiskho/stocksharp/tree/master/Connectors/"
    click Storages "https://github.com/mbiskho/stocksharp/tree/master/Algo/Storages/"
    click Exporters "https://github.com/mbiskho/stocksharp/tree/master/Algo.Export/"
    click AnalyticsFramework "https://github.com/mbiskho/stocksharp/tree/master/Algo.Analytics/"
    click CSharpScripts "https://github.com/mbiskho/stocksharp/tree/master/Algo.Analytics.CSharp/"
    click FSharpScripts "https://github.com/mbiskho/stocksharp/tree/master/Algo.Analytics.FSharp/"
    click PythonScripts "https://github.com/mbiskho/stocksharp/tree/master/Algo.Analytics.Python/"
    click LocalizationCore "https://github.com/mbiskho/stocksharp/tree/master/Localization/"
    click LocalizationLangs "https://github.com/mbiskho/stocksharp/tree/master/Localization.Langs/"
    click LocalizationGen "https://github.com/mbiskho/stocksharp/tree/master/Localization.Generator/"
    click MediaCore "https://github.com/mbiskho/stocksharp/tree/master/Media/"
    click MediaNames "https://github.com/mbiskho/stocksharp/tree/master/Media.Names/"
    click MediaGen "https://github.com/mbiskho/stocksharp/tree/master/Media.Generator/"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085
    classDef api fill:#d4edda,stroke:#155724,color:#155724
    classDef core fill:#d1ecf1,stroke:#0c5460,color:#0c5460
    classDef plugin fill:#fff3cd,stroke:#856404,color:#856404
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
