-
Notifications
You must be signed in to change notification settings - Fork 724
Main GUI Application
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
This document describes the main GUI application of Hikyuu, focusing on the Qt-based desktop interface that orchestrates data import, real-time quote collection, and configuration persistence. It explains the main window layout, navigation structure, and core interaction patterns, and demonstrates how the GUI integrates with the underlying Python/C++ core for real-time data display and system control. The document also covers event handling, component initialization, state management, and the relationship between the main application window and backend components such as StockManager and TradeManager. Practical troubleshooting guidance is provided for common UI freezing and display inconsistencies, along with customization options for themes and layout adjustments.
The GUI is organized around a Qt main window with tabbed navigation and a set of worker threads for long-running tasks. The main window is generated from a UI definition and extended with Python logic to manage configuration, threading, and UI updates.
graph TB
subgraph "GUI Layer"
MW["MyMainWindow<br/>HikyuuTDX.py"]
UI["Ui_MainWindow<br/>MainWindow.py"]
Threads["Worker Threads<br/>CollectSpotThread.py<br/>SchedImportThread.py<br/>UseTdxImportToH5Thread.py<br/>UsePytdxImportToH5Thread.py"]
end
subgraph "Backend Layer"
PyCore["Python Core<br/>hikyuu.core"]
CppCore["C++ Core<br/>hikyuu_cpp"]
TradeMgrWrap["_TradeManager.cpp<br/>pybind11 wrapper"]
TradeMgrCpp["TradeManager.cpp<br/>C++ implementation"]
BrokerMgr["BrokerTradeManager.cpp<br/>C++ implementation"]
end
MW --> UI
MW --> Threads
Threads --> PyCore
Threads --> CppCore
MW --> TradeMgrWrap
TradeMgrWrap --> TradeMgrCpp
TradeMgrCpp --> BrokerMgr
Diagram sources
- HikyuuTDX.py
- MainWindow.py
- CollectSpotThread.py
- SchedImportThread.py
- UseTdxImportToH5Thread.py
- UsePytdxImportToH5Thread.py
- _TradeManager.cpp
- TradeManager.cpp
- BrokerTradeManager.cpp
Section sources
- HikyuuTDX.py
- MainWindow.py
- MyMainWindow (HikyuuTDX.py): The main Qt window class that initializes UI, configuration, logging, and threads; handles user actions and signals from worker threads; persists configuration to disk; manages real-time quote collection and scheduled imports.
- Ui_MainWindow (MainWindow.py): Generated UI definition providing the tabbed layout and widgets (tabs for data source selection, database configuration, import controls, log output).
- Worker Threads:
- CollectSpotThread: Real-time quote collection loop using external quote server configuration.
- SchedImportThread: Scheduled import trigger using wait conditions and mutexes.
- UseTdxImportToH5Thread and UsePytdxImportToH5Thread: Multi-process import pipelines for historical data to HDF5/MySQL/ClickHouse, emitting progress messages.
- Backend Integration:
- TradeManager pybind11 wrapper and C++ implementation for trading system control and order execution.
- BrokerTradeManager for broker-side order handling and cash management.
Section sources
- HikyuuTDX.py
- MainWindow.py
- CollectSpotThread.py
- SchedImportThread.py
- UseTdxImportToH5Thread.py
- UsePytdxImportToH5Thread.py
- _TradeManager.cpp
- TradeManager.cpp
- BrokerTradeManager.cpp
The GUI follows a Qt event-driven model with asynchronous worker threads. The main window coordinates:
- UI initialization and widget wiring (tabbed layout, buttons, checkboxes, date editors, progress bars, log text).
- Configuration loading/saving to user config files and backend hikyuu.ini.
- Thread lifecycle management for import and real-time collection.
- Signal-slot connections to update UI state and logs.
- Backend integration for trading operations via TradeManager.
sequenceDiagram
participant User as "User"
participant MW as "MyMainWindow"
participant Threads as "Worker Threads"
participant Backend as "TradeManager/Backend"
User->>MW : Click "Start Import"
MW->>MW : saveConfig()
MW->>Threads : Start UseTdxImportToH5Thread or UsePytdxImportToH5Thread
Threads-->>MW : message (progress, info, finished)
MW->>MW : Update progress bars, log text, status label
User->>MW : Click "Start Collection"
MW->>Threads : Start CollectSpotThread
Threads-->>MW : periodic updates (via log)
User->>MW : Click "Start Scheduled Import"
MW->>Threads : Start SchedImportThread
Threads-->>MW : message -> trigger "Start Import"
User->>MW : Control Trading (buy/sell)
MW->>Backend : TradeManager buy/sell/checkin/checkout
Backend-->>MW : TradeRecord/status
MW->>MW : Update UI state and logs
Diagram sources
- HikyuuTDX.py
- UseTdxImportToH5Thread.py
- UsePytdxImportToH5Thread.py
- CollectSpotThread.py
- SchedImportThread.py
- _TradeManager.cpp
- Tabbed structure:
- Data Source Selection: Choose between TDX, Pytdx, and QMT; configure directories and number of concurrent servers.
- Database Configuration: Enable/disable HDF5, MySQL, ClickHouse; set connection parameters and temporary directories.
- Import Controls: Start import, scheduled import, progress indicators, and detailed log output.
- Preload Settings: Configure preloading of various K-line types and maximum counts.
- Centralized UI wiring is generated from the UI file and extended in the main window class.
classDiagram
class MyMainWindow {
+initUI()
+initLogger()
+initThreads()
+saveConfig()
+getCurrentConfig()
+on_* slots...
+closeEvent(event)
}
class Ui_MainWindow {
+setupUi(MainWindow)
+retranslateUi(MainWindow)
}
class MainWindow {
+QMainWindow
}
MyMainWindow --> Ui_MainWindow : "inherits from"
MainWindow <|-- MyMainWindow : "extends"
Diagram sources
- HikyuuTDX.py
- MainWindow.py
Section sources
- MainWindow.py
- HikyuuTDX.py
- Signals and Slots:
- Worker threads emit messages to the main window via Qt signals.
- Main window connects to these signals to update progress bars, status labels, and log text.
- Buttons trigger slots that save configuration, validate directories, and start threads.
- State flags:
- import_running, _is_sched_import_running, _is_collect_running track active operations.
- UI toggles enable/disable buttons and update status labels accordingly.
- Logging:
- Output redirection to a rich-text log widget with colored severity tags.
- Multi-process logging via QueueListener.
sequenceDiagram
participant MW as "MyMainWindow"
participant Import as "UseTdxImportToH5Thread"
participant Log as "QueueListener"
MW->>Import : start()
Import-->>MW : message(["HDF5_IMPORT","INFO", "..."])
MW->>MW : append to log_textEdit
Import-->>MW : message(["HDF5_IMPORT","THREAD","FINISHED"])
MW->>MW : reset UI, enable buttons, show completion
Import-->>Log : emit log records (multiprocess)
Diagram sources
- HikyuuTDX.py
- UseTdxImportToH5Thread.py
- HikyuuTDX.py
Section sources
- HikyuuTDX.py
- HikyuuTDX.py
- UseTdxImportToH5Thread.py
- CollectSpotThread runs a continuous loop to collect real-time quotes using the configured quote server address and collection parameters.
- The main window starts/stops the thread and reflects status in the UI.
sequenceDiagram
participant MW as "MyMainWindow"
participant Coll as "CollectSpotThread"
MW->>Coll : start()
Coll->>Coll : connect to quote server
Coll-->>MW : periodic log updates
MW->>Coll : terminate() on stop
Diagram sources
- HikyuuTDX.py
- CollectSpotThread.py
Section sources
- HikyuuTDX.py
- CollectSpotThread.py
- SchedImportThread calculates the next import time and emits a signal to trigger import when due.
- The main window connects this signal to the import start slot.
flowchart TD
Start([Start SchedImportThread]) --> Calc["Calculate next import time"]
Calc --> Wait["Wait until next time (QWaitCondition)"]
Wait --> |Trigger| Emit["Emit message signal"]
Emit --> MW["MyMainWindow on_start_import_pushButton_clicked"]
MW --> Import["Start UseTdx/UsePytdx Import Thread"]
Import --> End([Done])
Diagram sources
- SchedImportThread.py
- HikyuuTDX.py
Section sources
- SchedImportThread.py
- HikyuuTDX.py
- UseTdxImportToH5Thread and UsePytdxImportToH5Thread coordinate multi-process tasks for importing K-lines, weights, finance, block info, and bond data.
- They compute progress per market/ktype and aggregate totals to update progress bars and log details.
flowchart TD
Init([Init Tasks]) --> Hosts["Resolve best TDX hosts"]
Hosts --> CreateDB["Create target DB (SQLite/MySQL/ClickHouse)"]
CreateDB --> Names["Import index/stock names"]
Names --> Spawn["Spawn worker processes per task"]
Spawn --> Loop{"Receive progress messages"}
Loop --> |KDATA| UpdateK["Aggregate progress per ktype"]
Loop --> |TRANS/TIME| UpdateT["Aggregate progress per ktype"]
Loop --> |WEIGHT| UpdateW["Report weight/finance counts"]
Loop --> |FINANCE| UpdateF["Report finance progress"]
UpdateK --> Loop
UpdateT --> Loop
UpdateW --> Loop
UpdateF --> Loop
Loop --> Done([Tasks finished])
Diagram sources
- UseTdxImportToH5Thread.py
- UsePytdxImportToH5Thread.py
Section sources
- UseTdxImportToH5Thread.py
- UsePytdxImportToH5Thread.py
- The GUI interacts with TradeManager via pybind11 wrappers to place buy/sell orders, check cash positions, and manage stock holdings.
- The C++ TradeManager implementation and BrokerTradeManager handle order execution and cash accounting.
classDiagram
class TradeManagerBase {
+first_datetime()
+last_datetime()
+current_cash()
+cash(datetime, ktype)
+have(stock)
+have_short(stock)
+get_stock_num()
+get_short_stock_num()
+get_hold_num(datetime, stock)
+checkin_stock(datetime, stock, price, number)
+checkout_stock(datetime, stock, price, number)
+buy(datetime, stock, realPrice, number, stoploss, goalPrice, planPrice, from, remark)
+sell(datetime, stock, realPrice, number, stoploss, goalPrice, planPrice, from, remark)
}
class TradeManager {
+_saveAction(record)
+... (C++ implementation)
}
class BrokerTradeManager {
+checkin(datetime, cash)
+buy(...)
+sell(...)
+... (C++ implementation)
}
TradeManagerBase <|-- TradeManager : "extends"
TradeManagerBase <|-- BrokerTradeManager : "extends"
Diagram sources
- _TradeManager.cpp
- _TradeManager.cpp
- TradeManager.cpp
- BrokerTradeManager.cpp
Section sources
- _TradeManager.cpp
- _TradeManager.cpp
- TradeManager.cpp
- BrokerTradeManager.cpp
- Coupling:
- MyMainWindow depends on Ui_MainWindow for layout and on worker threads for background tasks.
- Worker threads depend on Python data modules and external libraries (pytdx, mysql, clickhouse).
- TradeManager integration relies on pybind11 bindings to C++ implementations.
- Cohesion:
- UI logic is centralized in MyMainWindow; worker threads encapsulate import and collection concerns.
- External dependencies:
- Qt (PySide6) for UI.
- MySQL and ClickHouse connectors for database targets.
- pytdx for historical data import.
- multiprocessing and threading for concurrency.
graph TB
MW["MyMainWindow"]
UI["Ui_MainWindow"]
Threads["Import/Collection Threads"]
PyCore["Python Data Modules"]
CppCore["C++ Core"]
TMWrap["_TradeManager.cpp"]
TMCPP["TradeManager.cpp"]
MW --> UI
MW --> Threads
Threads --> PyCore
Threads --> CppCore
MW --> TMWrap
TMWrap --> TMCPP
Diagram sources
- HikyuuTDX.py
- MainWindow.py
- UseTdxImportToH5Thread.py
- _TradeManager.cpp
Section sources
- HikyuuTDX.py
- UseTdxImportToH5Thread.py
- _TradeManager.cpp
- Asynchronous UI updates:
- Use signals to receive progress updates from worker threads and update UI without blocking the main thread.
- Call QApplication.processEvents() sparingly during long-running operations to keep the UI responsive.
- Threading best practices:
- Use QThread subclasses for long-running tasks; avoid heavy computation in the main thread.
- Use multiprocessing for CPU-intensive tasks (e.g., importing K-lines) to leverage multiple cores.
- Logging overhead:
- Limit log verbosity in production builds; use QueueListener for multiprocess logging to avoid UI stalls.
- Database operations:
- Batch operations and minimize round-trips when importing to MySQL/ClickHouse.
- Ensure proper indexing and connection pooling for large datasets.
[No sources needed since this section provides general guidance]
Common issues and resolutions:
- UI freezing during imports:
- Ensure worker threads emit signals frequently and the main window processes events.
- Avoid long-running operations on the main thread; delegate to threads/processes.
- Display inconsistencies:
- Verify that progress bars and log text are updated via signals; ensure the log widget’s maximum block count is set appropriately.
- Real-time collection errors:
- Confirm the quote server address is valid and reachable; adjust proxy settings if needed.
- Check network connectivity and firewall rules for the quote server port.
- Database connection failures:
- Test database connections using the built-in test buttons; verify credentials and network reachability.
- Scheduled import not triggering:
- Verify the scheduled time and timezone; ensure the scheduler thread is running and emitting signals.
- Trade execution issues:
- Validate TradeManager configuration and broker settings; confirm cash availability and order constraints.
Section sources
- HikyuuTDX.py
- HikyuuTDX.py
- CollectSpotThread.py
The Hikyuu GUI provides a robust, Qt-based interface for managing data imports, real-time quote collection, and trading system control. Its architecture cleanly separates UI logic from background tasks using signals and threads, enabling responsive interactions and reliable backend integration. By following the event handling patterns and threading best practices outlined here, developers can extend the GUI while maintaining stability and performance.
[No sources needed since this section summarizes without analyzing specific files]
- Themes and fonts:
- The application sets a default font on Windows if available; otherwise falls back to a generic match.
- Layout adjustments:
- The central widget uses a horizontal layout combining the tabbed interface and a log panel; resizing the window adjusts the tab and log areas proportionally.
Section sources
- HikyuuTDX.py
- MainWindow.py