# artooze/crypto-arbitrager architecture

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

- Interactive diagram: https://gitdiagram.com/artooze/crypto-arbitrager
- Repository: https://github.com/artooze/crypto-arbitrager

## Overview

##### Main Components
- Entry Point and Core Arbitrage Engine: The main script (arbitrage/arbitrage.py) acts as a command-line interface and orchestrates the operation. It parses command‐line flags (e.g., “watch”, “get-balance”, “list-public-markets”) and triggers the core arbitrage logic.
- Configuration Module: There is a configurable file (config.py, copied from config.py-example) which holds settings like watched markets and observer preferences.
- Exchanges Interfaces: The project splits the exchange connectivity into “public_markets” and “private_markets” directories. These modules are responsible for fetching public order book data (e.g., from Bitfinex, OkCoin) and handling account‐related data or trading (for authenticated sessions).
- Broker Communication: Within the “lib” folder, modules like broker_api.py and broker.thrift hint at inter-process communication with an external broker service (using thrift). This broker service is required for trade automation.
- Observers (Plugins): In “observers”, various observers (e.g., TraderBot, HedgerBot, Logger, Emailer) encapsulate different behaviors triggered by arbitrage opportunities. They represent reaction or execution strategies.
- Utility Libraries: Common helper functions and settings are located in “lib” and the root “utils.py”. These modules help in formatting data, performing calculations, and managing common tasks.
- Testing and Miscellaneous: The “test” directory holds tests for arbitrage speed and functionality. Auxiliary tools are provided in the “tools” directory (e.g., for autopep8 formatting).

##### Relationships and Data Flow
- The User (or a scheduled task) initiates execution via the command-line interface (arbitrage.py).
- The core engine reads the configuration and instructs the appropriate market modules (either public or private) to retrieve market data.
- The fetched data flows into the arbitrage algorithm (in modules like arbitrage/arbitrer.py) that calculates possible arbitrage opportunities.
- Once opportunities are detected, control is passed to one or more observers. Each observer (such as TraderBot, BalanceDumper, Logger) handles actions like executing trades, logging activities, or sending notifications.
- For automated trades, the engine may communicate with the external broker service via the broker API (thrift-based) to execute orders on the various exchanges.
- Helper and utility functions from the “lib” and “utils.py” are used throughout these interactions.
- Test modules reside at the periphery for integrity checks.

##### Architectural Patterns and Key Technologies
- Modular Design: The clear separation between market data access (public/private), core calculation (arbitrage.py and arbitrer.py), and various observers adheres to separation of concerns.
- Observer/Plugin Pattern: The observer modules provide extension points where multiple strategies or logging functions can be attached without altering the central arbitrage engine.
- External Service Integration: Interaction with an external market broker via thrift demonstrates the use of service-oriented communication.
- Language and Libraries: The project uses Python 3, with additional libraries like requests, zmq, thriftpy, and sleekxmpp for messaging. Emphasis is given to real-time data processing and execution.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Global Entities
    User["User/Scheduled Task"]:::core

    %% Main Modules
    CLI["CLI Entry Point & Core Engine (arbitrage.py)"]:::core
    Config["Configuration Module (config.py)"]:::core
    Pub["Public Markets Module"]:::external
    Priv["Private Markets Module"]:::external
    ArbComp["Arbitrage Computation Module (arbitrer.py)"]:::core

    %% Broker Communication Subgraph
    subgraph "Broker Communication Module"
        BrokerAPI["Broker API"]:::external
        BrokerThrift["Broker Thrift"]:::external
    end

    %% Observers/Plugins Subgraph
    subgraph "Observers/Plugins"
        TraderBot["TraderBot"]:::plugin
        HedgerBot["HedgerBot"]:::plugin
        Emailer["Emailer"]:::plugin
        Logger["Logger"]:::plugin
    end

    %% Utility Libraries Subgraph
    subgraph "Utility Libraries"
        Helpers["Helpers"]:::utility
        Settings["Settings"]:::utility
        Utils["Utils"]:::utility
    end

    %% Testing and Auxiliary Tools
    Testing["Testing"]:::test
    AuxTools["Auxiliary Tools"]:::auxiliary

    %% Data Flow Connections
    User --> CLI
    CLI -->|"reads_config"| Config
    CLI -->|"fetches_public_data"| Pub
    CLI -->|"fetches_private_data"| Priv
    Pub -->|"sends_data"| ArbComp
    Priv -->|"sends_data"| ArbComp
    CLI -->|"triggers_computation"| ArbComp
    ArbComp -->|"notifies"| TraderBot
    ArbComp -->|"notifies"| HedgerBot
    ArbComp -->|"notifies"| Emailer
    ArbComp -->|"notifies"| Logger
    ArbComp -->|"executes"| BrokerAPI
    ArbComp -->|"executes"| BrokerThrift
    CLI -->|"utilizes"| Helpers
    CLI -->|"utilizes"| Settings
    CLI -->|"utilizes"| Utils
    ArbComp -->|"utilizes"| Helpers
    ArbComp -->|"utilizes"| Settings
    ArbComp -->|"utilizes"| Utils
    Testing -.->|"tests"| CLI
    AuxTools -.->|"formats"| CLI

    %% Click Events
    click CLI "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/arbitrage.py"
    click ArbComp "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/arbitrer.py"
    click Config "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/config.py-example"
    click Pub "https://github.com/artooze/crypto-arbitrager/tree/master/arbitrage/public_markets/"
    click Priv "https://github.com/artooze/crypto-arbitrager/tree/master/arbitrage/private_markets/"
    click BrokerAPI "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/lib/broker_api.py"
    click BrokerThrift "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/lib/broker.thrift"
    click Helpers "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/lib/helpers.py"
    click Settings "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/lib/settings.py"
    click Utils "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/utils.py"
    click TraderBot "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/observers/traderbot.py"
    click HedgerBot "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/observers/hedgerbot.py"
    click Emailer "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/observers/emailer.py"
    click Logger "https://github.com/artooze/crypto-arbitrager/blob/master/arbitrage/observers/logger.py"
    click Testing "https://github.com/artooze/crypto-arbitrager/tree/master/arbitrage/test/"
    click AuxTools "https://github.com/artooze/crypto-arbitrager/blob/master/tools/autopep8-project.sh"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef external fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef plugin fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef utility fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef test fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef auxiliary fill:#f5c6cb,stroke:#721c24,stroke-width:2px;
```
