# lowquant/ib-multi-strategy-ats architecture

> Architecture diagram of the GitHub repository lowquant/ib-multi-strategy-ats, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lowquant/ib-multi-strategy-ats
- Repository: https://github.com/lowquant/ib-multi-strategy-ats

## Overview

##### Project Type and Purpose
- **Type**: Full-stack desktop application (Automated Trading System) with GUI, core trading logic, data/research modules, and integration with external services.
- **Purpose**: Manage and execute multiple trading strategies via Interactive Brokers (using `ib_async`), store and query data in ArcticDB, provide a user-friendly GUI for configuration, monitoring, and research.

##### File Structure Analysis
- **Top-level modules**:
- `gui/` – Presentation layer (settings, portfolio, database, research windows)
- `strategy_manager/` – Strategy loading, management, and execution
- `broker/` – Trading integration, connection, order/risk/portfolio management
- `data_and_research/` – Data ingestion, storage (ArcticDB), scheduled jobs, Jupyter notebooks
- `main.py` – Entry point that initializes GUI and core services
- **Supporting files**:
- `options.xlsx`, `univ_us_equities.csv` – static reference data
- `setup.py` & `.egg-info` – installation/configuration
- `README.md` – project overview, architecture notes, usage

##### Key README Insights
- **Architecture calls out**:
- StrategyManager handles dynamic strategy loading & execution.
- PortfolioManager interfaces with IB and ArcticDB for live P&L & positions.
- GUI modules orchestrate user interactions with core services and data.
- **External services**: IB Gateway/TWS via `ib_async`, ArcticDB for persistence, cron for scheduled jobs.

##### Main Components
a. **Frontend (GUI)**
- Settings Window
- Portfolio Window
- Database Explorer
- Research Tools
- Start Trading Dashboard
b. **Core Application Services**
- StrategyManager
- PortfolioManager
- TradeManager & RiskManager (inside `broker/`)
- DataManager (ArcticDB interface)
c. **External Services & Data Stores**
- Interactive Brokers API (via ib_async)
- ArcticDB (local or remote storage)
- File-based data (CSV/XLSX)
- Cron scheduler for data jobs
d. **Research & Batch Jobs**
- Jupyter Notebooks for ad-hoc analysis
- Python scripts under `data_and_research/jobs/`

##### Relationships and Interactions
- GUI → Core Services
- User actions in GUI invoke StrategyManager, PortfolioManager, DataManager.
- StrategyManager ↔ Broker
- Loads strategy code → generates orders → sends to TradeManager.
- Broker ↔ Interactive Brokers
- TradeManager submits orders, receives fills; PortfolioManager fetches positions and account data.
- Core Services ↔ ArcticDB
- Persist P&L, cash flows, positions, historical data via DataManager.
- GUI Database Explorer queries DataManager.
- DataAndResearch ↔ ArcticDB
- Scheduled jobs pull market data, store in ArcticDB.
- Research notebooks query ArcticDB for backtesting or analysis.

##### Architectural Patterns & Principles
- **Modular Layering**: Clear separation between Presentation (GUI), Business Logic (StrategyManager, PortfolioManager), Data Access (DataManager/ArcticDB), and External Integrations (IB API).
- **Plugin Strategy Pattern**: Dynamic loading of strategy modules under `strategy_manager/strategies/`.
- **Event-driven / Async I/O**: `ib_async` allows non-blocking market data & order flow.
- **Batch vs Interactive Modes**: GUI-driven live trading vs scheduled data jobs.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer (GUI)
    subgraph "Presentation Layer"
        direction TB
        Main["main.py\n(Launcher)"]:::ui
        GUI["GUI Module\n(gui/gui.py)"]:::ui
        Settings["Settings Window\n(gui/settings_window.py)"]:::ui
        PortfolioWin["Portfolio Window\n(gui/portfolio_window.py)"]:::ui
        DatabaseWin["Database Explorer\n(gui/database_window.py)"]:::ui
        ResearchTools["Research Tools"]:::ui
        Assets["GUI Assets\n(gui/assets/)"]:::ui
        UtilsGUI["GUI Utils & Logging\n(gui/utils.py, gui/log.py)"]:::ui

        Main --> GUI
        GUI --> Settings
        GUI --> PortfolioWin
        GUI --> DatabaseWin
        GUI --> ResearchTools
        GUI --> Assets
        GUI --> UtilsGUI
    end

    %% Trading Engine Layer
    subgraph "Trading Engine"
        direction TB
        StrategyMgr["StrategyManager\n(strategy_manager/strategy_manager.py)"]:::core
        Strategies["Strategy Plugins\n(strategy_manager/strategies/)"]:::core
        PortfolioMgr["PortfolioManager\n(broker/portfoliomanager.py)"]:::core
        TradeMgr["TradeManager\n(broker/trademanager.py)"]:::core
        RiskMgr["RiskManager\n(broker/riskmanager.py)"]:::core
        BrokerConn["Broker Connection\n(broker/connection.py,\nbroker/trade.py,\nbroker/functions.py,\nbroker/utilityfunctions.py,\nbroker/utils.py)"]:::core

        StrategyMgr --> Strategies
        StrategyMgr --> TradeMgr
        TradeMgr --> BrokerConn
        PortfolioMgr --> BrokerConn
        TradeMgr --> RiskMgr
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DataMgr["DataManager\n(data_and_research/data_manager.py)"]:::data
        BatchJobs["Scheduled Batch Jobs\n(data_and_research/jobs/)"]:::data
        Notebooks["Research Notebooks\n(data_and_research/notebooks/)"]:::data
        DataUtils["Data & Research Utils\n(data_and_research/utils.py,\ndata_and_research/db/)"]:::data
        StaticData["Static Reference Data\n(options.xlsx,\nuniv_us_equities.csv)"]:::data
        ArcticDB["ArcticDB\n(Cylinder)"]:::storage

        BatchJobs --> DataMgr
        Notebooks --> DataMgr
        DataMgr --> ArcticDB
        PortfolioMgr --> DataMgr
        StrategyMgr --> DataMgr
        DataUtils --> BatchJobs
        StaticData --> DataMgr
    end

    %% External Services
    subgraph "External Services"
        direction TB
        IBAPI["Interactive Brokers API\n(ib_async)"]:::external
        Cron["Cron Scheduler"]:::external
    end

    %% Interactions
    Settings -->|"invoke"| StrategyMgr
    PortfolioWin -->|"invoke"| PortfolioMgr
    DatabaseWin -->|"query"| DataMgr
    ResearchTools -->|"invoke analysis"| Notebooks

    StrategyMgr -->|"order request"| TradeMgr
    TradeMgr -->|"submit orders / receive fills"| IBAPI
    PortfolioMgr -->|"fetch positions/P&L"| IBAPI
    Cron --> BatchJobs

    %% Click Events
    click Main "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/main.py"
    click Settings "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/gui/settings_window.py"
    click PortfolioWin "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/gui/portfolio_window.py"
    click DatabaseWin "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/gui/database_window.py"
    click GUI "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/gui/gui.py"
    click Assets "https://github.com/lowquant/ib-multi-strategy-ats/tree/master/gui/assets/"
    click UtilsGUI "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/gui/utils.py"
    click StrategyMgr "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/strategy_manager/strategy_manager.py"
    click Strategies "https://github.com/lowquant/ib-multi-strategy-ats/tree/master/strategy_manager/strategies/"
    click PortfolioMgr "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/broker/portfoliomanager.py"
    click TradeMgr "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/broker/trademanager.py"
    click RiskMgr "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/broker/riskmanager.py"
    click BrokerConn "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/broker/connection.py"
    click DataMgr "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/data_and_research/data_manager.py"
    click BatchJobs "https://github.com/lowquant/ib-multi-strategy-ats/tree/master/data_and_research/jobs/"
    click Notebooks "https://github.com/lowquant/ib-multi-strategy-ats/tree/master/data_and_research/notebooks/"
    click DataUtils "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/data_and_research/utils.py"
    click StaticData "https://github.com/lowquant/ib-multi-strategy-ats/blob/master/options.xlsx"

    %% Styling
    classDef ui fill:#D0E6FF,stroke:#3399FF,color:#000;
    classDef core fill:#DFFFD0,stroke:#78C000,color:#000;
    classDef data fill:#FFE8A1,stroke:#FFB600,color:#000;
    classDef storage fill:#CCCCCC,stroke:#888888,color:#000,stroke-dasharray: 5 5;
    classDef external fill:#E8E8E8,stroke:#666666,color:#000,stroke-dasharray: 2 2;
```
