XtQuant是基于迅投MiniQMT衍生出来的一套完善的Python策略运行框架,对外以Python库的形式提供策略交易所需要的行情和交易相关的API接口。
XtQuant目前提供的库包括Python3.6、3.7、3.8版本,不同版本的python导入时会自动切换。
在运行使用XtQuant的程序前需要先启动MiniQMT客户端。
2020-09-01
2020-10-14
2020-10-21
2020-11-13
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
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
2024-02-29
query_position_statistics
2024-04-25
stock_code1
字段以适配长代码2024-05-24
2024-06-27
2024-08-27
XtTrade
新增手续费字段commission
- 手续费#coding=utf-8
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xttype import StockAccount
from xtquant import xtconstant
class MyXtQuantTraderCallback(XtQuantTraderCallback):
def on_disconnected(self):
"""
连接断开
:return:
"""
print("connection lost")
def on_stock_order(self, order):
"""
委托回报推送
:param order: XtOrder对象
:return:
"""
print("on order callback:")
print(order.stock_code, order.order_status, order.order_sysid)
def on_stock_asset(self, asset):
"""
资金变动推送
:param asset: XtAsset对象
:return:
"""
print("on asset callback")
print(asset.account_id, asset.cash, asset.total_asset)
def on_stock_trade(self, trade):
"""
成交变动推送
:param trade: XtTrade对象
:return:
"""
print("on trade callback")
print(trade.account_id, trade.stock_code, trade.order_id)
def on_stock_position(self, position):
"""
持仓变动推送
:param position: XtPosition对象
:return:
"""
print("on position callback")
print(position.stock_code, position.volume)
def on_order_error(self, order_error):
"""
委托失败推送
:param order_error:XtOrderError 对象
:return:
"""
print("on order_error callback")
print(order_error.order_id, order_error.error_id, order_error.error_msg)
def on_cancel_error(self, cancel_error):
"""
撤单失败推送
:param cancel_error: XtCancelError 对象
:return:
"""
print("on cancel_error callback")
print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
def on_order_stock_async_response(self, response):
"""
异步下单回报推送
:param response: XtOrderResponse 对象
:return:
"""
print("on_order_stock_async_response")
print(response.account_id, response.order_id, response.seq)
def on_account_status(self, status):
"""
:param response: XtAccountStatus 对象
:return:
"""
print("on_account_status")
print(status.account_id, status.account_type, status.status)
if __name__ == "__main__":
print("demo test")
# path为mini qmt客户端安装目录下userdata_mini路径
path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
# session_id为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
session_id = 123456
xt_trader = XtQuantTrader(path, session_id)
# 创建资金账号为1000000365的证券账号对象
acc = StockAccount('1000000365')
# StockAccount可以用第二个参数指定账号类型,如沪港通传'HUGANGTONG',深港通传'SHENGANGTONG'
# acc = StockAccount('1000000365','STOCK')
# 创建交易回调类对象,并声明接收回调
callback = MyXtQuantTraderCallback()
xt_trader.register_callback(callback)
# 启动交易线程
xt_trader.start()
# 建立交易连接,返回0表示连接成功
connect_result = xt_trader.connect()
print(connect_result)
# 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
subscribe_result = xt_trader.subscribe(acc)
print(subscribe_result)
stock_code = '600000.SH'
# 使用指定价下单,接口返回订单编号,后续可以用于撤单操作以及查询委托状态
print("order using the fix price:")
fix_result_order_id = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5, 'strategy_name', 'remark')
print(fix_result_order_id)
# 使用订单编号撤单
print("cancel order:")
cancel_order_result = xt_trader.cancel_order_stock(acc, fix_result_order_id)
print(cancel_order_result)
# 使用异步下单接口,接口返回下单请求序号seq,seq可以和on_order_stock_async_response的委托反馈response对应起来
print("order using async api:")
async_seq = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5, 'strategy_name', 'remark')
print(async_seq)
# 查询证券资产
print("query asset:")
asset = xt_trader.query_stock_asset(acc)
if asset:
print("asset:")
print("cash {0}".format(asset.cash))
# 根据订单编号查询委托
print("query order:")
order = xt_trader.query_stock_order(acc, fix_result_order_id)
if order:
print("order:")
print("order {0}".format(order.order_id))
# 查询当日所有的委托
print("query orders:")
orders = xt_trader.query_stock_orders(acc)
print("orders:", len(orders))
if len(orders) != 0:
print("last order:")
print("{0} {1} {2}".format(orders[-1].stock_code, orders[-1].order_volume, orders[-1].price))
# 查询当日所有的成交
print("query trade:")
trades = xt_trader.query_stock_trades(acc)
print("trades:", len(trades))
if len(trades) != 0:
print("last trade:")
print("{0} {1} {2}".format(trades[-1].stock_code, trades[-1].traded_volume, trades[-1].traded_price))
# 查询当日所有的持仓
print("query positions:")
positions = xt_trader.query_stock_positions(acc)
print("positions:", len(positions))
if len(positions) != 0:
print("last position:")
print("{0} {1} {2}".format(positions[-1].account_id, positions[-1].stock_code, positions[-1].volume))
# 根据股票代码查询对应持仓
print("query position:")
position = xt_trader.query_stock_position(acc, stock_code)
if position:
print("position:")
print("{0} {1} {2}".format(position.account_id, position.stock_code, position.volume))
# 阻塞线程,接收交易推送
xt_trader.run_forever()
XtQuant封装了策略交易所需要的Python API接口,可以和MiniQMT客户端交互进行报单、撤单、查询资产、查询委托、查询成交、查询持仓以及收到资金、委托、成交和持仓等变动的主推消息。
xtconstant.SH_MARKET
xtconstant.SZ_MARKET
xtconstant.MARKET_ENUM_BEIJING
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
xtconstant.MARKET_SHANGHAI
xtconstant.MARKET_SHENZHEN
xtconstant.MARKET_BEIJING
xtconstant.MARKET_SHANGHAI_FUTURE
xtconstant.MARKET_DALIANG_FUTURE
xtconstant.MARKET_ZHENGZHOU_FUTURE
xtconstant.MARKET_INDEX_FUTURE
xtconstant.MARKET_INTL_ENERGY_FUTURE
xtconstant.MARKET_GUANGZHOU_FUTURE
xtconstant.MARKET_SHANGHAI_STOCK_OPTION
xtconstant.MARKET_SHENZHEN_STOCK_OPTION
xtconstant.FUTURE_ACCOUNT
xtconstant.SECURITY_ACCOUNT
xtconstant.CREDIT_ACCOUNT
xtconstant.FUTURE_OPTION_ACCOUNT
xtconstant.STOCK_OPTION_ACCOUNT
xtconstant.HUGANGTONG_ACCOUNT
xtconstant.SHENGANGTONG_ACCOUNT
股票
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_ETFBUY
xtconstant.ETF_ETFSELL
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
枚举变量名 | 值 | 含义 |
---|---|---|
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 | 未知 |
枚举变量名 | 值 | 含义 |
---|---|---|
xtconstant.ACCOUNT_STATUS_INVALID | -1 | 无效 |
xtconstant.ACCOUNT_STATUS_OK | 0 | 正常 |
xtconstant.ACCOUNT_STATUS_WAITING_LOGIN | 1 | 连接中 |
xtconstant.ACCOUNT_STATUSING | 2 | 登陆中 |
xtconstant.ACCOUNT_STATUS_FAIL | 3 | 失败 |
xtconstant.ACCOUNT_STATUS_INITING | 4 | 初始化中 |
xtconstant.ACCOUNT_STATUS_CORRECTING | 5 | 数据刷新校正中 |
xtconstant.ACCOUNT_STATUS_CLOSED | 6 | 收盘后 |
xtconstant.ACCOUNT_STATUS_ASSIS_FAIL | 7 | 穿透副链接断开 |
xtconstant.ACCOUNT_STATUS_DISABLEBYSYS | 8 | 系统停用(总线使用-密码错误超限) |
xtconstant.ACCOUNT_STATUS_DISABLEBYUSER | 9 | 用户停用(总线使用) |
枚举变量名 | 值 | 含义 |
---|---|---|
xtconstant.FUNDS_TRANSFER_NORMAL_TO_SPEED | 510 | 资金划拨-普通柜台到极速柜台 |
xtconstant.FUNDS_TRANSFER_SPEED_TO_NORMAL | 511 | 资金划拨-极速柜台到普通柜台 |
xtconstant.NODE_FUNDS_TRANSFER_SH_TO_SZ | 512 | 节点资金划拨-上海节点到深圳节点 |
xtconstant.NODE_FUNDS_TRANSFER_SZ_TO_SH | 513 | 节点资金划拨-深圳节点到上海节点 |
枚举变量名 | 值 | 含义 |
---|---|---|
xtconstant.DIRECTION_FLAG_LONG | 48 | 多 |
xtconstant.DIRECTION_FLAG_SHORT | 49 | 空 |
枚举变量名 | 值 | 含义 |
---|---|---|
xtconstant.OFFSET_FLAG_OPEN | 48 | 买入,开仓 |
xtconstant.OFFSET_FLAG_CLOSE | 49 | 卖出,平仓 |
xtconstant.OFFSET_FLAG_FORCECLOSE | 50 | 强平 |
xtconstant.OFFSET_FLAG_CLOSETODAY | 51 | 平今 |
xtconstant.OFFSET_FLAG_ClOSEYESTERDAY | 52 | 平昨 |
xtconstant.OFFSET_FLAG_FORCEOFF | 53 | 强减 |
xtconstant.OFFSET_FLAG_LOCALFORCECLOSE | 54 | 本地强平 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
cash | float | 可用金额 |
frozen_cash | float | 冻结金额 |
market_value | float | 持仓市值 |
total_asset | float | 总资产 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
stock_code | str | 证券代码,例如”600000.SH” |
order_id | int | 订单编号 |
order_sysid | str | 柜台合同编号 |
order_time | int | 报单时间 |
order_type | int | 委托类型,参见数据字典 |
order_volume | int | 委托数量 |
price_type | int | 报价类型,参见数据字典 |
price | float | 委托价格 |
traded_volume | int | 成交数量 |
traded_price | float | 成交均价 |
order_status | int | 委托状态,参见数据字典 |
status_msg | str | 委托状态描述,如废单原因 |
strategy_name | str | 策略名称 |
order_remark | str | 委托备注 |
direction | int | 多空方向,股票不需要;参见数据字典 |
offset_flag | int | 交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等;参见数据字典 |
stock_code1 | str | 证券代码,例如”600000.SH” |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
stock_code | str | 证券代码 |
order_type | int | 委托类型,参见数据字典 |
traded_id | str | 成交编号 |
traded_time | int | 成交时间 |
traded_price | float | 成交均价 |
traded_volume | int | 成交数量 |
traded_amount | float | 成交金额 |
order_id | int | 订单编号 |
order_sysid | str | 柜台合同编号 |
strategy_name | str | 策略名称 |
order_remark | str | 委托备注 |
direction | int | 多空方向,股票不需要;参见数据字典 |
offset_flag | int | 交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等;参见数据字典 |
stock_code1 | str | 证券代码,例如”600000.SH” |
commission | float | 手续费 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
stock_code | str | 证券代码 |
volume | int | 持仓数量 |
can_use_volume | int | 可用数量 |
open_price | float | 开仓价 |
market_value | float | 市值 |
frozen_volume | int | 冻结数量 |
on_road_volume | int | 在途股份 |
yesterday_volume | int | 昨夜拥股 |
avg_price | float | 成本价 |
direction | int | 多空方向,股票不需要;参见数据字典 |
stock_code1 | str | 证券代码,例如”600000.SH” |
属性 | 类型 | 注释 |
---|---|---|
account_id |
string |
账户 |
exchange_id |
string |
市场代码 |
exchange_name |
string |
市场名称 |
product_id |
string |
品种代码 |
instrument_id |
string |
合约代码 |
instrument_name |
string |
合约名称 |
direction |
int |
多空 |
hedge_flag |
int |
投保 |
position |
int |
持仓 |
yesterday_position |
int |
昨仓 |
today_position |
int |
今仓 |
can_close_vol |
int |
可平 |
position_cost |
float |
持仓成本 |
avg_price |
float |
持仓均价 |
position_profit |
float |
持仓盈亏 |
float_profit |
float |
浮动盈亏 |
open_price |
float |
开仓均价 |
used_margin |
float |
已使用保证金 |
used_commission |
float |
已使用的手续费 |
frozen_margin |
float |
冻结保证金 |
frozen_commission |
float |
冻结手续费 |
instrument_value |
float |
市值,合约价值 |
open_times |
int |
开仓次数 |
open_volume |
int |
总开仓量 中间平仓不减 |
cancel_times |
int |
撤单次数 |
last_price |
float |
最新价 |
rise_ratio |
float |
当日涨幅 |
product_name |
string |
产品名称 |
royalty |
float |
权利金市值 |
expire_date |
string |
到期日 |
assest_weight |
float |
资产占比 |
increase_by_settlement |
float |
当日涨幅(结) |
margin_ratio |
float |
保证金占比 |
float_profit_divide_by_used_margin |
float |
浮盈比例(保证金) |
float_profit_divide_by_balance |
float |
浮盈比例(动态权益) |
today_profit_loss |
float |
当日盈亏(结) |
yesterday_init_position |
int |
昨日持仓 |
frozen_royalty |
float |
冻结权利金 |
today_close_profit_loss |
float |
当日盈亏(收) |
close_profit |
float |
平仓盈亏 |
ft_product_name |
string |
品种名称 |
open_cost |
float |
开仓成本 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
order_id | int | 订单编号 |
strategy_name | str | 策略名称 |
order_remark | str | 委托备注 |
seq | int | 异步下单的请求序号 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
order_id | int | 订单编号 |
order_sysid | str | 柜台委托编号 |
cancel_result | int | 撤单结果 |
seq | int | 异步撤单的请求序号 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
order_id | int | 订单编号 |
error_id | int | 下单失败错误码 |
error_msg | str | 下单失败具体信息 |
strategy_name | str | 策略名称 |
order_remark | str | 委托备注 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
order_id | int | 订单编号 |
market | int | 交易市场 0:上海 1:深圳 |
order_sysid | str | 柜台委托编号 |
error_id | int | 下单失败错误码 |
error_msg | str | 下单失败具体信息 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
m_nStatus | int | 账号状态 |
m_nUpdateTime | int | 更新时间 |
m_nCalcConfig | int | 计算参数 |
m_dFrozenCash | float | 冻结金额 |
m_dBalance | float | 总资产 |
m_dAvailable | float | 可用金额 |
m_dPositionProfit | float | 持仓盈亏 |
m_dMarketValue | float | 总市值 |
m_dFetchBalance | float | 可取金额 |
m_dStockValue | float | 股票市值 |
m_dFundValue | float | 基金市值 |
m_dTotalDebt | float | 总负债 |
m_dEnableBailBalance | float | 可用保证金 |
m_dPerAssurescaleValue | float | 维持担保比例 |
m_dAssureAsset | float | 净资产 |
m_dFinDebt | float | 融资负债 |
m_dFinDealAvl | float | 融资本金 |
m_dFinFee | float | 融资息费 |
m_dSloDebt | float | 融券负债 |
m_dSloMarketValue | float | 融券市值 |
m_dSloFee | float | 融券息费 |
m_dOtherFare | float | 其它费用 |
m_dFinMaxQuota | float | 融资授信额度 |
m_dFinEnableQuota | float | 融资可用额度 |
m_dFinUsedQuota | float | 融资冻结额度 |
m_dSloMaxQuota | float | 融券授信额度 |
m_dSloEnableQuota | float | 融券可用额度 |
m_dSloUsedQuota | float | 融券冻结额度 |
m_dSloSellBalance | float | 融券卖出资金 |
m_dUsedSloSellBalance | float | 已用融券卖出资金 |
m_dSurplusSloSellBalance | float | 剩余融券卖出资金 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
compact_type | int | 合约类型 |
cashgroup_prop | int | 头寸来源 |
exchange_id | int | 证券市场 |
open_date | int | 开仓日期 |
business_vol | int | 合约证券数量 |
real_compact_vol | int | 未还合约数量 |
ret_end_date | int | 到期日 |
business_balance | float | 合约金额 |
businessFare | float | 合约息费 |
real_compact_balance | float | 未还合约金额 |
real_compact_fare | float | 未还合约息费 |
repaid_fare | float | 已还息费 |
repaid_balance | float | 已还金额 |
instrument_id | str | 证券代码 |
compact_id | str | 合约编号 |
position_str | str | 定位串 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
slo_status | int | 融券状态 |
fin_status | int | 融资状态 |
exchange_id | int | 证券市场 |
slo_ratio | float | 融券保证金比例 |
fin_ratio | float | 融资保证金比例 |
instrument_id | str | 证券代码 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
cashgroup_prop | int | 头寸来源 |
exchange_id | int | 证券市场 |
enable_amount | int | 融券可融数量 |
instrument_id | str | 证券代码 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
assure_status | int | 是否可做担保 |
exchange_id | int | 证券市场 |
assure_ratio | float | 担保品折算比例 |
instrument_id | str | 证券代码 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
status | int | 账号状态,参见数据字典 |
属性 | 类型 | 注释 |
---|---|---|
account_type | int | 账号类型,参见数据字典 |
account_id | str | 资金账号 |
broker_type | int | 同 account_type |
platform_id | int | 平台号 |
account_classification | int | 账号分类 |
login_status | int | 账号状态,参见数据字典 |
属性 | 类型 | 注释 |
---|---|---|
seq | int | 异步请求序号 |
success | bool | 申请是否成功 |
msg | str | 反馈信息 |
apply_id | str | 若申请成功返回资券申请编号,否则返回-1 |
XtQuantTrader(path, session_id)
path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
# session_id为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
session_id = 123456
#后续的所有示例将使用该实例对象
xt_trader = XtQuantTrader(path, session_id)
register_callback(callback)
# 创建交易回调类对象,并声明接收回调
class MyXtQuantTraderCallback(XtQuantTraderCallback):
...
pass
callback = MyXtQuantTraderCallback()
#xt_trader为XtQuant API实例对象
xt_trader.register_callback(callback)
start()
# 启动交易线程
#xt_trader为XtQuant API实例对象
xt_trader.start()
connect()
# 建立交易连接,返回0表示连接成功
#xt_trader为XtQuant API实例对象
connect_result = xt_trader.connect()
print(connect_result)
stop()
#xt_trader为XtQuant API实例对象
xt_trader.stop()
run_forever()
#xt_trader为XtQuant API实例对象
xt_trader.run_forever()
set_relaxed_response_order_enabled(enabled)
释义
参数
返回
备注
如果开启,在on_stock_order等推送回调中调用同步请求不会卡住,但查询和推送的数据在时序上会变得不确定
timeline t1 t2 t3 t4
callback push1 push2 push3 resp4
do 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
subscribe(account)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
subscribe_result = xt_trader.subscribe(account)
unsubscribe(account)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
unsubscribe_result = xt_trader.unsubscribe(account)
order_stock(account, stock_code, order_type, order_volume, price_type, price, strategy_name, order_remark)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
order_id = xt_trader.order_stock(account, '600000.SH', xtconstant.STOCK_BUY, 1000, xtconstant.FIX_PRICE, 10.5, 'strategy1', 'order_test')
order_stock_async(account, stock_code, order_type, order_volume, price_type, price, strategy_name, order_remark)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
seq = xt_trader.order_stock_async(account, '600000.SH', xtconstant.STOCK_BUY, 1000, xtconstant.FIX_PRICE, 10.5, 'strategy1', 'order_test')
cancel_order_stock(account, order_id)
account = StockAccount('1000000365')
order_id = 100
#xt_trader为XtQuant API实例对象
cancel_result = xt_trader.cancel_order_stock(account, order_id)
cancel_order_stock_sysid(account, market, order_sysid)
account = StockAccount('1000000365')
market = xtconstant.SH_MARKET
order_sysid = "100"
#xt_trader为XtQuant API实例对象
cancel_result = xt_trader.cancel_order_stock_sysid(account, market, order_sysid)
cancel_order_stock_async(account, order_id)
account = StockAccount('1000000365')
order_id = 100
#xt_trader为XtQuant API实例对象
cancel_result = xt_trader.cancel_order_stock_async(account, order_id)
cancel_order_stock_sysid_async(account, market, order_sysid)
account = StockAccount('1000000365')
market = xtconstant.SH_MARKET
order_sysid = "100"
#xt_trader为XtQuant API实例对象
cancel_result = xt_trader.cancel_order_stock_sysid_async(account, market, order_sysid)
fund_transfer(account, transfer_direction, price)
sync_transaction_from_external(operation, data_type, account, deal_list)
释义
参数
返回
示例
deal_list = [
{'m_strExchangeID':'SF', 'm_strInstrumentID':'ag2407'
, 'm_strTradeID':'123456', 'm_strOrderSysID':'1234566'
, 'm_dPrice':7600, 'm_nVolume':1
, 'm_strTradeDate': '20240627'
}
]
resp = xt_trader.sync_transaction_from_external('ADD', 'DEAL', acc, deal_list)
print(resp)
#成功输出示例:{'msg': 'sync transaction from external success'}
#失败输出示例:{'error': {'msg': '[0-0: invalid operation type: ADDD], '}}
query_stock_asset(account)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
asset = xt_trader.query_stock_asset(account)
query_stock_orders(account, cancelable_only = False)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
orders = xt_trader.query_stock_orders(account, False)
query_stock_trades(account)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
trades = xt_trader.query_stock_trades(account)
query_stock_positions(account)
account = StockAccount('1000000365')
#xt_trader为XtQuant API实例对象
positions = xt_trader.query_stock_positions(account)
query_position_statistics(account)
account = StockAccount('1000000365', 'FUTURE')
#xt_trader为XtQuant API实例对象
positions = xt_trader.query_position_statistics(account)
query_credit_detail(account)
account = StockAccount('1208970161', 'CREDIT')
#xt_trader为XtQuant API实例对象
datas = xt_trader.query_credit_detail(account)
query_stk_compacts(account)
account = StockAccount('1208970161', 'CREDIT')
#xt_trader为XtQuant API实例对象
datas = xt_trader.query_stk_compacts(account)
query_credit_subjects(account)
account = StockAccount('1208970161', 'CREDIT')
#xt_trader为XtQuant API实例对象
datas = xt_trader.query_credit_subjects(account)
query_credit_slo_code(account)
account = StockAccount('1208970161', 'CREDIT')
#xt_trader为XtQuant API实例对象
datas = xt_trader.query_credit_slo_code(account)
query_credit_assure(account)
account = StockAccount('1208970161', 'CREDIT')
#xt_trader为XtQuant API实例对象
datas = xt_trader.query_credit_assure(account)
query_new_purchase_limit(account)
KCB
- 科创板,SH
- 上海,SZ
- 深圳query_ipo_data()
释义
参数
返回
dict 新股新债信息数据集
{ stock1: info1, stock2: info2, … }
STOCK
- 股票,BOND
- 债券返回值示例
{'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}}
备注
query_account_infos()
释义
参数
返回
list 账号信息列表
备注
query_com_fund(account)
query_com_position(account)
export_data(account, result_path, data_type, start_time = None, end_time = None, user_param = {})
释义
参数
返回
示例
resp = xt_trader.export_data(acc, 'C:\\Users\\Desktop\\test\\deal.csv', 'deal')
print(resp)
#成功输出示例:{'msg': 'export success'}
#失败输出示例:{'error': {'errorMsg': 'can not find account info, accountID:2000449 accountType:2'}}
query_data(account, result_path, data_type, start_time = None, end_time = None, user_param = {})
释义
参数
同export_data
返回
示例
data = xt_trader.query_data(acc, 'C:\\Users\\Desktop\\test\\deal.csv', 'deal')
print(data)
#成功输出示例:
# account_id account_Type stock_code order_type ...
#0 2003695 2 688488.SH 23 ...
#1 2003695 2 000096.SZ 23 ...
#失败输出示例:{'error': {'errorMsg': 'can not find account info, accountID:2000449 accountType:2'}}
smt_query_quoter(account)
smt_negotiate_order_async(self, account, src_group_id, order_code, date, amount, apply_rate, dict_param={})
释义
参数
注:目前有如下参数通过一个可缺省的字典传递,键名与参数名称相同
返回
示例
account = StockAccount('1000008', 'CREDIT')
dict_param = {'subFareRate':0.1, 'fineRate':0.1}
#xt_trader为XtQuant API实例对象
seq = xt_trader.smt_negotiate_order_async(account, '', '000001.SZ', 7, 100, 0.2, dict_param)
smt_query_compact(account)
class MyXtQuantTraderCallback(XtQuantTraderCallback):
def on_disconnected(self):
"""
连接状态回调
:return:
"""
print("connection lost")
def on_account_status(self, status):
"""
账号状态信息推送
:param response: XtAccountStatus 对象
:return:
"""
print("on_account_status")
print(status.account_id, status.account_type, status.status)
def on_stock_asset(self, asset):
"""
资金信息推送
:param asset: XtAsset对象
:return:
"""
print("on asset callback")
print(asset.account_id, asset.cash, asset.total_asset)
def on_stock_order(self, order):
"""
委托信息推送
:param order: XtOrder对象
:return:
"""
print("on order callback:")
print(order.stock_code, order.order_status, order.order_sysid)
def on_stock_trade(self, trade):
"""
成交信息推送
:param trade: XtTrade对象
:return:
"""
print("on trade callback")
print(trade.account_id, trade.stock_code, trade.order_id)
def on_stock_position(self, position):
"""
持仓信息推送
:param position: XtPosition对象
:return:
"""
print("on position callback")
print(position.stock_code, position.volume)
def on_order_error(self, order_error):
"""
下单失败信息推送
:param order_error:XtOrderError 对象
:return:
"""
print("on order_error callback")
print(order_error.order_id, order_error.error_id, order_error.error_msg)
def on_cancel_error(self, cancel_error):
"""
撤单失败信息推送
:param cancel_error: XtCancelError 对象
:return:
"""
print("on cancel_error callback")
print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
def on_order_stock_async_response(self, response):
"""
异步下单回报推送
:param response: XtOrderResponse 对象
:return:
"""
print("on_order_stock_async_response")
print(response.account_id, response.order_id, response.seq)
def on_smt_appointment_async_response(self, response):
"""
:param response: XtAppointmentResponse 对象
:return:
"""
print("on_smt_appointment_async_response")
print(response.account_id, response.order_sysid, response.error_id, response.error_msg, response.seq)
on_disconnected()
on_account_status(data)
on_stock_asset(data)
on_stock_order(data)
on_stock_trade(data)
on_stock_position(data)
on_order_error(data)
on_cancel_error(data)
on_order_stock_async_response(data)
on_smt_appointment_async_response(data)