MiniQMT Quantitative Trading Skill
Task Objective
- This Skill is used to: connect to the MiniQMT client through the XtQuant library, obtain A-shares/futures/options market data, and execute quantitative trading operations
- Capabilities include:
- Market Data Module (xtdata): K-line data, tick data, real-time market data subscription, financial data, sector classification, ETF information, new stock subscription, trading calendar
- Trading Module (xttrader): stock/futures/options order placement, order cancellation, querying assets/orders/positions, fund transfer, margin trading, securities lending
- Trigger conditions: used when the user mentions miniqmt, xtquant, obtaining market data, quantitative trading, or placing trading orders
Differences from qmt-strategy: This skill uses the xtquant libraryinexternal Python programsto connect to the MiniQMT client (xtdata market data / xttrader trading); if you need to write within the QMT clientbuilt-in strategies(ContextInfo/passorder、#coding:gbk), please use the qmt-strategy skill.
Prerequisites
MiniQMT Environment Requirements
- Client Installation: Requires installation of the XunTou ultra-fast trading terminal and startup of MiniQMT (supports simulation/live trading)
- Python Library:
pip install xtquant
- Path Configuration: Under the MiniQMT installation directory, the
userdata_mini path is used for xttrader connection
Directory Structure
QMT安装目录\
├── bin.x64\XtMiniQmt.exe # MiniQMT 主程序
├── userdata_mini\ # 用户数据目录(xttrader 连接路径)
│ ├── xqtrader.ini # 交易配置
│ └── xtdatacenter.ini # 行情配置
Core Concepts
Security Code Format
- Stocks: 6-digit number + market suffix, such as
600000.SH(Shanghai),000001.SZ(Shenzhen)
- Futures: product code + contract month, such as
rb2405.SF(rebar)
- Options: underlying code + exercise month, such as
510050.SH(SSE 50 ETF option)
Period Type (period)
| Period |
Description |
Period |
Description |
tick |
Tick data |
1q |
Quarterly line |
1m |
1-minute line |
1hy |
Semi-annual line |
5m |
5-minute line |
1y |
Annual line |
15m |
15-minute line |
1w |
Weekly line |
30m |
30-minute line |
1d |
Daily line |
1h |
1-hour line |
1mon |
Monthly line |
Adjustment Type (dividend_type)
none - No adjustment
front - Forward adjustment
back - Backward adjustment
front_ratio - Proportional forward adjustment
back_ratio - Proportional backward adjustment
Trading Market (market)
| Market |
Constant |
Market |
Constant |
| Shanghai |
xtconstant.SH_MARKET |
CFFEX |
xtconstant.MARKET_ENUM_INDEX_FUTURE |
| Shenzhen |
xtconstant.SZ_MARKET |
SHFE |
xtconstant.MARKET_ENUM_SHANGHAI_FUTURE |
| BSE |
xtconstant.MARKET_ENUM_BEIJING |
CZCE |
xtconstant.MARKET_ENUM_ZHENGZHOU_FUTURE |
| Dalian Commodity Exchange (DCE) |
xtconstant.MARKET_ENUM_DALIANG_FUTURE |
Guangzhou Futures Exchange (GFEX) |
xtconstant.MARKET_ENUM_GUANGZHOU_FUTURE |
Account Type (account_type)
| Type |
Constant |
Type |
Constant |
| Stock |
xtconstant.SECURITY_ACCOUNT |
Shanghai-Hong Kong Stock Connect |
xtconstant.HUGANGTONG_ACCOUNT |
| Futures |
xtconstant.FUTURE_ACCOUNT |
Shenzhen-Hong Kong Stock Connect |
xtconstant.SHENGANGTONG_ACCOUNT |
| Credit |
xtconstant.CREDIT_ACCOUNT |
Futures Options |
xtconstant.FUTURE_OPTION_ACCOUNT |
| Stock Options |
xtconstant.STOCK_OPTION_ACCOUNT |
- |
- |
Operation Steps
- Determine Requirements — Identify whether it is market data retrieval or trading operation
- Select Module — xtdata for market data, xttrader for trading
- Call Script — Select the corresponding script based on the data type
- Parse Results — The agent analyzes the returned JSON-format data
Intent Recognition Mapping Example
| User Query |
Corresponding Function |
Invocation Method |
| "Guizhou Moutai real-time stock price" |
Real-time market snapshot |
xtdata.get_full_tick |
| "Ping An Bank K-line data" |
K-line data |
xtdata.get_market_data |
| "China Merchants Bank financial indicators" |
Financial statements |
xtdata.get_financial_data |
| "Semiconductor sector constituent stocks" |
Sector constituent stocks |
xtdata.get_stock_list_in_sector |
| "Today's convertible bond information" |
ETF/convertible bond data |
xtdata.get_cb_info |
| "New stock subscription" |
New stock information |
xtdata.get_ipo_info |
| "Place order to buy Ping An Bank" |
Trade order placement |
xttrader.order_stock |
| "Query positions" |
Position query |
xttrader.query_stock_positions |
| "Cancel order" |
Cancel order operation |
xttrader.cancel_order_stock |
Usage Examples
Example 1: Get real-time stock market data
import xtdata
# 获取全推行情快照
ticks = xtdata.get_full_tick(['600519.SH', '000001.SZ'])
# 订阅单股实时行情
def on_data(datas):
for code in datas:
print(code, datas[code])
xtdata.subscribe_quote('600519.SH', period='tick', callback=on_data)
xtdata.run()
Example 2: Get K-line historical data
import xtdata
# 下载历史K线数据
xtdata.download_history_data2(['600519.SH'], period='1d', start_time='')
# 获取K线数据
data = xtdata.get_market_data(
field_list=['open', 'high', 'low', 'close', 'volume'],
stock_list=['600519.SH'],
period='1d',
start_time='20240101',
end_time='',
count=100,
dividend_type='front'
)
Example 3: Place trade order
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xttype import StockAccount
from xtquant import xtconstant
# 配置路径和会话
path = 'D:\\迅投极速交易终端\\userdata_mini'
session_id = 123456
xt_trader = XtQuantTrader(path, session_id)
# 创建账号对象
acc = StockAccount('1000000365') # 替换为实际账号
# 连接交易
xt_trader.start()
connect_result = xt_trader.connect()
subscribe_result = xt_trader.subscribe(acc)
# 下单买入
order_id = xt_trader.order_stock(
acc,
'600519.SH',
xtconstant.STOCK_BUY,
100, # 100股
xtconstant.FIX_PRICE,
1800.0, # 价格
'strategy1',
'remark'
)
# 查询资产
asset = xt_trader.query_stock_asset(acc)
print(f"可用资金: {asset.cash}")
Example 4: Subscribe to market data and process in real time
import xtdata
def on_tick_data(datas):
for code in datas:
tick = datas[code]
print(f"{code}: 现价={tick['lastPrice']}, 成交量={tick['volume']}")
# 订阅多只股票
xtdata.subscribe_whole_quote(['SH', 'SZ'], callback=on_tick_data)
xtdata.run()
Resource Index
Market Data Scripts
- Market snapshot:
python scripts/market_data.py snapshot --code 600519.SH
- K-line data:
python scripts/market_data.py kline --code 600519.SH --period 1d --count 100
- Tick data:
python scripts/market_data.py tick --code 600519.SH --count 100
- Real-time market data:
python scripts/market_data.py full_tick --codes 600519.SH,000001.SZ
Sector and Financial Scripts
- Sector list:
python scripts/sector_data.py sector_list
- Sector constituent stocks:
python scripts/sector_data.py sector_stocks --sector 半导体
- Financial data:
python scripts/financial_data.py financial --code 600519.SH --tables Balance,Income
Trading Scripts
- Order placement:
python scripts/trade.py order --code 600519.SH --type buy --volume 100 --price 1800.0
- Cancel order:
python scripts/trade.py cancel --order_id 12345
- Query positions:
python scripts/trade.py positions
- Query orders:
python scripts/trade.py orders
- Query assets:
python scripts/trade.py asset
- Query trades:
python scripts/trade.py trades
Reference Documentation
Notes
Environment Requirements
- MiniQMT must be running: the xttrader trading module requires the MiniQMT client to be running in the background
- Path Configuration: ensure the
userdata_mini path is correct, otherwise the connection will fail
- Market Data Subscription Limits: for single-stock subscriptions, it is recommended not to exceed 50; when there are many, it is recommended to use full push data
Data Retrieval Notes
- Data Supplementary Download: historical data must first be downloaded via
download_history_data2 download
- Permission Limits: Level2 data requires the terminal to have the corresponding permissions
- Time Format: the K-line time parameter format is
'20240101' or '20240101000000'
Trading Notes
- Account Format: for stock accounts, use the fund account string directly; for futures accounts, specify
'FUTURE'
- Order Quantity: stocks are in "shares", bonds are in "units", and futures are in "lots"
- Order Number: after a successful order, a positive integer order_id is returned; -1 indicates failure
- Asynchronous Operations: trading operations support both synchronous and asynchronous modes; asynchronous mode requires callbacks
Performance Optimization
- Batch Requests: for multiple stocks' data, it is recommended to use
get_market_data for batch retrieval
- Cache Utilization: subscribed data is automatically cached, so there is no need to subscribe repeatedly
- Thread Safety: xttrader supports multiple strategies, but different session_ids must be used