# tienan92it/backtesting-system architecture

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

- Interactive diagram: https://gitdiagram.com/tienan92it/backtesting-system
- Repository: https://github.com/tienan92it/backtesting-system

## Overview

##### Project Type and Purpose
- The system is a specialized backtesting engine for simulating and evaluating cryptocurrency trading strategies using historical data.
- It uses an event‑driven architecture where market data flows through various components to generate trading signals and simulate executions.
- Additionally, there is an AI‑driven layer that parses natural language strategy descriptions and automatically generates Python code to integrate with the backtester.

##### Analyzing the File Structure
- The “backtesting” directory contains all the core modules for the backtesting engine:
– Data (data handlers using Binance or CCXT data)
– Strategy (strategy definitions and examples)
– Portfolio (position and risk management)
– Execution (order simulation and execution handling)
– Metrics (performance measurements)
– Engine (backtester control logic, event loop, and event coordinating)
- The “ai_driven” folder introduces AI‑enhancements:
– API submodule for interfacing with external services or exposing endpoints.
– Parser (for converting natural language strategy instructions into structured data)
– Code Generator (to produce strategy code automatically)
– Report generation and UI components for visualizing outcomes
- Other folders (cli, examples, tests) support command line operations, example scenarios, and testing respectively.

##### Main Components
- Data Handler: Fetches and formats market data.
- Strategy: Applies trading algorithms and generates signal events.
- Portfolio: Manages assets, positions, and risk.
- Execution Handler: Simulates order placement, fills, and tracks trading orders.
- Event Loop: Coordinates and propagates various market and strategy events.
- Backtester: Orchestrates the overall simulation, manages components, and computes performance metrics.

b. AI-Driven Modules (can be a separate cluster connected to the core engine):
- Strategy Parser Module: Converts natural language instructions into structured strategy specifications.
- Strategy Code Generator Module: Works with the parsed strategy to generate executable Python code that integrates with the backtesting engine.
- UI and Reporting: Enables interaction and visualizes generated reports/templates.

c. Supporting Components:
- API Layer: For exposing functionality (under ai_driven/api).
- CLI Interface: Allows users to run backtests via the command line.
- External Data Sources: Indicate connection to crypto exchanges like Binance (shown as external systems feeding data).

##### Relationships and Interactions
- Data flows from the Data Handler into the Event Loop by triggering Market Events.
- The Event Loop distributes events (Market, Signal, Order, Fill, Portfolio updates) across the Strategy, Portfolio, and Execution Handler.
- The Backtester coordinates these interactions and aggregates performance results.
- In the AI‑driven cluster:
- The Parser’s output feeds into the Code Generator, which produces executable code that then can influence the Strategy component of the core engine.
- Optionally, display integration with the API and UI components for external interaction.

##### Architectural Patterns/Design Principles
- Event‑driven nature using a central Event Loop that orchestrates all components.
- Modularity and separation of concerns (each module handles its specific tasks: data fetching, strategy logic, portfolio management, etc.).
- How AI modules extend the system by transforming natural language into structured trading strategies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Backtesting Engine Cluster
    subgraph "Core Backtesting Engine"
        DataHandler["Data Handler"]:::core
        Strategy["Strategy"]:::core
        Portfolio["Portfolio"]:::core
        ExecHandler["Execution Handler"]:::core
        EventLoop["Event Loop"]:::core
        Backtester["Backtester"]:::core
    end

    %% AI-Driven Modules Cluster
    subgraph "AI-Driven Modules"
        StrategyParser["Strategy Parser Module"]:::ai
        CodeGenerator["Strategy Code Generator Module"]:::ai
        APILayer["API Layer"]:::ai
        %% UI and Reporting Subcluster
        subgraph "UI & Reporting"
            UI["UI"]:::ai
            Report["Report Generation"]:::ai
        end
    end

    %% Supporting Components
    External["External Data Sources"]:::external
    CLI["CLI Interface"]:::cli

    %% Connections among Core components
    External -- "MarketData" --> DataHandler
    DataHandler -- "MarketEvent" --> EventLoop
    EventLoop -- "SignalEvent" --> Strategy
    EventLoop -- "OrderEvent" --> ExecHandler
    EventLoop -- "PortfolioUpdate" --> Portfolio
    Backtester -- "Orchestrates" --> EventLoop
    CodeGenerator -- "GeneratesCode" --> Strategy

    %% Connections among AI modules
    StrategyParser -- "ParsesStrategy" --> CodeGenerator
    APILayer -- "ExposesServices" --> StrategyParser
    APILayer -- "ExposesServices" --> CodeGenerator
    CodeGenerator -- "Reports" --> UI
    CodeGenerator -- "Reports" --> Report

    %% Supporting connection for CLI
    CLI -- "TriggersBacktest" --> Backtester

    %% Class definitions for colors and styles
    classDef core fill:#ADD8E6,stroke:#0000FF,stroke-width:2px;
    classDef ai fill:#90EE90,stroke:#008000,stroke-width:2px;
    classDef external fill:#FFA07A,stroke:#FF4500,stroke-width:2px;
    classDef cli fill:#FFFFE0,stroke:#FFD700,stroke-width:2px;

    %% Click Events
    click DataHandler "https://github.com/tienan92it/backtesting-system/blob/master/backtesting/data/data_handler.py"
    click Strategy "https://github.com/tienan92it/backtesting-system/tree/master/backtesting/strategy/"
    click Portfolio "https://github.com/tienan92it/backtesting-system/blob/master/backtesting/portfolio/portfolio.py"
    click ExecHandler "https://github.com/tienan92it/backtesting-system/blob/master/backtesting/execution/execution_handler.py"
    click EventLoop "https://github.com/tienan92it/backtesting-system/blob/master/backtesting/engine/event_loop.py"
    click Backtester "https://github.com/tienan92it/backtesting-system/blob/master/backtesting/engine/backtester.py"
    click StrategyParser "https://github.com/tienan92it/backtesting-system/blob/master/ai_driven/parser.py"
    click CodeGenerator "https://github.com/tienan92it/backtesting-system/blob/master/ai_driven/code_generator.py"
    click APILayer "https://github.com/tienan92it/backtesting-system/tree/master/ai_driven/api/"
    click UI "https://github.com/tienan92it/backtesting-system/blob/master/ai_driven/ui/app.py"
    click Report "https://github.com/tienan92it/backtesting-system/blob/master/ai_driven/report/report.py"
    click CLI "https://github.com/tienan92it/backtesting-system/blob/master/cli/backtest_cli.py"
```
