# e-geist/ddtp architecture

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

- Interactive diagram: https://gitdiagram.com/e-geist/ddtp
- Repository: https://github.com/e-geist/ddtp

## Overview

##### Project type and purpose
- This is a proof-of-concept Distributed Trading Platform (DDTP) implemented in Python.
- It consists of independently deployable components (market data provider, order execution engine, trading strategy) that communicate via Kafka.
- It integrates with the Kraken Derivatives API for both market data ingestion and order execution.

##### File structure
- Top-level “src/ddtp” contains five logical subpackages:
· api/kraken_derivatives – adapters for Kraken REST and WebSocket endpoints
· marketdata – local market-data logic and adapters
· order_execution – core order management and engine integration
· serialization – Kafka producer/consumer setup and message schema config
· strategy – trading logic that consumes market data and emits orders
- Entry-point modules at src/ddtp/ (marketdata_provider, order_execution_engine, buy_on_small_spread_strategy) correspond to three long-running services.
- docker/docker-compose.yml sets up a Kafka broker for inter‐service messaging.
- .env_template and config modules drive API credentials, Kafka URLs, and topic names.

##### README for additional insights
- Kafka is the backbone for component communication (market data → strategy → order execution).
- uv is used as the build/run system, Docker Compose for local Kafka.
- No persistent storage beyond Kafka—this is an in-memory streaming architecture.

1. Kraken Derivatives Exchange (external)
2. Market Data Provider Service
– Subcomponents: Kraken WebSocket client, MarketData Adapter, Kafka Producer
3. Trading Strategy Service
– Subcomponents: Kafka Consumer (“marketdata” topic), Strategy Logic, Kafka Producer (“orders” topic)
4. Order Execution Engine Service
– Subcomponents: Kafka Consumer (“orders” topic), Order Manager, Execution Engine Adapter, Kraken REST client
5. Kafka Cluster (broker)
– Topics: marketdata, orders
– Kraken → Market Data Provider (WebSocket)
– Market Data Provider → Kafka (produce marketdata)
– Kafka → Strategy Service (consume marketdata)
– Strategy Service → Kafka (produce orders)
– Kafka → Order Execution Service (consume orders)
– Order Execution Service → Kraken (REST)
c. Architectural patterns & design principles:
– Microservices with asynchronous messaging bus (Kafka)
– Adapter pattern for external API integration (in api/kraken_derivatives and order_execution/execution_engine_adapter.py)
– Single Responsibility: each service does one main job (ingest, decide, execute)
– Config-driven deployment via environment variables (.env + config.py)
d. Technologies & frameworks to annotate:
– Python 3.13
– uv build/run tool
– kafka-python (or confluent-kafka) under serialization/producer.py, consumer.py
– Docker Compose for Kafka test container
– WebSocket and REST libraries (in api/kraken_derivatives)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    Kraken["Kraken Derivatives Exchange"]:::external
    click Kraken "https://github.com/e-geist/ddtp/tree/main/none"

    %% Kafka Broker
    KafkaBroker["Kafka Broker\n(topics: marketdata, orders)"]:::kafka
    click KafkaBroker "https://github.com/e-geist/ddtp/blob/main/docker/docker-compose.yml"

    %% Market Data Provider Service
    subgraph "Market Data Provider Service" 
        direction TB
        WSClient["Kraken WebSocket Client"]:::ingestion
        click WSClient "https://github.com/e-geist/ddtp/blob/main/src/ddtp/api/kraken_derivatives/websocket.py"
        MDAPI["MarketData API Adapter"]:::ingestion
        click MDAPI "https://github.com/e-geist/ddtp/blob/main/src/ddtp/api/kraken_derivatives/marketdata_adapter.py"
        MDLogic["Local MarketData Logic"]:::ingestion
        click MDLogic "https://github.com/e-geist/ddtp/blob/main/src/ddtp/marketdata/marketdata_adapter.py"
        MDData["MarketData Modules\n(data.py, orderbook.py)"]:::ingestion
        click MDData "https://github.com/e-geist/ddtp/blob/main/src/ddtp/marketdata/data.py"
        click MDData "https://github.com/e-geist/ddtp/blob/main/src/ddtp/marketdata/orderbook.py"
        ProducerMD["Kafka Producer\n(topic: marketdata)"]:::ingestion
        click ProducerMD "https://github.com/e-geist/ddtp/blob/main/src/ddtp/serialization/producer.py"
    end
    click MarketDataProvider "https://github.com/e-geist/ddtp/blob/main/src/ddtp/marketdata_adapter.py"

    %% Strategy Service
    subgraph "Trading Strategy Service"
        direction TB
        ConsumerMD["Kafka Consumer\n(topic: marketdata)"]:::strategy
        click ConsumerMD "https://github.com/e-geist/ddtp/blob/main/src/ddtp/serialization/consumer.py"
        StrategyLogic["Strategy Logic"]:::strategy
        click StrategyLogic "https://github.com/e-geist/ddtp/blob/main/src/ddtp/strategy/marketdata.py"
        click StrategyLogic "https://github.com/e-geist/ddtp/blob/main/src/ddtp/strategy/order_execution.py"
        ProducerOrders["Kafka Producer\n(topic: orders)"]:::strategy
        click ProducerOrders "https://github.com/e-geist/ddtp/blob/main/src/ddtp/serialization/producer.py"
    end
    click StrategyService "https://github.com/e-geist/ddtp/blob/main/src/ddtp/buy_on_small_spread_strategy.py"

    %% Order Execution Engine Service
    subgraph "Order Execution Engine Service"
        direction TB
        ConsumerOrders["Kafka Consumer\n(topic: orders)"]:::execution
        click ConsumerOrders "https://github.com/e-geist/ddtp/blob/main/src/ddtp/serialization/consumer.py"
        OrderManager["Order Manager"]:::execution
        click OrderManager "https://github.com/e-geist/ddtp/blob/main/src/ddtp/order_execution/order_manager.py"
        ExecAdapter["Execution Engine Adapter"]:::execution
        click ExecAdapter "https://github.com/e-geist/ddtp/blob/main/src/ddtp/api/kraken_derivatives/execution_engine_adapter.py"
        click ExecAdapter "https://github.com/e-geist/ddtp/blob/main/src/ddtp/order_execution/execution_engine_adapter.py"
        RESTClient["Kraken REST Client"]:::execution
        click RESTClient "https://github.com/e-geist/ddtp/blob/main/src/ddtp/api/kraken_derivatives/rest.py"
    end
    click OrderExecutionEngine "https://github.com/e-geist/ddtp/blob/main/src/ddtp/order_execution_engine.py"

    %% Configuration & Environment
    subgraph "Configuration"
        Env[".env_template"]:::config
        click Env "https://github.com/e-geist/ddtp/blob/main/.env_template"
        APIConfig["Kraken API Config"]:::config
        click APIConfig "https://github.com/e-geist/ddtp/blob/main/src/ddtp/api/kraken_derivatives/config.py"
        KafkaConfig["Kafka Config"]:::config
        click KafkaConfig "https://github.com/e-geist/ddtp/blob/main/src/ddtp/serialization/config.py"
    end

    %% Data Flows
    Kraken -->|"WebSocket Stream"| WSClient
    WSClient -->|processes| MDAPI
    MDAPI -->|formats| MDLogic
    MDLogic -->|feeds| ProducerMD
    ProducerMD -->|"marketdata"| KafkaBroker

    KafkaBroker -->|"marketdata"| ConsumerMD
    ConsumerMD -->|delivers| StrategyLogic
    StrategyLogic -->|generates| ProducerOrders
    ProducerOrders -->|"orders"| KafkaBroker

    KafkaBroker -->|"orders"| ConsumerOrders
    ConsumerOrders -->|forwards| OrderManager
    OrderManager -->|calls| ExecAdapter
    ExecAdapter -->|REST call| RESTClient
    RESTClient --> Kraken

    %% Classes
    classDef ingestion fill:#E0F7FA,stroke:#006064,color:#006064
    classDef strategy fill:#E8F5E9,stroke:#1B5E20,color:#1B5E20
    classDef execution fill:#FFEBEE,stroke:#B71C1C,color:#B71C1C
    classDef kafka fill:#FFF3E0,stroke:#E65100,color:#E65100
    classDef external fill:#ECEFF1,stroke:#90A4AE,color:#90A4AE
    classDef config fill:#F3E5F5,stroke:#4A148C,color:#4A148C
```
