# open-trader/opentrader architecture

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

- Interactive diagram: https://gitdiagram.com/open-trader/opentrader
- Repository: https://github.com/open-trader/opentrader

## Overview

##### Main Components Identification
- Frontend/UI: Represented in “apps/cli/frontend” – this is the web console that interacts with the backend and offers features such as monitoring, managing bots, and viewing backtests.
- CLI/Daemon & API Server: Found in “apps/cli” (with commands like “opentrader up”, “opentrader backtest”, “opentrader trade”) and a daemon process that launches an RPC server (listening on port 8000).
- Trading Bot Engine: Several packages exist for core functionality – the “packages/bot”, “packages/bot-processor”, and “packages/processing” directories include components for executing strategies (like grid, DCA, RSI), handling market data, and interacting with exchanges.
- Strategy Templates & Indicators: In “packages/bot-templates” (for prebuilt strategies) and “packages/indicators” (with technical indicator implementations) that plug into the bot engine.
- Exchange Connectors: The “packages/exchanges” directory contains connectors that abstract interactions with 100+ exchanges (leveraging CCXT) for both live trading and backtesting.
- Database & Persistence: Managed by “packages/db” and “packages/prisma” for ORM, migrations, and structured logging of bots, orders, and trades.
- API & RPC Layer: “packages/trpc” exposes backend API endpoints (with many routers handling bot, exchange accounts, orders, etc.) for integration between UI and backend.
- Developer & Utility Tools: Configuration files (Dockerfile, docker-compose.yml, moon toolchain, pnpm-workspace configuration) and packages for linting, testing, and shared types (“packages/types”, “packages/tsconfig”) ensure maintainability.

##### Relationships and Interactions
- The CLI commands (e.g., “opentrader up”, “opentrader trade”, “opentrader backtest”) trigger processes in the bot engine modules that use the strategy templates and indicators. Represent these as separate but connected components.
- The bot processing components (order execution logic, market data processing) exchange events via the event bus (see “packages/event-bus”) and persist state into the database.
- Backtesting modules take historical market data inputs (either via simulated data or connectors) to evaluate strategy performance and then relay results to the UI.

##### Architectural Patterns and Design Principles
- Modularity: The design encourages swapping or extending strategies, connectors, or indicators without significant cross-coupling.
- Service-Oriented/Microservice-like Organization within the Monorepo: Although built as a single repository, components (such as API endpoints, processing, and database) are clearly segregated.
- Use of an RPC/API Layer: The TRPC server ensures that the UI and CLI interact with backend services in a consistent and type-safe manner.

##### Relevant Technologies and Frameworks
- Node.js (v22 or higher) and TypeScript for the entire stack.
- Docker (with Dockerfile, docker-compose) for containerized deployment.
- CCXT for connecting to multiple cryptocurrency exchanges.
- Prisma as the ORM to interact with the database.
- TRPC for exposing an API layer that ties frontend and backend components together.
- Various build and lint tools (Moon, TSConfig, ESLint, Prettier) to maintain code quality within the monorepo.

##### Specific Elements
- From UI → API/RPC Server.
- From CLI/daemon → Bot Engine.
- From Bot Engine → Exchange Connectors.
- From Bot Engine → Database (for persistence and logging).
- From Backtesting Module → UI (for results visualization).
– Clearly annotate important technical details, such as “CCXT for exchange connectivity”, “Prisma ORM for DB interactions”, and “TRPC for API endpoints”.

- Drill down to show how components interact – from the user command (either CLI or via the web UI) through the bot processing and strategy modules, to the external exchanges and database.
- Modularity, separation of concerns, and the streaming flow of market data and trading commands.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Subgraphs
    subgraph "User Interface"
        UI["Frontend/UI"]:::frontend
    end

    subgraph "CLI & API Server"
        CLI["CLI/Daemon & API Server"]:::cli
    end

    subgraph "Core Trading System"
        subgraph "Bot Engine"
            BOT["Bot"]:::core
            BP["Bot Processor"]:::core
            PROC["Processing"]:::core
        end
        ST["Strategy Templates"]:::core
        IND["Indicators"]:::core
        BACK["Backtesting Module"]:::core
    end

    subgraph "Infrastructure"
        RPC["API/RPC Layer"]:::infra
        EB["Event Bus"]:::infra
        DB["Database"]:::infra
        PRISMA["Prisma ORM"]:::infra
    end

    subgraph "Exchange Integration"
        EXC["Exchange Connectors"]:::exchange
    end

    %% Connections
    UI -->|"sends_commands"| RPC
    CLI -->|"integrates_with"| RPC
    CLI -->|"triggers_bot_engine"| BOT

    RPC -->|"commands"| BOT

    BOT -->|"uses"| ST
    BOT -->|"uses"| IND

    PROC -->|"persists_state"| DB
    PROC -->|"processes_orders"| EXC
    BP  -->|"publishes_events"| EB

    BACK -->|"sends_results"| UI

    %% Class Definitions for Colour Coding
    classDef frontend fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef cli fill:#C6F1C6,stroke:#333,stroke-width:2px;
    classDef core fill:#FFF7C2,stroke:#333,stroke-width:2px;
    classDef infra fill:#FFD3B6,stroke:#333,stroke-width:2px;
    classDef exchange fill:#FFB6C1,stroke:#333,stroke-width:2px;

    %% Click Events
    click UI "https://github.com/open-trader/opentrader/tree/dev/apps/cli/frontend"
    click CLI "https://github.com/open-trader/opentrader/tree/dev/apps/cli"
    click BOT "https://github.com/open-trader/opentrader/tree/dev/packages/bot"
    click BP "https://github.com/open-trader/opentrader/tree/dev/packages/bot-processor"
    click PROC "https://github.com/open-trader/opentrader/tree/dev/packages/processing"
    click ST "https://github.com/open-trader/opentrader/tree/dev/packages/bot-templates"
    click IND "https://github.com/open-trader/opentrader/tree/dev/packages/indicators"
    click EXC "https://github.com/open-trader/opentrader/tree/dev/packages/exchanges"
    click RPC "https://github.com/open-trader/opentrader/tree/dev/packages/trpc"
    click EB "https://github.com/open-trader/opentrader/tree/dev/packages/event-bus"
    click DB "https://github.com/open-trader/opentrader/tree/dev/packages/db"
    click PRISMA "https://github.com/open-trader/opentrader/tree/dev/packages/prisma"
```
