miniqmt

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MiniQMT 量化交易技能

MiniQMT Quantitative Trading Skill

任务目标

Task Objectives

  • 本 Skill 用于:通过 XtQuant 库连接 MiniQMT 客户端,获取 A 股/期货/期权行情数据,执行量化交易操作
  • 能力包含:
    • 行情模块 (xtdata):K线数据、分笔数据、实时行情订阅、财务数据、板块分类、ETF信息、新股申购、交易日历
    • 交易模块 (xttrader):股票/期货/期权下单、撤单、查询资产/委托/持仓、资金划拨、信用交易、约券
  • 触发条件:用户提及 miniqmt、xtquant、迅投、获取行情、量化交易、下单交易时使用
  • This Skill is used to: connect to the MiniQMT client via the XtQuant library, obtain market data for A-shares, futures, and options, and execute quantitative trading operations
  • Capabilities include:
    • Market Module (xtdata): K-line data, tick data, real-time market subscription, financial data, sector classification, ETF information, IPO subscription, trading calendar
    • Trading Module (xttrader): Stock/futures/options order placement, order cancellation, asset/order/position query, fund transfer, margin trading, securities lending
  • Trigger conditions: Used when users mention miniqmt, xtquant, Xuntou, market data acquisition, quantitative trading, or order placement

前置准备

Preparations

MiniQMT 环境要求

MiniQMT Environment Requirements

  • 客户端安装:需安装迅投极速交易终端,并启动 MiniQMT(支持模拟/实盘)
  • Python 库
    pip install xtquant
  • 路径配置:MiniQMT 安装目录下的
    userdata_mini
    路径用于 xttrader 连接
  • Client Installation: Need to install Xuntou Extreme Speed Trading Terminal and start MiniQMT (supports simulation/live trading)
  • Python Library:
    pip install xtquant
  • Path Configuration: The
    userdata_mini
    path under the MiniQMT installation directory is used for xttrader connection

目录结构

Directory Structure

QMT安装目录\
├── bin.x64\XtMiniQmt.exe # MiniQMT 主程序
├── userdata_mini\        # 用户数据目录(xttrader 连接路径)
│   ├── xqtrader.ini      # 交易配置
│   └── xtdatacenter.ini  # 行情配置
QMT安装目录\
├── bin.x64\XtMiniQmt.exe # MiniQMT 主程序
├── userdata_mini\        # 用户数据目录(xttrader 连接路径)
│   ├── xqtrader.ini      # 交易配置
│   └── xtdatacenter.ini  # 行情配置

核心概念

Core Concepts

证券代码格式

Security Code Format

  • 股票:6位数字 + 市场后缀,如
    600000.SH
    (上海)、
    000001.SZ
    (深圳)
  • 期货:品种代码 + 合约月份,如
    rb2405.SF
    (螺纹钢)
  • 期权:标的代码 + 行权月份,如
    510050.SH
    (上证50ETF期权)
  • Stocks: 6-digit number + market suffix, e.g.,
    600000.SH
    (Shanghai),
    000001.SZ
    (Shenzhen)
  • Futures: Product code + contract month, e.g.,
    rb2405.SF
    (Rebar)
  • Options: Underlying code + exercise month, e.g.,
    510050.SH
    (SSE 50 ETF Option)

周期类型 (period)

Period Type (period)

周期说明周期说明
tick
分笔数据
1q
季度线
1m
1分钟线
1hy
半年线
5m
5分钟线
1y
年线
15m
15分钟线
1w
周线
30m
30分钟线
1d
日线
1h
1小时线
1mon
月线
PeriodDescriptionPeriodDescription
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

复权类型 (dividend_type)

Dividend Adjustment Type (dividend_type)

  • none
    - 不复权
  • front
    - 前复权
  • back
    - 后复权
  • front_ratio
    - 等比前复权
  • back_ratio
    - 等比后复权
  • none
    - No adjustment
  • front
    - Forward adjustment
  • back
    - Backward adjustment
  • front_ratio
    - Equal ratio forward adjustment
  • back_ratio
    - Equal ratio backward adjustment

交易市场 (market)

Trading Market (market)

市场常量市场常量
上海
xtconstant.SH_MARKET
中金所
xtconstant.MARKET_ENUM_INDEX_FUTURE
深圳
xtconstant.SZ_MARKET
上期所
xtconstant.MARKET_ENUM_SHANGHAI_FUTURE
北交所
xtconstant.MARKET_ENUM_BEIJING
郑商所
xtconstant.MARKET_ENUM_ZHENGZHOU_FUTURE
大商所
xtconstant.MARKET_ENUM_DALIANG_FUTURE
广期所
xtconstant.MARKET_ENUM_GUANGZHOU_FUTURE
MarketConstantMarketConstant
Shanghai
xtconstant.SH_MARKET
CFFEX
xtconstant.MARKET_ENUM_INDEX_FUTURE
Shenzhen
xtconstant.SZ_MARKET
SHFE
xtconstant.MARKET_ENUM_SHANGHAI_FUTURE
Beijing Stock Exchange
xtconstant.MARKET_ENUM_BEIJING
CZCE
xtconstant.MARKET_ENUM_ZHENGZHOU_FUTURE
DCE
xtconstant.MARKET_ENUM_DALIANG_FUTURE
GFEX
xtconstant.MARKET_ENUM_GUANGZHOU_FUTURE

账号类型 (account_type)

Account Type (account_type)

类型常量类型常量
股票
xtconstant.SECURITY_ACCOUNT
沪港通
xtconstant.HUGANGTONG_ACCOUNT
期货
xtconstant.FUTURE_ACCOUNT
深港通
xtconstant.SHENGANGTONG_ACCOUNT
信用
xtconstant.CREDIT_ACCOUNT
期货期权
xtconstant.FUTURE_OPTION_ACCOUNT
股票期权
xtconstant.STOCK_OPTION_ACCOUNT
--
TypeConstantTypeConstant
Stock
xtconstant.SECURITY_ACCOUNT
Shanghai-Hong Kong Stock Connect
xtconstant.HUGANGTONG_ACCOUNT
Futures
xtconstant.FUTURE_ACCOUNT
Shenzhen-Hong Kong Stock Connect
xtconstant.SHENGANGTONG_ACCOUNT
Margin
xtconstant.CREDIT_ACCOUNT
Futures Option
xtconstant.FUTURE_OPTION_ACCOUNT
Stock Option
xtconstant.STOCK_OPTION_ACCOUNT
--

操作步骤

Operation Steps

  1. 确定需求 — 识别是行情数据获取还是交易操作
  2. 选择模块 — xtdata 用于行情,xttrader 用于交易
  3. 调用脚本 — 根据数据类型选择对应脚本
  4. 解析结果 — 智能体分析返回的 JSON 格式数据
  1. Confirm Requirements — Identify whether it is market data acquisition or trading operation
  2. Select Module — Use xtdata for market data, xttrader for trading
  3. Call Script — Select the corresponding script based on data type
  4. Parse Results — The agent analyzes the returned JSON format data

意图识别映射示例

Intent Recognition Mapping Examples

用户提问对应功能调用方式
"贵州茅台实时股价"实时行情快照xtdata.get_full_tick
"平安银行K线数据"K线数据xtdata.get_market_data
"招商银行财务指标"财务报表xtdata.get_financial_data
"半导体板块成分股"板块成分股xtdata.get_stock_list_in_sector
"今日可转债信息"ETF/可转债数据xtdata.get_cb_info
"新股申购"新股信息xtdata.get_ipo_info
"下单买入平安银行"交易下单xttrader.order_stock
"查询持仓"持仓查询xttrader.query_stock_positions
"撤单"撤单操作xttrader.cancel_order_stock
User QueryCorresponding FunctionCalling Method
"贵州茅台实时股价"Real-time market snapshotxtdata.get_full_tick
"平安银行K线数据"K-line dataxtdata.get_market_data
"招商银行财务指标"Financial statementsxtdata.get_financial_data
"半导体板块成分股"Sector constituent stocksxtdata.get_stock_list_in_sector
"今日可转债信息"ETF/convertible bond dataxtdata.get_cb_info
"新股申购"IPO informationxtdata.get_ipo_info
"下单买入平安银行"Trading order placementxttrader.order_stock
"查询持仓"Position queryxttrader.query_stock_positions
"撤单"Order cancellationxttrader.cancel_order_stock

使用示例

Usage Examples

示例1:获取股票实时行情

Example 1: Get Real-time Stock Market Data

python
import xtdata
python
import xtdata

获取全推行情快照

获取全推行情快照

ticks = xtdata.get_full_tick(['600519.SH', '000001.SZ'])
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()
undefined
def on_data(datas): for code in datas: print(code, datas[code])
xtdata.subscribe_quote('600519.SH', period='tick', callback=on_data) xtdata.run()
undefined

示例2:获取K线历史数据

Example 2: Get Historical K-line Data

python
import xtdata
python
import xtdata

下载历史K线数据

下载历史K线数据

xtdata.download_history_data2(['600519.SH'], period='1d', start_time='')
xtdata.download_history_data2(['600519.SH'], period='1d', start_time='')

获取K线数据

获取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' )
undefined
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' )
undefined

示例3:交易下单

Example 3: Trading Order Placement

python
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xttype import StockAccount
from xtquant import xtconstant
python
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)
path = 'D:\迅投极速交易终端\userdata_mini' session_id = 123456 xt_trader = XtQuantTrader(path, session_id)

创建账号对象

创建账号对象

acc = StockAccount('1000000365') # 替换为实际账号
acc = StockAccount('1000000365') # 替换为实际账号

连接交易

连接交易

xt_trader.start() connect_result = xt_trader.connect() subscribe_result = xt_trader.subscribe(acc)
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' )
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}")
undefined
asset = xt_trader.query_stock_asset(acc) print(f"可用资金: {asset.cash}")
undefined

示例4:订阅行情并实时处理

Example 4: Subscribe to Market Data and Process in Real-time

python
import xtdata

def on_tick_data(datas):
    for code in datas:
        tick = datas[code]
        print(f"{code}: 现价={tick['lastPrice']}, 成交量={tick['volume']}")
python
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()
undefined
xtdata.subscribe_whole_quote(['SH', 'SZ'], callback=on_tick_data) xtdata.run()
undefined

资源索引

Resource Index

行情数据脚本

Market Data Scripts

  • 行情快照
    python scripts/market_data.py snapshot --code 600519.SH
  • K线数据
    python scripts/market_data.py kline --code 600519.SH --period 1d --count 100
  • 分笔数据
    python scripts/market_data.py tick --code 600519.SH --count 100
  • 实时行情
    python scripts/market_data.py full_tick --codes 600519.SH,000001.SZ
  • 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:
    python scripts/market_data.py full_tick --codes 600519.SH,000001.SZ

板块与财务脚本

Sector and Financial Scripts

  • 板块列表
    python scripts/sector_data.py sector_list
  • 板块成分股
    python scripts/sector_data.py sector_stocks --sector 半导体
  • 财务数据
    python scripts/financial_data.py financial --code 600519.SH --tables Balance,Income
  • 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

  • 下单
    python scripts/trade.py order --code 600519.SH --type buy --volume 100 --price 1800.0
  • 撤单
    python scripts/trade.py cancel --order_id 12345
  • 查询持仓
    python scripts/trade.py positions
  • 查询委托
    python scripts/trade.py orders
  • 查询资产
    python scripts/trade.py asset
  • 查询成交
    python scripts/trade.py trades
  • Order Placement:
    python scripts/trade.py order --code 600519.SH --type buy --volume 100 --price 1800.0
  • Order Cancellation:
    python scripts/trade.py cancel --order_id 12345
  • Position Query:
    python scripts/trade.py positions
  • Order Query:
    python scripts/trade.py orders
  • Asset Query:
    python scripts/trade.py asset
  • Trade Query:
    python scripts/trade.py trades

参考文档

Reference Documents

  • xtdata 行情模块 API(何时读取:需要查看行情数据接口时)
  • xtdata行情数据字段与数据字典 (何时读取:需要查看行情数据字段、数据字典时)
  • xttrader 交易模块 API(何时读取:需要查看交易接口、数据结构说明时)
  • 安装与下载指南(何时读取:首次安装 XtQuant 或遇到安装问题时)
  • 常见问题(何时读取:遇到常见问题时)
  • 代码示例(何时读取:需要参考完整代码示例时)
  • 更新日志(何时读取:查看版本更新历史时)
  • xtdata Market Module API (When to read: Need to check market data interfaces)
  • xtdata Market Data Fields and Data Dictionary (When to read: Need to check market data fields and data dictionary)
  • xttrader Trading Module API (When to read: Need to check trading interfaces and data structure descriptions)
  • Installation and Download Guide (When to read: First-time installation of XtQuant or encountering installation issues)
  • FAQs (When to read: Encountering common issues)
  • Code Examples (When to read: Need to refer to complete code examples)
  • Changelog (When to read: Check version update history)

注意事项

Notes

环境要求

Environment Requirements

  • 必须运行 MiniQMT:xttrader 交易模块需要 MiniQMT 客户端在后台运行
  • 路径配置:确保
    userdata_mini
    路径正确,否则连接会失败
  • 行情订阅限制:单股订阅建议不超过 50 只,较多时建议使用全推数据
  • Must Run MiniQMT: The xttrader trading module requires the MiniQMT client to run in the background
  • Path Configuration: Ensure the
    userdata_mini
    path is correct, otherwise the connection will fail
  • Market Subscription Limits: It is recommended not to subscribe to more than 50 individual stocks; use full push data when subscribing to more

数据获取注意

Data Acquisition Notes

  • 数据补下载:历史数据需先通过
    download_history_data2
    下载
  • 权限限制:Level2 数据需要终端有相应权限
  • 时间格式:K线时间参数格式为
    '20240101'
    '20240101000000'
  • Data Supplementary Download: Historical data must be downloaded first via
    download_history_data2
  • Permission Restrictions: Level2 data requires corresponding terminal permissions
  • Time Format: K-line time parameters are in the format
    '20240101'
    or
    '20240101000000'

交易注意

Trading Notes

  • 账号格式:股票账号直接使用资金账号字符串,期货账号需指定
    'FUTURE'
  • 委托数量:股票以"股"为单位,债券以"张"为单位,期货以"手"为单位
  • 订单编号:下单成功后返回正整数 order_id,-1 表示失败
  • 异步操作:交易操作支持同步/异步两种模式,异步模式需配合回调使用
  • Account Format: Use the capital account string directly for stock accounts; specify
    'FUTURE'
    for futures accounts
  • Order Quantity: Stocks are in "shares", bonds in "sheets", futures in "lots"
  • Order ID: A positive integer order_id is returned upon successful order placement; -1 indicates failure
  • Asynchronous Operations: Trading operations support both synchronous and asynchronous modes; asynchronous mode requires matching callbacks

性能优化

Performance Optimization

  • 批量请求:多只股票数据建议使用
    get_market_data
    批量获取
  • 缓存利用:已订阅的数据会自动缓存,无需重复订阅
  • 线程安全:xttrader 支持多策略,但需使用不同的 session_id
  • Batch Requests: It is recommended to use
    get_market_data
    to obtain data for multiple stocks in batches
  • Cache Utilization: Subscribed data is automatically cached, no need to subscribe repeatedly
  • Thread Safety: xttrader supports multiple strategies, but different session_ids must be used