XtQuant / MiniQMT Integration
Relevant source files
The following files were used as context for generating this wiki page:
The XtQuant / MiniQMT integration provides a Python-based interface for interacting with the QMT (Quantitative Master Trader) platform, specifically supporting "MiniQMT" or Research versions. This module enables automated trading, real-time market data subscription, and historical data retrieval through a socket-based client-server architecture.
The integration is primarily used to bridge the gap between high-level algorithmic strategies (such as those in Backtesting) and the execution layer of domestic Chinese markets. It facilitates a modular workflow where agents can observe streaming data, make structured decisions, and execute trades via a dedicated trading service.
Sources: xtquant/xtdata.py:1-40, xtquant/qmttools/stgframe.py:1-20
Architecture and Connection
The integration relies on a client-server model where the Python environment (Client) connects to a running QMT instance (Server). The server exposes various ports for different services, including trading, quoting, and risk control.
Default Service Ports
Configuration for these services is defined in the environment configuration, primarily targeting local loopback addresses for security and performance.
| Service Name | Default Address | Description |
|---|---|---|
xtdaemon | 127.0.0.1:55000 | Name service and daemon control |
xtservice | 127.0.0.1:56000 | Core logic service |
xttraderservice | 127.0.0.1:57000 | Execution and order routing |
xtquoter | 127.0.0.1:59000 | Market data distribution |
Sources: xtquant/config/env.lua:10-25
Connection Logic
The xtdata module manages connections by scanning available server addresses. By default, it attempts to connect to 127.0.0.1:58610. Once connected, it retrieves data and application directories from the server to synchronize local file paths.
Sources: xtquant/xtdata.py:81-125
Market Data Management (xtdata)
The xtdata module provides a comprehensive API for subscribing to and downloading market data. It supports various granularities, from "tick" data to "yearly" K-lines.
Key Data Functions
subscribe_quote: Subscribes to real-time market updates for a specific instrument and period.download_history_data: Pulls historical data to local storage for backtesting or analysis.get_market_data: Retrieves local or server-cached data aspandas.DataFrameornumpy.ndarray.
K-Line Periods Supported
The system maps millisecond durations to human-readable strings for consistency.
| Code | Period Name | Milliseconds |
|---|---|---|
| 0 | tick | N/A |
| 60000 | 1m | One Minute |
| 300000 | 5m | Five Minutes |
| 86400000 | 1d | Daily |
Sources: xtquant/xtdata.py:530-580, xtquant/qmttools/stgframe.py:45-65
Data Flow for Subscriptions
Sources: xtquant/xtdata.py:750-785
Strategy Framework (qmttools)
The integration includes a strategy framework designed to mimic the internal QMT execution environment, providing classes like ContextInfo and StrategyLoader.
Strategy Execution Flow
The StrategyLoader initializes the environment, fixes parameters (such as stock codes and timeframes), and manages the main loop for both historical and real-time modes.
- Initialization: Sets up
request_id,quote_mode(realtime/history), andtrade_mode(backtest/trading). - History Loading: If requested, it loads the main historical sequence via
load_main_history. - Bar Loop: Iterates through the time list, calling
handlebar()for each candle. - Real-time Transition: Switches to
subscribe_quoteto receive new data and extend the bar loop live.
Sources: xtquant/qmttools/stgframe.py:25-100
ContextInfo Component
The ContextInfo object acts as the primary data container within a strategy, tracking assets, positions, and order states.
| Property | Type | Description |
|---|---|---|
asset | float | Initial capital (default 1,000,000) |
barpos | int | Current K-line index in the timelist |
slippage | float | Execution slippage value |
benchmark | str | Performance benchmark (default 000300.SH) |
Sources: xtquant/qmttools/contextinfo.py:15-55
Trading Operations (xttrader)
While xtdata handles information, the trading service handles execution. Commands are sent via passorder implementation.
Order Execution Sequence
Configuration Helpers
The integration uses Lua-based configuration helpers to manage complex entity relationships (Brokers, Platforms, Banks). This allows the Python client to understand the capabilities of the specific QMT instance it is connected to, such as supported order types and batch limits.
Sources: xtquant/config/env.lua:140-160, xtquant/config/configHelper.lua:1-50
Conclusion
The XtQuant / MiniQMT integration provides a robust, asynchronous bridge to the QMT platform. By separating market data (xtdata) from strategy logic (qmttools) and execution (xttrader), it enables developers to build complex, multi-agent trading systems in Python while leveraging the specialized execution infrastructure of QMT.