# ccxt/binance-trade-bot architecture

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

- Interactive diagram: https://gitdiagram.com/ccxt/binance-trade-bot
- Repository: https://github.com/ccxt/binance-trade-bot

## Overview

##### Project Type and Purpose
- This is an automated trading bot for Binance. Its purpose is to analyze coin fluctuations, make trade decisions, and execute trades using strategies based on observed trends between coins, bridging via a stable currency (default USDT).
- The README clarifies that the bot supports automated trading, backtesting, an API server for external monitoring, and notification integrations (e.g., via Apprise).

##### File Structure
- The core logic is contained within the “binance_trade_bot” directory, showing several components:
- Main modules: __main__.py (entry point) and auto_trader.py (core trading logic).
- API server: api_server.py for serving external requests or status.
- Exchange communication: Modules such as binance_api_manager.py and binance_stream_manager.py suggest dedicated components for interacting with Binance over REST/websockets.
- Scheduling: scheduler.py manages periodic tasks.
- Data models: Subdirectory models holds definitions (coin, coin_value, etc.) to represent trading entities.
- Strategies: A dedicated folder under “binance_trade_bot/strategies” holds different trading strategies (e.g., default_strategy.py, multiple_coins_strategy.py) to encapsulate trading rules.
- Additionally, standalone files (backtest.py) allow for simulation/backtesting, while configuration files (e.g., .user.cfg.example, config directory) and Docker-related files (Dockerfile, docker-compose.yml) support deployment.

##### Components and Their Relationships
a. Main Components:
- Trading Engine: Auto-trader (auto_trader.py) that coordinates trade logic and decision making.
- API Communication: Binance API Manager and Binance Stream Manager handle market data and order execution.
- Data Models/Database: Modules under models, along with the “database.py” file, which persist and manage trading/scouting data.
- Scheduler: Handles the periodic invocation of tasks, such as refreshing market data or checking for trade opportunities.
- Strategy Module: Contains various trading strategies that the auto-trader uses to decide trades.
- API Server: (api_server.py) Provides an interface for external monitoring or integrations.
- Notification System: Possibly managed via notifications.py and configurations for Apprise integration.
- Backtesting Module: (backtest.py) For running simulations based on historic data.
- Configuration Management: Reads settings from “user.cfg” and environment variables to control behavior (e.g., API keys, strategy selection, coin lists).

b. Relationships and Data Flow:
- The startup flow begins with configuration being loaded.
- The auto_trader then uses the Binance API Manager and Binance Stream Manager to receive real-time market data.
- Based on data models and the chosen trading strategy, the auto_trader decides when to trade.
- Scheduled tasks (by the scheduler) ensure continuous monitoring and periodic checks.
- The API server component exposes endpoints for monitoring or retrieving trade data.
- Notifications are sent out via the Apprise system whenever trade events occur.
- In a separate flow, the backtesting module simulates the trading logic on historical records.

##### Architectural Patterns and Design Principles
- Modularity: The project is clearly modularized (core engine, strategies, API server, notifications) ensuring separation of concerns.
- Config-driven behavior: Settings can be tweaked via configuration files and environment variables.
- Micro-component architecture: Although not a microservices architecture per se, the segregation of the Binance communication modules, trading logic, and API server mirror independent components.
- Extensibility: The “strategies” folder implies that new trading strategies can be added with minimal changes to the core engine.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Management Subgraph
    subgraph "Configuration Management"
        a1["Default Config Template"]:::config
        a2["Configuration Loader"]:::config
        a3["Additional Config Files"]:::config
    end

    %% Core Trading Bot Subgraph
    subgraph "Core Trading Bot"
        b1["Trading Engine"]:::core
        subgraph "API Communication"
            b2["Binance API Manager"]:::api
            b3["Binance Stream Manager"]:::api
        end
        subgraph "Data Persistence"
            b4["Data Models"]:::modeling
            b5["Database Handler"]:::modeling
        end
        b6["Scheduler"]:::core
        b7["Trading Strategies"]:::core
        b8["API Server"]:::core
        subgraph "Notification System"
            b9["Notification Code"]:::core
            b10["Notification Config"]:::core
        end
        subgraph "Backtesting"
            b11["Standalone Backtest"]:::core
            b12["Package Backtest"]:::core
        end
    end

    %% External Systems Subgraph
    subgraph "External Systems"
        c1["Binance Exchange"]:::external
        c2["External Monitoring/Users"]:::external
    end

    %% Data Flow Connections
    a2 -->|"loads"| b1
    a3 -->|"supplies"| b1

    b1 -->|"calls"| b2
    b1 -->|"calls"| b3
    b2 -->|"REST/Websocket"| c1
    b3 -->|"REST/Websocket"| c1

    b1 -->|"selects"| b7

    b6 -->|"triggers"| b1

    b1 -->|"persists"| b4
    b1 -->|"persists"| b5

    b1 -->|"notifies"| b9
    b9 -->|"alerts"| c2

    b1 -->|"exposes"| b8
    b8 -->|"serves"| c2

    b11 -->|"simulates"| b1
    b12 -->|"simulates"| b1

    %% Styling Classes
    classDef config fill:#f0e68c,stroke:#333,stroke-width:2px;
    classDef core fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef api fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef modeling fill:#ffe4e1,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcccb,stroke:#333,stroke-width:2px;

    %% Click Events for Configuration Management
    click a1 "https://github.com/ccxt/binance-trade-bot/blob/master/.user.cfg.example"
    click a2 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/config.py"
    click a3 "https://github.com/ccxt/binance-trade-bot/tree/master/config/"

    %% Click Events for Core Trading Bot
    click b1 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/auto_trader.py"
    click b2 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/binance_api_manager.py"
    click b3 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/binance_stream_manager.py"
    click b4 "https://github.com/ccxt/binance-trade-bot/tree/master/binance_trade_bot/models/"
    click b5 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/database.py"
    click b6 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/scheduler.py"
    click b7 "https://github.com/ccxt/binance-trade-bot/tree/master/binance_trade_bot/strategies/"
    click b8 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/api_server.py"
    click b9 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/notifications.py"
    click b10 "https://github.com/ccxt/binance-trade-bot/blob/master/config/apprise_example.yml"
    click b11 "https://github.com/ccxt/binance-trade-bot/blob/master/backtest.py"
    click b12 "https://github.com/ccxt/binance-trade-bot/blob/master/binance_trade_bot/backtest.py"
```
