QMT Broker & Store
Relevant source files
The following files were used as context for generating this wiki page:
The QMT Broker & Store module provides an integration layer between the Backtrader framework and the Quant Management Terminal (QMT) trading system. It enables automated trading and real-time data ingestion specifically tailored for the Chinese financial markets using the xtquant library.
This system is designed as a singleton-based architecture where a central Store manages the connection to the QMT terminal (Mini QMT), while specialized Broker and Feed classes handle order execution and market data synchronization respectively.
Sources: qmtbt/qmtbroker.py, qmtbt/qmtfeed.py
Core Architecture
The architecture follows the standard Backtrader pattern for external service integration. The QMTStore (referenced as the orchestrator) manages the lifecycle of the connection to the QMT client path.
Component Relationships
The following diagram illustrates how the QMT-specific components interact with the xtquant backend and the standard Backtrader base classes.
The QMTBroker utilizes the XtQuantTrader interface to perform live trading operations, while the QMTFeed manages data buffering and loading into Backtrader's line-based system.
Sources: qmtbt/qmtbroker.py:46-56, qmtbt/qmtfeed.py:34-40
Broker Implementation
The QMTBroker manages order execution, position tracking, and account assets by interfacing with the Mini QMT terminal via a local path.
Order Lifecycle
When a strategy issues a buy or sell command, the broker translates Backtrader parameters into a QMT-compatible dictionary and submits it through xt_trader.order.
| Parameter | Type | Description |
|---|---|---|
stock_code | String | Format typically '600000.SH' or '000001.SZ' |
order_type | Integer | Mapped to xttype.LIMIT_ORDER or MARKET_ORDER |
order_volume | Integer | Quantity of shares (must be integer for QMT) |
price | Float | Price for limit orders; 0 for market orders |
side | Integer | Mapped to xttype.SIDE_BUY or SIDE_SELL |
Sources: qmtbt/qmtbroker.py:165-177, qmtbt/qmtbroker.py:202-214
Order Status Polling
The broker implements a next() method that iterates through active orders and queries their status from the QMT backend.
The status check ensures that the Backtrader strategy is notified immediately when an order transitions to a terminal state.
Sources: qmtbt/qmtbroker.py:145-163
Data Feed and Market Connectivity
The QMTFeed class handles both historical data backfilling and live data streaming. It maps QMT's specific data fields to Backtrader lines.
Supported Data Lines
The feed supports an extended set of lines beyond the standard Open, High, Low, Close, and Volume:
lastClose,amount,pvolumeaskPrice1throughaskPrice5bidPrice1throughbidPrice5askVol1throughaskVol5bidVol1throughbidVol5openInterest,preClose,pe
Sources: qmtbt/qmtfeed.py:41-76
Data Loading Process
The feed maps timeframe requests to QMT's string-based periods (e.g., bt.TimeFrame.Days maps to "1d").
| Backtrader Timeframe | QMT Period String |
|---|---|
bt.TimeFrame.Days | "1d" |
bt.TimeFrame.Minutes | "1m" |
bt.TimeFrame.Ticks | "tick" |
Sources: qmtbt/qmtfeed.py:101-105
Historical data is fetched as a Pandas DataFrame and converted to records, while live data uses an asynchronous callback (on_data) to update the feed's internal queue.
Sources: qmtbt/qmtfeed.py:180-210, xtquant/xtdata.py:596-620
QMT Strategy Framework
The underlying xtquant tools provide a ContextInfo object and a StrategyLoader that manage the execution environment for strategies running within the QMT terminal.
Context and Backtest Parameters
The ContextInfo class defines the operational parameters for backtesting and live simulation.
| Property | Default | Description |
|---|---|---|
asset | 1,000,000.0 | Initial capital for backtesting |
margin_ratio | 0.05 | Margin requirement ratio |
slippage_type | 2 | Type of slippage simulation |
benchmark | "000300.SH" | Performance benchmark code |
Sources: xtquant/qmttools/contextinfo.py:48-65
Strategy Lifecycle Management
The StrategyLoader (in stgframe.py) initializes the environment by registering formulas and setting up views. It handles the run_bar logic which iterates through the timelist and executes the handlebar method for every new price bar.
Sources: xtquant/qmttools/stgframe.py:18-35, xtquant/qmttools/stgframe.py:175-200
Conclusion
The QMT Broker & Store integration provides a robust framework for connecting Backtrader to the Chinese market ecosystem. By leveraging the singleton QMTStore to manage connections and the specialized QMTBroker and QMTFeed for execution and data, developers can transition from backtesting to live trading in the QMT environment with minimal code changes. The system effectively bridges Backtrader's object-oriented order management with QMT's procedural API.