# quantweb3-com/nexustrader architecture

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

- Interactive diagram: https://gitdiagram.com/quantweb3-com/nexustrader
- Repository: https://github.com/quantweb3-com/nexustrader

## Overview

##### Main Components
- Engine & Core: At the center is the “Engine” (nexustrader/engine.py) that orchestrates the entire flow. Surrounding it are the core modules (found in nexustrader/core) handling caching, logging, entity management, and the central registry.
- API/Connector Modules: There are “Connector” classes in the base folder (nexustrader/base) and in each exchange-specific folder (nexustrader/exchange/binance, bybit, okx, etc.) which handle both public (market data) and private (order execution) connections.
- Order Management: Components such as the “OrderExecutionSystem” and “OrderManagementSystem” (defined conceptually in the README and implemented in files like oms.py) control order submission, updates, and status management.
- Integration Components: The configuration modules (nexustrader/config.py) tie everything together, mapping basic configurations and connector configurations for different exchange types.
- External Dependencies & Performance Optimizations: The system integrates technologies like uvloop (to boost asyncio performance), picows (for fast WebSocket handling), and msgspec (for efficient data serialization). There are also Rust-powered modules (via nautilius) that underpin components like the MessageBus and Clock.

##### Relationships and Interactions
- The Engine initializes all the core modules and uses configuration settings to set up the corresponding connectors for each exchange.
- The “Connector” serves as the key interface with external exchanges—providing both market data (via the PublicConnector) and trade execution (via the PrivateConnector).
- Order information flows from the Engine to the OrderExecutionSystem where orders are sent to exchanges; subsequent responses and order updates then return via the OrderManagementSystem to the strategy.
- The asynchronous event loop (powered by uvloop) and high-performance WebSocket framework (picows) ensure that data flows efficiently between these components.

##### Architectural Patterns and Design Principles
- Asynchronous, event-driven design: The whole framework is built on asyncio, with strategies reacting to market events and timer signals.
- Modular separation: There’s a clear separation for exchange-specific logic (different folders under nexustrader/exchange) and core engine functionalities, making it easy to extend by adding new exchanges or strategies.
- Pub/Sub and callback handling: The design employs callback functions for various order statuses (accepted, filled, etc.) and market updates, making the system reactive to real-time events.
- High-performance optimizations: The use of uvloop and picows (along with efficient serialization via msgspec) are crucial design choices for ensuring low latency and high throughput.

##### Relevant Technologies, Frameworks, and Libraries
- Python 3.11+ with asyncio for asynchronous operations.
- uvloop for the event loop,
- picows for WebSocket communication,
- msgspec for data serialization,
- Docker, Redis (as shown in the docker-compose.yml and start_redis.sh) for deployment and caching/queueing,
- Rust (via nautilius) for some core module implementations.
- CI/CD configurations are available under .github/workflows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    uvloop["uvloop"]:::external
    picows["picows"]:::external
    msgspec["msgspec"]:::external
    Docker["Docker"]:::external
    Redis["Redis"]:::external
    Rust["Rust (nautilius)"]:::external
    CICD["CI/CD (.github/workflows)"]:::external

    %% Integration & Configuration
    Config["Config Module"]:::integration
    Constants["Constants"]:::integration

    %% API/Connector Modules
    BaseConn["Base Connector"]:::connector
    BinanceConn["Exchange Connector - Binance"]:::connector
    BybitConn["Exchange Connector - Bybit"]:::connector
    OKXConn["Exchange Connector - OKX"]:::connector
    HPYerliqConn["Exchange Connector - HPyerliq"]:::connector

    %% Order Management Systems
    BaseOMS["Base OMS"]:::oms
    BinanceOMS["Binance OMS"]:::oms

    %% Strategy & Signal Processing
    Strategy["Strategy & Signal Processing"]:::strategy

    %% Core Engine & Modules
    Engine["Engine"]:::core
    CoreModules["Core Modules (cache,entity,log,nautilius,registry)"]:::core

    %% Relationships

    %% External dependencies support Engine and Connectors
    uvloop -->|"supports"| Engine
    picows  -->|"supports"| BaseConn
    msgspec -->|"optimizes"| Engine
    Rust    -->|"powers"| Engine
    Docker  -->|"deploys"| Engine
    Redis   -->|"caches"| Engine
    CICD    -->|"integrates"| Engine

    %% Integration configures Engine
    Config -->|"configures"| Engine
    Constants -->|"defines"| Engine

    %% Engine initializes its core modules
    Engine -->|"initializes"| CoreModules

    %% API/Connector Modules feed data into Engine
    BaseConn -->|"providesData"| Engine
    BinanceConn -->|"providesData"| Engine
    BybitConn -->|"providesData"| Engine
    OKXConn -->|"providesData"| Engine
    HPYerliqConn -->|"providesData"| Engine

    %% Engine handles order execution via Order Management
    Engine -->|"submitsOrder"| BaseOMS
    Engine -->|"submitsOrder"| BinanceOMS
    BaseOMS -- "orderUpdate" --> Engine
    BinanceOMS -- "orderUpdate" --> Engine

    %% Strategy interacts with Engine for signal and order requests
    Engine -->|"sendsSignal"| Strategy
    Strategy -->|"requestsOrder"| Engine

    %% Click Events
    click Engine "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/engine.py"
    click CoreModules "https://github.com/quantweb3-com/nexustrader/tree/main/nexustrader/core"
    click BaseConn "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/base/connector.py"
    click BinanceConn "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/exchange/binance/connector.py"
    click BybitConn "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/exchange/bybit/connector.py"
    click OKXConn "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/exchange/okx/connector.py"
    click HPYerliqConn "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/exchange/hpyerliquid/exchange.py"
    click BaseOMS "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/base/oms.py"
    click BinanceOMS "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/exchange/binance/oms.py"
    click Config "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/config.py"
    click Constants "https://github.com/quantweb3-com/nexustrader/blob/main/nexustrader/constants.py"
    click Strategy "https://github.com/quantweb3-com/nexustrader/blob/main/strategy/binance/buy_and_sell.py"

    %% Styles
    classDef core fill:#8FBC8F,stroke:#000,stroke-width:2px;
    classDef connector fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef oms fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef strategy fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef integration fill:#E6E6FA,stroke:#000,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
