QMT Python API

XtQuant.Xttrade 交易模块

版本信息

  • 2020-09-01

    • 初稿
  • 2020-10-14

    • 持仓结构添加字段
    • 投资备注相关修正
  • 2020-10-21

    • 添加信用交易相关委托类型(order_type)枚举
    • 调整XtQuant运行依赖环境说明,更新多版本支持相关说明
  • 2020-11-13

    • 添加信用交易相关类型定义说明
    • 添加信用交易相关接口说明
    • 添加异步撤单委托反馈结构说明
    • 添加下单失败和撤单失败主推结构说明
    • 添加订阅和反订阅接口
    • 添加创建API实例,注册回调类,准备API环境,创建连接,停止运行,阻塞进程接口说明
    • 调整API接口说明
      • 将接口细分为"系统设置接口",“操作接口”,“查询接口”,"信用相关查询接口",“回调类”等五类
      • 接口返回“None”修改为“无”
      • 去掉回调类接口中的示例
      • 添加“备注”项
    • 所有“证券账号”改为“资金账号”
    • 英文“,”调整为中文“,”
    • 示例代码中增加XtQuant API实例对象,修正没有实例,直接调用的错误
    • 添加股票异步撤单接口说明,将原股票撤单修改为股票同步撤单
  • 2020-11-19

    • 添加账号状态主推接口
    • 添加账号状态数据结构说明
    • 添加账号状态枚举值
    • 回调类接口说明调整
      • 将回调函数定义及函数说明标题调整一致
      • 补充异步下单回报推送、异步撤单回报推送接口说明
  • 2021-07-20

    • 修改回调/主推函数实现机制,提升报撤单回报的速度,降低穿透延时波动
    • XtQuantTrader.run_forever()修改实现,支持Ctrl+C跳出
  • 2022-06-27

    • 委托查询支持仅查询可撤委托
    • 添加新股申购相关接口
      • query_new_purchase_limit 查询新股申购额度
      • query_ipo_data 查询新股信息
    • 添加账号信息查询接口
      • query_account_infos
  • 2022-11-15

    • 修复XtQuantTrader.unsubscribe的实现
  • 2022-11-17

    • 交易数据字典格式调整
  • 2022-11-28

    • 为主动请求接口的返回增加专用线程以及相关控制,以支持在on_stock_order等推送接口中调用同步请求
      • XtQuantTrader.set_relaxed_response_order_enabled
  • 2023-07-17

    • 持仓结构XtPosition 成本价字段调整
      • open_price - 开仓价
      • avg_price - 成本价
  • 2023-07-26

    • 添加资金划拨接口 fund_transfer
  • 2023-08-11

    • 添加划拨业务查询普通柜台资金接口 query_com_fund
    • 添加划拨业务查询普通柜台持仓接口 query_com_position
  • 2023-10-16

    • 添加期货市价的报价类型
      • xtconstant.MARKET_BEST - 市价最优价[郑商所]
      • xtconstant.MARKET_CANCEL - 市价即成剩撤[大商所]
      • xtconstant.MARKET_CANCEL_ALL - 市价全额成交或撤[大商所]
      • xtconstant.MARKET_CANCEL_1 - 市价最优一档即成剩撤[中金所]
      • xtconstant.MARKET_CANCEL_5 - 市价最优五档即成剩撤[中金所]
      • xtconstant.MARKET_CONVERT_1 - 市价最优一档即成剩转[中金所]
      • xtconstant.MARKET_CONVERT_5 - 市价最优五档即成剩转[中金所]
  • 2023-10-20

  • 委托结构XtOrder,成交结构XtTrade,持仓结构XtPosition 新增多空字段

    • direction - 多空,股票不需要
  • 委托结构XtOrder,成交结构XtTrade新增交易操作字段

    • offset_flag - 交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等
  • 2023-11-03

    • 添加券源行情查询接口 smt_query_quoter
    • 添加库存券约券申请接口 smt_negotiate_order
    • 添加约券合约查询接口 smt_query_compact
  • 2024-01-02

    • 委托类型增加ETF申赎
  • 2024-02-29

    • 添加期货持仓统计查询接口query_position_statistics
  • 2024-04-25

    • 数据结构添加stock_code1字段以适配长代码
  • 2024-05-24

    • 添加通用数据导出接口export_data
    • 添加通用数据查询接口query_data
  • 2024-06-27

    • 添加外部成交导入接口sync_transaction_from_external

快速入门

创建策略

qmt://docs/python
Python
1#coding=utf-8
2from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
3from xtquant.xttype import StockAccount
4from xtquant import xtconstant
5 
6 
7class MyXtQuantTraderCallback(XtQuantTraderCallback):
8 def on_disconnected(self):
9 """
10 连接断开
11 :return:
12 """
13 print("connection lost")
14 def on_stock_order(self, order):
15 """
16 委托回报推送
17 :param order: XtOrder对象
18 :return:
19 """
20 print("on order callback:")
21 print(order.stock_code, order.order_status, order.order_sysid)
22 def on_stock_trade(self, trade):
23 """
24 成交变动推送
25 :param trade: XtTrade对象
26 :return:
27 """
28 print("on trade callback")
29 print(trade.account_id, trade.stock_code, trade.order_id)
30 def on_order_error(self, order_error):
31 """
32 委托失败推送
33 :param order_error:XtOrderError 对象
34 :return:
35 """
36 print("on order_error callback")
37 print(order_error.order_id, order_error.error_id, order_error.error_msg)
38 def on_cancel_error(self, cancel_error):
39 """
40 撤单失败推送
41 :param cancel_error: XtCancelError 对象
42 :return:
43 """
44 print("on cancel_error callback")
45 print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
46 def on_order_stock_async_response(self, response):
47 """
48 异步下单回报推送
49 :param response: XtOrderResponse 对象
50 :return:
51 """
52 print("on_order_stock_async_response")
53 print(response.account_id, response.order_id, response.seq)
54 def on_account_status(self, status):
55 """
56 :param response: XtAccountStatus 对象
57 :return:
58 """
59 print("on_account_status")
60 print(status.account_id, status.account_type, status.status)
61 
62if __name__ == "__main__":
63 print("demo test")
64 # path为mini qmt客户端安装目录下userdata_mini路径
65 path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
66 # session_id为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
67 session_id = 123456
68 xt_trader = XtQuantTrader(path, session_id)
69 # 创建资金账号为1000000365的证券账号对象
70 acc = StockAccount('1000000365')
71 # StockAccount可以用第二个参数指定账号类型,如沪港通传'HUGANGTONG',深港通传'SHENGANGTONG'
72 # acc = StockAccount('1000000365','STOCK')
73 # 创建交易回调类对象,并声明接收回调
74 callback = MyXtQuantTraderCallback()
75 xt_trader.register_callback(callback)
76 # 启动交易线程
77 xt_trader.start()
78 # 建立交易连接,返回0表示连接成功
79 connect_result = xt_trader.connect()
80 print(connect_result)
81 # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
82 subscribe_result = xt_trader.subscribe(acc)
83 print(subscribe_result)
84 stock_code = '600000.SH'
85 # 使用指定价下单,接口返回订单编号,后续可以用于撤单操作以及查询委托状态
86 print("order using the fix price:")
87 fix_result_order_id = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5, 'strategy_name', 'remark')
88 print(fix_result_order_id)
89 # 使用订单编号撤单
90 print("cancel order:")
91 cancel_order_result = xt_trader.cancel_order_stock(acc, fix_result_order_id)
92 print(cancel_order_result)
93 # 使用异步下单接口,接口返回下单请求序号seq,seq可以和on_order_stock_async_response的委托反馈response对应起来
94 print("order using async api:")
95 async_seq = xt_trader.order_stock_async(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5, 'strategy_name', 'remark')
96 print(async_seq)
97 # 查询证券资产
98 print("query asset:")
99 asset = xt_trader.query_stock_asset(acc)
100 if asset:
101 print("asset:")
102 print("cash {0}".format(asset.cash))
103 # 根据订单编号查询委托
104 print("query order:")
105 order = xt_trader.query_stock_order(acc, fix_result_order_id)
106 if order:
107 print("order:")
108 print("order {0}".format(order.order_id))
109 # 查询当日所有的委托
110 print("query orders:")
111 orders = xt_trader.query_stock_orders(acc)
112 print("orders:", len(orders))
113 if len(orders) != 0:
114 print("last order:")
115 print("{0} {1} {2}".format(orders[-1].stock_code, orders[-1].order_volume, orders[-1].price))
116 # 查询当日所有的成交
117 print("query trade:")
118 trades = xt_trader.query_stock_trades(acc)
119 print("trades:", len(trades))
120 if len(trades) != 0:
121 print("last trade:")
122 print("{0} {1} {2}".format(trades[-1].stock_code, trades[-1].traded_volume, trades[-1].traded_price))
123 # 查询当日所有的持仓
124 print("query positions:")
125 positions = xt_trader.query_stock_positions(acc)
126 print("positions:", len(positions))
127 if len(positions) != 0:
128 print("last position:")
129 print("{0} {1} {2}".format(positions[-1].account_id, positions[-1].stock_code, positions[-1].volume))
130 # 根据股票代码查询对应持仓
131 print("query position:")
132 position = xt_trader.query_stock_position(acc, stock_code)
133 if position:
134 print("position:")
135 print("{0} {1} {2}".format(position.account_id, position.stock_code, position.volume))
136 # 阻塞线程,接收交易推送
137 xt_trader.run_forever()

进阶篇

XtQuant运行逻辑

XtQuant封装了策略交易所需要的Python API接口,可以和MiniQMT客户端交互进行报单、撤单、查询资产、查询委托、查询成交、查询持仓以及收到资金、委托、成交和持仓等变动的主推消息。

XtQuant数据字典

交易市场(market)

  • 上交所 - xtconstant.SH_MARKET
  • 深交所 - xtconstant.SZ_MARKET
  • 北交所 - xtconstant.MARKET_ENUM_BEIJING
  • 沪港通 - xtconstant.MARKET_ENUM_SHANGHAI_HONGKONG_STOCK
  • 深港通 - xtconstant.MARKET_ENUM_SHENZHEN_HONGKONG_STOCK
  • 上期所 - xtconstant.MARKET_ENUM_SHANGHAI_FUTURE
  • 大商所 - xtconstant.MARKET_ENUM_DALIANG_FUTURE
  • 郑商所 - xtconstant.MARKET_ENUM_ZHENGZHOU_FUTURE
  • 中金所 - xtconstant.MARKET_ENUM_INDEX_FUTURE
  • 能源中心 - xtconstant.MARKET_ENUM_INTL_ENERGY_FUTURE
  • 广期所 - xtconstant.MARKET_ENUM_GUANGZHOU_FUTURE
  • 上海期权 - xtconstant.MARKET_ENUM_SHANGHAI_STOCK_OPTION
  • 深证期权 - xtconstant.MARKET_ENUM_SHENZHEN_STOCK_OPTION

账号类型(account_type)

  • 期货 - xtconstant.FUTURE_ACCOUNT
  • 股票 - xtconstant.SECURITY_ACCOUNT
  • 信用 - xtconstant.CREDIT_ACCOUNT
  • 期货期权 - xtconstant.FUTURE_OPTION_ACCOUNT
  • 股票期权 - xtconstant.STOCK_OPTION_ACCOUNT
  • 沪港通 - xtconstant.HUGANGTONG_ACCOUNT
  • 深港通 - xtconstant.SHENGANGTONG_ACCOUNT

委托类型(order_type)

  • 股票

    • 买入 - xtconstant.STOCK_BUY
    • 卖出 - xtconstant.STOCK_SELL
  • 信用

    • 担保品买入 - xtconstant.CREDIT_BUY
    • 担保品卖出 - xtconstant.CREDIT_SELL
    • 融资买入 - xtconstant.CREDIT_FIN_BUY
    • 融券卖出 - xtconstant.CREDIT_SLO_SELL
    • 买券还券 - xtconstant.CREDIT_BUY_SECU_REPAY
    • 直接还券 - xtconstant.CREDIT_DIRECT_SECU_REPAY
    • 卖券还款 - xtconstant.CREDIT_SELL_SECU_REPAY
    • 直接还款 - xtconstant.CREDIT_DIRECT_CASH_REPAY
    • 专项融资买入 - xtconstant.CREDIT_FIN_BUY_SPECIAL
    • 专项融券卖出 - xtconstant.CREDIT_SLO_SELL_SPECIAL
    • 专项买券还券 - xtconstant.CREDIT_BUY_SECU_REPAY_SPECIAL
    • 专项直接还券 - xtconstant.CREDIT_DIRECT_SECU_REPAY_SPECIAL
    • 专项卖券还款 - xtconstant.CREDIT_SELL_SECU_REPAY_SPECIAL
    • 专项直接还款 - xtconstant.CREDIT_DIRECT_CASH_REPAY_SPECIAL
  • 期货六键风格

    • 开多 - xtconstant.FUTURE_OPEN_LONG
    • 平昨多 - xtconstant.FUTURE_CLOSE_LONG_HISTORY
    • 平今多 - xtconstant.FUTURE_CLOSE_LONG_TODAY
    • 开空 - xtconstant.FUTURE_OPEN_SHORT
    • 平昨空 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY
    • 平今空 - xtconstant.FUTURE_CLOSE_SHORT_TODAY
  • 期货四键风格

    • 平多,优先平今 - xtconstant.FUTURE_CLOSE_LONG_TODAY_FIRST
    • 平多,优先平昨 - xtconstant.FUTURE_CLOSE_LONG_HISTORY_FIRST
    • 平空,优先平今 - xtconstant.FUTURE_CLOSE_SHORT_TODAY_FIRST
    • 平空,优先平昨 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY_FIRST
  • 期货两键风格

    • 卖出,如有多仓,优先平仓,优先平今,如有余量,再开空 - xtconstant.FUTURE_CLOSE_LONG_TODAY_HISTORY_THEN_OPEN_SHORT
    • 卖出,如有多仓,优先平仓,优先平昨,如有余量,再开空 - xtconstant.FUTURE_CLOSE_LONG_HISTORY_TODAY_THEN_OPEN_SHORT
    • 买入,如有空仓,优先平仓,优先平今,如有余量,再开多 - xtconstant.FUTURE_CLOSE_SHORT_TODAY_HISTORY_THEN_OPEN_LONG
    • 买入,如有空仓,优先平仓,优先平昨,如有余量,再开多 - xtconstant.FUTURE_CLOSE_SHORT_HISTORY_TODAY_THEN_OPEN_LONG
    • 买入,不优先平仓 - xtconstant.FUTURE_OPEN
    • 卖出,不优先平仓 - xtconstant.FUTURE_CLOSE
  • 期货 - 跨商品套利

    • 开仓 - xtconstant.FUTURE_ARBITRAGE_OPEN
    • 平, 优先平昨 - xtconstant.FUTURE_ARBITRAGE_CLOSE_HISTORY_FIRST
    • 平, 优先平今 - xtconstant.FUTURE_ARBITRAGE_CLOSE_TODAY_FIRST
  • 期货展期

    • 看多, 优先平昨 - xtconstant.FUTURE_RENEW_LONG_CLOSE_HISTORY_FIRST
    • 看多,优先平今 - xtconstant.FUTURE_RENEW_LONG_CLOSE_TODAY_FIRST
    • 看空,优先平昨 - xtconstant.FUTURE_RENEW_SHORT_CLOSE_HISTORY_FIRST
    • 看空,优先平今 - xtconstant.FUTURE_RENEW_SHORT_CLOSE_TODAY_FIRST
  • 股票期权

    • 买入开仓,以下用于个股期权交易业务 - xtconstant.STOCK_OPTION_BUY_OPEN
    • 卖出平仓 - xtconstant.STOCK_OPTION_SELL_CLOSE
    • 卖出开仓 - xtconstant.STOCK_OPTION_SELL_OPEN
    • 买入平仓 - xtconstant.STOCK_OPTION_BUY_CLOSE
    • 备兑开仓 - xtconstant.STOCK_OPTION_COVERED_OPEN
    • 备兑平仓 - xtconstant.STOCK_OPTION_COVERED_CLOSE
    • 认购行权 - xtconstant.STOCK_OPTION_CALL_EXERCISE
    • 认沽行权 - xtconstant.STOCK_OPTION_PUT_EXERCISE
    • 证券锁定 - xtconstant.STOCK_OPTION_SECU_LOCK
    • 证券解锁 - xtconstant.STOCK_OPTION_SECU_UNLOCK
  • 期货期权

    • 期货期权行权 - xtconstant.OPTION_FUTURE_OPTION_EXERCISE
  • ETF申赎

    • 申购 - xtconstant.ETF_PURCHASE
    • 赎回 - xtconstant.ETF_REDEMPTION

报价类型(price_type)

提示

  1. 市价类型只在实盘环境中生效,模拟环境不支持市价方式报单
  • 最新价 - xtconstant.LATEST_PRICE
  • 指定价 - xtconstant.FIX_PRICE
  • 郑商所 期货
    • 市价最优价 - xtconstant.MARKET_BEST
  • 大商所 期货
    • 市价即成剩撤 - xtconstant.MARKET_CANCEL
    • 市价全额成交或撤 - xtconstant.MARKET_CANCEL_ALL
  • 中金所 期货
    • 市价最优一档即成剩撤 - xtconstant.MARKET_CANCEL_1
    • 市价最优五档即成剩撤 - xtconstant.MARKET_CANCEL_5
    • 市价最优一档即成剩转 - xtconstant.MARKET_CONVERT_1
    • 市价最优五档即成剩转 - xtconstant.MARKET_CONVERT_5
  • 上交所/北交所 股票
    • 最优五档即时成交剩余撤销 - xtconstant.MARKET_SH_CONVERT_5_CANCEL
    • 最优五档即时成交剩转限价 - xtconstant.MARKET_SH_CONVERT_5_LIMIT
    • 对手方最优价格委托 - xtconstant.MARKET_PEER_PRICE_FIRST
    • 本方最优价格委托 - xtconstant.MARKET_MINE_PRICE_FIRST
  • 深交所 股票 期权
    • 对手方最优价格委托 - xtconstant.MARKET_PEER_PRICE_FIRST
    • 本方最优价格委托 - xtconstant.MARKET_MINE_PRICE_FIRST
    • 即时成交剩余撤销委托 - xtconstant.MARKET_SZ_INSTBUSI_RESTCANCEL
    • 最优五档即时成交剩余撤销 - xtconstant.MARKET_SZ_CONVERT_5_CANCEL
    • 全额成交或撤销委托 - xtconstant.MARKET_SZ_FULL_OR_CANCEL

委托状态(order_status)

枚举变量名含义
xtconstant.ORDER_UNREPORTED48未报
xtconstant.ORDER_WAIT_REPORTING49待报
xtconstant.ORDER_REPORTED50已报
xtconstant.ORDER_REPORTED_CANCEL51已报待撤
xtconstant.ORDER_PARTSUCC_CANCEL52部成待撤
xtconstant.ORDER_PART_CANCEL53部撤(已经有一部分成交,剩下的已经撤单)
xtconstant.ORDER_CANCELED54已撤
xtconstant.ORDER_PART_SUCC55部成(已经有一部分成交,剩下的待成交)
xtconstant.ORDER_SUCCEEDED56已成
xtconstant.ORDER_JUNK57废单
xtconstant.ORDER_UNKNOWN255未知

账号状态(account_status)

枚举变量名含义
xtconstant.ACCOUNT_STATUS_INVALID-1无效
xtconstant.ACCOUNT_STATUS_OK0正常
xtconstant.ACCOUNT_STATUS_WAITING_LOGIN1连接中
xtconstant.ACCOUNT_STATUSING2登陆中
xtconstant.ACCOUNT_STATUS_FAIL3失败
xtconstant.ACCOUNT_STATUS_INITING4初始化中
xtconstant.ACCOUNT_STATUS_CORRECTING5数据刷新校正中
xtconstant.ACCOUNT_STATUS_CLOSED6收盘后
xtconstant.ACCOUNT_STATUS_ASSIS_FAIL7穿透副链接断开
xtconstant.ACCOUNT_STATUS_DISABLEBYSYS8系统停用(总线使用-密码错误超限)
xtconstant.ACCOUNT_STATUS_DISABLEBYUSER9用户停用(总线使用)

划拨方向(transfer_direction)

枚举变量名含义
xtconstant.FUNDS_TRANSFER_NORMAL_TO_SPEED510资金划拨-普通柜台到极速柜台
xtconstant.FUNDS_TRANSFER_SPEED_TO_NORMAL511资金划拨-极速柜台到普通柜台
xtconstant.NODE_FUNDS_TRANSFER_SH_TO_SZ512节点资金划拨-上海节点到深圳节点
xtconstant.NODE_FUNDS_TRANSFER_SZ_TO_SH513节点资金划拨-深圳节点到上海节点

多空方向(direction)

枚举变量名含义
xtconstant.DIRECTION_FLAG_LONG48
xtconstant.DIRECTION_FLAG_SHORT49

交易操作(offset_flag)

枚举变量名含义
xtconstant.OFFSET_FLAG_OPEN48买入,开仓
xtconstant.OFFSET_FLAG_CLOSE49卖出,平仓
xtconstant.OFFSET_FLAG_FORCECLOSE50强平
xtconstant.OFFSET_FLAG_CLOSETODAY51平今
xtconstant.OFFSET_FLAG_ClOSEYESTERDAY52平昨
xtconstant.OFFSET_FLAG_FORCEOFF53强减
xtconstant.OFFSET_FLAG_LOCALFORCECLOSE54本地强平

XtQuant数据结构说明

资产XtAsset

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
cashfloat可用金额
frozen_cashfloat冻结金额
market_valuefloat持仓市值
total_assetfloat总资产

委托XtOrder

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
stock_codestr证券代码,例如"600000.SH"
order_idint订单编号
order_sysidstr柜台合同编号
order_timeint报单时间
order_typeint委托类型,参见数据字典
order_volumeint委托数量
price_typeint报价类型,该字段在返回时为柜台返回类型,不等价于下单传入的price_type,枚举值不一样功能一样,参见数据字典
pricefloat委托价格
traded_volumeint成交数量
traded_pricefloat成交均价
order_statusint委托状态,参见数据字典
status_msgstr委托状态描述,如废单原因
strategy_namestr策略名称
order_remarkstr委托备注,最大 24 个英文字符
directionint多空方向,股票不适用;参见数据字典
offset_flagint交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等;参见数据字典

成交XtTrade

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
stock_codestr证券代码
order_typeint委托类型,参见数据字典
traded_idstr成交编号
traded_timeint成交时间
traded_pricefloat成交均价
traded_volumeint成交数量
traded_amountfloat成交金额
order_idint订单编号
order_sysidstr柜台合同编号
strategy_namestr策略名称
order_remarkstr委托备注,最大 24 个英文字符(
directionint多空方向,股票不适用;参见数据字典
offset_flagint交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等;参见数据字典

持仓XtPosition

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
stock_codestr证券代码
volumeint持仓数量
can_use_volumeint可用数量
open_pricefloat开仓价(返回与成本价一致)
market_valuefloat市值
frozen_volumeint冻结数量
on_road_volumeint在途股份
yesterday_volumeint昨夜拥股
avg_pricefloat成本价
directionint多空方向,股票不适用;参见数据字典

期货持仓统计XtPositionStatistics

属性类型注释
account_idstring账户
exchange_idstring市场代码
exchange_namestring市场名称
product_idstring品种代码
instrument_idstring合约代码
instrument_namestring合约名称
directionint多空方向,股票不适用;参见数据字典
hedge_flagint投保类型;参见投保类型
positionint持仓数量
yesterday_positionint昨仓数量
today_positionint今仓数量
can_close_volint可平数量
position_costfloat持仓成本
avg_pricefloat持仓均价
position_profitfloat持仓盈亏
float_profitfloat浮动盈亏
open_pricefloat开仓均价
open_costfloat开仓成本
used_marginfloat已使用保证金
used_commissionfloat已使用的手续费
frozen_marginfloat冻结保证金
frozen_commissionfloat冻结手续费
instrument_valuefloat市值,合约价值
open_timesint开仓次数
open_volumeint总开仓量 中间平仓不减
cancel_timesint撤单次数
last_pricefloat最新价
rise_ratiofloat当日涨幅
product_namestring产品名称
royaltyfloat权利金市值
expire_datestring到期日
assest_weightfloat资产占比
increase_by_settlementfloat当日涨幅(结)
margin_ratiofloat保证金占比
float_profit_divide_by_used_marginfloat浮盈比例(保证金)
float_profit_divide_by_balancefloat浮盈比例(动态权益)
today_profit_lossfloat当日盈亏(结)
yesterday_init_positionint昨日持仓
frozen_royaltyfloat冻结权利金
today_close_profit_lossfloat当日盈亏(收)
close_profitfloat平仓盈亏
ft_product_namestring品种名称

异步下单委托反馈XtOrderResponse

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
order_idint订单编号
strategy_namestr策略名称
order_remarkstr委托备注
seqint异步下单的请求序号

异步撤单委托反馈XtCancelOrderResponse

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
order_idint订单编号
order_sysidstr柜台委托编号
cancel_resultint撤单结果(0 成功,-1 失败)
seqint异步撤单的请求序号

下单失败错误XtOrderError

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
order_idint订单编号
error_idint下单失败错误码
error_msgstr下单失败具体信息
strategy_namestr策略名称
order_remarkstr委托备注

撤单失败错误XtCancelError

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
order_idint订单编号
marketint交易市场 0:上海 1:深圳
order_sysidstr柜台委托编号
error_idint下单失败错误码
error_msgstr下单失败具体信息

信用账号资产XtCreditDetail

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
m_nStatusint账号状态
m_nUpdateTimeint更新时间
m_nCalcConfigint计算参数
m_dFrozenCashfloat冻结金额
m_dBalancefloat总资产
m_dAvailablefloat可用金额
m_dPositionProfitfloat持仓盈亏
m_dMarketValuefloat总市值
m_dFetchBalancefloat可取金额
m_dStockValuefloat股票市值
m_dFundValuefloat基金市值
m_dTotalDebtfloat总负债
m_dEnableBailBalancefloat可用保证金
m_dPerAssurescaleValuefloat维持担保比例
m_dAssureAssetfloat净资产
m_dFinDebtfloat融资负债
m_dFinDealAvlfloat融资本金
m_dFinFeefloat融资息费
m_dSloDebtfloat融券负债
m_dSloMarketValuefloat融券市值
m_dSloFeefloat融券息费
m_dOtherFarefloat其它费用
m_dFinMaxQuotafloat融资授信额度
m_dFinEnableQuotafloat融资可用额度
m_dFinUsedQuotafloat融资冻结额度
m_dSloMaxQuotafloat融券授信额度
m_dSloEnableQuotafloat融券可用额度
m_dSloUsedQuotafloat融券冻结额度
m_dSloSellBalancefloat融券卖出资金
m_dUsedSloSellBalancefloat已用融券卖出资金
m_dSurplusSloSellBalancefloat剩余融券卖出资金

负债合约StkCompacts

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
compact_typeint合约类型
cashgroup_propint头寸来源
exchange_idint证券市场
open_dateint开仓日期
business_volint合约证券数量
real_compact_volint未还合约数量
ret_end_dateint到期日
business_balancefloat合约金额
businessFarefloat合约息费
real_compact_balancefloat未还合约金额
real_compact_farefloat未还合约息费
repaid_farefloat已还息费
repaid_balancefloat已还金额
instrument_idstr证券代码
compact_idstr合约编号
position_strstr定位串

融资融券标的CreditSubjects

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
slo_statusint融券状态(参见下一条融券状态说明)
fin_statusint融资状态
exchange_idint证券市场
slo_ratiofloat融券保证金比例
fin_ratiofloat融资保证金比例
instrument_idstr证券代码

融券状态说明

返回值状态
48正常
49暂停
50作废

可融券数据CreditSloCode

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
cashgroup_propint头寸来源
exchange_idint证券市场
enable_amountint融券可融数量
instrument_idstr证券代码

标的担保品CreditAssure

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
assure_statusint是否可做担保
exchange_idint证券市场
assure_ratiofloat担保品折算比例
instrument_idstr证券代码

账号状态XtAccountStatus

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
statusint账号状态,参见数据字典

账号信息XtAccountInfo

属性类型注释
account_typeint账号类型,参见数据字典
account_idstr资金账号
broker_typeint同 account_type
platform_idint平台号
account_classificationint账号分类
login_statusint账号状态,参见数据字典

约券相关异步接口的反馈XtSmtAppointmentResponse

属性类型注释
seqint异步请求序号
successbool申请是否成功
msgstr反馈信息
apply_idstr若申请成功返回资券申请编号,否则返回-1

XtQuant API说明

系统设置接口

创建API实例

qmt://docs/python
Python
1XtQuantTrader(path, session_id)
  • 释义
    • 创建XtQuant API的实例
  • 参数
    • path - str MiniQMT客户端userdata_mini的完整路径
    • session_id - int 与MiniQMT通信的会话ID,不同的会话要保证不重
  • 返回
    • XtQuant API实例对象
  • 备注
    • 后续对XtQuant API的操作都需要该实例对象
    • 通常情况下只需要创建一个XtQuant API实例
  • 示例
qmt://docs/python
Python
1path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
2# session_id为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
3session_id = 123456
4#后续的所有示例将使用该实例对象
5xt_trader = XtQuantTrader(path, session_id)

注册回调类

qmt://docs/python
Python
1register_callback(callback)
  • 释义
    • 将回调类实例对象注册到API实例中,用以消息回调和主推
  • 参数
    • callback - XtQuantTraderCallback 回调类实例对象
  • 返回
  • 备注
  • 示例
qmt://docs/python
Python
1# 创建交易回调类对象,并声明接收回调
2class MyXtQuantTraderCallback(XtQuantTraderCallback)
3 ...
4 pass
5callback = MyXtQuantTraderCallback()
6#xt_trader为XtQuant API实例对象
7xt_trader.register_callback(callback)

准备API环境

qmt://docs/python
Python
1start()
  • 释义
    • 启动交易线程,准备交易所需的环境
  • 参数
  • 返回
  • 备注
  • 示例
qmt://docs/python
Python
1# 启动交易线程
2#xt_trader为XtQuant API实例对象
3xt_trader.start()

创建连接

qmt://docs/python
Python
1connect()
  • 释义
    • 连接MiniQMT
  • 参数
  • 返回
    • 连接结果信息,连接成功返回0,失败返回非0
  • 备注
    • 该连接为一次性连接,断开连接后不会重连,需要再次主动调用
  • 示例
qmt://docs/python
Python
1# 建立交易连接,返回0表示连接成功
2#xt_trader为XtQuant API实例对象
3connect_result = xt_trader.connect()
4print(connect_result)

停止运行

qmt://docs/python
Python
1stop()
  • 释义
    • 停止API接口
  • 参数
  • 返回
  • 备注
  • 示例
qmt://docs/python
Python
1#xt_trader为XtQuant API实例对象
2xt_trader.stop()

阻塞当前线程进入等待状态

qmt://docs/python
Python
1run_forever()
  • 释义
    • 阻塞当前线程,进入等待状态,直到stop函数被调用结束阻塞
  • 参数
  • 返回
  • 备注
  • 示例
qmt://docs/python
Python
1#xt_trader为XtQuant API实例对象
2xt_trader.run_forever()

开启主动请求接口的专用线程

qmt://docs/python
Python
1set_relaxed_response_order_enabled(enabled)
  • 释义

    • 控制主动请求接口的返回是否从额外的专用线程返回,以获得宽松的数据时序
  • 参数

    • enabled - bool 是否开启,默认为False关闭
  • 返回

  • 备注

    • 如果开启,在on_stock_order等推送回调中调用同步请求不会卡住,但查询和推送的数据在时序上会变得不确定

    • qmt://docs/python
      Python
      1timeline t1 t2 t3 t4
      2callback push1 push2 push3 resp4
      3do query4 ------------------^
    • 例如:分别在t1 t2 t3时刻到达三条委托数据,在on_push1中调用同步委托查询接口query_orders()

    • 未开启宽松时序时,查询返回resp4会在t4时刻排队到push3完成之后处理,这使得同步等待结果的查询不能返回而卡住执行

    • 开启宽松时序时,查询返回的resp4由专用线程返回,程序正常执行,但此时查到的resp4是push3之后的状态,也就是说resp4中的委托要比push2 push3这两个前一时刻推送的数据新,但在更早的t1时刻就进入了处理

    • 使用中请根据策略实际情况来开启,通常情况下,推荐在on_stock_order等推送回调中使用查询接口的异步版本,如query_stock_orders_async

操作接口

订阅账号信息

qmt://docs/python
Python
1subscribe(account)
  • 释义
    • 订阅账号信息,包括资金账号、委托信息、成交信息、持仓信息
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 订阅结果信息,订阅成功返回0,订阅失败返回-1
  • 备注
  • 示例
    • 订阅资金账号1000000365
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3subscribe_result = xt_trader.subscribe(account)

反订阅账号信息

qmt://docs/python
Python
1unsubscribe(account)
  • 释义
    • 反订阅账号信息
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 反订阅结果信息,订阅成功返回0,订阅失败返回-1
  • 备注
  • 示例
    • 订阅资金账号1000000365
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3unsubscribe_result = xt_trader.unsubscribe(account)

股票同步报单

qmt://docs/python
Python
1order_stock(account, stock_code, order_type, order_volume, price_type, price, strategy_name, order_remark)
  • 释义
    • 对股票进行下单操作
  • 参数
    • account - StockAccount 资金账号
    • stock_code - str 证券代码,如'600000.SH'
    • order_type - int 委托类型
    • order_volume - int 委托数量,股票以'股'为单位,债券以'张'为单位
    • price_type - int 报价类型
    • price - float 委托价格
    • strategy_name - str 策略名称
    • order_remark - str 委托备注
  • 返回
    • 系统生成的订单编号,成功委托后的订单编号为大于0的正整数,如果为-1表示委托失败
  • 备注
  • 示例
    • 股票资金账号1000000365对浦发银行买入1000股,使用限价价格10.5元, 委托备注为'order_test'
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3order_id = xt_trader.order_stock(account, '600000.SH', xtconstant.STOCK_BUY, 1000, xtconstant.FIX_PRICE, 10.5, 'strategy1', 'order_test')

股票异步报单

qmt://docs/python
Python
1order_stock_async(account, stock_code, order_type, order_volume, price_type, price, strategy_name, order_remark)
  • 释义
    • 对股票进行异步下单操作,异步下单接口如果正常返回了下单请求序号seq,会收到on_order_stock_async_response的委托反馈
  • 参数
    • account - StockAccount 资金账号
    • stock_code - str 证券代码, 如'600000.SH'
    • order_type - int 委托类型
    • order_volume - int 委托数量,股票以'股'为单位,债券以'张'为单位
    • price_type - int 报价类型
    • price - float 委托价格
    • strategy_name - str 策略名称
    • order_remark - str 委托备注
  • 返回
    • 返回下单请求序号seq,成功委托后的下单请求序号为大于0的正整数,如果为-1表示委托失败
  • 备注
    • 如果失败,则通过下单失败主推接口返回下单失败信息
  • 示例
    • 股票资金账号1000000365对浦发银行买入1000股,使用限价价格10.5元,委托备注为'order_test'
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3seq = xt_trader.order_stock_async(account, '600000.SH', xtconstant.STOCK_BUY, 1000, xtconstant.FIX_PRICE, 10.5, 'strategy1', 'order_test')

股票同步撤单

qmt://docs/python
Python
1cancel_order_stock(account, order_id)
  • 释义
    • 根据订单编号对委托进行撤单操作
  • 参数
    • account - StockAccount 资金账号
    • order_id - int 同步下单接口返回的订单编号,对于期货来说,是order结构中的order_sysid字段
  • 返回
    • 返回是否成功发出撤单指令,0: 成功, -1: 表示撤单失败
  • 备注
  • 示例
    • 股票资金账号1000000365对订单编号为order_id的委托进行撤单
qmt://docs/python
Python
1account = StockAccount('1000000365')
2order_id = 100
3#xt_trader为XtQuant API实例对象
4cancel_result = xt_trader.cancel_order_stock(account, order_id)

股票同步撤单

qmt://docs/python
Python
1cancel_order_stock_sysid(account, market, order_sysid)
  • 释义
    • 根据券商柜台返回的合同编号对委托进行撤单操作
  • 参数
    • account - StockAccount 资金账号
    • market - int 交易市场
    • order_sysid - str 券商柜台的合同编号
  • 返回
    • 返回是否成功发出撤单指令,0: 成功, -1: 表示撤单失败
  • 备注
  • 示例
    • 股票资金账号1000000365对柜台合同编号为order_sysid的上交所委托进行撤单
qmt://docs/python
Python
1account = StockAccount('1000000365')
2market = xtconstant.SH_MARKET
3order_sysid = "100"
4#xt_trader为XtQuant API实例对象
5cancel_result = xt_trader.cancel_order_stock_sysid(account, market, order_sysid)

股票异步撤单

qmt://docs/python
Python
1cancel_order_stock_async(account, order_id)
  • 释义
    • 根据订单编号对委托进行异步撤单操作
  • 参数
    • account - StockAccount 资金账号
    • order_id - int 下单接口返回的订单编号,对于期货来说,是order结构中的order_sysid
  • 返回
    • 返回撤单请求序号, 成功委托后的撤单请求序号为大于0的正整数, 如果为-1表示委托失败
  • 备注
    • 如果失败,则通过撤单失败主推接口返回撤单失败信息
  • 示例
    • 股票资金账号1000000365对订单编号为order_id的委托进行异步撤单
qmt://docs/python
Python
1account = StockAccount('1000000365')
2order_id = 100
3#xt_trader为XtQuant API实例对象
4cancel_result = xt_trader.cancel_order_stock_async(account, order_id)

股票异步撤单

qmt://docs/python
Python
1cancel_order_stock_sysid_async(account, market, order_sysid)
  • 释义
    • 根据券商柜台返回的合同编号对委托进行异步撤单操作
  • 参数
    • account - StockAccount 资金账号
    • market - int 交易市场
    • order_sysid - str 券商柜台的合同编号
  • 返回
    • 返回撤单请求序号, 成功委托后的撤单请求序号为大于0的正整数, 如果为-1表示委托失败
  • 备注
    • 如果失败,则通过撤单失败主推接口返回撤单失败信息
  • 示例
    • 股票资金账号1000000365对柜台合同编号为order_sysid的上交所委托进行异步撤单
qmt://docs/python
Python
1account = StockAccount('1000000365')
2market = xtconstant.SH_MARKET
3order_sysid = "100"
4#xt_trader为XtQuant API实例对象
5cancel_result = xt_trader.cancel_order_stock_sysid_async(account, market, order_sysid)

资金划拨

qmt://docs/python
Python
1fund_transfer(account, transfer_direction, price)
  • 释义
    • 资金划拨
  • 参数
    • account - StockAccount 资金账号
    • transfer_direction - int 划拨方向,见数据字典划拨方向(transfer_direction)字段说明
    • price - float 划拨金额
  • 返回
    • (success, msg)
      • success - bool 划拨操作是否成功
      • msg - str 反馈信息

外部交易数据录入

qmt://docs/python
Python
1sync_transaction_from_external(operation, data_type, account, deal_list)
  • 释义

    • 通用数据导出
  • 参数

    • operation - str 操作类型,有"UPDATE","REPLACE","ADD","DELETE"
    • data_type - str 数据类型,有"DEAL"
    • account - StockAccount 资金账号
    • deal_list - list 成交列表,每一项是Deal成交对象的参数字典,键名参考官网数据字典,大小写保持一致
  • 返回

    • result - dict 结果反馈信息
  • 示例

    qmt://docs/python
    Python
    1deal_list = [
    2 {'m_strExchangeID':'SF', 'm_strInstrumentID':'ag2407'
    3 , 'm_strTradeID':'123456', 'm_strOrderSysID':'1234566'
    4 , 'm_dPrice':7600, 'm_nVolume':1
    5 , 'm_strTradeDate': '20240627'
    6 }
    7]
    8resp = xt_trader.sync_transaction_from_external('ADD', 'DEAL', acc, deal_list)
    9print(resp)
    10#成功输出示例:{'msg': 'sync transaction from external success'}
    11#失败输出示例:{'error': {'msg': '[0-0: invalid operation type: ADDD], '}}

股票查询接口

资产查询

qmt://docs/python
Python
1query_stock_asset(account)
  • 释义
    • 查询资金账号对应的资产
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账号对应的资产对象XtAsset或者None
  • 备注
    • 返回None表示查询失败
  • 示例
    • 查询股票资金账号1000000365对应的资产数据
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3asset = xt_trader.query_stock_asset(account)

委托查询

qmt://docs/python
Python
1query_stock_orders(account, cancelable_only = False)
  • 释义
    • 查询资金账号对应的当日所有委托
  • 参数
    • account - StockAccount 资金账号
    • cancelable_only - bool 仅查询可撤委托
  • 返回
    • 该账号对应的当日所有委托对象XtOrder组成的list或者None
  • 备注
    • None表示查询失败或者当日委托列表为空
  • 示例
    • 查询股票资金账号1000000365对应的当日所有委托
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3orders = xt_trader.query_stock_orders(account, False)

成交查询

qmt://docs/python
Python
1query_stock_trades(account)
  • 释义
    • 查询资金账号对应的当日所有成交
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账号对应的当日所有成交对象XtTrade组成的list或者None
  • 备注
    • None表示查询失败或者当日成交列表为空
  • 示例
    • 查询股票资金账号1000000365对应的当日所有成交
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3trades = xt_trader.query_stock_trades(account)

持仓查询

qmt://docs/python
Python
1query_stock_positions(account)
  • 释义
    • 查询资金账号对应的持仓
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账号对应的最新持仓对象XtPosition组成的list或者None
  • 备注
    • None表示查询失败或者当日持仓列表为空
  • 示例
    • 查询股票资金账号1000000365对应的最新持仓
qmt://docs/python
Python
1account = StockAccount('1000000365')
2#xt_trader为XtQuant API实例对象
3positions = xt_trader.query_stock_positions(account)

期货持仓统计查询

qmt://docs/python
Python
1query_position_statistics(account)
  • 释义
    • 查询期货账号的持仓统计
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者当日持仓列表为空
  • 示例
    • 查询期货资金账号1000000365对应的最新持仓
qmt://docs/python
Python
1account = StockAccount('1000000365', 'FUTURE')
2#xt_trader为XtQuant API实例对象
3positions = xt_trader.query_position_statistics(account)

信用查询接口

信用资产查询

qmt://docs/python
Python
1query_credit_detail(account)
  • 释义
    • 查询信用资金账号对应的资产
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该信用账户对应的资产对象XtCreditDetail组成的list或者None
  • 备注
    • None表示查询失败
    • 通常情况下一个资金账号只有一个详细信息数据
  • 示例
    • 查询信用资金账号1208970161对应的资产信息
qmt://docs/python
Python
1account = StockAccount('1208970161', 'CREDIT')
2#xt_trader为XtQuant API实例对象
3datas = xt_trader.query_credit_detail(account)

负债合约查询

qmt://docs/python
Python
1query_stk_compacts(account)
  • 释义
    • 查询资金账号对应的负债合约
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账户对应的负债合约对象StkCompacts组成的list或者None
  • 备注
    • None表示查询失败或者负债合约列表为空
  • 示例
    • 查询信用资金账号1208970161对应的负债合约
qmt://docs/python
Python
1account = StockAccount('1208970161', 'CREDIT')
2#xt_trader为XtQuant API实例对象
3datas = xt_trader.query_stk_compacts(account)

融资融券标的查询

qmt://docs/python
Python
1query_credit_subjects(account)
  • 释义
    • 查询资金账号对应的融资融券标的
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账户对应的融资融券标的对象CreditSubjects组成的list或者None
  • 备注
    • None表示查询失败或者融资融券标的列表为空
  • 示例
    • 查询信用资金账号1208970161对应的融资融券标的
qmt://docs/python
Python
1account = StockAccount('1208970161', 'CREDIT')
2#xt_trader为XtQuant API实例对象
3datas = xt_trader.query_credit_subjects(account)

可融券数据查询

qmt://docs/python
Python
1query_credit_slo_code(account)
  • 释义
    • 查询资金账号对应的可融券数据
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 该账户对应的可融券数据对象CreditSloCode组成的list或者None
  • 备注
    • None表示查询失败或者可融券数据列表为空
  • 示例
    • 查询信用资金账号1208970161对应的可融券数据
qmt://docs/python
Python
1account = StockAccount('1208970161', 'CREDIT')
2#xt_trader为XtQuant API实例对象
3datas = xt_trader.query_credit_slo_code(account)

标的担保品查询

qmt://docs/python
Python
1query_credit_assure(account)
  • 释义
    • 查询资金账号对应的标的担保品
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 该账户对应的标的担保品对象CreditAssure组成的list或者None
  • 备注
    • None表示查询失败或者标的担保品列表为空
  • 示例
    • 查询信用资金账号1208970161对应的标的担保品
qmt://docs/python
Python
1account = StockAccount('1208970161', 'CREDIT')
2#xt_trader为XtQuant API实例对象
3datas = xt_trader.query_credit_assure(account)

其他查询接口

新股申购额度查询

qmt://docs/python
Python
1query_new_purchase_limit(account)
  • 释义
    • 查询新股申购额度
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • dict 新股申购额度数据集
      • { type1: number1, type2: number2, ... }
        • type - str 品种类型
          • KCB - 科创板,SH - 上海,SZ - 深圳
        • number - int 可申购股数
  • 备注
    • 数据仅代表股票申购额度,债券的申购额度固定10000张

当日新股信息查询

qmt://docs/python
Python
1query_ipo_data()
  • 释义

    • 查询当日新股新债信息
  • 参数

  • 返回

    • dict 新股新债信息数据集

      • { stock1: info1, stock2: info2, ... }

        • stock - str 品种代码,例如 '301208.SZ'
        • info - dict 新股信息
          • name - str 品种名称
          • type - str 品种类型
            • STOCK - 股票,BOND - 债券
          • minPurchaseNum / maxPurchaseNum - int 最小 / 最大申购额度
            • 单位为股(股票)/ 张(债券)
          • purchaseDate - str 申购日期
          • issuePrice - float 发行价
      • 返回值示例

        qmt://docs/python
        Python
        1{'754810.SH': {'name': '丰山发债', 'type': 'BOND', 'maxPurchaseNum': 10000, 'minPurchaseNum': 10, 'purchaseDate': '20220627', 'issuePrice': 100.0}, '301208.SZ': {'name': '中亦科技', 'type': 'STOCK', 'maxPurchaseNum': 16500, 'minPurchaseNum': 500, 'purchaseDate': '20220627', 'issuePrice': 46.06}}
  • 备注

账号信息查询

qmt://docs/python
Python
1query_account_infos()
  • 释义

    • 查询所有资金账号
  • 参数

  • 返回

    • list 账号信息列表

      • [ XtAccountInfo ]
  • 备注

账号状态查询

qmt://docs/python
Python
1query_account_status()
  • 释义

    • 查询所有账号状态
  • 参数

  • 返回

    • list 账号状态列表

      • [ XtAccountStatus ]
  • 备注

普通柜台资金查询

qmt://docs/python
Python
1query_com_fund(account)
  • 释义
    • 划拨业务查询普通柜台的资金
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • result - dict 资金信息,包含以下字段
      • success - bool
      • erro - str
      • currentBalance - double 当前余额
      • enableBalance - double 可用余额
      • fetchBalance - double 可取金额
      • interest - double 待入账利息
      • assetBalance - double 总资产
      • fetchCash - double 可取现金
      • marketValue - double 市值
      • debt - double 负债

普通柜台持仓查询

qmt://docs/python
Python
1query_com_position(account)
  • 释义
    • 划拨业务查询普通柜台的持仓
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • result - list 持仓信息列表[position1, position2, ...]
      • position - dict 持仓信息,包含以下字段
        • success - bool
        • error - str
        • stockAccount - str 股东号
        • exchangeType - str 交易市场
        • stockCode - str 证券代码
        • stockName - str 证券名称
        • totalAmt - float 总量
        • enableAmount - float 可用量
        • lastPrice - float 最新价
        • costPrice - float 成本价
        • income - float 盈亏
        • incomeRate - float 盈亏比例
        • marketValue - float 市值
        • costBalance - float 成本总额
        • bsOnTheWayVol - int 买卖在途量
        • prEnableVol - int 申赎可用量

通用数据导出

qmt://docs/python
Python
1export_data(account, result_path, data_type, start_time = None, end_time = None, user_param = {})
  • 释义

    • 通用数据导出
  • 参数

    • account - StockAccount 资金账号
    • result_path - str 导出路径,包含文件名及.csv后缀,如'C:\Users\Desktop\test\deal.csv'
    • data_type - str 数据类型,如'deal'
    • start_time - str 开始时间(可缺省)
    • end_time - str 结束时间(可缺省)
    • user_param - dict 用户参数(可缺省)
  • 返回

    • result - dict 结果反馈信息
  • 示例

    qmt://docs/python
    Python
    1resp = xt_trader.export_data(acc, 'C:\\Users\\Desktop\\test\\deal.csv', 'deal')
    2print(resp)
    3#成功输出示例:{'msg': 'export success'}
    4#失败输出示例:{'error': {'errorMsg': 'can not find account info, accountID:2000449 accountType:2'}}

通用数据查询

qmt://docs/python
Python
1query_data(account, result_path, data_type, start_time = None, end_time = None, user_param = {})
  • 释义

    • 通用数据查询,利用export_data接口导出数据后再读取其中的数据内容,读取完毕后删除导出的文件
  • 参数

    同export_data

  • 返回

    • result - dict 数据信息
  • 示例

    qmt://docs/python
    Python
    1data = xt_trader.query_data(acc, 'C:\\Users\\Desktop\\test\\deal.csv', 'deal')
    2print(data)
    3#成功输出示例:
    4# account_id account_Type stock_code order_type ...
    5#0 2003695 2 688488.SH 23 ...
    6#1 2003695 2 000096.SZ 23 ...
    7#失败输出示例:{'error': {'errorMsg': 'can not find account info, accountID:2000449 accountType:2'}}

约券相关接口

券源行情查询

qmt://docs/python
Python
1smt_query_quoter(account)
  • 释义
    • 券源行情查询
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • result - list 券源信息列表[quoter1, quoter2, ...]
      • quoter - dict 券源信息,包含以下字段
        • success - bool
        • error - str
        • finType - str 金融品种
        • stockType - str 证券类型
        • date - int 期限天数
        • code - str 证券代码
        • codeName - str 证券代码名称
        • exchangeType - str 市场
        • fsmpOccupedRate - float 资券占用利率
        • fineRate - float 罚息利率
        • fsmpreendRate - float 资券提前归还利率
        • usedRate - float 资券使用利率
        • unUusedRate - float 资券占用未使用利率
        • initDate - int 交易日期
        • endDate - int 到期日期
        • enableSloAmountT0 - float T+0可融券数量
        • enableSloAmountT3 - float T+3可融券数量
        • srcGroupId - str 来源组编号
        • applyMode - str 资券申请方式,"1":库存券,"2":专项券
        • lowDate - int 最低期限天数

库存券约券申请

qmt://docs/python
Python
1smt_negotiate_order_async(self, account, src_group_id, order_code, date, amount, apply_rate, dict_param={})
  • 释义

    • 库存券约券申请的异步接口,异步接口如果正常返回了请求序号seq,会收到on_smt_appointment_async_response的反馈
  • 参数

    • account - StockAccount 资金账号
    • src_group_id - str 来源组编号
    • order_code - str 证券代码,如'600000.SH'
    • date - int 期限天数
    • amount - int 委托数量
    • apply_rate - float 资券申请利率

    注:目前有如下参数通过一个可缺省的字典传递,键名与参数名称相同

    • dict_param - dict 可缺省的字典参数
      • subFareRate - float 提前归还利率
      • fineRate - float 罚息利率
  • 返回

    • 返回请求序号seq,成功发起申请后的请求序号为大于0的正整数,如果为-1表示发起申请失败
  • 示例

qmt://docs/python
Python
1account = StockAccount('1000008', 'CREDIT')
2dict_param = {'subFareRate':0.1, 'fineRate':0.1}
3#xt_trader为XtQuant API实例对象
4seq = xt_trader.smt_negotiate_order_async(account, '', '000001.SZ', 7, 100, 0.2, dict_param)

约券合约查询

qmt://docs/python
Python
1smt_query_compact(account)
  • 释义
    • 约券合约查询
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • result - list 约券合约信息列表[compact1, compact2, ...]
      • compact - dict 券源信息,包含以下字段
        • success - bool
        • error - str
        • createDate - int 创建日期
        • cashcompactId - str 头寸合约编号
        • oriCashcompactId - str 原头寸合约编号
        • applyId - str 资券申请编号
        • srcGroupId - str 来源组编号
        • comGroupId - str 资券组合编号
        • finType - str 金融品种
        • exchangeType - str 市场
        • code - str 证券代码
        • codeName - str 证券代码名称
        • date - int 期限天数
        • beginCompacAmount - float 期初合约数量
        • beginCompacBalance - float 期初合约金额
        • compacAmount - float 合约数量
        • compacBalance - float 合约金额
        • returnAmount - float 返还数量
        • returnBalance - float 返还金额
        • realBuyAmount - float 回报买入数量
        • fsmpOccupedRate - float 资券占用利率
        • compactInterest - float 合约利息金额
        • compactFineInterest - float 合约罚息金额
        • repaidInterest - float 已还利息
        • repaidFineInterest - float 归还罚息
        • fineRate - float 罚息利率
        • preendRate - float 资券提前归还利率
        • compactType - str 资券合约类型
        • postponeTimes - int 展期次数
        • compactStatus - str 资券合约状态,"0":未归还,"1":部分归还,"2":提前了结,"3":到期了结,"4":逾期了结,"5":逾期,"9":已作废
        • lastInterestDate - int 上次结息日期
        • interestEndDate - int 记息结束日期
        • validDate - int 有效日期
        • dateClear - int 清算日期
        • usedAmount - float 已使用数量
        • usedBalance - float 使用金额
        • usedRate - float 资券使用利率
        • unUusedRate - float 资券占用未使用利率
        • srcGroupName - str 来源组名称
        • repaidDate - int 归还日期
        • preOccupedInterest - float 当日实际应收利息
        • compactInterestx - float 合约总利息
        • enPostponeAmount - float 可展期数量
        • postponeStatus - str 合约展期状态,"0":未审核,"1":审核通过,"2":已撤销,"3":审核不通过
        • applyMode - str 资券申请方式,"1":库存券,"2":专项券

回调类

qmt://docs/python
Python
1class MyXtQuantTraderCallback(XtQuantTraderCallback):
2 def on_disconnected(self):
3 """
4 连接状态回调
5 :return:
6 """
7 print("connection lost")
8 def on_account_status(self, status):
9 """
10 账号状态信息推送
11 :param response: XtAccountStatus 对象
12 :return:
13 """
14 print("on_account_status")
15 print(status.account_id, status.account_type, status.status)
16 def on_stock_order(self, order):
17 """
18 委托信息推送
19 :param order: XtOrder对象
20 :return:
21 """
22 print("on order callback:")
23 print(order.stock_code, order.order_status, order.order_sysid)
24 def on_stock_trade(self, trade):
25 """
26 成交信息推送
27 :param trade: XtTrade对象
28 :return:
29 """
30 print("on trade callback")
31 print(trade.account_id, trade.stock_code, trade.order_id)
32 def on_order_error(self, order_error):
33 """
34 下单失败信息推送
35 :param order_error:XtOrderError 对象
36 :return:
37 """
38 print("on order_error callback")
39 print(order_error.order_id, order_error.error_id, order_error.error_msg)
40 def on_cancel_error(self, cancel_error):
41 """
42 撤单失败信息推送
43 :param cancel_error: XtCancelError 对象
44 :return:
45 """
46 print("on cancel_error callback")
47 print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
48 def on_order_stock_async_response(self, response):
49 """
50 异步下单回报推送
51 :param response: XtOrderResponse 对象
52 :return:
53 """
54 print("on_order_stock_async_response")
55 print(response.account_id, response.order_id, response.seq)
56 def on_smt_appointment_async_response(self, response):
57 """
58 :param response: XtAppointmentResponse 对象
59 :return:
60 """
61 print("on_smt_appointment_async_response")
62 print(response.account_id, response.order_sysid, response.error_id, response.error_msg, response.seq)

连接状态回调

qmt://docs/python
Python
1on_disconnected()
  • 释义
    • 失去连接时推送信息
  • 参数
  • 返回
  • 备注

账号状态信息推送

qmt://docs/python
Python
1on_account_status(data)
  • 释义
    • 账号状态信息变动推送
  • 参数
  • 返回
  • 备注

委托信息推送

qmt://docs/python
Python
1on_stock_order(data)
  • 释义
    • 委托信息变动推送,例如已成交数量,委托状态变化等
  • 参数
  • 返回
  • 备注

成交信息推送

qmt://docs/python
Python
1on_stock_trade(data)
  • 释义
    • 成交信息变动推送
  • 参数
  • 返回
  • 备注

下单失败信息推送

qmt://docs/python
Python
1on_order_error(data)
  • 释义
    • 下单失败信息推送
  • 参数
  • 返回
  • 备注

撤单失败信息推送

qmt://docs/python
Python
1on_cancel_error(data)
  • 释义
    • 撤单失败信息的推送
  • 参数
  • 返回
  • 备注

异步下单回报推送

qmt://docs/python
Python
1on_order_stock_async_response(data)
  • 释义
    • 异步下单回报推送
  • 参数
  • 返回
  • 备注

约券相关异步接口的回报推送

qmt://docs/python
Python
1on_smt_appointment_async_response(data)
  • 释义
    • 异步约券相关接口回报推送
  • 参数
  • 返回
  • 备注

智能助手

咨询式 AI · 带入当前文档

智能助手加载中...