# akhandhediya/openalgo_v2 architecture

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

- Interactive diagram: https://gitdiagram.com/akhandhediya/openalgo_v2
- Repository: https://github.com/akhandhediya/openalgo_v2

## Overview

##### Project Type & Purpose
- OpenAlgo is a monolithic, full-stack Flask application with:
- A unified REST API (Flask-RestX + Blueprints)
- Real-time WebSocket streaming (Flask-SocketIO + ZeroMQ)
- Modular broker integrations (24+ “broker/…” packages)
- Python strategy hosting, sandbox (simulated trading engine)
- UI layer (Jinja2 templates + static assets)
- Auxiliary services (Telegram bot, ChartInk, AI integrations)
- Multiple SQLite DBs (main, logs, latency, sandbox)

##### File Structure & Map to Layers
- Presentation Layer
- templates/ & static/ – Jinja2 HTML, Tailwind/DaisyUI CSS, JS
- playground/, examples/ – client-side demos
- API Layer
- app.py + blueprints/*.py – Flask app entry and route modules
- restx_api/*.py – OpenAPI schema + REST endpoints
- Service Layer
- services/*.py – Business logic for each endpoint (order, funds, quotes, sandbox, etc.)
- Data Access Layer
- database/*.py – SQLAlchemy models, initialization, caching hooks
- sandbox/*.py – In‐memory/simulated trading logic
- Broker Integration Layer
- broker/<broker-name>/* – Each broker adapter implements auth_api, data_api, order_api, streaming, mapping, plugin.json
- websocket_proxy/ – Unified WebSocket proxy, ZMQ bus, broker factory
- Utility Layer
- utils/*.py – config loader, encryption, rate limiter, logging, middleware, plugin loader
- Infrastructure & Config
- Dockerfile, docker-compose.yaml, .ebextensions, install/, design/, docs/
- .env/.sample.env, config files
- Databases
- SQLite files (main, logs, latency, sandbox) managed via SQLAlchemy

##### README for Architectural Highlights
- Shows four separate SQLite DBs, broker-pattern, ZeroMQ bus, Flask + SocketIO, Tailwind + DaisyUI
- Lists external clients: TradingView, Python/Go/Node SDKs, AI agents, Telegram, ChartInk
- Emphasizes sandbox isolation, action-center, strategy manager

1. Clients & Integrations
– Web UI (browser)
– REST clients (Python SDK, Go/Node SDK, Postman, Excel, TradingView webhooks)
– AI/MCP agents
– Telegram Bot
2. API & UI Server
– Flask App (app.py)
- Blueprints (auth, dashboard, analyzer, apikey, etc.)
- RESTX API layer
- SocketIO endpoints
3. Service Layer
– Order router, quote service, funds service, sandbox service, action center service, etc.
4. Broker Adapters
– Adapter modules under broker/*: each implements
- REST APIs (auth, data, order, margin)
- Streaming clients + mapping
5. WebSocket Proxy & Message Bus
– websocket_proxy/server.py
– ZeroMQ pub/sub bus
6. Data Layer & Databases
– SQLAlchemy models & multiple SQLite files
– Caching & hooks (master_contract_cache_hook)
7. Sandbox Engine
– sandbox/*.py modules: order_manager, execution_engine, fund_manager, etc.

– Clients → Flask App → Blueprints/RestX → Services → Broker Adapters → External Broker APIs
– Services ↔ Database (SQLAlchemy)
– Broker Adapters → ZMQ Bus → WebSocket Proxy → Clients (via SocketIO/WebSocket)
– UI/browser ↔ Flask (Jinja2 templates + SocketIO)
– Telegram Bot Service → Telegram API
– ChartInk Webhooks → Flask Blueprints → Services
– AI Agents ↔ MCP Server (external component) → Flask App

c. Architectural Patterns & Principles:
– Layered Architecture (Presentation → API → Service → Data)
– Broker-Pattern for pluggable integrations
– Plugin Loader (utils/plugin_loader) for dynamic broker discovery
– Separation of concerns: sandbox vs. live mode (isolated DB)
– Event-driven streaming: ZeroMQ message bus
– Monolithic deployment but modular code organization

– Flask 3.0, Flask-SocketIO, Flask-RestX
– SQLAlchemy 2.0 (SQLite)
– ZeroMQ
– Jinja2, Tailwind CSS + DaisyUI
– Python 3.11+
– Docker, AWS Elastic Beanstalk (.ebextensions)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        UI["Web UI<br/>(templates/, static/)"]:::presentation
        Playground["Client Demos<br/>(playground/, examples/)"]:::presentation
    end

    %% API & Service Layer
    subgraph "API & Service Layer"
        FlaskApp["Flask App<br/>(app.py)"]:::apiService
        Blueprints["Blueprints<br/>(blueprints/)"]:::apiService
        RestX["RESTX API<br/>(restx_api/)"]:::apiService
        Services["Business Logic<br/>(services/)"]:::apiService
    end

    %% Broker Integration & Messaging
    subgraph "Broker Integration & Messaging"
        BrokerAdapters["Broker Adapters<br/>(broker/)"]:::broker
        ZMQBus["ZeroMQ Bus"]:::broker
        WSP["WebSocket Proxy<br/>(websocket_proxy/)"]:::broker
    end

    %% Data & Sandbox Layer
    subgraph "Data & Sandbox Layer"
        DB["Databases<br/>(database/)"]:::data
        Sandbox["Sandbox Engine<br/>(sandbox/)"]:::data
    end

    %% Utility Layer
    subgraph "Utility Layer"
        Utils["Utilities<br/>(utils/)"]:::utility
        CORS["Middleware<br/>(cors.py, csp.py, limiter.py, extensions.py)"]:::utility
    end

    %% Infrastructure & Config
    subgraph "Infrastructure & Config"
        Docker["Dockerfile & docker-compose.yaml"]:::infra
        EB[".ebextensions/"]:::infra
        Install["install/"]:::infra
        Design["design/"]:::infra
        Docs["docs/"]:::infra
    end

    %% External Clients
    subgraph "External Clients"
        Browser["Browser UI"]:::external
        SDKs["REST Clients<br/>(Python/Go/Node/TradingView)"]:::external
        AIAgents["AI Agents"]:::external
        Telegram["Telegram Bot"]:::external
        ChartInk["ChartInk Webhooks"]:::external
    end

    %% Connections
    Browser -->|"HTTP/WebSocket"| UI
    UI -->|"Renders Jinja2"| FlaskApp
    Browser -->|"SocketIO"| WSP
    SDKs -->|"HTTP REST"| RestX
    ChartInk -->|"Webhook"| Blueprints
    Telegram -->|"Bot API"| FlaskApp
    AIAgents -->|"API Calls"| RestX

    FlaskApp -->|"Registers"| Blueprints
    Blueprints -->|"Calls"| Services
    RestX -->|"Calls"| Services
    Services -->|"Reads/Writes"| DB
    Services -->|"Uses"| Sandbox
    Services -->|"Invokes"| BrokerAdapters

    BrokerAdapters -->|"Publishes"| ZMQBus
    ZMQBus -->|"Subscribes"| WSP
    WSP -->|"Broadcast"| Browser

    Utils -->|"Loaded by"| FlaskApp
    CORS -->|"Middleware"| FlaskApp

    %% Click Events
    click UI "https://github.com/akhandhediya/openalgo_v2/tree/main/templates/"
    click UI "https://github.com/akhandhediya/openalgo_v2/tree/main/static/"
    click Playground "https://github.com/akhandhediya/openalgo_v2/tree/main/playground/"
    click Playground "https://github.com/akhandhediya/openalgo_v2/tree/main/examples/"
    click FlaskApp "https://github.com/akhandhediya/openalgo_v2/blob/main/app.py"
    click Blueprints "https://github.com/akhandhediya/openalgo_v2/tree/main/blueprints/"
    click RestX "https://github.com/akhandhediya/openalgo_v2/tree/main/restx_api/"
    click Services "https://github.com/akhandhediya/openalgo_v2/tree/main/services/"
    click DB "https://github.com/akhandhediya/openalgo_v2/tree/main/database/"
    click Sandbox "https://github.com/akhandhediya/openalgo_v2/tree/main/sandbox/"
    click BrokerAdapters "https://github.com/akhandhediya/openalgo_v2/tree/main/broker/"
    click WSP "https://github.com/akhandhediya/openalgo_v2/tree/main/websocket_proxy/"
    click Utils "https://github.com/akhandhediya/openalgo_v2/tree/main/utils/"
    click CORS "https://github.com/akhandhediya/openalgo_v2/blob/main/utils/extensions.py"
    click Docker "https://github.com/akhandhediya/openalgo_v2/tree/main/Dockerfile"
    click Docker "https://github.com/akhandhediya/openalgo_v2/blob/main/docker-compose.yaml"
    click EB "https://github.com/akhandhediya/openalgo_v2/tree/main/.ebextensions/"
    click Install "https://github.com/akhandhediya/openalgo_v2/tree/main/install/"
    click Design "https://github.com/akhandhediya/openalgo_v2/tree/main/design/"
    click Docs "https://github.com/akhandhediya/openalgo_v2/tree/main/docs/"

    %% Styles
    classDef presentation fill:#cce5ff,stroke:#339,stroke-width:1px
    classDef apiService fill:#d4edda,stroke:#2a6b2e,stroke-width:1px
    classDef broker fill:#fff3cd,stroke:#785f01,stroke-width:1px
    classDef data fill:#fffbea,stroke:#826700,stroke-width:1px
    classDef utility fill:#e2e3e5,stroke:#6c757d,stroke-width:1px
    classDef infra fill:#f8d7da,stroke:#721c24,stroke-width:1px
    classDef external fill:#d6d8db,stroke:#1b1e21,stroke-width:1px
```
