# miniQMT使用教程 **Repository Path**: vivicai/quant0808 ## Basic Information - **Project Name**: miniQMT使用教程 - **Description**: miniQMT使用教程 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2026-01-04 - **Last Updated**: 2026-01-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # miniQMT使用教程 #### 简介 miniQMT和大QMT完全不一样,在使用之前需要搞清楚两者的区别,有助于快速上手开发。 miniQMT相当于提供了一个xtquant的Python库,两大核心模块xtdata和xttrade,前者主要用于获取数据,后者主要用于下单交易(可以实盘和模拟)。 通过这个库,可以实现数据获取、策略开发、下单交易全链路流程,有别于聚宽等平台,解决了不能实盘的问题。 本文主要讲解miniQMT常用的核心接口,帮助有Python编程能力,但是刚接触miniQMT的朋友,快速上手,miniQMT接口众多,没有讲到的还需要自己多尝试,强烈建议看一遍官方的API文档。 QMT账户开通、使用交流、联系微信:quant0808 [官网链接](https://dict.thinktrader.net/) #### 使用教程 1. 安装xtquant库 不能通过pip安装,需要到官网下载 [官网链接](https://dict.thinktrader.net/) 内置Python是大QMT的使用文档 xtquant是miniQMT使用文档 [xtquant下载,下载最新版就行](https://dict.thinktrader.net/nativeApi/download_xtquant.html) 下载好以后,解压到Python安装路径下的:Python\Lib\site-packages,如图 ![输入图片说明](1.png) 2. 登录miniQMT ![输入图片说明](2.png) **使用前必须安装xtquant,使用时必须登录miniQMT,才可以使用** ### 数据获取 数据获取分为多种方式 **1. 订阅+获取方式** 主要用于获取历史或实时行情数据 ``` #单股订阅 从服务器订阅数据,获取进入本地缓存,取消订阅或关闭程序后数据消失,不占用本地内存,无需下载 seq = xtdata.subscribe_quote(stock_code='000001.SZ',period='tick',start_time='',end_time='',count=-1) #1表示订阅成功 print('订阅号是:',seq) #订阅后,通过该接口获取数据,返回的是一个字典,key是股票代码,value是DF,index是时间,columns是各个字段 res=xtdata.get_market_data_ex(stock_list=['000001.SZ'],period='tick',start_time='',end_time='',count=-1) print('------tick数据-------') print(res['000001.SZ']) ``` 返回结果 近三天的tick数据,从集合竞价到收盘的数据 ``` 订阅号是: 1 ------tick数据------- time lastPrice open high low lastClose ... bidPrice askVol bidVol settlementPrice transactionNum pe 20250407091500 1743988500000 11.30 0.00 0.00 0.00 11.34 ... [11.3, 0.0, 0.0, 0.0, 0.0] [770, 0, 0, 0, 0] [770, 196, 0, 0, 0] 0.0 0 0.0 20250407091509 1743988509000 11.20 0.00 0.00 0.00 11.34 ... [11.200000000000001, 0.0, 0.0, 0.0, 0.0] [10267, 0, 0, 0, 0] [10267, 775, 0, 0, 0] 0.0 0 0.0 20250407091518 1743988518000 11.20 0.00 0.00 0.00 11.34 ... [11.200000000000001, 0.0, 0.0, 0.0, 0.0] [10309, 0, 0, 0, 0] [10309, 818, 0, 0, 0] 0.0 0 0.0 20250407091527 1743988527000 11.20 0.00 0.00 0.00 11.34 ... [11.200000000000001, 0.0, 0.0, 0.0, 0.0] [10314, 0, 0, 0, 0] [10314, 844, 0, 0, 0] 0.0 0 0.0 20250407091536 1743988536000 11.20 0.00 0.00 0.00 11.34 ... [11.200000000000001, 0.0, 0.0, 0.0, 0.0] [11157, 740, 0, 0, 0] [11157, 0, 0, 0, 0] 0.0 0 0.0 ... ... ... ... ... ... ... ... ... ... ... ... ... ... 20250409094427 1744163067000 10.75 10.73 10.79 10.71 10.82 ... [10.75, 10.74, 10.73, 10.72, 10.71] [462, 2443, 4805, 3465, 4198] [557, 1232, 2901, 4454, 7133] 0.0 11983 0.0 20250409094430 1744163070000 10.76 10.73 10.79 10.71 10.82 ... [10.75, 10.74, 10.73, 10.72, 10.71] [481, 2442, 5008, 3465, 4198] [601, 1239, 3001, 4454, 7133] 0.0 11987 0.0 20250409094433 1744163073000 10.76 10.73 10.79 10.71 10.82 ... [10.75, 10.74, 10.73, 10.72, 10.71] [454, 2442, 5008, 3465, 4198] [823, 1289, 3001, 4484, 7105] 0.0 12003 0.0 20250409094436 1744163076000 10.75 10.73 10.79 10.71 10.82 ... [10.75, 10.74, 10.73, 10.72, 10.71] [574, 2442, 5008, 3465, 4198] [714, 1289, 3523, 4496, 7105] 0.0 12019 0.0 20250409094439 1744163079000 10.75 10.73 10.79 10.71 10.82 ... [10.75, 10.74, 10.73, 10.72, 10.71] [423, 2452, 5053, 3465, 4198] [169, 1292, 3523, 4496, 7105] 0.0 12052 0.0 [10021 rows x 20 columns] ``` 订阅获取1分钟数据 只需要在订阅函数和获取函数中修改price的参数即可 ``` #单股订阅 从服务器订阅数据,获取进入本地缓存,取消订阅或关闭程序后数据消失,不占用本地内存,无需下载 seq = xtdata.subscribe_quote(stock_code='000001.SZ',period='1m',start_time='',end_time='',count=-1) #1表示订阅成功 print('订阅号是:',seq) #订阅后,通过该接口获取数据,返回的是一个字典,key是股票代码,value是DF,index是时间,columns是各个字段 res=xtdata.get_market_data_ex(stock_list=['000001.SZ'],period='1m',start_time='',end_time='',count=-1) print('------1分钟数据-------') print(res['000001.SZ']) ``` 返回结果 ``` 订阅号是: 1 ------1分钟数据------- time open high low close volume amount settelementPrice openInterest preClose suspendFlag 20240918093000 1726623000000 9.72 9.72 9.72 9.72 3777 3671253.00 0.0 14 9.70 0 20240918093100 1726623060000 9.73 9.76 9.73 9.75 9139 8900064.00 0.0 13 9.72 0 20240918093200 1726623120000 9.75 9.76 9.74 9.75 2112 2059085.00 0.0 13 9.75 0 20240918093300 1726623180000 9.75 9.75 9.73 9.73 4292 4179533.00 0.0 13 9.75 0 20240918093400 1726623240000 9.73 9.75 9.73 9.74 3623 3527409.00 0.0 13 9.73 0 ... ... ... ... ... ... ... ... ... ... ... ... 20250409094700 1744163220000 10.73 10.74 10.71 10.74 18460 19799413.00 0.0 13 10.73 0 20250409094800 1744163280000 10.74 10.74 10.72 10.73 10175 10915142.00 0.0 13 10.74 0 20250409094900 1744163340000 10.72 10.72 10.71 10.72 10368 11112996.40 0.0 13 10.73 0 20250409095000 1744163400000 10.73 10.73 10.70 10.71 13085 14015218.08 0.0 13 10.72 0 20250409095100 1744163460000 10.72 10.72 10.69 10.70 12175 13029304.63 0.0 13 10.71 0 [31834 rows x 11 columns] ``` **2、获取最新的行情数据** xtdata.get_full_tick(),每次调用返回一个最新的tick数据,最小间隔是3秒 ``` from xtquant import xtdata import time #获取全推数据,仅返回实时数据 #3秒返回一个最新tick while True: res=xtdata.get_full_tick(['600109.SH']) print(res) time.sleep(3) ``` 返回结果 ``` {'600109.SH': {'timetag': '20250409 09:54:46', 'lastPrice': 7.67, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85447600, 'volume': 111903, 'pvolume': 11190300, 'stockStatus': 3, 'openInt': 13, 'settlementPrice': 0, 'lastSettlementPrice': 7.73, 'askPrice': [7.68, 7.69, 7.7, 7.71, 7.72], 'bidPrice': [7.67, 7.66, 7.65, 7.640000000000001, 7.63], 'askVol': [1255, 206, 1830, 399, 1016], 'bidVol': [241, 238, 191, 517, 739]}} {'600109.SH': {'timetag': '20250409 09:54:49', 'lastPrice': 7.67, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85507500, 'volume': 111981, 'pvolume': 11198100, 'stockStatus': 3, 'openInt': 13, 'settlementPrice': 0, 'lastSettlementPrice': 7.73, 'askPrice': [7.68, 7.69, 7.7, 7.71, 7.72], 'bidPrice': [7.67, 7.66, 7.65, 7.640000000000001, 7.63], 'askVol': [1295, 206, 1840, 399, 1016], 'bidVol': [184, 238, 201, 517, 739]}} {'600109.SH': {'timetag': '20250409 09:54:52', 'lastPrice': 7.67, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85507500, 'volume': 111981, 'pvolume': 11198100, 'stockStatus': 3, 'openInt': 13, 'settlementPrice': 0, 'lastSettlementPrice': 7.73, 'askPrice': [7.68, 7.69, 7.7, 7.71, 7.72], 'bidPrice': [7.67, 7.66, 7.65, 7.640000000000001, 7.63], 'askVol': [1295, 106, 1839, 399, 1016], 'bidVol': [217, 238, 201, 517, 749]}} {'600109.SH': {'timetag': '20250409 09:54:55', 'lastPrice': 7.67, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85656300, 'volume': 112175, 'pvolume': 11217500, 'stockStatus': 3, 'openInt': 13, 'settlementPrice': 0, 'lastSettlementPrice': 7.73, 'askPrice': [7.68, 7.69, 7.7, 7.71, 7.72], 'bidPrice': [7.67, 7.66, 7.65, 7.640000000000001, 7.63], 'askVol': [1334, 106, 1835, 399, 1016], 'bidVol': [37, 238, 201, 517, 749]}} {'600109.SH': {'timetag': '20250409 09:54:58', 'lastPrice': 7.68, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85670800, 'volume': 112194, 'pvolume': 11219400, 'stockStatus': 3, 'openInt': 13, 'settlementPrice': 0, 'lastSettlementPrice': 7.73, 'askPrice': [7.68, 7.69, 7.7, 7.71, 7.72], 'bidPrice': [7.67, 7.66, 7.65, 7.640000000000001, 7.63], 'askVol': [1379, 106, 1819, 399, 1016], 'bidVol': [120, 238, 201, 557, 749]}} {'600109.SH': {'timetag': '20250409 09:55:01', 'lastPrice': 7.66, 'open': 7.7, 'high': 7.7, 'low': 7.54, 'lastClose': 7.73, 'amount': 85798200, 'volume': 112360, 'pvolume': 11236000, 'stockStatus': 3, 'openIn ``` **3、回调模式获取数据** 用于获取最新的行情数据 ``` from xtquant import xtdata def on_data(datas): #datas默认是一个字典,key是股票代码,value是DF,3秒刷新一次 #用于获取最新行情数据,收盘以后不再返回数据 print(datas) xtdata.subscribe_quote(stock_code='000001.SZ',period='1m',start_time='',end_time='',count=-1,callback=on_data) xtdata.run() ``` 返回值 ``` {'000001.SZ': [{'time': 1744164120000, 'open': 10.72, 'high': 10.74, 'low': 10.71, 'close': 10.73, 'volume': 8564, 'amount': 9183326.0, 'settlementPrice': 0.0, 'openInterest': 13, 'dr': 1.0, 'totaldr': 98.9329302783749, 'preClose': nan, 'suspendFlag': -1710229840}, {'time': 1744164180000, 'open': 10.73, 'high': 10.74, 'low': 10.73, 'close': 10.74, 'volume': 3408, 'amount': 3656295.0, 'settlementPrice': 0.0, 'openInterest': 13, 'dr': 1.0, 'totaldr': 98.9329302783749, 'preClose': nan, 'suspendFlag': -1710229840}]} {'000001.SZ': [{'time': 1744164180000, 'open': 10.73, 'high': 10.74, 'low': 10.73, 'close': 10.74, 'volume': 4008, 'amount': 4300517.0, 'settlementPrice': 0.0, 'openInterest': 13, 'dr': 1.0, 'totaldr': 98.9329302783749, 'preClose': 0.0, 'suspendFlag': -1710229840}]} {'000001.SZ': [{'time': 1744164180000, 'open': 10.73, 'high': 10.74, 'low': 10.73, 'close': 10.73, 'volume': 4246, 'amount': 4556119.0, 'settlementPrice': 0.0, 'openInterest': 13, 'dr': 1.0, 'totaldr': 98.9329302783749, 'preClose': 0.0, 'suspendFlag': -1710229840}]} {'000001.SZ': [{'time': 1744164180000, 'open': 10.73, 'high': 10.74, 'low': 10.73, 'close': 10.74, 'volume': 4279, 'amount': 4591547.0, 'settlementPrice': 0.0, 'openInterest': 13, 'dr': 1.0, 'totaldr': 98.9329302783749, 'preClose': 0.0, 'suspendFlag': -1710229840}]} ``` 除了了上面的单独订阅、单股获取以外,支持多股订阅,多股数据,只需要修改订阅函数即可,其他的方式亦是如此,不在赘述。 调用一次订阅函数,最多支持订阅三百只标的,可以多次调用,多次传参,以此实现全市场订阅。 另外,QMT模拟盘获取数据可能会有卡顿,如果无法获取,建议使用实盘。 ``` from xtquant import xtdata def on_data(datas): #datas默认是一个字典,key是股票代码,value是DF,3秒刷新一次 #用于获取最新行情数据,收盘以后不再返回数据 print(datas) xtdata.subscribe_whole_quote(code_list=['600109.SH','600100.SH'],callback=on_data) xtdata.run() ``` 返回值 ``` {'600100.SH': {'time': 1744164369000, 'lastPrice': 6.76, 'open': 6.63, 'high': 6.8100000000000005, 'low': 6.34, 'lastClose': 6.78, 'amount': 229356100.0, 'volume': 351470, 'pvolume': 35147044, 'stockStatus': 3, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 6.78, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [6.7700000000000005, 6.78, 6.79, 6.8, 6.8100000000000005], 'bidPrice': [6.76, 6.75, 6.74, 6.73, 6.72], 'askVol': [675, 1169, 653, 1192, 786], 'bidVol': [432, 1512, 1508, 106, 1812], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}} {'600109.SH': {'time': 1744164370000, 'lastPrice': 7.72, 'open': 7.7, 'high': 7.7700000000000005, 'low': 7.54, 'lastClose': 7.73, 'amount': 117440100.0, 'volume': 153324, 'pvolume': 15332400, 'stockStatus': 3, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 7.73, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [7.72, 7.73, 7.74, 7.75, 7.76], 'bidPrice': [7.71, 7.7, 7.69, 7.68, 7.67], 'askVol': [114, 159, 248, 452, 685], 'bidVol': [1389, 411, 204, 120, 191], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}} ``` ### 链接账户 ``` # 导入所需的库 from xtquant import xttrader from xtquant.xttype import StockAccount import random ##订阅账户 # 设置 QMT 交易端的数据路径和会话ID min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 创建 XtQuantTrader 实例并启动 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接 QMT 交易端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() exit() # 设置账户信息 account = '39972702' acc = StockAccount(account) # 订阅账户 res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') ``` 返回值 ``` 链接成功 订阅成功 ``` ### 查询账户资产 ``` # 导入所需的库 from xtquant import xttrader from xtquant.xttype import StockAccount import random import pandas as pd ##订阅账户 # 设置 QMT 交易端的数据路径和会话ID min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 创建 XtQuantTrader 实例并启动 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接 QMT 交易端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() exit() # 设置账户信息 account = '39972702' acc = StockAccount(account) # 订阅账户 res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') # 查询股票资产信息 asset=xt_trader.query_stock_asset(acc) asset_list=[] # 将资产信息转换为字典格式 assset_dict={ '账户类型':asset.account_type, '资金账户':asset.account_id, '可用资金':asset.cash, '冻结金额':asset.frozen_cash, '持仓市值':asset.market_value, '总资产':asset.total_asset } asset_list.append(assset_dict) # 将资产信息转换为DataFrame格式并设置索引为资金账户 asset=pd.DataFrame(asset_list) asset.set_index('资金账户',inplace=True) # 打印资产信息 print(asset) ``` 返回值 ``` 账户类型 可用资金 冻结金额 持仓市值 总资产 资金账户 10503139 2 31126696.03 0.0 1.832906e+08 2.153872e+08 ``` ### 查询当日委托 ``` # 导入所需的库 from xtquant import xttrader from xtquant.xttype import StockAccount import random import pandas as pd ##订阅账户 # 设置 QMT 交易端的数据路径和会话ID min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 创建 XtQuantTrader 实例并启动 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接 QMT 交易端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() exit() # 设置账户信息 account = '10503139' acc = StockAccount(account) # 订阅账户 res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') # 查询账户委托 traders = xt_trader.query_stock_orders(acc) # 处理查询结果,将其转换为 DataFrame 格式 list = [] for i in traders: traders_dict = { '资金账号': i.account_id, '证券代码': i.stock_code, '订单编号': i.order_id, '柜台合同编号': i.order_sysid, '报单时间': i.order_time, '委托类型': i.order_type, '委托数量': i.order_volume, '报价类型': i.price_type, '委托价格': i.price, '成交数量': i.traded_volume, '成交均价': i.traded_price, '委托状态': i.order_status, '委托状态描述': i.status_msg, '策略名称': i.strategy_name, '委托备注': i.order_remark, } list.append(traders_dict) # 将列表转换为 DataFrame df = pd.DataFrame(list) # 打印 DataFrame print(df) # 委托状态说明 # 枚举变量名 值 含义 # xtconstant.ORDER_UNREPORTED 48 未报 # xtconstant.ORDER_WAIT_REPORTING 49 待报 # xtconstant.ORDER_REPORTED 50 已报 # xtconstant.ORDER_REPORTED_CANCEL 51 已报待撤 # xtconstant.ORDER_PARTSUCC_CANCEL 52 部成待撤 # xtconstant.ORDER_PART_CANCEL 53 部撤(已经有一部分成交,剩下的已经撤单) # xtconstant.ORDER_CANCELED 54 已撤 # xtconstant.ORDER_PART_SUCC 55 部成(已经有一部分成交,剩下的待成交) # xtconstant.ORDER_SUCCEEDED 56 已成 # xtconstant.ORDER_JUNK 57 废单 # xtconstant.ORDER_UNKNOWN 255 未知 # 委托类型说明 # 参考链接: https://dict.thinktrader.net/innerApi/enum_constants.html#enum-efuturetradetype-%E6%88%90%E4%BA%A4%E7%B1%BB%E5%9E%8Bfrom xtquant import xttrader ``` 返回值 ``` 连接成功 订阅成功 资金账号 证券代码 订单编号 柜台合同编号 报单时间 委托类型 委托数量 报价类型 委托价格 成交数量 成交均价 委托状态 委托状态描述 策略名称 委托备注 0 10503139 588000.SH 0 1 1744163616 23 100 50 1.004 100 1.004 56 1 10503139 588000.SH 0 10 1744163618 23 100 50 0.995 100 0.995 56 2 10503139 588000.SH 0 100 1744163637 24 100 50 0.912 100 0.912 56 3 10503139 588000.SH 0 101 1744163637 23 100 50 0.910 100 0.910 56 4 10503139 588000.SH 0 102 1744163637 24 100 50 0.911 100 0.911 56 .. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 137 10503139 588000.SH 0 94 1744163637 23 100 50 0.913 100 0.913 56 138 10503139 588000.SH 0 95 1744163637 24 100 50 0.915 100 0.915 56 139 10503139 588000.SH 0 97 1744163637 23 100 50 0.912 100 0.912 56 140 10503139 588000.SH 0 98 1744163637 23 100 50 0.911 100 0.911 56 141 10503139 588000.SH 0 99 1744163637 24 100 50 0.913 100 0.913 56 [142 rows x 15 columns] ``` ### 查询当日成交 ``` # 导入所需的库 from xtquant import xttrader from xtquant.xttype import StockAccount import random import pandas as pd ## 订阅账户 # 设置 QMT 交易端的数据路径和会话ID min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 创建 XtQuantTrader 实例并启动 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接 QMT 交易端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() exit() # 设置账户信息 account = '10503139' acc = StockAccount(account) # 订阅账户 res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') ### 查询当日成交 # 查询指定账户的股票交易记录 trades = xt_trader.query_stock_trades(acc) data_list = [] for i in trades: # 将每笔交易的信息转换成字典,并添加到列表中 i_dict = { '资金账号': i.account_id, '证券代码': i.stock_code, '成交时间': i.traded_time, '成交数量': i.traded_volume, '成交金额': i.traded_amount, '成交均价': i.traded_price, '委托类型': i.order_type, '交易编号': i.traded_id, } data_list.append(i_dict) # 将交易数据列表转换为 DataFrame 格式 df = pd.DataFrame(data_list) # 转换成交时间为本地时间 df['成交时间'] = pd.to_datetime(df['成交时间'], unit='s', utc=True) df['成交时间'] = df['成交时间'].dt.tz_convert('Asia/Shanghai') df['成交时间'] = df['成交时间'].dt.strftime("%Y-%m-%d %H:%M:%S") df['成交时间'] = pd.to_datetime(df['成交时间']) # 设置资金账号作为 DataFrame 的索引 df.set_index('资金账号', inplace=True) print(df) ``` 返回值 ``` 证券代码 成交时间 成交数量 成交金额 成交均价 委托类型 交易编号 资金账号 10503139 588000.SH 2025-04-09 09:53:37 100 100.4 1.004 23 50011444 10503139 588000.SH 2025-04-09 09:53:37 100 100.3 1.003 23 50011446 10503139 588000.SH 2025-04-09 09:53:37 100 100.2 1.002 23 50011447 10503139 588000.SH 2025-04-09 09:53:37 100 100.1 1.001 23 50011449 10503139 588000.SH 2025-04-09 09:53:38 100 100.0 1.000 23 50011450 ... ... ... ... ... ... ... ... 10503139 588000.SH 2025-04-09 09:56:53 100 94.6 0.946 23 50011977 10503139 588000.SH 2025-04-09 09:56:53 100 93.5 0.935 23 50011978 10503139 588000.SH 2025-04-09 09:56:53 100 92.4 0.924 23 50011979 10503139 588000.SH 2025-04-09 09:56:53 100 91.3 0.913 23 50011980 10503139 588000.SH 2025-04-09 09:56:54 100 90.2 0.902 23 50011981 [142 rows x 7 columns] ``` ### 查询持仓 ``` # 导入所需的库 # 导入所需的库 from xtquant import xttrader from xtquant.xttype import StockAccount import random import pandas as pd ##订阅账户 # 设置 QMT 交易端的数据路径和会话ID min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 创建 XtQuantTrader 实例并启动 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接 QMT 交易端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() exit() # 设置账户信息 account = '39972702' acc = StockAccount(account) # 订阅账户 res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') # 查询账户持仓 pos = xt_trader.query_stock_positions(acc) # 处理查询结果,将其转换为 DataFrame 格式 list_pos = [] for i in pos: dict_pos = { '资金账号': i.account_id, # 资金账号 '证券代码': i.stock_code, # 证券代码 '持仓数量': i.volume, # 持仓数量 '可用数量': i.can_use_volume, # 可用数量 '开仓价': i.open_price, # 开仓价 '市值': i.market_value, # 市值 '冻结数量': i.frozen_volume, # 冻结数量 '在途股份': i.on_road_volume, # 在途股份 '昨夜拥股': i.yesterday_volume, # 昨夜拥股 '成本价': i.open_price } list_pos.append(dict_pos) # 将列表转换为 DataFrame pos_df = pd.DataFrame(list_pos) # 打印 DataFrame print(pos_df) # pos_df.to_csv('持仓.csv', index=False) ``` 返回值 ``` 资金账号 证券代码 持仓数量 可用数量 开仓价 市值 冻结数量 在途股份 昨夜拥股 成本价 0 10503139 019742.SH -9999 -9999 0.000000 0.00 0 0 -9999 0.000000 1 10503139 019756.SH -999 -999 0.000000 0.00 0 0 -999 0.000000 2 10503139 110061.SH 90 90 174.484222 16274.70 0 0 90 174.484222 3 10503139 113662.SH -69 -69 144.903913 -10223.73 0 0 -69 144.903913 4 10503139 588000.SH 12600 0 1.018127 13066.20 0 12600 1600 1.018127 .. ... ... ... ... ... ... ... ... ... ... 109 10503139 301501.SZ 400 400 71.035000 17620.00 0 0 400 71.035000 110 10503139 830946.BJ 100 100 10.005000 988.00 0 0 100 10.005000 111 10503139 832786.BJ 200 200 13.706900 2210.00 0 0 200 13.706900 112 10503139 833781.BJ 200 200 13.836900 2262.00 0 0 200 13.836900 113 10503139 836221.BJ 1100 1100 26.663336 17798.00 0 0 1100 26.663336 [114 rows x 10 columns] ``` ### 委托交易 ``` # 导入所需的库和模块 from xtquant import xttrader from xtquant.xttype import StockAccount import random from xtquant import xtconstant # 设置账户信息和路径 account_id = '10503139' min_path = r"D:\国金QMT交易端模拟\userdata_mini" session_id = int(random.randint(100000, 999999)) # 生成一个随机的会话ID # 初始化并启动交易客户端 xt_trader = xttrader.XtQuantTrader(min_path, session_id) xt_trader.start() # 连接交易客户端 connect_result = xt_trader.connect() if connect_result == 0: print('连接成功') else: print('连接失败') xt_trader.stop() # 如果连接失败,停止交易客户端 exit() # 退出程序 # 订阅账户信息 account_id = account_id # 确保 account_id 已定义 acc = StockAccount(account_id) res = xt_trader.subscribe(acc) if res == 0: print('订阅成功') else: print('订阅失败') # 执行买入操作 buy_code = '600109.SH' # 要买入的股票代码 target_volume = 300 # 目标买入数量(单位:股) buy_id = xt_trader.order_stock( acc, buy_code, xtconstant.STOCK_BUY, target_volume, xtconstant.LATEST_PRICE,0 ) #acc-->账户信息,buy_code--->买入股票代码,xtconstant.STOCK_BUY--->买入类型,target_volume--->买入数量,xtconstant.FIX_PRICE--->买入价格类型,current_price--->买入价格 print(f'下单成功,买入标的:{buy_code},买入数量:{target_volume},委托价:{xtconstant.LATEST_PRICE}') # 输出买入订单ID # 执行卖出操作 sell_code = '002426.SZ' # 要卖出的股票代码 target_volume = 100 # 目标卖出数量(单位:股) sell_id = xt_trader.order_stock( acc, sell_code, xtconstant.STOCK_SELL, target_volume, xtconstant.LATEST_PRICE, 0 ) print(f'下单成功,卖出标的:{sell_code},卖出数量{target_volume},委托价:{xtconstant.LATEST_PRICE}') ``` 返回值 ``` 下单成功,买入标的:600109.SH,买入数量:300,委托价:5 #5表示最新价 下单成功,卖出标的:002426.SZ,卖出数量100,委托价:5 ``` ![输入图片说明](3.png) ![输入图片说明](4.png)