# jensnesten/rust_bt architecture

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

- Interactive diagram: https://gitdiagram.com/jensnesten/rust_bt
- Repository: https://github.com/jensnesten/rust_bt

## Overview

##### Main Components
- rust_core: This is the heart of the project. It implements the core trading engine including data handling, execution logic, strategies, order management, position tracking, hedging, and other essential trading functionalities. It also contains shared code that is reused by both backtesting and live trading modes.
- rust_bt: The backtesting interface. This component connects the core engine with historical market data (OHLC) to simulate trading scenarios over past data. It interacts with rust_core to run strategies for backtesting.
- rust_live: The live trading interface. It handles real-time market data streaming (currently setup for Saxo Bank’s SaxoOpenAPI) and connects live data with the core engine to execute live trading strategies.
- rust_ml: The machine learning integration module. This part loads and applies pretrained models (and scalers) for inference on both historical and live data, and includes auxiliary tools for model conversion and export.

##### Relationships and Interactions
- Within rust_core, show sub-components such as:
– Strategies (implemented in rust_core/src/strategies)
– Data Handling (rust_core/src/data_handler)
– Position & Trade Management (position and engine modules)
– Hedging and other utilities.
- The rust_ml module should be connected to rust_core to show how its inference outputs could modify or signal trading decisions (e.g., enhancing strategies within rust_core).

##### Architectural Patterns and Design Principles
- Modular Separation: Emphasize that while the core trading logic is centralized in rust_core, interfaces for different operational modes (backtest, live, and ML) are clearly separated.
- Strategy Pattern: Strategies implement a common trait (either Strategy or LiveStrategy) so that the trading logic can be easily extended.
- Data Flow Pipeline: Illustrate the sequential data processing from input (historical CSV files or live API streams), through the core engine (order book management, order execution, position management), and output (trade logs, position updates, plots).
- Extensibility: Show how additional components (e.g., new asset classes, alternate data sources) could be added with minimal changes to the core.

##### Technologies and Frameworks
- Rust: Identify it is the primary language with Cargo as the build system (each component has its Cargo.toml).
- For plotting, note the built-in equity curve plotting within backtesting.
- Machine Learning: Indicate integration with PyTorch models and scalers, as seen in rust_ml.
- API Integration: Mention the use of SaxoOpenAPI within the live trading module.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Data Sources
    HCSV["Historical Data CSV"]:::external
    LAPI["Live Data API"]:::external

    %% Backtesting Interface (rust_bt)
    subgraph "Backtesting Interface (rust_bt)"
        BT["Backtesting Interface"]:::interface
        BT_Main["Backtesting Main"]:::interface
    end

    %% Live Trading Interface (rust_live)
    subgraph "Live Trading Interface (rust_live)"
        LIVE["Live Trading Interface"]:::interface
        Live_Stream["Live Data Stream"]:::interface
        Live_Server["Trading Server"]:::interface
        Live_Main["Live Main"]:::interface
    end

    %% Machine Learning Module (rust_ml)
    subgraph "Machine Learning Module (rust_ml)"
        ML["Machine Learning Module"]:::interface
        ML_Inference["Inference Module"]:::interface
        ML_Models["Pretrained Models"]:::interface
        ML_Tools["Model Conversion Tools"]:::interface
        ML_Util["ML Utilities"]:::interface
    end

    %% Core Trading Engine (rust_core)
    subgraph "Core Trading Engine (rust_core)"
        CORE["Core Engine"]:::core
        CORE_Data["Data Handling"]:::core
        CORE_Strategies["Strategies"]:::core
        CORE_Position["Position & Trade Management"]:::core
        CORE_Hedging["Hedging & Utilities"]:::core
    end

    %% Data Flow Arrows
    HCSV -->|"input"| BT_Main
    LAPI -->|"stream"| Live_Stream

    BT_Main -->|"simulate"| BT
    BT -->|"feeds"| CORE

    Live_Stream -->|"channels"| LIVE
    Live_Server --> LIVE
    Live_Main --> LIVE
    LIVE -->|"executes"| CORE

    ML_Inference --> ML
    ML_Models --> ML
    ML_Tools --> ML
    ML_Util --> ML
    ML -->|"signals"| CORE

    %% Internal Core Flow
    CORE_Data -->|"processes"| CORE_Strategies
    CORE_Strategies -->|"executes"| CORE_Position
    CORE_Position -->|"manages"| CORE_Hedging
    %% Core engine central connection
    CORE --- CORE_Data
    CORE --- CORE_Strategies
    CORE --- CORE_Position
    CORE --- CORE_Hedging

    %% Click Events for rust_core
    click CORE "https://github.com/jensnesten/rust_bt/tree/main/rust_core/"
    click CORE_Data "https://github.com/jensnesten/rust_bt/blob/main/rust_core/src/data_handler/mod.rs"
    click CORE_Strategies "https://github.com/jensnesten/rust_bt/tree/main/rust_core/src/strategies/"
    click CORE_Position "https://github.com/jensnesten/rust_bt/blob/main/rust_core/src/position/mod.rs"
    click CORE_Hedging "https://github.com/jensnesten/rust_bt/blob/main/rust_core/src/hedging/mod.rs"

    %% Click Events for rust_bt
    click BT "https://github.com/jensnesten/rust_bt/tree/main/rust_bt/"
    click BT_Main "https://github.com/jensnesten/rust_bt/blob/main/rust_bt/src/main.rs"
    click HCSV "https://github.com/jensnesten/rust_bt/blob/main/rust_bt/data/SP500_DJIA_2m_clean.csv"

    %% Click Events for rust_live
    click LIVE "https://github.com/jensnesten/rust_bt/tree/main/rust_live/"
    click Live_Stream "https://github.com/jensnesten/rust_bt/blob/main/rust_live/src/stream.rs"
    click Live_Server "https://github.com/jensnesten/rust_bt/blob/main/rust_live/src/server.rs"
    click Live_Main "https://github.com/jensnesten/rust_bt/blob/main/rust_live/src/main.rs"

    %% Click Events for rust_ml
    click ML "https://github.com/jensnesten/rust_bt/tree/main/rust_ml/"
    click ML_Inference "https://github.com/jensnesten/rust_bt/blob/main/rust_ml/src/inference/mod.rs"
    click ML_Models "https://github.com/jensnesten/rust_bt/tree/main/rust_ml/src/models/"
    click ML_Tools "https://github.com/jensnesten/rust_bt/tree/main/rust_ml/src/tools/"
    click ML_Util "https://github.com/jensnesten/rust_bt/blob/main/rust_ml/src/scaler.rs"

    %% Styles
    classDef core fill:#ffe6aa,stroke:#b58900,stroke-width:2px;
    classDef interface fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef external fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
```
