# bhavik2205/go-project architecture

> Architecture diagram of the GitHub repository bhavik2205/go-project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bhavik2205/go-project
- Repository: https://github.com/bhavik2205/go-project

## Overview

##### Project type and purpose
- This is a modular, backend‐focused “Equity Trading Bot” written in Go.
- It provides real-time market data ingestion, strategy execution, order placement via Zerodha, sentiment analysis, multi-user management, observability (Prometheus/Grafana), and background utilities (backtesting, data download, model training).

##### File structure
- Top-level:
- cmd/ – entry points (live bot, backtester, token fetcher)
- configs/ – YAML settings for app, DB, APIs, strategies, ML
- internal/ – core layered modules:
– api/ (broker & news clients, HTTP handlers)
– auth/ (JWT, signup/login, broker OAuth)
– db/ (PostgreSQL client, models, migrations)
– cache/ (Redis)
– data/ (tick ingestion, preprocessing, pipelines)
– model/ (inference, training, sentiment)
– strategy/ (intraday, swing, scalping, selector)
– execution/ (order placement, monitoring)
– server/ (HTTP router, WebSocket broadcaster)
– metrics/ (Prometheus integration)
– utils/ (config loader, logger, errors)
- Supporting: scripts/, notebooks/, models/ (ONNX), Dockerfile, docker-compose.yaml, Makefile.

##### README for additional insights
- Detailed system flow from initialization → API integrations → data ingestion → strategy engine → execution → monitoring → real-time broadcasting.
- Tech stack: Go, PostgreSQL/TimescaleDB, Redis, ONNX Runtime, Python scripts, Prometheus, Grafana, Docker.
- Emphasis on concurrency (goroutines/channels), fault tolerance (retries, encryption, graceful shutdown), modularity, multi-user support.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Control Plane
    subgraph "Control Plane"
        direction TB
        ENV[".env"]:::config
        CFG["Config Loader"]:::config
        LOG["Logger"]:::core
        ENTRY["Entry Points\n(cmd/main.go\ncmd/backtest.go\ncmd/get_token.go)"]:::core
    end

    ENV --> CFG
    CFG --> ENTRY
    ENTRY --> LOG

    click CFG "https://github.com/bhavik2205/go-project/blob/main/internal/utils/config.go"
    click ENV "https://github.com/bhavik2205/go-project/blob/main/.env"
    click ENTRY "https://github.com/bhavik2205/go-project/blob/main/cmd/main.go"

    %% Data Plane - Core Services
    subgraph "Data Plane"
        direction TB
        subgraph "API Clients" 
            direction TB
            ZER["Zerodha API Client"]:::core
            NEWS["NewsAPI Client"]:::core
            MW["MarketWatch Client"]:::core
        end
        subgraph "HTTP Handlers"
            direction TB
            HANDLER["Stock HTTP Handler"]:::core
        end
        subgraph "Data Ingestion & Processing"
            direction TB
            INGEST["Ingest"]:::core
            PREP["Preprocess"]:::core
            INDIC["Indicators Manager"]:::core
            CAND["Candles"]:::core
            NEWS_PIPE["News Pipeline"]:::core
        end
        subgraph "Strategy Selector & Executors"
            direction TB
            SELECT["Selector"]:::core
            INTRA["Intraday"]:::core
            SWING["Swing"]:::core
            SCALP["Scalping"]:::core
        end
        subgraph "Execution & Order Monitor"
            direction TB
            ORDER["Order Placement"]:::core
            MON["Order Monitor"]:::core
        end
        subgraph "Auth & HTTP/WebSocket Server"
            direction TB
            AUTH["Auth Module"]:::core
            SERVER["HTTP & WS Server"]:::core
        end
        subgraph "Metrics Collector"
            direction TB
            METRICS["Prometheus Metrics"]:::core
        end
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        PG["PostgreSQL + TimescaleDB"]:::db
        REDIS["Redis Pub/Sub"]:::db
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        EXT_ZER["Zerodha Kite Connect"]:::ext
        EXT_NEWS["NewsAPI"]:::ext
        EXT_MW["MarketWatch API"]:::ext
        ONNX["ONNX Runtime"]:::ext
        PROM["Prometheus Server"]:::ext
        GRAF["Grafana Dashboard"]:::ext
    end

    %% Batch/Offline Components
    subgraph "Batch / Offline"
        direction TB
        SCRIPTS["Auxiliary Scripts"]:::script
        NOTEBOOKS["Notebooks"]:::script
        TRAINER["Model Trainer"]:::core
    end

    %% Flows
    ENTRY -->|"init services"| CFG
    ENTRY -->|"init services"| LOG
    ENTRY -->|"init DB & Redis"| PG
    ENTRY -->|"init DB & Redis"| REDIS
    ENTRY -->|"init metrics"| METRICS

    %% API to ingestion
    ZER -.->|"REST call"| EXT_ZER
    NEWS -.->|"REST call"| EXT_NEWS
    MW -.->|"REST call"| EXT_MW
    ZER -->|"ticks"| INGEST
    NEWS -->|"news"| NEWS_PIPE
    MW -->|"data"| INGEST

    %% Handlers
    HANDLER -->|"HTTP request"| AUTH
    AUTH -->|"JWT / user"| PG

    %% Ingestion pipeline
    INGEST -->|"raw ticks"| PREP
    PREP --> INDIC
    INDIC --> CAND
    CAND -->|"persist"| PG
    CAND -->|"publish"| REDIS
    NEWS_PIPE -->|"persist"| PG
    NEWS_PIPE -->|"publish"| REDIS

    %% Strategy
    REDIS -->|"Pub/Sub messages"| SELECT
    PG -->|"DB state"| SELECT
    SELECT --> INTRA
    SELECT --> SWING
    SELECT --> SCALP

    INTRA --> ORDER
    SWING --> ORDER
    SCALP --> ORDER

    %% Execution and monitoring
    ORDER -->|"place order"| EXT_ZER
    ORDER -->|"persist order"| PG
    ORDER -->|"publish update"| REDIS
    ORDER --> MON
    MON -->|"order status"| PG
    MON -->|"publish"| REDIS

    %% Server
    SERVER --> REDIS
    SERVER --> HANDLER
    SERVER --> AUTH
    SERVER -->|"WS ticks"| EXT_ZER

    %% Metrics
    METRICS --> PROM
    PROM --> GRAF
    METRICS -->|"expose /metrics"| SERVER

    %% Batch offline
    SCRIPTS --> TRAINER
    NOTEBOOKS --> TRAINER
    TRAINER --> ONNX
    TRAINER --> PG
    TRAINER --> REDIS

    %% Click events for components
    click ZER "https://github.com/bhavik2205/go-project/blob/main/internal/api/zerodha.go"
    click NEWS "https://github.com/bhavik2205/go-project/blob/main/internal/api/newsapi.go"
    click MW "https://github.com/bhavik2205/go-project/blob/main/internal/api/marketwatch.go"
    click HANDLER "https://github.com/bhavik2205/go-project/blob/main/internal/api/handlers/stockHandler/instrumentData.go"
    click INGEST "https://github.com/bhavik2205/go-project/blob/main/internal/data/ingest.go"
    click PREP "https://github.com/bhavik2205/go-project/blob/main/internal/data/preprocess.go"
    click INDIC "https://github.com/bhavik2205/go-project/blob/main/internal/data/indicators_manager.go"
    click CAND "https://github.com/bhavik2205/go-project/blob/main/internal/data/candels.go"
    click NEWS_PIPE "https://github.com/bhavik2205/go-project/blob/main/internal/data/news_pipeline.go"
    click SELECT "https://github.com/bhavik2205/go-project/blob/main/internal/strategy/selector.go"
    click INTRA "https://github.com/bhavik2205/go-project/blob/main/internal/strategy/intraday.go"
    click SWING "https://github.com/bhavik2205/go-project/blob/main/internal/strategy/swing.go"
    click SCALP "https://github.com/bhavik2205/go-project/blob/main/internal/strategy/scalping.go"
    click ORDER "https://github.com/bhavik2205/go-project/blob/main/internal/execution/order.go"
    click MON "https://github.com/bhavik2205/go-project/blob/main/internal/execution/monitor.go"
    click PG "https://github.com/bhavik2205/go-project/blob/main/internal/db/postgres.go"
    click REDIS "https://github.com/bhavik2205/go-project/blob/main/internal/cache/redis.go"
    click TRAINER "https://github.com/bhavik2205/go-project/blob/main/internal/model/trainer.go"
    click SERVER "https://github.com/bhavik2205/go-project/blob/main/internal/server/routes.go"
    click METRICS "https://github.com/bhavik2205/go-project/blob/main/internal/db/migrations/000010_create_metrics_table.up.sql"
    click LOG "https://github.com/bhavik2205/go-project/blob/main/internal/utils/logger.go"
    click SCRIPTS "https://github.com/bhavik2205/go-project/blob/main/scripts/convert_to_onnx.py"
    click NOTEBOOKS "https://github.com/bhavik2205/go-project/blob/main/notebooks/EDA_intraday.ipynb"

    classDef core fill:#D6E4FF,stroke:#004085
    classDef db fill:#FFE8CC,stroke:#FFB020
    classDef ext fill:#D4EDDA,stroke:#155724
    classDef config fill:#F0F0F0,stroke:#6C757D
    classDef script fill:#F8F9FA,stroke:#6C757D
```
