# lueasf/hftrading architecture

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

- Interactive diagram: https://gitdiagram.com/lueasf/hftrading
- Repository: https://github.com/lueasf/hftrading

## Overview

##### Project Overview
- HFTrading is a high-frequency trading tool built using both C++ and Python. It integrates components for real-time data (streams), trading algorithms (algos), backtesting (backtest), communication (via Aeron and NATS), and data exporting (exporter).
- The project uses a mix of lower-level performance-critical modules (C++ source code in streams, communication, and algos) and higher-level business logic and simulation modules in Python (backtest and exporter).

##### Main Components
a. Streams Layer (C++):
- Contains components for real-time data ingestion and processing.
- Modules include:
- Binance WebSocket client (binance_websocket_client.cpp)
- NATS client for messaging (nats_client.[cpp/h])
- Configuration (config.cpp/h & config.yaml)
- Metrics handling (metrics.cpp/h)
- This layer is responsible for receiving live market data and relaying it via NATS messaging.

b. Communication Layer (C++):
- Implements the Aeron publisher (aeron_publisher.cpp/h) for robust UDP transport.
- Tests exist to ensure data integrity and proper handling.
- This module likely interfaces between low-latency messaging and the trading engine/algorithms.

c. Trading Algorithms (C++):
- Under the “algos” directory, which includes a main entry (main.cpp) along with algorithmic modules such as regression (regression.cpp/h).
- This component implements the statistical or machine learning-based strategies that decide trading actions.

d. Backtesting Module (Python):
- Designed for simulation and testing strategies against historical data.
- Contains sub-components that mirror live trading:
- Core engine for simulation (backtest/core/engine.py)
- Data management (backtest/data with data_source.py and feed.py)
- Exchange emulation (backtest/exchange/exchange.py, orderbook.py)
- Reporters to assess performance and risk (backtest/reporters)
- Serves as a sandboxed environment to measure how algorithms perform on historical data.

e. Exporter Component (Python):
- Handles exporting or interfacing with external systems, possibly for analysis or logging.
- Uses modules like exporter.py and integration with NATS via nats_py.py.

f. External Dependencies & Infrastructure:
- NATS Server: Run via Docker as per the README. Acts as the message broker, especially for the streams and exporter components.
- Aeron (for UDP transport): Built and incorporated in the communication layer.
- Additional Tools: References to Prometheus Client, Boost Beast (WebSocket), and Polars suggest external integrations.

##### Relationships and Interactions
- Data flows from external market sources into the Streams module (Binance WebSocket client).
- Streams publish data over NATS to downstream components.
- The Communication layer (Aeron) might supplement these data flows ensuring low-latency messaging.
- The Algorithms module consumes data (possibly directly or through intermediary messaging) to execute trading strategies.
- The Backtesting module simulates these interactions in a controlled environment – mimicking data feeds, order/exchange interactions, and reporting performance.
- The Exporter interacts with NATS and maybe other endpoints to output results or analytics.
- All modules are built and managed using CMake (as seen from various CMakeLists.txt files) and have GitHub workflows for CI/CD.

##### Architectural Patterns & Design Principles
- Modular decomposition separating real-time streams, communication, strategy logic, and simulation.
- A mix of microservice-like separation (each folder is a distinct “service” or component) and layered architecture (data ingestion, processing, simulation, exporting).
- Use of messaging (NATS and Aeron) to decouple producers and consumers, which is key in high-frequency trading systems.
- A hybrid design with performance-critical C++ layers and flexible Python layers for backtesting and exporting.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Nodes
    ED2("Market Data Source"):::external
    ED1("NATS Server"):::external

    %% C++ Layers
    subgraph "Streams Layer (C++)"
        STR("Streams"):::cpp
        S1("Binance WebSocket Client"):::cpp
        S2("Configuration & Metrics"):::cpp
        S3("NATS Client"):::cpp
        STR --> S1
        STR --> S2
        STR --> S3
    end

    subgraph "Communication Layer (C++)"
        COM("Communication"):::cpp
        C1("Aeron Publisher"):::cpp
        C2("Communication Tests"):::cpp
        COM --> C1
        COM --> C2
    end

    subgraph "Trading Algorithms (C++)"
        TA("Trading Algorithms"):::cpp
        TA1("Main Entry & Regression"):::cpp
        TA --> TA1
    end

    %% Python Layers
    subgraph "Backtesting Module (Python)"
        BT("Backtesting"):::python
        BT1("Core Engine"):::python
        BT2("Data (Source & Feed)"):::python
        BT3("Exchange Emulation"):::python
        BT4("Reporters"):::python
        BT --> BT1
        BT --> BT2
        BT --> BT3
        BT --> BT4
    end

    subgraph "Exporter Component (Python)"
        EXP("Exporter"):::python
        EXP1("Exporter & NATS Integration"):::python
        EXP --> EXP1
    end

    %% Data Flow
    ED2 -->|"RealTimeData"| STR
    STR -->|"PublishesMsg"| ED1
    STR -->|"FeedsData"| COM
    COM -->|"LowLatencyMsg"| TA
    STR -->|"FeedsData"| BT
    TA -->|"TradingSignals"| EXP
    EXP -->|"ExportsData"| ED1
    ED1 -->|"MessageBroker"| EXP

    %% Styles
    classDef cpp fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef python fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
    classDef external fill:#AED6F1,stroke:#2471A3,stroke-width:2px;

    %% Click Events - Streams Layer
    click STR "https://github.com/lueasf/hftrading/tree/main/streams/"
    click S1 "https://github.com/lueasf/hftrading/blob/main/streams/binance_websocket_client.cpp"
    click S2 "https://github.com/lueasf/hftrading/blob/main/streams/config.yaml"
    click S3 "https://github.com/lueasf/hftrading/blob/main/streams/nats_client.cpp"

    %% Click Events - Communication Layer
    click COM "https://github.com/lueasf/hftrading/tree/main/communication/"
    click C1 "https://github.com/lueasf/hftrading/blob/main/communication/aeron_publisher.cpp"
    click C2 "https://github.com/lueasf/hftrading/tree/main/communication/tests"

    %% Click Events - Trading Algorithms
    click TA "https://github.com/lueasf/hftrading/tree/main/algos/"
    click TA1 "https://github.com/lueasf/hftrading/blob/main/algos/main.cpp"

    %% Click Events - Backtesting Module
    click BT "https://github.com/lueasf/hftrading/tree/main/backtest/"
    click BT1 "https://github.com/lueasf/hftrading/blob/main/backtest/core/engine.py"
    click BT2 "https://github.com/lueasf/hftrading/tree/main/backtest/data/"
    click BT3 "https://github.com/lueasf/hftrading/tree/main/backtest/exchange/"
    click BT4 "https://github.com/lueasf/hftrading/tree/main/backtest/reporters/"

    %% Click Events - Exporter Component
    click EXP "https://github.com/lueasf/hftrading/tree/main/exporter/"
    click EXP1 "https://github.com/lueasf/hftrading/blob/main/exporter/exporter.py"
```
