# guling-pro/guling-trader architecture

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

- Interactive diagram: https://gitdiagram.com/guling-pro/guling-trader
- Repository: https://github.com/guling-pro/guling-trader

## Overview

guling-trader is a Windows desktop trading bridge: an AI client sends MCP tool requests through a relay to the running app, which dispatches them to automate the logged-in 同花顺 xiadan client and returns structured results. The app also exposes account, order, settlement, and watchlist queries; watches order and watchlist changes; manages pairing and local settings; and supports OCR setup and self-updating. Relationships are limited to README-documented flows and source-index references or shown excerpts; unsampled subsystem details are not inferred.

Main entry points: [`src/trader/main.py`](https://github.com/guling-pro/guling-trader/blob/main/src/trader/main.py) (App startup), [`src/trader/ws_client.py`](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ws_client.py) (WebSocket client), [`src/trader/dispatcher.py`](https://github.com/guling-pro/guling-trader/blob/main/src/trader/dispatcher.py) (Tool dispatcher), [`src/trader/ths/win.py`](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/win.py) (THS automation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Desktop app"]
  node_main["App startup<br/>[main.py]"]
  node_window["Main window<br/>[main_window.py]"]
  node_tray["System tray<br/>[tray.py]"]
  node_config[("Local config<br/>[config.py]")]
  node_brand["Brand/version<br/>[brand.py]"]
end

subgraph group_protocol["Pairing and RPC"]
  node_ws["WebSocket client<br/>[ws_client.py]"]
  node_handshake["Pairing handshake<br/>[handshake.py]"]
  node_dispatcher["Tool dispatcher<br/>[dispatcher.py]"]
  node_contract["Reply contract<br/>[contract.py]"]
end

subgraph group_trading["Trading runtime"]
  node_win["THS automation<br/>[win.py]"]
  node_dialogs["Dialog sentry<br/>[dialogs.py]"]
  node_rows["Row normalization<br/>[rows.py]"]
  node_ledger[("Order ledger<br/>[order_ledger.py]")]
  node_order_watch["Order watcher<br/>[order_watch.py]"]
  node_watchlist_watch["Watchlist watcher<br/>[watchlist_watch.py]"]
  node_ths_const["THS constants<br/>[const.py]"]
end

subgraph group_setup["Setup and updates"]
  node_installer["THS installer<br/>[auto_install.py]"]
  node_detect["THS detection<br/>[detect.py]"]
  node_download["Download helper<br/>[download.py]"]
  node_tesseract["OCR installer<br/>[tesseract.py]"]
  node_update_check["Update checker<br/>[check.py]"]
  node_update_apply["Update applier<br/>[apply.py]"]
end

node_user(("User"))
node_ai(("AI client"))
node_relay{{"MCP relay"}}
node_ths_client["同花顺 xiadan"]

node_user -->|"requests trades"| node_ai
node_ai -->|"sends MCP calls"| node_relay
node_relay -->|"relays frames"| node_ws
node_main -->|"starts client"| node_ws
node_ws -->|"performs handshake"| node_handshake
node_ws -->|"dispatches calls"| node_dispatcher
node_dispatcher -->|"builds replies"| node_contract
node_dispatcher -->|"invokes tools"| node_win
node_dispatcher -->|"records orders"| node_ledger
node_dispatcher -->|"uses normalization"| node_rows
node_win -->|"automates trades"| node_ths_client
node_win -->|"handles dialogs"| node_dialogs
node_win -->|"normalizes tables"| node_rows
node_win -->|"checks order records"| node_ledger
node_dialogs -->|"uses dialog rules"| node_ths_const
node_main -->|"starts watcher"| node_order_watch
node_main -->|"starts watcher"| node_watchlist_watch
node_order_watch -->|"sends order events"| node_ws
node_watchlist_watch -->|"sends watchlist events"| node_ws
node_main -->|"runs interface"| node_window
node_main -->|"starts tray"| node_tray
node_window -->|"reads settings"| node_config
node_tray -->|"controls connection"| node_ws
node_main -->|"loads settings"| node_config
node_main -->|"runs setup"| node_installer
node_installer -->|"checks installation"| node_detect
node_installer -->|"downloads installer"| node_download
node_main -->|"sets up OCR"| node_tesseract
node_main -->|"checks releases"| node_update_check
node_main -->|"applies updates"| node_update_apply
node_update_apply -->|"downloads update"| node_download
node_window -->|"shows version"| node_brand

click node_main "https://github.com/guling-pro/guling-trader/blob/main/src/trader/main.py"
click node_window "https://github.com/guling-pro/guling-trader/blob/main/src/trader/main_window.py"
click node_tray "https://github.com/guling-pro/guling-trader/blob/main/src/trader/tray.py"
click node_config "https://github.com/guling-pro/guling-trader/blob/main/src/trader/config.py"
click node_ws "https://github.com/guling-pro/guling-trader/blob/main/src/trader/ws_client.py"
click node_handshake "https://github.com/guling-pro/guling-trader/blob/main/src/trader/handshake.py"
click node_dispatcher "https://github.com/guling-pro/guling-trader/blob/main/src/trader/dispatcher.py"
click node_contract "https://github.com/guling-pro/guling-trader/blob/main/src/trader/contract.py"
click node_win "https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/win.py"
click node_dialogs "https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/dialogs.py"
click node_rows "https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/rows.py"
click node_ledger "https://github.com/guling-pro/guling-trader/blob/main/src/trader/order_ledger.py"
click node_order_watch "https://github.com/guling-pro/guling-trader/blob/main/src/trader/order_watch.py"
click node_watchlist_watch "https://github.com/guling-pro/guling-trader/blob/main/src/trader/watchlist_watch.py"
click node_ths_const "https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/const.py"
click node_installer "https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/auto_install.py"
click node_detect "https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/detect.py"
click node_download "https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/download.py"
click node_tesseract "https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/tesseract.py"
click node_update_check "https://github.com/guling-pro/guling-trader/blob/main/src/trader/selfupdate/check.py"
click node_update_apply "https://github.com/guling-pro/guling-trader/blob/main/src/trader/selfupdate/apply.py"
click node_brand "https://github.com/guling-pro/guling-trader/blob/main/src/trader/brand.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_window,node_tray,node_config,node_brand,node_user,node_ai toneBlue
class node_ws,node_handshake,node_dispatcher,node_contract toneAmber
class node_win,node_dialogs,node_rows,node_ledger,node_order_watch,node_watchlist_watch,node_ths_const toneMint
class node_installer,node_detect,node_download,node_tesseract,node_update_check,node_update_apply toneRose
class node_relay,node_ths_client toneIndigo
```

## Components

### Desktop app

- [App startup](https://github.com/guling-pro/guling-trader/blob/main/src/trader/main.py): `src/trader/main.py`
- [Main window](https://github.com/guling-pro/guling-trader/blob/main/src/trader/main_window.py): `src/trader/main_window.py`
- [System tray](https://github.com/guling-pro/guling-trader/blob/main/src/trader/tray.py): `src/trader/tray.py`
- [Local config](https://github.com/guling-pro/guling-trader/blob/main/src/trader/config.py): `src/trader/config.py`
- [Brand/version](https://github.com/guling-pro/guling-trader/blob/main/src/trader/brand.py): `src/trader/brand.py`

### Pairing and RPC

- [WebSocket client](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ws_client.py): `src/trader/ws_client.py`
- [Pairing handshake](https://github.com/guling-pro/guling-trader/blob/main/src/trader/handshake.py): `src/trader/handshake.py`
- [Tool dispatcher](https://github.com/guling-pro/guling-trader/blob/main/src/trader/dispatcher.py): `src/trader/dispatcher.py`
- [Reply contract](https://github.com/guling-pro/guling-trader/blob/main/src/trader/contract.py): `src/trader/contract.py`

### Trading runtime

- [THS automation](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/win.py): `src/trader/ths/win.py`
- [Dialog sentry](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/dialogs.py): `src/trader/ths/dialogs.py`
- [Row normalization](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/rows.py): `src/trader/ths/rows.py`
- [Order ledger](https://github.com/guling-pro/guling-trader/blob/main/src/trader/order_ledger.py): `src/trader/order_ledger.py`
- [Order watcher](https://github.com/guling-pro/guling-trader/blob/main/src/trader/order_watch.py): `src/trader/order_watch.py`
- [Watchlist watcher](https://github.com/guling-pro/guling-trader/blob/main/src/trader/watchlist_watch.py): `src/trader/watchlist_watch.py`
- [THS constants](https://github.com/guling-pro/guling-trader/blob/main/src/trader/ths/const.py): `src/trader/ths/const.py`

### Setup and updates

- [THS installer](https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/auto_install.py): `src/trader/installer/auto_install.py`
- [THS detection](https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/detect.py): `src/trader/installer/detect.py`
- [Download helper](https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/download.py): `src/trader/installer/download.py`
- [OCR installer](https://github.com/guling-pro/guling-trader/blob/main/src/trader/installer/tesseract.py): `src/trader/installer/tesseract.py`
- [Update checker](https://github.com/guling-pro/guling-trader/blob/main/src/trader/selfupdate/check.py): `src/trader/selfupdate/check.py`
- [Update applier](https://github.com/guling-pro/guling-trader/blob/main/src/trader/selfupdate/apply.py): `src/trader/selfupdate/apply.py`

### Other components

- **User**
- **AI client**
- **MCP relay**
- **同花顺 xiadan**

## Connections

- User → AI client: requests trades (evidence: `README.md`)
- AI client → MCP relay: sends MCP calls (evidence: `README.md`)
- MCP relay → WebSocket client: relays frames (evidence: `README.md`)
- App startup → WebSocket client: starts client (evidence: `src/trader/main.py`)
- WebSocket client → Pairing handshake: performs handshake (evidence: `src/trader/ws_client.py`)
- WebSocket client → Tool dispatcher: dispatches calls (evidence: `src/trader/ws_client.py`)
- Tool dispatcher → Reply contract: builds replies (evidence: `src/trader/dispatcher.py`)
- Tool dispatcher → THS automation: invokes tools (evidence: `src/trader/dispatcher.py`)
- Tool dispatcher → Order ledger: records orders (evidence: `src/trader/dispatcher.py`)
- Tool dispatcher → Row normalization: uses normalization (evidence: `src/trader/dispatcher.py`)
- THS automation → 同花顺 xiadan: automates trades (evidence: `README.md`)
- THS automation → Dialog sentry: handles dialogs (evidence: `src/trader/ths/win.py`)
- THS automation → Row normalization: normalizes tables (evidence: `src/trader/ths/win.py`)
- THS automation → Order ledger: checks order records (evidence: `src/trader/ths/win.py`)
- Dialog sentry → THS constants: uses dialog rules (evidence: `src/trader/ths/dialogs.py`)
- App startup → Order watcher: starts watcher (evidence: `src/trader/main.py`)
- App startup → Watchlist watcher: starts watcher (evidence: `src/trader/main.py`)
- Order watcher → WebSocket client: sends order events (evidence: `src/trader/main.py`)
- Watchlist watcher → WebSocket client: sends watchlist events (evidence: `src/trader/main.py`)
- App startup → Main window: runs interface (evidence: `src/trader/main.py`)
- App startup → System tray: starts tray (evidence: `src/trader/main.py`)
- Main window → Local config: reads settings (evidence: `src/trader/main_window.py`)
- System tray → WebSocket client: controls connection (evidence: `src/trader/tray.py`)
- App startup → Local config: loads settings (evidence: `src/trader/main.py`)
- App startup → THS installer: runs setup (evidence: `src/trader/main.py`)
- THS installer → THS detection: checks installation (evidence: `src/trader/installer/auto_install.py`)
- THS installer → Download helper: downloads installer (evidence: `src/trader/installer/auto_install.py`)
- App startup → OCR installer: sets up OCR (evidence: `src/trader/main.py`)
- App startup → Update checker: checks releases (evidence: `src/trader/main.py`)
- App startup → Update applier: applies updates (evidence: `src/trader/main.py`)
- Update applier → Download helper: downloads update (evidence: `src/trader/selfupdate/apply.py`)
- Main window → Brand/version: shows version (evidence: `src/trader/main_window.py`)
