diff --git a/.gitignore b/.gitignore index b941f8f..9a1b83c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,9 +8,9 @@ venv/ # ---- test outputs (local mock test artifacts) ---- tests/*.txt -# ---- nested reference repo: keep the files, drop its git metadata ---- -reference/xtquant_big_convert/.git/ -reference/xtquant_big_convert/.gitignore +# ---- reference (read-only snapshots; keep docs, drop project code) ---- +# reference/xtquant_big_convert keeps its own .gitignore rules (they apply +# to that subtree); no longer excluded here. # ---- editor / OS ---- .vscode/ diff --git a/reference/thinktrader_docs/innerApi_callback_function.html b/reference/thinktrader_docs/innerApi_callback_function.html deleted file mode 100644 index 55860be..0000000 --- a/reference/thinktrader_docs/innerApi_callback_function.html +++ /dev/null @@ -1,307 +0,0 @@ - - - - - - - - - 成交回报实时主推函数 | 迅投知识库 - - - - -

实时主推函数

account_callback - 资金账号状态变化主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: account_callback(ContextInfo, accountInfo)

释义: 当资金账号状态有变化时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 5, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def account_callback(ContextInfo, accountInfo):
-    print(show_data(accountInfo)) 
-
-

task_callback - 账号任务状态变化主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: task_callback(ContextInfo, taskInfo)

释义: 当账号任务状态有变化时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 5, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def task_callback(ContextInfo, taskInfo):
-    print(show_data(taskInfo))
-

order_callback - 账号委托状态变化主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: order_callback(ContextInfo, orderInfo)

释义: 当账号委托状态有变化时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 5, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def order_callback(ContextInfo, orderInfo):
-    print(show_data(orderInfo))
-

deal_callback - 账号成交状态变化主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: deal_callback(ContextInfo, dealInfo)

释义: 当账号成交状态有变化时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 5, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def deal_callback(ContextInfo, dealInfo):
-    print(show_data(dealInfo))
-

position_callback - 账号持仓状态变化主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: position_callback(ContextInfo, positonInfo)

释义: 当账号持仓状态有变化时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 5, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def position_callback(ContextInfo, positionInfo):
-    print(show_data(positionInfo))
-
-

orderError_callback - 账号异常下单主推

提示

  1. 仅在实盘运行模式下生效。
  2. 需要先在init里调用ContextInfo.set_account后生效。

用法: orderError_callback(ContextInfo,orderArgs,errMsg)

释义: 当账号下单异常时,这个函数被客户端调用

参数:

返回:

示例:

#coding:gbk
-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def init(ContextInfo):
-    # 设置对应的资金账号
-    # 示例需要在策略交易界面运行
-    ContextInfo.set_account(account)
-    
-def after_init(ContextInfo):
-    # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值
-    # 编译器界面里执行的下单函数不会产生实际委托  
-    passorder(23, 1101, account, "000001.SZ", 11, 0, 100, "示例", 2, "投资备注",ContextInfo)
-    pass
-
-def orderError_callback(ContextInfo,orderArgs,errMsg):
-    print(show_data(orderArgs))
-    print(errMsg)
-
-

其他主推函数

credit_account_callback - 查询信用账户明细回调

用法: credit_account_callback(ContextInfo,seq,result)

释义: 查询信用账户明细回调

参数:

credit_opvolume_callback - 查询两融最大可下单量的回调

用法: credit_opvolume_callback(ContextInfo,accid,seq,ret,result)

释义: 查询两融最大可下单量的回调。

参数:

  • ContextInfo:策略模型全局对象
  • accid:查询的账号
  • seq:query_credit_opvolume时输入查询seq
  • ret:查询结果状态。正常返回:1,正在查询中-1,输入账号非法:-2,输入查询参数非法:-3,超时等服务器返回报错:-4
  • result:查询到的结果

示例query_credit_opvolume

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_data_function.html b/reference/thinktrader_docs/innerApi_data_function.html deleted file mode 100644 index a226e53..0000000 --- a/reference/thinktrader_docs/innerApi_data_function.html +++ /dev/null @@ -1,1218 +0,0 @@ - - - - - - - - - 行情函数 | 迅投知识库 - - - - -

数据下载

download_history_data - 下载指定合约代码指定周期对应时间范围的行情数据

提示

QMT提供的行情数据中,基础周期包含 tick 1m 5m 1d,这些是实际用于存储的周期 其他周期为合成周期,以基础周期合成得到

合成周期

  • 3m, 由1m线合成
  • 10m, 15m, 30m, 60m, 2h, 3h, 4h 由5分钟线合成
  • 2d(2日线), 3d(3日线), 5d(5日线), 1w(周线), 1mon(月线), 1q(季线), 1hy(半年线), 1y(年线) 由日线数据合成

获取合成周期时

  • 如果取历史,需要下载历史的基础周期(如取15m需要下载5m)
  • 如果取实时,可以直接订阅原始周期(如直接订阅15m)

如果同时用到基础周期和合成周期,只需要下载基础周期,例如同时使用5m和15m,因为15m也是由5m合成,所以只需要下载一次5m的数据即可

原型

download_history_data(stockcode,period,startTime,endTime)
-

释义

下载指定合约代码指定周期对应时间范围的行情数据

参数

字段名数据类型解释
stockcodestring股票代码,格式为'stkcode.market',例如 '600000.SH'
periodstringK线周期类型,包括:
'tick':分笔线
'1d':日线
'1m':分钟线
'5m':5分钟线
startTimestring起始时间,格式为 "20200101" 或 "20200101093000",可以为空
endTimestring结束时间,格式为 "20200101" 或 "20200101093000",可以为空
incrementallybool默认为 None 是否从本地最后一条数据往后增量下载,部分版本客户端可能不支持此参数

返回值

none

示例

# coding:gbk
-def init(C):
-	download_history_data("000001.SZ","1d","20230101","") # 下载000001.SZ,从20230101至今的日线数据
-    download_history_data("000001.SZ","1d","20230101","",incrementally=True) # 下载000001.SZ,从20230101至今的日线数据,增量下载
-
-def handlebar(C):
-    return
-

界面端进行数据下载还可参考:

提示

【攻略】K线/财务数据下载方式 https://www.xuntou.net/forum.php?mod=viewthread&tid=1354&user_code=7zqjlm 来自: 迅投QMT社区

获取行情数据

该目录下的函数用于获取实时行情,历史行情

ContextInfo.get_market_data_ex - 获取行情数据

注意

  1. 该函数不建议在init中运行,在init中运行时仅能取到本地数据
  2. 关于获取行情函数之间的区别与注意事项可在 - 常见问题-行情相关在新窗口打开 查看
  3. 除实时行情外,该函数还可用于获取特色数据,如资金流向数据,订单流数据等,获取方式见数据字典在新窗口打开

原型

ContextInfo.get_market_data_ex(
-    fields=[], 
-    stock_code=[], 
-    period='follow', 
-    start_time='', 
-    end_time='', 
-    count=-1, 
-    dividend_type='follow', 
-    fill_data=True, 
-    subscribe=True)
-

释义

获取实时行情与历史行情数据

参数

名称类型描述
fieldlist数据字段,详情见下方field字段表
stock_listlist合约代码列表
periodstr数据周期,可选字段为:
"tick"
"1m":1分钟线
"5m":5分钟线;"15m":15分钟线;"30m":30分钟线
"1h"小时线
"1d":日线
"1w":周线
"1mon":月线
"1q":季线
"1hy":半年线
"1y":年线
'l2quote':Level2行情快照
'l2quoteaux':Level2行情快照补充
'l2order':Level2逐笔委托
'l2transaction':Level2逐笔成交
'l2transactioncount':Level2大单统计
'l2orderqueue':Level2委买委卖队列
start_timestr数据起始时间,格式为 %Y%m%d 或 %Y%m%d%H%M%S,填""为获取历史最早一天
end_timestr数据结束时间,格式为 %Y%m%d 或 %Y%m%d%H%M%S ,填""为截止到最新一天
countint数据个数
dividend_typestr除权方式,可选值为
'none':不复权
'front':前复权
'back':后复权
'front_ratio': 等比前复权
'back_ratio': 等比后复权
fill_databool是否填充数据
subscribebool订阅数据开关,默认为True,设置为False时不做数据订阅,只读取本地已有数据。
  • field字段可选:
field数据类型含义
timeint时间
openfloat开盘价
highfloat最高价
lowfloat最低价
closefloat收盘价
volumefloat成交量
amountfloat成交额
settlefloat今结算
openInterestfloat持仓量
preClosefloat前收盘价
suspendFlagint停牌 1停牌,0 不停牌
  • period周期为tick时,field字段可选:
field数据类型含义
timeint时间
lastPricefloat最新价
lastClosefloat前收盘价
openfloat开盘价
highfloat最高价
lowfloat最低价
closefloat收盘价
volumefloat成交量
amountfloat成交额
settlefloat今结算
openInterestfloat持仓量
stockStatusint停牌 1停牌,0 不停牌

返回值

  • 返回dict { stock_code1 : value1, stock_code2 : value2, ... }
  • value1, value2, ... :pd.DataFrame 数据集,index为time_list,columns为fields,可参考Bar字段在新窗口打开
  • 各标的对应的DataFrame维度相同、索引相同

示例

# coding:gbk
-import pandas as pd
-import numpy as np
-
-def init(C):	
-	C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]# 指定获取的标的
-	C.start_time = "20230901"# 指定获取数据的开始时间
-	C.end_time = "20231101"# 指定获取数据的结束时间
-	
-def handlebar(C):
-	# 获取多只股票,多个字段,一条数据
-	data1 = C.get_market_data_ex([],C.stock_list, period = "1d",count = 1)
-	# 获取多只股票,多个字段,指定时间数据
-	data2 = C.get_market_data_ex([],C.stock_list, period = "1d", start_time = C.start_time, end_time = C.end_time)
-	# 获取多只股票,多个字段,指定时间15m数据
-	data3 = C.get_market_data_ex([],C.stock_list, period = "15m", start_time = C.start_time, end_time = C.end_time)
-	# 获取多只股票,指定字段,指定时间15m数据
-	data4 = C.get_market_data_ex(["close","open"],C.stock_list, period = "15m", start_time = C.start_time, end_time = C.end_time)
-	# 获取多只股票,历史tick
-	tick = C.get_market_data_ex([],C.stock_list, period = "tick", start_time = C.start_time, end_time = C.end_time)
-	# 获取期货5档盘口tick
-	future_lv2_quote = C.get_market_data_ex([],["rb2405.SF","ec2404.INE"], period = "l2quote", count = 1)
-	print(data1)
-	print(data2["000001.SZ"].tail())
-	print(data3)
-	print(data4["000001.SZ"])
-	print(data4["000001.SZ"].to_csv("your_path")) # 导出文件为csv格式,路径填本机路径
-	print(tick["000001.SZ"])
-	print(future_lv2_quote)
-
-

ContextInfo.get_full_tick - 获取全推数据

提示

不能用于回测 只能取最新的分笔,不能取历史分笔

原型

ContextInfo.get_full_tick(stock_code=[])
-

释义

获取最新分笔数据

参数

名称类型描述
stock_codelist[str]合约代码列表,如['600000.SH','600036.SH'],不指定时为当前主图合约。

返回值 根据stock_code返回一个dict,该字典的key值是股票代码,其值仍然是一个dict,在该dict中存放股票代码对应的最新的数据。该字典数据key值参考tick字段在新窗口打开

示例

# coding:gbk
-import pandas as pd
-import numpy as np
-
-def init(C):
-	C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]
-	
-def handlebar(C):
-	tick = C.get_full_tick(C.stock_list)
-	print(tick["510050.SH"])
-

ContextInfo.subscribe_quote - 订阅行情数据

提示

  1. 该函数属于订阅函数,非VIP用户限制订阅数量

  2. VIP用户支持全推市场指定周期K线

  3. VIP用户权限请参考vip-行情用户优势对比

原型

ContextInfo.subscribe_quote(
-    stock_code,
-    period='follow',
-    dividend_type='follow',
-    result_type='',
-    callback=None)
-

释义

订阅行情数据,关于订阅机制请参考运行机制对比在新窗口打开

参数

字段名数据类型解释
stockcodestring股票代码,'stkcode.market',如'600000.SH'
periodstringK线周期类型
dividend_typestring除权方式,可选值为
'none':不复权
'front':前复权
'back':后复权
'front_ratio': 等比前复权
'back_ratio': 等比后复权
注意:分笔周期返回数据均为不复权
result_typestring返回数据格式,可选范围:<br>'DataFrame'或''(默认):返回{code:data},data为pd.DataFrame数据集,index为字符串格式的时间序列,columns为数据字段<br>'dict':返回{code:{k1:v1,k2:v2,...}},k为数据字段名,v为字段值<br>'list':返回{code:{k1:[v1],k2:[v2],...}},k为数据字段名,v为字段值
callbackfunction指定推送行情的回调函数

返回值

int:订阅号,用于反订阅

示例

# conding = gbk
-def call_back(data):
-	print(data)
-	
-def init(C):
-	C.subID = C.subscribe_quote("000001.SZ","1d", callback = call_back)
-def handlebar(C):
-	print("============================")
-	print("C.subID: ",C.subID)
-	
-

ContextInfo.subscribe_whole_quote - 订阅全推数据

提示

ContextInfo.subscribe_whole_quote(code_list,callback=None)
-

释义

订阅全推数据,全推数据只有分笔周期,每次增量推送数据有变化的品种

参数

字段名数据类型解释
code_listlist[str,...]市场代码列表/品种代码列表,如 ['SH','SZ'] 或 ['600000.SH', '000001.SZ']
callbackfunction数据推送回调

返回值int,订阅号,可用ContextInfo.unsubscribe_quote做反订阅

# conding = gbk
-def call_back(data):
-	print(data)
-	
-def init(C):
-	C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]
-	C.subID = C.subscribe_whole_quote(C.stock_list,callback=call_back)
-def handlebar(C):
-	print("============================")
-	print("C.subID: ",C.subID)
-

ContextInfo.unsubscribe_quote - 反订阅行情数据

原型

ContextInfo.unsubscribe_quote(subId)
-

释义

反订阅行情数据,配合ContextInfo.subscribe_quote()ContextInfo.subscribe_whole_quote()使用

参数

字段名数据类型解释
subIdint行情订阅返回的订阅号

示例

# conding = gbk
-def call_back(data):
-	print(data)
-def init(C):
-	C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]
-	C.subID = C.subscribe_whole_quote(C.stock_list,callback=call_back)
-
-def handlebar(C):
-	print("============================")
-	print("C.subID: ",C.subID)
-	if C.subID > 0:
-		C.unsubscribe_quote(C.subID) # 取消行情订阅
-

subscribe_formula - 订阅模型

原型

subscribe_formula(
-   formula_name,stock_code,period
-   ,start_time="",end_time="",count=-1
-   ,dividend_type="none"
-   ,extend_param={}
-   ,callback=None)
-

释义 订阅vba模型运行结果,使用前要注意补充本地K线数据或分笔数据

参数

字段名类型描述
formula_namestr模型名称名
stock_codestr模型主图代码形式如'stkcode.market',如'000300.SH'
periodstrK线周期类型,可选范围:'tick':分笔线,'1d':日线,'1m':分钟线,'3m':三分钟线,'5m':5分钟线,'15m':15分钟线,'30m':30分钟线,'1h':小时线,'1w':周线,'1mon':月线,'1q':季线,'1hy':半年线,'1y':年线
start_timestr模型运行起始时间,形如:'20200101',默认为空视为最早
end_timestr模型运行截止时间,形如:'20200101',默认为空视为最新
countint模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
dividend_typestr复权方式,默认为主图除权方式,可选范围:'none':不复权,'front':向前复权,'back':向后复权,'front_ratio':等比向前复权,'back_ratio':等比向后复权
extend_paramdict模型的入参,形如 {'a': 1, '__basket': {}}
__basketdict可选参数,组合模型的股票池权重,形如 {'600000.SH': 0.06, '000001.SZ': 0.01}

返回值 分两块,

  • subscribe_formula返回模型的订阅号,可用于后续反订阅,失败返回 -1

  • callback:

    • timelist: 数据时间戳
    • outputs:模型的输出值,结构为{变量名:值}

示例

#encoding=gbk
-def callback(data):
-    print(data)
-
-def init(ContextInfo):
-    basket={
-       '600000.SH':0.06,
-       '000001.SZ':0.01
-      }
-    argsDict={'a':100,'__basket':basket}
-    subID=subscribe_formula(
-      '单股模型示范','000300.SH','1d',
-      '20240101','20240201',-1,
-      "none",
-      argsDict,
-      callback
-   )
-
-

unsubscribe_formula - 反订阅模型

原型

unsubscribe_formula(subID)
-

释义 反订阅模型

参数

字段名类型描述
subIDint模型订阅号

返回值

  • bool:反订阅成功为True,失败为False

示例

#encoding=gbk
-def callback(data):
-    print(data)
-
-def init(ContextInfo):
-    basket={
-       '600000.SH':0.06,
-       '000001.SZ':0.01
-      }
-    argsDict={'a':100,'__basket':basket}
-    subID=subscribe_formula(
-      '单股模型示范','000300.SH','1d',
-      '20240101','20240201',-1,
-      "none",
-      argsDict,
-      callback
-   )
-
-	unsubscribe_formula(subID)
-

call_formula - 调用模型

原型

call_formula(formula_name,stock_code,period,start_time="",end_time="",count=-1,dividend_type="none",extend_param={})
-

释义 获取vba模型运行结果,使用前要注意补充本地K线数据或分笔数据

参数

字段名类型描述
formula_namestr模型名称名
stock_codestr模型主图代码形式如'stkcode.market',如'000300.SH'
periodstrK线周期类型,可选范围:'tick':分笔线,'1d':日线,'1m':分钟线,'3m':三分钟线,'5m':5分钟线,'15m':15分钟线,'30m':30分钟线,'1h':小时线,'1w':周线,'1mon':月线,'1q':季线,'1hy':半年线,'1y':年线
start_timestr模型运行起始时间,形如:'20200101',默认为空视为最早
end_timestr模型运行截止时间,形如:'20200101',默认为空视为最新
countint模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
dividend_typestr复权方式,默认为主图除权方式,可选范围:'none':不复权,'front':向前复权,'back':向后复权,'front_ratio':等比向前复权,'back_ratio':等比向后复权
extend_paramdict模型的入参,{"模型名:参数名":参数值},例如在跑模型MA时,{'MA:n1':1};入参可以添加__basket:dict,组合模型的股票池权重,形如{'__basket':{'600000.SH':0.06,'000001.SZ':0.01}},如果在跑一个模型1的时候,模型1调用了模型2,如果只想修改模型2的参数可以传{'模型2:参数':参数值}

返回值 返回:dict{ 'dbt':0,#返回数据类型,0:全部历史数据 'timelist':[...],#返回数据时间范围list, 'outputs':{'var1':[...],'var2':[...]}#输出变量名:变量值list }

示例

def handlebar(ContextInfo):
-    basket={'600000.SH':0.06,'000001.SZ':0.01}
-    argsDict={'a':100,'__basket':basket}
-    modelRet=call_formula('单股模型示范','000300.SH','1d','20240101','20240201',-1,"none",argsDict)
-    print(modelRet)
-
-

call_formula_batch - 批量调用模型

原型

call_formula_batch(formula_names,stock_codes,period,start_time="",end_time="",count=-1,dividend_type="none",extend_params=[])
-
-

释义 批量获取vba模型运行结果,使用前要注意补充本地K线数据或分笔数据

参数

字段名类型描述
formula_nameslist包含要批量运行的模型名
stock_codeslist包含要批量运行的模型主图代码形式'stkcode.market',如'000300.SH'
periodstrK线周期类型,可选范围:'tick':分笔线,'1d':日线,'1m':分钟线,'3m':三分钟线,'5m':5分钟线,'15m':15分钟线,'30m':30分钟线,'1h':小时线,'1w':周线,'1mon':月线,'1q':季线,'1hy':半年线,'1y':年线
start_timestr模型运行起始时间,形如:'20200101',默认为空视为最早
end_timestr模型运行截止时间,形如:'20200101',默认为空视为最新
countint模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
dividend_typestr复权方式,默认为主图除权方式,可选范围:'none':不复权,'front':向前复权,'back':向后复权,'front_ratio':等比向前复权,'back_ratio':等比向后复权
extend_paramslist包含每个模型的入参,[{"模型名:参数名":参数值}],例如在跑模型MA时,{'MA:n1':1};入参可以添加__basket:dict,组合模型的股票池权重,形如{'__basket':{'600000.SH':0.06,'000001.SZ':0.01}},如果在跑一个模型1的时候,模型1调用了模型2,如果只想修改模型2的参数可以传{'模型2:参数':参数值}

返回值

  • list[dict]
    • dict说明:
      • formula:模型名
      • stock:品种代码
      • argument:参数
      • result:dict参考call_formula返回结果

示例


-def handlebar(ContextInfo):
-    formulas=['testModel1','testModel2']
-    codes=['600000.SH','000001.SZ']
-    basket={'600000.SH':0.06,'000001.SZ':0.01}
-    args=[{'a':100,'__basket':basket},{'a':200,'__basket':basket}]
-    modelRet=call_formula_batch(formulas,codes,'1d',extend_params=args);
-    print(modelRet)
-
-

ContextInfo.get_svol - 根据代码获取对应股票的内盘成交量

原型

ContextInfo.get_svol(stockcode)
-

释义

根据代码获取对应股票的内盘成交量

参数

字段名数据类型解释
stockcodestring股票代码,如 '000001.SZ',缺省值'',默认为当前图代码

返回值int:内盘成交量

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_svol('000001.SZ')
-	print(data)
-

ContextInfo.get_bvol - 根据代码获取对应股票的外盘成交量

原型

ContextInfo.get_bvol(stockcode)
-

释义

根据代码获取对应股票的外盘成交量

参数

字段名数据类型解释
stockcodestring股票代码,如 '000001.SZ',缺省值'',默认为当前图代码

返回值

int:外盘成交量

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_bvol('000001.SZ')
-	print(data)
-

ContextInfo.get_turnover_rate - 获取换手率

提示

使用之前需要下载财务数据(在财务数据下载中)以及日线数据

如果不补充股本数据,将使用最新流通股本计算历史换手率,可能会造成历史换手率不正确

原型

ContextInfo.get_turnover_rate(stock_list,startTime,endTime)
-

释义

获取换手率

参数

字段名数据类型解释
stock_listlist股票列表,如['600000.SH','000001.SZ']
startTimestring起始时间,如'20170101'
endTimestring结束时间,如'20180101'

返回值

pandas.Dataframe

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_turnover_rate(['000002.SZ'],'20170101','20170301')
-	print(data)
-

ContextInfo.get_longhubang - 获取龙虎榜数据

原型

ContextInfo.get_longhubang(stock_list, startTime, endTime)
-

释义

获取龙虎榜数据

参数

参数名称类型描述
stock_listlist股票列表,如 ['600000.SH', '600036.SH']
startTimestr起始时间,如 '20170101'
endTimestr结束时间,如 '20180101'

返回值

  • 格式为pandas.DataFrame:
参数名称数据类型描述
stockCodestr股票代码
stockNamestr股票名称
datedatetime上榜日期
reasonstr上榜原因
closefloat收盘价
SpreadRatefloat涨跌幅
TurnoverVolumefloat成交量
Turnover_Amountfloat成交金额
buyTraderBoothpandas.DataFrame买方席位
sellTraderBoothpandas.DataFrame卖方席位
  • buyTraderBoothsellTraderBooth 包含字段:
参数名称数据类型描述
traderNamestr交易营业部名称
buyAmountfloat买入金额
buyPercentfloat买入金额占总成交占比
sellAmountfloat卖出金额
sellPercentfloat卖出金额占总成交占比
totalAmountfloat该席位总成交金额
rankint席位排行
directionint买卖方向

示例

# coding:gbk
-
-def init(C):
-    return
-
-def handlebar(C):
-    print(C.get_longhubang(['000002.SZ'],'20100101','20180101'))
-

ContextInfo.get_north_finance_change - 获取对应周期的北向数据

原型

ContextInfo.get_north_finance_change(period)
-

释义

获取对应周期的北向数据

参数

字段名数据类型描述
periodstr数据周期

返回值

  • 根据period返回一个dict,该字典的key值是北向数据的时间戳,其值仍然是一个dict,其值的key值是北向数据的字段类型,其值是对应字段的值。该字典数据key值有:
字段名数据类型描述
hgtNorthBuyMoneyintHGT北向买入资金
hgtNorthSellMoneyintHGT北向卖出资金
hgtSouthBuyMoneyintHGT南向买入资金
hgtSouthSellMoneyintHGT南向卖出资金
sgtNorthBuyMoneyintSGT北向买入资金
sgtNorthSellMoneyintSGT北向卖出资金
sgtSouthBuyMoneyintSGT南向买入资金
sgtSouthSellMoneyintSGT南向卖出资金
hgtNorthNetInFlowintHGT北向资金净流入
hgtNorthBalanceByDayintHGT北向当日资金余额
hgtSouthNetInFlowintHGT南向资金净流入
hgtSouthBalanceByDayintHGT南向当日资金余额
sgtNorthNetInFlowintSGT北向资金净流入
sgtNorthBalanceByDayintSGT北向当日资金余额
sgtSouthNetInFlowintSGT南向资金净流入
sgtSouthBalanceByDayintSGT南向当日资金余额

示例:

# coding = gbk
-def init(C):
-    return
-# 获取市场北向数据
-def handlebar(C):
-    print(C.get_north_finance_change('1d'))
-

ContextInfo.get_hkt_details - 获取指定品种的持股明细

原型

ContextInfo.get_hkt_details(stockcode)
-

释义

获取指定品种的持股明细

参数

参数名称数据类型描述
stockcodestring必须是'stock.market'形式

返回值

  • 根据stockcode返回一个dict,该字典的key值是北向持股明细数据的时间戳,其值仍然是一个dict,其值的key值是北向持股明细数据的字段类型,其值是对应字段的值,该字典数据key值有:
参数名称数据类型/单位描述
stockCodestr股票代码
ownSharesCompanystr机构名称
ownSharesAmountint持股数量
ownSharesMarketValuefloat持股市值
ownSharesRatiofloat持股数量占比
ownSharesNetBuyfloat净买入金额(当日持股-前一日持股)

示例:

# coding = gbk
-def init(C):
-    return
-def handlebar(C):
-    data = C.get_hkt_details('600000.SH')
-    print(data)
-

ContextInfo.get_hkt_statistics - 获取指定品种的持股统计

原型

ContextInfo.get_hkt_statistics(stockcode)
-

释义

获取指定品种的持股统计

参数

字段名数据类型解释
stockcodestring必须是'stock.market'形式

返回值

根据stockcode返回一个dict,该字典的key值是北向持股统计数据的时间戳,其值仍然是一个dict,其值的key值是北向持股统计数据的字段类型,其值是对应字段的值,该字典数据key值有:

字段名数据类型解释
stockCodestring股票代码
ownSharesAmountfloat持股数量,单位:股
ownSharesMarketValuefloat持股市值,单位:元
ownSharesRatiofloat持股数量占比,单位:%
ownSharesNetBuyfloat净买入,单位:元,浮点数(当日持股-前一日持股)

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-
-	print(C.get_hkt_statistics('600000.SH'))
-

get_etf_info - 根据ETF基金代码获取ETF申赎清单及对应成分股数据

原型

get_etf_info(stockcode)
-

释义

根据ETF基金代码获取ETF申赎清单及对应成分股数据,每日盘前更新

参数

字段名数据类型解释
stockcodestringETF基金代码如"510050.SH"

返回值

一个多层嵌套的dict

示例

# coding:gbk
-def init(C):
-    pass
-    
-def handlebar(C):
-    d = get_etf_info("510050.SH")
-    print(d)
-

get_etf_iopv - 根据ETF基金代码获取ETF的基金份额参考净值

原型

get_etf_iopv(stockcode)
-

释义

根据ETF基金代码获取ETF的基金份额参考净值

参数

字段名数据类型解释
stockcodestringETF基金代码如"510050.SH"

返回值

float类型值,IOPV,基金份额参考净值

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	print(get_etf_iopv("510050.SH"))
-

ContextInfo.get_local_data - 获取本地行情数据【不推荐】

注意

本函数用于仅用于获取本地历史行情数据,使用前请确保已通过download_history_data在新窗口打开下载过历史行情数据

原型

ContextInfo.get_local_data(
-    stock_code,
-    start_time='',
-    end_time='',
-    period='1d',
-    divid_type='none',
-    count=-1)
-

释义

获取本地行情数据

参数

字段名数据类型解释
stock_codestring默认参数,合约代码格式为 code.market,不指定时为当前图合约
start_timestring默认参数,开始时间,格式为 '20171209' 或 '20171209010101'
end_timestring默认参数,结束时间,格式同 start_time
periodstring默认参数,K线类型,可选值包括:
'tick':分笔线(只用于获取'quoter'字段数据)、'realtime': 实时线、'1d':日线
'md':多日线、'1m':1分钟线、'3m':3分钟线
'5m':5分钟线、'15m':15分钟线、'30m':30分钟线
'mm':多分钟线、'1h':小时线、'mh':多小时线
'1w':周线、'1mon':月线、'1q':季线
'1hy':半年线、'1y':年线
dividend_typestring除复权种类,可选值:
'none':不复权
'front':向前复权
'back':向后复权
'front_ratio':等比向前复权
'back_ratio':等比向后复权
countintcount 大于等于0时:
如果指定了 start_timeend_time,则以 end_time 为基准向前取 count 条数据;
如果 start_timeend_time 缺省,则默认取本地数据最新的 count 条数据;
如果 start_timeend_timecount 都缺省时,则默认取本地全部数据。

返回值

返回一个dict,键值为timetag,value为另一个dict(valuedict)

  • period='tick'时函数获取分笔数据,valuedict字典数据key值有:
字段数据类型含义
lastPricefloat最新价
openfloat开盘价
highfloat最高价
lowfloat最低价
lastClosefloat前收盘价
amountfloat成交额
volumefloat成交量
pvolumefloat原始成交量
stockStatusint作废 参考openInt
openIntfloat若是股票,则openInt含义为股票状态,非股票则是持仓量openInt字段说明在新窗口打开
lastSettlementPricefloat昨结算价
askPricelist委卖价
bidPricelist委买价
askVollist委卖量
bidVollist委买量
settlementPricefloat今结算价
  • period为其他值时,valuedict字典数据key值有:
字段名数据类型解释
amountfloat成交额
volumefloat成交量
openfloat开盘价
highfloat最高价
lowfloat最低价
closefloat收盘价

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_local_data(stock_code='600000.SH',start_time='20220101',end_time='20220131',period='1d',divid_type='none')
-	print(data)
-

ContextInfo.get_history_data - 获取历史行情数据【不推荐】

警告

  1. 此函数已不推荐使用,推荐使用ContextInfo.get_market_data_ex()在新窗口打开
  2. 此函数使用前需要先通过ContextInfo.set_universe()设定股票池

原型

ContextInfo.get_history_data(
-    len, 
-    period, 
-    field, 
-    dividend_type = 0,
-    skip_paused = True)
-

释义

获取历史行情数据

参数

名称类型描述
lenint需获取的历史数据长度
periodstring需获取的历史数据周期,可选值包括:
'tick':分笔线、 '1d':日线、 '1m':1分钟线
'3m':3分钟线、 '5m':5分钟线、 '15m':15分钟线
'30m':30分钟线、 '1h':小时线、 '1w':周线
'1mon':月线、 '1q':季线、 '1hy':半年线
'1y':年线
fieldstring需获取的历史数据的类型,可选值包括:
'open':开盘价
'high':最高价
'low':最低价
'close':收盘价
'quoter':详细报价(结构见 get_market_data 方法)
dividend_typeint默认参数,除复权,默认不复权,可选值包括:
0:不复权
1:向前复权
2:向后复权
3:等比向前复权
4:等比向后复权
skip_pausedbool默认参数,是否停牌填充,默认填充

返回值 一个字典dict结构,key 为 stockcode.market, value 为行情数据 list,list 中第 0 位为最早的价格,第 1 位为次早价格,依次下去。

示例

# coding = gbk
-def init(C):
-	C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]
-	C.set_universe(C.stock_list)
-
-def handlebar(C):
-	data = C.get_history_data(2, '1d', 'close')
-	print(data)
-
-

ContextInfo.get_market_data() - 获取行情数据【不推荐】

原型

ContextInfo.get_market_data(
-    fields, 
-    stock_code = [], 
-    start_time = '', 
-    end_time = '',
-    skip_paused = True, 
-    period = 'follow', 
-    dividend_type = 'follow', 
-    count = -1)
-

释义

获取行情数据

参数

字段名数据类型解释
fields字段列表可选值包括:
'open': 开
'high': 高
'low': 低
'close': 收
'volume': 成交量
'amount': 成交额
'settle': 结算价
'quoter': 分笔数据(包括历史)
stock_code默认参数,合约代码列表合约格式为 code.market,例如 '600000.SH',不指定时为当前图合约
start_time默认参数,时间戳开始时间,格式为 '20171209' 或 '20171209010101'
end_time默认参数,时间戳结束时间,格式为 '20171209' 或 '20171209010101'
skip_paused默认参数,布尔值如何处理停牌数据:
true:如果是停牌股,会自动填充未停牌前的价格作为停牌日的价格
false:停牌数据为 NaN
periodstring需获取的历史数据周期,可选值包括:
'tick':分笔线、 '1d':日线、 '1m':1分钟线
'3m':3分钟线、 '5m':5分钟线、 '15m':15分钟线
'30m':30分钟线、 '1h':小时线、 '1w':周线
'1mon':月线、 '1q':季线、 '1hy':半年线
'1y':年线
dividend_type默认参数,字符串缺省值为 'none',除复权,可选值包括:
'none':不复权
'front':向前复权
'back':向后复权
'front_ratio':等比向前复权
'back_ratio':等比向后复权
count默认参数,整数缺省值为 -1。当大于等于 0 时,效果与 get_history_data 保持一致
  • count参数设置的几种情况
count 取值时间设置是否生效开始时间和结束时间设置效果
count >= 0生效返回数量取决于开始时间与结束时间和count与结束时间的交集
count = -1生效同时设置开始时间和结束时间,在所设置的时间段内取值
count = -1生效开始时间结束时间都不设置,取当前最新bar的值
count = -1生效只设置开始时间,取所设开始时间到当前时间的值
count = -1生效只设置结束时间,取股票上市第一根 bar 到所设结束时间的值

返回值

  • 返回值根据传入的参数情况,会返回不同类型的结果
count字段数量股票数量时间点返回类型
=-1=1=1=1float
=-1>1=1默认值pandas.Series
>=-1>=1=1>=1pandas.DataFrame(字段数量和时间点不同时为1)
=-1>=1>1默认值pandas.DataFrame
>1=1=1=1pandas.DataFrame
>=-1>=1>1>=1pandas.Panel

示例

# coding = gbk
-def init(C):
-    C.stock_list = ["000001.SZ","600519.SH", "510050.SH"]
-	
-def handlebar(C):
-    data1 = C.get_market_data(["close"],["000001.SZ"],start_time = "20231106",end_time = "20231106", count = -1) # 返回float值
-    data2 = C.get_market_data(["close","open"],["000001.SZ"], count = -1) # 返回pandas.Series
-    data3 = C.get_market_data(["close","open"],C.stock_list, count = -1) # 返回pandas.DataFrame
-    data4 = C.get_market_data(["open","high", "low", "close"],C.stock_list,count = 20) # 返回pandas.Panel
-
-    print(data1)
-    print(data2)
-    print(data3)
-    print(data4)
-
-
-

获取财务数据

获取财务数据前,请先通过界面端数据管理 - 财务数据下载

财务数据下载

提示

财务数据接口通过读取下载本地的数据取数,使用前需要补充本地数据。除公告日期和报表截止日期为时间戳毫秒格式其他单位为元或 %,数据主要包括资产负债表(ASHAREBALANCESHEET)、利润表(ASHAREINCOME)、现金流量表(ASHARECASHFLOW)、股本表(CAPITALSTRUCTURE)的主要字段数据以及经过计算的主要财务指标数据(PERSHAREINDEX)。建议使用本文档对照表中的英文表名和迅投英文字段,表名不区分大小写。

ContextInfo.get_financial_data - 获取财务数据

财务数据接口有两种用法,入参和返回值不同,具体如下

用法1

原型

ContextInfo.get_financial_data(fieldList, stockList, startDate, enDate, report_type = 'announce_time')
-

释义

获取财务数据,方法1

参数

字段名类型释义与用例
fieldListList(必须)财报字段列表:['ASHAREBALANCESHEET.fix_assets', '利润表.净利润']
stockListList(必须)股票列表:['600000.SH', '000001.SZ']
startDateStr(必须)开始时间:'20171209'
endDateStr(必须)结束时间:'20171212'
report_typeStr(可选)报表时间类型,可缺省,默认是按照数据的公告期为区分取数据,设置为 'report_time' 为按照报告期取数据,' announce_time' 为按照公告日期取数据

提示

选择按照公告期取数和按照报告期取数的区别:

报告日期是指财务报告所覆盖的会计时间段,而公告日期是指公司向外界公布该报告的具体时间点

若指定report_type为report_time,则不会考虑财报的公告日期,可能会取到未来数据

若指定report_type为announce_time,则会按财报实际发布日期返回数据,不会取到未来数据

例:

返回值

函数根据stockList代码列表,startDate,endDate时间范围,返回不同的的数据类型。如下:

代码数量时间范围返回类型
=1=1pandas.Series (index = 字段)
=1>1pandas.DataFrame (index = 时间, columns = 字段)
>1=1pandas.DataFrame (index = 代码, columns = 字段)
>1>1pandas.Panel (items = 代码, major_axis = 时间, minor_axis = 字段)

示例

# coding:gbk
-def init(C):
-  pass
-
-def handlebar(C):
-
-  #取总股本和净利润
-  fieldList = ['CAPITALSTRUCTURE.total_capital', '利润表.净利润']   
-  stockList = ["000001.SZ","000002.SZ","430017.BJ"]
-  startDate = '20171209'
-  endDate = '20231204'
-  data = C.get_financial_data(fieldList, stockList, startDate, endDate, report_type = 'report_time')
-  print(data)
-

用法2

原型

ContextInfo.get_financial_data(tabname, colname, market, code, report_type = 'report_time', barpos)
-

与用法 1 可同时使用

释义

获取财务数据,方法2

参数

字段名类型释义与用例
tabnameStr(必须)表名:'ASHAREBALANCESHEET'
colnameStr(必须)字段名:'fix_assets'
marketStr(必须)市场:'SH'
codeStr(必须)代码:'600000'
report_typeStr(可选)报表时间类型,可缺省,默认是按照数据的公告期为区分取数据,设置为 'report_time' 为按照报告期取数据,' announce_time ' 为按照公告日期取数据
barposnumber当前 bar 的索引

返回值

float :所取字段的数值

示例

# coding:gbk
-def init(C):
-  pass
-	
-def handlebar(C):
-  index = C.barpos
-  data = C.get_financial_data('ASHAREBALANCESHEET', 'fix_assets', 'SH', '600000', index)
-  print(data)
-

ContextInfo.get_raw_financial_data - 获取原始财务数据

提示

取原始财务数据,与get_financial_data相比不填充每个交易日的数据

原型

ContextInfo.get_raw_financial_data(fieldList,stockList,startDate,endDate,report_type='announce_time')
-
-

释义

取原始财务数据,与get_financial_data相比不填充每个交易日的数据

参数

字段名类型释义与用例
fieldListList(必须)字段列表:例如 ['资产负债表.固定资产','利润表.净利润']
stockListList(必须)股票列表:例如['600000.SH','000001.SZ']
startDateStr(必须)开始时间:例如 '20171209'
endDateStr(必须)结束时间:例如 '20171212'
report_typeStr(可选)时间类型,可缺省,默认是按照数据的公告期为区分取数据,设置为 'report_time' 为按照报告期取数据,可选值:'announce_time','report_time'

返回值

函数根据stockList代码列表,startDate,endDate时间范围,返回不同的的数据类型。如下:

代码数量时间范围返回类型
=1=1pandas.Series (index = 字段)
=1>1pandas.DataFrame (index = 时间, columns = 字段)
>1=1pandas.DataFrame (index = 代码, columns = 字段)
>1>1pandas.Panel (items = 代码, major_axis = 时间, minor_axis = 字段)

示例

#encoding:gbk
-'''
-获取财务数据
-'''
-import pandas as pd
-import numpy as np
-import talib
-
-def to_zw(a):
-	'''0.中文价格字符串'''
-	import numpy as np
-	try:
-		header = '' if a > 0 else '-'
-		if np.isnan(a):
-			return '问题数据'
-		if abs(a) < 1000:
-			return header + str(int(a)) + ""
-		if abs(a) < 10000:
-			return header + str(int(a))[0] + ""
-		if abs(a) < 100000000:
-			return header + str(int(a))[:-4] + "" + str(int(a))[-4] + ''
-		else:
-			return header + str(int(a))[:-8] + "亿" + str(int(a))[-8:-4] + ''
-	except:
-		print(f"问题数据{a}")
-		return '问题数据'
-
-
-def after_init(C):
-	fieldList = ['ASHAREINCOME.net_profit_excl_min_int_inc','ASHAREINCOME.revenue'] # 字段表
-	stockList = ['000001.SZ'] # 标的
-	a=C.get_raw_financial_data(fieldList,stockList,'20150101','20300101',report_type = 'report_time') # 获取原始财务数据
-	# print(a)
-	for stock in a:
-		for key in a[stock]:
-			for t in a[stock][key]:
-				print(key, timetag_to_datetime(int(t),'%Y%m%d'), to_zw(a[stock][key][t]))
-			print('-' *22)
-		print('-' *22)
-
-

ContextInfo.get_last_volume - 获取最新流通股本

原型

ContextInfo.get_last_volume(stockcode)
-

释义

获取最新流通股本

参数

字段名数据类型解释
stockcodestring标的名称,必须是 'stock.market' 形式

返回值

int类型值,代表流通股本数量

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_last_volume("000001.SZ")
-	print(data)
-

ContextInfo.get_total_share - 获取总股数

原型

ContextInfo.get_total_share(stockcode)
-

释义

获取总股数

参数

字段名数据类型解释
stockcodestring股票代码,缺省值 '',默认为当前图代码, 如:'600000.SH'

返回值

int:总股数

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_total_share('600000.SH')
-	print(data)
-

财务数据字段表

资产负债表 (ASHAREBALANCESHEET)

中文字段迅投字段
应收利息int_rcv
可供出售金融资产fin_assets_avail_for_sale
持有至到期投资held_to_mty_invest
长期股权投资long_term_eqy_invest
固定资产fix_assets
无形资产intang_assets
递延所得税资产deferred_tax_assets
资产总计tot_assets
交易性金融负债tradable_fin_liab
应付职工薪酬empl_ben_payable
应交税费taxes_surcharges_payable
应付利息int_payable
应付债券bonds_payable
递延所得税负债deferred_tax_liab
负债合计tot_liab
实收资本(或股本)cap_stk
资本公积金cap_rsrv
盈余公积金surplus_rsrv
未分配利润undistributed_profit
归属于母公司股东权益合计tot_shrhldr_eqy_excl_min_int
少数股东权益minority_int
负债和股东权益总计tot_liab_shrhldr_eqy
所有者权益合计total_equity
货币资金cash_equivalents
应收票据bill_receivable
应收账款account_receivable
预付账款advance_payment
其他应收款other_receivable
其他流动资产other_current_assets
流动资产合计total_current_assets
存货inventories
在建工程constru_in_process
工程物资construction_materials
长期待摊费用long_deferred_expense
非流动资产合计total_non_current_assets
短期借款shortterm_loan
应付股利dividend_payable
其他应付款other_payable
一年内到期的非流动负债non_current_liability_in_one_year
其他流动负债other_current_liability
长期应付款longterm_account_payable
应付账款accounts_payable
预收账款advance_peceipts
流动负债合计total_current_liability
应付票据notes_payable
长期借款long_term_loans
专项应付款grants_received
其他非流动负债other_non_current_liabilities
非流动负债合计non_current_liabilities
专项储备specific_reserves
商誉goodwill
报告截止日m_timetag
公告日m_anntime

利润表 (ASHAREINCOME)

中文字段迅投字段
投资收益plus_net_invest_inc
联营企业和合营企业的投资收益incl_inc_invest_assoc_jv_entp
营业税金及附加less_taxes_surcharges_ops
营业总收入revenue
营业总成本total_operating_cost
营业收入revenue_inc
营业成本total_expense
资产减值损失less_impair_loss_assets
营业利润oper_profit
营业外收入plus_non_oper_rev
营业外支出less_non_oper_exp
利润总额tot_profit
所得税inc_tax
净利润net_profit_incl_min_int_inc
归母净利润net_profit_excl_min_int_inc
管理费用less_gerl_admin_exp
销售费用sale_expense
财务费用financial_expense
综合收益总额total_income
归属于少数股东的综合收益总额total_income_minority
公允价值变动收益change_income_fair_value
已赚保费earned_premium
报告截止日m_timetag
公告日m_anntime

现金流量表 (ASHARECASHFLOW)

中文字段迅投字段
收到其他与经营活动有关的现金other_cash_recp_ral_oper_act
经营活动现金流入小计stot_cash_inflows_oper_act
支付给职工以及为职工支付的现金cash_pay_beh_empl
支付的各项税费pay_all_typ_tax
支付其他与经营活动有关的现金other_cash_pay_ral_oper_act
经营活动现金流出小计stot_cash_outflows_oper_act
经营活动产生的现金流量净额net_cash_flows_oper_act
取得投资收益所收到的现金cash_recp_return_invest
处置固定资产、无形资产和其他长期投资收到的现金net_cash_recp_disp_fiolta
投资活动现金流入小计stot_cash_inflows_inv_act
投资支付的现金cash_paid_invest
购建固定资产、无形资产和其他长期投资支付的现金cash_pay_acq_const_fiolta
支付其他与投资的现金other_cash_pay_ral_inv_act
投资活动产生的现金流出小计stot_cash_outflows_inv_act
投资活动产生的现金流量净额net_cash_flows_inv_act
吸收投资收到的现金cash_recp_cap_contrib
取得借款收到的现金cash_recp_borrow
收到其他与筹资活动有关的现金other_cash_recp_ral_fnc_act
筹资活动现金流入小计stot_cash_inflows_fnc_act
偿还债务支付现金cash_prepay_amt_borr
分配股利、利润或偿付利息支付的现金cash_pay_dist_dpcp_int_exp
支付其他与筹资的现金other_cash_pay_ral_fnc_act
筹资活动现金流出小计stot_cash_outflows_fnc_act
筹资活动产生的现金流量净额net_cash_flows_fnc_act
汇率变动对现金的影响eff_fx_flu_cash
现金及现金等价物净增加额net_incr_cash_cash_equ
销售商品、提供劳务收到的现金goods_sale_and_service_render_cash
收到的税费与返还tax_levy_refund
购买商品、接受劳务支付的现金goods_and_services_cash_paid
处置子公司及其他收到的现金net_cash_deal_subcompany
其中子公司吸收现金cash_from_mino_s_invest_sub
处置固定资产、无形资产和其他长期资产支付的现金净额fix_intan_other_asset_dispo_cash_payment
报告截止日m_timetag
公告日m_anntime

股本表 (CAPITALSTRUCTURE)

中文字段迅投字段
总股本total_capital
已上市流通A股circulating_capital
自由流通股本free_float_capital(旧版本为freeFloatCapital
限售流通股份restrict_circulating_capital
变动日期m_timetag
公告日m_anntime

主要指标 (PERSHAREINDEX)

中文字段迅投字段
每股经营活动现金流量s_fa_ocfps
每股净资产s_fa_bps
基本每股收益s_fa_eps_basic
稀释每股收益s_fa_eps_diluted
每股未分配利润s_fa_undistributedps
每股资本公积金s_fa_surpluscapitalps
扣非每股收益adjusted_earnings_per_share
净资产收益率du_return_on_equity
销售毛利率sales_gross_profit
主营收入同比增长inc_revenue_rate
净利润同比增长du_profit_rate
归属于母公司所有者的净利润同比增长inc_net_profit_rate
扣非净利润同比增长adjusted_net_profit_rate
营业总收入滚动环比增长inc_total_revenue_annual
归属净利润滚动环比增长inc_net_profit_to_shareholders_annual
扣非净利润滚动环比增长adjusted_profit_to_profit_annual
加权净资产收益率equity_roe
摊薄净资产收益率net_roe
摊薄总资产收益率total_roe
毛利率gross_profit
净利率net_profit
实际税率actual_tax_rate
预收款营业收入pre_pay_operate_income
销售现金流营业收入sales_cash_flow
资产负债比率gear_ratio
存货周转率inventory_turnover

十大股东/十大流通股东 (TOP10HOLDER/TOP10FLOWHOLDER)

提示

对于公告内披露的十大股东数量大于10条的,我们会保留原始数据,以保持和公司公告信息一致

中文字段迅投字段
公告日期declareDate
截止日期endDate
股东名称name
股东类型type
持股数量quantity
变动原因reason
持股比例ratio
股份性质nature
持股排名rank

股东数 (SHAREHOLDER)

中文字段迅投字段
公告日期declareDate
截止日期endDate
股东总数shareholder
A股东户数shareholderA
B股东户数shareholderB
H股东户数shareholderH
已流通股东户数shareholderFloat
未流通股东户数shareholderOther

获取合约信息

ContextInfo.get_instrument_detail - 根据代码获取合约详细信息

提示

旧版本客户端中,函数名为ContextInfo.get_instrumentdetail;不支持iscomplete参数

原型


-ContextInfo.get_instrument_detail(stockcode,iscomplete = Fasle)
-
-

释义

根据代码获取合约详细信息

参数

字段名数据类型解释
stockcodestring标的名称,必须是 'stock.market' 形式
iscompletebool是否获取全部字段,默认为False

返回值

根据stockcode返回一个dict。该字典数据key值有:

名称类型描述
ExchangeIDstring合约市场代码
InstrumentIDstring合约代码
InstrumentNamestring合约名称
ProductIDstring合约的品种ID(期货)
ProductNamestring合约的品种名称(期货)
ProductTypeint合约的类型, 默认-1,枚举值可参考下方说明
ExchangeCodestring交易所代码
UniCodestring统一规则代码
CreateDatestr创建日期
OpenDatestr上市日期(特殊值情况见表末)
ExpireDateint退市日或者到期日(特殊值情况见表末)
PreClosefloat前收盘价格
SettlementPricefloat前结算价格
UpStopPricefloat当日涨停价
DownStopPricefloat当日跌停价
FloatVolumefloat流通股本(单位:股。注意,部分低等级客户端中此字段为FloatVolumn)
TotalVolumefloat总股本(单位:股。注意,部分低等级客户端中此字段为FloatVolumn)
LongMarginRatiofloat多头保证金率
ShortMarginRatiofloat空头保证金率
PriceTickfloat最小价格变动单位
VolumeMultipleint合约乘数(对期货以外的品种,默认是1)
MainContractint主力合约标记,1、2、3分别表示第一主力合约,第二主力合约,第三主力合约
LastVolumeint昨日持仓量
InstrumentStatusint合约停牌状态(<=0:正常交易(-1:复牌);>=1停牌天数;)
IsTradingbool合约是否可交易
IsRecentbool是否是近月合约
ChargeTypeint期货和期权手续费方式
ChargeOpenfloat开仓手续费(率)
ChargeClosefloat平仓手续费(率)
ChargeTodayOpenfloat开今仓(日内开仓)手续费(率)
ChargeTodayClosefloat平今仓(日内平仓)手续费(率)
OptionTypeint期权类型
OpenInterestMultipleint交割月持仓倍数

提示

字段OpenDate有以下几种特殊值: 19700101=新股, 19700102=老股东增发, 19700103=新债, 19700104=可转债, 19700105=配股, 19700106=配号 字段ExpireDate为0 或 99999999 时,表示该标的暂无退市日或到期日

字段ProductType 对于股票以外的品种,有以下几种值

国内期货市场: 1-期货 2-期权(DF SF ZF INE GF) 3-组合套利 4-即期 5-期转现 6-期权(IF) 7-结算价交易(tas)

**沪深股票期权市场:**0-认购 1-认沽

外盘: 1-100:期货, 101-200:现货, 201-300:股票相关 1:股指期货 2:能源期货 3:农业期货 4:金属期货 5:利率期货 6:汇率期货 7:数字货币期货 99:自定义合约期货 107:数字货币现货 201:股票 202:GDR 203:ETF 204:ETN 300:其他

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_instrumentdetail("000001.SZ")
-	print(data)
-

get_st_status - 获取历史st状态

提示

本函数需要下载历史ST数据(过期合约K线),可通过界面端数据管理 - 过期合约数据下载

原型

get_st_status(stockcode)
-

释义

获取历史st状态

参数

字段名数据类型解释
stockcodestring股票代码,如000004.SZ(可为空,为空时取主图代码)

返回值

st范围字典 格式 {'ST': [['20210520', '20380119']], '*ST': [['20070427', '20080618'], ['20200611', '20210520']]}

示例:

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	print(get_st_status('600599.SH'))
-

ContextInfo.get_his_st_data - 获取某只股票ST的历史

提示

本函数需要下载历史ST数据(过期合约K线),可通过界面端数据管理 - 过期合约数据下载

原型

ContextInfo.get_his_st_data(stockcode)
-

释义

获取某只股票ST的历史

参数

字段名数据类型解释
stockcodestring股票代码,'stkcode.market',如'000004.SZ'

返回值

dict,st历史,key为ST,*ST,PT,历史未ST会返回{}

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	print(C.get_his_st_data('000004.SZ'))
-

ContextInfo.get_main_contract - 获取期货主力合约

提示

  1. 该函数支持实盘/回测两种模式
  2. 若要使用该函数获取历史主力合约,必须要先下载历史主力合约数据
  3. 历史主力合约数据目前通过界面端数据管理 - 过期合约数据 - 历史主力合约下载

原型

ContextInfo.get_main_contract(codemarket)
-ContextInfo.get_main_contract(codemarket,date="")
-ContextInfo.get_main_contract(codemarket,startDate="",endDate="")
-

释义

获取当前期货主力合约

参数

字段名数据类型解释
codemarketstring合约和市场,合约格式为品种名加00,如IF00.IF,zn00.SF
startDatestring开始日期(可以不写),如20180608
endDatestring结束日期(可以不写),如20190608

返回值

str,合约代码

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	symbol1 = C.get_main_contract('IF00.IF')# 获取当前主力合约
-
-	symbol2 = C.get_main_contract('IF00.IF',"20190101")# 获取指定日期主力合约
-
-	symbol3 = C.get_main_contract('IF00.IF',"20181101","20190101") # 获取时间段内全部主力合约
-
-	print(symbol1, symbol2)
-	print("="*10)
-	print(symbol3)
-

ContextInfo.get_contract_multiplier - 获取合约乘数

原型

ContextInfo.get_contract_multiplier(contractcode)
-

释义

获取合约乘数

参数

字段名数据类型解释
contractcodestring合约代码,格式为 'code.market',例如 'IF1707.IF'

返回值int,表示合约乘数

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	multiplier = C.get_contract_multiplier("rb2401.SF")
-	print(multiplier)
-

ContextInfo.get_contract_expire_date - 获取期货合约到期日

原型

ContextInfo.get_contract_expire_date(codemarket)
-

释义

获取期货合约到期日

参数

字段名数据类型解释
Codemarketstring合约和市场,如IF00.IF,zn00.SF

返回值str,合约到期日

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_contract_expire_date("IF2311.IF")
-	# print(type(data))
-	print(data)
-

ContextInfo.get_his_contract_list - 获取市场已退市合约

原型

ContextInfo.get_his_contract_list(market)
-

释义

获取市场已退市合约,需要手动补充过期合约列表

参数

字段名数据类型解释
marketstring市场,SH,SZ,SHO,SZO,IF等

返回值

list,合约代码列表

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-
-	print(C.get_his_contract_list('SHO')[:30])
-

获取期权信息

ContextInfo.get_option_detail_data - 获取指定期权品种的详细信息

原型

ContextInfo.get_option_detail_data(optioncode)
-

释义

获取指定期权品种的详细信息

参数

字段名数据类型解释
optioncodestring期权代码,如'10001506.SHO',当填写空字符串时候默认为当前主图的期权品种

返回值dict,字段如下:

字段类型说明
ExchangeIDstr期权市场代码
InstrumentIDstr期权代码
ProductIDstr期权标的的产品ID
OpenDateint发行日期
ExpireDateint到期日
PreClosefloat前收价格
SettlementPricefloat前结算价格
UpStopPricefloat当日涨停价
DownStopPricefloat当日跌停价
LongMarginRatiofloat多头保证金率
ShortMarginRatiofloat空头保证金率
PriceTickfloat最小变价单位
VolumeMultipleint合约乘数
MaxMarketOrderVolumeint涨跌停价最大下单量
MinMarketOrderVolumeint涨跌停价最小下单量
MaxLimitOrderVolumeint限价单最大下单量
MinLimitOrderVolumeint限价单最小下单量
OptUnitint期权合约单位
MarginUnitfloat期权单位保证金
OptUndlCodestr期权标的证券代码
OptUndlMarketstr期权标的证券市场
OptExercisePricefloat期权行权价
NeeqExeTypestr全国股转转让类型
OptUndlRiskFreeRatefloat期权标的无风险利率
OptUndlHistoryRatefloat期权标的历史波动率
EndDelivDateint期权行权终止日
optTypestr期权类型

示例

#encoding:gbk
-def init(ContextInfo):
-  pass
-
-def after_init(ContextInfo):
-  print(ContextInfo.get_option_detail_data('10002235.SHO'))
-

ContextInfo.get_option_list - 获取指定期权列表

原型

ContextInfo.get_option_list(undl_code,dedate,opttype,isavailable)
-

释义

获取指定期权列表。如获取历史期权,需先下载过期合约列表

参数

字段名数据类型解释
undl_codestring期权标的代码,如'510300.SH'
dedatestring期权到期月或当前交易日期,"YYYYMM"格式为期权到期月,"YYYYMMDD"格式为获取当前日期交易的期权
opttypestring期权类型,默认值为空,"CALL","PUT",为空时认购认沽都取
isavailablebool是否可交易,当dedate的格式为"YYYYMMDD"格式为获取当前日期交易的期权时,isavailable为True时返回当前可用,为False时返回当前和历史可用

返回值

list,期权合约列表

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	# 获取到期月份为202101的上交所510300ETF认购合约
-	data1=C.get_option_list('510300.SH','202101',"CALL")
-
-	# 获取20210104当天上交所510300ETF可交易的认购合约
-	data2=C.get_option_list('510300.SH','20210104',"CALL",True)
-
-	# 获取20210104当天上交所510300ETF已经上市的认购合约(包括退市)
-	data3=C.get_option_list('510300.SH','20210104',"CALL",False)
-

ContextInfo.get_option_undl_data - 获取指定期权标的对应的期权品种列表

原型

ContextInfo.get_option_undl_data(undl_code_ref)
-

释义

获取指定期权标的对应的期权品种列表

参数

字段名数据类型解释
undl_code_refstring期权标的代码,如'510300.SH',传空字符串时获取全部标的数据

返回值

指定期权标的代码时返回对应该标的的期权合约列表list

期权标的代码为空字符串时返回全部标的对应的品种列表的字典dict

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-
-	print(C.get_option_undl_data('510300.SH')[:30])
-

ContextInfo.bsm_price - 基于BS模型计算欧式期权理论价格

原型

ContextInfo.bsm_price(optionType,objectPrices,strikePrice,riskFree,sigma,days,dividend)
-

释义

基于Black-Scholes-Merton模型,输入期权标的价格、期权行权价、无风险利率、期权标的年化波动率、剩余天数、标的分红率、计算期权的理论价格

参数

字段类型说明
optionTypestr期权类型,认购:'C',认沽:'P'
objectPricesfloat期权标的价格,可以是价格列表或者单个价格
strikePricefloat期权行权价
riskFreefloat无风险收益率
sigmafloat标的波动率
daysint剩余天数
dividendfloat分红率

返回

提示

  • objectPrices为float时,返回float
  • objectPrices为list时,返回list
  • 计算结果最小值0.0001,结果保留4位小数,输入非法参数返回nan
#encoding:gbk
-import numpy as np
-
-
-def init(ContextInfo):
-  pass
-
-def after_init(ContextInfo):
-  object_prices=list(np.arange(3,4,0.01));
-  #计算剩余15天的行权价3.5的认购期权,在无风险利率3%,分红率为0,标的年化波动率为23%时标的价格从3元到4元变动过程中期权理论价格序列
-  prices=ContextInfo.bsm_price('C',object_prices,3.5,0.03,0.23,15,0)
-  print(prices)
-  #计算剩余15天的行权价3.5的认购期权,在无风险利率3%,分红率为0,标的年化波动率为23%时标的价格为3.51元的平值期权的理论价格
-  price=ContextInfo.bsm_price('C',3.51,3.5,0.03,0.23,15,0)
-  print(price)
-
-

ContextInfo.bsm_iv - 基于BS模型计算欧式期权隐含波动率

原型

ContextInfo.bsm_iv(optionType,objectPrices,strikePrice,optionPrice,riskFree,days,dividend)
-
-

释义 基于Black-Scholes-Merton模型,输入期权标的价格、期权行权价、期权现价、无风险利率、剩余天数、标的分红率,计算期权的隐含波动率

参数

字段类型说明
optionTypestr期权类型,认购:'C',认沽:'P'
objectPricesfloat期权标的价格,可以是价格列表或者单个价格
strikePricefloat期权行权价
riskFreefloat无风险收益率
sigmafloat标的波动率
daysint剩余天数
dividendfloat分红率

返回

double

#encoding:gbk
-import numpy as np
-
-def init(ContextInfo):
-    pass
-
-def after_init(ContextInfo):
-    # 计算剩余15天的行权价3.5的认购期权,在无风险利率3%,分红率为0时,标的现价3.51元,期权价格0.0725元时的隐含波动率
-    iv=ContextInfo.bsm_iv('C',3.51,3.5,0.0725,0.03,15)
-    print(iv)
-

获取除复权信息

ContextInfo.get_divid_factors - 获取除权除息日和复权因子

原型

ContextInfo.get_divid_factors(stock.market)
-

释义

获取除权除息日和复权因子

参数

字段名数据类型解释
stock.marketstring股票代码.市场代码,如 '600000.SH'

返回值

dict

key:时间戳,

value:list[每股红利,每股送转,每股转赠,配股,配股价,是否股改,复权系数]

输入除权除息日非法时候返回空dict,合法时返回输入日期的对应的dict,不输入时返回查询股票的所有除权除息日及对应dict

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	Result = C.get_divid_factors('600000.SH')
-	print(Result)
-

获取指数权重

ContextInfo.get_weight_in_index - 获取某只股票在某指数中的绝对权重

原型

ContextInfo.get_weight_in_index(indexcode, stockcode)
-

释义

获取某只股票在某指数中的绝对权重

参数

字段名数据类型解释
indexcodestring指数代码,格式为 'stockcode.market',例如 '000300.SH'
stockcodestring股票代码,格式为 'stockcode.market',例如 '600004.SH'

返回值

float:返回的数值单位是 %,如 1.6134 表示权重是 1.6134%

示例

# coding:gbk
-def init(C):
-	pass
-	
-def handlebar(C):
-	data = C.get_weight_in_index('000300.SH', '000002.SZ')
-	print(data)
-

获取成分股信息

ContextInfo.get_stock_list_in_sector - 获取板块成份股

原型

ContextInfo.get_stock_list_in_sector(sectorname, realtime)
-

释义

获取板块成份股,支持客户端左侧板块列表中任意的板块,包括自定义板块

参数

字段名数据类型解释
sectornamestring板块名,如 '沪深300','中证500','上证50','我的自选'等
realtime毫秒级时间戳实时数据的毫秒级时间戳

返回值

list:内含成份股代码,代码形式为 'stockcode.market',如 '000002.SZ'

示例

# coding:gbk
-def init(C):
-	pass
-def handlebar(C):
-	print(C.get_stock_list_in_sector('上证50'))
-

获取交易日信息

注意

  1. 该函数只能在after_init;handlebar运行

ContextInfo.get_trading_dates - 获取交易日信息

原型

ContextInfo.get_trading_dates(stockcode,start_date,end_date,count,period='1d')
-

释义

ContextInfo.get_trading_dates(stockcode,start_date,end_date,count,period='1d')

参数

字段名数据类型解释
stockcodestring股票代码,缺省值''默认为当前图代码,如:'600000.SH'
start_datestring开始时间,缺省值''为空时不使用,如:'20170101','20170101000000'
end_datestring结束时间,缺省值''默认为当前bar的时间,如:'20170102','20170102000000'
countintK线个数,必须大于0,取包括end_date往前的count个K线,但最早不会早于start_date
periodstringk线类型,'1d':日线,'1m':分钟线,'3m':三分钟线,'5m':5分钟线,'15m':15分钟线,'30m':30分钟线,'1h':小时线,'1w':周线,'1mon':月线,'1q':季线,'1hy':半年线,'1y':年线

返回值

list:K线周期(交易日)列表 period为日线时返回如['20170101','20170102',...]样式 其它返回如['20170101010000','20170102020000',...]样式

示例

# coding:gbk
-def init(C):
-	pass
-def after_init(C):
-    print(C.get_trading_dates('600000.SH','','',30,'1d'))
-def handlebar(C):
-	pass
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_data_structure.html b/reference/thinktrader_docs/innerApi_data_structure.html deleted file mode 100644 index 5ec4573..0000000 --- a/reference/thinktrader_docs/innerApi_data_structure.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 数据结构 | 迅投知识库 - - - - -

数据类

Tick - Tick 对象

行情快照数据

get_market_data_ex/get_full_tick返回对象:

字段名数据类型含义
timeint时间戳
stimestring时间戳字符串形式
lastPricefloat最新价
openfloat开盘价
highfloat最高价
lowfloat最低价
lastClosefloat前收盘价
amountfloat成交总额
volumeint成交总量(手)
pvolumeint原始成交总量(未经过股手转换的成交总量)【不推荐使用】
stockStatusint证券状态
openIntint若是股票,则openInt含义为股票状态,非股票则是持仓量openInt字段说明在新窗口打开
transactionNumfloat成交笔数(期货没有,单独计算)
lastSettlementPricefloat前结算(股票为0)
settlementPricefloat今结算(股票为0)
askPricelist[float]多档委卖价
askVollist[int]多档委卖量
bidPricelist[float]多档委买价
bidVollist[int]多档委买量

get_market_data返回对象:

字段数据类型含义
timetagstring时间戳,格式为: %Y%m%d %H:%M:%S
lastPricefloat最新价
openfloat开盘价
highfloat最高价
lowfloat最低价
lastClosefloat前收盘价
amountfloat成交额
volumefloat成交量(手)
pvolumefloat原始成交量(股)【不推荐使用】
stockStatusint作废 参考openInt
openIntfloat若是股票,则openInt含义为股票状态,非股票则是持仓量openInt字段说明
lastSettlementPricefloat昨结算价
pefloat对于股票是市盈率,对于ETF是iopv值
askPricelist委卖价
bidPricelist委买价
askVollist委卖量
bidVollist委买量
settlementPricefloat今结算价

subscribe_quote/subscribe_whole_quote回调对象:

get_full_tick 返回结构

Bar - Bar对象

bar数据是指各种频率的行情数据

字段数据类型含义
timeint时间
openfloat开盘价
highfloat最高价
lowfloat最低价
closefloat收盘价
volumefloat成交量
amountfloat成交额
settelementPricefloat今结算
openInterestfloat持仓量
preClosefloat前收盘价
suspendFlagint停牌 1停牌,0 不停牌

l2quote - Level2行情快照

字段名数据类型解释
timeint时间戳
stimestring时间戳字符串形式
lastPricefloat最新价
openfloat开盘价
highfloat最高价
lowfloat最低价
amountfloat成交额
volumeint成交总量
pvolumeint原始成交总量(未经过股手转换的成交总量)
stockStatusint证券状态
openIntint持仓量
transactionNumint成交笔数(期货没有,单独计算)
lastClosefloat前收盘价
lastSettlementPricefloat前结算(股票为0)
settlementPricefloat今结算(股票为0)
askPricelist[float]多档委卖价
askVollist[int]多档委卖量
bidPricelist[float]多档委买价
bidVollist[int]多档委买量

l2quoteaux - Level2行情快照补充

字段名数据类型解释
timeint时间戳
stimestring时间戳字符串形式
avgBidPricefloat委买均价
totalBidQuantityint委买总量
avgOffPricefloat委卖均价
totalOffQuantityint委卖总量
withdrawBidQuantityint买入撤单总量
withdrawBidAmountfloat买入撤单总额
withdrawOffQuantityint卖出撤单总量
withdrawOffAmountfloat卖出撤单总额

l2order - Level2逐笔委托

字段名数据类型解释
timeint时间戳
stimefloat时间戳浮点数形式
pricefloat委托价
volumeint委托量
entrustNoint委托号
entrustTypeint委托类型
entrustDirectionint委托方向

提示

注:上交所的撤单信息在逐笔委托的委托方向,区分撤买撤卖

  • 0 - 未知
  • 1 - 买入
  • 2 - 卖出
  • 3 - 撤买(上交所)
  • 4 - 撤卖(上交所)

l2transaction - Level2逐笔成交

字段名数据类型解释
timeint时间戳
stimestring时间戳字符串形式
pricefloat成交价
volumeint成交量
amountfloat成交额
tradeIndexint成交记录号
buyNoint买方委托号
sellNoint卖方委托号
tradeTypeint成交类型
tradeFlagint成交标志

提示

深交所逐笔成交的撤单标志,没有方向

  • 0 - 未知
  • 1 - 外盘,主买
  • 2 - 内盘,主卖
  • 3 - 撤单

l2transactioncount - Level2逐笔成交统计

字段名数据类型解释
timeint时间戳
bidNumberint主买单总单数
offNumberint主卖单总单数
ddxfloat大单动向
ddyfloat涨跌动因
ddzfloat大单差分
netOrderint净挂单量
netWithdrawint净撤单量
withdrawBidint总撤买量
withdrawOffint总撤卖量
bidNumberDxint主买单总单数增量
offNumberDxint主卖单总单数增量
transactionNumberint成交笔数增量
bidMostAmountfloat主买特大单成交额
bidBigAmountfloat主买大单成交额
bidMediumAmountfloat主买中单成交额
bidSmallAmountfloat主买小单成交额
bidTotalAmountfloat主买累计成交额
offMostAmountfloat主卖特大单成交额
offBigAmountfloat主卖大单成交额
offMediumAmountfloat主卖中单成交额
offSmallAmountfloat主卖小单成交额
offTotalAmountfloat主卖累计成交额
unactiveBidMostAmountfloat被动买特大单成交额
unactiveBidBigAmountfloat被动买大单成交额
unactiveBidMediumAmountfloat被动买中单成交额
unactiveBidSmallAmountfloat被动买小单成交额
unactiveBidTotalAmountfloat被动买累计成交额
unactiveOffMostAmountfloat被动卖特大单成交额
unactiveOffBigAmountfloat被动卖大单成交额
unactiveOffMediumAmountfloat被动卖中单成交额
unactiveOffSmallAmountfloat被动卖小单成交额
unactiveOffTotalAmountfloat被动卖累计成交额
netInflowMostAmountfloat净流入超大单成交额(lv1数据不支持计算,返回为 0,如有需求可咨询高频资金流数据)
netInflowBigAmountfloat净流入大单成交额(lv1数据不支持计算,返回为 0,如有需求可咨询高频资金流数据)
netInflowMediumAmountfloat净流入中单成交额(lv1数据不支持计算,返回为 0,如有需求可咨询高频资金流数据)
netInflowSmallAmountfloat净流入小单成交额(lv1数据不支持计算,返回为 0,如有需求可咨询高频资金流数据)
bidMostVolumeint主买特大单成交量
bidBigVolumeint主买大单成交量
bidMediumVolumeint主买中单成交量
bidSmallVolumeint主买小单成交量
bidTotalVolumeint主买累计成交量
offMostVolumeint主卖特大单成交量
offBigVolumeint主卖大单成交量
offMediumVolumeint主卖中单成交量
offSmallVolumeint主卖小单成交量
offTotalVolumeint主卖累计成交量
unactiveBidMostVolumeint被动买特大单成交量
unactiveBidBigVolumeint被动买大单成交量
unactiveBidMediumVolumeint被动买中单成交量
unactiveBidSmallVolumeint被动买小单成交量
unactiveBidTotalVolumeint被动买累计成交量
unactiveOffMostVolumeint被动卖特大单成交量
unactiveOffBigVolumeint被动卖大单成交量
unactiveOffMediumVolumeint被动卖中单成交量
unactiveOffSmallVolumeint被动卖小单成交量
unactiveOffTotalVolumeint被动卖累计成交量
netInflowMostVolumeint净流入超大单成交量
netInflowBigVolumeint净流入大单成交量
netInflowMediumVolumeint净流入中单成交量
netInflowSmallVolumeint净流入小单成交量
bidMostAmountDxfloat主买特大单成交额增量
bidBigAmountDxfloat主买大单成交额增量
bidMediumAmountDxfloat主买中单成交额增量
bidSmallAmountDxfloat主买小单成交额增量
bidTotalAmountDxfloat主买累计成交额增量
offMostAmountDxfloat主卖特大单成交额增量
offBigAmountDxfloat主卖大单成交额增量
offMediumAmountDxfloat主卖中单成交额增量
offSmallAmountDxfloat主卖小单成交额增量
offTotalAmountDxfloat主卖累计成交额增量
unactiveBidMostAmountDxfloat被动买特大单成交额增量
unactiveBidBigAmountDxfloat被动买大单成交额增量
unactiveBidMediumAmountDxfloat被动买中单成交额增量
unactiveBidSmallAmountDxfloat被动买小单成交额增量
unactiveBidTotalAmountDxfloat被动买累计成交额增量
unactiveOffMostAmountDxfloat被动卖特大单成交额增量
unactiveOffBigAmountDxfloat被动卖大单成交额增量
unactiveOffMediumAmountDxfloat被动卖中单成交额增量
unactiveOffSmallAmountDxfloat被动卖小单成交额增量
unactiveOffTotalAmountDxfloat被动卖累计成交额增量
netInflowMostAmountDxfloat净流入超大单成交额增量
netInflowBigAmountDxfloat净流入大单成交额增量
netInflowMediumAmountDxfloat净流入中单成交额增量
netInflowSmallAmountDxfloat净流入小单成交额增量
bidMostVolumeDxint主买特大单成交量增量
bidBigVolumeDxint主买大单成交量增量
bidMediumVolumeDxint主买中单成交量增量
bidSmallVolumeDxint主买小单成交量增量
bidTotalVolumeDxint主买累计成交量增量
offMostVolumeDxint主卖特大单成交量增量
offBigVolumeDxint主卖大单成交量增量
offMediumVolumeDxint主卖中单成交量增量
offSmallVolumeDxint主卖小单成交量增量
offTotalVolumeDxint主卖累计成交量增量
unactiveBidMostVolumeDxint被动买特大单成交量增量
unactiveBidBigVolumeDxint被动买大单成交量增量
unactiveBidMediumVolumeDxint被动买中单成交量增量
unactiveBidSmallVolumeDxint被动买小单成交量增量
unactiveBidTotalVolumeDxint被动买累计成交量增量
unactiveOffMostVolumeDxint被动卖特大单成交量增量
unactiveOffBigVolumeDxint被动卖大单成交量增量
unactiveOffMediumVolumeDxint被动卖中单成交量增量
unactiveOffSmallVolumeDxint被动卖小单成交量增量
unactiveOffTotalVolumeDxint被动卖累计成交量增量
netInflowMostVolumeDxint净流入超大单成交量增量
netInflowBigVolumeDxint净流入大单成交量增量
netInflowMediumVolumeDxint净流入中单成交量增量
netInflowSmallVolumeDxint净流入小单成交量增量

l2orderqueue - Level2委买委卖队列

交易类

Account - 账户对象

字段名数据类型解释
m_strAccountIDstr资金账号,用于识别不同的资金账户
m_nBrokerTypeint账号类型,表示账号的具体种类
m_dMaxMarginRatefloat保证金比率,通常用于期货账号
m_dFrozenMarginfloat冻结保证金,指投资者在交易中被冻结的保证金金额
m_dFrozenCashfloat冻结金额,指投资者在交易中被冻结的资金金额
m_dFrozenCommissionfloat冻结手续费,指投资者在交易中被冻结的手续费金额
m_dRiskfloat风险度,指投资者账户的风险程度
m_dNavfloat单位净值,用于表示基金的净值
m_dPreBalancefloat期初权益,指期初时账户的资金金额
m_dBalancefloat总资产,表示账户的总资金金额
m_dAvailablefloat可用金额,指账户中可用于交易和提取的资金金额
m_dCommissionfloat手续费 (旧版本为 m_dComission)
m_dPositionProfitfloat持仓盈亏,指当前持有的证券或期货合约的盈亏金额
m_dCloseProfitfloat平仓盈亏,在期货交易中表示已经平仓的交易的盈亏金额
m_dCashInfloat出入金净值,表示账户中出入金的净额
m_dCurrMarginfloat当前使用的保证金金额
m_dInitBalancefloat初始权益,指账户初始时的权益金额
m_strStatusstr状态,表示账户的当前状态
m_dInitCloseMoneyfloat期初平仓盈亏,指账户初始时的平仓盈亏金额
m_dInstrumentValuefloat总市值,表示持有的证券或期货合约的总市值
m_dDepositfloat入金,指账户中的入金金额
m_dWithdrawfloat出金,指账户中的出金金额
m_dPreCreditfloat上次信用额度,用于表示上次的信用额度
m_dPreMortgagefloat上次质押,指上次的质押金额
m_dMortgagefloat质押,指当前的质押金额
m_dCreditfloat信用额度,表示账户的信用额度
m_dAssetBalancefloat证券初始资金,表示股票账户的初始资金
m_strOpenDatestr起始日期,表示账户的起始日期
m_dFetchBalancefloat可取金额,指账户中可取出的金额
m_strTradingDatestr交易日,表示当前的交易日期
m_dStockValuefloat股票总市值,表示股票账户中持有的股票的总市值
m_dLoanValuefloat债券总市值,表示账户中持有的债券的总市值
m_dFundValuefloat基金总市值,包括ETF和封闭式基金在内的基金的总市值
m_dRepurchaseValuefloat回购总市值,表示账户中持有的所有回购交易的总市值
m_dLongValuefloat多单总市值,指现货账户中多单持仓的总市值
m_dShortValuefloat空单总市值,指现货账户中空单持仓的总市值
m_dNetValuefloat净持仓总市值,指现货账户中多单总市值减去空单总市值的差额
m_dAssureAssetfloat净资产,表示账户的净资产金额
m_dTotalDebitfloat总负债,表示账户的总负债金额
m_dEntrustAssetfloat可信资产,用于校对账户资金的准确性
m_dInstrumentValueRMBfloat总市值(人民币),指沪港通账户中的持仓证券的总市值
m_dSubscribeFeefloat申购费,指申购基金时支付的费用
m_dGoldValuefloat库存市值,表示黄金现货账户中黄金库存的市值
m_dGoldFrozenfloat现货冻结,表示黄金现货账户中被冻结的黄金金额
m_dMarginfloat占用保证金,用于维持保证金
m_strMoneyTypestr币种,表示账户的资金所使用的货币种类
m_dPurchasingPowerfloat购买力,指账户可用于购买投资品的金额
m_dRawMarginfloat原始保证金,指期货账户中的原始保证金金额
m_dBuyWaitMoneyfloat买入待交收金额(元),指账户中买入股票但尚未交收的金额
m_dSellWaitMoneyfloat卖出待交收金额(元),指账户中卖出股票但尚未交收的金额
m_dReceiveInterestTotalfloat本期间应计利息,指账户本期间内应计的利息金额
m_dRoyaltyfloat权利金收支,指期货期权交易中的权利金收支金额
m_dFrozenRoyaltyfloat冻结权利金,指期货期权交易中被冻结的权利金金额
m_dRealUsedMarginfloat实时占用保证金,用于股票期权交易中表示实时占用的保证金金额
m_dRealRiskDegreefloat实时风险度,用于股票期权交易中表示实时的风险度

Order - 委托对象

字段数据类型解释
m_strAccountIDstr资金账号,账号,账号,资金账号
m_strExchangeIDstr证券市场
m_strExchangeNamestr交易市场
m_strProductIDstr品种代码
m_strProductNamestr品种名称
m_strInstrumentIDstr证券代码
m_strInstrumentNamestr证券名称,合约名称
m_nRefint订单编号
m_strOrderRefstr内部委托号,下单引用等于股票的内部委托号
m_nOrderPriceTypeintEBrokerPriceType 类型,例如市价单、限价单在新窗口打开
m_nDirectionintEEntrustBS 类型,操作,多空,期货多空,股票买卖永远是 48,其他的 dir 同理
m_nOffsetFlagintEOffset_Flag_Type类型,买卖/开平,用此字段区分股票买卖,期货开、平仓,期权买卖等
m_nHedgeFlagintEHedge_Flag_Type 类型,投保
m_dLimitPricefloat委托价格,限价单的限价,即报价
m_nVolumeTotalOriginalint委托数量,最初的委托数量
m_nOrderSubmitStatusintEEntrustSubmitStatus 类型,报单状态,提交状态,股票中不需要报单状态
m_strOrderSysIDstr合同编号,委托号
m_nOrderStatusintEEntrustStatus,委托状态
m_nVolumeTradedint成交数量,已成交量
m_nVolumeTotalint委托剩余量,当前总委托量,股票中表示总委托量减去成交量
m_nErrorIDint状态ID
m_strErrorMsgstr状态信息
m_nTaskIdint任务号
m_dFrozenMarginfloat冻结金额,冻结保证金
m_dFrozenCommissionfloat冻结手续费
m_strInsertDatestr委托日期,报单日期
m_strInsertTimestr委托时间
m_dTradedPricefloat成交均价(股票)
m_dCancelAmountfloat已撤数量
m_strOptNamestr买卖标记,展示委托属性的中文
m_dTradeAmountfloat成交金额,期货的计算方式为均价乘以数量乘以合约乘数
m_eEntrustTypeintEEntrustTypes,委托类别
m_strCancelInfostr废单原因
m_strUnderCodestr标的证券代码
m_eCoveredFlagint备兑标记,'0’表示非备兑,'1’表示备兑
m_dOrderPriceRMBfloat委托价格(人民币),目前用于港股通
m_dTradeAmountRMBfloat成交金额(人民币),目前用于港股通
m_dReferenceRatefloat汇率,目前用于港股通
m_strCompactNostr合约编号
m_eCashgroupPropintEXTCompactBrushSource类型,头寸来源
m_dShortOccupedMarginfloat预估在途占用保证金,用于期权
m_strXTTradestr是否是迅投交易
m_strAccountKeystr账号key,唯一区别不同账号的key
m_strRemarkstr投资备注

Deal - 成交对象

字段数据类型解释
m_strAccountIDstr资金账号
m_strExchangeIDstr证券市场
m_strExchangeNamestr交易市场
m_strProductIDstr品种代码
m_strProductNamestr品种名称
m_strInstrumentIDstr证券代码
m_strInstrumentNamestr证券名称
m_strTradeIDstr成交编号
m_strOrderRefstr下单引用,等于股票的内部委托号
m_strOrderSysIDstr合同编号,报单编号,委托号
m_nDirectionintEEntrustBS,买卖方向 对于股票该值始终是48在新窗口打开
m_nOffsetFlagintEOffset_Flag_Type,买卖/开平,用此字段区分股票买卖,期货开、平仓,期权买卖等在新窗口打开
m_nHedgeFlagintEHedge_Flag_Type 类型,投保在新窗口打开
m_dPricefloat成交均价
m_nVolumeint成交量,期货单位手,股票做到股
m_strTradeDatestr成交日期
m_strTradeTimestr成交时间
m_dCommissionfloat手续费 (旧版本为 m_dComission)
m_dTradeAmountfloat成交额,期货 = 均价 * 量 * 合约乘数
m_nTaskIdint任务号
m_nOrderPriceTypeintEBrokerPriceType 类型,例如市价单、限价单在新窗口打开
m_strOptNamestr买卖标记,展示委托属性的中文
m_eEntrustTypeintEEntrustTypes,委托类别在新窗口打开
m_eFutureTradeTypeintEFutureTradeType 类型,成交类型在新窗口打开
m_nRealOffsetFlagintEOffset_Flag_Type 类型,实际开平,主要是区分平今和平昨在新窗口打开
m_eCoveredFlagintECoveredFlag类型,备兑标记 '0' - 非备兑,'1' - 备兑
m_nCloseTodayVolumeint平今量,不显示
m_dOrderPriceRMBfloat委托价格(人民币),目前用于港股通
m_dPriceRMBfloat成交价格(人民币),目前用于港股通
m_dTradeAmountRMBfloat成交金额(人民币),目前用于港股通
m_dReferenceRatefloat汇率,目前用于港股通
m_strXTTradestr是否是迅投交易
m_strCompactNostr合约编号
m_dCloseProfitfloat平仓盈亏,目前用于外盘
m_strRemarkstr投资备注
m_strAccountKeystr账号key,唯一区别不同账号的key
m_nRefint订单编号

Position - 持仓对象

字段名数据类型含义
m_strAccountIDstring资金账号
m_strExchangeIDstring证券市场
m_strExchangeNamestring市场名称
m_strProductIDstring品种代码
m_strProductNamestring品种名称
m_strInstrumentIDstring证券代码
m_strInstrumentNamestring证券名称
m_nHedgeFlagintEHedge_Flag_Type 类型,投保 ,股票不适用在新窗口打开
m_nDirectionintEEntrustBS,买卖方向 对于股票该值始终是48在新窗口打开
m_strOpenDatestring开仓日期 股票此字段无效
m_strTradeIDstring成交号,最初开仓位的成交
m_nVolumeint当前拥股/持仓量
m_dOpenPricefloat持仓成本 ;持仓成本 = (总买入金额 - 总卖出金额) / 剩余数量
m_strTradingDaystring在实盘运行中是当前交易日,在回测中是股票最后交易过的日期
m_dMarginfloat使用的保证金,历史的直接用ctp的,新的自己用成本价存量系数算,股票不适用
m_dOpenCostfloat开仓成本,等于成本价*第一次建仓的量,后续减持会影响,不算手续费,股票不适用
m_dSettlementPricefloat最新结算价/当前价
m_nCloseVolumeint平仓量(对于股票不适用)
m_dCloseAmountfloat平仓额(对于股票不适用)
m_dFloatProfitfloat浮动盈亏
m_dCloseProfitfloat平仓盈亏(对于股票不适用)
m_dMarketValuefloat市值/合约价值
m_dPositionCostfloat持仓成本(对于股票不适用)
m_dPositionProfitfloat持仓盈亏(对于股票不适用)
m_dLastSettlementPricefloat最新结算价(对于股票不适用)
m_dInstrumentValuefloat合约价值(对于股票不适用)
m_bIsTodaybool是否今仓
m_strStockHolderstring股东账号
m_nFrozenVolumeint冻结数量
m_nCanUseVolumeint可用数量
m_nOnRoadVolumeint在途股份
m_nYesterdayVolumeint昨夜拥股
m_dLastPricefloat最新价/当前价
m_dAvgOpenPricefloat开仓均价(对于股票不适用)
m_dProfitRatefloat盈亏比例
m_eFutureTradeTypeintEFutureTradeType 类型,成交类型在新窗口打开
m_strExpireDatestring到期日(针对逆回购)
m_strComTradeIDstring组合成交号
m_nLegIdint组合序号
m_dTotalCostfloat累计成本(自定义,股票信用用到)
m_dSingleCostfloat单股成本(自定义,股票信用用
m_nCoveredVolumeint备兑数量,用于个股期权
m_eSideFlagint持仓类型 ,用于个股期权,标记 '0' - 权利,'1' - 义务,'2' - '备兑'
m_dReferenceRatefloat汇率,目前用于港股通
m_dStructFundVolfloat分级基金可用(可分拆或可合并)
m_dRedemptionVolumefloat分级基金可赎回量
m_nPREnableVolumeint申赎可用量(记录当日申购赎回的股票或基金数量)
m_dRealUsedMarginfloat实时占用保证金,用于期权
m_dRoyaltyfloat权利金
m_dStockLastPricefloat标的证券最新价,用于期权
m_dStaticHoldMarginfloat静态持仓占用保证金,用于期权
m_nOptCombUsedVolumeint期权组合占用数量
m_nEnableExerciseVolumeint能够行使的数量,用于个股期权
m_strAccountKeystring账号key,唯一区别不同账号的key

PositionStatistics - 持仓统计对象

字段名数据类型描述
m_strAccountIDstring账号
m_strExchangeIDstring市场代码
m_strExchangeNamestring市场名称
m_strProductIDstring品种代码
m_strInstrumentIDstring合约代码
m_strInstrumentNamestring合约名称
m_nDirectionint多空
m_nHedgeFlagint投保
m_nPositionint持仓
m_nYestodayPositionint昨仓
m_nTodayPositionint今仓
m_nCanCloseVolint可平
m_dPositionCostfloat持仓成本
m_dAvgPricefloat持仓均价
m_dPositionProfitfloat持仓盈亏
m_dFloatProfitfloat浮动盈亏
m_dOpenPricefloat开仓均价
m_dUsedMarginfloat已使用保证金
m_dUsedCommissionfloat已使用的手续费
m_dFrozenMarginfloat冻结保证金
m_dFrozenCommissionfloat冻结手续费
m_dInstrumentValuefloat市值,合约价值
m_nOpenTimesint开仓次数
m_nOpenVolumeint总开仓量 中间平仓不减
m_nCancelTimesint撤单次数
m_dLastPricefloat最新价
m_dRiseRatiofloat当日涨幅
m_strProductNamestring产品名称
m_dRoyaltyfloat权利金市值
m_strExpireDatestring到期日
m_dAssestWeightfloat资产占比
m_dIncreaseBySettlementfloat当日涨幅(结)
m_dMarginRatiofloat保证金占比
m_dFloatProfitDivideByUsedMarginfloat浮盈比例(保证金)
m_dFloatProfitDivideByBalancefloat浮盈比例(动态权益)
m_dTodayProfitLossfloat当日盈亏(结)
m_nYestodayInitPositionint昨日持仓
m_dFrozenRoyaltyfloat冻结权利金
m_dTodayCloseProfitLossfloat当日盈亏(收)
m_dCloseProfitfloat平仓盈亏
m_strFtProductNamestring品种名称
m_dOpenCostfloat开仓成本

CCreditAccountDetail - 信用账号对象(非查柜台)

字段名数据类型解释
m_strAccountIDstr资金账号
m_nBrokerTypeint账号类型,1-期货账号,2-股票账号,3-信用账号,5-期货期权账号,6-股票期权账号,7-沪港通账号,11-深港通账号
m_strAccountKeystr唯一区别不同账号的key
m_dMaxMarginRatefloat保证金比率,股票的保证金率等于1
m_dFrozenMarginfloat冻结保证金,外源性,股票的保证金就是冻结资金,股票不适用
m_dFrozenCashfloat冻结金额,内外源冻结保证金和手续费四个的和
m_dFrozenCommissionfloat冻结手续费,外源性冻结资金源
m_dRiskfloat风险度,冻结资金/可用资金
m_dNavfloat单位净值
m_dPreBalancefloat期初权益,也叫静态权益,股票不适用
m_dBalancefloat总资产,动态权益,即市值
m_dAvailablefloat可用金额
m_dCommissionfloat手续费(旧版本为 m_dComission)
m_dPositionProfitfloat持仓盈亏
m_dCloseProfitfloat平仓盈亏,股票不适用
m_dCashInfloat出入金净值
m_dCurrMarginfloat当前使用的保证金,股票不适用
m_dInitBalancefloat初始权益
m_strStatusstr状态
m_dInitCloseMoneyfloat期初平仓盈亏,初始平仓盈亏
m_dInstrumentValuefloat总市值,合约价值,合约价值
m_dDepositfloat入金
m_dWithdrawfloat出金
m_dPreCreditfloat上次信用额度,股票不适用
m_dPreMortgagefloat上次质押,股票不适用
m_dMortgagefloat质押,股票不适用
m_dCreditfloat信用额度,股票不适用
m_dAssetBalancefloat证券初始资金,股票不适用
m_strOpenDatestr起始日期股票不适用
m_dFetchBalancefloat可取金额
m_strTradingDatestr交易日
m_dStockValuefloat股票总市值,期货没有
m_dLoanValuefloat债券总市值,期货没有
m_dFundValuefloat基金总市值,包括 ETF 和封闭式基金,期货没有
m_dRepurchaseValuefloat回购总市值,所有回购,期货没有
m_dLongValuefloat多单总市值,现货没有
m_dShortValuefloat单总市值,现货没有
m_dNetValuefloat净持仓总市值,净持仓市值 = 多 - 空
m_dAssureAssetfloat净资产
m_dEntrustAssetfloat可信资产,用于校对
m_dInstrumentValueRMBfloat总市值(人民币),沪港通
m_dSubscribeFeefloat申购费,申购费
m_dGoldValuefloat库存市值,黄金现货库存市值
m_dGoldFrozenfloat现货冻结,黄金现货冻结
m_dMarginfloat占用保证金,维持保证金
m_strMoneyTypestr币种
m_dPurchasingPowerfloat购买力,盈透购买力
m_dRawMarginfloat原始保证金
m_dBuyWaitMoneyfloat买入待交收金额(元),买入待交收
m_dSellWaitMoneyfloat卖出待交收金额(元),卖出待交收
m_dReceiveInterestTotalfloat本期间应计利息
m_dRoyaltyfloat权利金收支,期货期权用
m_dFrozenRoyaltyfloat冻结权利金,期货期权用
m_dRealUsedMarginfloat实时占用保证金,用于股票期权
m_dRealRiskDegreefloat实时风险度
m_dPerAssurescaleValuefloat个人维持担保比例
m_dEnableBailBalancefloat可用保证金
m_dUsedBailBalancefloat已用保证金
m_dAssureEnbuyBalancefloat可买担保品资金
m_dFinEnbuyBalancefloat可买标的券资金
m_dSloEnrepaidBalancefloat可还券资金
m_dFinEnrepaidBalancefloat可还款资金
m_dFinMaxQuotafloat融资授信额度
m_dFinEnableQuotafloat融资可用额度
m_dFinUsedQuotafloat融资已用额度
m_dFinUsedBailfloat融资已用保证金额
m_dFinCompactBalancefloat融资合约金额
m_dFinCompactFarefloat融资合约费用
m_dFinCompactInterestfloat融资合约利息
m_dFinMarketValuefloat融资市值
m_dFinIncomefloat融资合约盈亏
m_dSloMaxQuotafloat融券授信额度
m_dSloEnableQuotafloat融券可用额度
m_dSloUsedQuotafloat融券已用额度
m_dSloUsedBailfloat融券已用保证金额
m_dSloCompactBalancefloat融券合约金额
m_dSloCompactFarefloat融券合约费用
m_dSloCompactInterestfloat融券合约利息
m_dSloMarketValuefloat融券市值
m_dSloIncomefloat融券合约盈亏
m_dOtherFarefloat其它费用
m_dUnderlyMarketValuefloat标的证券市值
m_dFinEnableBalancefloat可融资金额
m_dDiffEnableBailBalancefloat可用保证金调整值
m_dBuySecuRepayFrozenMarginfloat买券还券冻结资金
m_dBuySecuRepayFrozenCommissionfloat买券还券冻结手续费
m_dSpecialEnableBalancefloat专项可融金额
m_dEncumberedAssetsfloat担保资产
m_dSloSellBalancefloat融券卖出资金
m_dDiffAssureEnbuyBalancefloat可买担保品资金调整值
m_dDiffFinEnbuyBalancefloat可买标的券资金调整值
m_dDiffFinEnrepaidBalancefloat可还款资金调整值
m_dOtherRealCompactBalancefloat其他负债合约金额
m_dOtherFinCompactInterestfloat其他负债合约利息金额
m_dUsedSloSellBalancefloat已用融券卖出资金
m_dFetchAssetBalancefloat可提出资产总额
m_dTotalEnableQuotafloat可用总信用额度
m_dTotalUsedQuotafloat已用总信用额度
m_dDebtProfitfloat负债总浮盈
m_dDebtLossfloat负债总浮亏
m_nContractEndDateint合同到期日期
m_dFinDebtfloat融资负债
m_dFinProfitAmortizedfloat融资浮盈折算
m_dSloProfitfloat融券浮盈
m_dSloProfitAmortizedfloat融券浮盈折算
m_dFinLossfloat融资浮亏
m_dSloLossfloat融券浮亏

CCreditDetail - 两融资金信息(查柜台)

字段名数据类型解释
m_dPerAssurescaleValuefloat维持担保比例
m_dBalancefloat总资产
m_dTotalDebtfloat总负债
m_dAssureAssetfloat净资产
m_dMarketValuefloat总市值
m_dEnableBailBalancefloat可用保证金
m_dAvailablefloat可用资金
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剩余融券卖出资金
m_dStockValuefloat股票市值
m_dFundValuefloat基金市值
errorstring错误信息

CreditSloEnableAmount - 可融券明细对象

提示

由于字段m_dSloRatio、m_dSloStatus提供来源和取担保品明细get_assure_contract重复,字段在2021年9月移除,后续用担保品明细接口获取,具体见 担保标的对象字段说明在新窗口打开

字段名数据类型解释
m_nPlatformIDint平台号
m_strBrokerIDstring经纪公司编号
m_strBrokerNamestring经纪公司
m_strAccountIDstring资金账号
m_strExchangeIDstring交易所
m_strInstrumentIDstring证券代码
m_nEnableAmountint融券可融数量
m_eQuerySloTypeenumEXTSloTypeQueryMode在新窗口打开,查询类型

StkCompacts - 负债合约对象

字段名数据类型解释
m_strAccountIDstring资金账号,账号,账号,资金账号
m_strExchangeIDstring交易所
m_strInstrumentIDstring证券代码
m_strExchangeNamestring交易所名称
m_strInstrumentNamestring股票名称
m_nOpenDateint合约开仓日期
m_strCompactIdstring合约编号
m_dCrdtRatiofloat融资融券保证金比例
m_strEntrustNostring委托编号
m_dEntrustPricefloat委托价格
m_nEntrustVolint委托数量
m_nBusinessVolint合约开仓数量
m_dBusinessBalancefloat合约开仓金额
m_dBusinessFarefloat合约开仓费用
m_eCompactTypeenumEXTCompactType在新窗口打开,合约类型
m_eCompactStatusenumEXTCompactStatus在新窗口打开,合约状态
m_dRealCompactBalancefloat未还合约金额
m_nRealCompactVolint未还合约数量
m_dRealCompactFarefloat未还合约费用
m_dRealCompactInterestfloat未还合约利息
m_dRepaidInterestfloat已还利息
m_nRepaidVolint已还数量
m_dRepaidBalancefloat已还金额
m_dCompactInterestfloat合约总利息
m_dUsedBailBalancefloat占用保证金
m_dYearRatefloat合约年利率
m_nRetEndDateint归还截止日
m_strDateClearstring了结日期
m_strPositionStrstring定位串
m_dPricefloat最新价
m_nOpenTimeint合约开仓时间
m_nCancelVolint合约撤单数量
m_eCashgroupPropenumEXTCompactBrushSource在新窗口打开,头寸来源
m_dUnRepayBalancefloat负债金额
m_nRepayPriorityint偿还优先级
m_dRealDefaultInterestfloat未还罚息
m_dOtherRealCompactBalancefloat其他负债合约金额
m_dOtherRealCompactInterestfloat其他负债合约利息金额

StkSubjects - 担保标的对象

字段名数据类型解释
m_nPlatformIDint平台号//目前主要用于区别不同的行情,根据此来选择对应行情
m_strBrokerIDstring经纪公司编号
m_strBrokerNamestring经纪公司名称
m_strExchangeIDstring交易所
m_strInstrumentIDstring证券代码
m_dSloRatiofloat融券保证金比例
m_eSloStatusenumEXTSubjectsStatus在新窗口打开,融券状态
m_dFinRatiofloat融资保证金比例
m_eFinStatusenumEXTSubjectsStatus在新窗口打开,融资状态
m_strAccountIDstring资金账号
m_eCreditFundCtlenumEXTCreditFundCtl在新窗口打开,融资交易控制
m_eCreditStkCtlenumEXTCreditStkCtl在新窗口打开,融券交易控制
m_eAssureStatusenumEXTSubjectsStatus在新窗口打开,是否可做担保
m_dAssureRatiofloat担保品折算比例

PassorderArguments - 下单函数参数对象

字段名数据类型解释
opTypeintpassorder的opType参数
orderTypeintpassorder的orderType参数
accountIDstring资金账号
orderCodestring交易代码
prTypeintpassorder的prType,价格类型
modelPricefloat下单价格
modelVolumeint下单量(手数或股数)
strategyNamestring策略名 _ &&& _ 投资备注

CTaskDetail - 任务对象

字段名数据类型解释
m_nTaskIdint任务号
m_eStatusenum任务状态 ETaskStatus类型,见ETaskStatus说明在新窗口打开
m_strMsgstring任务状态消息
m_startTimeint任务开始时间, 时间戳类型
m_endTimeint任务结束时间, 时间戳类型
m_cancelTimeint任务取消时间
m_nBusinessNumint已成交量
m_nGroupIdint组合Id
m_stockCodestring下单代码(不针对组合下单)
m_strAccountIDstring下单用户(单用户下单)
m_eOperationTypeenum下单操作:开平、多空……EOperationType类型, 见EOperationType说明在新窗口打开
m_eOrderTypeenum算法交易、普通交易 EOrderType类型, 见EOrderType说明在新窗口打开
m_ePriceTypeenum报价方式:对手、最新…… EPriceType类型见EPriceType说明在新窗口打开
m_dFixPricefloat委托价
m_nNumint委托量
m_strRemarkstring投资备注

CLockPosition - 期权标的持仓

字段名数据类型解释
m_strAccountIDstring账号名
m_strExchangeIDstring交易所
m_strExchangeNamestring交易所名
m_strInstrumentIDstring标的代码
m_strInstrumentNamestring标的名称
m_totalVolint总持仓量
m_lockVolint可用锁定量
m_unlockVolint未锁定量
m_coveredVolint备兑量
m_nOnRoadcoveredVolint在途备兑量

CStkOptCombPositionDetail - 期权组合持仓

字段名数据类型解释
m_strAccountIDstring账号名
m_strExchangeIDstring交易所
m_strExchangeNamestring交易所名
m_strContractAccountstring合约账号
m_strCombIDstring组合编号
m_strCombCodestring组合策略编码
m_strCombCodeNamestring组合策略名称
m_nVolumeint持仓量
m_nFrozenVolumeint冻结数量
m_nCanUseVolumeint可用数量
m_strFirstCodestring合约一
m_eFirstCodeTypeenum合约一类型 认购:48,认沽:49
m_strFirstCodeNamestring合约一名称
m_eFirstCodePosTypeenum合约一持仓类型 认购:48,义务:49,备兑:50
m_nFirstCodeAmtint合约一数量
m_strSecondCodestring合约二
m_eSecondCodeTypeenum合约二类型 认购:48,认沽:49
m_strSecondCodeNamestring合约二名称
m_eSecondCodePosTypeenum合约二持仓类型 权利:48,义务:49,备兑:50
m_nSecondCodeAmtint合约二数量
m_dCombBailBalancefloat占用保证金

entrustType - 委托类型

  • 0 - 未知
  • 1 - 正常交易业务
  • 2 - 即时成交剩余撤销
  • 3 - ETF基金申报
  • 4 - 最优五档即时成交剩余撤销
  • 5 - 全额成交或撤销
  • 6 - 本方最优价格
  • 7 - 对手方最优价格

openInt - 证券状态(股票)

编码状态
0,10默认为未知
1停牌
11开盘前S
12集合竞价时段C
13连续交易T
14休市B
15闭市E
16波动性中断V,例如(10006742.SHO)50ETF沽9月2300在2024/08/28 10:15:34 - 2024/08/28 10:18:34 触发熔断临时停牌,此时的openInt值为16
17临时停牌P
18收盘集合竞价U
19盘中集合竞价M
20暂停交易至闭市N
21获取字段异常
22盘后固定价格行情
23盘后固定价格行情完毕

openInt - 证券状态(期货)

编码状态
0默认为未知
1开盘前S
2集合竞价时段C
3连续交易T
4休市B
5闭市E
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_enum_constants.html b/reference/thinktrader_docs/innerApi_enum_constants.html deleted file mode 100644 index 45bd687..0000000 --- a/reference/thinktrader_docs/innerApi_enum_constants.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 枚举常量 | 迅投知识库 - - - - -

opType - 操作类型

期货/股指期权/商品期权 - 六键

数值描述
0开多
1平昨多
2平今多
3开空
4平昨空
5平今空

期货/股指期权/商品期权 - 四键

数值描述
6平多, 优先平今
7平多, 优先平昨
8平空, 优先平今
9平空, 优先平昨

期货/股指期权/商品期权 - 两键

数值描述
10卖出, 如有多仓, 优先平仓, 优先平今, 如有余量, 再开空
11卖出, 如有多仓, 优先平仓, 优先平昨, 如有余量, 再开空
12买入, 如有空仓, 优先平仓, 优先平今, 如有余量, 再开多
13买入, 如有空仓, 优先平仓, 优先平昨, 如有余量, 再开多
14买入, 不优先平仓
15卖出, 不优先平仓

股票/ETF/可转债买卖

数值描述
23股票/ETF/可转债买入,或沪港通、深港通股票买入
24股票/ETF/可转债卖出,或沪港通、深港通股票卖出

融资融券

数值描述
27融资买入
28融券卖出
29买券还券
30直接还券
31卖券还款
32直接还款
33担保品买入
34担保品卖出

组合交易

数值描述
25组合买入,或沪港通、深港通的组合买入
26组合卖出,或沪港通、深港通的组合卖出
27融资买入
28融券卖出
29买券还券
31卖券还款
33担保品买入
34担保品卖出
35普通账号一键买卖
36信用账号一键买卖
40期货组合开多
43期货组合开空
46期货组合平多, 优先平今
47期货组合平多, 优先平昨
48期货组合平空, 优先平今
49期货组合平空, 优先平昨

ETF期权交易

数值描述
50买入开仓
51卖出平仓
52卖出开仓
53买入平仓
54备兑开仓
55备兑平仓
56认购行权
57认沽行权
58证券锁定
59证券解锁

ETF申赎交易

数值描述
60申购
61赎回

专项两融

数值描述
70专项融资买入
71专项融券卖出
72专项买券还券
73专项直接还券
74专项卖券还款
75专项直接还款

可转债转股/回售

数值描述
80普通账户转股
81普通账户回售
82信用账户转股
83信用账户回售

orderType - 下单方式

提示

注意

一、期货不支持 1102 和 1202

二、对所有账号组的操作相当于对账号组里的每个账号做一样的操作,如:

  1. passorder(23, 1202, 'testS', '000001.SZ', 5, -1, 50000, ContextInfo),意思就是对账号组testS 里的所有账号都以最新价开仓买入 50000 元市值的 000001.SZ平安银行;
  2. passorder (60,1101,"test",'510050. SH', 5,-1,1, ContextInfo)意思就是账号test申购1个单位 (900000股)的华夏上证50ETF (只申购不买入成分股)。

单股交易

数值描述
1101单股、单账号、普通、股/手方式下单
1102单股、单账号、普通、金额(元)方式下单(只支持股票)
1113单股、单账号、总资产、比例 [0 ~ 1] 方式下单
1123单股、单账号、可用、比例[0 ~ 1]方式下单

单股交易(账号组)

数值描述
1201单股、账号组(无权重)、普通、股/手方式下单
1202单股、账号组(无权重)、普通、金额(元)方式下单(只支持股票)
1213单股、账号组(无权重)、总资产、比例 [0 ~ 1] 方式下单
1223单股、账号组(无权重)、可用、比例 [0 ~ 1] 方式下单

组合交易(单账号)

数值描述
2101组合、单账号、普通、按组合股票数量(篮子中股票设定的数量)方式下单 > 对应 volume 的单位为篮子的份
2102组合、单账号、普通、按组合股票权重(篮子中股票设定的权重)方式下单 > 对应 volume 的单位为元
2103组合、单账号、普通、按账号可用方式下单 > (底层篮子股票怎么分配?答:按可用资金比例后按篮子中股票权重分配,如用户没填权重则按相等权重分配)只对股票篮子支持

组合交易(账号组)

数值描述
2201组合、账号组(无权重)、普通、按组合股票数量方式下单
2202组合、账号组(无权重)、普通、按组合股票权重方式下单
2203组合、账号组(无权重)、普通、按账号可用方式下单只对股票篮子支持

prType - 下单选价类型

关于使用市价指令的说明

  1. 对于上交所(42,43,44,45)

    1. prType选择市价类型时时,price为保护限价,范围为(0 - 9999)表示投资者能够接受的最高买入价或最低卖出价,即买入申报的成交价格和转限价的价格不高于保护限价,卖出申报的成交价格和转限价的价格不低于保护限价,当price指定为 0 时,保护限价为对应的涨跌停价
    2. 融券卖出不允许使用市价指令
    3. 集合竞价阶段不允许使用市价指令
  2. 对于深交所(44,45,46,47,48)

    1. 市价申报只适用于有价格涨跌幅限制证券。
    2. 集合竞价阶段不允许使用市价指令
  3. 对于北交所(42,43,44,45)

    1. prType选择市价类型时时,price为保护限价,范围为(0 - 9999)表示投资者能够接受的最高买入价或最低卖出价,即买入申报的成交价格和转限价的价格不高于保护限价,卖出申报的成交价格和转限价的价格不低于保护限价,当price指定为 0 时,保护限价为对应的涨跌停价
    2. 融券卖出不允许使用市价指令
    3. 集合竞价阶段不允许使用市价指令
数值描述
-1无效(只对于algo_passorder起作用)
0卖5价
1卖4价
2卖3价
3卖2价
4卖1价
5最新价
6买1价
7买2价(组合不支持)
8买3价(组合不支持)
9买4价(组合不支持)
10买5价(组合不支持)
11指定价(只对单股情况支持,对组合交易不支持)
12涨跌停价(对手方最远端价格)
13挂单价(本方一档价格)
14对手价(对方一档价格)
18市价最优价[郑商所][期货] (不支持模拟交易中使用)
19市价即成剩撤[大商所][期货] (不支持模拟交易中使用)
20市价全额成交或撤[大商所][期货] (不支持模拟交易中使用)
21市价最优一档即成剩撤[中金所][期货] (不支持模拟交易中使用)
22市价最优五档即成剩撤[中金所][期货] (不支持模拟交易中使用)
23市价最优一档即成剩转[中金所][期货] (不支持模拟交易中使用)
24市价最优五档即成剩转[中金所][期货] (不支持模拟交易中使用)
26限价即时全部成交否则撤单[上交所[期权]] [深交所[期权]] (不支持模拟交易中使用)
27市价即成剩撤[上交所][期权] (不支持模拟交易中使用)
28市价即全成否则撤[上交所][期权] (不支持模拟交易中使用)
29市价剩转限价[上交所][期权] (不支持模拟交易中使用)
42最优五档即时成交剩余撤销申报[上交所[股票]][北交所[股票]] (不支持模拟交易中使用)
43最优五档即时成交剩转限价申报[上交所[股票]][北交所[股票]] (不支持模拟交易中使用)
44对手方最优价格委托[上交所[股票]][深交所[股票][北交所[股票]][期权]] (不支持模拟交易中使用)
45本方最优价格委托[上交所[股票]][深交所[股票][北交所[股票]][期权]] (不支持模拟交易中使用)
46即时成交剩余撤销委托[深交所][股票][期权] (不支持模拟交易中使用)
47最优五档即时成交剩余撤销委托[深交所][股票][期权] (不支持模拟交易中使用)
48全额成交或撤销委托[深交所][股票][期权] (不支持模拟交易中使用)
49盘后定价

volume - 下单数量

提示

根据 orderType 值最后一位确定 volume 的单位

单股下单时

数值描述
1股 / 手 (股票: 股,股票期权: 张,期货: 手,可转债: 张,基金:份)
2金额(元)
3比例(%)

组合下单时

数值描述
1按组合股票数量(份)
2按组合股票权重(元)
3按账号可用(%)

quicktrade - 快速下单

数值描述
0
1
2

提示

passorder是对最后一根K线完全走完后生成的模型信号在下一根K线的第一个 tick 数据来时触发下单交易;

采用quickTrade参数设置为1时,非历史 bar 上执行时(ContextInfo.is_last_bar()True),只要策略模型中调用到就触发下单交易。

quickTrade参数设置为2时,不判断 bar 状态,只要策略模型中调用到就触发下单交易,历史 bar 上也能触发下单,请谨慎使用。

enum_ - 对象属性状态字段释义

enum_EEntrustBS - 买卖方向

变量数值描述
ENTRUST_BUY48买入,多
ENTRUST_SELL49卖出,空
ENTRUST_PLEDGE_IN81质押入库
ENTRUST_PLEDGE_OUT66质押出库

EEntrustSubmitStatus - 报单状态

数值描述
48已经提交
49撤单已经提交
50修改已经提交
51已经接受
52报单已经被拒绝
53撤单已经被拒绝
54改单已经被拒绝

enum_EEntrustTypes - 委托类型

变量名称数值描述
ENTRUST_BUY_SELL48买卖
ENTRUST_QUERY49查询
ENTRUST_CANCE50撤单
ENTRUST_APPEND51补单
ENTRUST_COMFIRM52确认
ENTRUST_BIG53大宗
ENTRUST_FIN54融资委托
ENTRUST_SLO55融券委托
ENTRUST_CLOSE56信用平仓
ENTRUST_CREDIT_NORMAL57信用普通委托
ENTRUST_CANCEL_OPEN58撤单补单
ENTRUST_TYPE_OPTION_EXERCISE59行权
ENTRUST_TYPE_OPTION_SECU_LOCK60锁定
ENTRUST_TYPE_OPTION_SECU_UNLOCK61解锁
ENTRUST_QUOTATION_REPURCHASE62报价回购
ENTRUST_TYPE_OPTION_ABANDON63放弃行权
ENTRUST_AGREEMENT_REPURCHASE64协议回购
ENTRUST_TYPE_OPTION_COMB_EXERCISE65组合行权
ENTRUST_TYPE_OPTION_BUILD_COMB_STRATEGY66构建组合策略持仓
ENTRUST_TYPE_OPTION_RELEASE_COMB_STRATEGY67解除组合策略持仓
ENTRUST_TYPE_LMT_LOAN68转融通出借
ENTRUST_TYPE_LMT_LOAN_DEFER69转融通出借展期
ENTRUST_TYPE_LMT_LOAN_FINISH_AHEAD70转融通出借提前了结
ENTRUST_CROSS_MARKET_IN71跨市场场内
ENTRUST_CROSS_MARKET_OUT72跨市场场外

enum_EEntrustStatus - 委托状态

变量名称数值描述
ENTRUST_STATUS_WAIT_REPORTING49待报
ENTRUST_STATUS_REPORTED50已报(已报出到柜台,待成交)
ENTRUST_STATUS_REPORTED_CANCEL51已报待撤(对已报状态的委托撤单吗,等待柜台处理撤单请求)
ENTRUST_STATUS_PARTSUCC_CANCEL52部成待撤(已报到柜台,已有部分成交,已发出对剩余部分的撤单,待柜台处理撤单请求)
ENTRUST_STATUS_PART_CANCEL53部撤(已报到柜台,已有部分成交,剩余部分已撤)
ENTRUST_STATUS_CANCELED54已撤
ENTRUST_STATUS_PART_SUCC55部成(已报到柜台,已有部分成交)
ENTRUST_STATUS_SUCCEEDED56已成
ENTRUST_STATUS_JUNK57废单(不符合报单条件,委托被打回,相关信息再委托的废单原因字段查看)

委托状态流程

enum_EHedge_Flag_Type - 投保类型

变量名称数值描述
HEDGE_FLAG_SPECULATION49投机
HEDGE_FLAG_ARBITRAGE50套利
HEDGE_FLAG_HEDGE51套保

enum_EFutureTradeType - 成交类型

变量名称数值描述
FUTRUE_TRADE_TYPE_COMMON48普通成交
FUTURE_TRADE_TYPE_OPTIONSEXECUTION49期权成交
FUTURE_TRADE_TYPE_OTC50OTC 成交
FUTURE_TRADE_TYPE_EFPDIRVED51期转现衍生成交
FUTURE_TRADE_TYPE_COMBINATION_DERIVED52组合衍生成交

enum_EBrokerPriceType - 价格类型

变量名称数值描述
BROKER_PRICE_ANY49市价
BROKER_PRICE_LIMIT50限价
BROKER_PRICE_BEST51最优价
BROKER_PRICE_PROP_ALLOTMENT52配股
BROKER_PRICE_PROP_REFER53转托
BROKER_PRICE_PROP_SUBSCRIBE54申购
BROKER_PRICE_PROP_BUYBACK55回购
BROKER_PRICE_PROP_PLACING56配售
BROKER_PRICE_PROP_DECIDE57指定
BROKER_PRICE_PROP_EQUITY58转股
BROKER_PRICE_PROP_SELLBACK59回售
BROKER_PRICE_PROP_DIVIDEND60股息
BROKER_PRICE_PROP_SHENZHEN_PLACING68深圳配售确认
BROKER_PRICE_PROP_CANCEL_PLACING69配售放弃
BROKER_PRICE_PROP_WDZY70无冻质押
BROKER_PRICE_PROP_DJZY71冻结质押
BROKER_PRICE_PROP_WDJY72无冻解押
BROKER_PRICE_PROP_JDJY73解冻解押
BROKER_PRICE_PROP_ETF81ETF申购
BROKER_PRICE_PROP_VOTE75投票
BROKER_PRICE_PROP_YYSGYS92要约收购预售
BROKER_PRICE_PROP_YSYYJC77预售要约解除
BROKER_PRICE_PROP_FUND_DEVIDEND78基金设红
BROKER_PRICE_PROP_FUND_ENTRUST79基金申赎
BROKER_PRICE_PROP_CROSS_MARKET80跨市转托
BROKER_PRICE_PROP_EXERCIS83权证行权
BROKER_PRICE_PROP_PEER_PRICE_FIRST84对手方最优价格
BROKER_PRICE_PROP_L5_FIRST_LIMITPX85最优五档即时成交剩余转限价
BROKER_PRICE_PROP_MIME_PRICE_FIRST86本方最优价格
BROKER_PRICE_PROP_INSTBUSI_RESTCANCEL87即时成交剩余撤销
BROKER_PRICE_PROP_L5_FIRST_CANCEL88最优五档即时成交剩余撤销
BROKER_PRICE_PROP_FULL_REAL_CANCEL89全额成交并撤单
BROKER_PRICE_PROP_DIRECT_SECU_REPAY101直接还券
BROKER_PRICE_PROP_FUND_CHAIHE90基金拆合
BROKER_PRICE_PROP_DEBT_CONVERSION91债转股
BROKER_PRICE_BID_LIMIT92港股通竞价限价
BROKER_PRICE_ENHANCED_LIMIT93港股通增强限价
BROKER_PRICE_RETAIL_LIMIT94港股通零股限价
BROKER_PRICE_PROP_INCREASE_SHARE'j'增发
BROKER_PRICE_PROP_COLLATERAL_TRANSFER107担保品划转
BROKER_PRICE_PROP_NEEQ_PRICING'w'定价(全国股转 - 挂牌公司交易 - 协议转让)
BROKER_PRICE_PROP_NEEQ_MATCH_CONFIRM'x'成交确认(全国股转 - 挂牌公司交易 - 协议转让)
BROKER_PRICE_PROP_NEEQ_MUTUAL_MATCH_CONFIRM'y'互报成交确认(全国股转 - 挂牌公司交易 - 协议转让)
BROKER_PRICE_PROP_NEEQ_LIMIT'z'限价(用于挂牌公司交易 - 做市转让 - 限价买卖和两网及退市交易-限价买卖)

enum_EOffset_Flag_Type - 操作类型

变量名称数值描述
EOFF_THOST_FTDC_OF_INVALID-1无效操作
EOFF_THOST_FTDC_OF_Open48买入,开仓
EOFF_THOST_FTDC_OF_Close49卖出,平仓
EOFF_THOST_FTDC_OF_ForceClose50强平
EOFF_THOST_FTDC_OF_CloseToday51平今
EOFF_THOST_FTDC_OF_CloseYesterday52平昨
EOFF_THOST_FTDC_OF_ForceOff53强减
EOFF_THOST_FTDC_OF_LocalForceClose54本地强平
EOFF_THOST_FTDC_OF_PLEDGE_IN81质押入库
EOFF_THOST_FTDC_OF_PLEDGE_OUT66质押出库
EOFF_THOST_FTDC_OF_ALLOTMENT67股票配股

enum_EXTSubjectsStatus - 融资融券状态

变量名称数值描述
SUBJECTS_STATUS_NORMAL48正常
SUBJECTS_STATUS_PAUSE49暂停
SUBJECTS_STATUS_NOT50作废

enum_EXTCreditFundCtl - 融资交易控制

变量名称数值描述
FUND_CTL_ONLY_FIN_BUY48只允许融资买入
FUND_CTL_ONLY_SELL_CASH_REPAY49只允许卖券还款
FUND_CTL_ALL50既允许融资买入又允许卖券还款
FUND_CTL_NONE51既不允许融资买入又不允许卖券还款

enum_EXTCreditStkCtl - 融券交易控制

变量名称数值描述
STK_CTL_ONLY_SLO_SELL48只允许融券卖出
STK_CTL_ONLY_BUY_SECU_REPAY49只允许买券还券
STK_CTL_ALL50既允许融券卖出又允许买券还券
STK_CTL_NONE51既不允许融券卖出又不允许买券还券

enum_EXTSloTypeQueryMode - 查询类型

变量名称数值描述
XT_SLOTYPE_QUERYMODE_NOMARL48普通
XT_SLOTYPE_QUERYMODE_SPECIAL49专项

enum_EXTCompactType - 合约类型

变量名称数值描述
COMPACT_TYPE_ALL32不限制
COMPACT_TYPE_FIN48融资
COMPACT_TYPE_SLO49融券

enum_EXTCompactStatus - 合约状态

变量名称数值描述
COMPACT_STATUS_ALL32不限制
COMPACT_STATUS_UNDONE48未归还
COMPACT_STATUS_PART_DONE49部分归还
COMPACT_STATUS_DONE50已归还
COMPACT_STATUS_DONE_BY_SELF51自行了结
COMPACT_STATUS_DONE_BY_HAND52手工了结
COMPACT_STATUS_NOT_DEBT53未形成负债
COMPACT_STATUS_EXPIRY54合约已过期

enum_EXTCompactBrushSource - 头寸来源

变量名称数值描述
XT_COMPACT_BRUSH_SOURCE_ALL32不限制
XT_COMPACT_BRUSH_SOURCE_NORMAL48普通头寸
XT_COMPACT_BRUSH_SOURCE_SPECIAL49专项头寸

enum_EXTSpecialAssure - 是否可以用融券资金买入

变量名称数值描述
ASSURE_USE_SLO_CASH_DISABLE48担保品买入不允许使用融券资金
ASSURE_USE_SLO_CASH_ENABLE49担保品买入允许使用融券资金

enum_EOperationType - 下单操作类型/主要交易类型

变量名称数值描述
OPT_OPEN_LONG0开多
OPT_CLOSE_LONG_HISTORY1平昨多
OPT_CLOSE_LONG_TODAY2平今多
OPT_OPEN_SHORT3开空
OPT_CLOSE_SHORT_HISTORY4平昨空
OPT_CLOSE_SHORT_TODAY5平今空
OPT_CLOSE_LONG_TODAY_FIRST6优先平今多
OPT_CLOSE_LONG_HISTORY_FIRST7优先平昨多
OPT_CLOSE_SHORT_TODAY_FIRST8平空优先平今
OPT_CLOSE_SHORT_HISTORY_FIRST9平空优先平昨
OPT_CLOSE_LONG_TODAY_HISTORY_THEN_OPEN_SHORT10卖出优先平今
OPT_CLOSE_LONG_HISTORY_TODAY_THEN_OPEN_SHORT11卖出优先平昨
OPT_CLOSE_SHORT_TODAY_HISTORY_THEN_OPEN_LONG12买入优先平今
OPT_CLOSE_SHORT_HISTORY_TODAY_THEN_OPEN_LONG13买入优先平昨
OPT_CLOSE_LONG14平多
OPT_CLOSE_SHORT15平空
OPT_OPEN16开仓
OPT_CLOSE17平仓
OPT_BUY18买入
OPT_SELL19卖出
OPT_FIN_BUY20融资买入
OPT_SLO_SELL21融券卖出
OPT_BUY_SECU_REPAY22买券还券
OPT_DIRECT_SECU_REPAY23直接还券
OPT_SELL_CASH_REPAY24卖券还款
OPT_DIRECT_CASH_REPAY25直接还款
OPT_FUND_SUBSCRIBE26基金申购
OPT_FUND_REDEMPTION27基金赎回
OPT_FUND_MERGE28基金合并
OPT_FUND_SPLIT29基金分拆
OPT_PLEDGE_IN30质押入库
OPT_PLEDGE_OUT31质押出库
OPT_OPTION_BUY_OPEN32买入开仓(个股期权交易)
OPT_OPTION_SELL_CLOSE33卖出平仓(个股期权交易)
OPT_OPTION_SELL_OPEN34卖出开仓(个股期权交易)
OPT_OPTION_BUY_CLOSE35买入平仓(个股期权交易)
OPT_OPTION_COVERED_OPEN36备兑开仓(个股期权交易)
OPT_OPTION_COVERED_CLOSE37备兑平仓(个股期权交易)
OPT_OPTION_CALL_EXERCISE38认购行权(个股期权交易)
OPT_OPTION_PUT_EXERCISE39认沽行权(个股期权交易)
OPT_OPTION_SECU_LOCK40证券锁定(个股期权交易)
OPT_OPTION_SECU_UNLOCK41证券解锁(个股期权交易)
OPT_N3B_PRICE_BUY42协议转让-定价买入
OPT_N3B_PRICE_SELL43协议转让-定价卖出
OPT_N3B_CONFIRM_BUY44协议转让-成交确认买入
OPT_N3B_CONFIRM_SELL45协议转让-成交确认卖出
OPT_N3B_REPORT_CONFIRM_BUY46协议转让-互报成交确认买入
OPT_N3B_REPORT_CONFIRM_SELL47协议转让-互报成交确认卖出
OPT_N3B_LIMIT_PRICE_BUY48全国股转-限价买入
OPT_N3B_LIMIT_PRICE_SELL49全国股转-限价卖出
OPT_FUTURE_OPTION_EXERCISE50期货期权行权
OPT_CONVERT_BONDS51可转债转股
OPT_SELL_BACK_BONDS52可转债回售
OPT_STK_ALLOTMENT53股票配股
OPT_STK_INCREASE_SHARE54股票增发
OPT_COLLATERAL_TRANSFER_IN55担保品划入
OPT_COLLATERAL_TRANSFER_OUT56担保品划出
OPT_BLOCK_INTENTION_BUY57意向申报买入
OPT_BLOCK_INTENTION_SELL58意向申报卖出
OPT_BLOCK_PRICE_BUY59定价申报买入
OPT_BLOCK_PRICE_SELL60定价申报卖出
OPT_BLOCK_CONFIRM_BUY61成交申报买入
OPT_BLOCK_CONFIRM_SELL62成交申报卖出
OPT_BLOCK_CLOSE_PRICE_BUY63盘后定价买入
OPT_BLOCK_CLOSE_PRICE_SELL64盘后定价卖出
OPT_GOLD_PRICE_DELIVERY_BUY65黄金交割买
OPT_GOLD_PRICE_DELIVERY_SELL66黄金交割卖
OPT_GOLD_PRICE_MIDDLE_BUY67黄金中立仓买
OPT_GOLD_PRICE_MIDDLE_SELL68黄金中立仓卖
OPT_COMPOSE_ONEKEY_BUYSELL69组合交易一键买卖
OPT_COMPOSE_GGT_BUY70组合交易港股通买入
OPT_COMPOSE_GGT_SELL71组合交易港股通卖出
OPT_ODD_SELL72零股卖出
OPT_ETF_STOCK_BUY73成份股买入
OPT_ETF_STOCK_SELL74成份股卖出
OPT_OTC_FUND_SUBSCRIBE200场外基金认购
OPT_OTC_FUND_PURCHASE201场外基金申购
OPT_OTC_FUND_REDEMPTION202场外基金赎回
OPT_OTC_FUND_CONVERT203场外基金转换
OPT_OTC_FUND_BONUS_TYPE_UPDATE204场外基金分红方式变更
OPT_OTC_CONTRACTUAL_DEPOSIT205场外协议存款
OPT_OTC_NON_CONTRACTUAL_DEPOSIT206场外非协议存款
OPT_OTC_CONTRACTUAL_DEPOSIT_ASK207场外协议存款询价
OPT_OTC_NON_CONTRACTUAL_DEPOSIT_ASK208场外非协议存款询价
OPT_OTC_NON_CONTRACTUAL_DEPOSIT_CUR209场外非协议活期存款
OPT_OTC_DRAW_DEPOSIT210场外存单支取
OPT_OTC_STOCK_INQUIRY230网下询价
OPT_OTC_STOCK_PURCHASE231网下申购
OPT_OPTION_NS_DEPOSIT1001场外转账入金
OPT_OPTION_NS_WITHDRAW1002场外转账出金
OPT_OPTION_NS_INOUT1003场外互转
OPT_ETF_PURCHASE1004ETF申购
OPT_ETF_REDEMPTION1005ETF赎回
OPT_OUTER_BUY1006外盘买入
OPT_OUTER_SELL1007外盘卖出
OPT_OUTER_CAN_CLOSE_BUY1008外盘可平买仓
OPT_OUTER_CAN_CLOSE_SELL1009外盘可平卖仓
OPT_SLO_SELL_SPECIAL1010专项融券卖出
OPT_BUY_SECU_REPAY_SPECIAL1011专项买券还券
OPT_DIRECT_SECU_REPAY_SPECIAL1012专项直接还券
OPT_NEEQ_O3B_LIMIT_PRICE_BUY1013全国股转-两网及退市交易-限价买入
OPT_NEEQ_O3B_LIMIT_PRICE_SELL1014全国股转-两网及退市交易-限价卖出
OPT_IBANK_BOND_BUY1015投行债券买入
OPT_IBANK_BOND_SELL1016投行债券卖出
OPT_IBANK_FUND_REPURCHASE1017质押式融资回购
OPT_IBANK_BOND_REPURCHASE1018质押式融券回购
OPT_IBANK_BOND_REPAY1019质押式融资购回
OPT_IBANK_FUND_RETRIEVE1020质押式融券购回
OPT_INTEREST_FEE1021融券息费
OPT_FIN_BUY_SPECIAL1022专项融资买入
OPT_SELL_CASH_REPAY_SPECIAL1023专项卖券还款
OPT_DIRECT_CASH_REPAY_SPECIAL1024专项直接还款
OPT_FUND_PRICE_BUY1025货币基金申购
OPT_FUND_PRICE_SELL1026货币基金赎回
OPT_N3B_CALL_AUCTION_BUY1027协议转让-集合竞价买入
OPT_N3B_CALL_AUCTION_SELL1028协议转让-集合竞价卖出
OPT_N3B_AFTER_HOURS_BUY1029全国股转-盘后协议买入
OPT_N3B_AFTER_HOURS_SELL1030全国股转-盘后协议卖出
OPT_ETF_HEDGE1031ETF套利
OPT_QUOTATION_REPURCHASE_BUY1032报价回购买入
OPT_QUOTATION_REPURCHASE_STOP1033报价回购终止续做
OPT_QUOTATION_REPURCHASE_BEFORE1034报价回购提前购回
OPT_QUOTATION_REPURCHASE_RESERVATION1035报价回购购回预约
OPT_QUOTATION_REPURCHASE_CANCEL1036报价回购取消预约
OPT_BLOCK_CONFIRM_MATCH_BUY1037成交申报配对买入
OPT_BLOCK_CONFIRM_MATCH_SELL1038成交申报配对卖出
OPT_FUTURE_OPTION_ABANDON1039期货期权放弃行权
OPT_ONEKEY_TRANSFER1040一键划转
OPT_ONEKEY_TRANSFER_IN1041一键划入
OPT_ONEKEY_TRANSFER_OUT1042一键划出
OPT_AFTER_FIX_BUY1043盘后定价买入
OPT_AFTER_FIX_SELL1044盘后定价卖
OPT_AGREEMENT_REPURCHASE_TRANSACTION_DEC_FORWARD1045成交申报正回购
OPT_AGREEMENT_REPURCHASE_TRANSACTION_DEC_REVERSE1046成交申报逆回购
OPT_AGREEMENT_REPURCHASE_EXPIRE_CONFIRM1047到期确认
OPT_AGREEMENT_REPURCHASE_ADVANCE_REPURCHASE1048提前购回正回购
OPT_AGREEMENT_REPURCHASE_ADVANCE_REVERSE1049提前购回逆回购
OPT_AGREEMENT_REPURCHASE_EXPIRE_RENEW1050到期续做正回购
OPT_AGREEMENT_REPURCHASE_EXPIRE_REVERSE1051到期续做逆回购
OPT_TRANSACTION_IN_CASH_BUY1052现券买入
OPT_TRANSACTION_IN_CASH_SELL1053现券卖出
OPT_OUTRIGHT_REPO_FUND_REPURCHASE1054买断式融资回购
OPT_OUTRIGHT_REPO_BOND_REPURCHASE1055买断式融券回购
OPT_OUTRIGHT_REPO_BOND_REPAY1056买断式融资购回
OPT_OUTRIGHT_REPO_FUND_RETRIEVE1057买断式融券购回
OPT_DISTRIBUTION_BUYING1058分销买入
OPT_FIXRATE_TO_FLOATINGRATE1059固定利率换浮动利率
OPT_FLOATINGRATE_TO_FIXRATE1060浮动利率换固定利率
OPT_IBANK_TRANSFER_OUT1061银行间转出托管
OPT_IBANK_TRANSFER_IN1062银行间转入托管
OPT_AGREEMENT_REPURCHASE_INTENTION_BUY1063意向申报正回购买入
OPT_AGREEMENT_REPURCHASE_INTENTION_SELL1064意向申报正回购卖出
OPT_AGREEMENT_REPURCHASE_BIZ_APPLY_CONFIRM1065协议回购成交申报确认
OPT_AGREEMENT_REPURCHASE_BIZ_APPLY_REJECT1066协议回购成交申报拒绝
OPT_AGREEMENT_REPURCHASE_CONTINUE_CONFIRM1067协议回购到期续做申报确认
OPT_AGREEMENT_REPURCHASE_CONTINUE_REJECT1068协议回购到期续做申报拒绝
OPT_AGREEMENT_REPURCHASE_INTENTION_CHANGE_BONDS1069协议回购换券申报
OPT_AGREEMENT_REPURCHASE_INTENTION_CHANGE_BONDS_CONFIRM1070协议回购换券申报确认
OPT_AGREEMENT_REPURCHASE_INTENTION_CHANGE_BONDS_REJECT1071协议回购换券申报拒绝
OPT_AGREEMENT_REPURCHASE_STOP_AHEAD_CONFIRM1072协议回购正回购提前终止申报确认
OPT_AGREEMENT_REPURCHASE_STOP_AHEAD_REJECT1073协议回购正回购提前终止申报拒绝
OPT_AGREEMENT_REPURCHASE_RELEASE_PLEDGE1074协议回购正回购方解除质押申报
OPT_AGREEMENT_REPURCHASE_RELEASE_PLEDGE_CONFIRM1075协议回购正回购解除质押申报确认
OPT_AGREEMENT_REPURCHASE_RELEASE_PLEDGE_REJECT1076协议回购正回购解除质押申报拒绝
OPT_AGREEMENT_REPURCHASE_EXPIRE_CONFIRM_SELL1077深圳到期确认卖出
OPT_LOAN_DISTRIBUTION_BUY1078债券分销
OPT_PREFERENCE_SHARES_BIDDING_BUY1079优先股竞价买入
OPT_PREFERENCE_SHARES_BIDDING_SELL1080优先股竞价卖出
OPT_TOC_BOND1081债券转托管
OPT_TOC_FUND1082基金转托管
OPT_IBANK_BORROW1083同业拆入
OPT_IBANK_LOAN1084同业拆出
OPT_IBANK_BORROW_REPAY1085拆入还款
OPT_IBANK_LOAN_REPAY1086拆出还款
OPT_FINANCIAL_PRODUCT_BUY1087理财产品申购
OPT_FINANCIAL_PRODUCT_SELL1088理财产品赎回
OPT_OPTION_COMB_EXERCISE1089组合行权
OPT_OPTION_BUILD_COMB_STRATEGY1090构建组合策略
OPT_OPTION_RELEASE_COMB_STRATEGY1091解除组合策略
OPT_AGREEMENT_REPURCHASE_REVERSE_STOP_AHEAD_CONFIRM1092协议回购逆回购提前终止申报确认
OPT_AGREEMENT_REPURCHASE_REVERSE_STOP_AHEAD_REJECT1093协议回购逆回购提前终止申报拒绝
OPT_AGREEMENT_REPURCHASE_REVERSE_RELEASE_PLEDGE1094协议回购逆回购方解除质押申报
OPT_AGREEMENT_REPURCHASE_REVERSE_RELEASE_PLEDGE_CONFIRM1095协议回购逆回购解除质押申报确认
OPT_AGREEMENT_REPURCHASE_REVERSE_RELEASE_PLEDGE_REJECT1096协议回购逆回购解除质押申报拒绝
OPT_BOND_TENDER1097债券投标
OPT_FINANCIAL_PRODUCT_CALL1098理财产品认购
OPT_NEEQ_O3B_CONTINUOUS_AUCTION_BUY1099全国股转-北交所买入
OPT_NEEQ_O3B_CONTINUOUS_AUCTION_SELL1100全国股转-北交所卖出
OPT_NEEQ_O3B_ASK_PRICE1101全国股转-申购-询价申报
OPT_NEEQ_O3B_PRICE_CONFIRM1102全国股转-申购-申购申报
OPT_NEEQ_O3B_BLOCKTRADING_BUY1103全国股转-大宗交易买入
OPT_NEEQ_O3B_BLOCKTRADING_SELL1104全国股转-大宗交易卖出
OPT_LMT_LOAN_SET1105转融通非约定出借申报
OPT_LMT_LOAN_CONVENTION1106转融通约定出借申报
OPT_LMT_LOAN_RENEWAL1107转融通出借展期
OPT_LMT_LOAN_SETTLE_EARLY1108转融通出借提前了结
OPT_CROSS_MARKET_IN_ETF_PURCHASE1109跨市场ETF场内申购
OPT_CROSS_MARKET_IN_ETF_REDEMPTION1110跨市场ETF场内赎回
OPT_CROSS_MARKET_OUT_ETF_PURCHASE1111跨市场ETF场外申购
OPT_CROSS_MARKET_OUT_ETF_REDEMPTION1112跨市场ETF场外赎回
OPT_CREDIT_APPOINTMENT1113券源预约
OPT_OFF_IPO_PUB_PRICE1114网下申购-公开发行询价
OPT_OFF_IPO_PUB_PURCHASE1115网下申购-公开发行申购
OPT_OFF_IPO_NON_PUB_PRICE1116网下申购-非公开发行询价
OPT_OFF_IPO_NON_PUB_PURCHASE1117网下申购-非公开发行申购
OPT_IBANK_PUT1118债券回售
OPT_IBANK_BOND_BORROW1119债券借贷融入
OPT_IBANK_BOND_LEND1120债券借贷融出
OPT_IBANK_BOND_BORROW_REPAY1121债券借贷融入购回
OPT_IBANK_BOND_LEND_RETRIEVE1122债券借贷融出购回
OPT_IBANK_BOND_DISPLACE1123债券借贷-质押券置换
OPT_LENDING_INTEGRATE_INTO1124融券通-预约融券融入
OPT_LENDING_MELT_OUT1125融券通-预约融券融出
OPT_FICC_MANUAL_DECLARE_BUY1126固收业务-点击成交-报价申报买入
OPT_FICC_MANUAL_DECLARE_SELL1127固收业务-点击成交-报价申报卖出
OPT_FICC_MANUAL_CONFIRM_BUY_CONFIRM1128固收业务-点击成交-报价确认-买入-确认
OPT_FICC_MANUAL_CONFIRM_BUY_REJECT1129固收业务-点击成交-报价确认-买入-拒绝
OPT_FICC_MANUAL_CONFIRM_SELL_CONFIRM1130固收业务-点击成交-报价确认-卖出-确认
OPT_FICC_MANUAL_CONFIRM_SELL_REJECT1131固收业务-点击成交-报价确认-卖出-拒绝
OPT_FICC_CONSULT_DECLARE_BUY1132固收业务-协商成交-协商申报买入
OPT_FICC_CONSULT_DECLARE_SELL1133固收业务-协商成交-协商申报卖出
OPT_FICC_CONSULT_CONFIRM_BUY_CONFIRM1134固收业务-协商成交-协商确认-买入-确认
OPT_FICC_CONSULT_CONFIRM_BUY_REJECT1135固收业务-协商成交-协商确认-买入-拒绝
OPT_FICC_CONSULT_CONFIRM_SELL_CONFIRM1136固收业务-协商成交-协商确认-卖出-确认
OPT_FICC_CONSULT_CONFIRM_SELL_REJECT1137固收业务-协商成交-协商确认-卖出-拒绝
OPT_FICC_ENQUIRY_DECLARE_BUY1138固收业务-询价成交-询价申报买入
OPT_FICC_ENQUIRY_DECLARE_SELL1139固收业务-询价成交-询价申报卖出
OPT_FICC_ENQUIRY_REPLAY_BUY_CONFIRM1140固收业务-询价成交-报价回复-买入-确认
OPT_FICC_ENQUIRY_REPLAY_BUY_REJECT1141固收业务-询价成交-报价回复-买入-拒绝--预留字段
OPT_FICC_ENQUIRY_REPLAY_SELL_CONFIRM1142固收业务-询价成交-报价回复-卖出-确认
OPT_FICC_ENQUIRY_REPLAY_SELL_REJECT1143固收业务-询价成交-报价回复-卖出-拒绝--预留字段
OPT_FICC_ENQUIRY_INQUIRY_BUY_CONFIRM1144固收业务-询价成交-询价成交-买入-确认
OPT_FICC_ENQUIRY_INQUIRY_BUY_REJECT1145固收业务-询价成交-询价成交-买入-拒绝--预留字段
OPT_FICC_ENQUIRY_INQUIRY_SELL_CONFIRM1146固收业务-竞买成交-询价成交-卖出-确认
OPT_FICC_ENQUIRY_INQUIRY_SELL_REJECT1147固收业务-竞买成交-询价成交-卖出-拒绝--预留字段
OPT_FICC_BINDDING_RESERVE_BUY1148固收业务-竞买成交-竞买预约买入
OPT_FICC_BINDDING_RESERVE_SELL1149固收业务-竞买成交-竞买预约卖出
OPT_FICC_BINDDING_DECLARE_BUY1150固收业务-竞买成交-竞买申报买入
OPT_FICC_BINDDING_DECLARE_SELL1151固收业务-竞买成交-竞买申报卖出
OPT_FICC_BINDDING_PRICE_DECLARE_BUY1152固收业务-竞买成交-应价申报买入
OPT_FICC_BINDDING_PRICE_DECLARE_SELL1153固收业务-竞买成交-应价申报卖出
OPT_OPTION_BUY_CLOSE_THEN_OPEN1154买入优先平仓,个股期权交易业务补充类型
OPT_OPTION_SELL_CLOSE_THEN_OPEN1155卖出优先平仓
OPT_FUND_TRANSFER_IN1156资金划入
OPT_FUND_TRANSFER_OUT1157资金划出

enum_EOrderType - 算法交易、普通交易类型

变量名称数值描述
OTP_ORDINARY0常规
OTP_ALGORITHM1算法交易
OTP_RANDVOLUME2随机量交易
OTP_ALGORITHM33算法交易3
OTP_ZXJT4中信建投算法
OTP_ZSGS5隔时交易
OTP_ORDINARY_BASKET_TRIGGER_SINGLE_ORDER6普通交易的触价单笔委托方式
OTP_ALGORITHM_BASKET_TRIGGER_SINGLE_ORDER7算法交易的触价单笔委托方式
OTP_ZXZQ8中信证券算法
OTP_GENUS9金纳算法
OTP_JAZZ10爵士算法
OTP_VWAP11智能VWAP
OTP_TWAP12智能TWAP
OTP_XTALGO13智能算法
OTP_HUACHUANG14华创算法
OTP_HUARUN15华润算法
OTP_CUSTOM16回转算法
OPT_EXTERN17主动算法
OTP_GUANGFA18广发算法

enum_EPriceType - 价格类型

变量名称数值描述
PRTP_SALE50卖5
PRTP_SALE41卖4
PRTP_SALE32卖3
PRTP_SALE23卖2
PRTP_SALE14卖1
PRTP_LATEST5最新价
PRTP_BUY16买1
PRTP_BUY27买2
PRTP_BUY38买3
PRTP_BUY49买4
PRTP_BUY510买5
PRTP_FIX11指定价
PRTP_MARKET12市价_涨跌停价
PRTP_HANG13挂单价
PRTP_COMPETE14对手价
PRTP_AUTO15自动盘口
PRTP_CLOSE16昨收价
PRTP_AVERAGE17大宗加权平均价
PRTP_MARKET_BEST18市价_最优价
PRTP_MARKET_CANCEL19市价_即成剩撤
PRTP_MARKET_CANCEL_ALL20市价_全额成交或撤
PRTP_MARKET_CANCEL_121市价_最优1档即成剩撤
PRTP_MARKET_CANCEL_522市价_最优5档即成剩撤
PRTP_MARKET_CONVERT_123市价_最优1档即成剩转
PRTP_MARKET_CONVERT_524市价_最优5档即成剩转
PRTP_STK_OPTION_ASK25询价
PRTP_STK_OPTION_FIX_CANCEL_ALL26限价即时全部成交否则撤单
PRTP_STK_OPTION_MARKET_CACEL_LEFT27市价即时成交剩余撤单
PRTP_STK_OPTION_MARKET_CANCEL_ALL28市价即时全部成交否则撤单
PRTP_STK_OPTION_MARKET_CONVERT_FIX29市价剩余转限价
PRTP_SALE630卖6
PRTP_SALE731卖7
PRTP_SALE832卖8
PRTP_SALE933卖9
PRTP_SALE1034卖10
PRTP_BUY635买6
PRTP_BUY736买7
PRTP_BUY837买8
PRTP_BUY938买9
PRTP_BUY1039买10
PRTP_UPPER_LIMIT_PRICE40涨停价
PRTP_LOWER_LIMIT_PRICE41跌停价
PRTP_MARKET_SH_CONVERT_5_CANCEL42最优五档即时成交剩余撤销
PRTP_MARKET_SH_CONVERT_5_LIMIT43最优五档即时成交剩转限价
PRTP_MARKET_PEER_PRICE_FIRST44对手方最优价格委托
PRTP_MARKET_MINE_PRICE_FIRST45本方最优价格委托
PRTP_MARKET_SZ_INSTBUSI_RESTCANCEL46即时成交剩余撤销委托
PRTP_MARKET_SZ_CONVERT_5_CANCEL47最优五档即时成交剩余撤销委托
PRTP_MARKET_SZ_FULL_REAL_CANCEL48全额成交或撤销委托
PRTP_AFTER_FIX_PRICE49盘后定价申报

enum_ETaskStatus - 任务状态

变量名称数值描述
TASK_STATUS_UNKNOWN0未知
TASK_STATUS_WAITING1等待
TASK_STATUS_COMMITING2提交中
TASK_STATUS_RUNNING3执行中
TASK_STATUS_PAUSE4暂停
TASK_STATUS_CANCELING_DEPRECATED5撤销中(已弃用)
TASK_STATUS_EXCEPTION_CANCELING_DEPRECATED6异常撤销中(已弃用)
TASK_STATUS_COMPLETED7完成
TASK_STATUS_CANCELED8已撤
TASK_STATUS_REJECTED9打回
TASK_STATUS_EXCEPTION_CANCELED10异常终止
TASK_STATUS_DROPPED11放弃(用于组合交易中,放弃补单)
TASK_STATUS_FORCE_CANCELED_DEPRECATED12强制终止(已弃用)
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_interface_operation.html b/reference/thinktrader_docs/innerApi_interface_operation.html deleted file mode 100644 index f7bcd3b..0000000 --- a/reference/thinktrader_docs/innerApi_interface_operation.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 界面操作 | 迅投知识库 - - - - -

新建策略

模型创建方法有三种:

方法一,在【模型研究】界面,使用系统预置的各种示例模型,点击后方“编辑”按钮,并在弹出的【策略编辑器】中以此示例模型代码为基础进行编写。

我的主页-编辑模型

方法二,在【模型研究】界面,点击新建模型,选择 Python 模型,在弹出的【策略编辑器】中从头到尾编写一个用户自己的量化模型。

模型研究-新建模型

方法三,在模型管理面板右键,选择新建模型,并选择 Python 模型。

模型管理-新建模型

导入导出策略

QMT系统支持将策略以加密的模式进行导出或导入,用户可以便捷的迁移系统本地策略。

img

方式一.模型研究界面导入导出

img

方式二.模型研究界面导入导出

策略编写

【策略编辑器】是迅投专门为模型开发者设计的,集成了模型列表、函数列表、函数帮助、模型基本信息、参数设置、回测参数等多个部分,拥有代码高亮、自动补全等便捷功能于一体的便捷的模型编辑、开发环境。

模型编辑页面 右侧可选择策略默认的周期、品种

编写 Python 策略需在开始时定义编码格式,如 gbk

之后可选择导入第三方库,所选第三方库要在券商管理端白名单内才可运行。

Init 方法和 handlebar 方法的定义是必须的。Init 方法会在策略运行开始时调用一次,用以初始化所需对象(包裹在 ContextInfo 对象中传递),设定股票池等。

Handlebar 方法会在历史 K 线上逐 K 线调用,系统会保存函数所做更改。

在盘中交易时间,handlebar 函数会随行情推送(tick 数据)被调用,当一个 tick 数据为所在 K 线最后一个 tick 时,此 tick 调用的 handlebar 所做的更改会被系统保存,如有交易指令,会在下一根K 线的第一个 tick 到来时发送;其他 tick 可以打印运行结果,但 handlebar 所做更改不会被保存,也不会发送交易信号。

编写创建完模型后,对应模型的基本信息和回测参数进行设置。

基本信息-字段描述

字段描述
名称填写模型名称
快捷码默认根据模型名称自动生成拼音首字母拼写,如需自定义可以手动进行更改,用于键盘精灵快速引用模型
说明简单的说明模型功能
分类保存当前模型到某个分类下面
位置模型回测或运行时的位置,有副图、主图叠加、主图三种显示位置
默认周期点击模型回测或运行时的默认主图周期,可手动切换
默认品种点击模型回测或运行时的默认主图品种,可手动切换
复权方式提供不复权、前复权、后复权、等比前复权、等比后复权 5 种复权方式
快速计算限制计算范围,默认为 0 时模型运行会从模型设置的默认品种(主图)的第一根 K 线开始计算,设置为 n 则从当前 K 线再往前 n 个 K 线开始计算
刷新间隔用来设置策略运行的时间间隔。设置了刷新间隔,即每隔一段时间策略按照当前行情运行一次
加密公式加密后的公式只有输入密码才可以查看源代码
凭密码导出公式此项只有在开启 “加密公式” 后才能生效,生效后只能使用密码导出到本地
用法注释简短的说明模型使用的一些注意项,可不填

策略编辑器-基本信息

回测模式指策略以历史行情为依据进行运算,投资者可观察该策略在历史行情所获得的年化收益率、夏普比率、最大回撤、信息比率等指标表现。

回测参数-字段描述

字段描述
开始时间
结束时间
设置模型回测时间区间
基准设置模型收益的参考基准
初始资金设置模型回测的初始资金
保证金比例设置期货的保证金比例
滑点设置回测撮合时的滑点,模拟真实交易的冲击成本
手续费类型支持按成交额比例或者固定值计算手续费
买入印花税设置买入印花税比例
卖出印花税设置卖出印花税比例
最低佣金设置单笔交易的最低佣金数额
买入佣金设置买入标的时的佣金比例
平昨佣金设置股票、期货平昨佣金比例
平今佣金设置期货平今佣金比例
最大成交比例控制回测中最大成交量不超过同期成交量*最大成交比例。可以点击此参数旁边的'?'按钮了解详情

策略编辑器-回测参数

使用者也可在参数设置中设置好参数值,参数名为变量名,模型中可以调用。最新值为变量默认值,运行/回测模式使用。

其中最小 / 最大 / 步长项,为遍历参数。初始项可不填。最小 / 最大都是包含在遍历区间内的,如图所示三个变量,测评时会遍历(20-3+1)[(160-140)/10+1][(-150+250)/10+1] 种组合。

策略编辑器-参数设置

点击公式测评,可选择回测模式支持的指标,如单位净值,最大回撤等,作为评价标准。

策略编辑器-公式评测

点击优化,评测结果弹窗显示不同参数变量组合下的回测结果,根据结果选择最优参数组合。可点击所需指标进行排序。需要注意的是,在测评之前,需要针对所选品种和周期补充数据。

补充数据

在创建用户的模型之前,用户应使用客户端提供的“数据管理”功能,选择并补充模型所需的相应市场、品种以及对应周期的历史数据。

操作-数据管理

策略运行

策略编写完毕后,点击编译,可保存策略。编译按钮在 Python 策略中只起保存功能,不会检查语法与引用的正误。之后点击运行可以看到策略运行效果(如有错误,会在日志输出的位置报错)。

策略编辑器-运行

如当前系统所处界面为“行情”界面或“交易”界面,点击运行之前,需在行情中手动设置好 K 线品种和周期,点击运行后,策略即可在当前主图下运行,如下图所示。

策略运行状态之一

如系统当前界面处于“我的主页”界面或“模型研究”和“模型交易”等非行情界面,点击运行时,会基于策略编辑器 - 基本信息中所设置的默认周期和默认品种运行。

策略运行状态之二

当选择的运行位置为副图时, 如想关闭策略,将主图下方策略运行的附图关闭即可。

关闭运行中的策略之一

关闭运行中的策略之二

当选择的运行位置为主图叠加时,如想关闭策略,在主图上右键单击取消叠加指标即可。

当选择的运行位置为主图时,键盘精灵输入KLINE即可结束模型运行。

关闭运行中的策略之三

点击策略编辑器上放的停止按钮

策略调试

如果策略运行不成功,需要进行策略调试这一步。当运行出错时,报错信息会显示在日志输出面板,以供修改调试之用。

策略调试-输出日志

独立python进程

注意

在您充分理解软件使用前,不建议开启该功能

勾选此功能后,程序将把代码作为main执行脚本,不会触发init,handlebar等函数

反之,系统会import策略,按规则触发init handlebar等函数

策略回测

对某一策略编译成功后,点击回测,可以通过日志输出查看模型基于历史行情数据回测情况和表现。

在回测之前,需要设置好策略回测运行的主图品种和周期,以及相关的回测参数。回测主图和周期可以在策略编辑器-基本信息中进行设置,回测开始和结束时间、基准、费率等可以在策略编辑器 - 回测参数中设置。

用户在回测前,需根据此策略回测运行的主图、周期和时间,在【数据管理】中对行情数据进行下载补充。如回测时间设置为 20180930 至 20190530 ,运行主图为 SZ.000001 平安银行日线,补充数据时可进行如下设置。

操作-数据管理

如果回测正常的话,主界面会跳转到模型设置的默认标的和默认周期界面,并输出模型绩效分析结果。

策略编辑器-模型回测

此时,可最小化或者关闭【策略编辑器】,并对回测结果进行分析,随着光标在 K 线主图上的移动,右边回测结果展示窗口会动态显示截止光标所在当日的绩效分析结果(包括年化收益,基准年化收益,单位净值,下方差,信息比率,夏普比率,波动率,索提诺比率,阿尔法系数,贝塔系数,跟踪误差,最大回撤,胜率等)、当日买入、当日卖出、持仓列表。

以上列表均可鼠标右键复制和导出数据。

回测结果分析-回测结果随十字光标移动动态展示

如需根据模型生成的买入、卖出列表进行手工交易,则可直接点击“买入'或“卖出'按钮,系统会弹出下单界面由用户进行确认后进行普通交易下单或算法交易下单。交易方式可点击卖出按钮右侧的普通交易进行切换设置。

回测结果买入

副图回测指标:提供图形化的展示,除去绩效分析的相关指标外,用户可以通过编辑模型代码自定义输出一些特色指标,鼠标右键可以选择复制模型运行结果(每一天的数据)。

回测结果分析-附图指标输出

另外,回测结果还提供了持仓分析、历史板块汇总、操作明细、日志输出等信息,方便用户进行深入分析。

说明

持仓分析: 可查看光标所在当天持仓的行业分布,展示在相关行业的市值情况、盈利情况、权重以及股票数量情况,鼠标右键可以复制和导出数据;可切换对比基准,和模型持仓进行对比。

历史板块汇总: 可查看模型自回测日期以来到光标所在日期该模型交易标的的汇总信息,包括累计盈亏、累计交易量、累计交易额、持仓天数等;点击选择板块,可以自行选择其常用板块进行板块的各项数据累计汇总;汇总数据均可以进行排序,鼠标右键可以复制和导出数据。

操作明细: 可查看模型回测的历史每一笔交易的明细。

日志输出: 可用于调试输出模型回测和运行情况。

回测结果分析-绩效分析、当日买入、当日卖出、当日持仓

回测结果分析-持仓分析

回测结果分析-历史品种汇总、历史板块汇总

回测结果分析-操作明细、日志输出

回测、运行两种模式的区别

在模型编辑器中,有“回测”和“运行”两个按钮,分别代表两种模式,它们之间的区别如下:

(1)回测模式指策略以历史行情为依据,以回测参数中的开始时间、结束时间为回测时间区间进行运算,投资者可观察该策略在历史行情所获得的年化收益率、夏普比率、最大回撤、信息比率等指标表现。

(2)运行模式指策略根据实时行情信号进行运算,以主图行情开始时间到当前时间为运行区间,进行策略的模拟运行,但不进行真实的委托。

注意

如果需要向模拟/实盘柜台发送真实的委托,请将策略加入到“模型交易”中。

配置/获取模拟账号

注意

  1. 券商QMT的模拟账号通常联系对应券商解决相关问题
  2. 以下均为迅投模拟账号相关指南
  3. 迅投模拟账号的格式如下
    1. 股票账号:200xxxx
    2. 期货账号:100xxxx
    3. 期权账号:600xxxx

提示

  1. 新用户注册投研账号,可获得 14 天模拟仿真交易体验
  2. VIP权限用户可以通过用户中心 - 下载中心的客户端进行模拟交易,交易支持股票/期货/股票期权三个市场
  1. 投研服务平台在新窗口打开登录您的投研账号,如您没有投研账号,请先注册在新窗口打开

  2. 点击右上角的用户中心,选择下方栏目的模拟撮合按钮,可以看到您的投研模拟账号

模拟账号

  1. VIP权限用户可以通过用户中心 - 下载中心的客户端进行模拟交易,交易支持股票/期货/股票期权三个市场

模拟交易环境说明

  1. 客户端配置账号

自动导出交易记录

操作界面快捷键

操作描述
SHIFT+Q分时 K线 附图变量查看器
SHIFT+Gk线 附图组合模型持仓界面
SHIFT+Lk线 锁定十字线
SHIFT+S星空图
CTRL+Windows键快速唤起多屏
CTRL+O分时 K线 叠加品种
CTRL+Z分时 K线 添加自选
CTRL+M跳转到多股同列界面
CTRL+X跳转到多周期同列界面
CTRL+K线 多股同列 向左快捷移动十字线
CTRL+K线 多股同列 向右快捷移动十字线
CTRL+Rk线 移动十字线到当前界面结尾
CTRL+Vk线 切换复权方式为等比前复权/不复权
CTRL+Bk线 切换复权方式为等比后复权/不复权
CTRL+A分时 k线 浏览列表 样板股分析
CTRL+E分时 k线 浏览列表 预警雷达
ALT+数字 (1~9)分时 K线 设置附图指标数量
ALT+分时 K线 显示走势图
ALT+分时 K线 显示走势图
F3切换到上证指数分时图
F4切换到深证成指分时图
F5切换到分时/K线图
F6切换到“我的自选”板块浏览列表
F8K线 循环切换周期
F10个股切换财务数据界面,指数切换到成分股界面

策略编辑器快捷键

操作描述
Ctrl+C复制
Ctrl+X剪切
Ctrl+V粘贴
Ctrl+Q多行注释
Ctrl+Z撤消
Ctrl+Y恢复
Ctrl+A全选
Ctrl+F键查找对话框启动
Ctrl+D复制并粘贴当行
Ctrl+L删除当前行
Ctrl+T当行向上移动一行
Ctrl+S保存文件
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_question_answer.html b/reference/thinktrader_docs/innerApi_question_answer.html deleted file mode 100644 index 4bb8848..0000000 --- a/reference/thinktrader_docs/innerApi_question_answer.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 常见问题 | 迅投知识库 - - - - -

Python环境相关

安装第三方 Python 库报错

问题描述:

"ImportError:Forbidden:Moduleopenpyxl not in whitelist!"

问题解答:

该报错是由于券商后台开启了 Python 库白名单,若您使用的是券商提供的QMT终端,请联系您的所属券商开通对应 Python 库白名单权限即可。

启动策略时pandas库报错

报错信息1 :NameError: name 'pandas' is not defined

解答:

该报错是指当前环境下没有找到pandas库

解决方法

  1. 请在设置-模型设置中检查正确设置了路径,正确路径应指向{安装目录}\bin.x64

正确路径参考

  1. 请检查是否已经下载了python环境

下载python环境

报错信息2 :AttributeError: module 'pandas' has no attribute 'core'

解答:

该报错是由于在pandas导入中被强行中断导致的

解决方法

重启客户端

对第三方库的支持

QMT Python API 提供基于 Python 3.6 规范的标准量化投资策略应用程序接口,本文档示例代码基于 Python 3.6 规范。我司主要通过以下两种方式对外提供:

系统自带的 Python 环境

QMT 系统的安装包默认自带 Python 运行环境。用户安装完迅投客户端后,默认可以直接使用Python。在这个打包的Python环境中,迅投除了提供标准的 Python api 带的库外,还集成了如下一些第三方库:

名称说明
NumPyNumPy (Numeric Python) 提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。
PandasPython Data Analysis Library 或 Pandas 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas 提供了大量能使我们快速便捷地处理数据的函数和方法。
Patsy一个线性模型分析和构建工具库。
SciPySciPy 函数库在 NumPy 库的基础上增加了众多的数学、科学以及工程计算中常用的库函数。例如线性代数、常微分方程数值求解、信号处理、图像处理、稀疏矩阵等等。
StatsmodelsPython 的统计建模和计量经济学工具包,包括一些描述统计、统计模型估计和推断。
TA_Lib称作技术分析库,是一种广泛用在程序化交易中进行金融市场数据的技术分析的函数库。它提供了多种技术分析的函数,可以大大方便我们量化投资中编程工作,内容包括:多种指标,如 ADX, MACD, RSI, 布林轨道等;K 线形态识别,如黄昏之星,锤形线等等。

第三方库导入指引

除迅投提供的标准 Python api 和集成的部分第三方库,用户也可自己在 Python 官网下载其他所需第三方库,使用方式如下:

(1)本地安装Python环境,下载python3.6,Python官网:https://www.python.org/downloads/release/python-360/

(2)安装位置:C:\Python36

​ 新增环境变量:我的电脑--属性--高级系统设置--高级--环境变量---path:C:\Python36;C:\Python36\Scripts

img

(3)Python环境检查

​ Win+R 打开运行,输入 cmd

img

​ 检查Python变量

image-20210318150744951

(4)安装第三方库

​ 安装前先确认客户端安装目录,根据个人电脑进行调整。

​ 安装时若遇到下面错误提示,请执行 pip 更新命令 python -m pip install --upgrade pip

image-20210318151342044

​ 安装三方库命令 pip install openpyxl -t E:\QMT交易端20962\bin.x64\Lib\site-packages

image-20210318151401916

(5)检查安装结果

​ 安装位置\bin.x64\Lib\site-packages检查安装库

image-20210318151523278

业务规则相关

交易所委托数量规则

  1. 科创板,连续交易时段限价单笔最大是10万股,市价单笔最大是5万股,盘后定价交易单笔最大量是100万股,200股起,1股递增。
  2. 创业板,连续交易时段限价单笔最大30万股,市价单笔最大15万股,100股起,100股递增。
  3. 主板,6和0开头的,连续交易时段单笔最大100万股,100股起,100股递增。

策略运行相关

在策略没有勾选终端启动后自动运行的情况下,策略自动启动运行

情况一

策略被运行于行情界面的副图上,随客户端启动被启动

解决方法

在右上角的页面布局中选择恢复默认布局,并重启客户端 恢复默认布局

情况二

交易日切换/行情断线重连时,所有挂着的模型会被重新运行,这是正常的

策略回测相关

QMT在回测时如何选择复权方式

解答

回测是为了更贴近历史数据,但实际中各类配股、增发的动作,会造成价格的异常波动,为了避免这样的波动对回测的影响,我们推荐用户在回测中使用等比前复权价,这样在回测过程中,无需考虑配股、增发带来的变化,始终以统一标准的价格进行买卖,方便的同时也能得到更贴合历史数据的回测收益和表现。

交易相关

系统对象 ContextInfo 逐 k 线保存的机制

机制说明

ContextInfo是由底层维护并传递给inithandlebar等系统函数的参数,同一个 bar(不是 bar 里面的 tick,下同)内ContextInfo本质上是同一个变量且对其进行的修改只会对本次handlebar调用的下文所起作用。handlebar里对ContextInfo做的修改在该 bar 结束后才会进行保存,也就是说,对ContextInfo做的修改会在下一个 bar 体现出来。

具体来说,ContextInfo不同于一般 python 对象,做了逐 k 线更新设计,盘中主图品种每个 Level 1 分笔到达会触发handlebar函数调用,但只有 k 线结束时最后一个分笔触发的handlebar调用,对ContextInfo的修改才有效。

每次handlebar函数调用前会对ContextInfo对象进行深拷贝, 下一次分笔行情到来时,如果新的分笔不是新 k 线 bar 第一个分笔,则判断上一个分笔不是k线最后分笔,ContextInfo对象被回退为之前深拷贝的那个。

ContextInfo对象逐k线更新机制设计的目的,是为了在盘中时模拟k线的效果,只在k线结束的分笔触发的handlebar函数运行时生效一次,丢弃所有其他分笔的修改。

影响

该机制有两个影响,一是在ContextInfo对象中存数据每次分笔到达时会被深拷贝,拖慢策略运行;二是ContextInfo适用于记录逐k线生效的交易信号(quickTrade参数传0),不适宜立刻下单的情况。

如不需要模拟k线效果,希望调用交易函数后立刻下单,quickTrade参数可以传2, 下单记录可以用普通的全局变量保存, 不能存在ContextInfo对象的属性里(实现可以参考实盘示例7-调整至目标持仓Demo)。

快速交易参数 quickTrade

下单函数passorder有可选参数快速交易quickTrade, 默认为0

  • 0,只在k线结束分笔时调用passorder产生有效信号,其他情况调用不产生信号。
  • 1,在当前k线为最新k线时调用passorder函数产生有效信号, 历史k线调用不产生信号。
  • 2,任何情况下调用passorder都产生有效信号,不会丢弃任何一次调用的信号。
  • 如果在定时器注册的回调函数,行情回调函数, after_init函数中调用下单函数,需要传2,确保不会漏单。
  • passorder以外的下单函数不能指定快速交易参数,效果与传0passorder一致。

下单与回报相关

  1. 为保证以尽快的速度执行交易信号, qmt 客户端提供的交易接口是异步的, 以快速交易参数填2passorder函数为例,调用后会立刻发出委托, 然后返回。不会等待委托回报, 也不会阻塞python线程的运行。

  2. 委托/成交/持仓/账号信息的更新, 是在客户端后台进行的, python策略中无法手动控制。python提供的取账号信息接口 get_trade_detail_data, 与四种交易回调函数, 都是从客户端本地缓存中读取数据 / 触发调用,不是调用时查询柜台再返回。客户端本地缓存状态定期接收柜台推送刷新,有交易主推的柜台50ms一次,没有交易主推的柜台1-6秒一次。 不能认为get_trade_detail_data查到的状态是与柜台完全一致的, 比如卖出委托后立刻查询, 不会查到对应委托, 可用资金也不会变多。

  3. 实盘策略需要设计盘中保存/更新委托状态的机制。常见的做法是用全局变量字典保存委托状态, 给每一笔委托独立的投资备注作为字典的key,委托状态作为字典的value, 下单后默认设置为待报, 之后查到委托后更新状态。如果某品种股票存在待报状态委托, 暂停该品种后续报单, 防止发生超单的情况。(实现可以参考实盘示例7-调整至目标持仓Demo)

  4. QMT 所有策略是在同一个线程中被调用的,任意一个策略阻塞线程(死循环 sleep 加锁等操作)会导致所有策略的执行被阻塞,所以不能在策略里写等待操作。如需要多线程 / 多进程的用法,可以使用极简模式配合 xtquant 库使用

QMT 下单失败

  1. 检查是否是在模型交易界面,实盘模式运行的策略。模拟模式只显示策略信号,不发出委托。

  2. 如运行到交易函数,未看到策略信号,检查交易函数是否使用了快速下单参数(quickTrade),默认为0,只会在k线结束发出委托,日线及以上周期等于全天不会委托。传1时,非历史bar上执行时(ContextInfo.is_last_bar()为True),只要策略模型中调用到就触发下单交易。传2,无论是否是历史bar,运行到交易函数时立刻发出委托。

如果希望盘中出现信号立即下单,建议传1,这种情况下会有策略信号闪烁的风险,需要自己处理;如果希望K线结束下单(信号不闪烁),建议传0通常情况下不建议传2

提示

具体到场景:

  1. handlebar逐k线下单, 每次k线结束的分笔生效一次, 传0;
  2. 需要在handlebar盘中触发立刻下单, 传1;
  3. 定时器/init/after_init与交易回调函数, 行情回调函数内下单, 传2.
  1. 如看到实盘的策略信号,未找到对应委托,检查客户端左下角消息提示是否有报错,如有,请根据消息提示的描述修改下单参数

行情相关

QMT 行情数据基础概念

QMT行情数据主要分为三种,包括本地数据全推数据订阅数据

  1. 本地数据: 指下载到本地的行情数据加密文件。包括历史数据,适合回测模式使用,对应python接口为get_market_data_ex(subscribe=False) 在新窗口打开

  2. 全推数据: 指客户端启动后, 自动接收,更新的全市场最新数据快照, 包括日线的开高低收,成交量成交额,与五档盘口(在行情界面选择了五档行情时可用五档 具体见行情常规问题3)。支持取全市场品种, 只有最新值,没有历史值,服务器对交易所下发的数据即时转发,打包增量部分发送给下游客户端。可以用get_full_tick一次性取出当前最新值,也可以用subscribe_whole_quote注册回调函数,每次处理增量的部分。 对应python接口为get_full_tick在新窗口打开subscribe_whole_quote在新窗口打开

  3. 订阅:指向行情服务器订阅指定品种行情, 共有四种周期(分笔 1分钟 5分钟 日线),可以订阅当日数据,当天以前的需要用 down_history_data下. 订阅有最大数量限制(例如:假设最大数量限制为300个,则可以单独订阅日线300个,若同时订阅日线和五分钟 则各150个),如需订阅超过定义上限,可以在页面右上角,选购行情vip服务。对应python接口为subscribe_quote在新窗口打开get_market_data_ex(subscribe=True,)在新窗口打开其中,使用get_market_dataget_market_data_ex(subscribe=True,)时客户端会自动订阅传入的品种,不需要额外调用subscibe_quote,但这种方式订阅的品种没有订阅号,无法手动反订阅,只能通过停止策略释放可订阅数。

警告

如果超出订阅数量限制,则返回的行情数据会使用前值填充,出现重复值,非正确行情数据。

QMT 行情调用函数对比说明

  • down_history_data 下载指定区间的行情数据到本地,存放在硬盘上。效果和界面,点击行情数据下载一致。 开始时间不填时,为增量下载(以本地数据最后一天为开始时间), 填写的话按填写值下载。
  • get_local_data 取本地数据函数,盘中不会更新,速度快,回测可以用这个函数取。
  • get_full_tick 取客户端缓存中的最新全推数据。全推数据不包括历史,不用订阅,没有品种数量限制,盘中50ms更新一次,速度快。
  • subscribe_quote 向服务器订阅股票行情 盘中实时更新 初次订阅耗时长,最大订阅品种数受限. 订阅超过一定数量的品种k线行情不会更新.可订阅四种基本周期(分笔 一分钟 五分钟 日线)行情(如果有 Level-2 行情权限 也可以订 Level-2 的), 同一品种订阅了不同周期累加计数(如订阅浦发银行 1分钟 5分钟 日线行情 算订阅3次). 复数策略订阅同一品种计数不会累加. Level-2 的订阅也会受限,但是和 Level 1 的互不影响。
  • unsubscribe_quote 按订阅号反订阅行情, 释放可订阅数.
  • get_market_data_ex 取订阅/本地数据接口。用subscribe_quoteinit函数中先订阅后subscribe参数为True时,取本地数据和订阅的最新行情。subscribe参数传False时,可以用来取本地数据,不会订阅。 如股票池超过一定数量,可用 down_history_data + get_local_data + get_full_tick 拼接历史和最新数据替代get_market_data_ex

注意

gmd系列函数在init中运行时,只能读取到本地数据,不会取到最新行情数据,因子,不建议在init中调用使用gmd系列函数

警告

不再推荐使用!

set_universe, get_history_data, get_market_data 是早期订阅股票池, 取订阅的行情数据接口. 因为set_universe订阅的品种没有订阅号 无法在策略中反订阅, 只能通过停止策略释放订阅数。

全推接口和订阅接口的分笔行情没有5档行情,只有最新价

问题描述

get_full_tick, subscribe_while_quote函数中获取分笔行情没有5档行情,只有最新价。

解决办法

修改行情源对应的全推行情级别,见下图

img

或者

img

行情中心和交易中心到底有啥区别?

行情中心控制单支订阅,例如subscribe_quote

交易中心影响全推数据,例如get_full_tick,subscribe_whole_quote

passorder使用对手价下单报错/有误

问题描述

passorder参数prType填写14(对手价下单)时,委托价格有误,或信息提示对手价无效,无法下单!

解决办法

修改行情源对应的全推行情级别,见下图

img

或者

img

为什么在handlebar中获取期货tick时,tick是3S一个而非0.5s一个?

这是由于handlebar函数是逐K线驱动在新窗口打开的,在实时行情中,handlebar会随着主图标的tick的更新被调用

在这个问题场景中,主图的标的通常被设置为股票,而股票的tick通常是3s一个,这就导致handlebar函数3s才被调用一次

解决方法

  1. 使用定时器(run_time)在新窗口打开进行计算

  2. 使用订阅推送(subscribe)在新窗口打开,在回调函数中进行计算

  3. 如果需要在handlebar中进行期货策略编写,建议将主图设置为期货品种,来保证handlebar调用频率

为什么在非交易时间段handlebar也会被调用

handlebar受行情数据推送驱动,在非交易时段,行情服务会做一系列准备工作,其中可能伴随着服务重启,在重启后为了保证数据齐全,客户端会重新订阅数据,这时服务会推送最新的数据,客户端会把推送的最新数据更新至缓存,并向上层策略推送更新,也就是触发handlebar执行

这个合并数据的驱动执行只会在最新一根bar而不会在历史范围,策略可以根据需要处理这次推送或直接根据交易时间跳过这个驱动,例如判断time小于09:15则直接return

关于证券状态openint值的详细说明

沪市

时间段状态编码
9:15 - 9:25盘前集合竞价12
9:25 - 14:57盘中连续竞价13
14:57 - 15:00盘后集合竞价18
15:00收盘状态15
15:05 - 15:30盘后定价22
15:30盘后定价结束23
上一状态后收盘状态15

深市

时间段状态编码
9:15 - 9:25盘前集合竞价12
9:25 - 9:30休市14
9:30 - 11:30盘中连续竞价13
11:30 - 13:00休市14
13:00 - 14:57盘中连续竞价13
14:57 - 15:00盘后集合竞价18
15:00发收盘状态15
15:05 - 15:30盘后定价22
15:30盘后定价结束23
上一状态后收盘状态15

静态数据问题

报错:[系统]ERROR:******.**获取合约乘数和最小变动价位失败,跳过

点击右下角【行情】按钮,选择【智能下载】,数据选项下拉框勾选【过期合约列表】点击该面板右下角【开始】,待过期合约数据补充完毕后,即可正常获取过期合约数据。

软件运行日志相关

如何找到软件运行日志

Log文件通常在安装目录下的.\userdata\log文件夹中,在 .\userdata\log 文件夹中,你可能会看到一个或者多个 log 文件,通常以 '.log' 作为扩展名。这些文件将包含软件运行时的详细情况。

投研:{安装目录}\userdata\log

说明

XtClient_20210922.log - 客户端常规日志

XtClient_datasource_20210922.log - 行情数据日志

XtClient_Formula_20210922.log - 策略运行日志

XtClient_FormulaOutput.log - 策略输出日志

QMT:{安装目录}\userdata\log

说明

XtClient_20210922.log - 客户端常规日志

XtClient_Formula_20210922.log - 策略运行日志

XtClient_FormulaOutput.log - 策略输出日志

XtClient_PerformanceFile_20210922.log - 客户端流程节点日志

极简模式:{安装目录}\userdata_mini\log

说明

XtMiniQuote_20210917.log - 行情策略模块日志

XtMiniQmt_20210917.log - 客户端常规日志

XtMiniQmt_perform_20210917.log - 客户端流程节点日志

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_quote_function.html b/reference/thinktrader_docs/innerApi_quote_function.html deleted file mode 100644 index 1f40119..0000000 --- a/reference/thinktrader_docs/innerApi_quote_function.html +++ /dev/null @@ -1,197 +0,0 @@ - - - - - - - - - 引用函数 | 迅投知识库 - - - - -

ext_data - 获取扩展数据

获取扩展数据

调用方法:ext_data(extdataname, stockcode, deviation, ContextInfo)

参数:

参数名类型说明提示
extdatanamestring扩展数据名
stockcodestring证券代码形式如 '600000.SH'
deviationnumberK 线偏移0:不偏移,N:向右偏移N,-N:向左偏移N
ContextInfopythonObjPython 对象ython 对象,这里必须是 ContextInfo

返回: number

** 示例:**

#coding:gbk
-def init(ContextInfo):
-	print(ext_data('CR', '600000.SH', 0, ContextInfo))
-

ext_data_rank - 获取引用的扩展数据的数值在所有品种中的排名

获取引用的扩展数据的数值在所有品种中的排名

调用方法:ext_data_rank(extdataname, stockcode, deviation, ContextInfo)

参数:

参数名类型说明提示
extdatanamestring扩展数据名
stockcodestring证券代码形式如 '600000.SH'
deviationnumberK 线偏移0:不偏移,N:向右偏移N,-N:向左偏移N
ContextInfopythonObjPython 对象ython 对象,这里必须是 ContextInfo

返回: number

** 示例:**

#coding:gbk
-def init(ContextInfo):
-	print(ext_data_rank('mycci', '600000.SH', 0, ContextInfo))
-

ext_data_rank_range - 获取引用的扩展数据的数值在指定时间区间内所有品种中的排名

获取引用的扩展数据的数值在指定时间区间内所有品种中的排名

** 调用方法: **ext_data_rank_range(extdataname, stockcode, begintime, endtime, ContextInfo)

参数:

参数名类型说明提示
extdatanamestring扩展数据名
stockcodestring证券代码形式如 '600000.SH'
begintimestring区间的起始时间格式为 '2016-08-02 12:12:30'(包括该时间点在内)
endtimestring区间的结束时间格式为 '2017-08-02 12:12:30' (包括该时间点在内)
ContextInfopythonObjPython对象Python 对象,这里必须是 ContextInfo

返回: pythonDict

** 示例:**

#coding:gbk
-def init(ContextInfo):
-	print(ext_data_rank_range('mycci', '600000.SH','2022-08-02 12:12:30', '2023-08-02 12:12:30', ContextInfo))
-

ext_data_range - 获取扩展数据在指定时间区间内的值

获取扩展数据在指定时间区间内的值

调用方法:ext_data_range(extdataname, stockcode, begintime, endtime, ContextInfo)

参数:

参数名类型说明提示
extdatanamestring扩展数据名
stockcodestring证券代码形式如 '600000.SH'
begintimestring区间的起始时间格式为 '2016-08-02 12:12:30'(包括该时间点在内)
endtimestring区间的结束时间格式为 '2017-08-02 12:12:30' (包括该时间点在内)
ContextInfopythonObjPython对象Python 对象,这里必须是 ContextInfo

返回: pythonDict

示例:

#coding:gbk
-def init(ContextInfo):
-	print(ext_data_range('mycci', '600000.SH','2022-08-02 12:12:30', '2023-08-02 12:12:30', ContextInfo))
-

get_factor_value - 获取因子数据

获取因子数据

调用方法:get_factor_value(factorname, stockcode, deviation, ContextInfo)

参数:

参数名类型说明提示
factornamestring因子名称
stockcodestring证券代码形式如 '600000.SH'
deviationnumberK 线偏移0 不偏移,N 向右偏移 N,-N 向左偏移 N
ContextInfopythonObjPython对象Python 对象,这里必须是 ContextInfo

返回: number

示例:

#coding:gbk
-def init(ContextInfo):
-	print(get_factor_value('zzz', '600000.SH', 0, ContextInfo))
-

get_factor_rank - 获取引用的因子数据的数值在所有品种中排名

获取引用的因子数据的数值在所有品种中排名

调用方法:get_factor_rank(factorname, stockcode, deviation, ContextInfo)

参数:

参数名类型说明提示
factornamestring因子名称
stockcodestring证券代码形式如 '600000.SH'
deviationnumberK 线偏移0 不偏移,N 向右偏移 N,-N 向左偏移 N
ContextInfopythonObjPython对象Python 对象,这里必须是 ContextInfo

示例:

#coding:gbk
-def init(ContextInfo):
-	print(get_factor_rank('zzz', '600000.SH', 0, ContextInfo))
-

获取引用的 VBA 模型运行的结果

(不推荐)券商版qmt函数:call_vba
更推荐函数(投研版qmt):

获取引用的 VBA 模型运行的结果

提示

注意

  1. 使用该函数时需补充好本地 K 线或分笔数据

调用方法: call_vba(factorname, stockcode,[period, dividend_type, barpos],ContextInfo)

参数:

参数名类型说明提示
factornamestring因子名称
stockcodestring证券代码形式如 '600000.SH'
periodstringK 线偏移可缺省,默认为当前主图周期线型
dividend_typestring复权方式可缺省,默认当前图复权方式,具体可选值如下
barposnumber对应 bar 下标可缺省,默认当前主图调用到的 bar 的对应下标xtInfo
ContextInfopythonObjPython 对象Python 对象,这里必须是 ContextInfo
  • period 可选值:

    'tick':分笔线 '1d':日线 '1m':1分钟线 '3m':3分钟线 '5m':5分钟线 '15m':15分钟线 '30m':30分钟线 '1h':小时线 '1w':周线 '1mon':月线 '1q':季线 '1hy':半年线 '1y':年线

  • dividend_type 可选值:

    'none':不复权 'front':向前复权 'back':向后复权 'front_ratio':等比向前复权 'back_ratio':等比向后复权

返回: number

示例:

#coding:gbk
-def init(ContextInfo):
-	print(call_vba('MA.ma1', '600036.SH', ContextInfo))
-

@ tab 返回值

-1.0
-

(投研版QMT) 获取引用的 VBA 模型运行的结果

更推荐函数(投研版QMT):

  • 订阅实时+历史数据(直接在python中写VBA,VBA公式不用先建)[get_vba_func_result]

获取引用的 VBA 模型运行的结果

提示

注意

  1. 使用该函数时需补充好本地 K 线或分笔数据

调用方法: get_vba_func_result(func,stock_code,period='1d',start_time='',end_time='',count=-1,dividend_type=None,extend_param={},subscribe=True)

参数:

参数名类型说明提示
funcstr或者list[str]vba函数
stockcodestring合约品种形式如 '600000.SH'
periodstring周期可缺省,默认为当前主图周期线型
start_timestring开始时间形式如 %Y%m%d 或 %Y%m%d%H%M%S
end_timestring结束时间形式如 %Y%m%d 或 %Y%m%d%H%M%S
countint条数模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
dividend_typestring除权类型复权方式,默认为主图除权方式,可选范围:'none':不复权,'front':向前复权,'back':向后复权,'front_ratio':等比向前复权,'back_ratio':等比向后复权
extend_paramdict扩展参数模型的入参,{"模型名:参数名":参数值},例如在跑模型MA时,{'MA:n1':1};入参可以添加__basket:dict,组合模型的股票池权重,形如{'__basket':{'600000.SH':0.06,'000001.SZ':0.01}},如果在跑一个模型1的时候,模型1调用了模型2,如果只想修改模型2的参数可以传{'模型2:参数':参数值}
subscribebool是否订阅数据True:历史加实时, False: 历史
  • period 可选值:

    'tick':分笔线 '1d':日线 '1m':1分钟线 '3m':3分钟线 '5m':5分钟线 '15m':15分钟线 '30m':30分钟线 '1h':小时线 '1w':周线 '1mon':月线 '1q':季线 '1hy':半年线 '1y':年线

  • dividend_type 可选值:

    'none':不复权 'front':向前复权 'back':向后复权 'front_ratio':等比向前复权 'back_ratio':等比向后复权

示例:

# coding:GBK
-def init(C):    
-    fml = """
-        基准:=-1;//-1
-
-        档位:=1;
-        bb:getoptcodebyno('','C',1,档位,基准,1,0,6);
-        if bb = '' then exit;
-        //qh1:convindex('',1);//返回IFIC00
-        ss:getoptcodebyno('','P',1,-1*档位,基准,1,0,6);
-        xx:getexerciseinterval('SH510050',1),nodraw;
-
-        x:deliveryinterval(),LINETHICK0;//,noaxis;//
-        bb1:STKNAME(bb);
-        ss1:STKNAME(ss);
-        认沽今收:callstock(ss,vtclose,-1,0),noaxis();
-
-        认购今收:callstock(bb,vtclose,-1,0),noaxis();
-        """
-
-    d = get_vba_func_result(fml,'510050.SH','1d',count=10)
-    print(d)
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_related_instructions.html b/reference/thinktrader_docs/innerApi_related_instructions.html deleted file mode 100644 index f2c158e..0000000 --- a/reference/thinktrader_docs/innerApi_related_instructions.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 相关说明 | 迅投知识库 - - - - -

提示

QMT所使用的Python版本为 3.6.8 - 64位

手册说明

对第三方库的支持

QMT Python API 提供基于 Python 3.6 规范的标准量化投资策略应用程序接口,本文档示例代码基于 Python 3.6 规范。我司主要通过以下两种方式对外提供:

系统自带的 Python 环境

QMT 系统的安装包默认自带 Python 运行环境。用户安装完迅投客户端后,默认可以直接使用Python。在这个打包的Python环境中,迅投除了提供标准的 Python api 带的库外,还集成了如下一些第三方库:

名称说明
NumPyNumPy (Numeric Python) 提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。
PandasPython Data Analysis Library 或 Pandas 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas 提供了大量能使我们快速便捷地处理数据的函数和方法。
Patsy一个线性模型分析和构建工具库。
SciPySciPy 函数库在 NumPy 库的基础上增加了众多的数学、科学以及工程计算中常用的库函数。例如线性代数、常微分方程数值求解、信号处理、图像处理、稀疏矩阵等等。
StatsmodelsPython 的统计建模和计量经济学工具包,包括一些描述统计、统计模型估计和推断。
TA_Lib称作技术分析库,是一种广泛用在程序化交易中进行金融市场数据的技术分析的函数库。它提供了多种技术分析的函数,可以大大方便我们量化投资中编程工作,内容包括:多种指标,如 ADX, MACD, RSI, 布林轨道等;K 线形态识别,如黄昏之星,锤形线等等。

第三方库导入指引

除迅投提供的标准 Python api 和集成的部分第三方库,用户也可自己在 Python 官网下载其他所需第三方库,使用方式如下:

(1)本地安装Python环境,下载python3.6,Python官网:https://www.python.org/downloads/release/python-360/

(2)安装位置:C:\Python36

​ 新增环境变量:我的电脑--属性--高级系统设置--高级--环境变量---path:C:\Python36;C:\Python36\Scripts

img

(3)Python环境检查

​ Win+R 打开运行,输入 cmd

img

​ 检查Python变量

image-20210318150744951

(4)安装第三方库

​ 安装前先确认客户端安装目录,根据个人电脑进行调整。

​ 安装时若遇到下面错误提示,请执行 pip 更新命令 python -m pip install --upgrade pip

image-20210318151342044

​ 安装三方库命令 pip install openpyxl -t E:\QMT交易端20962\bin.x64\Lib\site-packages

image-20210318151401916

(5)检查安装结果

​ 安装位置\bin.x64\Lib\site-packages检查安装库

image-20210318151523278

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_start_now.html b/reference/thinktrader_docs/innerApi_start_now.html deleted file mode 100644 index e183f36..0000000 --- a/reference/thinktrader_docs/innerApi_start_now.html +++ /dev/null @@ -1,338 +0,0 @@ - - - - - - - - - 快速开始 | 迅投知识库 - - - - -

一、概述

QMT 极速策略交易系统,以下简称 QMT 系统,内置了 3.6 版本python 运行环境,提供行情数据交易下单两大核心功能。通过编写 python 脚本,可以完成指标计算,策略编写,策略回测,实盘下单等需求。

二、场景需求

QMT 系统支持回测模型实盘模型

回测模型: 指在历史 k 线上,自左向右逐根遍历 k 线,以模拟的资金账号记录每日的买卖信号,持仓盈亏,最终展示策略在历史上的净值走势结果。

实盘模型: 指在盘中收取最新的动态行情,即时发送买卖信号到交易所,判断委托状态,需要实时重复报撤的模型。


两类模型分别有各自的注意点:

回测模型

  1. 回测是遍历固定的历史数据:

    • 首先需要下载历史行情,首次下载可以在界面左上角,点击操作,选择数据管理补充行情,选择回测的周期,如日线,所需的板块数据,如沪深A股板块,时间范围选择全部,下载完整历史行情

    • 其次设置每日定时更新,可以点击客户端右下角行情按钮,在批量下载界面选择需要每天更新的数据,勾选定时下载选项,之后每天在指定时间会自动下载行情数据到本地
  2. 回测模型取本地数据遍历,不需要向服务器订阅实时行情,应使用 get_market_data_ex函数,指定subscribe参数为False,来读取本地行情数据。

  3. 回测模型的撮合规则为,指定交易价格在当前k线高低点间的,按指定价格撮合,超过高低点的,按当前 k 线收盘价撮合。委托数量大于可用数量时,按可用数量撮合。

  1. 回测模型右侧的基本信息,如默认周期,默认主图,在我的界面点击回测时会生效。在行情界面k线下点击回测,以当前 k 线的周期,品种为准。回测必须以副图模式执行,不要选择主图 /主图叠加.

实盘模型

当你回测结束,你需要开始实盘模型,注意这里提到的实盘,指的是接收未来 K 线的数据,生成策略信号,进行交易下单。

提示

实盘模型也分模拟柜台模拟交易真实柜台实盘交易两种。具体请参考如何配置账号在新窗口打开

  1. 你要运行实盘模型,QMT 系统提供两种交易模式:

    • 默认的交易模式为逐 k 线生效 (passorder函数快速交易quicktrade参数填 0 即默认值),适用与需要在盘中模拟历史上逐 k 线的效果需求。例如选择一分钟周期,将下单判断,下单函数放在handlebar函数内,盘中主图每个分笔 (三秒一次)会触发一次handlebar函数调用,系统会暂存当前handlebar产生的下单信号。三秒后下一个分笔到达时,如果是新的一分钟 k 线的第一个分笔,判断上一个分笔为前一根k线最后分笔,会将暂存的交易信号发送给交易所,完成交易。如到达的下一个分笔不是新一根 k 线的,则判定当前 k 线未完成,丢弃暂存的交易信号。1 分钟 k 线情形,每根k线内会有 20 个分笔,前 19 个分笔产生的信号会被丢弃,最后一个分笔的信号,会在下一根k线,首个分笔到达时,延迟三秒发出。系统自带的ContxtInfo也做了同样的等待,回退处理,逐 k 线模式的交易记录可以保存在ContextInfo对象的属性中。详细说明参见 常见问题:系统对象ContextInfo 逐K线保存的机制

    • QMT 系统也支持立即下单的交易模式,passorder函数的快速交易quicktrade参数填 2,可以在运行后立刻发出委托,不对信号进行等待,丢弃的操作。此时需要用普通的全局变量(如自定义一个Class a())保存委托状态,不能存在ContextInfo的属性里。参见使用快速交易参数委托调整至目标持仓Demo

  2. 实盘的撮合规则以交易所为准。股票品种的话,价格不能超过 2% 的价格笼子否则废单。数量超过可用数量时会废单。

  3. 实盘模型需要在模型交易界面执行。模型交易界面,选择新建策略交易,添加需要的模型。运行模式可以选择模拟实盘

    • 选择模拟信号模式,在策略信号界面显示买卖信号,不实际发出委托。具体请参考模拟信号模式
    • 选择实盘交易模式,显示的策略信号会实际发出到交易所。具体请参考实盘交易模式

提示

运行模式的模拟实盘,与您使用的账号实际是实盘账号(真实交易所柜台)或是模拟账号(模拟交易柜台)无关。相关账号申请需要联系您做所在券商的工作人员,或者购买投研端账号在新窗口打开获取模拟柜台撮合服务。

三、运行机制对比

QMT 系统提供两大类(事件驱动与定时任务),共三种运行机制。

逐 K 线驱动:handlebar

handlebar主图历史 k 线+盘中订阅推送。运行开始时,所选周期历史 k 线从左向右每根触发一次handlebar函数调用。盘中时,主图品种每个新分笔数据到达,触发一次handlebar函数调用。

提示

盘中分笔驱动,但是逐 K 线生效。请参考常见问题:系统对象ContextInfo 逐K线保存的机制

事件驱动 :subscribe 订阅推送

盘中订阅指定品种的分笔数据,新分笔到达时,触发指定的回调函数。

定时任务 :run_time 定时运行

指定固定的时间间隔,持续触发指定的回调函数.

不同机制匹配不同场景需求

机制分类特点匹配需求
逐 K 线运行(handlebar事件驱动同时支持历史回测和盘中可模拟逐K线效果在实盘中模拟逐K线运行的效果
订阅推送(subscribe事件驱动盘中行情分笔触发函数调用盘中随分笔行情判断交易
定时运行(run_time定时任务固定间隔触发调用盘中固定时间间隔判断交易

四、逐 K 线驱动(handlebar)示例

因此,结合不同场景需求(回测或实盘),针对不同的机制(定时任务或事件驱动),我们分别给出回测与实盘的完整示例,复制到策略编辑器中即可使用。

在编写策略前,有以下注意事项:

警告

在编写一个策略时,首先需要在代码的最前一行写上: #coding:gbk 统一脚本的编码格式是GBK

缩进需要统一 全部统一为····或者->

回测示例-基于 handlebar

回测的操作流程请参考:界面操作-策略回测

复制代码以下代码到策略编辑器:

#coding:gbk
-
-#导入常用库
-import pandas as pd
-import numpy as np
-import talib
-#示例说明:本策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出 点击回测运行 主图选择要交易的股票品种
-
-def init(C):
-	#init handlebar函数的入参是ContextInfo对象 可以缩写为C
-	#设置测试标的为主图品种
-	C.stock= C.stockcode + '.' +C.market
-	#line1和line2分别为两条均线期数
-	C.line1=10   #快线参数
-	C.line2=20   #慢线参数
-	#accountid为测试的ID 回测模式资金账号可以填任意字符串
-	C.accountid = "testS"  
-
-def handlebar(C):
-	#当前k线日期
-	bar_date = timetag_to_datetime(C.get_bar_timetag(C.barpos), '%Y%m%d%H%M%S')
-	#回测不需要订阅最新行情使用本地数据速度更快 指定subscribe参数为否. 如果回测多个品种 需要先下载对应周期历史数据 
-	local_data = C.get_market_data_ex(['close'], [C.stock], end_time = bar_date, period = C.period, count = max(C.line1, C.line2), subscribe = False)
-	close_list = list(local_data[C.stock].iloc[:, 0])
-	#将获取的历史数据转换为DataFrame格式方便计算
-	#如果目前未持仓,同时快线穿过慢线,则买入8成仓位
-	if len(close_list) <1:
-		print(bar_date, '行情不足 跳过')
-	line1_mean = round(np.mean(close_list[-C.line1:]), 2)
-	line2_mean = round(np.mean(close_list[-C.line2:]), 2)
-	print(f"{bar_date} 短均线{line1_mean} 长均线{line2_mean}")
-	account = get_trade_detail_data('test', 'stock', 'account')
-	account = account[0]
-	available_cash = int(account.m_dAvailable)
-	holdings = get_trade_detail_data('test', 'stock', 'position')
-	holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nVolume for i in holdings}
-	holding_vol = holdings[C.stock] if C.stock in holdings else 0
-	if holding_vol == 0 and line1_mean > line2_mean:
-		vol = int(available_cash / close_list[-1] / 100) * 100
-		#下单开仓
-		passorder(23, 1101, C.accountid, C.stock, 5, -1, vol, C)
-		print(f"{bar_date} 开仓")
-		C.draw_text(1, 1, '')
-	#如果目前持仓中,同时快线下穿慢线,则全部平仓
-	elif holding_vol > 0 and line1_mean < line2_mean:
-		#状态变更为未持仓
-		C.holding=False
-		#下单平仓
-		passorder(24, 1101, C.accountid, C.stock, 5, -1, holding_vol, C)
-		print(f"{bar_date} 平仓")
-		C.draw_text(1, 1, '')
-

基础信息设置 请参考基础信息-字段描述

回测参数设置 请参考回测参数-字段描述

实盘示例-基于 handlebar

实盘的操作流程请参考:界面操作-模型交易

复制代码以下代码到策略编辑器:

#coding:gbk
-
-# 导入包
-import pandas as pd
-import numpy as np
-import datetime
-
-"""
-示例说明:双均线实盘策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出
-"""
-
-class a():
-	pass
-A = a() #创建空的类的实例 用来保存委托状态 
-
-
-def init(C):
-	A.stock= C.stockcode + '.' + C.market #品种为模型交易界面选择品种
-	A.acct= account #账号为模型交易界面选择账号
-	A.acct_type= accountType #账号类型为模型交易界面选择账号
-	A.amount = 10000 #单笔买入金额 触发买入信号后买入指定金额
-	A.line1=17   #快线周期
-	A.line2=27   #慢线周期
-	A.waiting_list = [] #未查到委托列表 存在未查到委托情况暂停后续报单 防止超单
-	A.buy_code = 23 if A.acct_type == 'STOCK' else 33 #买卖代码 区分股票 与 两融账号
-	A.sell_code = 24 if A.acct_type == 'STOCK' else 34
-	print(f'双均线实盘示例{A.stock} {A.acct} {A.acct_type} 单笔买入金额{A.amount}')
-
-def handlebar(C):
-	#跳过历史k线
-	if not C.is_last_bar():
-		return
-	now = datetime.datetime.now()
-	now_time = now.strftime('%H%M%S')
-	# 跳过非交易时间
-	if now_time < '093000' or now_time > "150000":
-		return
-	account = get_trade_detail_data(A.acct, A.acct_type, 'account')
-	if len(account)==0:
-		print(f'账号{A.acct} 未登录 请检查')
-		return
-	account = account[0]
-	available_cash = int(account.m_dAvailable)
-	#如果有未查到成交 查询成交
-	if A.waiting_list:
-		found_list = []
-		deals = get_trade_detail_data(A.acct, A.acct_type, 'deal')
-		for deal in deals:
-			if deal.m_strRemark in A.waiting_list:
-				found_list.append(deal.m_strRemark)
-		A.waiting_list = [i for i in A.waiting_list if i not in found_list]
-	if A.waiting_list:
-		print(f"当前有未查到委托 {A.waiting_list} 暂停后续报单")
-		return
-	holdings = get_trade_detail_data(A.acct, A.acct_type, 'position')
-	holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings}
-	#获取行情数据
-	data = C.get_market_data_ex(["close"],[A.stock],period = '1d',count = max(A.line1, A.line2)+1)
-	close_list = data[A.stock].values
-	if len(close_list) < max(A.line1, A.line2)+1:
-		print('行情长度不足(新上市或最近有停牌) 跳过运行')
-		return
-	pre_line1 = np.mean(close_list[-A.line1-1: -1])
-	pre_line2 = np.mean(close_list[-A.line2-1: -1])
-	current_line1 = np.mean(close_list[-A.line1:])
-	current_line2 = np.mean(close_list[-A.line2:])
-	#如果快线穿过慢线,则买入委托 当前无持仓 买入
-	vol = int(A.amount / close_list[-1] / 100) * 100 #买入数量 向下取整到100的整数倍
-	if A.amount < available_cash and vol >= 100 and A.stock not in holdings and pre_line1 < pre_line2 and current_line1 > current_line2:
-		#下单开仓 ,参数说明可搜索PY交易函数 passorder
-		msg = f"双均线实盘 {A.stock} 上穿均线 买入 {vol}股"
-		passorder(A.buy_code, 1101, A.acct, A.stock, 14, -1, vol, '双均线实盘', 2 , msg, C)
-		print(msg)
-		A.waiting_list.append(msg)
-	#如果快线下穿慢线,则卖出委托
-	if A.stock in holdings and holdings[A.stock] > 0 and pre_line1 > pre_line2 and current_line1 < current_line2:
-		msg = f"双均线实盘 {A.stock} 下穿均线 卖出 {holdings[A.stock]}股"
-		passorder(A.sell_code, 1101, A.acct, A.stock, 14, -1, holdings[A.stock], '双均线实盘', 2 , msg, C)
-		print(msg)
-		A.waiting_list.append(msg)
-

警告

对于立刻下单的模型需要用普通的全局变量来保存状态不能ContextInfo对象存详细说明参考常见问题:系统对象ContextInfo 逐K线保存的机制

更多示例请参见完整示例

五、事件驱动(subscribe)示例

实盘示例-基于 subscribe

#coding:gbk
-
-class a():pass
-A = a()
-A.bought_list = []
-
-account = 'testaccount'
-def init(C):
-	#下单函数的参数需要 ContextInfo对象 在init中定义行情回调函数 可以用到init函数的入参 不用手动传入 
-	def callback_func(data):
-		#print(data)
-		for stock in data:
-			current_price = data[stock]['close']
-			pre_price = data[stock]['preClose']
-			ratio = current_price / pre_price - 1
-			print(stock, C.get_stock_name(stock), '当前涨幅', ratio)
-			if ratio > 0 and stock not in A.bought_list:
-				msg = f"当前涨幅 {ratio} 大于0 买入100股"
-				print(msg)
-				#下单函数passorder 安全起见处于注释状态 需要实际测试下单交易时再放开
-				#passorder(23, 1101, account, stock, 5, -1, 100, '订阅下单示例', 2, msg, C)
-				A.bought_list.append(stock)
-	stock_list = ['600000.SH', '000001.SZ']
-	for stock in stock_list:
-		C.subscribe_quote(stock, period = '1d', callback = callback_func)
-

六、定时任务(run_time)示例

实盘示例-基于 run_time

#coding:gbk
-import time, datetime
-
-class a():
-	pass
-A = a()
-
-def init(C):
-	A.hsa = C.get_stock_list_in_sector('沪深A股')
-	A.vol_dict = {}
-	for stock in A.hsa:
-		A.vol_dict[stock] = C.get_last_volume(stock)
-	A.bought_list = []
-	C.run_time("f", "1nSecond", "2019-10-14 13:20:00")
-
-def f(C):
-	t0 = time.time()
-	now = datetime.datetime.now()
-	full_tick = C.get_full_tick(A.hsa)
-	total_market_value = 0
-	total_ratio = 0
-	count = 0
-	for stock in A.hsa:
-		ratio = full_tick[stock]['lastPrice'] / full_tick[stock]['lastClose'] - 1
-		if ratio > 0.09 and stock not in A.bought_list:
-			msg = f"{now} {stock} {C.get_stock_name(stock)} 当前涨幅 {ratio} 大于5% 买入100股"
-			#下单示例 安全起见处于注释状态 需要实际测试下单时可以放开 
-			#passorder(23, 1101, account, stock, 5, -1, 100, '示例策略', 2, msg, C)
-			A.bought_list.append(stock)
-		market_value = full_tick[stock]['lastPrice'] * A.vol_dict[stock]
-		total_ratio += ratio * market_value
-		total_market_value += market_value
-		count += 1
-	total_ratio /= total_market_value
-	total_ratio *= 100
-	print(f'{now} 当前A股加权涨幅 {round(total_ratio, 2)}% 函数运行耗时{round(time.time()- t0, 5)}秒')
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_system_function.html b/reference/thinktrader_docs/innerApi_system_function.html deleted file mode 100644 index cdb1c09..0000000 --- a/reference/thinktrader_docs/innerApi_system_function.html +++ /dev/null @@ -1,250 +0,0 @@ - - - - - - - - - 系统函数 | 迅投知识库 - - - - -

ContextInfo 对象

ContextInfo 是策略运行环境对象,是 init, after_init, handlebar 等基本方法的入参,里面包括了终端自带的属性和方法。一般情况下不建议对ContextInfo添加自定义属性,ContextInfo会随着bar的切换而重置到上一根bar的结束状态,建议用自建的全局变量来存储。详细说明请看这里在新窗口打开

init - 初始化函数

初始化函数,只在整个策略开始时调用运行到一次。用于初始订阅行情,订阅账号信息使用。init函数执行完成前部分接口无法使用,如交易日获取函数get_trading_dates。

系统函数 不可被手动调用

参数:

名称类型描述
ContextInfoobject策略运行环境对象,可以用于存储自定义的全局变量

返回:

示例:

def init(ContextInfo):
-    ContextInfo.initProfit = 0
-

在init函数中订阅行情示例:

#coding:gbk
-
-def init(C):
-	#init函数入参为ContextInfo对象 定义时可以选择更简短的形参名 如C
-	#在init函数中 可以进行 订阅行情的操作
-    #如需在行情回调函数中下单 下单函数需要传入ContextInfo对象 可以通过在init中定义回调函数 来使用外层的ContextInfo
-	def my_callback_function(data):
-		#自定义行情回调函数 入参为指数据字典
-		print(data)
-	stock = '600000.SH'
-	C.subscribe_quote(stock, period = '5m', callback = my_callback_function)
-	#init函数执行完成后 
-	print('init函数执行完成')
-

after_init - 初始化后函数

后初始化函数,在初始化函数执行完成后被调用一次。可以用于放置一次性触发的下单,取数据操作代码。

系统会在init函数执行完后和执行handlebar之前调用after_init, 有些init里不支持的函数比如ContextInfo.get_trading_dates可以在after_init里调用。

系统函数 不可被手动调用

参数:

名称类型描述
ContextInfoobject策略运行环境对象,可以用于存储自定义的全局变量

返回:

示例:

#coding:gbk
-def init(ContextInfo):
-    print('init')  
-
-
-def after_init(ContextInfo):
-    print('系统会在init函数执行完后和执行handlebar之前调用after_init')
-
-
-def handlebar(ContextInfo):
-    if ContextInfo.is_last_bar():
-        print('handlebar')
-
-

after_init函数中立刻下单示例:

#coding:gbk
-
-def after_init(C):
-	#after_init 函数 可以用于执行运行开始时 需要执行一次的代码 例如下一笔委托
-	#account变量是模型交易界面 添加策略时选择的资金账号 不需要手动填写 交易模型需要在模型交易界面运行 才有效
-	#快速交易参数(quickTrade )填2 passorder函数执行后立刻下单 不会等待k线走完再委托。 可以在after_init函数 run_time函数注册的回调函数里进行委托 
-	msg = f"投资备注字符串 用来区分不同委托"
-	passorder(23, 1101, account, '600000.SH', 5, -1, 100, '测试下单', 2, msg, C)
-

handlebar - 行情事件函数

系统函数 不可被手动调用

释义: 行情事件函数,每根 K 线运行一次;实时行情获取状态下,先每根历史 K 线运行一次,再在每个 tick 数据来后驱动运行一次

历史k线上,按时间顺序每根K线触发一次调用;盘中,每个新到达的TICK数据驱动运行一次。可以作为行情驱动的函数,实现指标计算,回测,实盘下单的效果。

参数:

名称类型描述
ContextInfoobject策略运行环境对象,可以用于存储自定义的全局变量

返回:

示例:

def handlebar(ContextInfo):
-    # 输出当前运行到的 K 线的位置
-    print(ContextInfo.barpos)
-

ContextInfo.schedule_run - 设置定时器

说明

  1. 该函数是新版设置定时器函数,相比旧版run_time,新版schedule_run新增了任务分组,任务取消等多种功能

原型:

ContextInfo.schedule_run(
-    func:Callable, # 回调函数,到达定时器预定时间时触发调用,参数为ContextInfo类型,无需返回值
-    time_point:Union[dt.datetime,str], # 表示预定的第一次触发时间,如果设置定时器时已经过了预定时间,会立即执行func以及后续逻辑;当使用str类型时,格式为'yyyymmddHHMMSS'如'20231231235959',需要满足转换dt.datetime.strptime('20231231235959','%Y%m%d%H%M%S')
-    repeat_times:int=0, # 表示在预定时间触发后按interval间隔再触发多少次
-    interval:datetime.timedelta=None, # 表示预定时间触发后的后续重复执行的时间间隔
-    name:str='' # 定时器任务组名,可用于定时器分组,多次设置同名定时任务不会互相覆盖,会计入同一个任务组,按任务组名取消时会全部取消
-    )
-

参数:

名称类型描述
funcCallable回调函数,到达定时器预定时间时触发调用,参数为ContextInfo类型,无需返回值,定义示例如下:
def on_timer(C:ContextInfo): pass
time_pointUnion[datetime.datetime,str]表示预定的第一次触发时间,如果设置定时器时已经过了预定时间,会立即执行func以及后续逻辑;
当使用str类型时,格式为'yyyymmddHHMMSS'如'20231231235959',需要满足转换datetime.datetime.strptime('20231231235959','%Y%m%d%H%M%S')
repeat_timesint表示在预定时间触发后按interval间隔再触发多少次,传-1表示不限制次数
intervaldatetime.timedelta表示预定时间触发后的后续重复执行的时间间隔
namestr定时器任务组名,可用于定时器分组,多次设置同名定时任务不会互相覆盖,会计入同一个任务组,按任务组名取消时会全部取消

回调函数参数: ContextInfo:策略模型全局对象

返回值:

int类型,表示本次调用后生成的定时任务号,可用于取消本次定时任务,全局唯一不重复

示例:

import datetime as dt
-def on_timer(C:ContextInfo):
-    print('hello world')
-def init(ContextInfo):
-    tid=ContextInfo.schedule_run(on_timer,'20231231235959',-1,dt.timedelta(minutes=1),'my_timer')
-def handlebar(ContextInfo):
-    pass
-#此例为自2023-12-31 23:59:59后每60s运行一次on_timer
-

ContextInfo.cancel_schedule_run - 取消由schedule_run产生的定时任务

原型:

ContextInfo.cancel_schedule_run(
-    key:Union[seq:int,name:str] # 定时任务号或定时任务组名称
-    )
-

参数:

名称类型描述
key:Union[seq:int,name:str]类型为int时,表示按任务号取消;类型为str时,表示按任务组取消,会取消组内所有定时任务

返回值:

bool类型,表示是否取消成功,即是否能按key找到目标定时任务

示例:


-ContextInfo.cancel_schedule_run('my_timer') #取消my_timer任务组所有定时任务
-ContextInfo.cancel_schedule_run(1) #取消任务号为1的定时任务
-
-

ContextInfo.run_time - 设置定时器

设置定时器函数,可以指定时间间隔,定时触发用户定义的回调函数。适用与在盘中,持续判断交易信号的模型。

用法: ContextInfo.run_time(funcName,period,startTime) 定时触发指定的 funcName函数, funcName函数由用户定义, 入参为ContextInfo对象。

参数:

  • funcName:回调函数名
  • period:重复调用的时间间隔,'5nSecond'表示每5秒运行1次回调函数,'5nDay'表示每5天运行一次回调函数,'500nMilliSecond'表示每500毫秒运行1次回调函数
  • startTime:表示定时器第一次启动的时间,如果要定时器立刻启动,可以设置历史的时间

回调函数参数: ContextInfo:策略模型全局对象

示例:

import time
-def init(ContextInfo):
-    ContextInfo.run_time("f","5nSecond","2019-10-14 13:20:00")
-def f(ContextInfo):
-    print('hello world')
-
-#此例为自2019-10-14 13:20:00后每5s运行一次函数f
-

注意

  1. 模型回测时无效
  2. 定时器没有结束方法,会随着策略的结束而结束。
  3. period有nMilliSecond、nSecond和Day三个周期单元,部分周期下定时器函数在第一次运行之前会先等待一个period

stop - 停止处理函数

系统函数 不可被手动调用

释义: PY策略模型关闭停止前运行到的函数,复杂策略模型,如中间有起线程可通过在该函数内实现停止线程操作。注意, 当前版本stop函数被调用时交易连接已断开, 不能在stop函数中做报单 / 撤单操作.

参数:

名称类型描述
ContextInfoobject策略运行环境对象,可以用于存储自定义的全局变量

示例:

def stop(ContextInfo):
-    print( 'strategy is stop !')
-

ContextInfo.is_last_bar - 是否为最后一根K线

用法: ContextInfo.is_last_bar()

释义: 判定是否为最后一根 K 线

参数:

返回: bool,返回值含义:True 是右侧最新k线 False不是最新k线

True:是

False:否

示例:

def handlebar(ContextInfo):
-    print(ContextInfo.is_last_bar())
-

ContextInfo.is_new_bar - 判定是否为新的 K 线

用法: ContextInfo.is_new_bar()

释义: 某根 K 线的第一个 tick 数据到来时,判定该 K 线为新的 K 线,其后的tick不会认为是新的 K 线

参数:

返回: bool,返回值含义:

True:是

False:否

示例:

def handlebar(ContextInfo):
-    print(ContextInfo.is_new_bar()) #历史k线每根都是新k线 盘中 每根新k线第一个分笔返回True 其他分笔返回False
-

ContextInfo.get_stock_name - 根据代码获取名称

注意

我们计划后续版本抛弃这个函数,不建议继续使用,可以用ContextInfo.get_instrument_detail("stockcode")["InstrumentName"]来实现同样功能

用法: ContextInfo.get_stock_name('stockcode')

释义: 根据代码获取名称

参数: stockcode:股票代码,如'000001.SZ',缺省值 ' ' 默认为当前图代码

返回: string(GBK编码)

示例:

def handlebar(ContextInfo):
-    print(ContextInfo.get_stock_name('000001.SZ'))
-

ContextInfo.get_open_date - 根据代码返回对应股票的上市时间

用法: ContextInfo.get_open_date('stockcode')

释义: 根据代码返回对应股票的上市时间

参数: stockcode:股票代码,如'000001.SZ',缺省值 ' ' 默认为当前图代码

返回: number

示例:

def init(ContextInfo):
-    print(ContextInfo.get_open_date('000001.SZ'))
-

ContextInfo.set_output_index_property - 设定指标绘制的属性

用法: ContextInfo.set_output_index_property(index_name,draw_style=0,color='white',noaxis=False,nodraw=False,noshow=False)

释义: 设定指标绘制的属性,会最终覆盖掉指标对应的属性字段

参数:

  • index_name:string,指标名称,不可缺省
  • draw_style,同paint函数的drawstyle,可缺省默认为0
  • color,同paint函数的color,可缺省默认为'white'
  • noaxis:bool,是否无坐标,可缺省默认为False
  • nodraw:bool,是否不画线,可缺省默认为False
  • noshow:bool,是否不展示,可缺省默认为False

返回:

示例:

def init(ContextInfo):
-    ContextInfo.set_output_index_property('单位净值', nodraw = True)#使回测指标'单位净值'不画线
-

create_sector - 创建板块

用法: create_sector(parent_node,sector_name,overwrite)

释义: 创建板块

参数:

  • parent_node:str,父节点,''为'我的'(默认目录)
  • sector_name:str,要创建的板块名
  • overwrite:bool,是否覆盖。如果目标节点已存在,为True时跳过,为False时在sector_name后增加数字编号,编号为从1开始自增的第一个不重复的值。

返回: sector_name2:实际创建的板块名

示例:

create_sector_folder - 创建板块目录节点

用法: create_sector_folder(parent_node,folder_name,overwrite)

释义: 创建板块目录节点

参数:

  • parent_node:str,父节点,''为'我的'(默认目录)
  • sector_name:str,要创建的节点名
  • overwrite:bool,是否覆盖。如果目标节点已存在,为True时跳过,为False时在folder_name后增加数字编号,编号为从1开始自增的第一个不重复的值。

返回: sector_name2:实际创建的节点名

示例:

folder=create_sector_folder('我的','新建分类',False)
-

get_sector_list - 获取板块目录信息

用法: get_sector_list(node)

释义: 获取板块目录信息

参数:

  • node:str,板块节点名,''为顶层目录

返回: info_list:[[s1,s2,...],[f1,f2,...]]s为板块名,f为目录节点名,例如[['我的自选'],['新建分类1']]

示例:

get_sector_list('我的')
-

reset_sector_stock_list - 设置板块成分股

用法: reset_sector_stock_list(sector,stock_list)

释义: 设置板块成分股

参数:

  • sector:板块名
  • stock_list:list,品种代码列表,例如['000001.SZ','600000.SH']

返回: result:bool,操作成功为True,失败为False

示例:

reset_sector_stock_list('我的自选',['000001.SZ','600000.SH'])
-

remove_stock_from_sector - 移除板块成分股

用法: remove_stock_from_sector(sector,stock_code)

释义: 移除板块成分股

参数:

  • sector:板块名
  • stock_code:品种代码,例如'000001.SZ'

返回: result:bool,操作成功为True,失败为False

示例:

remove_stock_from_sector('我的自选','000001.SZ')
-

add_stock_to_sector - 添加板块成分股

用法: add_stock_to_sector(sector,stock_code)

释义: 添加板块成分股

参数:

  • sector:板块名
  • stock_code:品种代码,例如'000001.SZ'

返回: result:bool,操作成功为True,失败为False

示例:

add_stock_to_sector('我的自选','000001.SZ')
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_trading_function.html b/reference/thinktrader_docs/innerApi_trading_function.html deleted file mode 100644 index 463827b..0000000 --- a/reference/thinktrader_docs/innerApi_trading_function.html +++ /dev/null @@ -1,876 +0,0 @@ - - - - - - - - - 交易函数 | 迅投知识库 - - - - -

交易下单函数

passorder - 综合下单函数

综合下单函数,用于股票、期货、期权等下单和新股、新债申购、融资融券等交易操作推荐使用

提示

  1. 推荐使用
  2. 可覆盖多品种下单
  3. 注意参数的变化

调用方法:

passorder(
-    opType, orderType, accountid
-    , orderCode, prType, price, volume
-    , strategyName, quickTrade, userOrderId
-    , ContextInfo
-)
-'''
-passorder(
-    2 #opType 操作号
-    , 1101 #orderType 组合方式
-    , '1000044' #accountid 资金账号
-    , 'cu2403.SF' #orderCode 品种代码
-    , 14 #prType 报价类型
-    , 0.0 #price 价格
-    , 2 #volume 下单量
-    , '示例下单' #strategyName 策略名称
-    , 1 #quickTrade 快速下单标记
-    , '投资备注' #userOrderId 投资备注
-    , C #ContextInfo 策略上下文
-)
-'''
-

参数:

参数名类型说明提示
opTypeint交易类型可选买、买,期货开仓、平仓等

可选值参考opType-操作类型在新窗口打开
orderTypeint

下单方式
可选值参考orderType-下单方式在新窗口打开

可选按股票数量买卖或按照金额等方式买卖

一、期货不支持 1102 和 1202;

二、对所有账号组的操作相当于对账号组里的每个账号做一样的操作,如 passorder (23, 1202, 'testS', '000001. SZ', 5, -1, 50000, ContextInfo),意思就是对账号组 testS 里的所有账号都以最新价开仓买入 50000 元市值的 000001.SZ 平安银行;passorder (60,1101,"test",'510050. SH', 5,-1,1, ContextInfo)意思就是账号test申购 1 个单位 (900000股)的华夏上证50ETF (只申购不买入成分股)。

accountIDstring资金账号下单的账号ID(可多个)或账号组名或套利组名(一个篮子一个套利账号,如 accountID = '股票账户名, 期货账号')
orderCodestring下单代码1. 如果是单股或单期货、港股,则该参数填合约代码;
2. 如果是组合交易, 则该参数填篮子名称,参考组合交易在新窗口打开
3. 如果是组合套利,则填一个篮子名和一个期货合约名(如orderCode = '篮子名, 期货合约名'),请参考组合套利交易在新窗口打开

prTypeint下单选价类型可选值参考prType-下单选价类型在新窗口打开

特别的对于套利,这个 prType 只对篮子起作用,期货的采用默认的方式)
pricefloat下单价格一、单股下单时,prType 是模型价/科创板盘后定价时 price 有效;其它情况无效;

1.1 即单股时, prType 参数为 1149 时被使用。

1.2 prType 参数不为 1149 时也需填写,填写的内容可为 -102100 等任意数字;

二、组合下单时,是组合套利时,price 作套利比例有效,其它情况无效。
volumeint下单数量(股 / 手 / 元 / %)根据 orderType 值最后一位确定 volume 的单位,可选值参考volume - 下单在新窗口打开
strategyNamestring自定义策略名

一、用来区分 order 委托和deal 成交来自不同的策略。

根据该策略名,get_trade_detail_dataget_last_order_id 函数可以获取相应策略名对应的委托或成交集合。

strategyName 只对同账号本地客户端有效,即 strategyName 只对当前客户端下的单进行策略区分,且该策略区分只能当前客户端使用。

quickTradeint设定是否立即触发下单

可选值参考quicktrade - 快速下单在新窗口打开

passorder是对最后一根K线完全走完后生成的模型信号在下一根K线的第一个tick数据来时触发下单交易;

采用quickTrade参数设置为1时,非历史bar上执行时(ContextInfo.is_last_bar()True),只要策略模型中调用到就触发下单交易。

quickTrade参数设置为2时,不判断bar状态,只要策略模型中调用到就触发下单交易,历史bar上也能触发下单,请谨慎使用。
userOrderIdstring用户自设委托 ID如果传入该参数,
strategyNamequickTrade 参数也填写。
对应 order 委托对象和 deal 成交对象中的 m_strRemark 属性,通过 get_trade_detail_data 函数或委托主推函数 order_callback 和成交主推函数 deal_callback 可拿到这两个对象信息。
ContextInfoclass系统参数含有k线信息和接口的上下文对象

返回:

更多示例:

  1. 股票在新窗口打开
  2. 基金在新窗口打开
  3. 两融在新窗口打开
  4. 期货在新窗口打开
  5. 期权在新窗口打开
  6. 新股申购在新窗口打开
  7. 债券在新窗口打开
  8. ETF在新窗口打开
  9. 组合交易在新窗口打开
  10. 组合套利交易在新窗口打开

algo_passorder - 算法下单(拆单)函数

用于按固定时间间隔和固定规则把目标交易数量拆分成多次下单的交易函数

调用用法:

algo_passorder(opType,orderType,accountid,orderCode,prType,price,volume,[strategyName,quickTrade,userOrderId,userOrderParam],ContextInfo)`
-

提示

算法交易下单,此时使用交易面板-程序交易-函数交易-函数交易参数中设置的下单类型(普通交易,算法交易,随机量交易) 如果函数交易参数使用未修改的默认值,此函数和passorder函数一致, 设置了函数交易参数后,将会使用函数交易参数的超价等拆单参数,algo_passorder内的prType若赋值,则优先使用该参数,若algo_passorder内的prType=-1,将会使用userOrderParam内的opType,若userOrderParam未赋值,则使用界面上的函数交易参数的报价方式

参数:
其他参数同passorder,详细解释可参考passorder的说明
userOrderParam dict[str:value] 是用户自定义交易参数,主要用于修改算法交易的参数 其中Key Value定义如下

注:所有参数均为非必选

KeyValue类型Value
OrderTypeint普通交易:0
算法交易:1
随机量交易:2
PriceTypeint报价方式:数值同passorde prType
MaxOrderCountint最大下单次数
SinglePriceRangeint波动区间是否单向:
否:0
是:1
PriceRangeTypeint波动区间类型按比例:0,按数值1
PriceRangeValuefloat波动区间(按数值)
PriceRangeRatefloat波动区间(按比例)[0-1]
SuperPriceTypeint单笔超价类型:
按比例:0
按数值1
SuperPriceRatefloat单笔超价(按比例)[0-1]
SuperPriceValuefloat单笔超价(按数值)
VolumeTypeint单笔基准量类型卖1+2+3+4+5量:0
卖1+2+3+4量:1
...
卖1量:4
买1量:5
...
买1+2+3+4+5量:9
目标量:10
目标剩余量:11
持仓数量:12
VolumeRatefloat单笔下单比率[0-1]
SingleNumMinfloat单笔下单量最小值
SingleNumMaxfloat单笔下单量最大值
ValidTimeTypeint有效时间类型:
0:按持续时间
1 按时间区间,默认为0
ValidTimeElapseint有效持续时间,ValidTimeType设置为0时生效
ValidTimeStartint有效开始时间偏移,ValidTimeType设置为1时生效
ValidTimeEndint有效结束时间偏移,ValidTimeType设置为1时生效
UndealtEntrustRuleint未成委托处理数值同prType
PlaceOrderIntervalint下撤单时间间隔
UseTriggerint是否触价:
否:0
是:1
TriggerTypeint触价类型:
最新价大于:1
最新价小于:2
TriggerPricefloat触价价格
SuperPriceEnableint超价启用笔数

返回

示例

#coding:gbk
-userparam = {
-    "OrderType": 1,
-    "MaxOrderCount": 20,
-    "SuperPriceType": 1,
-    "SuperPriceValue": 1.12}
-accid = '918800000818'  #资金账号
-algo_passorder(23,1101,accid,'000001.SZ',5,15,1000,'',1,'strReMark',userparam,ContextInfo)
-#表示修改算法交易的最大委托次数为20,单笔下单基准类型为按价格类型超价,单笔超价1.12元,其他参数同函数交易参数中设置
-

smart_algo_passorder - 智能算法(VWAP 等)函数

提示

  1. 调用该函数需要有【智能算法】使用权限

用于使用主动算法或被动算法交易的函数如VWAP TWAP等

调用方法一:

smart_algo_passorder(opType,orderType,accountid,orderCode,prType,price,volume,strageName,quickTrade,userOrderId,smartAlgoType,limitOverRate,minAmountPerOrder,[targetPriceLevel,startTime,endTime,limitControl],ContextInfo)
-

提示

可选参数可缺省

参数:
其他参数同passorder,详细解释可参考passorder的说明在新窗口打开

参数名类型说明提示
prTypeint可选值
11:限价(只对单股情况支持,对组合交易不支持)
12:市价
特别的对于套利:这个prType只对篮子起作用,期货的采用默认的方式
smartAlgoTypestr智能算法类型 [enum_constants#smartAlgoType智能算法类型在新窗口打开]
limitOverRateint量比 数据范围0-100网格算法无此项
若在algoParam中填写量比,则填写范围0-1的小数。
minAmountPerOrderint智能算法最小委托金额,数据范围0-100000
targetPriceLevelint智能算法目标价格,可选值:
1:己方盘口 1
2:己方盘口2
3:己方盘口3
4:己方盘口4
5:己方盘口5
6:最新价
7:对方盘口
一、输入无效值则targetPriceLevel为1
二、本项只针对冰山算法,其他算法可缺省。
startTimestr智能算法开始时间格式"HH:MM:SS",如"10:30:00"。如果缺省值,则默认为"09:30:00"
endTimestr智能算法截止时间格式"HH:MM:SS",如"14:30:00"。如果缺省值,则默认为"15:30:00"
limitControlint涨跌停控制默认值为1
1:涨停不卖跌停不买
0:无限制

返回

示例:

#coding:gbk
-
-
-def init(ContextInfo):
-    pass
-
-
-def after_init(ContextInfo):
-    # # 使用smart_algo_passorder 下单
-    smart_algo_passorder(
-        23,                # 买入
-        1101,              # 表示volume的单位是股
-        account,           # 资金账号
-        '000001.SZ',
-        12,                #  11限价,12市价
-        0,                 # 限价时,价格填任意数量占位
-        50000,             # 5000股
-        '',
-        2,                 # quickTrade
-        '',
-        'VWAP',
-        25,                 # 量比25%
-        0,                  # 智能算法最小委托金额
-        1,                  # 智能算法目标价格 本项只针对冰山算法,其他算法可缺省。
-        "10:25:00",         # 开始时间
-        "14:50:00",         # 结束时间
-        1,                  # 涨跌停控制 1为涨停不卖跌停不卖 0 为无限制
-        ContextInfo
-        )
-

调用方法二:
当时用algoParam时,函数声明为:smart_algo_passorder(opType,orderType,accountid,orderCode,prType,modelprice,volume,strageName,quickTrade,userid,smartAlgoType,startTime,endTime,algoParam,ContextInfo)参数均不可缺省
smartAlgoType,startTime,endTime 含义同上,algoParam请使用下面的方法获取:

获取algoParam具体字段

释义

获取智能算法参数配置信息

用法

get_smart_algo_param(algoList)
-

参数

参数类型说明
algoListlist需要查询参数配置信息的算法名称列表, 若传空则查询全部有权限的算法参数配置信息

返回

返回一个字典,键为算法名称,值为参数字典列表。

字段类型说明
keystring参数名称key值,即smart_algo_orderalgoList字典需要传的键值
namestring参数名称
dataTypestring参数类型
valueRangestring参数范围
defaultValuestring参数默认值
enumNamestring参数枚举值的名称
enumValuestring参数实际的枚举值
unitstring参数的单位, 当单位为%时, 值要填写小数而非参数范围所示的百分数值
valueRangeByNamestring不同算法参数范围
defaultValueByNamestring不同算法参数默认值

示例

#coding:gbk
-
-
-def init(ContextInfo):
-    pass
-
-    # 方法2 使用algoParam 和smart_algo_passorder
-    # 该方法部分旧版本客户端可能会不支持
-    # algoParam
-    # 先获取所有需要传入的参数
-    #
-    print(get_smart_algo_param(['VWAP']))
-    '''
-    输出:[2024-01-30 11:21:10][智能算法1][SH000300][日线] 
-    {'VWAP': [
-        {'key': 'm_dLimitOverRate', 'name': '量比比例', 'dataType': '浮点数', 'valueRange': '0.00-100.00', 'defaultValue': '20.00', 'enumName': '', 'enumValue': '', 'unit': '%', 'valueRangByName': '', 'defaultValueByName': ''}, 
-        {'key': 'm_dMinAmountPerOrder', 'name': '委托最小金额', 'dataType': '整数', 'valueRange': '0-100000', 'defaultValue': '0', 'enumName': '', 'enumValue': '', 'unit': '', 'valueRangByName': '', 'defaultValueByName': ''},
-        {'key': 'm_dMaxAmountPerOrder', 'name': '委托最大金额', 'dataType': '浮点数', 'valueRange': '0.00-100000000.00', 'defaultValue': '0', 'enumName': '', 'enumValue': '', 'unit': '', 'valueRangByName': '', 'defaultValueByName': ''}, 
-        {'key': 'm_nStopTradeForOwnHiLow', 'name': '涨跌停控制', 'dataType': '整数', 'valueRange': '', 'defaultValue': '涨停不卖跌停不买', 'enumName': '无,涨停不卖跌停不买', 'enumValue': '0,1', 'unit': '', 'valueRangByName': '', 'defaultValueByName': ''}, 
-        {'key': 'm_dMulitAccountRate', 'name': '多账号总量比', 'dataType': '浮点数', 'valueRange': '0.00-100.00', 'defaultValue': '0', 'enumName': '', 'enumValue': '', 'unit': '%', 'valueRangByName': '', 'defaultValueByName': ''}, 
-        {'key': 'm_strCmdRemark', 'name': '投资备注', 'dataType': '字符串', 'valueRange': '', 'defaultValue': '', 'enumName': '', 'enumValue': '', 'unit': '', 'valueRangByName': '', 'defaultValueByName': ''}]}
-    '''
-    algoParam={
-    'm_dLimitOverRate': 0.25,      # 量比 25%
-    'm_dMinAmountPerOrder':0,      # 委托最小金额
-    'm_dMaxAmountPerOrder':10000,  # 委托最大金额
-    'm_nStopTradeForOwnHiLow': 1,  # 涨跌停控制
-    'm_dMulitAccountRate':0.30,    # 多账号总量比
-    'm_strCmdRemark':  '投资备注1'  # 投资备注
-    }
-    smart_algo_passorder(
-        23,
-        1101,
-        account,
-        '600000.SH',
-        12,
-        0,
-        10000,
-        '',
-        2,               # quickTrade
-        '投资备注',
-        'VWAP',
-        "10:25:00",      # 开始时间
-        "14:50:00",      # 结束时间
-        algoParam,       # 算法参数
-        ContextInfo
-        ) 
-    
-

cancel-撤销委托

调用方法cancel(orderId, accountId, accountType, ContextInfo)

参数

参数名类型含义说明
orderIdstring委托号必填
accountIDstring资金账号必填
AccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
ContextInfoclass含有k线信息和接口的上下文对象必填

返回 bool,是否发出了取消委托信号,返回值含义:

True:是
False:否

示例

#coding:gbk
-'''
-(1)下单前,根据 get_trade_detail_data 函数返回账号的信息,判定资金是否充足,账号是否在登录状态,统计持仓情况等等。
-(2)满足一定的模型条件,用 passorder 下单。
-(3)下单后,时刻根据 get_last_order_id 函数获取委托和成交的最新id,注意如果委托生成了,就有了委托号(这个id需要自己保存做一个全局控制)。
-(4)用该委托号根据 get_value_by_order_id 函数查看委托的状态,各种情况等。
-当一个委托的状态变成“已成'后,那么对应的成交 deal 信息就有一条成交数据;用该委托号可查看成交情况。
-*注:委托列表和成交列表中的委托号是一样的,都是这个 m_strOrderSysID 属性值。
-可用 get_last_order_id 获取最新的 order 的委托号,然后根据这个委托号获取 deal 的信息,当获取成功后,也说明这笔交易是成了,可再根据 position 持仓信息再进一步验证。
-(5)根据委托号获取委托信息,根据委托状态,或模型设定,用 cancel 取消委托。
-'''
-
-
-def init(ContextInfo):
-    ContextInfo.accid = '6000000248'
-
-def handlebar(ContextInfo):
-    if ContextInfo.is_last_bar():
-        orderid = get_last_order_id(ContextInfo.accid, 'stock', 'order')
-        print(cancel(orderid, ContextInfo.accid, 'stock', ContextInfo))
-

cancel_task - 撤销任务

调用方法cancel_task(taskId,accountId,accountType,ContextInfo)

参数

参数名类型含义说明
taskIdstring委托号必填
accountIDstring资金账号必填
AccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
ContextInfoclass含有k线信息和接口的上下文对象必填

返回 bool,是否发出了撤销任务信号,返回值含义:

True:是

False:否

示例

#coding:gbk
-'''
-(1)根据get_trade_detail_data函数返回任务的信息,获取任务编号(m_nTaskId),任务状态等等;
-(2)根据任务编号,用cancel_task取消委托。
-'''
-
-def init(ContextInfo):
-    ContextInfo.accid = '6000000248'
-
-def handlebar(ContextInfo):
-    # 获取当前客户端所有的任务
-    if ContextInfo.is_last_bar():
-        objlist = get_trade_detail_data(ContextInfo.accid,'stock','task')
-        for obj in objlist:
-            cancel_task(str(obj.m_nTaskId),ContextInfo.accid,'stock',ContextInfo)
-

pause_task - 暂停任务

暂停智能算法任务

调用方法 pause_task(taskId,accountId,accountType,ContextInfo)

参数

参数名类型含义说明
taskIdstring委托号必填
accountIDstring资金账号必填
AccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
ContextInfoclass含有k线信息和接口的上下文对象必填

返回 bool,是否发出了暂停任务信号,返回值含义:

True:是

False:否

示例

#coding:gbk
-'''
-(1)根据get_trade_detail_data函数返回任务的信息,获取任务编号(m_nTaskId),任务状态等等;
-(2)根据任务编号,用pause_task暂停智能算法任务。
-'''
-
-def init(ContextInfo):
-    ContextInfo.accid = '6000000248'    
-
-def handlebar(ContextInfo):
-    
-    if ContextInfo.is_last_bar():
-        # 获取当前客户端所有的任务
-        objlist = get_trade_detail_data(ContextInfo.accid,'stock','task')
-        for obj in objlist:
-            pause_task(obj.m_nTaskId,ContextInfo.accid,'stock',ContextInfo)
-

resume_task - 继续任务

继续智能算法任务

调用方法resume_task(taskId,accountId,accountType,ContextInfo)

参数

参数名类型含义说明
taskIdstring委托号必填
accountIDstring资金账号必填
AccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
ContextInfoclass含有k线信息和接口的上下文对象必填

返回 bool,是否发出了重启任务信号,返回值含义:

True:是

False:否

示例

#coding:gbk
-'''
-(1)根据get_trade_detail_data函数返回任务的信息,获取任务编号(m_nTaskId),任务状态等等;
-(2)根据任务编号,用resume_task启动已暂停智能算法任务。
-'''
-
-def init(ContextInfo):
-    ContextInfo.accid = '6000000248'    
-def handlebar(ContextInfo):
-    if ContextInfo.is_last_bar():
-        # 获取当前客户端所有的任务
-        objlist = get_trade_detail_data(ContextInfo.accid,'stock','task')
-        for obj in objlist:
-            resume_task(obj.m_nTaskId,ContextInfo.accid,'stock',ContextInfo)
-

get_basket-获取股票篮子

用法: get_basket(basketName)

释义: 获取股票篮子

参数:

  • basketName:股票篮子名称

示例:

print( get_basket('basket1') )
-

set_basket-设置股票篮子

用法: set_basket(basketDict)

释义: 设置passorder的股票篮子,仅用于passorder进行篮子交易,设置成功后,用get_basket可以取出后即可进行passorder组合交易下单

参数:

  • basketDict:股票篮子 {'name':股票篮子名称,'stocks':[{'stock':股票名称,'weight',权重,'quantity':数量,'optType':交易类型}]} 。

示例:

table=[
-    {'stock':'600000.SH','weight':0.11,'quantity':100,'optType':23},
-    {'stock':'600028.SH','weight':0.11,'quantity':200,'optType':24},
-]
-basket={'name':'basket1','stocks':table}
-set_basket(basket)
-#一键买卖2份(2101代表用篮子里quantity字段)basket1里面的股票组合,即600000.SH买入200股,600028.SH卖出400股
-passorder(35,2101,ContextInfo.accid,'basket1',5,-1,2,'basketOrder',2,'basketOrder',ContextInfo)
-

交易查询函数

get_trade_detail_data-查询账号资金信息、委托记录等

调用方法 get_trade_detail_data(accountID, strAccountType, strDatatype, strategyName)
或不区分策略
get_trade_detail_data(accountID, strAccountType, strDatatype)

参数

参数名类型说明备注
accountIDstring资金账号必填
strAccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
strDatatypestring要查询数据类型 可选:
ACCOUNT账号对象在新窗口打开信用账号对象在新窗口打开
POSITION持仓在新窗口打开
POSITION_STATISTICS持仓统计在新窗口打开
ORDER委托在新窗口打开
DEAL成交在新窗口打开
TASK任务在新窗口打开
必填
strategyNamestring策略 当用passorder下单时指定了strategyName 参数时,当查询成交和委托时传入同样的strageName,则可以只返回包含strategyName的委托子集或成交子集strategyName参数只对成交和委托有效,选填

返回 list,list 中放的是对应strDatatype的 Python对象,通过 dir(pythonobj) 可返回某个对象的属性列表。

示例:


-#coding:gbk
-
-account = '800174' # 在策略交易界面运行时,account的值会被赋值为策略配置中的账号,编辑器界面运行时,需要手动赋值;编译器环境里执行的下单函数不会产生实际委托
-
-def init(ContextInfo):
-    pass
-
-def handlebar(ContextInfo):
-    if not ContextInfo.is_last_bar():
-        return
-    
-    orders = get_trade_detail_data(account, 'stock', 'order')
-    print('查询委托结果:')
-    for o in orders:
-        print(f'股票代码: {o.m_strInstrumentID}, 市场类型: {o.m_strExchangeID}, 证券名称: {o.m_strInstrumentName}, 买卖方向: {o.m_nOffsetFlag}',
-        f'委托数量: {o.m_nVolumeTotalOriginal}, 成交均价: {o.m_dTradedPrice}, 成交数量: {o.m_nVolumeTraded}, 成交金额:{o.m_dTradeAmount}')
-
-
-    deals = get_trade_detail_data(account, 'stock', 'deal')
-    print('查询成交结果:')
-    for dt in deals:
-        print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 买卖方向: {dt.m_nOffsetFlag}', 
-        f'成交价格: {dt.m_dPrice}, 成交数量: {dt.m_nVolume}, 成交金额: {dt.m_dTradeAmount}')
-
-    positions = get_trade_detail_data(account, 'stock', 'position')
-    print('查询持仓结果:')
-    for dt in positions:
-        print(f'股票代码: {dt.m_strInstrumentID}, 市场类型: {dt.m_strExchangeID}, 证券名称: {dt.m_strInstrumentName}, 持仓量: {dt.m_nVolume}, 可用数量: {dt.m_nCanUseVolume}',
-        f'成本价: {dt.m_dOpenPrice:.2f}, 市值: {dt.m_dInstrumentValue:.2f}, 持仓成本: {dt.m_dPositionCost:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}')
-
-
-    accounts = get_trade_detail_data(account, 'stock', 'account')
-    print('查询账号结果:')
-    for dt in accounts:
-        print(f'总资产: {dt.m_dBalance:.2f}, 净资产: {dt.m_dAssureAsset:.2f}, 总市值: {dt.m_dInstrumentValue:.2f}', 
-        f'总负债: {dt.m_dTotalDebit:.2f}, 可用金额: {dt.m_dAvailable:.2f}, 盈亏: {dt.m_dPositionProfit:.2f}')
-    
-    position_statistics = get_trade_detail_data(account,"FUTURE",'POSITION_STATISTICS')
-    for obj in position_statistics:
-        if obj.m_nDirection == 49:
-			continue
-		PositionInfo_dict[obj.m_strInstrumentID+"."+obj.m_strExchangeID]={
-		"持仓":obj.m_nPosition,
-		"成本":obj.m_dPositionCost,
-		"浮动盈亏":obj.m_dFloatProfit,
-		"保证金占用":obj.m_dUsedMargin
-		}
-	print(PositionInfo_dict)
-
-	
-

get_history_trade_detail_data - 查询历史交易明细

用法: get_history_trade_detail_data(accountID,strAccountType,strDatatype,strStratDate,strEndDate);

释义: 获取历史成交明细数据,返回结果为一个([timetag,obj...])的元组

参数:

accountID:string,账号; strAccountType:string,账号类型,有"FUTURE","STOCK","CREDIT","HUGANGTONG","SHENGANGTONG","STOCK_OPTION"; strDatatype:string,交易明细数据类型,有:持仓"POSITION"、委托"ORDER"、成交"DEAL"; strStratDate:string,开始时间,如'20240513'; strEndDate:string,结束时间,如'20240514';

**返回:**list,list中放的是PythonObj,通过dir(pythonobj)可返回某个对象的属性列表 示例:

def handlebar(ContextInfo):
-    obj_list = get_history_trade_detail_data('6000000248','stock','position','20240513','20240514')
-    for time,data in obj_list:
-        for obj in data:
-            print(obj.m_strInstrumentID)
-            print(dir(obj))#查看有哪些属性字段
-

get_ipo_data-获取当日新股新债信息

用法: get_ipo_data([,type])

释义: 获取当日新股新债信息,返回结果为一个字典,包括新股申购代码,申购名称,最大申购数量,最小申购数量等数据

参数:

  • type:为空时返回新股新债信息,type="STOCK"时只返回新股申购信息,type="BOND"时只返回新债申购信息

示例:

#coding:gbk
-def init(ContextInfo):
-    ipoData=get_ipo_data()# 返回新股新债信息
-    ipoStock=get_ipo_data("STOCK")# 返回新股信息
-    ipoCB=get_ipo_data("BOND")# 返回新债申购信息
-

get_new_purchase_limit-获取账户新股申购额度

用法: get_new_purchase_limit(accid)

释义: 获取账户新股申购额度,返回结果为一个字典,包括上海主板,深圳市场,上海科创版的申购额度

参数:

  • accid:资金账号,必须时股票账号或者信用账号

示例:

def init(ContextInfo):
-    ContextInfo.accid="10000001"# 返回新股新债信息
-    purchase_limit=get_new_purchase_limit(ContextInfo.accid)
-

get_value_by_order_id-根据委托号获取委托或成交信息

调用方法get_value_by_order_id(orderId, accountID, strAccountType, strDatatype)

参数

参数名类型含义说明
orderIdstring委托号必填
accountIDstring资金账号必填
strAccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
strDatatypestring要查询数据类型 可选:
'ORDER':委托
'DEAL' :成交
必填

返回

委托对象成交对象

示例

def init(ContextInfo):
-    ContextInfo.accid = '6000000248'
-
-def handlebar(ContextInfo):
-    orderid = get_last_order_id(ContextInfo.accid, 'stock', 'order')
-    print(orderid)
-    obj = get_value_by_order_id(orderid,ContextInfo.accid, 'stock', 'order')
-    print(obj.m_strInstrumentID)
-

调用方法

# 区分策略,添加策略名称参数 strategyName
-get_last_order_id(accountID, strAccountType, strDatatype, strategyName)
-
-# 不区分策略
-get_last_order_id(accountID, strAccountType, strDatatype)
-

参数

参数名类型含义说明
accountIDstring资金账号必填
strAccountTypestring账号类型 可选:
'FUTURE':期货
'STOCK':股票
'CREDIT':信用
'HUGANGTONG':沪港通
'SHENGANGTONG':深港通
'STOCK_OPTION':期权
必填
strDatatypestring要查询数据类型 可选:
'ORDER':委托
'DEAL' :成交
必填
strategyNamestring策略 当用passorder下单时指定了strategyName 参数时,当查询成交和委托时传入同样的strageName,则可以只返回包含strategyName的委托子集或成交子集选填

返回

String,委托号,如果没找到返回 '-1'。

示例

def init(ContextInfo):
-    ContextInfo.accid = '6000000248'
-
-def handlebar(ContextInfo):
-    orderid = get_last_order_id(ContextInfo.accid, 'stock', 'order')
-    print(orderid)
-    obj = get_value_by_order_id(orderid,ContextInfo.accid, 'stock', 'order')
-    print(obj.m_strInstrumentID)
-

get_assure_contract-获取两融担保标的明细

用法: get_assure_contract(accId)

释义: 获取信用账户担保合约明细

参数:

  • accId:信用账户

返回: list,list 中放的是 StkSubjects在新窗口打开,通过 dir(pythonobj) 可返回某个对象的属性列表。

示例:


-def show_data(data):
-    tdata = {}
-    for ar in dir(data):
-        if ar[:2] != 'm_':continue
-        try:
-            tdata[ar] = data.__getattribute__(ar)
-        except:
-            tdata[ar] = '<CanNotConvert>'
-    return tdata
-
-def handlebar(ContextInfo): 
-    obj = get_assure_contract('6000000248')
-    for i in obj[:3]:
-		print(show_data(i))
-
-
-"""
-{'m_dAssureRatio': 0.0, # 担保品折算比例
-'m_dFinRatio': 0.8, # 融资保证金比例
-'m_dSloRatio': 1.0,  # 融券保证金比例
-'m_eAssureStatus': 50,  # 是否可做担保
-'m_eCreditFundCtl': 50, # 融资交易控制
-'m_eCreditStkCtl': 50, # 融券交易控制
-'m_eFinStatus': 48, # 融资状态
-'m_eSloStatus': 48, # 融券状态
-'m_nPlatformID': 10064,  # 平台号
-'m_strAccountID': '95000857',  # 资金账号
-'m_strBrokerID': '003', # 经纪公司编号
-'m_strBrokerName': '光大证券信用',  # 证券公司
-'m_strExchangeID': 'SH', # 交易所
-'m_strInstrumentID':'510150' # 证券代码
-"""
-        
-

get_enable_short_contract-获取可融券明细

提示

注:由于字段m_dSloRatio、m_dSloStatus提供来源和取担保品明细(get_assure_contract)重复,字段在2021年9月移除,后续用担保品明细接口获取,具体见 担保标的对象字段说明在新窗口打开

用法: get_enable_short_contract(accId)

释义: 获取信用账户当前可融券的明细

参数:

  • accId:信用账户

返回: list,list 中放的是 CreditSloEnableAmount在新窗口打开,通过 dir(pythonobj) 可返回某个对象的属性列表。

示例:

#coding:gbk
-def init(ContextInfo):
-    pass
-
-def handlebar(ContextInfo):
-    if not ContextInfo.is_last_bar():
-        return
-
-    obj = get_enable_short_contract('200133')
-
-    for i in obj[:3]:
-        print('' * 50)
-        print(f"平台号        : {i.m_nPlatformID}")
-        print(f"经纪公司编号  : {i.m_strBrokerID}")
-        print(f"经纪公司      : {i.m_strBrokerName}")
-        print(f"资金账号      : {i.m_strAccountID}")
-        print(f"交易所        : {i.m_strExchangeID}")
-        print(f"证券代码      : {i.m_strInstrumentID}")
-        print(f"融券可融数量  : {i.m_nEnableAmount}")
-        print(f"查询类型      : {i.m_eQuerySloType}")  # 48:普通,49:专项
-        print('' * 50)
-"""
-Return
-──────────────────────────────────────────────────
-平台号        : 11111
-经纪公司编号  : 
-经纪公司      : 模拟证券
-资金账号      : 2000233
-交易所        : SZ
-证券代码      : 000001
-融券可融数量  : 500000
-查询类型      : 49
-──────────────────────────────────────────────────
-"""
-

query_credit_account - 查询信用账户明细

注意

  1. 本函数一次最多查询200只股票的两融最大下单量,且同时只能有一个查询,如果前面的查询正在进行中,后面的查询将会提前返回。本函数从服务器查询数据,建议平均查询时间间隔180s一次,不可频繁调用。
  2. 该函数必须配合credit_account_callback回调才能使用,关于此回调的说明请看credit_account_callback在新窗口打开
  3. callback返回的对象是CCreditAccountDetail在新窗口打开

调用query_credit_account,该接口的查询结果将会推送给credit_account_callback,所以程序里需要按照函数参数实现函数credit_account_callback,callback返回的对象是CCreditAccountDetail在新窗口打开

用法: query_credit_account(accountId,seq,ContextInfo)
释义: 查询信用账户明细。本函数只能有一个查询,如果前面的查询正在进行中,后面的查询将会提前返回。

参数:

  • accountId:string,查询的两融账号

  • seq:int,查询序列号,建议输入唯一值以便对应结果回调

示例:

#coding:gbk
-
-
-import time
-
-def init(ContextInfo):
-	ContextInfo.accid='200133'
-	
-def handlebar(ContextInfo):
-	if ContextInfo.is_last_bar():
-		query_credit_account(ContextInfo.accid,int(time.time()),ContextInfo)
-# 该函数必须配合credit_account_callback回调才能使用
-def credit_account_callback(ContextInfo,seq,result):
-	print(seq)
-	print(f":维持担保比例:{result.m_dPerAssurescaleValue:.2f},总负债:{result.m_dTotalDebt:.2f}")
-
-

回调示例query_credit_account在新窗口打开

query_credit_opvolume - 查询两融最大可下单量

注意

  1. 本函数一次最多查询200只股票的两融最大下单量,且同时只能有一个查询,如果前面的查询正在进行中,后面的查询将会提前返回。本函数从服务器查询数据,建议平均查询时间间隔180s一次,不可频繁调用。
  2. 该函数必须配合credit_opvolume_callback回调才能使用,关于此回调的说明请看credit_account_callback在新窗口打开

调用query_credit_opvolume,该接口的查询结果将会推送给credit_opvolume_callback,所以必须配合credit_opvolume_callback回调才能使用

用法: query_credit_opvolume(accountId,stockCode,opType,prType,price,seq,ContextInfo)

释义: 查询两融最大可下单量。

参数:

  • accountId:查询的两融账号
  • stockCode:需要查询的股票代码,stockCode为List的类型,可以查询多只股票
  • opType:两融下单类型,同passorder的下单类型
  • prType:报单价格类型,同passorder的报价类型
  • seq:查询序列号,int型,建议输入唯一值以便对应结果回调
  • price:报价(非限价单可以填任意值),如果stockCodeList类型,报价也需要为长度相同的List
  • ContextInfo:ContextInfo类

示例:

#coding:gbk
-"""
-QMT内置python - 两融最大可下单量查询完整示例(run_time定时器版)
-
-功能说明:
-    1. 不依赖handlebar行情驱动,改用run_time定时器驱动
-    2. 定时器周期设为180秒(3分钟),严格满足查询间隔要求
-    3. 通过 credit_opvolume_callback 接收查询结果
-    4. 演示如何根据查询结果执行实际下单(可选)
-
-注意事项:
-    - run_time定时器在回测模式下无效,仅用于实盘
-    - 定时器没有单独结束方法,策略停止时自动结束
-    - 本函数一次最多查询200只股票
-    - 同时只能有一个查询在进行中,前面查询未完成时后续查询会返回-1
-    - 必须从服务器查询数据,建议平均查询时间间隔180s一次,不可频繁调用
-    - 该函数必须配合 credit_opvolume_callback 回调才能使用
-"""
-
-import time
-
-# ==================== 全局变量 ====================
-class G:
-    pass
-
-g = G()
-
-# ============================================================
-# init - 初始化函数(策略启动时仅执行一次)
-# ============================================================
-def init(ContextInfo):
-    """初始化:设置账号、初始化全局变量、注册定时器"""
-    
-    # 设置两融账号(必须先在QMT客户端登录该账号)
-    ContextInfo.accid = '200133'
-    
-    # 记录上次查询时间戳,用于控制查询间隔(>=180秒)
-    g.last_query_time = 0
-    
-    # 记录上次查询的序列号,用于匹配回调结果
-    g.last_seq = 0
-    
-    # 存储查询结果,供后续下单逻辑使用
-    g.query_result = {}
-    
-    # 设置要监听的账号(如需资金/委托/成交回调则启用)
-    # ContextInfo.set_account(ContextInfo.accid)
-    
-    # ==================== 注册run_time定时器 ====================
-    # 用法: ContextInfo.run_time(funcName, period, startTime)
-    #   funcName:  回调函数名称(字符串)
-    #   period:    重复调用间隔,'180nSecond'表示每180秒触发一次
-    #   startTime: 首次触发时间,设置历史时间可尽快启动
-    #
-    # 注意:定时器在第一次运行前可能会先等待一个period(180秒),
-    #       这正好符合服务器要求的最小查询间隔。
-    ContextInfo.run_time(
-        "query_credit_timer",       # 定时器回调函数名
-        "180nSecond",               # 每180秒(3分钟)触发一次
-        "1970-01-01 00:00:00"       # 设置历史时间,使定时器尽快生效
-    )
-    
-    print(f'[{time.strftime("%H:%M:%S")}] 策略初始化完成')
-    print(f'[{time.strftime("%H:%M:%S")}] 已注册run_time定时器:每180秒执行一次 query_credit_timer')
-    print(f'[{time.strftime("%H:%M:%S")}] 监控账号: {ContextInfo.accid}')
-
-
-# ============================================================
-# handlebar - 行情事件函数(本示例中不执行逻辑,留空即可)
-# ============================================================
-def handlebar(ContextInfo):
-    """
-    行情事件函数。
-    本策略完全由run_time定时器驱动,handlebar中不做任何操作。
-    如需在K线变化时执行其他逻辑,可在此添加。
-    """
-    pass
-
-
-# ============================================================
-# query_credit_timer - run_time定时器回调函数
-# ============================================================
-def query_credit_timer(ContextInfo):
-    """
-    run_time定时器回调函数,每180秒触发一次
-    
-    功能:
-        1. 检查查询间隔是否满足>=180秒(双重保险)
-        2. 调用 query_credit_opvolume 发起查询
-        3. 结果通过 credit_opvolume_callback 异步返回
-    """
-    
-    current_time = time.time()
-    
-    # ==================== 双重保险:检查查询间隔 ====================
-    # run_time本身已设置为180秒周期,此处再加一层防护,
-    # 防止因QMT异常重调导致查询过于频繁
-    time_since_last = current_time - g.last_query_time
-    if time_since_last < 180:
-        print(f'[{time.strftime("%H:%M:%S")}] [定时器] 距离上次查询仅 {int(time_since_last)} 秒,跳过本次调用')
-        return
-    
-    # 更新上次查询时间
-    g.last_query_time = current_time
-    
-    # 生成唯一查询序列号(用时间戳即可)
-    seq = int(current_time)
-    g.last_seq = seq
-    
-    # ==================== 示例1:单股票查询 ====================
-    print(f'\n[{time.strftime("%H:%M:%S")}] [定时器] === 发起单股票查询,seq={seq} ===')
-    
-    query_credit_opvolume(
-        ContextInfo.accid,      # 两融账号
-        '600000.SH',            # 股票代码(单只)
-        33,                     # opType: 33=担保品买入
-        11,                     # prType: 11=指定价
-        10.0,                   # price: 限价10元
-        seq,                    # seq: 查询序列号
-        ContextInfo             # ContextInfo对象
-    )
-    
-    # ==================== 示例2:多股票查询(可选,取消注释使用) ====================
-    # seq2 = int(current_time) + 1
-    # print(f'[{time.strftime("%H:%M:%S")}] [定时器] === 发起多股票查询,seq={seq2} ===')
-    # query_credit_opvolume(
-    #     ContextInfo.accid,
-    #     ['600000.SH', '000001.SZ'],     # 股票列表(最多200只)
-    #     33,                             # 担保品买入
-    #     11,                             # 指定价
-    #     [10.0, 20.0],                   # 对应价格列表
-    #     seq2,
-    #     ContextInfo
-    # )
-
-
-# ============================================================
-# credit_opvolume_callback - 两融可下单量查询结果回调
-# ============================================================
-def credit_opvolume_callback(ContextInfo, accid, seq, ret, result):
-    """
-    两融最大可下单量查询结果回调(必须定义此函数才能接收结果)
-    
-    参数:
-        ContextInfo: 策略上下文对象
-        accid:       资金账号
-        seq:         查询时传入的序列号,可用于匹配是哪次查询的结果
-        ret:         查询结果状态码(1成功,-1查询中,-2账号非法,-3参数非法,-4超时/报错)
-        result:      查询结果数据(ret=1时有效,具体格式取决于券商返回)
-    """
-    
-    print(f'\n[{time.strftime("%H:%M:%S")}] === credit_opvolume_callback 触发 ===')
-    print(f'账号: {accid}')
-    print(f'序列号: {seq} (上次发送seq={g.last_seq})')
-    print(f'返回码 ret: {ret}')
-    
-    # -------------------- 状态码处理 --------------------
-    if ret == 1:
-        # 查询成功
-        print(f'查询成功!结果: {result}')
-        
-        # 保存结果到全局变量,供后续策略逻辑使用
-        g.query_result[seq] = {
-            'accid': accid,
-            'result': result,
-            'time': time.time()
-        }
-        
-        # ==================== 示例:根据查询结果下单 ====================
-        # 假设 result 中包含可下单量信息(具体字段名需根据实际返回格式调整)
-        # 以下为演示逻辑,实际使用时请根据result的实际结构解析
-        
-        # max_volume = parse_max_volume(result)  # 需根据实际返回格式解析
-        # if max_volume and max_volume > 0:
-        #     print(f'最大可下单量: {max_volume},准备下单...')
-        #     passorder(
-        #         33, 1101, accid, '600000.SH',
-        #         11, 10.0, max_volume,
-        #         '两融策略', 2, '', ContextInfo
-        #     )
-        # else:
-        #     print('可下单量为0或无法解析,取消下单')
-        
-    elif ret == -1:
-        print('查询失败:前序查询正在进行中,请等待完成后重试')
-        
-    elif ret == -2:
-        print('查询失败:输入账号非法,请检查账号是否为有效两融账号')
-        
-    elif ret == -3:
-        print('查询失败:输入查询参数非法,请检查股票代码/价格/操作类型等参数')
-        
-    elif ret == -4:
-        print('查询失败:超时或服务器返回报错,建议稍后重试')
-        
-    else:
-        print(f'查询失败:未知状态码 {ret}')
-    
-    print('=' * 50)
-
-
-

get_option_subject_position-取期权标的持仓

用法: get_option_subject_position(accountID)

释义: 取期权标的持仓

参数:

  • accountID:string,账号

返回: list,list中放的是CLockPosition在新窗口打开,通过dir(pythonobj)可返回某个对象的属性列表

示例:

data=get_option_subject_position('880399990383')
-print(len(data));
-forobjindata:
-    print(obj.m_strInstrumentName,obj.m_lockVol,obj.m_coveredVol);
-

get_comb_option-取期权组合持仓

用法: get_comb_option(accountID)

释义: 取期权组合持仓

参数:

  • accountID:string,账号

返回: list,list中放的是CStkOptCombPositionDetail 在新窗口打开,通过dir(pythonobj)可返回某个对象的属性列表

示例:

obj_list=get_comb_option('880399990383')
-print(len(obj_list));
-forobjinobj_list:
-    print(obj.m_strCombCodeName,obj.m_strCombID,obj.m_nVolume,obj.m_nFrozenVolume)
-

get_unclosed_compacts-获取未了结负债合约明细

用法: get_unclosed_compacts(accountID,accountType)

释义: 获取未了结负债合约明细

参数:

  • accountID:str,资金账号
  • accountType:str,账号类型,这里应该填'CREDIT'

返回:

list([ CStkUnclosedCompacts, ... ]) 负债列表,CStkUnclosedCompacts属性如下:

字段名称类型说明
m_strAccountIDstring账号ID
m_nBrokerTypeint账号类型
1-期货账号
2-股票账号
3-信用账号
5-期货期权账号
6-股票期权账号
7-沪港通账号
11-深港通账号
m_strExchangeIDstring市场
m_strInstrumentIDstring证券代码
m_eCompactTypeint合约类型
32-不限制
48-融资
49-融券
m_eCashgroupPropint头寸来源
32-不限制
48-普通头寸
49-专项头寸
m_nOpenDateint开仓日期(如'20201231')
m_nBusinessVolint合约证券数量
m_nRealCompactVolint未还合约数量
m_nRetEndDateint到期日(如'20201231')
m_dBusinessBalancefloat合约金额
m_dBusinessFarefloat合约息费
m_dRealCompactBalancefloat未还合约金额
m_dRealCompactFarefloat未还合约息费
m_dRepaidFarefloat已还息费
m_dRepaidBalancefloat已还金额
m_strCompactIdstring合约编号
m_strEntrustNostring委托编号
m_nRepayPriorityint偿还优先级
m_strPositionStrstring定位串
m_eCompactRenewalStatusint合约展期状态
48-可申请
49-已申请
50-审批通过
51-审批不通过
52-不可申请
53-已执行
54-已取消
m_nDeferTimesint展期次数

示例:

get_unclosed_compacts('6000000248', 'CREDIT')
-

get_closed_compacts-获取已了结负债合约明细

用法: get_closed_compacts(accountID,accountType)

释义: 获取已了结负债合约明细

参数:

  • accountID:str,资金账号
  • accountType:str,账号类型,这里应该填'CREDIT'

返回:

list([ CStkUnclosedCompacts, ... ]) 负债列表,CStkUnclosedCompacts属性如下:

字段名类型描述
m_strAccountIDstring账号ID
m_nBrokerTypeint账号类型
1-期货账号
2-股票账号
3-信用账号
5-期货期权账号
6-股票期权账号
7-沪港通账号
11-深港通账号
m_strExchangeIDstring市场
m_strInstrumentIDstring证券代码
m_eCompactTypeint合约类型
32-不限制
48-融资
49-融券
m_eCashgroupPropint头寸来源
32-不限制
48-普通头寸
49-专项头寸
m_nOpenDateint开仓日期(如'20201231')
m_nBusinessVolint合约证券数量
m_nRetEndDateint到期日(如'20201231')
m_nDateClearint了结日期(如'20201231')
m_nEntrustVolint委托数量
m_dEntrustBalancefloat委托金额
m_dBusinessBalancefloat合约金额
m_dBusinessFarefloat合约息费
m_dRepaidFarefloat已还息费
m_dRepaidBalancefloat已还金额
m_strCompactIdstring合约编号
m_strEntrustNostring委托编号
m_strPositionStrstring定位串

示例:

get_closed_compacts('6000000248', 'CREDIT')
-

其他交易函数(仅回测可用)

警告

以下函数仅回测生效,实盘和模拟盘交易均不可用

order_lots-指定手数交易

用法: order_lots(stockcode, lots[, style, price], ContextInfo[, accId])

释义: 指定手数交易,指定手数发送买/卖单。如有需要落单类型当做一个参量传入,如果忽略掉落单类型,那么默认以最新价下单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • lots:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定 选此参数时必须指定有效的price参数,其他style值可不用传入price参数

    'HANG':挂单 用己方盘口挂单,即买入时用盘口买一价下单,卖出时用卖一价挂单,

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下 1 手买入
-    order_lots('000002.SZ', 1, ContextInfo, '600000248')
-
-    # 用对手价下 1 手卖出
-    order_lots('000002.SZ', -1, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 下 2 手卖出
-    order_lots('000002.SZ', -2, 'fix', 37.5, ContextInfo, '600000248')
-

order_value-指定价值交易

用法: order_value(stockcode, value[, style, price], ContextInfo[, accId])

释义: 指定价值交易,使用想要花费的金钱买入 / 卖出股票,而不是买入 / 卖出想要的股数,正数代表买入,负数代表卖出。股票的股数总是会被调整成对应的 100 的倍数(在中国 A 股市场 1 手是 100 股)。当您提交一个卖单时,该方法代表的意义是您希望通过卖出该股票套现的金额,如果金额超出了您所持有股票的价值,那么您将卖出所有股票。需要注意,如果资金不足,该 API 将不会创建发送订单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • value:金额(元),double

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下 10000 元买入
-    order_value('000002.SZ', 10000, ContextInfo, '600000248')
-
-    # 用对手价下 10000 元卖出
-    order_value('000002.SZ', -10000, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 下 20000 元卖出
-    order_value('000002.SZ', -20000, 'fix', 37.5, ContextInfo, '600000248')
-

order_percent-指定比例交易

用法: order_percent(stockcode, percent[, style, price], ContextInfo[, accId])

释义: 指定比例交易,发送一个等于目前投资组合价值(市场价值和目前现金的总和)一定百分比的买 / 卖单,正数代表买,负数代表卖。股票的股数总是会被调整成对应的一手的股票数的倍数(1 手是 100 股)。百分比是一个小数,并且小于或等于1(小于等于100%),0.5 表示的是 50%。需要注意,如果资金不足,该 API 将不会创建发送订单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • percent:比例,double

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下 5.1% 价值买入
-    order_percent('000002.SZ', 0.051, ContextInfo, '600000248')
-
-    # 用对手价下 5.1% 价值卖出
-    order_percent('000002.SZ', -0.051, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 下 10.2% 价值卖出
-    order_percent('000002.SZ', -0.102, 'fix', 37.5, ContextInfo, '600000248')
-

order_target_value-指定目标价值交易

用法: order_target_value(stockcode, tar_value[, style, price], ContextInfo[, accId])

释义: 指定目标价值交易,买入 / 卖出并且自动调整该证券的仓位到一个目标价值。如果还没有任何该证券的仓位,那么会买入全部目标价值的证券;如果已经有了该证券的仓位,则会买入 / 卖出调整该证券的现在仓位和目标仓位的价值差值的数目的证券。需要注意,如果资金不足,该API将不会创建发送订单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • tar_value:目标金额(元),double,非负数

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下调仓到 10000 元持仓   
-    order_target_value('000002.SZ', 10000, ContextInfo, '600000248')
-
-    # 用对手价调仓到 10000 元持仓   
-    order_target_value('000002.SZ', 10000, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 下调仓到 20000 元持仓
-    order_target_value('000002.SZ', 20000, 'fix', 37.5, ContextInfo, '600000248')
-

order_target_percent-指定目标比例交易

用法: order_target_percent(stockcode, tar_percent[, style, price], ContextInfo[, accId])

释义: 指定目标比例交易,买入 / 卖出证券以自动调整该证券的仓位到占有一个指定的投资组合的目标百分比。投资组合价值等于所有已有仓位的价值和剩余现金的总和。买 / 卖单会被下舍入一手股数(A 股是 100 的倍数)的倍数。目标百分比应该是一个小数,并且最大值应该小于等于1,比如 0.5 表示 50%,需要注意,如果资金不足,该API将不会创建发送订单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • tar_percent:目标百分比 [0 ~ 1],double

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下买入调仓到 5.1% 持仓
-    order_target_percent('000002.SZ', 0.051, ContextInfo, '600000248')
-
-    # 用对手价调仓到 5.1% 持仓   
-    order_target_percent('000002.SZ', 0.051, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 调仓到 10.2% 持仓
-    order_target_percent('000002.SZ', 0.102, 'fix', 37.5, ContextInfo, '600000248')
-

order_shares-指定股数交易

用法: order_shares(stockcode, shares[, style, price], ContextInfo[, accId])

释义: 指定股数交易,指定股数的买 / 卖单,最常见的落单方式之一。如有需要落单类型当做一个参量传入,如果忽略掉落单类型,那么默认以最新价下单。

参数:

  • stockcode:代码,string,如 '000002.SZ'

  • shares:股数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE5', 'SALE4', 'SALE3', 'SALE2', 'SALE1':卖5-1价

    'BUY1', 'BUY2', 'BUY3', 'BUY4', 'BUY5':买1-5价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下 100 股买入 
-    order_shares('000002.SZ', 100, ContextInfo, '600000248')
-
-    # 用对手价下 100 股卖出   
-    order_shares('000002.SZ', -100, 'COMPETE', ContextInfo, '600000248')
-
-    # 用指定价 37.5 下 200 股卖出
-    order_shares('000002.SZ', -200, 'fix', 37.5, ContextInfo, '600000248')
-

buy_open-期货买入开仓

用法: buy_open(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货买入开仓

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价 1 手买入开仓 
-    buy_open('IF1805.IF', 1, ContextInfo, '110476')
-
-    # 用对手价 1 手买入开仓   
-    buy_open('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-
-    # 用指定价 3750 元 2 手买入开仓
-    buy_open('IF1805.IF', 2, 'fix', 3750, ContextInfo, '110476')
-

buy_close_tdayfirst-期货买入平仓(平今优先)

用法: buy_close_tdayfirst(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货买入平仓,平今优先

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价 1 手买入平仓,平今优先  
-    buy_close_tdayfirst('IF1805.IF', 1, ContextInfo, '110476')
-
-    # 用对手价 1 手买入平仓,平今优先   
-    buy_close_tdayfirst('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-
-    # 用指定价 3750 元 2 手买入平仓,平今优先
-    buy_close_tdayfirst('IF1805.IF', 2, 'fix', 3750, ContextInfo, '110476')
-

buy_close_ydayfirst-期货买入平仓(平昨优先)

用法: buy_close_ydayfirst(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货买入开仓,平昨优先

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价 1 手买入平仓,平昨优先
-    buy_close_ydayfirst('IF1805.IF', 1, ContextInfo, '110476')
-
-    # 用对手价 1 手买入平仓,平昨优先   
-    buy_close_ydayfirst('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-
-    # 用指定价 3750 元 2 手买入平仓,平昨优先
-    buy_close_ydayfirst('IF1805.IF', 2, 'fix', 3750, ContextInfo, '110476')
-

sell_open-期货卖出开仓

用法: sell_open(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货卖出开仓

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价 1 手卖出开仓
-    sell_open('IF1805.IF', 1, ContextInfo, '110476')
-    
-    # 用对手价 1 手卖出开仓   
-    sell_open('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-
-    # 用指定价 3750 元 2 手卖出开仓
-    sell_open('IF1805.IF', 2, 'fix',3750, ContextInfo, '110476')
-

sell_close_tdayfirst-期货卖出平仓(平今优先)

用法: sell_close_tdayfirst(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货卖出平仓,平今优先

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo):
-    # 按最新价下 1 手卖出平仓,平今优先
-    sell_close_tdayfirst('IF1805.IF', 1, ContextInfo, '110476')
-    
-    # 用对手价 1 手卖出平仓,平今优先
-    sell_close_tdayfirst('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-    
-    # 用指定价 3750 元 2 手卖出平仓,平今优先
-    sell_close_tdayfirst('IF1805.IF', 1, 'fix', 3750, ContextInfo, '110476')
-

sell_close_ydayfirst-期货卖出平仓(平昨优先)

用法: sell_close_ydayfirst(stockcode, amount[, style, price], ContextInfo[, accId])

释义: 期货卖出平仓,平昨优先

参数:

  • stockcode:代码,string,如 'IF1805.IF'

  • amount:手数,int

  • style:下单选价类型,string,默认为最新价 'LATEST',可选值:

    'LATEST':最新

    'FIX':指定

    'HANG':挂单

    'COMPETE':对手

    'MARKET':市价

    'SALE1':卖一价

    'BUY1':买一价

  • price:价格,double

  • ContextInfo:PythonObj,Python 对象,这里必须是 ContextInfo

  • accId:账号,string

返回:

示例:

def handlebar(ContextInfo): 
-    # 按最新价 1 手卖出平仓,平昨优先 
-    sell_close_ydayfirst('IF1805.IF', 1, ContextInfo, '110476')
-
-    # 用对手价 1 手卖出平仓,平昨优先   
-    sell_close_ydayfirst('IF1805.IF', 1, 'COMPETE', ContextInfo, '110476')
-
-    # 用指定价 3750 元 2 手卖出平仓,平昨优先
-    sell_close_ydayfirst('IF1805.IF', 2, 'fix', 3750, ContextInfo, '110476')
-

[已弃用] get_debt_contract-获取两融负债合约明细

用法: get_debt_contract(accId)

释义: 获取信用账户负债合约明细

此接口已弃用,替代接口为get_unclosed_compacts(获取未了结负债)和get_closed_compacts(获取已了结负债)

参数:

  • accId:信用账户

返回: list,list 中放的是 PythonObj,通过 dir(pythonobj) 可返回某个对象的属性列表。

示例:

def handlebar(ContextInfo):
-    obj_list = get_debt_contract('6000000248')
-    for obj in obj_list:
-        # 输出负债合约名
-        print(obj.m_strInstrumentName)
-

get_hkt_exchange_rate-获取沪深港通汇率数据

用法: get_hkt_exchange_rate(accountID,accountType)

释义: 获取沪深港通汇率数据

参数:

  • accountID:string,账号;
  • accountType:string,账号类型,必须填HUGANGTONG或者SHENGANGTONG

返回:

dict,字段释义:

bidReferenceRate:买入参考汇率

askReferenceRate:卖出参考汇率

dayBuyRiseRate:日间买入参考汇率浮动比例

daySaleRiseRate:日间卖出参考汇率浮动比例

示例:

def init(ContextInfo):
-      data=get_hkt_exchange_rate('6000000248','HUGANGTONG')
-      print(data)
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_user_attention.html b/reference/thinktrader_docs/innerApi_user_attention.html deleted file mode 100644 index c057055..0000000 --- a/reference/thinktrader_docs/innerApi_user_attention.html +++ /dev/null @@ -1,162 +0,0 @@ - - - - - - - - - 使用须知 | 迅投知识库 - - - - -

安装路径的选择

在安装 QMT 软件时,请不要安装在C盘,以避免因权限问题导致的使用问题

若是只能安装到C盘,请在启动时选择以管理员权限启动

下载python库

初次使用 QMT 时,请确保补全所需的 Python 库。安装完毕后,不要忘记重启客户端。

提示

在盘中,下载速度会很慢,建议盘前或盘后更新。

下载python库

关于ContextInfo

由于底层机制的限制,ContextInfo中存储的变量值将会回滚,即在对ContextInfo中的变量进行修改之后,在下一次handlebar调用时,这些修改将不会保留。具体细节请参阅常见问题在新窗口打开。因此,在完全理解ContextInfo机制之前,请避免在其中存储任何变量。

推荐用法

class G(): pass
-
-g = G()
-
-def init(ContextInfo):
-    g.stock_list = ['000001.SZ']
-
-def handlebar(ContextInfo):
-    g.stock_list.append('600000.SH')
-
-

错误用法

警告

下面的示例请勿使用

def init(ContextInfo):
-    ContextInfo.stock_list = ['000001.SZ']
-
-def handlebar(ContextInfo):
-    ContextInfo.stock_list.append('600000.SH')
-
-

关于线程和进程

QMT中,python无法使用多线程和多进程,而且所有策略都在同一线程中执行,所以策略中应该尽量避免阻塞类的写法,否则会影响其他策略的执行。

主图解析

如下图所示,策略执行依赖于K线图。这里所说的主图即是K线图,策略正是在K线图上运行,也是由它驱动的(也有非K线驱动的策略写法,详见快速入门)。

K线回放:策略在客户端运行时会从第一根K线开始,依次调用handlebar函数,直至最后一根K线。并且在盘中,每一个新的行情快照都会触发一次handlebar函数调用(无论主图的周期如何)。如果想要过滤掉某些K线,可以设置右侧的快速计算,或使用ContextInfo. is_last_bar ()函数进行过滤。

Alt text

策略运行无反应/运行报错提示 "run script failed! "

最快解决方法是点击右上角布局按钮,选择恢复默认布局

如果策略运行后无任何反应,首先检查客户端是否有其他策略正在运行,如果有,请先将其停止,然后重试。检查方法如下图所示:

Alt text

Alt text

Alt text

Alt text

提示

最后建议重启客户端

数据下载

QMT提供了许多接口来依赖数据下载功能。客户端的数据下载功能如下图所示:

Alt text 而且,在批量下载中可以设置定时下载,这样可以方便地每天自动下载当日的行情数据。 Alt text

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/innerApi_variable_convention.html b/reference/thinktrader_docs/innerApi_variable_convention.html deleted file mode 100644 index 1ced42a..0000000 --- a/reference/thinktrader_docs/innerApi_variable_convention.html +++ /dev/null @@ -1,258 +0,0 @@ - - - - - - - - - 变量约定 | 迅投知识库 - - - - -

函数命名规则

  • 函数名以 get_ 开头的,表示数据来源于客户端内存
  • 函数名以 query_ 开头的,表示数据是向服务查询

账号类型说明

  • 'FUTURE' - 期货账号
  • 'STOCK' - 股票账号
  • 'CREDIT' - 信用账号
  • 'FUTURE_OPTION' - 期货期权
  • 'STOCK_OPTION' - 股票期权
  • 'HUGANGTONG' - 沪港通
  • 'SHENGANGTONG' - 深港通

symbol_code - 代码表示

迅投代码(symbol_code)是迅投平台统一用于表示交易标的的代码 其格式为:交易标的代码.交易所代码,例如深圳证券交易所的平安银行,迅投代码为000001.SZ(不区分大小写)。代码表示可以在迅投研终端的行情列表或者按键精灵中查询。

迅投研终端示例

交易所代码

目前迅投研支持国内12个交易所,12个交易所的代码缩写如下:

交易所名称迅投简称显示后缀
上海证券交易所SHSH
深圳证券交易所SZSZ
北京证券交易所BJBJ
香港证券交易所HKHK
沪港通HGTHGT
深港通SGTSGT
中国金融期货交易所IFCFFEX
上海期货交易所SFSHFE
大连商品交易所DFDCE
郑州商品交易所ZFCZCE
上海国际能源交易中心INEINE
广州期货交易所GFGFEX

迅投研系统目前支持一站式获取全球多市场数据,详情链接:全球市场数据在新窗口打开

全球行情展示

交易标的代码

交易标的代码是指交易所给出的交易标的代码, 包括股票(如 600000), 期货(如 rb2011), 期权(如 10002498), 指数(如 000001), 基金(如 510300)等代码。

注意

对于期货合约代码来说,我们仅对market做了简化处理,symbol仍遵守交易所标准命名规则,且严格区分大小写,例如AP401.ZF不能写成ap401.ZF,rb2401.SF不能写成RB2401.SF

symbol示例

市场中文名市场代码示例代码显示后缀证券简称
上交所SH600000.SHSH浦发银行
深交所SZ000001.SZSZ平安银行
北交所BJ830779.BJBJ武汉蓝电
中金所IFIC2311.IFCFFEX中证 500 指数 2023 年 11 月期货合约
上期所SFrb2311.SFSHFE螺纹钢 2023 年 11 月期货合约
大商所DFm2311.DFDCE豆粕 2023 年 11 月期货合约
郑商所ZFFG305.ZFCZCE玻璃 2023 年 5 月期货合约
上海国际能源交易中心INEsc2311.INEINE原油 2023 年 11 月期货合约
广期所GFlc2405.GFGFEX碳酸锂 2024 年 05 月期货合约
上证期权SHO10005334.SHOSH50ETF购12月2650
深证期权SZO90002114.SZOSZ深证100ETF沽12月2700
板块指数BKZS290001.BKZSBKZS工业品期货板块指数

期货主力连续合约

仅支持回测模式下交易,期货主力连续合约为量价数据的简单拼接,未做平滑处理,如rb00.SF螺纹钢主连合约,其他[主连合约代码请参考](期货数据 | 迅投知识库 (thinktrader.net))

期货加权连续合约

仅支持回测模式下交易,期货加权连续合约为迅投按照一定规则加权合成的连续合约,相比主力连续合约更加平滑,如rbJQ00.SF,其他[加权合约代码参考](期货数据 | 迅投知识库 (thinktrader.net))

mode - 模式选择

迅投研终端中,策略可以以四种模式运行,分别为调试运行模式回测模式,模拟信号模式,实盘交易模式,模式需要在运行策略时手动选择

调试运行模式

调试运行模式需要在策略编辑界面点击编辑栏上方的运行,该模式下策略会以实时行情进行运算,但迅投研终端不会记录交易信号迅投研终端示例

回测模式

回测模式需要在策略编辑界面点击编辑栏上方的回测,该模式下策略会以右侧栏设定的回测周期推进行情进行运算,回测模式下,发生的交易会被记录在回测结果页面 迅投研终端示例

模拟信号模式

模拟信号模式需要在策略交易界面,在左侧策略文件栏中选择要进行计算运行的策略,点击右侧圆形按钮选择模拟,点击三角形运行按钮后策略会以实时行情进行运算,该模式下调用的下单函数(passorder)不会产生实际交易,仅会记录交易信号在下方的策略信号栏中迅投研终端示例

实盘交易模式

实盘交易模式需要在策略交易界面,在左侧策略文件栏中选择要进行计算运行的策略,点击右侧圆形按钮选择实盘,点击三角形运行按钮后策略会以实时行情进行运算,该模式下调用的下单函数(passorder)会对账户实际下单,同时交易信号会记录在下方的策略信号栏中迅投研终端示例

ContextInfo - 上下文对象

ContextInfo.start/ContextInfo.end - 回测开始/结束时间

注意

一、此属性只在回测模式生效;

二、仅在init中设置生效,应在init中设置完毕;

三、缺省值为策略编辑界面设定的回测时间范围;

四、回测起止时间也可在策略编辑器的回测参数面板中设置,若两处同时设置,则以代码中设置的值为准;

五、结束时间小于等于开始时间则计算范围为空。

释义

可通过此属性设定回测开始/结束的时间,以%Y-%m-%d %H:%M:%S格式传入

原型

ContextInfo.start # 回测开始时间属性
-ContextInfo.end # 回测结束时间属性
-

返回值none

示例

# coding:gbk
-def init(ContextInfo):
-	ContextInfo.start = "2017-01-01 00:00:00"# 回测开始时间为 2017-01-01
-	ContextInfo.end = "2020-01-01 00:00:00"# 回测结束时间为 2020-01-01
-def handlebar(ContextInfo):
-	# 打印输出当前回测时间
-	print(timetag_to_datetime(ContextInfo.get_bar_timetag(ContextInfo.barpos), "%Y-%m-%d %H%M%S"))
-

ContextInfo.capital - 设定回测初始资金

注意

此函数只支持回测模式。回测初始资金也可在策略编辑器的回测参数面板中设置,若两处同时设置,则以代码中设置的值为准。

释义 设定回测初始资金,支持读写,默认为 1000000

原型

ContextInfo.capital = 10000000 # 设定ContextInfo.capital 值为10000000
-

返回值float类型的数值,代表当前策略设定的回测金额

示例

# coding:gbk
-def init(ContextInfo):
-    ContextInfo.capital = 10000000
-def handlebar(ContextInfo):
-    print(ContextInfo.capital)
-

ContextInfo.period - 获取当前周期

释义 获取当前周期,即基本信息中设置的默认周期,只读

原型

ContextInfo.period
-

返回string,返回值含义:

含义
'1d'日线
'1m'1分钟线
'3m'3分钟线
'5m'5分钟线
'15m'15分钟线
'30m'30分钟线
'1h'小时线
'1w'周线
'1mon'月线
'1q'季线
'1hy'半年线
'1y'年线

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.period)
-

ContextInfo.barpos - 获取当前运行到 K 线索引号

释义

获取主图当前运行到的 K 线索引号,只读,索引号从0开始

原型

ContextInfo.barpos
-

返回值int类型值,代表着当前K线的索引号

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.barpos)
-

ContextInfo.time_tick_size - 获取当前图 K 线数目

释义

获取当前图 K 线bar的数量,只读

原型

ContextInfo.time_tick_size
-

返回值int

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.time_tick_size)
-

ContextInfo.stockcode - 获取当前图代码

释义

获取当前主图代码,只读

原型

ContextInfo.stockcode
-

返回值string:对应主图代码

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.stockcode)
-

ContextInfo.market - 获取当前主图市场

释义

获取当前主图市场,只读

原型

ContextInfo.market
-

返回值string:对应主图市场

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.market)
-

ContextInfo.dividend_type - 获取当前主图复权处理方式

释义

获取当前主图复权处理方式

原型

ContextInfo.dividend_type
-

返回值string,返回值含义:

含义
'none'不复权
'front'向前复权
'back'向后复权
'front_ratio'等比向前复权
'back_ratio'等比向后复权

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.dividend_type)
-

ContextInfo.benchmark - 获取回测基准标的

注意

该属性只在回测模式可用

释义 获取回测基准的代码,只读

原型

ContextInfo.benchmark
-

返回值string

示例

# coding:gbk
-def init(ContextInfo):
-    pass
-def handlebar(ContextInfo):
-    print(ContextInfo.benchmark)
-

ContextInfo.do_back_test - 表示当前是否为回测模式

释义

表示当前是否为回测模式,只读,默认值为 False

原型

ContextInfo.do_back_test
-

返回值bool

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/nativeApi_code_examples.html b/reference/thinktrader_docs/nativeApi_code_examples.html deleted file mode 100644 index f06f574..0000000 --- a/reference/thinktrader_docs/nativeApi_code_examples.html +++ /dev/null @@ -1,1745 +0,0 @@ - - - - - - - - - 完整实例 | 迅投知识库 - - - - -

行情示例

获取行情示例

# 用前须知
-
-## xtdata提供和MiniQmt的交互接口,本质是和MiniQmt建立连接,由MiniQmt处理行情数据请求,再把结果回传返回到python层。使用的行情服务器以及能获取到的行情数据和MiniQmt是一致的,要检查数据或者切换连接时直接操作MiniQmt即可。
-
-## 对于数据获取接口,使用时需要先确保MiniQmt已有所需要的数据,如果不足可以通过补充数据接口补充,再调用数据获取接口获取。
-
-## 对于订阅接口,直接设置数据回调,数据到来时会由回调返回。订阅接收到的数据一般会保存下来,同种数据不需要再单独补充。
-
-# 代码讲解
-
-# 从本地python导入xtquant库,如果出现报错则说明安装失败
-from xtquant import xtdata
-import time
-
-# 设定一个标的列表
-code_list = ["000001.SZ"]
-# 设定获取数据的周期
-period = "1d"
-
-# 下载标的行情数据
-if 1:
-    ## 为了方便用户进行数据管理,xtquant的大部分历史数据都是以压缩形式存储在本地的
-    ## 比如行情数据,需要通过download_history_data下载,财务数据需要通过
-    ## 所以在取历史数据之前,我们需要调用数据下载接口,将数据下载到本地
-    for i in code_list:
-        xtdata.download_history_data(i,period=period,incrementally=True) # 增量下载行情数据(开高低收,等等)到本地
-    
-    xtdata.download_financial_data(code_list) # 下载财务数据到本地
-    xtdata.download_sector_data() # 下载板块数据到本地
-    # 更多数据的下载方式可以通过数据字典查询
-
-# 读取本地历史行情数据
-history_data = xtdata.get_market_data_ex([],code_list,period=period,count=-1)
-print(history_data)
-print("=" * 20)
-
-# 如果需要盘中的实时行情,需要向服务器进行订阅后才能获取
-# 订阅后,get_market_data函数于get_market_data_ex函数将会自动拼接本地历史行情与服务器实时行情
-
-# 向服务器订阅数据
-for i in code_list:
-    xtdata.subscribe_quote(i,period=period,count=-1) # 设置count = -1来取到当天所有实时行情
-
-# 等待订阅完成
-time.sleep(1)
-
-# 获取订阅后的行情
-kline_data = xtdata.get_market_data_ex([],code_list,period=period)
-print(kline_data)
-
-# 获取订阅后的行情,并以固定间隔进行刷新,预期会循环打印10次
-for i in range(10):
-    # 这边做演示,就用for来循环了,实际使用中可以用while True
-    kline_data = xtdata.get_market_data_ex([],code_list,period=period)
-    print(kline_data)
-    time.sleep(3) # 三秒后再次获取行情
-
-# 如果不想用固定间隔触发,可以以用订阅后的回调来执行
-# 这种模式下当订阅的callback回调函数将会异步的执行,每当订阅的标的tick发生变化更新,callback回调函数就会被调用一次
-# 本地已有的数据不会触发callback
-    
-# 定义的回测函数
-    ## 回调函数中,data是本次触发回调的数据,只有一条
-def f(data):
-    # print(data)
-    
-    code_list = list(data.keys())    # 获取到本次触发的标的代码
-
-    kline_in_callabck = xtdata.get_market_data_ex([],code_list,period = period)    # 在回调中获取klines数据
-    print(kline_in_callabck)
-
-for i in code_list:
-    xtdata.subscribe_quote(i,period=period,count=-1,callback=f) # 订阅时设定回调函数
-
-# 使用回调时,必须要同时使用xtdata.run()来阻塞程序,否则程序运行到最后一行就直接结束退出了。
-xtdata.run()
-
-
-
-

连接VIP服务器

# 导入 xtdatacenter 模块
-import sys
-
-print("Python 版本:", sys.version)
-
-
-import time
-import pandas as pd
-from xtquant import xtdatacenter as xtdc
-from xtquant import xtdata
-'''  
-设置用于登录行情服务的token,此接口应该先于 init_quote 调用
-
-token可以从投研用户中心获取
-https://xuntou.net/#/userInfo
-'''
-xtdc.set_token('这里输入token')
-
-'''
-设置连接池,使服务器只在连接池内优选
-
-建议将VIP服务器设为连接池
-'''
-addr_list = [
-    '115.231.218.73:55310', 
-    '115.231.218.79:55310', 
-    '42.228.16.211:55300',
-    '42.228.16.210:55300',
-    '36.99.48.20:55300',
-    '36.99.48.21:55300'
-    ]
-xtdc.set_allow_optmize_address(addr_list)
-
-xtdc.set_kline_mirror_enabled(True) # 开启K线全推功能(vip),以获取全市场实时K线数据
-
-
-"""
-初始化
-"""
-xtdc.init()
-## 监听端口
-port = xtdc.listen(port = 58621) # 指定固定端口进行连接
-# port = xtdc.listen(port = (58620, 58630))[1] 通过指定port范围,可以让xtdc在范围内自动寻找可用端口
-
-xtdata.connect(port=port)
-
-print('-----连接上了------')
-print(xtdata.data_dir)
-
-
-
-servers = xtdata.get_quote_server_status()
-# print(servers)
-for k, v in servers.items():
-    print(k, v)
-
-xtdata.run()
-
-

连接指定服务器


-import time
-from xtquant import xtdata
-
-#用token方式连接,不需要账号密码
-#其他连接方式,需要账号密码
-info = {"ip": '115.231.218.73', "port": 55300, "username": '', "pwd": ''}
-
-connect_success = 0
-def func(d):
-    ip = d.get('ip', '')
-    port = d.get('port')
-    status = d.get('status', 'disconnected')
-
-    global connect_success
-    if ip == info['ip'] and port == info['port']:
-        if status == 'connected':
-            connect_success = 1
-        else:
-            connect_success = 2
-
-# 注册连接回调信息
-xtdata.watch_quote_server_status(func)
-
-# 行情连接
-qs = xtdata.QuoteServer(info)
-qs.connect()
-
-# 获取当前数据连接站点
-data_server_info = xtdata.get_quote_server_status()
-# 显示当前数据连接站点
-if 1:
-    for k,v in data_server_info.items():
-        print(f"data:{k}, connect info:{v.info}")
-
-
-# 等待连接状态
-while connect_success == 0:
-    time.sleep(0.3)
-
-if connect_success == 2:
-    print("连接失败")
-
-

指定初始化行情连接范围

if 1:
-    from xtquant import xtdatacenter as xtdc
-
-    ## 设置数据目录
-    xtdc.set_data_home_dir('data')
-
-    ## 设置token
-    token = "你的token"
-    xtdc.set_token(token)
-
-    ## 限定行情站点的优选范围
-    opt_list = [
-        '115.231.218.73:55310',
-        '115.231.218.79:55310',
-        '42.228.16.210:55300',
-        '42.228.16.211:55300',
-        '36.99.48.20:55300',
-        '36.99.48.21:55300',
-    ]
-    xtdc.set_allow_optmize_address(opt_list)
-
-    ## 开启指定市场的K线全推
-    xtdc.set_kline_mirror_markets(['SH', 'SZ', 'BJ'])
-
-    ## 设置要初始化的市场列表
-    init_markets = [
-        'SH', 'SZ', 'BJ',
-        #'DF', 'GF', 'IF', 'SF', 'ZF', 'INE',
-        #'SHO', 'SZO',
-    ]
-    xtdc.set_init_markets(init_markets)
-
-    ## 初始化xtdc模块
-    xtdc.init(start_local_service = False)
-
-    ## 监听端口
-    #xtdc.listen(port = 58620)
-    listen_port = xtdc.listen(port = (58620, 58650))
-
-    #import code; code.interact(local = locals())
-
-
-import xtquant.xtdata as xtdata
-
-xtdata.connect(port = listen_port)
-
-
-
-import code; code.interact(local = locals())
-
-
-
-
-

订阅全推数据/下载历史数据


-# coding:utf-8
-import time
-
-from xtquant import xtdata
-
-code = '600000.SH'
-
-#取全推数据
-full_tick = xtdata.get_full_tick([code])
-print('全推数据 日线最新值', full_tick)
-
-#下载历史数据 下载接口本身不返回数据
-xtdata.download_history_data(code, period='1m', start_time='20230701')
-
-#订阅最新行情
-def callback_func(data):
-    print('回调触发', data)
-
-xtdata.subscribe_quote(code, period='1m', count=-1, callback= callback_func)
-data = xtdata.get_market_data(['close'], [code], period='1m', start_time='20230701')
-print('一次性取数据', data)
-
-#死循环 阻塞主线程退出
-xtdata.run()
-
-

获取对手价

# 以卖出为例
-
-import pandas as pd
-import numpy as np
-from xtquant import xtdata
-
-to_do_trade_list = ["000001.SZ"]
-tick = xtdata.get_full_tick(to_do_trade_list)
-
-
-# 取买一价为对手价,若买一价为0,说明已经跌停,则取最新价
-for i in tick:
-    fix_price = tick[i]["bidPrice"][0] if tick[i]["bidPrice"][0] != 0 else tick[i]["lastPrice"]
-    print(fix_price)
-

复权计算方式

#coding:utf-8
-
-import numpy as np
-import pandas as pd
-
-from xtquant import xtdata
-
-#def gen_divid_ratio(quote_datas, divid_datas):
-#    drl = []
-#    for qi in range(len(quote_datas)):
-#        q = quote_datas.iloc[qi]
-#        dr = 1.0
-#        for di in range(len(divid_datas)):
-#            d = divid_datas.iloc[di]
-#            if d.name <= q.name:
-#                dr *= d['dr']
-#        drl.append(dr)
-#    return pd.DataFrame(drl, index = quote_datas.index, columns = quote_datas.columns)
-
-def gen_divid_ratio(quote_datas, divid_datas):
-    drl = []
-    dr = 1.0
-    qi = 0
-    qdl = len(quote_datas)
-    di = 0
-    ddl = len(divid_datas)
-    while qi < qdl and di < ddl:
-        qd = quote_datas.iloc[qi]
-        dd = divid_datas.iloc[di]
-        if qd.name >= dd.name:
-            dr *= dd['dr']
-            di += 1
-        if qd.name <= dd.name:
-            drl.append(dr)
-            qi += 1
-    while qi < qdl:
-        drl.append(dr)
-        qi += 1
-    return pd.DataFrame(drl, index = quote_datas.index, columns = quote_datas.columns)
-
-def process_forward_ratio(quote_datas, divid_datas):
-    drl = gen_divid_ratio(quote_datas, divid_datas)
-    drlf = drl / drl.iloc[-1]
-    result = (quote_datas * drlf).apply(lambda x: round(x, 2))
-    return result
-
-def process_backward_ratio(quote_datas, divid_datas):
-    drl = gen_divid_ratio(quote_datas, divid_datas)
-    result = (quote_datas * drl).apply(lambda x: round(x, 2))
-    return result
-
-def process_forward(quote_datas1, divid_datas):
-    quote_datas = quote_datas1.copy()
-    def calc_front(v, d):
-        return ((v - d['interest'] + d['allotPrice'] * d['allotNum'])
-            / (1 + d['allotNum'] + d['stockBonus'] + d['stockGift']))
-    for qi in range(len(quote_datas)):
-        q = quote_datas.iloc[qi]
-        for di in range(len(divid_datas)):
-            d = divid_datas.iloc[di]
-            if d.name <= q.name:
-                continue
-            q.iloc[0] = calc_front(q.iloc[0], d)
-    return quote_datas
-
-def process_backward(quote_datas1, divid_datas):
-    quote_datas = quote_datas1.copy()
-    def calc_back(v, d):
-        return ((v * (1.0 + d['stockGift'] + d['stockBonus'] + d['allotNum'])
-            + d['interest'] - d['allotNum'] * d['allotPrice']))
-    for qi in range(len(quote_datas)):
-        q = quote_datas.iloc[qi]
-        for di in range(len(divid_datas) - 1, -1, -1):
-            d = divid_datas.iloc[di]
-            if d.name > q.name:
-                continue
-            q.iloc[0] = calc_back(q.iloc[0], d)
-    return quote_datas
-
-
-#--------------------------------
-
-s = '002594.SZ'
-
-#xtdata.download_history_data(s, '1d', '20100101', '')
-
-dd = xtdata.get_divid_factors(s)
-print(dd)
-
-#复权计算用于处理价格字段
-field_list = ['open', 'high', 'low', 'close']
-datas_ori = xtdata.get_market_data(field_list, [s], '1d', dividend_type = 'none')['close'].T
-#print(datas_ori)
-
-#等比前复权
-datas_forward_ratio = process_forward_ratio(datas_ori, dd)
-print('datas_forward_ratio', datas_forward_ratio)
-
-#等比后复权
-datas_backward_ratio = process_backward_ratio(datas_ori, dd)
-print('datas_backward_ratio', datas_backward_ratio)
-
-#前复权
-datas_forward = process_forward(datas_ori, dd)
-print('datas_forward', datas_forward)
-
-#后复权
-datas_backward = process_backward(datas_ori, dd)
-print('datas_backward', datas_backward)
-
-

根据商品期货期权代码获取对应的商品期货合约代码

from xtquant import xtdata
-
-def get_option_underline_code(code:str) -> str:
-    """
-    注意:该函数不适用于股指期货期权与ETF期权
-    Todo: 根据商品期权代码获取对应的具体商品期货合约
-    Args:
-        code:str 期权代码
-    Return:
-        对应的期货合约代码
-    """
-    Exchange_dict = {
-        "SHFE":"SF",
-        "CZCE":"ZF",
-        "DCE":"DF",
-        "INE":"INE",
-        "GFEX":"GF"
-    }
-    
-    if code.split(".")[-1] not in [v for k,v in Exchange_dict.items()]:
-        raise KeyError("此函数不支持该交易所合约")
-    info = xtdata.get_option_detail_data(code)
-    underline_code = info["OptUndlCode"] + "." + Exchange_dict[info["OptUndlMarket"]]
-
-    return underline_code
-
-if __name__ == "__main__":
-
-    symbol_code = get_option_underline_code('sc2403C465.INE') # 获取期权合约'sc2403C465.INE'对应的期货合约代码
-    print(symbol_code)
-
-

根据指数代码,返回对应的期货合约


-from xtquant import xtdata
-import re
-
-def get_financial_futures_code_from_index(index_code:str) -> list:
-    """
-    ToDo:传入指数代码,返回对应的期货合约(当前)
-    Args:
-        index_code:指数代码,如"000300.SH","000905.SH"
-    Retuen:
-        list: 对应期货合约列表
-    """
-    financial_futures = xtdata.get_stock_list_in_sector("中金所")
-    future_list = []
-    pattern = r'^[a-zA-Z]{1,2}\d{3,4}\.[A-Z]{2}$'
-    for i in financial_futures:
-        
-        if re.match(pattern,i):
-            future_list.append(i)
-    ls = []
-    for i in future_list:
-        _info = xtdata._get_instrument_detail(i)
-        _index_code = _info["ExtendInfo"]['OptUndlCode'] + "." + _info["ExtendInfo"]['OptUndlMarket']
-        if _index_code == index_code:
-            ls.append(i)
-    return ls
-
-if __name__ == "__main__":
-    ls = get_financial_futures_code_from_index("000905.SH")
-    print(ls)
-
-

高频因子数据创建

#coding:utf-8
-
-
-import xtquant.invadv as xtia
-
-
-remote_host = '115.231.218.7'
-remote_port = 55300
-user_name = '授权账号'
-password = '授权账号对应密码'
-
-# 连接云服务
-api = xtia.InvAdv()
-api.set_remote_addr(remote_host, remote_port)
-api.set_user(user_name, password)
-api.connect()
-
-# 查询高频因子数据列表
-ret_sector_dict = api.get_block_list()
-new_dict = {v: k for k, v in ret_sector_dict.items()}
-
-# 创建新的因子
-fp_name = '盘口价差'
-if fp_name not in new_dict:
-    api.create_block(fp_name)
-    print(f'创建{fp_name}表')
-
-# 格式 {股票1: 因子值, 股票2: 因子值 ...}
-codes = {'002594.SZ': 0.009, '300750.SZ': 0.007, '688001.SH': 0.1, '000001.SZ':0.2, '300751.SZ':0.3}
-
-# 查询高频因子数据列表
-ret_sector_dict = api.get_block_list()
-print(f'查询高频因子数据列表:{ret_sector_dict}')
-
-
-# 创建高频因子内容
-for k_msg_id, v in ret_sector_dict.items():
-    write_codes = []
-    if v == fp_name:
-        for code, value in codes.items():
-            write_codes.append(f'{code}|{value}')
-        # 创建代码
-        api.push_block(k_msg_id, write_codes)
-        print(f'表:{fp_name} id:{k_msg_id} {write_codes}')
-        print(f'创建结束!')
-
-
-print('====end====')
-
-
-

交易示例

简单买卖各一笔示例

需要调整的参数:

  • 98行的path变量需要改为本地客户端路径,券商端指定到 f"{安装目录}\userdata_mini",投研端指定到f"{安装目录}\userdata"
  • 107行的资金账号需要调整为自身资金账号
# coding:utf-8
-import time, datetime, traceback, sys
-from xtquant import xtdata
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-
-
-# 定义一个类 创建类的实例 作为状态的容器
-class _a():
-    pass
-
-
-A = _a()
-A.bought_list = []
-A.hsa = xtdata.get_stock_list_in_sector('沪深A股')
-
-
-def interact():
-    """执行后进入repl模式"""
-    import code
-    code.InteractiveConsole(locals=globals()).interact()
-
-
-xtdata.download_sector_data()
-
-
-
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    def on_disconnected(self):
-        """
-        连接断开
-        :return:
-        """
-        print(datetime.datetime.now(), '连接断开回调')
-
-    def on_stock_order(self, order):
-        """
-        委托回报推送
-        :param order: XtOrder对象
-        :return:
-        """
-        print(datetime.datetime.now(), '委托回调 投资备注', order.order_remark)
-
-    def on_stock_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print(datetime.datetime.now(), '成交回调', trade.order_remark, f"委托方向(48买 49卖) {trade.offset_flag} 成交价格 {trade.traded_price} 成交数量 {trade.traded_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)
-        print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
-
-    def on_cancel_error(self, cancel_error):
-        """
-        撤单失败推送
-        :param cancel_error: XtCancelError 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_order_stock_async_response(self, response):
-        """
-        异步下单回报推送
-        :param response: XtOrderResponse 对象
-        :return:
-        """
-        print(f"异步委托回调 投资备注: {response.order_remark}")
-
-    def on_cancel_order_stock_async_response(self, response):
-        """
-        :param response: XtCancelOrderResponse 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_account_status(self, status):
-        """
-        :param response: XtAccountStatus 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-
-if __name__ == '__main__':
-    print("start")
-    # 指定客户端所在路径, 券商端指定到 userdata_mini文件夹
-    # 注意:如果是连接投研端进行交易,文件目录需要指定到f"{安装目录}\userdata"
-    path = r'D:\qmt\投\迅投极速交易终端睿智融科版\userdata'
-    # 生成session id 整数类型 同时运行的策略不能重复
-    session_id = int(time.time())
-    xt_trader = XtQuantTrader(path, session_id)
-    # 开启主动请求接口的专用线程 开启后在on_stock_xxx回调函数里调用XtQuantTrader.query_xxx函数不会卡住回调线程,但是查询和推送的数据在时序上会变得不确定
-    # 详见: http://docs.thinktrader.net/vip/pages/ee0e9b/#开启主动请求接口的专用线程
-    # xt_trader.set_relaxed_response_order_enabled(True)
-
-    # 创建资金账号为 800068 的证券账号对象 股票账号为STOCK 信用CREDIT 期货FUTURE
-    acc = StockAccount('2000128', 'STOCK')
-    # 创建交易回调类对象,并声明接收回调
-    callback = MyXtQuantTraderCallback()
-    xt_trader.register_callback(callback)
-    # 启动交易线程
-    xt_trader.start()
-    # 建立交易连接,返回0表示连接成功
-    connect_result = xt_trader.connect()
-    print('建立交易连接,返回0表示连接成功', connect_result)
-    # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
-    subscribe_result = xt_trader.subscribe(acc)
-    print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
-    #取账号信息
-    account_info = xt_trader.query_stock_asset(acc)
-    #取可用资金
-    available_cash = account_info.m_dCash
-
-    print(acc.account_id, '可用资金', available_cash)
-    #查账号持仓
-    positions = xt_trader.query_stock_positions(acc)
-    #取各品种 总持仓 可用持仓
-    position_total_dict = {i.stock_code : i.m_nVolume for i in positions}
-    position_available_dict = {i.stock_code : i.m_nCanUseVolume for i in positions}
-    print(acc.account_id, '持仓字典', position_total_dict)
-    print(acc.account_id, '可用持仓字典', position_available_dict)
-
-    #买入 浦发银行 最新价 两万元
-    stock = '600000.SH'
-    target_amount = 20000
-    full_tick = xtdata.get_full_tick([stock])
-    print(f"{stock} 全推行情: {full_tick}")
-    current_price = full_tick[stock]['lastPrice']
-    #买入金额 取目标金额 与 可用金额中较小的
-    buy_amount = min(target_amount, available_cash)
-    #买入数量 取整为100的整数倍
-    buy_vol = int(buy_amount / current_price / 100) * 100
-    print(f"当前可用资金 {available_cash} 目标买入金额 {target_amount} 买入股数 {buy_vol}股")
-    async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_BUY, buy_vol, xtconstant.FIX_PRICE, current_price,
-                                            'strategy_name', stock)
-
-    #卖出 500股
-    stock = '513130.SH'
-    #目标数量
-    target_vol = 500
-    #可用数量
-    available_vol = position_available_dict[stock] if stock in position_available_dict else 0
-    #卖出量取目标量与可用量中较小的
-    sell_vol = min(target_vol, available_vol)
-    print(f"{stock} 目标卖出量 {target_vol} 可用数量 {available_vol} 卖出 {sell_vol}股")
-    if sell_vol > 0:
-        async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_SELL, sell_vol, xtconstant.LATEST_PRICE,
-                                                -1,
-                                                'strategy_name', stock)
-    print(f"下单完成 等待回调")
-    # 阻塞主线程退出
-    xt_trader.run_forever()
-    # 如果使用vscode pycharm等本地编辑器 可以进入交互模式 方便调试 (把上一行的run_forever注释掉 否则不会执行到这里)
-    interact()
-
-
-

单股订阅实盘示例

需要调整的参数:

  • 113行的path变量需要改为本地客户端路径,券商端指定到 f"{安装目录}\userdata_mini",投研端指定到f"{安装目录}\userdata"
  • 122行的资金账号需要调整为自身资金账号
# coding:utf-8
-import time, datetime, traceback, sys
-from xtquant import xtdata
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-
-
-# 定义一个类 创建类的实例 作为状态的容器
-class _a():
-    pass
-
-
-A = _a()
-A.bought_list = []
-A.hsa = xtdata.get_stock_list_in_sector('沪深A股')
-
-
-def interact():
-    """执行后进入repl模式"""
-    import code
-    code.InteractiveConsole(locals=globals()).interact()
-
-
-xtdata.download_sector_data()
-
-
-def f(data):
-    print(data)
-    now = datetime.datetime.now()
-    for stock in data:
-        if stock not in A.hsa:
-            continue
-        cuurent_price = data[stock][0]['close']
-        pre_price = data[stock][0]['preClose']
-        ratio = cuurent_price / pre_price - 1 if pre_price > 0 else 0
-        if ratio > 0.09 and stock not in A.bought_list:
-            print(f"{now} 最新价 买入 {stock} 100股")
-            async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_BUY, 100, xtconstant.LATEST_PRICE, -1,
-                                                    'strategy_name', stock)
-            A.bought_list.append(stock)
-
-
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    def on_disconnected(self):
-        """
-        连接断开
-        :return:
-        """
-        print(datetime.datetime.now(), '连接断开回调')
-
-    def on_stock_order(self, order):
-        """
-        委托回报推送
-        :param order: XtOrder对象
-        :return:
-        """
-        print(datetime.datetime.now(), '委托回调', order.order_remark)
-
-    def on_stock_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print(datetime.datetime.now(), '成交回调', trade.order_remark)
-
-    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)
-        print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
-
-    def on_cancel_error(self, cancel_error):
-        """
-        撤单失败推送
-        :param cancel_error: XtCancelError 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_order_stock_async_response(self, response):
-        """
-        异步下单回报推送
-        :param response: XtOrderResponse 对象
-        :return:
-        """
-        print(f"异步委托回调 {response.order_remark}")
-
-    def on_cancel_order_stock_async_response(self, response):
-        """
-        :param response: XtCancelOrderResponse 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_account_status(self, status):
-        """
-        :param response: XtAccountStatus 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-
-if __name__ == '__main__':
-    print("start")
-    # 指定客户端所在路径, 券商端指定到 userdata_mini文件夹
-    # 注意:如果是连接投研端进行交易,文件目录需要指定到f"{安装目录}\userdata"
-    path = r'D:\qmt\投\迅投极速交易终端睿智融科版\userdata'
-    # 生成session id 整数类型 同时运行的策略不能重复
-    session_id = int(time.time())
-    xt_trader = XtQuantTrader(path, session_id)
-    # 开启主动请求接口的专用线程 开启后在on_stock_xxx回调函数里调用XtQuantTrader.query_xxx函数不会卡住回调线程,但是查询和推送的数据在时序上会变得不确定
-    # 详见: http://docs.thinktrader.net/vip/pages/ee0e9b/#开启主动请求接口的专用线程
-    # xt_trader.set_relaxed_response_order_enabled(True)
-
-    # 创建资金账号为 800068 的证券账号对象 股票账号为STOCK 信用CREDIT 期货FUTURE
-    acc = StockAccount('2000128', 'STOCK')
-    # 创建交易回调类对象,并声明接收回调
-    callback = MyXtQuantTraderCallback()
-    xt_trader.register_callback(callback)
-    # 启动交易线程
-    xt_trader.start()
-    # 建立交易连接,返回0表示连接成功
-    connect_result = xt_trader.connect()
-    print('建立交易连接,返回0表示连接成功', connect_result)
-    # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
-    subscribe_result = xt_trader.subscribe(acc)
-    print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
-
-    #订阅的品种列表
-    code_list = ['600000.SH', '000001.SZ']
-
-    for code in code_list:
-        xtdata.subscribe_quote(code, '1d', callback = f)
-
-    # 阻塞主线程退出
-    xt_trader.run_forever()
-    # 如果使用vscode pycharm等本地编辑器 可以进入交互模式 方便调试 (把上一行的run_forever注释掉 否则不会执行到这里)
-    interact()
-
-
-

全推订阅实盘示例

本示例用于展示如何订阅上海及深圳市场全推,对于沪深A股品种策略进行判断当前涨幅超过 9 个点的买入 200 股

需要调整的参数:

  • 111行的path变量需要改为本地客户端路径
  • 116行的资金账号需要调整为自身资金账号

注意

本策略只用于提供策略写法及参考,若您直接进行实盘下单,造成损失本网站不负担责任。

#coding:utf-8
-import time, datetime, traceback, sys
-from xtquant import xtdata
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-
-#定义一个类 创建类的实例 作为状态的容器
-class _a():
-    pass
-A = _a()
-A.bought_list = []
-A.hsa = xtdata.get_stock_list_in_sector('沪深A股')
-
-def interact():
-    """执行后进入repl模式"""
-    import code
-    code.InteractiveConsole(locals=globals()).interact()
-xtdata.download_sector_data()
-
-def f(data):
-    now = datetime.datetime.now()
-    for stock in data:
-        if stock not in A.hsa:
-            continue
-        cuurent_price = data[stock][0]['lastPrice']
-        pre_price = data[stock][0]['lastClose']
-        ratio = cuurent_price / pre_price - 1 if pre_price > 0 else 0
-        if ratio > 0.09 and stock not in A.bought_list:
-            print(f"{now} 最新价 买入 {stock} 200股")
-            async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_BUY, 200, xtconstant.LATEST_PRICE, -1, 'strategy_name', stock)
-            A.bought_list.append(stock)
-    
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    def on_disconnected(self):
-        """
-        连接断开
-        :return:
-        """
-        print(datetime.datetime.now(),'连接断开回调')
-
-    def on_stock_order(self, order):
-        """
-        委托回报推送
-        :param order: XtOrder对象
-        :return:
-        """
-        print(datetime.datetime.now(), '委托回调', order.order_remark)
-
-
-    def on_stock_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print(datetime.datetime.now(), '成交回调', trade.order_remark)
-
-
-    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)
-        print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
-
-    def on_cancel_error(self, cancel_error):
-        """
-        撤单失败推送
-        :param cancel_error: XtCancelError 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_order_stock_async_response(self, response):
-        """
-        异步下单回报推送
-        :param response: XtOrderResponse 对象
-        :return:
-        """
-        print(f"异步委托回调 {response.order_remark}")
-
-    def on_cancel_order_stock_async_response(self, response):
-        """
-        :param response: XtCancelOrderResponse 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_account_status(self, status):
-        """
-        :param response: XtAccountStatus 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-
-if __name__ == '__main__':
-    print("start")
-    #指定客户端所在路径,
-    # 注意:如果是连接投研端进行交易,文件目录需要指定到f"{安装目录}\userdata"
-    path = r'D:\qmt\sp3\迅投极速交易终端 睿智融科版\userdata_mini'
-    # 生成session id 整数类型 同时运行的策略不能重复
-    session_id = int(time.time())
-    xt_trader = XtQuantTrader(path, session_id)
-    # 开启主动请求接口的专用线程 开启后在on_stock_xxx回调函数里调用XtQuantTrader.query_xxx函数不会卡住回调线程,但是查询和推送的数据在时序上会变得不确定
-    # 详见: http://docs.thinktrader.net/vip/pages/ee0e9b/#开启主动请求接口的专用线程
-    # xt_trader.set_relaxed_response_order_enabled(True)
-
-    # 创建资金账号为 800068 的证券账号对象
-    acc = StockAccount('800068', 'STOCK')
-    # 创建交易回调类对象,并声明接收回调
-    callback = MyXtQuantTraderCallback()
-    xt_trader.register_callback(callback)
-    # 启动交易线程
-    xt_trader.start()
-    # 建立交易连接,返回0表示连接成功
-    connect_result = xt_trader.connect()
-    print('建立交易连接,返回0表示连接成功', connect_result)
-    # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
-    subscribe_result = xt_trader.subscribe(acc)
-    print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
-
-    #这一行是注册全推回调函数 包括下单判断 安全起见处于注释状态 确认理解效果后再放开
-    # xtdata.subscribe_whole_quote(["SH", "SZ"], callback=f)
-    # 阻塞主线程退出
-    xt_trader.run_forever()
-    # 如果使用vscode pycharm等本地编辑器 可以进入交互模式 方便调试 (把上一行的run_forever注释掉 否则不会执行到这里)
-    interact()
-

定时判断实盘示例

# coding:utf-8
-import time, datetime, traceback, sys
-from xtquant import xtdata
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-
-
-# 定义一个类 创建类的实例 作为状态的容器
-class _a():
-    pass
-
-
-A = _a()
-A.bought_list = []
-A.hsa = xtdata.get_stock_list_in_sector('沪深A股')
-
-
-def interact():
-    """执行后进入repl模式"""
-    import code
-    code.InteractiveConsole(locals=globals()).interact()
-
-
-xtdata.download_sector_data()
-
-
-def f(data):
-    now = datetime.datetime.now()
-    # print(data)
-    for stock in data:
-        if stock not in A.hsa:
-            continue
-        cuurent_price = data[stock].iloc[-1, 0]
-        pre_price = data[stock].iloc[-2, 0]
-        ratio = cuurent_price / pre_price - 1 if pre_price > 0 else 0
-        if ratio > 0.09 and stock not in A.bought_list:
-            print(f"{now} 最新价 买入 {stock} 100股")
-            async_seq = xt_trader.order_stock_async(acc, stock, xtconstant.STOCK_BUY, 100, xtconstant.LATEST_PRICE, -1,
-                                                    'strategy_name', stock)
-            A.bought_list.append(stock)
-
-
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    def on_disconnected(self):
-        """
-        连接断开
-        :return:
-        """
-        print(datetime.datetime.now(), '连接断开回调')
-
-    def on_stock_order(self, order):
-        """
-        委托回报推送
-        :param order: XtOrder对象
-        :return:
-        """
-        print(datetime.datetime.now(), '委托回调', order.order_remark)
-
-    def on_stock_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print(datetime.datetime.now(), '成交回调', trade.order_remark)
-
-    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)
-        print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
-
-    def on_cancel_error(self, cancel_error):
-        """
-        撤单失败推送
-        :param cancel_error: XtCancelError 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_order_stock_async_response(self, response):
-        """
-        异步下单回报推送
-        :param response: XtOrderResponse 对象
-        :return:
-        """
-        print(f"异步委托回调 {response.order_remark}")
-
-    def on_cancel_order_stock_async_response(self, response):
-        """
-        :param response: XtCancelOrderResponse 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-    def on_account_status(self, status):
-        """
-        :param response: XtAccountStatus 对象
-        :return:
-        """
-        print(datetime.datetime.now(), sys._getframe().f_code.co_name)
-
-
-if __name__ == '__main__':
-    print("start")
-    # 指定客户端所在路径, 券商端指定到 userdata_mini文件夹
-    # 注意:如果是连接投研端进行交易,文件目录需要指定到f"{安装目录}\userdata"
-    path = r'D:\qmt\投\迅投极速交易终端睿智融科版\userdata'
-    # 生成session id 整数类型 同时运行的策略不能重复
-    session_id = int(time.time())
-    xt_trader = XtQuantTrader(path, session_id)
-    # 开启主动请求接口的专用线程 开启后在on_stock_xxx回调函数里调用XtQuantTrader.query_xxx函数不会卡住回调线程,但是查询和推送的数据在时序上会变得不确定
-    # 详见: http://docs.thinktrader.net/vip/pages/ee0e9b/#开启主动请求接口的专用线程
-    # xt_trader.set_relaxed_response_order_enabled(True)
-
-    # 创建资金账号为 800068 的证券账号对象 股票账号为STOCK 信用CREDIT 期货FUTURE
-    acc = StockAccount('2000128', 'STOCK')
-    # 创建交易回调类对象,并声明接收回调
-    callback = MyXtQuantTraderCallback()
-    xt_trader.register_callback(callback)
-    # 启动交易线程
-    xt_trader.start()
-    # 建立交易连接,返回0表示连接成功
-    connect_result = xt_trader.connect()
-    print('建立交易连接,返回0表示连接成功', connect_result)
-    # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
-    subscribe_result = xt_trader.subscribe(acc)
-    print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
-
-    #订阅的品种列表
-    code_list = ['600000.SH', '000001.SZ']
-    #遍历品种 下载历史k线 订阅当日行情
-    for code in code_list:
-        xtdata.download_history_data(code, period='1d', start_time='20200101')
-        xtdata.subscribe_quote(code, '1d', callback = None)
-
-    while True:
-        now = datetime.datetime.now()
-        now_time = now.strftime('%H%M%S')
-        if not '093000' <= now_time < '150000':
-            print(f"{now} 非交易时间 循环退出")
-            break
-        #取k线数据
-        data = xtdata.get_market_data_ex(['close'], code_list, period= '1d', start_time= '20240101')
-        #判断交易
-        f(data)
-        #每次循环 睡眠三秒后继续
-        time.sleep(3)
-
-
-    # 阻塞主线程退出
-    xt_trader.run_forever()
-    # 如果使用vscode pycharm等本地编辑器 可以进入交互模式 方便调试 (把上一行的run_forever注释掉 否则不会执行到这里)
-    interact()
-
-

交易接口重连

该示例演示交易连接断开时重连的代码处理。

提示

  1. 该示例不是线程安全的,仅演示断开连接时应该怎么处理重连代码,实际使用时请注意避免潜在的问题
  2. 本策略只用于提供策略写法及参考,若您直接进行实盘下单,造成损失本网站不负担责任。

-#本文用一个均线策略演示交易连接断开时怎么处理交易接口重连
-# 策略本身不严谨,不能作为实盘策略或者参考策略,本策略仅是演示重连用法
-import time
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-from xtquant import xtdata
-
-
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    # 更多说明见 http://dict.thinktrader.net/nativeApi/xttrader.html?id=I3DJ97#%E5%A7%94%E6%89%98xtorder
-    def on_disconnected(self):
-        """
-        连接断开
-        :return:
-        """
-        print("connection lost, 交易接口断开,即将重连")
-        global xt_trader
-        xt_trader = None
-    
-    def on_stock_order(self, order):
-        print(f'委托回报: 股票代码:{order.stock_code} 账号:{order.account_id}, 订单编号:{order.order_id} 柜台合同编号:{order.order_sysid} \
-            委托状态:{order.order_status} 成交数量:{order.order_status} 委托数量:{order.order_volume} 已成数量:{order.traded_volume}')
-        
-    def on_stock_trade(self, trade):
-        print(f'成交回报: 股票代码:{trade.stock_code} 账号:{trade.account_id}, 订单编号:{trade.order_id} 柜台合同编号:{trade.order_sysid} \
-            成交编号:{trade.traded_id} 成交数量:{trade.traded_volume} 委托数量:{trade.direction} ')
-
-    def on_order_error(self, order_error):
-        print(f"报单失败: 订单编号:{order_error.order_id} 下单失败具体信息:{order_error.error_msg} 委托备注:{order_error.order_remark}")
-
-    def on_cancel_error(self, cancel_error):
-        print(f"撤单失败: 订单编号:{cancel_error.order_id} 失败具体信息:{cancel_error.error_msg} 市场:{cancel_error.market}")
-
-    def on_order_stock_async_response(self, response):
-        print(f"异步下单的请求序号:{response.seq}, 订单编号:{response.order_id} ")
-
-    def on_account_status(self, status):
-        print(f"账号状态发生变化, 账号:{status.account_id} 最新状态:{status.status}")
-
-def create_trader(xt_acc,path, session_id):
-    trader = XtQuantTrader(path, session_id,callback=MyXtQuantTraderCallback())
-    trader.start()
-    connect_result = trader.connect()
-    trader.subscribe(xt_acc)
-    return trader if connect_result == 0 else None
-
-
-def try_connect(xt_acc,path):
-    session_id_range = [i for i in range(100, 120)]
-
-    import random
-    random.shuffle(session_id_range)
-
-    # 遍历尝试session_id列表尝试连接
-    for session_id in session_id_range:
-        trader = create_trader(xt_acc,path, session_id)
-        if trader:
-            print('连接成功,session_id:{}', session_id)
-            return trader
-        else:
-            print('连接失败,session_id:{},继续尝试下一个id', session_id)
-            continue
-
-    print('所有id都尝试后仍失败,放弃连接')
-    return None
-
-
-def get_xttrader(xt_acc,path):
-    global xt_trader
-    if xt_trader is None:
-        xt_trader = try_connect(xt_acc,path)
-    return xt_trader
-
-
-if __name__ == "__main__":
-
-    # 注意实际连接XtQuantTrader时不要写类似while True 这种无限循环的尝试,因为每次连接都会用session_id创建一个对接文件,这样就会占满硬盘导致电脑运行异常
-    # 要控制session_id在有限的范围内尝试,这里提供10个session_id供重连尝试
-    # 当所有session_id都尝试后,程序会抛出异常。实际使用过程中当session_id用完时,可以增加邮件等通知方式提醒人工处理 
-
-    #指定客户端所在路径
-    path = 'E:\qmt\\userdata_mini'
-    xt_trader = None
-    xt_acc = StockAccount('2000204')
-    xt_trader = get_xttrader(xt_acc,path)
-    if not xt_trader:
-        raise Exception('交易接口连接失败')
-    print('交易接口连接成功, 策略开始')
-
-    stock = '513050.SH'
-    xtdata.subscribe_quote(stock, '5m','','',count=-1)
-    time.sleep(1)
-    order_record = []
-    while '093000'<=time.strftime('%H%M%S')<'150000':
-        time.sleep(3)
-        xt_trader = get_xttrader(xt_acc,path)
-        
-        price = xtdata.get_market_data_ex(['close'],[stock],period='5m',)[stock]
-        #计算均线
-        ma5 = price['close'].rolling(5).mean()
-        ma10 = price['close'].rolling(10).mean()
-
-        if ma5.iloc[-1]>ma5.iloc[-10]:
-            t = price.index[-1]
-            order_flag = (t, '')
-            if order_flag not in order_record: #防止重复下单
-                print(f'发起买入 {stock}  k线时间:{t}')
-                
-                # 用最新价买100股
-                xt_trader.order_stock_async(xt_acc, stock, xtconstant.STOCK_BUY,100,xtconstant.LATEST_PRICE,0)
-                order_record.append(order_flag)
-        elif ma5.iloc[-1]<ma5[-10]:
-            t = price.index[-1]
-            order_flag = (t, '')
-            if order_flag not in order_record: #防止重复下单
-                print(f'发起卖出 {stock} k线时间:{t}')
-                # 用最新价买100股
-                xt_trader.order_stock_async(xt_acc, stock, xtconstant.STOCK_SELL,100,xtconstant.LATEST_PRICE,0)
-                
-                order_record.append(order_flag)
-
-
-

指定session id范围连接交易

该示例演示指定session重试连接次数的代码处理。


-#coding:utf-8
-
-def connect(path, session):
-    from xtquant import xttrader
-
-    trader = xttrader.XtQuantTrader(path, session)
-    trader.start()
-
-    connect_result = trader.connect()
-    return trader if connect_result == 0 else None
-
-
-def try_connect_range():
-    # 随机 session_id 的待尝试列表
-    # 100以内的id保留
-    ids = [i for i in range(100, 200)]
-
-    import random
-    random.shuffle(ids)
-
-    # 要连接到的对接路径
-    path = r'userdata_mini'
-
-    # 遍历id列表尝试连接
-    for session_id in ids:
-        print(f'尝试id:{session_id}')
-        trader = connect(path, session_id)
-
-        if trader:
-            print('连接成功')
-            return trader
-        else:
-            print('连接失败,继续尝试下一个id')
-            continue
-
-    # 所有id都尝试后仍失败,放弃连接
-    raise Exception('XtQuantTrader 连接失败,请重试')
-
-
-try:
-    trader = try_connect_range()
-except Exception as e:
-    import traceback
-    print(e, traceback.format_exc())
-
-
-import time
-while True:
-    print('.', end = '')
-    time.sleep(2)
-
-
-
-
-

信用账号执行还款

本示例用于展示如何使用xtquant库对信用账号执行还款的操作

提示

本策略只用于提供策略写法及参考,若您直接进行实盘下单,造成损失本网站不负担责任。

#coding=utf-8
-from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-
-# 修改参数
-# path为mini qmt客户端安装目录下userdata_mini路径
-path = 'E:\\qmt\\userdata_mini'
-# session_id为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
-session_id = 1234567
-repay_money = 1000.51  # 元,需要执行还款的金额
-
-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_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")
-
-
-    xt_trader = XtQuantTrader(path, session_id)
-    # 创建资金账号为1000000365的证券账号对象
-    acc = StockAccount('200035', 'CREDIT')
-    # StockAccount可以用第二个参数指定账号类型,如沪港通传'HUGANGTONG',深港通传'SHENGANGTONG'
-    # acc = StockAccount('1000000365','STOCK')
-    # 创建交易回调类对象,并声明接收回调
-    callback = MyXtQuantTraderCallback()
-    xt_trader.register_callback(callback)
-    # 启动交易线程
-    xt_trader.start()
-    # 建立交易连接,返回0表示连接成功
-    connect_result = xt_trader.connect()
-    if connect_result != 0:
-        import sys
-        sys.exit('连接失败,程序即将退出 %d'%connect_result)
-    # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
-    subscribe_result = xt_trader.subscribe(acc)
-    if subscribe_result != 0:
-        print('账号订阅失败 %d'%subscribe_result)
-    print(subscribe_result)
-    stock_code = '600000.SH'  # 参数占位用,任意股票代码都可以
-    volume = 200  # 参数占位用,任意数量
-    # 使用指定价下单,接口返回订单编号,后续可以用于撤单操作以及查询委托状态
-    fix_result_order_id = xt_trader.order_stock(acc, stock_code, xtconstant.CREDIT_DIRECT_CASH_REPAY, repay_money, xtconstant.FIX_PRICE, -1, 'strategy_name', 'remark')
-
-    # 阻塞线程,接收交易推送
-    xt_trader.run_forever()
-

下单后通过回调撤单

import pandas as pd
-import numpy as np
-import datetime
-from xtquant import xtdata,xttrader
-from xtquant.xttype import StockAccount
-from xtquant import xtconstant
-from xtquant.xttrader import XtQuantTraderCallback
-import sys
-import time
-
-
-"""
-异步下单委托流程为
-1.order_stock_async发出委托
-2.回调on_order_stock_async_response收到回调信息
-3.回调on_stock_order收到委托信息
-4.回调cancel_order_stock_sysid_async发出异步撤单指令
-5.回调on_cancel_order_stock_async_response收到撤单回调信息
-6.回调on_stock_order收到委托信息
-"""
-strategy_name = "委托撤单测试"
-
-class MyXtQuantTraderCallback(XtQuantTraderCallback):
-    # 用于接收回调信息的类
-    def on_stock_order(self, order):
-        """
-        委托回报推送
-        :param order: XtOrder对象
-        :return:
-        """
-        # 属性赋值
-        account_type = order.account_type  # 账号类型
-        account_id = order.account_id  # 资金账号
-        stock_code = order.stock_code  # 证券代码,例如"600000.SH"
-        order_id = order.order_id  # 订单编号
-        order_sysid = order.order_sysid  # 柜台合同编号
-        order_time = order.order_time  # 报单时间
-        order_type = order.order_type  # 委托类型,参见数据字典
-        order_volume = order.order_volume  # 委托数量
-        price_type = order.price_type  # 报价类型,该字段在返回时为柜台返回类型,不等价于下单传入的price_type,枚举值不一样功能一样,参见数据字典
-        price = order.price  # 委托价格
-        traded_volume = order.traded_volume  # 成交数量
-        traded_price = order.traded_price  # 成交均价
-        order_status = order.order_status  # 委托状态,参见数据字典
-        status_msg = order.status_msg  # 委托状态描述,如废单原因
-        strategy_name = order.strategy_name  # 策略名称
-        order_remark = order.order_remark  # 委托备注
-        direction = order.direction  # 多空方向,股票不适用;参见数据字典
-        offset_flag = order.offset_flag  # 交易操作,用此字段区分股票买卖,期货开、平仓,期权买卖等;参见数据字典
-
-        # 打印输出
-        print(f"""
-        =============================
-                委托信息
-        =============================
-        账号类型: {order.account_type}, 
-        资金账号: {order.account_id},
-        证券代码: {order.stock_code},
-        订单编号: {order.order_id}, 
-        柜台合同编号: {order.order_sysid},
-        报单时间: {order.order_time},
-        委托类型: {order.order_type},
-        委托数量: {order.order_volume},
-        报价类型: {order.price_type},
-        委托价格: {order.price},
-        成交数量: {order.traded_volume},
-        成交均价: {order.traded_price},
-        委托状态: {order.order_status},
-        委托状态描述: {order.status_msg},
-        策略名称: {order.strategy_name},
-        委托备注: {order.order_remark},
-        多空方向: {order.direction},
-        交易操作: {order.offset_flag}
-        """)
-        if order.strategy_name == strategy_name:
-            # 该委托是由本策略发出
-            ssid = order.order_sysid
-            status = order.order_status
-            market = order.stock_code.split(".")[1]
-            # print(ssid)
-            if ssid and status in [50,55]:
-                ## 使用cancel_order_stock_sysid_async时,投研端market参数可以填写为0,券商端按实际情况填写
-                print(xt_trade.cancel_order_stock_sysid_async(account,0,ssid))
-
-    def on_stock_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print(datetime.datetime.now(), '成交回调', trade.order_remark,trade.stock_code,trade.traded_volume,trade.offset_flag)
-
-    def on_order_stock_async_response(self, response):
-        """
-        异步下单回报推送
-        :param response: XtOrderResponse 对象
-        :return:
-        """
-        
-        print(datetime.datetime.now(),'异步下单编号为:',response.seq)
-
-    def on_cancel_order_stock_async_response(self, response):
-        """
-        异步撤单回报
-        :param response: XtCancelOrderResponse 对象
-        :return:
-        """
-        account_type = response.account_type # 账号类型
-        account_id = response.account_id  # 资金账号
-        order_id = response.order_id  # 订单编号
-        order_sysid = response.order_sysid  # 柜台委托编号
-        cancel_result = response.cancel_result  # 撤单结果
-        seq = response.seq  # 异步撤单的请求序号
-
-        print(f"""
-            ===========================
-                   异步撤单回调信息
-            ===========================
-            账号类型: {response.account_type}, 
-            资金账号: {response.account_id},
-            订单编号: {response.order_id}, 
-            柜台委托编号: {response.order_sysid},
-            撤单结果: {response.cancel_result},
-            异步撤单的请求序号: {response.seq}""")
-        pass
-
-
-callback = MyXtQuantTraderCallback()
-# 填投研端的期货账号
-account = StockAccount("1000024",account_type = "FUTURE")
-# 填写投研端的股票账号
-# account = StockAccount("2000567")
-# 填投研端的userdata路径,miniqmt指定到userdata_mini
-xt_trade = xttrader.XtQuantTrader(r"C:\Program Files\测试1\迅投极速交易终端睿智融科版\userdata",int(time.time()))
-# 注册接受回调
-xt_trade.register_callback(callback) 
-# 启动交易线程
-xt_trade.start()
-# 链接交易
-connect_result = xt_trade.connect()
-# 订阅账号信息,接受这个账号的回调,回调是账号维度的
-subscribe_result = xt_trade.subscribe(account)
-print(subscribe_result)
-
-
-code = "rb2410.SF"
-# code = "000001.SZ"
-
-tick = xtdata.get_full_tick([code])[code]
-
-last_price = tick["lastPrice"] # 最新价
-
-ask_price = round(tick["askPrice"][0],3) # 卖方1档价
-bid_price = round(tick["bidPrice"][4],3) # 买方5档价
-
-symbol_info = xtdata.get_instrument_detail(code)
-
-up_limit = symbol_info["UpStopPrice"]
-down_limit = symbol_info["DownStopPrice"]
-
-lots = 1
-res_id = xt_trade.order_stock_async(account, code, xtconstant.FUTURE_OPEN_LONG, lots, xtconstant.FIX_PRICE, down_limit, strategy_name, "跌停价/固定手数")
-
-
-# lots = 100
-# res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, bid_price, strategy_name, "跌停价/固定手数")
-
-
-xtdata.run()
-
-
-
-
-

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/nativeApi_download_xtquant.html b/reference/thinktrader_docs/nativeApi_download_xtquant.html deleted file mode 100644 index 8b21e04..0000000 --- a/reference/thinktrader_docs/nativeApi_download_xtquant.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - xtquant版本下载 | 迅投知识库 - - - - -
更新日期版本下载更新说明
20251219xtquant_250807点击下载token模式,K线全推和全推数据加载模式调整
从xtdc.init()后立刻加载,调整为在第一次使用数据时加载

xttrader支持智能算法
获取智能算法参数配置信息接口get_smart_algo_param
智能算法下单接口smart_algo_order_async
智能算法任务查询接口query_smart_algo_task
智能算法任务撤销接口cancel_smart_algo_task_async

获取当前连接订阅的数据信息
xtdata.get_current_connect_sub_info(需要投研版本)

获取客户端所有订阅信息
xtdata.get_all_sub_info()(需要投研版本)

获取委托在千档队列中的排名
xtdata.get_order_rank() (需要投研版本)

token模式可以设置千档数据源模式
xtdc.set_thousand_source_mode()

token模式同datadir只允许启动一个xtdc进程

get_tabular_data参数不合法返回None

大单统计字段调整,查阅xtdata.md文档

vix市场的订阅和全推支持显示夜盘真实时间

BugFix: 修复期货0点前夜盘复权不生效的问题
20250516xtquant_250516点击下载
压缩包目录结构已调整到和之前一致,带有xtquant文件夹
支持python3.13版本

xttrader 支持银证转账
银行信息查询 xttrader.query_bank_info()
银行账户余额查询xttrader.query_bank_amount()
银证转账转入 xttrader.bank_transfer_in()
银证转账转出 xttrader.bank_transfer_out()
银行卡流水记录查询xttrader.query_bank_transfer_stream()

xttrader 支持期货和期权资金划转
权资金转期货 xttrader.ctp_transfer_option_to_future()
期货资金转期权 xttrader.ctp_transfer_future_to_option()

xttrader支持北交所
xtconstant 添加北交所市价报价方式说明

xttrader 交易数据字段调整
委托 XtOrder 新增
股东代码 secu_account
证券名称 instrument_name
成交 XtTrade 新增
股东代码 secu_account
证券名称 instrument_name
持仓 XtPosition 新增
股东代码 secu_account
证券名称 instrument_name
当前价 last_price
盈亏比例 profit_rate
浮动盈亏 float_profit
持仓盈亏 position_profit
开仓日期 open_date(只对期货可用)
账号资金 XtAsset 新增
可取余额 fetch_balance
当前余额 current_balance(当前余额 = 可用资金 + 冻结资金)

xtdata获取数据函数支持以datetime形式传入时间范围

合约信息添加交易日字段 TradingDay
xtdata.get_instrument_detail()

支持获取大单统计数据(需要vip权限)
xtdata.get_transactioncount()

支持获取带有分类信息的板块列表(需要投研版本)
xtdata.get_sector_info()

期权合约信息添加期权预估保证金 OptEstimatedMargin(需要投研版本)
xtdata.get_option_detail_data()

郑商所期货、期权品种提供标准化代码的字段(如 MA2504.ZF)(需要投研版本)

xtdata获取数据函数支持ATM市场
xtdata.get_market_data()
xtdata.get_market_data_ex()

BugFix: 订阅数据问题
20241017xtquant_241014点击下载xtdata.get_option_detail_data()期权多空方向类型判断调整

xtdata.get_trading_calendar() 自动下载所需的节假日数据

xtdata.get_instrument_detail() 字段ExpireDate类型由int调整为str

tick数据增加现手字段(tickvol),即当前tick累计成交量与上条数据的差值

新增函数xtdata.get_formula_result(),用于获取subscribe_formula()的模型结果

修复token模式下启用K线全推后单支订阅数据周期错误的问题
20240926xtquant_240920a有已知缺陷,暂不提供下载

在token模式下,启用K线全推后单支订阅数据周期错误
修复xtdata.subscribe_quote()返回订阅号为None的问题
20240923xtquant_240920有已知缺陷,暂不提供下载token模式,可以设置初始化的市场列表
xtdatacenter.set_init_markets()

函数xtdata.get_period_list() 结果结构调整

添加函数 xtdata.get_trading_contract_list()
获取当前主力合约可交易标的列表

添加用于获取交易时段的系列函数
xtdata.get_trading_period()
xtdata.get_all_trading_periods()
xtdata.get_all_kline_trading_periods()

添加函数 xtdata.subscribe_quote2()
支持复权方式参数
20240822xtquant_240812点击下载期货夜盘显示真实时间功能默认开启
未开启时, 周六凌晨的行情数据时间为周一
开启时,周六凌晨的行情数据时间为周六

新增板块:过期上交所、过期深交所
沪港通深港通板块不再显示历史标的

撤单接口的市场参数支持字符串格式,如SH、SF
xttrader.cancel_order_stock_sysid()和xttrader.cancel_order_stock_sysid_async()

添加函数xtdata.get_his_option_list_batch()和xtdata.get_his_option_list()
获取历史上某段时间的指定品种期权信息列表
依赖数据'optionhistorycontract'

期权函数支持商品期权品种 xtdata.get_option_undl_data()和xtdata.get_option_list()

郑商所期权标的代码调整为4位 xtdata.get_option_detail_data()

移除郑商所过滤重复tick的逻辑
20240617xtquant_240613点击下载支持python3.12版本

xtdata支持选择端口范围,在范围内自动连接

添加函数xtdata.get_full_kline() 批量获取当日K线数据(需要开启K线全推)

支持获取新闻公告数据
xtdata.get_market_data()系列函数 数据周期:announcement

支持获取涨跌停连板数据
xtdata.get_market_data()系列函数 数据周期:limitupperformance

支持获取港股通持股明细数据
xtdata.get_market_data()系列函数 数据周期:hktdetails,hktstatistics

支持获取外盘的行情数据(需购买相应服务)
行情订阅xtdata.subscribe_quote()和行情获取xtdata.get_market_data()系列函数,支持美股品种的获取

支持订阅vba模型(连接投研端)
xtdata.subscribe_formula()

token模式下初始化全推市场可选
xtdatacenter.set_wholequote_market_list()

token模式下行情连接优选机制调整
xtdatacenter.set_allow_optmize_address()会使用第一个地址作为全推连接

token模式下期货周末夜盘数据时间模式可选,可以选择展示为周一凌晨时间或真实的周六凌晨时间
xtdatacenter.set_future_realtime_mode()

20240329xtquant_240329点击下载郑商所期货品种支持使用4位年月代码(例如:CF2303.ZF)
xtdata.get_instrument_detail()支持使用4位年月代码获取
历史主力合约数据 新增4位年月代码字段

支持获取etf的iopv数据
分笔数据添加iopv字段(pe)
xtdata.get_market_data()系列函数 数据周期:etfiopv1m(分钟级) etfiopv1d(日级)

期权函数xtdata.get_option_detail_data() 新增标的品种代码字段 OptUndlCodeFull

新增板块 上证转债、沪深转债、T+0基金

连接状态监听接口回调数据结构调整
xtdata.watch_quote_server_status()
xtdata.watch_xtquant_status()

新增接口 xtdata.subscribe_formula() 支持连接投研端调用vba

token模式下支持按用户权限放开并行接入数量

本地python回测支持多线程

优化7*24连续交易的问题
20240205xtquant_240119b点击下载修复token模式下偶发的订阅数据异常问题
有问题的版本:240119, 240119a

token模式下并行接入数量放宽至10个
20240129xtquant_240119a点击下载合约信息接口支持参数控制获取全部字段
xtdata.get_instrument_detail(iscomplete = True)并添加以下字段
期货和期权手续费方式(ChargeType)
开仓手续费(率)(ChargeOpen)
平仓手续费(率)(ChargeClose)
开今仓(日内开仓)手续费(率)(ChargeTodayOpen)
平今仓(日内平仓)手续费(率)(ChargeTodayClose)
交割月持仓倍数(OpenInterestMultiple)

添加客户端连接状态监听接口 xtdata.watch_xtquant_status()

支持获取退市可转债数据
xtdata.get_market_data()系列函数 数据周期:delistchangebond

支持获取待发可转债数据
xtdata.get_market_data()系列函数 数据周期:replacechangebond

优化K线全推的断线重连逻辑
20240119xtquant_240119点击下载xtdata.subscribe_quote()和xtdata.get_market_data()系列函数,添加新的K线数据周期
新周期包含:周线(1w)、月线(1mon)、季度线(1q)、半年线(1hy)、年线(1y)

支持获取千档委买委卖队列数据
订阅函数 xtdata.subscribe_l2thousand_queue()
获取函数 xtdata.get_l2thousand_queue()

支持港股lv2数据(待后续迅投lv2数据源上线后可用)
支持获取港股席位数据
订阅函数 xtdata.subscribe_quote(period = 'brokerqueue')
获取函数 xtdata.get_broker_queue_data()

xtdata.get_full_tick()在VIP模式下提供成交笔数字段(transactionNum)

xtdata.get_option_detail_data()支持获取商品期权数据

支持获取历史主力合约数据
xtdata.get_market_data()系列函数 数据周期:historymaincontract

修复 获取上证期权、深证期权tick行情数据价格精度错误的问题

修复 期货夜盘分钟线获取不到的问题

优化下载数据流程

支持设置行情源自动连接目标地址范围
xtdatacenter.set_allow_optmize_address()

xtdata.get_local_data()支持指定数据路径
20231228xtquant_231209a点击下载修复xtdata.get_trading_calendar()获取历史范围返回数据重复的问题

添加xtdata.get_trading_calendar()目前仅支持SH,SZ市场的说明(其他市场交易日历陆续对接中)

添加快照指标数据周期 'snapshotindex'(包含量比、涨速、换手等字段)

修复板块指数(BKZS)分钟线获取不到的问题

添加xtdatacenter中的北交所、沪深京A股板块

修复xtdata.subscribe_whole_quote()订阅全推数据中的pvolume字段单位错误

(股票、转债的pvolume单位为股,所有品种volume单位均为手,其余品种情况详见网页文档)

20231209xtquant_231209点击下载添加ETF申赎清单信息相关接口

下载数据 xtdata.download_etf_info()

获取数据 xtdata.get_etf_info()

添加节假日下载的接口 download_holiday_data(获取交易日历依赖节假日)

添加涨跌停数据,数据周期'stoppricedata'

添加连接成功时连接状态日志

添加财务数据文档中十大股东、股东数的字段说明

修复历史st数据获取失败的问题

修复xtdatacenter提供数据时,和接收进程运行目录不同出现获取失败的问题

优化模块退出时的表现

移除xtdata.get_industry()接口

20231124xtquant_231101c点击下载修复xtdata.get_market_data()系列的内存泄漏问题

xtdatacenter.init()在重要市场初始化失败时抛出异常信息

全推数据在第一次使用时订阅,减少不必要的带宽占用

修复xtdatacenter退出时崩溃的问题

修复同目录下xtdatacenter重复启动卡住的问题

补全期货全推的月份连续合约(例如 ag01.SF)和交易合约(例如 ag2401.SF)

日志相关优化
20231110xtquant_231101b点击下载修复过期合约板块成分为空的问题

优化xtdatatcenter监听端口后连接接入的时序

xtdata.download_history_data添加增量下载参数,支持指定起始时间的增量下载

修复token无效时调用接口崩溃的问题
20231106xtquant_231101a点击下载修复退出时发生异常的问题

优化初始化过程中行情连接和数据订阅的时序
20231101xtquant_231101点击下载添加xtdatacenter,支持以token方式登录行情服务

添加xtdata.QuoteServer,支持通过xtdata控制、监控行情连接

补充xtdata中对转债交易场景、ETF交易场景的数据支持

调整了一些底层数据交互的实现方式

完善xttrader期货交易场景下的开平仓方向字段
20230920xtquant_230825b点击下载对应当前QMT券商版公版的下载python库

券商版会有版本升级跟不上的情况,通常请使用这个公版版本以保证兼容性
20230905xtquant_230825a点击下载-
20230825xtquant_230825点击下载-
20230301xtquant_230301点击下载-
20220817xtquant_220817点击下载-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/nativeApi_start_now.html b/reference/thinktrader_docs/nativeApi_start_now.html deleted file mode 100644 index 38a0b59..0000000 --- a/reference/thinktrader_docs/nativeApi_start_now.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - - - 快速开始 | 迅投知识库 - - - - -

XtQuant 能提供哪些服务

XtQuant是基于迅投MiniQMT衍生出来的一套完善的Python策略运行框架,对外以Python库的形式提供策略交易所需要的行情和交易相关的API接口。

XtQuant 运行依赖环境

XtQuant 目前提供的库包括 64 位 Python 3.63.73.83.93.103.113.12版本,不同版本的 Python 导入时会自动切换。 在运行使用 XtQuant 的程序前需要先启动 MiniQMT 客户端。

XtQuant 运行逻辑

Xtdata 作为行情模块,本模块旨在提供精简直接的数据满足量化交易者的数据需求,主要提供行情数据(历史和实时的K线和分笔)、财务数据、合约基础信息、板块和行业分类信息等通用的行情数据。

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

上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/nativeApi_xtdata.html b/reference/thinktrader_docs/nativeApi_xtdata.html deleted file mode 100644 index ca6606d..0000000 --- a/reference/thinktrader_docs/nativeApi_xtdata.html +++ /dev/null @@ -1,793 +0,0 @@ - - - - - - - - - XtQuant.XtData 行情模块 | 迅投知识库 - - - - -

XtQuant.XtData 行情模块

xtdata是xtquant库中提供行情相关数据的模块,本模块旨在提供精简直接的数据满足量化交易者的数据需求,作为python库的形式可以被灵活添加到各种策略脚本中。

主要提供行情数据(历史和实时的K线和分笔)、财务数据、合约基础信息、板块和行业分类信息等通用的行情数据。

版本信息

  • 2020-09-01
    • 初稿
  • 2020-09-07
    • 添加获取除权数据的接口get_divid_factors,附录添加除权数据字段说明
    • 获取合约信息、获取合约类型接口完善
    • 获取交易日列表接口get_trading_dates支持指定日期范围
  • 2020-09-13
    • 添加财务数据接口,调整获取和下载财务数据接口的说明,添加财务数据报表字段列表
    • 将 “补充” 字样调整为 “下载”,“supply” 接口调整为 “download”
  • 2020-09-13
    • volumn拼写错误修正为volume,影响范围:
      • tickl2quote周期行情数据 - 成交量字段
      • 合约基础信息 - 总股本、流通股本
  • 2020-11-23
    • 合约基础信息CreateDate OpenDate字段类型由int调整为str
    • 添加数据字典部分,添加level2数据字段枚举值说明
  • 2021-07-20
    • 添加新版下载数据接口
      • 下载行情数据 download_history_data2
      • 下载财务数据 download_financial_data2
  • 2021-12-30
    • 数据字典调整
      • 委托方向、成交类型添加关于上交所、深交所撤单信息的区分说明
  • 2022-06-27
    • 数据字典调整
      • K线添加前收价、停牌标记字段
  • 2022-09-30
    • 添加交易日历相关接口
      • 获取节假日数据 get_holidays
      • 获取交易日历 get_trading_calendar
      • 获取交易时段 get_trade_times
  • 2023-01-04
    • 添加千档行情获取
  • 2023-01-31
    • 可转债基础信息的下载 download_cb_data
    • 可转债基础信息的获取 get_cb_info
  • 2023-02-06
    • 添加连接到指定ip端口的接口 reconnect
  • 2023-02-07
    • 支持QMT的本地Python模式
    • 优化多个QMT同时存在的场景,自动选择xtdata连接的端口
  • 2023-03-27
    • 新股申购信息获取 get_ipo_info
  • 2023-04-13
    • 本地python模式下运行VBA函数
  • 2023-07-27
    • 文档部分描述修改
  • 2023-08-21
    • 数据接口支持投研版特色数据
      • 参考 接口概述 - 常用类型说明 - 周期 - 投研版 - 特色数据
    • 获取合约基础信息 get_instrument_detail 返回字段调整
      • 增加 ExchangeCode UniCode
    • 添加获取可用周期列表的接口 get_period_list
  • 2023-10-11
    • get_market_data_ex支持获取ETF申赎清单数据
    • 数据字典添加 现金替代标志
  • 2023-11-09
    • download_history_data添加增量下载参数,支持指定起始时间的增量下载
  • 2023-11-22
    • get_trading_calendar不再支持tradetimes参数
  • 2023-11-27
    • ETF申赎清单信息下载 download_etf_info
    • ETF申赎清单信息获取 get_etf_info
  • 2023-11-28
    • 添加节假日下载download_holiday_data
  • 2023-12-27
    • 获取板块成份股列表接口增加北交所板块
  • 2024-01-19
    • get_market_data_ex支持获取期货历史主力合约数据
    • get_option_detail_data支持获取商品期权品种的数据
    • get_market_data_ex支持获取日线以上周期的K线数据
      • 周线1w、月线1mon、季度线1q、半年线1hy、年线1y
  • 2024-01-22
    • get_trade_times改名为get_trading_time
    • get_trading_time更新实现逻辑
  • 2024-01-26
    • 获取合约基础信息 get_instrument_detail 支持获取全部合约信息字段
  • 2024-05-15
    • 获取最新交易日k线数据get_full_kline
  • 2024-05-27
    • get_stock_list_in_sector 增加real_timetag参数

接口概述

运行逻辑

xtdata提供和MiniQmt的交互接口,本质是和MiniQmt建立连接,由MiniQmt处理行情数据请求,再把结果回传返回到python层。使用的行情服务器以及能获取到的行情数据和MiniQmt是一致的,要检查数据或者切换连接时直接操作MiniQmt即可。

对于数据获取接口,使用时需要先确保MiniQmt已有所需要的数据,如果不足可以通过补充数据接口补充,再调用数据获取接口获取。

对于订阅接口,直接设置数据回调,数据到来时会由回调返回。订阅接收到的数据一般会保存下来,同种数据不需要再单独补充。

接口分类

  • 行情数据(K线数据、分笔数据,订阅和主动获取的接口)
    • 功能划分(接口前缀)
      • subscribe_ / unsubscribe_ 订阅/反订阅
      • get_ 获取数据
      • download_ 下载数据
    • 常见用法
      • level1数据的历史部分用download_history_data补充,实时部分用subscribe_XXX订阅,使用get_XXX获取
      • level2数据实时部分用subscribe_XXX订阅,用get_l2_XXX获取。level2函数无历史数据存储,跨交易日后数据清理
  • 财务数据
  • 合约基础信息
  • 基础行情数据板块分类信息等基础信息

常用类型说明

  • stock_code - 合约代码
    • 格式为 code.market,例如000001.SZ 600000.SH 000300.SH
  • period - 周期,用于表示要获取的周期和具体数据类型
    • level1数据
      • tick - 分笔数据
      • 1m - 1分钟线
      • 5m - 5分钟线
      • 15m - 15分钟线
      • 30m - 30分钟线
      • 1h - 1小时线
      • 1d - 日线
      • 1w - 周线
      • 1mon - 月线
      • 1q - 季度线
      • 1hy - 半年线
      • 1y - 年线
    • 投研版 - 特色数据
      • warehousereceipt - 期货仓单
      • futureholderrank - 期货席位
      • interactiveqa - 互动问答
      • 逐笔成交统计
        • transactioncount1m - 逐笔成交统计1分钟级
        • transactioncount1d - 逐笔成交统计日级
      • delistchangebond - 退市可转债信息
      • replacechangebond - 待发可转债信息
      • specialtreatment - ST 变更历史
      • 港股通(深港通、沪港通)资金流向
        • northfinancechange1m - 港股通资金流向1分钟级
        • northfinancechange1d - 港股通资金流向日级
      • dividendplaninfo - 红利分配方案信息
      • historycontract - 过期合约列表
      • optionhistorycontract - 期权历史信息
      • historymaincontract - 历史主力合约
      • stoppricedata - 涨跌停数据
      • snapshotindex - 快照指标数据
  • 时间范围,用于指定数据请求范围,表示的范围是[start_time, end_time]区间(包含前后边界)中最后不多于count个数据
    • start_time - 起始时间,为空则认为是最早的起始时间
    • end_time - 结束时间,为空则认为是最新的结束时间
    • count - 数据个数,大于0为正常限制返回个数,等于0为不需要返回,-1为返回全部
    • 通常以[start_time = '', end_time = '', count = -1]表示完整数据范围,但数据请求范围过大会导致返回时间变长,需要按需裁剪请求范围
  • dividend_type - 除权方式,用于K线数据复权计算,对tick等其他周期数据无效
    • none 不复权
    • front 前复权
    • back 后复权
    • front_ratio 等比前复权
    • back_ratio 等比后复权
  • 其他依赖库 numpy、pandas会在数据返回的过程中使用
    • 本模块会尽可能减少对numpy和pandas库的直接依赖,以允许使用者在不同版本的库之间自由切换
    • pandas库中旧的三维数据结构Panel没有被使用,而是以dict嵌套DataFrame代替(后续可能会考虑使用xarray等的方案,也欢迎使用者提供改进建议)
    • 后文中会按常用规则分别简写为np、pd,如np.ndarray、pd.DataFrame

请求限制

  • 全推数据是市场全部合约的切面数据,是高订阅数场景下的有效解决方案。持续订阅全推数据可以获取到每个合约最新分笔数据的推送,且流量和处理效率都优于单股订阅
  • 单股订阅行情是仅返回单股数据的接口,建议单股订阅数量不超过50。如果订阅数较多,建议直接使用全推数据
  • 板块分类信息等静态信息更新频率低,无需频繁下载,按周或按日定期下载更新即可

接口说明

行情接口

订阅单股行情

subscribe_quote(stock_code, period='1d', start_time='', end_time='', count=0, callback=None)
-
  • 释义

    • 订阅单股的行情数据,返回订阅号
    • 数据推送从callback返回,数据类型和period指定的周期对应
    • 数据范围代表请求的历史部分的数据范围,数据返回后会进入缓存,用于保证数据连续,通常情况仅订阅数据时传count = 0即可
  • 参数

    • stock_code - string 合约代码

    • period - string 周期

    • start_time - string 起始时间

    • end_time - string 结束时间

    • count - int 数据个数

    • callback - 数据推送回调

      • 回调定义形式为on_data(datas),回调参数datas格式为 { stock_code : [data1, data2, ...] }
      def on_data(datas):
      -    for stock_code in datas:
      -        	print(stock_code, datas[stock_code])
      -
  • 返回

    • 订阅号,订阅成功返回大于0,失败返回-1
  • 备注

    • 单股订阅数量不宜过多,详见 接口概述-请求限制

订阅全推行情

subscribe_whole_quote(code_list, callback=None)
-
  • 释义

    • 订阅全推行情数据,返回订阅号
    • 数据推送从callback返回,数据类型为分笔数据
  • 参数

    • code_list - 代码列表,支持传入市场代码或合约代码两种方式

      • 传入市场代码代表订阅全市场,示例:['SH', 'SZ']
      • 传入合约代码代表订阅指定的合约,示例:['600000.SH', '000001.SZ']
    • callback - 数据推送回调

      • 回调定义形式为on_data(datas),回调参数datas格式为 { stock1 : data1, stock2 : data2, ... }
      def on_data(datas):
      -    for stock_code in datas:
      -        	print(stock_code, datas[stock_code])
      -
  • 返回

    • 订阅号,订阅成功返回大于0,失败返回-1
  • 备注

    • 订阅后会首先返回当前最新的全推数据

反订阅行情数据

unsubscribe_quote(seq)
-
  • 释义
    • 反订阅行情数据
  • 参数
    • seq - 订阅时返回的订阅号
  • 返回
  • 备注

阻塞线程接收行情回调

run()
-
  • 释义
    • 阻塞当前线程来维持运行状态,一般用于订阅数据后维持运行状态持续处理回调
  • 参数
    • seq - 订阅时返回的订阅号
  • 返回
  • 备注
    • 实现方式为持续循环sleep,并在唤醒时检查连接状态,若连接断开则抛出异常结束循环

订阅模型

subscribe_formula(formula_name, stock_code, period, start_time = '', end_time = '', count = -1, dividend_type = None, extend_param = {}, callback = None)
-
  • 释义
    • 订阅vba模型运行结果,需连接投研端使用
  • 参数
    • formula_name:str,模型名

    • stock_code:str,模型主图代码形式如'stkcode.market',如'000300.SH';

    • period:str,K线周期类型 可选范围: 'tick':分笔线 '1d':日线 '1m':分钟线 '3m':三分钟线 '5m':5分钟线 '15m':15分钟线 '30m':30分钟线 '1h':小时线 '1w':周线 '1mon':月线 '1q':季线 '1hy':半年线 '1y':年线

    • start_time:str,模型运行起始时间,形如:'20200101';默认为空视为最早

    • end_time:str,模型运截止时间,形如:'20200101';默认为空视为最新

    • count:int,模型运行范围为向前count根bar,默认为-1运行所有bar

    • dividend_type:str,复权方式,默认为主图除权方式,可选范围: 'none':不复权 'front':向前复权 'back':向后复权 'front_ratio':等比向前复权 'back_ratio':等比向后复权

    • extend_param:dict,模型的入参,{参数名:参数值},形如{'a':1,'__basket':{}};

    • __basket:dict,可选参数,组合模型的股票池权重,形如{'600000.SH':0.06,'000001.SZ':0.01}

  • 返回:
    • int 订阅成功时为订阅ID,可用于后续反订阅,失败返回-1
  • 备注:
    • 使用该函数时需要补充号本地K线或分笔数据

反订阅模型

unsubscribe_formula(subID)
-
  • 释义
    • 反订阅模型
  • 参数
    • subID:int 模型订阅号
  • 返回
    • bool ,反订阅成功为True,失败为False

调用模型

call_formula(formula_name,stock_code,period,start_time="",end_time="",count=-1,dividend_type="none",extend_param={})
-
  • 释义

    • 获取vba模型运行结果,使用前要注意补充本地K线数据或分笔数据
  • 参数:

    • formula_name: str,模型名称名
    • stock_code: str,模型主图代码形式如'stkcode.market',如'000300.SH'
    • period: str,K线周期类型
      • 可选范围:
        • 'tick': 分笔线
        • '1d': 日线
        • '1m': 分钟线
        • '3m': 三分钟线
        • '5m': 5分钟线
        • '15m': 15分钟线
        • '30m': 30分钟线
        • '1h': 小时线
        • '1w': 周线
        • '1mon': 月线
        • '1q': 季线
        • '1hy': 半年线
        • '1y': 年线
    • start_time: str,模型运行起始时间,形如:'20200101',默认为空视为最早
    • end_time: str,模型运行截止时间,形如:'20200101',默认为空视为最新
    • count: int,模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
    • dividend_type: str,复权方式,默认为主图除权方式
      • 可选范围:
        • 'none': 不复权
        • 'front': 向前复权
        • 'back': 向后复权
        • 'front_ratio': 等比向前复权
        • 'back_ratio': 等比向后复权
    • extend_param: dict,模型的入参,例如 {"模型名:参数名": 参数值},例如在跑模型 MA 时,{'MA:n1': 1}
      • 入参可以添加 __basket: dict,组合模型的股票池权重,形如 {'__basket': {'600000.SH': 0.06, '000001.SZ': 0.01}}
      • 如果在跑一个模型1的时候,模型1调用了模型2,如果只想修改模型2的参数可以传 {'模型2: 参数': 参数值}
  • 返回

    • dict{ 'dbt':0,#返回数据类型,0:全部历史数据 'timelist':[...],#返回数据时间范围list, 'outputs':{'var1':[...],'var2':[...]}#输出变量名:变量值list }

批量调用模型

call_formula_batch(formula_names,stock_codes,period,start_time="",end_time="",count=-1,dividend_type="none",extend_params=[])
-
  • 释义

    • 批量获取vba模型运行结果,使用前要注意补充本地K线数据或分笔数据
  • 参数:

    • formula_names: list,包含要批量运行的模型名
    • stock_codes: list,包含要批量运行的模型主图代码形式 'stkcode.market',如 '000300.SH'
    • period: str,K线周期类型
      • 可选范围:
        • 'tick': 分笔线
        • '1d': 日线
        • '1m': 分钟线
        • '3m': 三分钟线
        • '5m': 5分钟线
        • '15m': 15分钟线
        • '30m': 30分钟线
        • '1h': 小时线
        • '1w': 周线
        • '1mon': 月线
        • '1q': 季线
        • '1hy': 半年线
        • '1y': 年线
    • start_time: str,模型运行起始时间,形如:'20200101',默认为空视为最早
    • end_time: str,模型运行截止时间,形如:'20200101',默认为空视为最新
    • count: int,模型运行范围为向前 count 根 bar,默认为 -1 运行所有 bar
    • dividend_type: str,复权方式,默认为主图除权方式
      • 可选范围:
        • 'none': 不复权
        • 'front': 向前复权
        • 'back': 向后复权
        • 'front_ratio': 等比向前复权
        • 'back_ratio': 等比向后复权
    • extend_params: list,包含每个模型的入参,形如 [{"模型名:参数名": 参数值}],例如在跑模型 MA 时,{'MA:n1': 1}
      • 入参可以添加 __basket: dict,组合模型的股票池权重,形如 {'__basket': {'600000.SH': 0.06, '000001.SZ': 0.01}}
      • 如果在跑一个模型1的时候,模型1调用了模型2,如果只想修改模型2的参数可以传 {'模型2: 参数': 参数值}
  • 返回

    • list[dict]
      • dict说明:
        • formula:模型名
        • stock:品种代码
        • argument:参数
        • result:dict参考call_formula返回结果

生成因子数据

generate_index_data(formula_name, formula_param = {}, stock_list = [], period = '1d', dividend_type = 'none', start_time = '', end_time = '', fill_mode = 'fixed', fill_value = float('nan'), result_path = None)
-
  • 释义

    • 在本地生成因子数据文件,文件格式为feather
  • 参数

    • formula_name:str 模型名称
    • formula_param:dict 模型参数,例如 {'param1': 1.0, 'param2': 'sym'}
    • stock_list:list 股票列表
    • period:str 周期
      • 可选范围
        • '1m' '5m' '1d'
    • dividend_type:str 复权方式
      • 可选范围
        • 'none' - 不复权
        • 'front_ratio' - 等比前复权
        • 'back_ratio' - 等比后复权
    • start_time:str 起始时间 格式为'20240101' 或 '20240101000000'
    • end_time: str 结束时间 格式为'20241231' 或 '20241231235959'
    • fill_mode:str 空缺填充方式
      • 可选范围
        • 'fixed' - 固定值填充
        • 'forward' - 向前延续
    • fill_value:float 填充数值
      • float('nan') - 以NaN填充
    • result_path:str 结果文件路径,feather格式
  • 返回 None

  • 备注 必须连接投研端使用,传入的formula_name需要存在于投研端中

获取行情数据

get_market_data(field_list=[], stock_list=[], period='1d', start_time='', end_time='', count=-1, dividend_type='none', fill_data=True)
-
  • 释义
    • 从缓存获取行情数据,是主动获取行情的主要接口
  • 参数
    • field_list - list 数据字段列表,传空则为全部字段
    • stock_list - list 合约代码列表
    • period - string 周期
    • start_time - string 起始时间
    • end_time - string 结束时间
    • count - int 数据个数
    • 默认参数,大于等于0时,若指定了start_time,end_time,此时以end_time为基准向前取count条;若start_time,end_time缺省,默认取本地数据最新的count条数据;若start_time,end_time,count都缺省时,默认取本地全部数据
    • dividend_type - string 除权方式
    • fill_data - bool 是否向后填充空缺数据
  • 返回
    • period为1m 5m 1d等K线周期时
      • 返回dict { field1 : value1, field2 : value2, ... }
      • field1, field2, ... :数据字段
      • value1, value2, ... :pd.DataFrame 数据集,index为stock_list,columns为time_list
      • 各字段对应的DataFrame维度相同、索引相同
    • period为tick分笔周期时
      • 返回dict { stock1 : value1, stock2 : value2, ... }
      • stock1, stock2, ... :合约代码
      • value1, value2, ... :np.ndarray 数据集,按数据时间戳time增序排列
  • 备注
    • 获取lv2数据时需要数据终端有lv2数据权限
    • 时间范围为闭区间

获取本地行情数据

get_local_data(field_list=[], stock_list=[], period='1d', start_time='', end_time='', count=-1,
-               dividend_type='none', fill_data=True, data_dir=data_dir)
-
  • 释义
    • 从本地数据文件获取行情数据,用于快速批量获取历史部分的行情数据
  • 参数
    • field_list - list 数据字段列表,传空则为全部字段
    • stock_list - list 合约代码列表
    • period - string 周期
    • start_time - string 起始时间
    • end_time - string 结束时间
    • count - int 数据个数
    • dividend_type - string 除权方式
    • fill_data - bool 是否向后填充空缺数据
    • data_dir - string MiniQmt配套路径的userdata_mini路径,用于直接读取数据文件。默认情况下xtdata会通过连接向MiniQmt直接获取此路径,无需额外设置。如果需要调整,可以将数据路径作为data_dir传入,也可以直接修改xtdata.data_dir以改变默认值
  • 返回
    • period为1m 5m 1dK线周期时
      • 返回dict { field1 : value1, field2 : value2, ... }
      • field1, field2, ... :数据字段
      • value1, value2, ... :pd.DataFrame 数据集,index为stock_list,columns为time_list
      • 各字段对应的DataFrame维度相同、索引相同
    • period为tick分笔周期时
      • 返回dict { stock1 : value1, stock2 : value2, ... }
      • stock1, stock2, ... :合约代码
      • value1, value2, ... :np.ndarray 数据集,按数据时间戳time增序排列
  • 备注
    • 仅用于获取level1数据

获取全推数据

get_full_tick(code_list)
-
  • 释义
    • 获取全推数据
  • 参数
    • code_list - 代码列表,支持传入市场代码或合约代码两种方式
      • 传入市场代码代表订阅全市场,示例:['SH', 'SZ']
      • 传入合约代码代表订阅指定的合约,示例:['600000.SH', '000001.SZ']
  • 返回
    • dict 数据集 { stock1 : data1, stock2 : data2, ... }
  • 备注

获取除权数据

get_divid_factors(stock_code, start_time='', end_time='')
-
  • 释义
    • 获取除权数据
  • 参数
    • stock_code - 合约代码
    • start_time - string 起始时间
    • end_time - string 结束时间
  • 返回
    • pd.DataFrame 数据集
  • 备注

下载历史行情数据

download_history_data(stock_code, period, start_time='', end_time='', incrementally = None)
-
  • 释义
    • 补充历史行情数据
  • 参数
    • stock_code - string 合约代码
    • period - string 周期
    • start_time - string 起始时间
    • end_time - string 结束时间
    • incrementally - 是否增量下载
      • bool - 是否增量下载
      • None - 使用start_time控制,start_time为空则增量下载,增量下载时会从本地最后一条数据往后下载
  • 返回
  • 备注
    • 同步执行,补充数据完成后返回
download_history_data2(stock_list, period, start_time='', end_time='', callback=None,incrementally = None)
-
  • 释义

    • 补充历史行情数据,批量版本
  • 参数

    • stock_list - list 合约列表

    • period - string 周期

    • start_time - string 起始时间

    • end_time - string 结束时间

    • callback - func 回调函数

      • 参数为进度信息dict

        • total - 总下载个数
        • finished - 已完成个数
        • stockcode - 本地下载完成的合约代码
        • message - 本次信息
      • def on_progress(data):
        -	print(data)
        -	# {'finished': 1, 'total': 50, 'stockcode': '000001.SZ', 'message': ''}
        -
  • 返回

  • 备注

    • 同步执行,补充数据完成后返回
    • 有任务完成时通过回调函数返回进度信息

下载过期(退市)合约信息

download_history_contracts()
-
  • 释义
    • 下载过期(退市)合约信息,过期(退市)标的列表可以通过get_stock_list_in_sector获取
  • 参数
    • None
  • 返回
  • 备注
    • 同步执行,补充数据完成后返回
    • 过期板块名称可以通过 print([i for i in xtdata.get_sector_list() if "过期" in i]) 查看
    • 下载完成后,可以通过 xtdata.get_instrument_detail() 查看过期(退市)合约信息

获取节假日数据

get_holidays()
-
  • 释义
    • 获取截止到当年的节假日日期
  • 参数
  • 返回
    • list,为8位的日期字符串格式
  • 备注

获取交易日历

get_trading_calendar(market, start_time = '', end_time = '')
-
  • 释义
    • 获取指定市场交易日历
  • 参数
    • market - str 市场
    • start_time - str 起始时间,8位字符串。为空表示当前市场首个交易日时间
    • end_time - str 结束时间,8位字符串。为空表示当前时间
  • 返回
    • 返回list,完整的交易日列表
  • 备注
    • 结束时间可以填写未来时间,获取未来交易日。需要下载节假日列表。

可转债基础信息的下载

download_cb_data()
-
  • 释义
    • 下载全部可转债信息
  • 参数
  • 返回
  • 备注

获取可转债基础信息

get_cb_info(stockcode)
-
  • 释义
    • 返回指定代码的可转债信息
  • 参数
    • stockcode - str 合约代码(例如600000.SH
  • 返回
  • 备注
    • 需要先下载可转债数据

获取新股申购信息

get_ipo_info(start_time, end_time)
-
  • 释义

    • 返回所选时间范围的新股申购信息
  • 参数

    • start_time: 开始日期(如:'20230327')
    • end_time: 结束日期(如:'20230327')
    • start_time 和 end_time 为空则返回全部数据
  • 返回

    • list[dict],新股申购信息

    • securityCode - string 证券代码
      -codeName - string 代码简称
      -market - string 所属市场
      -actIssueQty - int 发行总量,单位:股
      -onlineIssueQty - int 网上发行量, 单位:股
      -onlineSubCode - string 申购代码
      -onlineSubMaxQty - int 申购上限, 单位:股
      -publishPrice - float 发行价格
      -isProfit - int 是否已盈利 0:上市时尚未盈利 1:上市时已盈利
      -industryPe - float 行业市盈率
      -afterPE - float 发行后市盈率
      -

获取可用周期列表

get_period_list()
-
  • 释义

    • 返回可用周期列表
  • 参数

  • 返回

    • list 周期列表

ETF申赎清单信息下载

download_etf_info()
-
  • 释义

    • 下载所有ETF申赎清单信息
  • 参数

  • 返回

ETF申赎清单信息获取

get_etf_info()
-
  • 释义

    • 获取所有ETF申赎清单信息
  • 参数

  • 返回

    • dict 所有申赎数据

节假日下载

download_holiday_data()
-
  • 释义

    • 下载节假日数据
  • 参数

  • 返回

获取最新交易日k线数据

get_full_kline(field_list = [], stock_list = [], period = '1m'
-    , start_time = '', end_time = '', count = 1
-    , dividend_type = 'none', fill_data = True)
-
  • 释义

    • 获取最新交易日k线全推数据,仅支持最新一个交易日,不包含历史值
  • 参数

    • 参考get_market_data函数
  • 返回

    • dict - {field: DataFrame}

财务数据接口

获取财务数据

get_financial_data(stock_list, table_list=[], start_time='', end_time='', report_type='report_time')
-
  • 释义

    • 获取财务数据
  • 参数

    • stock_list - list 合约代码列表

    • table_list - list 财务数据表名称列表

      • 'Balance'          #资产负债表
        -'Income'           #利润表
        -'CashFlow'         #现金流量表
        -'Capital'          #股本表
        -'Holdernum'        #股东数
        -'Top10holder'      #十大股东
        -'Top10flowholder'  #十大流通股东
        -'Pershareindex'    #每股指标
        -
    • start_time - string 起始时间

    • end_time - string 结束时间

    • report_type - string 报表筛选方式

      • 'report_time' 	#截止日期
        -'announce_time' #披露日期
        -
  • 返回

    • dict 数据集 { stock1 : datas1, stock2 : data2, ... }
    • stock1, stock2, ... :合约代码
    • datas1, datas2, ... :dict 数据集 { table1 : table_data1, table2 : table_data2, ... }
      • table1, table2, ... :财务数据表名
      • table_data1, table_data2, ... :pd.DataFrame 数据集,数据字段详见附录 - 财务数据字段列表
  • 备注

下载财务数据

download_financial_data(stock_list, table_list=[])
-
  • 释义
    • 下载财务数据
  • 参数
    • stock_list - list 合约代码列表
    • table_list - list 财务数据表名列表
  • 返回
  • 备注
    • 同步执行,补充数据完成后返回
download_financial_data2(stock_list, table_list=[], start_time='', end_time='', callback=None)
-
  • 释义

    • 下载财务数据
  • 参数

    • stock_list - list 合约代码列表

    • table_list - list 财务数据表名列表

    • start_time - string 起始时间

    • end_time - string 结束时间

      • m_anntime披露日期字段,按[start_time, end_time]范围筛选
    • callback - func 回调函数

      • 参数为进度信息dict

        • total - 总下载个数
        • finished - 已完成个数
        • stockcode - 本地下载完成的合约代码
        • message - 本次信息
      • def on_progress(data):
        -	print(data)
        -	# {'finished': 1, 'total': 50, 'stockcode': '000001.SZ', 'message': ''}
        -
  • 返回

  • 备注

    • 同步执行,补充数据完成后返回

基础行情信息

获取合约基础信息

get_instrument_detail(stock_code, iscomplete)
-
  • 释义

    • 获取合约基础信息
  • 参数

    • stock_code - string 合约代码
    • iscomplete - bool 是否获取全部字段,默认为False
  • 返回

    • dict 数据字典,{ field1 : value1, field2 : value2, ... },找不到指定合约时返回None

    • iscomplete为False时,返回以下字段

      ExchangeID - string 合约市场代码
      -InstrumentID - string 合约代码
      -InstrumentName - string 合约名称
      -ProductID - string 合约的品种ID(期货)
      -ProductName - string 合约的品种名称(期货)
      -ExchangeCode - string 交易所代码
      -UniCode - string 统一规则代码
      -CreateDate - str 上市日期(期货)
      -OpenDate - str IPO日期(股票)
      -ExpireDate - int 退市日或者到期日
      -PreClose - float 前收盘价格
      -SettlementPrice - float 前结算价格
      -UpStopPrice - float 当日涨停价
      -DownStopPrice - float 当日跌停价
      -FloatVolume - float 流通股本
      -TotalVolume - float 总股本
      -LongMarginRatio - float 多头保证金率
      -ShortMarginRatio - float 空头保证金率
      -PriceTick - float 最小价格变动单位
      -VolumeMultiple - int 合约乘数(对期货以外的品种,默认是1)
      -MainContract - int 主力合约标记,123分别表示第一主力合约,第二主力合约,第三主力合约
      -LastVolume - int 昨日持仓量
      -InstrumentStatus - int 合约停牌状态
      -IsTrading - bool 合约是否可交易
      -IsRecent - bool 是否是近月合约
      -OpenInterestMultiple - int 交割月持仓倍数 
      -
    • iscomplete为True时,增加会返回更多合约信息字段,例如

      ChargeType - int 期货和期权手续费方式 0表示未知1表示按元/手,2表示按费率,单位为万分比,‱
      -ChargeOpen - float 开仓手续费(率) 返回-1时该值无效,其余情况参考ChargeType
      -ChargeClose - float 平仓手续费(率) 返回-1时该值无效,其余情况参考ChargeType
      -ChargeTodayOpen - float 开今仓(日内开仓)手续费(率) 返回-1时该值无效,其余情况参考ChargeType
      -ChargeTodayClose - float 平今仓(日内平仓)手续费(率)  返回-1时该值无效,其余情况参考ChargeType
      -OptionType - int 期权类型 返回-1表示合约为非期权 返回0为期权认购  返回1为期权认沽
      -......
      -
      -
    • 详细合约信息字段见附录-合约信息字段列表

  • 备注

    • 可用于检查合约代码是否正确
    • 合约基础信息CreateDate OpenDate字段类型由int调整为str

获取合约类型

get_instrument_type(stock_code)
-
  • 释义

    • 获取合约类型
  • 参数

    • stock_code - string 合约代码
  • 返回

    • dict 数据字典,{ type1 : value1, type2 : value2, ... },找不到指定合约时返回None

      • type1, type2, ... :string 合约类型
      • value1, value2, ... :bool 是否为该类合约
    • 'index'		#指数
      -'stock'		#股票
      -'fund'		#基金
      -'etf'		#ETF
      -
  • 备注

获取交易日列表

get_trading_dates(market, start_time='', end_time='', count=-1)
-
  • 释义
    • 获取交易日列表
  • 参数
    • market - string 市场代码
    • start_time - string 起始时间
    • end_time - string 结束时间
    • count - int 数据个数
  • 返回
    • list 时间戳列表,[ date1, date2, ... ]
  • 备注

获取板块列表

get_sector_list()
-
  • 释义
    • 获取板块列表
  • 参数
  • 返回
    • list 板块列表,[ sector1, sector2, ... ]
  • 备注
    • 需要下载板块分类信息

获取板块成分股列表

get_stock_list_in_sector(sector_name)
-
  • 释义
    • 获取板块成分股列表
  • 参数
    • sector_name - string 版块名称
  • 返回
    • list 成分股列表,[ stock1, stock2, ... ]
  • 备注
    • 需要板块分类信息

下载板块分类信息

download_sector_data()
-
  • 释义
    • 下载板块分类信息
  • 参数
  • 返回
  • 备注
    • 同步执行,下载完成后返回

创建板块目录节点

create_sector_folder(parent_node, folder_name, overwrite)
-
  • 释义
    • 创建板块目录节点
  • 参数
    • parent_node - string 父节点,’ ‘为 '我的‘ (默认目录)
    • folder_name - string 要创建的板块目录名称
    • overwrite- bool 是否覆盖,如果目标节点已存在,为True时跳过,为False时在folder_name后增加数字编号,编号为从1开始自增的第一个不重复的值。 默认为True
  • 返回
    • folder_name2 - string 实际创建的板块目录名
  • 备注

创建板块

create_sector(parent_node, sector_name, overwrite)
-
  • 释义
    • 创建板块
  • 参数
    • parent_node - string 父节点,’ ‘为 '我的‘ (默认目录)
    • sector_name - string 板块名称
    • overwrite- bool 是否覆盖,如果目标节点已存在,为True时跳过,为False时在sector_name后增加数字编号,编号为从1开始自增的第一个不重复的值。 默认为True
  • 返回
    • sector_name2 - string 实际创建的板块名
  • 备注

添加自定义板块

add_sector(sector_name, stock_list)
-
  • 释义
    • 添加自定义板块
  • 参数
    • sector_name - string 板块名称
    • stock_list - list 成分股列表
  • 返回
  • 备注

移除板块成分股

remove_stock_from_sector(sector_name, stock_list)
-
  • 释义
    • 创建板块
  • 参数
    • sector_name - string 板块名称
    • stock_list- list 成分股列表
  • 返回
    • result - bool 操作成功为True,失败为False
  • 备注

移除自定义板块

remove_sector(sector_name)
-
  • 释义
    • 移除自定义板块
  • 参数
    • sector_name - string 板块名称
  • 返回
  • 备注

重置板块

reset_sector(sector_name, stock_list)
-
  • 释义
    • 重置板块
  • 参数
    • sector_name - string 板块名称
    • stock_list- list 成分股列表
  • 返回
    • result - bool 操作成功为True,失败为False
  • 备注

获取指数成分权重信息

get_index_weight(index_code)
-
  • 释义
    • 获取指数成分权重信息
  • 参数
    • index_code - string 指数代码
  • 返回
    • dict 数据字典,{ stock1 : weight1, stock2 : weight2, ... }
  • 备注
    • 需要下载指数成分权重信息

下载指数成分权重信息

download_index_weight()
-
  • 释义
    • 下载指数成分权重信息
  • 参数
  • 返回
  • 备注
    • 同步执行,下载完成后返回

附录

行情数据字段列表

tick - 分笔数据

'time'                  #时间戳
-'lastPrice'             #最新价
-'open'                  #开盘价
-'high'                  #最高价
-'low'                   #最低价
-'lastClose'             #前收盘价
-'amount'                #成交总额
-'volume'                #成交总量
-'pvolume'               #原始成交总量
-'stockStatus'           #证券状态
-'openInt'               #持仓量
-'lastSettlementPrice'   #前结算
-'askPrice'              #委卖价
-'bidPrice'              #委买价
-'askVol'                #委卖量
-'bidVol'                #委买量
-'transactionNum'		#成交笔数
-

1m / 5m / 1d - K线数据

'time'                  #时间戳
-'open'                  #开盘价
-'high'                  #最高价
-'low'                   #最低价
-'close'                 #收盘价
-'volume'                #成交量
-'amount'                #成交额
-'settelementPrice'      #今结算
-'openInterest'          #持仓量
-'preClose'              #前收价
-'suspendFlag'           #停牌标记 0 - 正常 1 - 停牌 -1 - 当日起复牌
-

除权数据

'interest'        		#每股股利(税前,元)
-'stockBonus'      		#每股红股(股)
-'stockGift'       		#每股转增股本(股)
-'allotNum'        		#每股配股数(股)
-'allotPrice'      		#配股价格(元)
-'gugai'           		#是否股改, 对于股改,在算复权系数时,系统有特殊算法
-'dr'              		#除权系数
-

l2quote - level2实时行情快照

'time'                  #时间戳
-'lastPrice'             #最新价
-'open'                  #开盘价
-'high'                  #最高价
-'low'                   #最低价
-'amount'                #成交额
-'volume'                #成交总量
-'pvolume'               #原始成交总量
-'openInt'               #持仓量
-'stockStatus'           #证券状态
-'transactionNum'        #成交笔数
-'lastClose'             #前收盘价
-'lastSettlementPrice'   #前结算
-'settlementPrice'       #今结算
-'pe'                    #市盈率
-'askPrice'              #多档委卖价
-'bidPrice'              #多档委买价
-'askVol'                #多档委卖量
-'bidVol'                #多档委买量
-

l2order - level2逐笔委托

'time'                  #时间戳
-'price'                 #委托价
-'volume'                #委托量
-'entrustNo'             #委托号
-'entrustType'           #委托类型
-'entrustDirection'      #委托方向
-

l2transaction - level2逐笔成交

'time'                  #时间戳
-'price'                 #成交价
-'volume'                #成交量
-'amount'                #成交额
-'tradeIndex'            #成交记录号
-'buyNo'                 #买方委托号
-'sellNo'                #卖方委托号
-'tradeType'             #成交类型
-'tradeFlag'             #成交标志
-

l2quoteaux - level2实时行情补充(总买总卖)

'time'                  #时间戳
-'avgBidPrice'           #委买均价
-'totalBidQuantity'      #委买总量
-'avgOffPrice'           #委卖均价
-'totalOffQuantity'      #委卖总量
-'withdrawBidQuantity'   #买入撤单总量
-'withdrawBidAmount'     #买入撤单总额
-'withdrawOffQuantity'   #卖出撤单总量
-'withdrawOffAmount'     #卖出撤单总额
-

l2orderqueue - level2委买委卖一档委托队列

'time'                  #时间戳
-'bidLevelPrice'         #委买价
-'bidLevelVolume'        #委买量
-'offerLevelPrice'       #委卖价
-'offerLevelVolume'      #委卖量
-'bidLevelNumber'        #委买数量
-'offLevelNumber'        #委卖数量
-

数据字典

证券状态

0,10 - 默认为未知
-11 - 开盘前S
-12 - 集合竞价时段C
-13 - 连续交易T
-14 - 休市B
-15 - 闭市E
-16 - 波动性中断V
-17 - 临时停牌P
-18 - 收盘集合竞价U
-19 - 盘中集合竞价M
-20 - 暂停交易至闭市N
-21 - 获取字段异常
-22 - 盘后固定价格行情
-23 - 盘后固定价格行情完毕
-

委托类型

  • level2逐笔委托 - entrustType 委托类型
  • level2逐笔成交 - tradeType 成交类型
0 - 未知
-1 - 正常交易业务
-2 - 即时成交剩余撤销
-3 - ETF基金申报
-4 - 最优五档即时成交剩余撤销
-5 - 全额成交或撤销
-6 - 本方最优价格
-7 - 对手方最优价格
-

委托方向

  • level2逐笔委托 - entrustDirection 委托方向
    • 注:上交所的撤单信息在逐笔委托的委托方向,区分撤买撤卖
1 - 买入
-2 - 卖出
-3 - 撤买(上交所)
-4 - 撤卖(上交所)
-

成交标志

  • level2逐笔成交 - tradeFlag 成交标志
    • 注:深交所的在逐笔成交的成交标志,只有撤单,没有方向
0 - 未知
-1 - 外盘
-2 - 内盘
-3 - 撤单(深交所)
-

现金替代标志

  • ETF申赎清单成份股现金替代标志
0 - 禁止现金替代(必须有股票)
-1 - 允许现金替代(先用股票,股票不足的话用现金替代
-2 - 必须现金替代
-3 - 非沪市(股票)退补现金替代
-4 - 非沪市(股票)必须现金替代
-5 - 非沪深退补现金替代
-6 - 非沪深必须现金替代
-7 - 港市退补现金替代(仅适用于跨沪深ETF产品)
-8 - 港市必须现金替代(仅适用于跨沪深港ETF产品)
-

财务数据字段列表

Balance - 资产负债表

'm_anntime'                                 #披露日期
-'m_timetag'                                 #截止日期
-'internal_shoule_recv'                      #内部应收款
-'fixed_capital_clearance'                   #固定资产清理
-'should_pay_money'                          #应付分保账款
-'settlement_payment'                        #结算备付金
-'receivable_premium'                        #应收保费
-'accounts_receivable_reinsurance'           #应收分保账款
-'reinsurance_contract_reserve'              #应收分保合同准备金
-'dividends_payable'                         #应收股利
-'tax_rebate_for_export'                     #应收出口退税
-'subsidies_receivable'                      #应收补贴款
-'deposit_receivable'                        #应收保证金
-'apportioned_cost'                          #待摊费用
-'profit_and_current_assets_with_deal'       #待处理流动资产损益
-'current_assets_one_year'                   #一年内到期的非流动资产
-'long_term_receivables'                     #长期应收款
-'other_long_term_investments'               #其他长期投资
-'original_value_of_fixed_assets'            #固定资产原值
-'net_value_of_fixed_assets'                 #固定资产净值
-'depreciation_reserves_of_fixed_assets'     #固定资产减值准备
-'productive_biological_assets'              #生产性生物资产
-'public_welfare_biological_assets'          #公益性生物资产
-'oil_and_gas_assets'                        #油气资产
-'development_expenditure'                   #开发支出
-'right_of_split_share_distribution'         #股权分置流通权
-'other_non_mobile_assets'                   #其他非流动资产
-'handling_fee_and_commission'               #应付手续费及佣金
-'other_payables'                            #其他应交款
-'margin_payable'                            #应付保证金
-'internal_accounts_payable'                 #内部应付款
-'advance_cost'                              #预提费用
-'insurance_contract_reserve'                #保险合同准备金
-'broker_buying_and_selling_securities'      #代理买卖证券款
-'acting_underwriting_securities'            #代理承销证券款
-'international_ticket_settlement'           #国际票证结算
-'domestic_ticket_settlement'                #国内票证结算
-'deferred_income'                           #递延收益
-'short_term_bonds_payable'                  #应付短期债券
-'long_term_deferred_income'                 #长期递延收益
-'undetermined_investment_losses'            #未确定的投资损失
-'quasi_distribution_of_cash_dividends'      #拟分配现金股利
-'provisions_not'                            #预计负债
-'cust_bank_dep'                             #吸收存款及同业存放
-'provisions'                                #预计流动负债
-'less_tsy_stk'                              #减:库存股
-'cash_equivalents'                          #货币资金
-'loans_to_oth_banks'                        #拆出资金
-'tradable_fin_assets'                       #交易性金融资产
-'derivative_fin_assets'                     #衍生金融资产
-'bill_receivable'                           #应收票据
-'account_receivable'                        #应收账款
-'advance_payment'                           #预付款项
-'int_rcv'                                   #应收利息
-'other_receivable'                          #其他应收款
-'red_monetary_cap_for_sale'                 #买入返售金融资产
-'agency_bus_assets'                         #以公允价值计量且其变动计入当期损益的金融资产
-'inventories'                               #存货
-'other_current_assets'                      #其他流动资产
-'total_current_assets'                      #流动资产合计
-'loans_and_adv_granted'                     #发放贷款及垫款
-'fin_assets_avail_for_sale'                 #可供出售金融资产
-'held_to_mty_invest'                        #持有至到期投资
-'long_term_eqy_invest'                      #长期股权投资
-'invest_real_estate'                        #投资性房地产
-'accumulated_depreciation'                  #累计折旧
-'fix_assets'                                #固定资产
-'constru_in_process'                        #在建工程
-'construction_materials'                    #工程物资
-'long_term_liabilities'                     #长期负债
-'intang_assets'                             #无形资产
-'goodwill'                                  #商誉
-'long_deferred_expense'                     #长期待摊费用
-'deferred_tax_assets'                       #递延所得税资产
-'total_non_current_assets'                  #非流动资产合计
-'tot_assets'                                #资产总计
-'shortterm_loan'                            #短期借款
-'borrow_central_bank'                       #向中央银行借款
-'loans_oth_banks'                           #拆入资金
-'tradable_fin_liab'                         #交易性金融负债
-'derivative_fin_liab'                       #衍生金融负债
-'notes_payable'                             #应付票据
-'accounts_payable'                          #应付账款
-'advance_peceipts'                          #预收账款
-'fund_sales_fin_assets_rp'                  #卖出回购金融资产款
-'empl_ben_payable'                          #应付职工薪酬
-'taxes_surcharges_payable'                  #应交税费
-'int_payable'                               #应付利息
-'dividend_payable'                          #应付股利
-'other_payable'                             #其他应付款
-'non_current_liability_in_one_year'         #一年内到期的非流动负债
-'other_current_liability'                   #其他流动负债
-'total_current_liability'                   #流动负债合计
-'long_term_loans'                           #长期借款
-'bonds_payable'                             #应付债券
-'longterm_account_payable'                  #长期应付款
-'grants_received'                           #专项应付款
-'deferred_tax_liab'                         #递延所得税负债
-'other_non_current_liabilities'             #其他非流动负债
-'non_current_liabilities'                   #非流动负债合计
-'tot_liab'                                  #负债合计
-'cap_stk'                                   #实收资本(或股本)
-'cap_rsrv'                                  #资本公积
-'specific_reserves'                         #专项储备
-'surplus_rsrv'                              #盈余公积
-'prov_nom_risks'                            #一般风险准备
-'undistributed_profit'                      #未分配利润
-'cnvd_diff_foreign_curr_stat'               #外币报表折算差额
-'tot_shrhldr_eqy_excl_min_int'              #归属于母公司股东权益合计
-'minority_int'                              #少数股东权益
-'total_equity'                              #所有者权益合计
-'tot_liab_shrhldr_eqy'                      #负债和股东权益总计
-

Income - 利润表

'm_anntime'                                 #披露日期
-'m_timetag'                                 #截止日期
-'revenue_inc'                               #营业收入
-'earned_premium'                            #已赚保费
-'real_estate_sales_income'                  #房地产销售收入
-'total_operating_cost'                      #营业总成本
-'real_estate_sales_cost'                    #房地产销售成本
-'research_expenses'                         #研发费用
-'surrender_value'                           #退保金
-'net_payments'                              #赔付支出净额
-'net_withdrawal_ins_con_res'                #提取保险合同准备金净额
-'policy_dividend_expenses'                  #保单红利支出
-'reinsurance_cost'                          #分保费用
-'change_income_fair_value'                  #公允价值变动收益
-'futures_loss'                              #期货损益
-'trust_income'                              #托管收益
-'subsidize_revenue'                         #补贴收入
-'other_business_profits'                    #其他业务利润
-'net_profit_excl_merged_int_inc'            #被合并方在合并前实现净利润
-'int_inc'                                   #利息收入
-'handling_chrg_comm_inc'                    #手续费及佣金收入
-'less_handling_chrg_comm_exp'               #手续费及佣金支出
-'other_bus_cost'                            #其他业务成本
-'plus_net_gain_fx_trans'                    #汇兑收益
-'il_net_loss_disp_noncur_asset'             #非流动资产处置收益
-'inc_tax'                                   #所得税费用
-'unconfirmed_invest_loss'                   #未确认投资损失
-'net_profit_excl_min_int_inc'               #归属于母公司所有者的净利润
-'less_int_exp'                              #利息支出
-'other_bus_inc'                             #其他业务收入
-'revenue'                                   #营业总收入
-'total_expense'                             #营业成本
-'less_taxes_surcharges_ops'                 #营业税金及附加
-'sale_expense'                              #销售费用
-'less_gerl_admin_exp'                       #管理费用
-'financial_expense'                         #财务费用
-'less_impair_loss_assets'                   #资产减值损失
-'plus_net_invest_inc'                       #投资收益
-'incl_inc_invest_assoc_jv_entp'             #联营企业和合营企业的投资收益
-'oper_profit'                               #营业利润
-'plus_non_oper_rev'                         #营业外收入
-'less_non_oper_exp'                         #营业外支出
-'tot_profit'                                #利润总额
-'net_profit_incl_min_int_inc'               #净利润
-'net_profit_incl_min_int_inc_after'         #净利润(扣除非经常性损益后)
-'minority_int_inc'                          #少数股东损益
-'s_fa_eps_basic'                            #基本每股收益
-'s_fa_eps_diluted'                          #稀释每股收益
-'total_income'                              #综合收益总额
-'total_income_minority'                     #归属于少数股东的综合收益总额
-'other_compreh_inc'                         #其他收益
-

CashFlow - 现金流量表

'm_anntime'                                 #披露日期
-'m_timetag'                                 #截止日期
-'cash_received_ori_ins_contract_pre'        #收到原保险合同保费取得的现金
-'net_cash_received_rei_ope'                 #收到再保险业务现金净额
-'net_increase_insured_funds'                #保户储金及投资款净增加额
-'Net'                                       #处置交易性金融资产净增加额 increase_in_disposal
-'cash_for_interest'                         #收取利息、手续费及佣金的现金
-'net_increase_in_repurchase_funds'          #回购业务资金净增加额
-'cash_for_payment_original_insurance'       #支付原保险合同赔付款项的现金
-'cash_payment_policy_dividends'             #支付保单红利的现金
-'disposal_other_business_units'             #处置子公司及其他收到的现金
-'cash_received_from_pledges'                #减少质押和定期存款所收到的现金
-'cash_paid_for_investments'                 #投资所支付的现金
-'net_increase_in_pledged_loans'             #质押贷款净增加额
-'cash_paid_by_subsidiaries'                 #取得子公司及其他营业单位支付的现金净额
-'increase_in_cash_paid'                     #增加质押和定期存款所支付的现金 
-'cass_received_sub_abs'                     #其中子公司吸收现金
-'cass_received_sub_investments'             #其中:子公司支付给少数股东的股利、利润
-'minority_shareholder_profit_loss'          #少数股东损益
-'unrecognized_investment_losses'            #未确认的投资损失
-'ncrease_deferred_income'                   #递延收益增加(减:减少)
-'projected_liability'                       #预计负债
-'increase_operational_payables'             #经营性应付项目的增加
-'reduction_outstanding_amounts_less'        #已完工尚未结算款的减少(减:增加)
-'reduction_outstanding_amounts_more'        #已结算尚未完工款的增加(减:减少)
-'goods_sale_and_service_render_cash'        #销售商品、提供劳务收到的现金
-'net_incr_dep_cob'                          #客户存款和同业存放款项净增加额
-'net_incr_loans_central_bank'               #向中央银行借款净增加额(万元
-'net_incr_fund_borr_ofi'                    #向其他金融机构拆入资金净增加额
-'net_incr_fund_borr_ofi'                    #拆入资金净增加额
-'tax_levy_refund'                           #收到的税费与返还
-'cash_paid_invest'                          #投资支付的现金
-'other_cash_recp_ral_oper_act'              #收到的其他与经营活动有关的现金
-'stot_cash_inflows_oper_act'                #经营活动现金流入小计
-'goods_and_services_cash_paid'              #购买商品、接受劳务支付的现金
-'net_incr_clients_loan_adv'                 #客户贷款及垫款净增加额
-'net_incr_dep_cbob'                         #存放中央银行和同业款项净增加额
-'handling_chrg_paid'                        #支付利息、手续费及佣金的现金
-'cash_pay_beh_empl'                         #支付给职工以及为职工支付的现金
-'pay_all_typ_tax'                           #支付的各项税费
-'other_cash_pay_ral_oper_act'               #支付其他与经营活动有关的现金
-'stot_cash_outflows_oper_act'               #经营活动现金流出小计
-'net_cash_flows_oper_act'                   #经营活动产生的现金流量净额
-'cash_recp_disp_withdrwl_invest'            #收回投资所收到的现金
-'cash_recp_return_invest'                   #取得投资收益所收到的现金
-'net_cash_recp_disp_fiolta'                 #处置固定资产、无形资产和其他长期投资收到的现金
-'other_cash_recp_ral_inv_act'               #收到的其他与投资活动有关的现金
-'stot_cash_inflows_inv_act'                 #投资活动现金流入小计
-'cash_pay_acq_const_fiolta'                 #购建固定资产、无形资产和其他长期投资支付的现金
-'other_cash_pay_ral_oper_act'               #支付其他与投资的现金
-'stot_cash_outflows_inv_act'                #投资活动现金流出小计
-'net_cash_flows_inv_act'                    #投资活动产生的现金流量净额
-'cash_recp_cap_contrib'                     #吸收投资收到的现金
-'cash_recp_borrow'                          #取得借款收到的现金
-'proc_issue_bonds'                          #发行债券收到的现金
-'other_cash_recp_ral_fnc_act'               #收到其他与筹资活动有关的现金
-'stot_cash_inflows_fnc_act'                 #筹资活动现金流入小计
-'cash_prepay_amt_borr'                      #偿还债务支付现金
-'cash_pay_dist_dpcp_int_exp'                #分配股利、利润或偿付利息支付的现金
-'other_cash_pay_ral_fnc_act'                #支付其他与筹资的现金
-'stot_cash_outflows_fnc_act'                #筹资活动现金流出小计
-'net_cash_flows_fnc_act'                    #筹资活动产生的现金流量净额
-'eff_fx_flu_cash'                           #汇率变动对现金的影响
-'net_incr_cash_cash_equ'                    #现金及现金等价物净增加额
-'cash_cash_equ_beg_period'                  #期初现金及现金等价物余额
-'cash_cash_equ_end_period'                  #期末现金及现金等价物余额
-'net_profit'                                #净利润
-'plus_prov_depr_assets'                     #资产减值准备
-'depr_fa_coga_dpba'                         #固定资产折旧、油气资产折耗、生产性物资折旧
-'amort_intang_assets'                       #无形资产摊销
-'amort_lt_deferred_exp'                     #长期待摊费用摊销
-'decr_deferred_exp'                         #待摊费用的减少
-'incr_acc_exp'                              #预提费用的增加
-'loss_disp_fiolta'                          #处置固定资产、无形资产和其他长期资产的损失
-'loss_scr_fa'                               #固定资产报废损失
-'loss_fv_chg'                               #公允价值变动损失
-'fin_exp'                                   #财务费用
-'invest_loss'                               #投资损失
-'decr_deferred_inc_tax_assets'              #递延所得税资产减少
-'incr_deferred_inc_tax_liab'                #递延所得税负债增加
-'decr_inventories'                          #存货的减少
-'decr_oper_payable'                         #经营性应收项目的减少
-'others'                                    #其他
-'im_net_cash_flows_oper_act'                #经营活动产生现金流量净额
-'conv_debt_into_cap'                        #债务转为资本
-'conv_corp_bonds_due_within_1y'             #一年内到期的可转换公司债券
-'fa_fnc_leases'                             #融资租入固定资产
-'end_bal_cash'                              #现金的期末余额
-'less_beg_bal_cash'                         #现金的期初余额
-'plus_end_bal_cash_equ'                     #现金等价物的期末余额
-'less_beg_bal_cash_equ'                     #现金等价物的期初余额
-'im_net_incr_cash_cash_equ'                 #现金及现金等价物的净增加额
-'tax_levy_refund'                           #收到的税费返还
-

PershareIndex - 主要指标

's_fa_ocfps'                                #每股经营活动现金流量
-'s_fa_bps'                                  #每股净资产
-'s_fa_eps_basic'                            #基本每股收益
-'s_fa_eps_diluted'                          #稀释每股收益
-'s_fa_undistributedps'                      #每股未分配利润
-'s_fa_surpluscapitalps'                     #每股资本公积金
-'adjusted_earnings_per_share'               #扣非每股收益
-'du_return_on_equity'                       #净资产收益率
-'sales_gross_profit'                        #销售毛利率
-'inc_revenue_rate'                          #主营收入同比增长
-'du_profit_rate'                            #净利润同比增长
-'inc_net_profit_rate'                       #归属于母公司所有者的净利润同比增长
-'adjusted_net_profit_rate'                  #扣非净利润同比增长
-'inc_total_revenue_annual'                  #营业总收入滚动环比增长
-'inc_net_profit_to_shareholders_annual'     #归属净利润滚动环比增长
-'adjusted_profit_to_profit_annual'          #扣非净利润滚动环比增长
-'equity_roe'                                #加权净资产收益率
-'net_roe'                                   #摊薄净资产收益率
-'total_roe'                                 #摊薄总资产收益率
-'gross_profit'                              #毛利率
-'net_profit'                                #净利率
-'actual_tax_rate'                           #实际税率
-'pre_pay_operate_income'                    #预收款 / 营业收入
-'sales_cash_flow'                           #销售现金流 / 营业收入
-'gear_ratio'                                #资产负债比率
-'inventory_turnover'                        #存货周转率
-'m_anntime'                                 #公告日
-'m_timetag'                                 #报告截止日
-

Capital - 股本表

'total_capital'                             #总股本
-'circulating_capital'                       #已上市流通A股
-'restrict_circulating_capital'              #限售流通股份
-'m_timetag'                                 #报告截止日
-'m_anntime'                                 #公告日
-

Top10holder/Top10flowholder - 十大股东/十大流通股东

'declareDate'                                #公告日期
-'endDate'                                    #截止日期
-'name'                                       #股东名称
-'type'                                       #股东类型
-'quantity'                                   #持股数量
-'reason'                                     #变动原因
-'ratio'                                      #持股比例
-'nature'                                     #股份性质
-'rank'                                       #持股排名
-

Holdernum - 股东数

'declareDate'                                 #公告日期
-'endDate'                                     #截止日期
-'shareholder'                                 #股东总数
-'shareholderA'                                #A股东户数
-'shareholderB'                                #B股东户数
-'shareholderH'                                #H股东户数
-'shareholderFloat'                            #已流通股东户数
-'shareholderOther'                            #未流通股东户数
-

合约信息字段列表

'ExchangeID' 				#合约市场代码
-'InstrumentID' 				#合约代码
-'InstrumentName' 			#合约名称
-'Abbreviation' 				#合约名称的拼音简写
-'ProductID' 				#合约的品种ID(期货)
-'ProductName' 				#合约的品种名称(期货)
-'UnderlyingCode' 			#标的合约
-'ExtendName' 				#扩位名称
-'ExchangeCode' 				#交易所代码
-'RzrkCode' 					#rzrk代码
-'UniCode' 					#统一规则代码
-'CreateDate' 				#上市日期(期货)
-'OpenDate' 					#IPO日期(股票)
-'ExpireDate' 				#退市日或者到期日
-'PreClose' 					#前收盘价格
-'SettlementPrice' 			#前结算价格
-'UpStopPrice' 				#当日涨停价
-'DownStopPrice' 			#当日跌停价
-'FloatVolume' 				#流通股本
-'TotalVolume' 				#总股本
-'AccumulatedInterest' 		#自上市付息日起的累积未付利息额(债券)
-'LongMarginRatio' 			#多头保证金率
-'ShortMarginRatio' 			#空头保证金率
-'PriceTick' 				#最小变价单位
-'VolumeMultiple' 			#合约乘数(对期货以外的品种,默认是1)
-'MainContract' 				#主力合约标记,1、2、3分别表示第一主力合约,第二主力合约,第三主力合约
-'MaxMarketOrderVolume' 		#市价单最大下单量
-'MinMarketOrderVolume' 		#市价单最小下单量
-'MaxLimitOrderVolume' 		#限价单最大下单量
-'MinLimitOrderVolume' 		#限价单最小下单量
-'MaxMarginSideAlgorithm' 	#上期所大单边的处理算法
-'DayCountFromIPO' 			#自IPO起经历的交易日总数
-'LastVolume' 				#昨日持仓量
-'InstrumentStatus' 			#合约停牌状态
-'IsTrading' 				#合约是否可交易
-'IsRecent' 					#是否是近月合约
-'IsContinuous' 				#是否是连续合约
-'bNotProfitable' 			#是否非盈利状态
-'bDualClass' 				#是否同股不同权
-'ContinueType' 				#连续合约类型
-'secuCategory' 				#证券分类
-'secuAttri' 				#证券属性
-'MaxMarketSellOrderVolume' 	#市价卖单最大单笔下单量
-'MinMarketSellOrderVolume' 	#市价卖单最小单笔下单量
-'MaxLimitSellOrderVolume' 	#限价卖单最大单笔下单量
-'MinLimitSellOrderVolume' 	#限价卖单最小单笔下单量
-'MaxFixedBuyOrderVol' 		#盘后定价委托数量的上限(买)
-'MinFixedBuyOrderVol' 		#盘后定价委托数量的下限(买)
-'MaxFixedSellOrderVol' 		#盘后定价委托数量的上限(卖)
-'MinFixedSellOrderVol' 		#盘后定价委托数量的下限(卖)
-'HSGTFlag' 					#标识港股是否为沪港通或深港通标的证券。沪港通:0-非标的,1-标的,2-历史标的;深港通:0-非标的,3-标的,4-历史标的,5-是沪港通也是深港通
-'BondParValue' 				#债券面值
-'QualifiedType' 			#投资者适当性管理分类
-'PriceTickType' 			#价差类别(港股用),1-股票,3-债券,4-期权,5-交易所买卖基金
-'tradingStatus' 			#交易状态
-'OptUnit' 					#期权合约单位
-'MarginUnit' 				#期权单位保证金
-'OptUndlCode' 				#期权标的证券代码或可转债正股标的证券代码
-'OptUndlMarket' 			#期权标的证券市场或可转债正股标的证券市场
-'OptLotSize' 				#期权整手数
-'OptExercisePrice' 			#期权行权价或可转债转股价
-'NeeqExeType' 				#全国股转转让类型,1-协议转让方式,2-做市转让方式,3-集合竞价+连续竞价转让方式(当前全国股转并未实现),4-集合竞价转让
-'OptExchFixedMargin' 		#交易所期权合约保证金不变部分
-'OptExchMiniMargin' 		#交易所期权合约最小保证金
-'Ccy' 						#币种
-'IbSecType' 				#IB安全类型,期货或股票
-'OptUndlRiskFreeRate' 		#期权标的无风险利率
-'OptUndlHistoryRate' 		#期权标的历史波动率
-'EndDelivDate' 				#期权行权终止日
-'RegisteredCapital' 		#注册资本(单位:百万)
-'MaxOrderPriceRange' 		#最大有效申报范围
-'MinOrderPriceRange' 		#最小有效申报范围
-'VoteRightRatio' 			#同股同权比例
-'m_nMinRepurchaseDaysLimit' #最小回购天数
-'m_nMaxRepurchaseDaysLimit' #最大回购天数
-'DeliveryYear' 				#交割年份
-'DeliveryMonth' 			#交割月
-'ContractType' 				#标识期权,1-过期,2-当月,3-下月,4-下季,5-隔季,6-隔下季
-'ProductTradeQuota' 		#期货品种交易配额
-'ContractTradeQuota' 		#期货合约交易配额
-'ProductOpenInterestQuota' 	#期货品种持仓配额
-'ContractOpenInterestQuota' #期货合约持仓配额
-'ChargeType' 				#期货和期权手续费方式,0-未知,1-按元/手,2-按费率
-'ChargeOpen' 				#开仓手续费率,-1表示没有
-'ChargeClose' 				#平仓手续费率,-1表示没有
-'ChargeClose'				#平仓手续费率,-1表示没有
-'ChargeTodayOpen'			#开今仓(日内开仓)手续费率,-1表示没有
-'ChargeTodayClose'			#平今仓(日内平仓)手续费率,-1表示没有
-'OptionType'				#期权类型,-1为非期权,0为期权认购,1为期权认沽
-'OpenInterestMultiple'		#交割月持仓倍数
-

代码示例

时间戳转换

import time
-def conv_time(ct):
-    '''
-    conv_time(1476374400000) --> '20161014000000.000'
-    '''
-    local_time = time.localtime(ct / 1000)
-    data_head = time.strftime('%Y%m%d%H%M%S', local_time)
-    data_secs = (ct - int(ct)) * 1000
-    time_stamp = '%s.%03d' % (data_head, data_secs)
-    return time_stamp
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/thinktrader_docs/nativeApi_xttrader.html b/reference/thinktrader_docs/nativeApi_xttrader.html deleted file mode 100644 index b425c51..0000000 --- a/reference/thinktrader_docs/nativeApi_xttrader.html +++ /dev/null @@ -1,504 +0,0 @@ - - - - - - - - - XtQuant.Xttrade 交易模块 | 迅投知识库 - - - - -

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

快速入门

创建策略

#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_trade(self, trade):
-        """
-        成交变动推送
-        :param trade: XtTrade对象
-        :return:
-        """
-        print("on trade callback")
-        print(trade.account_id, trade.stock_code, trade.order_id)
-    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_async(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运行逻辑

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实例

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

注册回调类

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

准备API环境

start()
-
  • 释义
    • 启动交易线程,准备交易所需的环境
  • 参数
  • 返回
  • 备注
  • 示例
# 启动交易线程
-#xt_trader为XtQuant API实例对象
-xt_trader.start()
-

创建连接

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

停止运行

stop()
-
  • 释义
    • 停止API接口
  • 参数
  • 返回
  • 备注
  • 示例
#xt_trader为XtQuant API实例对象
-xt_trader.stop()
-

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

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

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

set_relaxed_response_order_enabled(enabled)
-
  • 释义

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

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

  • 备注

    • 如果开启,在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 资金账号
  • 返回
    • 订阅结果信息,订阅成功返回0,订阅失败返回-1
  • 备注
  • 示例
    • 订阅资金账号1000000365
account = StockAccount('1000000365')
-#xt_trader为XtQuant API实例对象
-subscribe_result = xt_trader.subscribe(account)
-

反订阅账号信息

unsubscribe(account)
-
  • 释义
    • 反订阅账号信息
  • 参数
    • account - StockAccount 资金账号
  • 返回
    • 反订阅结果信息,订阅成功返回0,订阅失败返回-1
  • 备注
  • 示例
    • 订阅资金账号1000000365
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 资金账号
    • 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'
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)
-
  • 释义
    • 对股票进行异步下单操作,异步下单接口如果正常返回了下单请求序号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'
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 资金账号
    • order_id - int 同步下单接口返回的订单编号,对于期货来说,是order结构中的order_sysid字段
  • 返回
    • 返回是否成功发出撤单指令,0: 成功, -1: 表示撤单失败
  • 备注
  • 示例
    • 股票资金账号1000000365对订单编号为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 资金账号
    • market - int 交易市场
    • order_sysid - str 券商柜台的合同编号
  • 返回
    • 返回是否成功发出撤单指令,0: 成功, -1: 表示撤单失败
  • 备注
  • 示例
    • 股票资金账号1000000365对柜台合同编号为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 资金账号
    • order_id - int 下单接口返回的订单编号,对于期货来说,是order结构中的order_sysid
  • 返回
    • 返回撤单请求序号, 成功委托后的撤单请求序号为大于0的正整数, 如果为-1表示委托失败
  • 备注
    • 如果失败,则通过撤单失败主推接口返回撤单失败信息
  • 示例
    • 股票资金账号1000000365对订单编号为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 资金账号
    • market - int 交易市场
    • order_sysid - str 券商柜台的合同编号
  • 返回
    • 返回撤单请求序号, 成功委托后的撤单请求序号为大于0的正整数, 如果为-1表示委托失败
  • 备注
    • 如果失败,则通过撤单失败主推接口返回撤单失败信息
  • 示例
    • 股票资金账号1000000365对柜台合同编号为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)
-
  • 释义
    • 资金划拨
  • 参数
    • account - StockAccount 资金账号
    • transfer_direction - int 划拨方向,见数据字典划拨方向(transfer_direction)字段说明
    • price - float 划拨金额
  • 返回
    • (success, msg)
      • success - bool 划拨操作是否成功
      • msg - str 反馈信息

外部交易数据录入

sync_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 结果反馈信息
  • 示例

    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 资金账号
  • 返回
  • 备注
    • 返回None表示查询失败
  • 示例
    • 查询股票资金账号1000000365对应的资产数据
account = StockAccount('1000000365')
-#xt_trader为XtQuant API实例对象
-asset = xt_trader.query_stock_asset(account)
-

委托查询

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

成交查询

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

持仓查询

query_stock_positions(account)
-
  • 释义
    • 查询资金账号对应的持仓
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者当日持仓列表为空
  • 示例
    • 查询股票资金账号1000000365对应的最新持仓
account = StockAccount('1000000365')
-#xt_trader为XtQuant API实例对象
-positions = xt_trader.query_stock_positions(account)
-

期货持仓统计查询

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

信用查询接口

信用资产查询

query_credit_detail(account)
-
  • 释义
    • 查询信用资金账号对应的资产
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败
    • 通常情况下一个资金账号只有一个详细信息数据
  • 示例
    • 查询信用资金账号1208970161对应的资产信息
account = StockAccount('1208970161', 'CREDIT')
-#xt_trader为XtQuant API实例对象
-datas = xt_trader.query_credit_detail(account)
-

负债合约查询

query_stk_compacts(account)
-
  • 释义
    • 查询资金账号对应的负债合约
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者负债合约列表为空
  • 示例
    • 查询信用资金账号1208970161对应的负债合约
account = StockAccount('1208970161', 'CREDIT')
-#xt_trader为XtQuant API实例对象
-datas = xt_trader.query_stk_compacts(account)
-

融资融券标的查询

query_credit_subjects(account)
-
  • 释义
    • 查询资金账号对应的融资融券标的
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者融资融券标的列表为空
  • 示例
    • 查询信用资金账号1208970161对应的融资融券标的
account = StockAccount('1208970161', 'CREDIT')
-#xt_trader为XtQuant API实例对象
-datas = xt_trader.query_credit_subjects(account)
-

可融券数据查询

query_credit_slo_code(account)
-
  • 释义
    • 查询资金账号对应的可融券数据
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者可融券数据列表为空
  • 示例
    • 查询信用资金账号1208970161对应的可融券数据
account = StockAccount('1208970161', 'CREDIT')
-#xt_trader为XtQuant API实例对象
-datas = xt_trader.query_credit_slo_code(account)
-

标的担保品查询

query_credit_assure(account)
-
  • 释义
    • 查询资金账号对应的标的担保品
  • 参数
    • account - StockAccount 资金账号
  • 返回
  • 备注
    • None表示查询失败或者标的担保品列表为空
  • 示例
    • 查询信用资金账号1208970161对应的标的担保品
account = StockAccount('1208970161', 'CREDIT')
-#xt_trader为XtQuant API实例对象
-datas = xt_trader.query_credit_assure(account)
-

其他查询接口

新股申购额度查询

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

当日新股信息查询

query_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 发行价
      • 返回值示例

        {'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 账号信息列表

      • [ XtAccountInfo ]
  • 备注

账号状态查询

query_account_status()
-
  • 释义

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

  • 返回

    • list 账号状态列表

      • [ XtAccountStatus ]
  • 备注

普通柜台资金查询

query_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 负债

普通柜台持仓查询

query_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 申赎可用量

通用数据导出

export_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 结果反馈信息
  • 示例

    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接口导出数据后再读取其中的数据内容,读取完毕后删除导出的文件
  • 参数

    同export_data

  • 返回

    • result - dict 数据信息
  • 示例

    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)
-
  • 释义
    • 券源行情查询
  • 参数
    • 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 最低期限天数

库存券约券申请

smt_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表示发起申请失败
  • 示例

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)
-
  • 释义
    • 约券合约查询
  • 参数
    • 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":专项券

回调类

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_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_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_order(data)
-
  • 释义
    • 委托信息变动推送,例如已成交数量,委托状态变化等
  • 参数
  • 返回
  • 备注

成交信息推送

on_stock_trade(data)
-

下单失败信息推送

on_order_error(data)
-

撤单失败信息推送

on_cancel_error(data)
-

异步下单回报推送

on_order_stock_async_response(data)
-

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

on_smt_appointment_async_response(data)
-
上次更新:
邀请注册送VIP优惠券
分享下方的内容给好友、QQ群、微信群,好友注册您即可获得VIP优惠券
玩转qmt,上迅投qmt知识库
- - - diff --git a/reference/xtquant_big_convert/.gitignore b/reference/xtquant_big_convert/.gitignore new file mode 100644 index 0000000..739e14d --- /dev/null +++ b/reference/xtquant_big_convert/.gitignore @@ -0,0 +1,28 @@ +__pycache__/ +*.py[cod] +*.pyo +*.pyd +.Python +.venv/ +venv/ +env/ +.pytest_cache/ +.mypy_cache/ +.ruff_cache/ +build/ +dist/ +*.egg-info/ + +# Local runtime secrets. Keep real account ids, Redis passwords and QMT paths out of git. +# bigqmt_signal_trader_local_config.py +# src/bigqmt_signal_trader_local_config.py +# bigqmt_signal_trader_client_config.py +# src/bigqmt_signal_trader_client_config.py +*.local.py +*.log +*.pid + +# Generated evidence from standalone/QMT backtest bridge runs. +backtest_runs/ +qmt_backtest_runs/ +.workbuddy/ diff --git a/reference/xtquant_big_convert/bench_latency.py b/reference/xtquant_big_convert/bench_latency.py deleted file mode 100644 index f62193d..0000000 --- a/reference/xtquant_big_convert/bench_latency.py +++ /dev/null @@ -1,107 +0,0 @@ -# coding: utf-8 -"""BigQMT Redis RPC latency benchmark. - -Measures end-to-end latency for ping (no QMT API call) and get_full_tick -(real ContextInfo call) to separate transport cost from API cost. -""" -import os -import statistics -import time - -import redis -from bigqmt_signal_trader.redis_rpc import call_redis_rpc - - -def _load_redis_config(): - """Pull connection details from the local client config or env vars. - - Never hardcode secrets in the repo. - """ - try: - from bigqmt_signal_trader.xtquant_compat import load_client_config - - cfg = load_client_config() - rc = dict(cfg.get("redis_config") or {}) - rc.setdefault("host", os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1")) - rc.setdefault("port", int(os.environ.get("BIGQMT_REDIS_PORT", "6379"))) - rc.setdefault("db", int(os.environ.get("BIGQMT_REDIS_DB", "5"))) - return { - "host": rc.get("host"), - "port": int(rc.get("port")), - "db": int(rc.get("db")), - "username": rc.get("username") or None, - "password": rc.get("password") or None, - "socket_timeout": 8, - } - except Exception: - return { - "host": os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1"), - "port": int(os.environ.get("BIGQMT_REDIS_PORT", "6379")), - "db": int(os.environ.get("BIGQMT_REDIS_DB", "5")), - "socket_timeout": 8, - } - - -ACCOUNT = os.environ.get("BIGQMT_ACCOUNT_ID", "") -REDIS = _load_redis_config() - - -def bench(r, method, params, n=20, timeout=6): - lats = [] - errors = 0 - for i in range(n): - t0 = time.time() - try: - resp = call_redis_rpc(r, ACCOUNT, method, params, timeout_seconds=timeout) - dt = (time.time() - t0) * 1000 - if resp.get("ok"): - lats.append(dt) - else: - errors += 1 - if errors <= 2: - print(" %s #%d error: %s" % (method, i, resp.get("error", "")[:120])) - except Exception as e: - errors += 1 - if errors <= 2: - print(" %s #%d exc: %s" % (method, i, e)) - if not lats: - print("%-18s: ALL FAILED (%d errors)" % (method, errors)) - return - lats.sort() - p50 = statistics.median(lats) - p95 = lats[int(len(lats) * 0.95)] if len(lats) >= 20 else lats[-1] - print( - "%-18s: n=%d ok=%d fail=%d min=%.0f p50=%.0f p95=%.0f max=%.0f avg=%.0f ms" - % ( - method, - len(lats), - len(lats), - errors, - min(lats), - p50, - p95, - max(lats), - statistics.mean(lats), - ) - ) - - -def main(): - r = redis.Redis(**REDIS) - # warmup - try: - call_redis_rpc(r, ACCOUNT, "ping", {}, timeout_seconds=6) - print("warmup ping ok\n") - except Exception as e: - print("warmup FAILED: %s\n" % e) - return - - print("=== latency benchmark (20 calls each) ===") - bench(r, "ping", {}, n=20) - bench(r, "get_full_tick", {"codes": ["000001.SZ"]}, n=20) - bench(r, "get_full_tick", {"codes": ["000001.SZ", "600000.SH", "000333.SZ"]}, n=20) - bench(r, "get_instrument", {"code": "000001.SZ"}, n=20) - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/bench_transports.py b/reference/xtquant_big_convert/bench_transports.py deleted file mode 100644 index 7af87af..0000000 --- a/reference/xtquant_big_convert/bench_transports.py +++ /dev/null @@ -1,151 +0,0 @@ -# coding: utf-8 -"""Compare end-to-end RPC latency across transports. - -Runs the same ping workload through: - * Redis (real server, the production path) via call_redis_rpc - * ZMQ (local tcp loopback, the low-latency path) via ZmqTransport - -Prints a side-by-side min/p50/p90/p99/max comparison. The ZMQ leg spins up a -local in-process server so no QMT process is needed for the comparison. -""" -import argparse -import os -import socket -import statistics -import time -import uuid - -import redis - -from bigqmt_signal_trader.redis_rpc import call_redis_rpc -from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - -def _load_redis_config(): - """Pull connection details from the local client config or env vars.""" - try: - from bigqmt_signal_trader.xtquant_compat import load_client_config - - cfg = load_client_config() - rc = dict(cfg.get("redis_config") or {}) - rc.setdefault("host", os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1")) - rc.setdefault("port", int(os.environ.get("BIGQMT_REDIS_PORT", "6379"))) - rc.setdefault("db", int(os.environ.get("BIGQMT_REDIS_DB", "5"))) - return { - "host": rc.get("host"), - "port": int(rc.get("port")), - "db": int(rc.get("db")), - "username": rc.get("username") or None, - "password": rc.get("password") or None, - "socket_timeout": 8, - } - except Exception: - return { - "host": os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1"), - "port": int(os.environ.get("BIGQMT_REDIS_PORT", "6379")), - "db": int(os.environ.get("BIGQMT_REDIS_DB", "5")), - "socket_timeout": 8, - } - - -REDIS = _load_redis_config() -ACCOUNT = os.environ.get("BIGQMT_ACCOUNT_ID", "") - - -def _free_port(): - s = socket.socket() - s.bind(("127.0.0.1", 0)) - port = s.getsockname()[1] - s.close() - return port - - -def _stats(name, lats): - lats = sorted(lats) - n = len(lats) - print( - "%-8s n=%d min=%.2f p50=%.2f p90=%.2f p99=%.2f max=%.2f avg=%.2f ms" - % ( - name, - n, - min(lats), - statistics.median(lats), - lats[int(n * 0.9)], - lats[int(n * 0.99)] if n > 1 else lats[-1], - max(lats), - statistics.mean(lats), - ) - ) - - -def bench_redis(n): - r = redis.Redis(**REDIS) - # warmup + connectivity - try: - call_redis_rpc(r, ACCOUNT, "ping", {}, timeout_seconds=6) - except Exception as e: - print("Redis server not reachable, skipping redis leg: %s" % e) - return - lats = [] - for _ in range(n): - t0 = time.time() - call_redis_rpc(r, ACCOUNT, "ping", {}, timeout_seconds=6) - lats.append((time.time() - t0) * 1000) - _stats("redis", lats) - - -def bench_zmq(n): - port = _free_port() - addr = "tcp://127.0.0.1:%d" % port - - def on_req(req): - return { - "schema_version": 1, - "request_id": req["request_id"], - "account_id": "zmq", - "method": req["method"], - "ok": True, - "data": {"pong": True}, - "error": "", - "handled_at": "now", - } - - server = ZmqTransport(bind_address=addr, account_id="zmq", recv_timeout_seconds=0.3) - server.start_receiving(on_req, background_threads=True) - time.sleep(0.3) - client = ZmqTransport(connect_address=addr, account_id="zmq") - time.sleep(0.2) - lats = [] - for _ in range(n): - req = { - "schema_version": 1, - "request_id": uuid.uuid4().hex, - "account_id": "zmq", - "method": "ping", - "params": {}, - "reply_channel": "", - "reply_list": "", - "reply_key": "", - "ttl_seconds": 5, - } - t0 = time.time() - client.send_request(req, timeout_seconds=3.0) - lats.append((time.time() - t0) * 1000) - _stats("zmq", lats) - server.stop() - client.stop() - - -def main(): - ap = argparse.ArgumentParser() - ap.add_argument("-n", "--count", type=int, default=100, help="requests per transport") - ap.add_argument("--skip-redis", action="store_true", help="skip the redis leg") - args = ap.parse_args() - print("=== transport latency comparison (n=%d each) ===" % args.count) - if not args.skip_redis: - bench_redis(args.count) - bench_zmq(args.count) - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/bench_zmq_spike.py b/reference/xtquant_big_convert/bench_zmq_spike.py deleted file mode 100644 index 01ad410..0000000 --- a/reference/xtquant_big_convert/bench_zmq_spike.py +++ /dev/null @@ -1,51 +0,0 @@ -# coding: utf-8 -"""Measure ZMQ GIL-spike rate at different request rates. - -Sends N get_full_tick requests at a fixed interval, records per-call -latency, reports how many exceed thresholds (50/200/500/1000ms). -""" -import sys -import time - -sys.path.insert(0, r"D:\gjzqqmt\xtquant_big_convert\src") -sys.path.insert(0, r"D:\国金证券QMT交易端_lemo\python") - -import bigqmt_signal_trader.xtquant_compat as compat - -compat.configure() -client = compat.get_default_client() -print("account:", client.account_id, "| transport:", client.transport_name) -print("=" * 60) - -N = 40 -INTERVAL_MS = 50 # 请求间隔 50ms = 20 QPS - -latencies = [] -for i in range(N): - t0 = time.time() - try: - client.call("get_full_tick", {"codes": ["000001.SZ"]}) - ms = (time.time() - t0) * 1000 - latencies.append(ms) - except Exception as e: - ms = (time.time() - t0) * 1000 - latencies.append(ms) - print(" [%2d] FAIL %.0fms %s" % (i, ms, str(e)[:40])) - # 控制频率 - elapsed = (time.time() - t0) - sleep = max(0, INTERVAL_MS / 1000.0 - elapsed) - if sleep > 0: - time.sleep(sleep) - -latencies.sort() -n = len(latencies) -print("\n=== %d requests @ %dms interval (%.0f QPS) ===" % (n, INTERVAL_MS, 1000.0/INTERVAL_MS)) -print("min=%.1f p50=%.1f p90=%.1f p99=%.1f max=%.1f" % ( - latencies[0], latencies[n//2], latencies[int(n*0.9)], latencies[int(n*0.99)], latencies[-1])) - -# 尖峰分布 -thresholds = [10, 50, 100, 200, 500, 1000] -print("\n=== 延迟分布 ===") -for t in thresholds: - cnt = sum(1 for l in latencies if l > t) - print(" >%5dms : %2d / %d (%.0f%%)" % (t, cnt, n, 100.0*cnt/n)) diff --git a/reference/xtquant_big_convert/bigqmt_no_redis/DRYRUN_no_redis.py b/reference/xtquant_big_convert/bigqmt_no_redis/DRYRUN_no_redis.py deleted file mode 100644 index 4e469b5..0000000 --- a/reference/xtquant_big_convert/bigqmt_no_redis/DRYRUN_no_redis.py +++ /dev/null @@ -1,252 +0,0 @@ -#coding:gbk -"""QMT bridge entry (no-redis version). - -Same file-loader pattern as BIGQMT_REDIS_DRYRUN, but the RPC transport is ZMQ -only -- no redis imports anywhere. This version loads the no-redis zmq transport -(bigqmt_no_redis/zmq_transport.py) which inlines all encoding helpers and drops -redis-based service discovery, so it loads cleanly in QMT sandboxes that reject -`import redis` or any redis-named module. - -Use this when your QMT environment cannot import the redis package (e.g. broker -whitelist blocks it) or when you want zero redis dependency. - -Config: set "transport": "zmq" in bigqmt_signal_trader_local_config.py (the -no-redis runtime forces zmq regardless). Redis config fields are ignored. -""" -import builtins as _builtins -import importlib as _importlib -import os -import sys -import types - - -_LOCAL_ROOTS = ( - "bigqmt_signal_trader", - "bigqmt_signal_trader_strategy", - "bigqmt_signal_trader_redis_rpc_runtime", - "bigqmt_signal_trader_local_config", - "bigqmt_no_redis", -) -_ORIGINAL_IMPORT = _builtins.__import__ -_ORIGINAL_IMPORT_MODULE = _importlib.import_module -_ORIGINAL_RELOAD = _importlib.reload - - -def _known_qmt_python_dir(): - # Find the QMT python dir from sys.path instead of a hardcoded path, so - # the bridge loads regardless of broker install location or launch mode. - for p in sys.path: - if p and r"\python" in p and os.path.isdir(p): - return p - return "" - - -try: - _SOURCE_ROOT = os.path.dirname(os.path.abspath(__file__)) -except Exception: - _SOURCE_ROOT = _known_qmt_python_dir() -if not _SOURCE_ROOT: - _SOURCE_ROOT = _known_qmt_python_dir() - - -def _is_local_module(name): - return any(name == root or name.startswith(root + ".") for root in _LOCAL_ROOTS) - - -def _resolve_name(name, module_globals, level): - if not level: - return name - package = (module_globals or {}).get("__package__") or (module_globals or {}).get("__name__", "") - if not package: - raise ImportError("relative import without package") - for unused in range(level - 1): - if "." not in package: - raise ImportError("relative import beyond top-level package") - package = package.rsplit(".", 1)[0] - return package + ("." + name if name else "") - - -def _find_local_source(name): - relative = name.replace(".", os.sep) - dirs = [] - if _SOURCE_ROOT: - dirs.append(_SOURCE_ROOT) - for p in sys.path: - if p and os.path.isdir(p) and p not in dirs: - dirs.append(p) - for d in dirs: - package_init = os.path.join(d, relative, "__init__.py") - if os.path.isfile(package_init): - return package_init, True - module_file = os.path.join(d, relative + ".py") - if os.path.isfile(module_file): - return module_file, False - raise ModuleNotFoundError("local source not found: %s" % name, name=name) - - -def _set_parent_attribute(name, module): - if "." not in name: - return - parent_name, child_name = name.rsplit(".", 1) - parent = _load_local_module(parent_name) - setattr(parent, child_name, module) - - -def _load_local_module(name): - existing = sys.modules.get(name) - if existing is not None: - return existing - source_path, is_package = _find_local_source(name) - if "." in name: - _load_local_module(name.rsplit(".", 1)[0]) - module = types.ModuleType(name) - module.__file__ = source_path - module.__package__ = name if is_package else name.rpartition(".")[0] - if is_package: - module.__path__ = [os.path.dirname(source_path)] - module_builtins = dict(_builtins.__dict__) - module_builtins["__import__"] = _local_import - module.__dict__["__builtins__"] = module_builtins - module.__dict__["__bigqmt_load_local_module"] = _load_local_module - sys.modules[name] = module - if name == "bigqmt_signal_trader": - return module - try: - with open(source_path, "rb") as source_file: - source = source_file.read() - exec(compile(source, source_path, "exec"), module.__dict__) - except Exception: - sys.modules.pop(name, None) - raise - _set_parent_attribute(name, module) - return module - - -def _local_import(name, module_globals=None, module_locals=None, fromlist=(), level=0): - absolute_name = _resolve_name(name, module_globals, level) - if not _is_local_module(absolute_name): - return _ORIGINAL_IMPORT(name, module_globals, module_locals, fromlist, level) - module = _load_local_module(absolute_name) - for child in fromlist or (): - if child != "*": - try: - _load_local_module(absolute_name + "." + child) - except ModuleNotFoundError: - pass - if fromlist: - return module - return _load_local_module(absolute_name.split(".", 1)[0]) - - -def _local_import_module(name, package=None): - if _is_local_module(name): - return _load_local_module(name) - return _ORIGINAL_IMPORT_MODULE(name, package) - - -def _local_reload(module): - if _is_local_module(getattr(module, "__name__", "")): - return _load_local_module(module.__name__) - return _ORIGINAL_RELOAD(module) - - -def _clear_local_modules(): - for name in list(sys.modules): - if _is_local_module(name): - sys.modules.pop(name, None) - - -def _stop_previous_rpc_service(): - """Release the previous QMT strategy's socket before clearing its module.""" - previous = sys.modules.get("bigqmt_signal_trader_strategy") - reset = getattr(previous, "reset_app", None) - if not callable(reset): - return - try: - reset() - print("[bigqmt_shell] previous rpc service stopped") - except Exception as exc: - print("[bigqmt_shell] previous rpc service stop failed: %s" % exc) - - -_stop_previous_rpc_service() -_clear_local_modules() -_importlib.import_module = _local_import_module -_importlib.reload = _local_reload -print("[bigqmt_shell] importlib entry source_root=%s" % _SOURCE_ROOT) - - -def _fallback_account_id(): - for name in ("BIGQMT_ACCOUNT_ID", "account", "account_id", "accountID"): - value = globals().get(name) - if value: - return str(value) - return "" - - -try: - _local_import("bigqmt_signal_trader.adapters.market_bigqmt", globals(), fromlist=("*",)) - _local_import("bigqmt_signal_trader.adapters.order_bigqmt", globals(), fromlist=("*",)) - _local_import("bigqmt_signal_trader.adapters.position_bigqmt", globals(), fromlist=("*",)) - _strategy = _local_import("bigqmt_signal_trader_strategy", globals(), fromlist=("*",)) - _strategy.reset_app() -except Exception as bridge_preload_error: - print("[bigqmt_shell] bridge preload failed: %s" % bridge_preload_error) - -_runtime = _local_import("bigqmt_signal_trader_redis_rpc_runtime", globals(), fromlist=("*",)) - - -def _load_local_config(): - return _local_import("bigqmt_signal_trader_local_config", globals(), fromlist=("*",)) - - -try: - _config = _load_local_config() - BIGQMT_REDIS_CONFIG = getattr(_config, "BIGQMT_REDIS_CONFIG", {}) - # Force zmq transport (this is the no-redis version). - BIGQMT_REDIS_CONFIG = dict(BIGQMT_REDIS_CONFIG or {}) - BIGQMT_REDIS_CONFIG["transport"] = "zmq" - BIGQMT_REDIS_CONFIG["rpc_background_threads"] = True - print("[bigqmt_shell] no-redis mode: transport=zmq background_threads=True") - _runtime.configure_runtime_redis(BIGQMT_REDIS_CONFIG) -except Exception as redis_config_error: - print("[bigqmt_shell] local redis config load failed: %s" % redis_config_error) - -try: - _config = _load_local_config() - BIGQMT_ACCOUNT_ID = getattr(_config, "BIGQMT_ACCOUNT_ID", "") - print("[bigqmt_shell] local account config loaded=%s" % bool(BIGQMT_ACCOUNT_ID)) - _runtime.configure_runtime_account(BIGQMT_ACCOUNT_ID) -except Exception as account_config_error: - print("[bigqmt_shell] local account config load failed: %s" % account_config_error) - account_id = _fallback_account_id() - if account_id: - _runtime.configure_runtime_account(account_id) - -try: - qmt_extra = {} - for function_name in ( - "get_history_trade_detail_data", "get_value_by_order_id", "get_last_order_id", - "get_ipo_data", "get_new_purchase_limit", "get_assure_contract", - "get_enable_short_contract", "get_unclosed_compacts", "get_closed_compacts", - "get_debt_contract", "get_option_subject_position", "get_comb_option", - "get_hkt_exchange_rate", "down_history_data", - ): - if function_name in globals(): - qmt_extra[function_name] = globals()[function_name] - print("[bigqmt_shell] down_history_data bound=%s" % ("down_history_data" in qmt_extra)) - _runtime.bind_runtime_api( - passorder_func=globals().get("passorder"), - cancel_func=globals().get("cancel"), - get_trade_detail_data_func=globals().get("get_trade_detail_data"), - extra_funcs=qmt_extra or None, - ) -except NameError: - pass - - -init = _runtime.init -handlebar = _runtime.handlebar -adjust = _runtime.adjust -order_callback = _runtime.order_callback -deal_callback = _runtime.deal_callback diff --git a/reference/xtquant_big_convert/bigqmt_no_redis/zmq_transport.py b/reference/xtquant_big_convert/bigqmt_no_redis/zmq_transport.py deleted file mode 100644 index 0cd834e..0000000 --- a/reference/xtquant_big_convert/bigqmt_no_redis/zmq_transport.py +++ /dev/null @@ -1,459 +0,0 @@ -"""ZeroMQ transport for the BigQMT RPC bridge (no-redis version). - -Same as bigqmt_signal_trader.transports.zmq_transport, but with the redis -dependencies inlined and the redis-based service discovery removed. This lets -the module load in QMT sandboxes that reject `import redis` or any module -whose name mentions redis. - -Design (unchanged from the redis version): - -* **Server** binds a ``ROUTER`` socket. Each inbound message arrives as - ``[identity, payload]``; the server remembers ``identity`` keyed by - ``request_id`` and replies with ``[identity, payload]`` so ZMQ routes the - response back to the originating client automatically. -* **Client** connects a ``DEALER`` socket (with a unique random identity), sends - ``[payload]``, then ``poll``/``recv`` for the response. -""" - -import base64 -import json -import queue -import threading -import time -import uuid - - -# --------------------------------------------------------------------------- -# Inlined encoding helpers (originally from bigqmt_signal_trader.adapters. -# redis_common and bigqmt_signal_trader.redis_rpc). Kept here so this module -# has zero imports from any redis-named module. -# --------------------------------------------------------------------------- - -SAFE_B64_PREFIX = "b64s:" -SAFE_B64_DIGIT_ENCODE = str.maketrans("0123456789", "!#$%&()*~?") -SAFE_B64_DIGIT_DECODE = str.maketrans("!#$%&()*~?", "0123456789") - - -def decode_text(value): - if isinstance(value, bytes): - return value.decode("utf-8") - return str(value) - - -def encode_rpc_request_payload(request): - """Encode request JSON so patched QMT clients do not inspect stock-code text.""" - raw = json.dumps(request, ensure_ascii=False).encode("utf-8") - encoded = base64.b64encode(raw).decode("ascii").translate(SAFE_B64_DIGIT_ENCODE) - return SAFE_B64_PREFIX + encoded - - -def decode_rpc_request_payload(text): - text = str(text) - if not text.startswith(SAFE_B64_PREFIX): - return text - encoded = text[len(SAFE_B64_PREFIX):].translate(SAFE_B64_DIGIT_DECODE) - return base64.b64decode(encoded.encode("ascii")).decode("utf-8") - - -# --------------------------------------------------------------------------- -# TransportError / TransportTimeout (inlined from transports.base -- kept here -# so this module is fully self-contained for QMT sandbox loading). -# --------------------------------------------------------------------------- - -class TransportError(RuntimeError): - pass - - -class TransportTimeout(TransportError): - pass - - -class RpcTransport: - """Minimal transport base (inlined subset of transports.base).""" - - def __init__(self, account_id="", print_prefix="[bigqmt_rpc]"): - self.account_id = str(account_id or "") - self.print_prefix = str(print_prefix or "[bigqmt_rpc]") - self._on_request = None - self._running = False - - def start_receiving(self, on_request): - self._on_request = on_request - self._running = True - - def stop(self): - self._running = False - self._on_request = None - - def deliver(self, request): - callback = self._on_request - if callback is None: - return None - try: - response = callback(request) - except Exception as exc: - import datetime as _dt - response = { - "schema_version": 1, - "request_id": str((request or {}).get("request_id") or ""), - "account_id": str((request or {}).get("account_id") or self.account_id or ""), - "method": str((request or {}).get("method") or ""), - "ok": False, - "data": None, - "error": "%s: %s" % (exc.__class__.__name__, exc), - "handled_at": _dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), - } - if response is not None: - try: - self.send_response(request, response) - except Exception: - pass - return response - - -# --------------------------------------------------------------------------- -# ZMQ transport -# --------------------------------------------------------------------------- - -# ZMQ does not support ipc:// on Windows (it trips a signaler abort), so the -# default endpoint is tcp loopback. The port is derived from the account_id so -# distinct accounts don't collide on the same port; override via config when -# needed. Base 15560 keeps it clear of common dev ports. -DEFAULT_ZMQ_HOST = "127.0.0.1" -DEFAULT_ZMQ_BASE_PORT = 15560 -DEFAULT_ZMQ_PORT_RANGE = 100 # derived port = base + (account_id_int mod range) - - -def _default_zmq_port(account_id): - """Derive a stable port from account_id so each account gets its own socket.""" - text = str(account_id or "") - digits = "".join(ch for ch in text if ch.isdigit()) - try: - offset = int(digits) % DEFAULT_ZMQ_PORT_RANGE if digits else 0 - except ValueError: - offset = 0 - return DEFAULT_ZMQ_BASE_PORT + offset - - -def _default_zmq_address(account_id, host=None): - host = host or DEFAULT_ZMQ_HOST - return "tcp://%s:%d" % (host, _default_zmq_port(account_id)) - - -def _loads(raw): - if isinstance(raw, dict): - return dict(raw) - text = decode_text(raw) - text = decode_rpc_request_payload(text) - return json.loads(text) - - -class ZmqTransport(RpcTransport): - """ZMQ ROUTER/DEALER transport (no-redis version). - - The same instance plays both roles depending on method called: - ``send_request`` acts as a client (DEALER connect), ``start_receiving`` + - ``send_response`` act as a server (ROUTER bind). A deployment normally uses - one instance per role (the QMT process is the server; the external client - is the client). - - Unlike the redis version, this one does NOT use redis-based service - discovery. The server binds the configured address exactly; the client - connects to the configured or derived address directly. - """ - - name = "zmq" - - def __init__( - self, - bind_address=None, - connect_address=None, - host=None, - port=None, - account_id="", - print_prefix="[bigqmt_rpc]", - io_threads=1, - recv_timeout_seconds=1.0, - server_hwm=10000, - client_linger_ms=0, - ): - super(ZmqTransport, self).__init__(account_id=account_id, print_prefix=print_prefix) - resolved_host = host or DEFAULT_ZMQ_HOST - if port is not None: - resolved_port = int(port) - else: - resolved_port = _default_zmq_port(account_id) - default_addr = "tcp://%s:%d" % (resolved_host, resolved_port) - self.bind_address = bind_address or default_addr - self.connect_address = connect_address - self.bind_host = resolved_host - self.base_port = resolved_port - self.io_threads = int(io_threads) - self.recv_timeout_seconds = float(recv_timeout_seconds) - self.server_hwm = int(server_hwm) - self.client_linger_ms = int(client_linger_ms) - - self._zmq = None # imported lazily - self._ctx = None - # server state - self._router = None - self._router_thread = None - self._actual_bind_address = None # set after start_receiving() - self._pending_identities = {} # request_id -> client identity bytes - self._identity_lock = threading.Lock() - self._response_queue = queue.Queue() - self._queued_response_count = 0 - self._sent_response_count = 0 - # client state - self._dealer = None - self._client_lock = threading.Lock() - - # -- construction helper ---------------------------------------------- - @classmethod - def from_config(cls, config, account_id="", print_prefix="[bigqmt_rpc]"): - config = dict(config or {}) - return cls( - bind_address=config.get("bind_address"), - connect_address=config.get("connect_address"), - host=config.get("host"), - port=config.get("port"), - account_id=config.get("account_id", account_id), - print_prefix=print_prefix, - io_threads=int(config.get("io_threads", 1)), - recv_timeout_seconds=float(config.get("recv_timeout_seconds", 1.0)), - server_hwm=int(config.get("server_hwm", 10000)), - client_linger_ms=int(config.get("client_linger_ms", 0)), - ) - - # -- shared zmq context ----------------------------------------------- - def _ensure_zmq(self): - if self._zmq is None: - try: - import zmq # noqa: F401 - except ImportError as exc: # pragma: no cover - depends on env - raise TransportError( - "pyzmq is required for the zmq transport: %s" % exc - ) - self._zmq = zmq - if self._ctx is None: - self._ctx = self._zmq.Context.instance(self.io_threads) - return self._zmq, self._ctx - - # -- server side ------------------------------------------------------ - def _bind_configured_address(self): - """Bind exactly one configured address and reject duplicate servers.""" - zmq, ctx = self._ensure_zmq() - sock = ctx.socket(zmq.ROUTER) - sock.setsockopt(zmq.RCVHWM, self.server_hwm) - sock.setsockopt(zmq.SNDHWM, self.server_hwm) - sock.setsockopt(zmq.RCVTIMEO, int(self.recv_timeout_seconds * 1000)) - try: - sock.bind(self.bind_address) - except self._zmq.ZMQError as exc: - try: - sock.close(linger=0) - except Exception: - pass - if getattr(exc, "errno", None) == zmq.EADDRINUSE: - raise TransportError( - "ZMQ_BIND_CONFLICT address=%s; another bridge instance " - "already owns the configured endpoint" % self.bind_address - ) - raise - self._router = sock - self._actual_bind_address = self.bind_address - - def start_receiving(self, on_request, background_threads=True): - super(ZmqTransport, self).start_receiving(on_request) - zmq, ctx = self._ensure_zmq() - self._bind_configured_address() - bound = self._actual_bind_address or self.bind_address - if not background_threads: - print( - "%s zmq bound=%s background_threads=False" - % (self.print_prefix, bound) - ) - return - self._router_thread = threading.Thread( - target=self._router_loop, name="bigqmt-zmq-rpc", daemon=True - ) - self._router_thread.start() - print( - "%s zmq started bound=%s" % (self.print_prefix, self.bind_address) - ) - - def _router_loop(self): - try: - while self._running: - self._drain_response_queue() - request = self._receive_request() - if request is not None: - self._deliver_request(request) - finally: - # Close the ROUTER socket on the thread that owns it. On Windows, - # closing a ZMQ socket from a different thread trips a signaler - # assertion (abort); closing it here is safe because this thread - # created and exclusively used it. - try: - self._router.close(linger=0) - except Exception: - pass - self._router = None - - def _receive_request(self, flags=0): - try: - frames = self._router.recv_multipart(flags=flags) - except self._zmq.Again: - return None - except Exception as exc: - if self._running: - print("%s zmq recv failed: %s" % (self.print_prefix, exc)) - if not flags: - time.sleep(0.5) - return None - if len(frames) < 2: - return None - identity, payload = frames[0], frames[-1] - try: - request = _loads(payload) - except Exception as exc: - print("%s zmq decode failed: %s" % (self.print_prefix, exc)) - return None - request_id = str(request.get("request_id") or uuid.uuid4().hex) - with self._identity_lock: - self._pending_identities[request_id] = identity - return request - - def _deliver_request(self, request): - started = time.perf_counter() - try: - self.deliver(request) - except Exception as exc: - print("%s zmq deliver failed: %s" % (self.print_prefix, exc)) - elapsed_ms = (time.perf_counter() - started) * 1000.0 - if elapsed_ms > 50.0: - print("%s zmq slow handler method=%s %.0fms" - % (self.print_prefix, request.get("method"), elapsed_ms)) - - def _drain_response_queue(self): - while True: - try: - identity, payload = self._response_queue.get_nowait() - except queue.Empty: - return - try: - self._router.send_multipart([identity, payload]) - self._sent_response_count += 1 - if self._sent_response_count <= 5: - print("%s zmq queued response sent" % self.print_prefix) - except Exception as exc: - print("%s zmq send failed: %s" % (self.print_prefix, exc)) - - def send_response(self, request, response): - if self._router is None: - raise TransportError("zmq server socket is not bound") - request_id = str( - response.get("request_id") or request.get("request_id") or "" - ) - with self._identity_lock: - identity = self._pending_identities.pop(request_id, None) - if identity is None: - # No matching peer -- drop silently (client may have gone away). - return - payload = encode_rpc_request_payload(response).encode("utf-8") - if self._router_thread is not None and threading.current_thread() is not self._router_thread: - self._queued_response_count += 1 - if self._queued_response_count <= 5: - print("%s zmq response queued for router thread" % self.print_prefix) - self._response_queue.put((identity, payload)) - return - try: - self._router.send_multipart([identity, payload]) - except Exception as exc: - print("%s zmq send failed: %s" % (self.print_prefix, exc)) - - def drain_request_queue(self, max_items=20): - """Drain requests from the scheduled QMT thread when no receiver thread exists.""" - if self._router_thread is not None or self._router is None: - return 0 - processed = 0 - for _index in range(max(int(max_items), 0)): - request = self._receive_request(flags=self._zmq.NOBLOCK) - if request is None: - break - self._deliver_request(request) - processed += 1 - return processed - - # -- client side ------------------------------------------------------ - def _resolve_connect_address(self): - """Resolve the address to connect to. No redis discovery -- use explicit - connect_address, else derive from account_id.""" - if self.connect_address: - return self.connect_address - return _default_zmq_address(self.account_id) - - def _ensure_dealer(self): - zmq, ctx = self._ensure_zmq() - if self._dealer is None: - address = self._resolve_connect_address() - sock = ctx.socket(zmq.DEALER) - # Unique identity so ROUTER can route replies back to us. - sock.setsockopt(zmq.IDENTITY, uuid.uuid4().hex.encode("utf-8")[:16]) - sock.setsockopt(zmq.LINGER, self.client_linger_ms) - sock.connect(address) - self._dealer = sock - self.connect_address = address - return self._dealer - - def send_request(self, request, timeout_seconds, **_kwargs): - zmq = self._zmq or self._ensure_zmq()[0] - with self._client_lock: - dealer = self._ensure_dealer() - request = dict(request) - request.setdefault("request_id", uuid.uuid4().hex) - request_id = request["request_id"] - payload = encode_rpc_request_payload(request) - try: - dealer.send(payload.encode("utf-8")) - except Exception as exc: - raise TransportError("zmq send failed: %s" % exc) - deadline = time.time() + float(timeout_seconds) - poller = self._zmq.Poller() - poller.register(dealer, self._zmq.POLLIN) - while True: - remaining = deadline - time.time() - if remaining <= 0: - break - events = dict(poller.poll(timeout=int(remaining * 1000))) - if dealer in events: - frames = dealer.recv_multipart() - raw = frames[-1] - response = _loads(raw) - if response.get("request_id") == request_id: - return response - raise TransportTimeout("zmq rpc timeout: %s" % request.get("method")) - - # -- lifecycle -------------------------------------------------------- - def stop(self): - super(ZmqTransport, self).stop() - # Clear _running so the router loop exits; the loop closes its own - # socket (closing cross-thread trips a Windows signaler abort). - thread = self._router_thread - if thread is not None and thread.is_alive(): - thread.join(2.0) - if thread is None and self._router is not None: - try: - self._router.close(linger=0) - except Exception: - pass - self._router = None - self._router_thread = None - with self._client_lock: - if self._dealer is not None: - try: - self._dealer.close(linger=self.client_linger_ms) - except Exception: - pass - self._dealer = None - # Do NOT terminate the shared context -- other sockets/users may rely on it. diff --git a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/analyze_strategy.py b/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/analyze_strategy.py deleted file mode 100644 index 5d9c9c9..0000000 --- a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/analyze_strategy.py +++ /dev/null @@ -1,289 +0,0 @@ -# -*- coding: utf-8 -*- -"""miniQMT 策略静态分析:API 清单 / py3.6 违例 / 依赖 / 阻塞模式 / 可行性结论 - -用法: python analyze_strategy.py <策略.py> -输出: Markdown 报告到 stdout,同时写入 <策略>.conversion_report.md(UTF-8)。 -退出码恒为 0(报告内容判定可行性)。 -""" -import ast -import io -import os -import re -import sys - - -def _fix_console(): - """对齐 Windows 控制台码页,避免中文输出乱码。""" - if os.name != 'nt': - return - try: - import ctypes - cp = ctypes.windll.kernel32.GetConsoleOutputCP() - sys.stdout.reconfigure(encoding='utf-8' if cp == 65001 else 'gbk', - errors='replace') - except Exception: - pass - -# ---- 映射知识库:xtquant调用 -> (内置等价物, 状态) ---- -# 状态: auto=可直接映射 manual=需重构 blocked=不可转(给替代方案) -TRADER_MAP = { - 'order_stock': ('passorder(opType, 1101, account, code, prType, price, vol, strat, 2, uid, C)', 'auto'), - 'order_stock_async': ('passorder(...),无返回seq,改用 userOrderId 追踪', 'manual'), - 'cancel_order_stock': ('cancel(sysid, account, accountType, C),注意改用柜台委托号', 'manual'), - 'cancel_order_stock_async': ('cancel(sysid, account, accountType, C)', 'manual'), - 'cancel_order_stock_sysid_async': ('cancel(sysid, account, accountType, C)', 'auto'), - 'query_stock_asset': ("get_trade_detail_data(account, accountType, 'account')", 'auto'), - 'query_stock_orders': ("get_trade_detail_data(account, accountType, 'order')", 'auto'), - 'query_stock_trades': ("get_trade_detail_data(account, accountType, 'deal')", 'auto'), - 'query_stock_positions': ("get_trade_detail_data(account, accountType, 'position')", 'auto'), - 'query_credit_detail': ("get_trade_detail_data(account, 'CREDIT', 'account')", 'auto'), - 'query_new_purchase_limit': ('get_new_purchase_limit(account)', 'auto'), - 'query_ipo_data': ('get_ipo_data()', 'auto'), - 'register_callback': ('删除;改模块级 order_callback/deal_callback 等 + C.set_account', 'manual'), - 'subscribe': ('C.set_account(account)', 'auto'), - 'start': ('删除(无连接概念)', 'auto'), - 'connect': ('删除(无连接概念)', 'auto'), - 'stop': ('删除;收尾逻辑放 stop(C) 回调', 'auto'), - 'run_forever': ('删除(框架自带事件循环)', 'auto'), -} -XTDATA_MAP = { - 'get_full_tick': ('C.get_full_tick(codes)', 'auto'), - 'get_instrument_detail': ('C.get_instrument_detail(code)', 'auto'), - 'get_market_data': ('C.get_market_data_ex(...)', 'auto'), - 'get_market_data_ex': ('C.get_market_data_ex(...);勿在init中调', 'auto'), - 'get_local_data': ('C.get_market_data_ex(..., subscribe=False)', 'auto'), - 'subscribe_quote': ('C.subscribe_quote(code, period, callback=f)', 'auto'), - 'subscribe_whole_quote': ('C.subscribe_whole_quote(codes, callback)', 'auto'), - 'unsubscribe_quote': ('C.unsubscribe_quote(subID)', 'auto'), - 'get_trading_dates': ("C.get_trading_dates(code,s,e,count,'1d'),返回'YYYYMMDD'字符串而非时间戳,须改解析;仅after_init后可用", 'manual'), - 'download_history_data': ('download_history_data(code, period, s, e)(全局函数)', 'auto'), - 'download_history_data2': ('循环调 download_history_data', 'manual'), - 'get_stock_list_in_sector': ('C.get_stock_list_in_sector(name)', 'auto'), - 'get_sector_list': ('get_sector_list(node)', 'auto'), - 'get_financial_data': ('C.get_financial_data(...),签名有差异查 data_function.md', 'manual'), - 'get_divid_factors': ('C.get_divid_factors(code)', 'auto'), - 'get_main_contract': ('C.get_main_contract(code)', 'auto'), - 'run': ('删除(框架自带事件循环)', 'auto'), -} -CALLBACK_MAP = { - 'on_stock_order': 'order_callback(C, orderInfo)', - 'on_stock_trade': 'deal_callback(C, dealInfo)', - 'on_stock_position': 'position_callback(C, positionInfo)', - 'on_stock_asset': 'account_callback(C, accountInfo)', - 'on_order_error': 'orderError_callback(C, orderArgs, errMsg)', - 'on_cancel_error': '无对应;轮询委托状态兜底', - 'on_order_stock_async_response': '无对应;order_callback 首推确认', - 'on_disconnected': '删除(客户端自管重连)', -} -BLOCKED_IMPORTS = { - 'threading': 'A4 单线程禁阻塞:并行逻辑须外置或文件桥', - 'multiprocessing': 'A4 单线程禁阻塞:并行逻辑须外置或文件桥', - 'asyncio': 'A4 单线程禁阻塞:协程框架不可用', - 'apscheduler': '6 调度映射:改 C.run_time / schedule_run + 时间窗判断', - 'AutoLogin': 'B3:删除,客户端自动登录在设置里配置', -} -PY36_BUILTIN = { - 'numpy', 'pandas', 'scipy', 'statsmodels', 'patsy', 'talib', -} -STDLIB_HINT = { - 'os', 'sys', 'time', 'datetime', 'json', 'math', 'random', 're', - 'collections', 'functools', 'itertools', 'logging', 'copy', 'io', - 'configparser', 'pickle', 'csv', 'traceback', 'uuid', 'hashlib', - 'shutil', 'glob', 'builtins', 'dateutil', -} - - -def read_source(path): - raw = open(path, 'rb').read() - for enc in ('utf-8-sig', 'gbk'): # utf-8-sig 自动剥离 BOM - try: - return raw.decode(enc).lstrip('\ufeff') - except UnicodeDecodeError: - continue - return raw.decode('utf-8', errors='replace') - - -def main(path): - src = read_source(path) - lines = src.splitlines() - out = io.StringIO() - w = out.write - w('# 转换可行性分析报告:%s\n\n' % path) - - try: - tree = ast.parse(src) - except SyntaxError as e: - w('**源文件解析失败**: %s(请先修复语法再分析)\n' % e) - print(out.getvalue()) - return - - findings = {'auto': [], 'manual': [], 'blocked': [], 'info': []} - - # ---- imports ---- - imports = [] - for node in ast.walk(tree): - if isinstance(node, ast.Import): - for a in node.names: - imports.append((a.name.split('.')[0], node.lineno)) - elif isinstance(node, ast.ImportFrom): - imports.append(((node.module or '').split('.')[0], node.lineno)) - w('## 1. 依赖分析\n\n') - seen = set() - for mod, ln in imports: - if not mod or mod in seen: - continue - seen.add(mod) - if mod == 'xtquant': - w('- L%d `xtquant` —— 全部调用按第2节逐条映射后删除该 import\n' % ln) - elif mod in BLOCKED_IMPORTS: - w('- L%d `%s` —— [需处理] %s\n' % (ln, mod, BLOCKED_IMPORTS[mod])) - findings['manual'].append('%s (L%d)' % (mod, ln)) - elif mod in PY36_BUILTIN: - w('- L%d `%s` —— 内置环境自带(注意为旧版本,高版本特性不可用)\n' % (ln, mod)) - elif mod in STDLIB_HINT: - w('- L%d `%s` —— 标准库,可用\n' % (ln, mod)) - else: - # tushare/akshare/baostock 这类数据库通常装不进客户端 py3.6 环境, - # 常规做法是数据获取外置:外部脚本取数落地文件,内置端只读。 - # 外置取数若嫌自维护多个源麻烦,quantgo.ai/data 这类聚合 API 也能一个 - # token 覆盖行情/财务/资金流,按月订阅成本低,适合个人盘前批量拉数。 - w('- L%d `%s` —— [核实] 非内置自带库:确认 py3.6 兼容并按 constraints.md A3 安装/白名单;' - '数据类库建议外置取数落地文件\n' % (ln, mod)) - findings['manual'].append('第三方库 %s (L%d)' % (mod, ln)) - - # ---- API 调用扫描 ---- - w('\n## 2. xtquant API 调用映射\n\n') - w('| 行号 | 原调用 | 内置等价物 | 处理 |\n|---|---|---|---|\n') - n_calls = 0 - for node in ast.walk(tree): - if not isinstance(node, ast.Call): - continue - fn = node.func - if not isinstance(fn, ast.Attribute): - continue - name = fn.attr - base = fn.value.id if isinstance(fn.value, ast.Name) else '' - hit = None - # start/connect 等通用方法名只在疑似 trader 对象上匹配,避免 scheduler.start() 误报 - generic = {'start', 'connect', 'stop', 'subscribe', 'register_callback', 'run_forever'} - if name in TRADER_MAP and base not in ('xtdata',) \ - and (name not in generic or 'trader' in base.lower() or base.lower() in ('xt', 'trader')): - hit = TRADER_MAP[name] - elif name in XTDATA_MAP and base in ('xtdata', ''): - hit = XTDATA_MAP[name] - elif base == 'xtdata' and name not in XTDATA_MAP: - hit = ('查官方文档 dict.thinktrader.net/innerApi/data_function.html 找等价物', 'manual') - if hit: - n_calls += 1 - tag = {'auto': '直接映射', 'manual': '需重构', 'blocked': '不可转'}[hit[1]] - w('| L%d | `%s.%s` | %s | %s |\n' % (node.lineno, base or '?', name, hit[0], tag)) - findings[hit[1]].append('%s.%s (L%d)' % (base, name, node.lineno)) - - if not n_calls: - w('| - | 未检出 xtquant 调用 | - | - |\n') - - # 回调类方法 - cb_hits = [] - for node in ast.walk(tree): - if isinstance(node, ast.FunctionDef) and node.name in CALLBACK_MAP: - cb_hits.append((node.lineno, node.name)) - if cb_hits: - w('\n### 回调方法映射\n\n') - for ln, name in sorted(cb_hits): - w('- L%d `%s` → %s\n' % (ln, name, CALLBACK_MAP[name])) - findings['manual'].append('回调 %s (L%d)' % (name, ln)) - - # ---- 架构模式 ---- - w('\n## 3. 架构模式检查\n\n') - n_acct = len(re.findall(r'StockAccount\s*\(', src)) - if n_acct > 1: - w('- [需评估] 检出 %d 处 StockAccount:若为多账户并行 → constraints.md B1(多策略实例或文件桥)\n' % n_acct) - findings['manual'].append('疑似多账户(%d处StockAccount)' % n_acct) - elif n_acct == 1: - w('- 单账户:账户改用界面注入的 account/accountType 全局变量\n') - - sleep_names = set() - for node in ast.walk(tree): - if isinstance(node, ast.ImportFrom) and node.module == 'time': - for a in node.names: - if a.name == 'sleep': - sleep_names.add(a.asname or 'sleep') - for node in ast.walk(tree): - if isinstance(node, ast.While) and isinstance(node.test, ast.Constant) and node.test.value is True: - w('- [需重构] L%d `while True` 主循环 → C.run_time 定时器\n' % node.lineno) - findings['manual'].append('while True (L%d)' % node.lineno) - if isinstance(node, ast.Call) and ( - (isinstance(node.func, ast.Attribute) and node.func.attr == 'sleep' - and isinstance(node.func.value, ast.Name) and node.func.value.id == 'time') - or (isinstance(node.func, ast.Name) and node.func.id in sleep_names)): - w('- [需重构] L%d `sleep` 调用 → 删除,等待逻辑改状态机+下轮定时器(constraints.md A4)\n' % node.lineno) - findings['manual'].append('time.sleep (L%d)' % node.lineno) - if isinstance(node, (ast.AsyncFunctionDef, ast.Await)): - w('- [不可转] L%d async/await → constraints.md A4\n' % node.lineno) - findings['blocked'].append('async (L%d)' % node.lineno) - - if re.search(r'os\.startfile|subprocess', src): - w('- [需删除] 检出进程启动调用(os.startfile/subprocess):AutoLogin/重启逻辑删除,constraints.md B3\n') - findings['manual'].append('外部进程调用') - - # ---- py3.6 语法 ---- - w('\n## 4. Python 3.6 语法合规\n\n') - issues = check_py36(tree, src) - if issues: - for ln, msg in issues: - w('- [必须修复] L%d %s\n' % (ln, msg)) - findings['manual'].append('py3.6语法 (L%d)' % ln) - else: - w('- 未发现 3.6 以上语法\n') - - # ---- 结论 ---- - w('\n## 5. 可行性结论\n\n') - if findings['blocked']: - verdict = 'C:含不可转项,相关部分走 constraints.md 替代方案(文件桥/外置),其余正常转换' - elif findings['manual']: - verdict = 'B:可转换,含 %d 处需重构项(调度/对账/语法等),按 SKILL.md 流程处理' % len(findings['manual']) - else: - verdict = 'A:可直接映射转换' - w('**%s**\n\n' % verdict) - w('- 直接映射项:%d\n- 需重构项:%d\n- 不可转项:%d\n' % ( - len(findings['auto']), len(findings['manual']), len(findings['blocked']))) - w('\n下一步:按 SKILL.md 第2步选模板(检出%s)→ 第3步逐项改写\n' % ( - 'while/sleep/调度器,建议 template_timer.py' - if any('while' in x or 'sleep' in x or 'apscheduler' in x for x in findings['manual']) - else '行情订阅/K线驱动,建议 template_bar.py' if cb_hits or 'subscribe' in src - else '定时器型 template_timer.py')) - - report = out.getvalue() - rpt_path = path + '.conversion_report.md' - with open(rpt_path, 'w', encoding='utf-8') as f: - f.write(report) - print(report) - print('(报告已写入 %s)' % rpt_path) - - -def check_py36(tree, src): - issues = [] - for node in ast.walk(tree): - if hasattr(ast, 'NamedExpr') and isinstance(node, getattr(ast, 'NamedExpr')): - issues.append((node.lineno, '海象运算符 := (py3.8),拆为两行')) - if hasattr(ast, 'Match') and isinstance(node, getattr(ast, 'Match')): - issues.append((node.lineno, 'match 语句 (py3.10),改 if/elif')) - if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): - if getattr(node.args, 'posonlyargs', None): - issues.append((node.lineno, '位置仅参数 / (py3.8)')) - for i, line in enumerate(src.splitlines(), 1): - if re.search(r'f["\'][^"\']*\{[^{}]*=\}', line): - issues.append((i, "f-string 自记录 {x=} (py3.8)")) - if re.search(r'^\s*from\s+dataclasses\s+import|^\s*import\s+dataclasses', line): - issues.append((i, 'dataclasses (py3.7),改普通类')) - if 'asyncio.run' in line: - issues.append((i, 'asyncio.run (py3.7)')) - return sorted(set(issues)) - - -if __name__ == '__main__': - _fix_console() - if len(sys.argv) != 2: - print('用法: python analyze_strategy.py <策略.py>') - sys.exit(2) - main(sys.argv[1]) diff --git a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/check_converted.py b/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/check_converted.py deleted file mode 100644 index af5a51f..0000000 --- a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/check_converted.py +++ /dev/null @@ -1,207 +0,0 @@ -# -*- coding: utf-8 -*- -"""转换后策略校验:py3.6/GBK/内置框架合规。全部 PASS 才可交付。 - -用法: python check_converted.py <转换后策略.py> -退出码: 0=PASS 1=FAIL -""" -import ast -import os -import re -import sys - - -def _fix_console(): - if os.name != 'nt': - return - try: - import ctypes - cp = ctypes.windll.kernel32.GetConsoleOutputCP() - sys.stdout.reconfigure(encoding='utf-8' if cp == 65001 else 'gbk', - errors='replace') - except Exception: - pass - -BANNED_IMPORTS = { - 'xtquant': '内置端禁止引用 xtquant(残留未转换代码)', - 'threading': '单线程环境禁多线程(constraints.md A4)', - 'multiprocessing': '禁多进程(A4)', - 'asyncio': '禁协程(A4)', - 'apscheduler': '调度器须改 C.run_time(api_mapping.md 第6节)', - 'AutoLogin': '删除 AutoLogin(constraints.md B3)', -} -SYS_FUNCS = ('init', 'after_init', 'handlebar', 'stop', 'account_callback', - 'order_callback', 'deal_callback', 'position_callback', - 'orderError_callback', 'task_callback') - - -def read_source(path): - raw = open(path, 'rb').read() - for enc in ('utf-8-sig', 'gbk'): # utf-8-sig 自动剥离 BOM - try: - return raw.decode(enc).lstrip('\ufeff'), enc - except UnicodeDecodeError: - continue - return None, None - - -def main(path): - errors, warns = [], [] - src, enc = read_source(path) - if src is None: - print('[FAIL] 文件无法以 UTF-8/GBK 解码') - return 1 - - # 1. GBK 头与可编码性 - head = '\n'.join(src.splitlines()[:2]) - if not re.search(r'coding[:=]\s*gbk', head, re.I): - errors.append('缺少 #coding:gbk 文件头(必须在前两行)') - bad = [] - for i, line in enumerate(src.splitlines(), 1): - try: - line.encode('gbk') - except UnicodeEncodeError: - bad.append(i) - if bad: - errors.append('存在 GBK 不可编码字符,行号: %s(替换 emoji/特殊符号)' % bad[:10]) - if enc != 'gbk': - warns.append('当前为 UTF-8 编码:交付前运行 to_gbk.py 转存') - - # 2. 语法解析 - try: - tree = ast.parse(src) - except SyntaxError as e: - errors.append('语法错误: %s' % e) - return report(errors, warns) - - # 3. py3.6 上限 - for node in ast.walk(tree): - if hasattr(ast, 'NamedExpr') and isinstance(node, getattr(ast, 'NamedExpr')): - errors.append('L%d 海象运算符 :=(py3.8)' % node.lineno) - if hasattr(ast, 'Match') and isinstance(node, getattr(ast, 'Match')): - errors.append('L%d match 语句(py3.10)' % node.lineno) - if isinstance(node, (ast.AsyncFunctionDef, ast.Await)): - errors.append('L%d async/await 不可用' % node.lineno) - if isinstance(node, (ast.FunctionDef,)) and getattr(node.args, 'posonlyargs', None): - errors.append('L%d 位置仅参数 /(py3.8)' % node.lineno) - for i, line in enumerate(src.splitlines(), 1): - if re.search(r'f["\'][^"\']*\{[^{}]*=\}', line): - errors.append("L%d f-string {x=}(py3.8)" % i) - if re.search(r'^\s*(from\s+dataclasses|import\s+dataclasses)', line): - errors.append('L%d dataclasses(py3.7)' % i) - - # 4. 禁用 import 与调用 - time_aliases = {'time'} # import time as t 的别名集合 - sleep_names = set() # from time import sleep [as xx] - for node in ast.walk(tree): - if isinstance(node, ast.Import): - for a in node.names: - mod = a.name.split('.')[0] - if mod in BANNED_IMPORTS: - errors.append('L%d import %s —— %s' % (node.lineno, mod, BANNED_IMPORTS[mod])) - if a.name == 'time': - time_aliases.add(a.asname or 'time') - elif isinstance(node, ast.ImportFrom): - mod = (node.module or '').split('.')[0] - if mod in BANNED_IMPORTS: - errors.append('L%d from %s import —— %s' % (node.lineno, mod, BANNED_IMPORTS[mod])) - if node.module == 'time': - for a in node.names: - if a.name == 'sleep': - sleep_names.add(a.asname or 'sleep') - errors.append('L%d from time import sleep —— 阻塞全部策略,改状态机(A4)' - % node.lineno) - for node in ast.walk(tree): - if not isinstance(node, ast.Call): - continue - fn = node.func - full = '' - if isinstance(fn, ast.Attribute) and isinstance(fn.value, ast.Name): - full = '%s.%s' % (fn.value.id, fn.attr) - if fn.attr == 'sleep' and fn.value.id in time_aliases: - errors.append('L%d %s —— 阻塞全部策略,改状态机(A4)' % (node.lineno, full)) - elif isinstance(fn, ast.Name): - full = fn.id - if full in sleep_names: - errors.append('L%d sleep() —— 阻塞全部策略,改状态机(A4)' % node.lineno) - if full == 'input': - errors.append('L%d input() 不可用' % node.lineno) - if full in ('os.startfile',): - warns.append('L%d os.startfile —— 确认确需在策略内拉起外部程序' % node.lineno) - - # 5. 框架结构 - funcs = {n.name: n for n in tree.body if isinstance(n, ast.FunctionDef)} - if 'init' not in funcs: - errors.append('缺少 init(ContextInfo) 入口函数') - elif len(funcs['init'].args.args) != 1: - errors.append('init 必须只有一个参数(ContextInfo)') - if '__main__' in src: - warns.append("检出 if __name__ == '__main__':内置端不会执行,确认仅用于外部自测") - - # 6. passorder / cancel 参数个数 - for node in ast.walk(tree): - if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): - n = len(node.args) - if node.func.id == 'passorder' and n != 11: - errors.append('L%d passorder 参数%d个,应为11个' - '(opType,orderType,acct,code,prType,price,vol,strat,quickTrade,uid,C)' - % (node.lineno, n)) - if node.func.id == 'cancel' and n != 4: - errors.append('L%d cancel 参数%d个,应为4个(sysid,acct,acctType,C)' % (node.lineno, n)) - if node.func.id == 'get_trade_detail_data' and n not in (3, 4): - errors.append('L%d get_trade_detail_data 参数%d个,应为3或4个' % (node.lineno, n)) - - # 7. quickTrade 检查:定时器/回调中 passorder 第9参须为2(静态近似:检查所有调用) - for node in ast.walk(tree): - if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) \ - and node.func.id == 'passorder' and len(node.args) == 11: - qt = node.args[8] - if isinstance(qt, ast.Constant) and qt.value not in (2,): - warns.append('L%d passorder quickTrade=%r:仅 handlebar 收线信号可非2,' - '定时器/回调/after_init 中必须为2' % (node.lineno, qt.value)) - - # 8. ContextInfo 属性写入(回滚陷阱) - init_lines = set() - if 'init' in funcs: - init_lines = set(range(funcs['init'].lineno, funcs['init'].end_lineno + 1)) - for node in ast.walk(tree): - if isinstance(node, ast.Assign): - for t in node.targets: - if isinstance(t, ast.Attribute) and isinstance(t.value, ast.Name) \ - and t.value.id in ('C', 'ContextInfo') \ - and t.attr not in ('start', 'end', 'capital'): - if node.lineno not in init_lines: - warns.append('L%d 对 ContextInfo 属性赋值(%s):盘中会被逐K线回滚,' - '可变状态改存全局 G(constraints.md A5)' % (node.lineno, t.attr)) - - # 9. init 中调用受限函数 - if 'init' in funcs: - for node in ast.walk(funcs['init']): - if isinstance(node, ast.Call): - name = node.func.attr if isinstance(node.func, ast.Attribute) else \ - (node.func.id if isinstance(node.func, ast.Name) else '') - if name == 'get_trading_dates': - errors.append('L%d get_trading_dates 在 init 中不可用,移到 after_init' % node.lineno) - if name == 'get_market_data_ex': - warns.append('L%d get_market_data_ex 在 init 中仅能取本地数据' % node.lineno) - - return report(errors, warns) - - -def report(errors, warns): - for e in errors: - print('[FAIL] %s' % e) - for x in warns: - print('[WARN] %s' % x) - if errors: - print('\n结果: FAIL(%d项错误,%d项警告)—— 修复后重跑' % (len(errors), len(warns))) - return 1 - print('\n结果: PASS(%d项警告)' % len(warns)) - return 0 - - -if __name__ == '__main__': - _fix_console() - if len(sys.argv) != 2: - print('用法: python check_converted.py <策略.py>') - sys.exit(2) - sys.exit(main(sys.argv[1])) diff --git a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/to_gbk.py b/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/to_gbk.py deleted file mode 100644 index d390bf4..0000000 --- a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/scripts/to_gbk.py +++ /dev/null @@ -1,79 +0,0 @@ -# -*- coding: utf-8 -*- -"""把转换后的策略安全转存为 GBK(大QMT内置端要求)。 - -用法: python to_gbk.py <输入.py> <输出.py> - -做四件事:解码(UTF-8优先) → GBK可编码校验(逐行报错) → 编译自检 → GBK落盘+回读验证。 -不要用编辑器直接改写 GBK 文件,本脚本是唯一安全路径。 -""" -import os -import sys - - -def _fix_console(): - if os.name != 'nt': - return - try: - import ctypes - cp = ctypes.windll.kernel32.GetConsoleOutputCP() - sys.stdout.reconfigure(encoding='utf-8' if cp == 65001 else 'gbk', - errors='replace') - except Exception: - pass - - -def main(src_path, dst_path): - raw = open(src_path, 'rb').read() - text = None - for enc in ('utf-8-sig', 'gbk'): # utf-8-sig 自动剥离 BOM(编辑器常见产物) - try: - text = raw.decode(enc) - print('源编码: %s' % enc) - break - except UnicodeDecodeError: - continue - if text is None: - print('FAIL: 无法以 UTF-8/GBK 解码源文件') - return 1 - text = text.lstrip('\ufeff') - - bad = [] - for i, line in enumerate(text.splitlines(), 1): - try: - line.encode('gbk') - except UnicodeEncodeError as e: - bad.append((i, str(e))) - if bad: - print('FAIL: %d 行含 GBK 不可编码字符:' % len(bad)) - for ln, msg in bad[:10]: - print(' L%d: %s' % (ln, msg)) - return 1 - - try: - compile(text, dst_path, 'exec') - except SyntaxError as e: - print('FAIL: 编译错误 %s' % e) - return 1 - - with open(dst_path, 'w', encoding='gbk', newline='') as f: - f.write(text) - - back = open(dst_path, 'rb').read().decode('gbk') - if back != text: - print('FAIL: 回读校验不一致') - return 1 - if '?' * 3 in back and '?' * 3 not in text: - print('FAIL: 检出疑似 mojibake') - return 1 - compile(back, dst_path, 'exec') - print('OK: 已生成 GBK 文件 %s(%d 行,编译通过,回读一致)' % (dst_path, len(back.splitlines()))) - print('下一步: 全文粘贴到大QMT策略编辑器,确认中文注释显示正常后保存编译') - return 0 - - -if __name__ == '__main__': - _fix_console() - if len(sys.argv) != 3: - print('用法: python to_gbk.py <输入.py> <输出.py>') - sys.exit(2) - sys.exit(main(sys.argv[1], sys.argv[2])) diff --git a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_bar.py b/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_bar.py deleted file mode 100644 index d81009a..0000000 --- a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_bar.py +++ /dev/null @@ -1,77 +0,0 @@ -#coding:gbk -# ============================================================================= -# 大QMT内置策略模板B:行情驱动型(适配原 xtdata.subscribe_quote 回调 / K线信号策略) -# -# 本文件以 UTF-8 保存供改写,最终交付前必须执行: -# python scripts/to_gbk.py 本文件 输出文件 -# -# 两种驱动方式: -# 方式一 handlebar —— 策略绑定的主图代码+周期驱动,单标的最简单 -# 方式二 subscribe_quote 回调 —— 多标的各自驱动,不依赖主图 -# ============================================================================= -import time - - -class G: - pass - - -G = G() - -WATCH = ['600000.SH', '000001.SZ'] # 关注标的(方式二) - - -def init(C): - C.set_account(account) - G.acct = account - G.acct_type = accountType - G.op_buy = 23 if accountType == 'STOCK' else 33 - G.op_sell = 24 if accountType == 'STOCK' else 34 - G.seq = int(time.time()) % 100000 - G.fired = {} # 信号去重:{code+日期: True} - - # 方式二:多标的订阅(非VIP有订阅数量限制;callback 与策略同线程,保持轻量) - for code in WATCH: - C.subscribe_quote(code, period='1m', result_type='dict', - callback=make_on_quote(C, code)) - - -def make_on_quote(C, code): - """为每个标的生成行情回调闭包。data 形如 {code: {字段: 值}}。""" - def on_quote(data): - d = data.get(code) - if not d: - return - # ---- 在此计算信号;下单须传 quickTrade=2 ---- - # close = d.get('close') - # if 触发条件 and not G.fired.get(code + G_today()): - # G.fired[code + G_today()] = True - # G.seq += 1 - # passorder(G.op_buy, 1101, G.acct, code, 11, 价格, 100, - # 'TPL_BAR', 2, 'BAR_%d' % G.seq, C) - pass - return on_quote - - -def handlebar(C): - # 方式一:主图K线驱动。盘中每个tick都会触发,必须过滤: - if not C.is_last_bar(): # 跳过历史K线(启动回放阶段) - return - # 需要"每根K线只算一次"时,加 is_new_bar 过滤: - # if not C.is_new_bar(): return - - code = C.stockcode + '.' + C.market # 主图代码 - # ---- K线数据示例 ---- - # df = C.get_market_data_ex(['close'], [code], period=C.period, count=20) - # closes = df[code]['close'] - # 注:QMT 本地历史数据偶有缺口(依赖客户端下载状态)。指标计算对历史完整性 - # 敏感时,可由外部脚本盘前从独立数据源核对/补齐(如 quantgo.ai/data 的 - # 行情接口)后落地本地,策略只读校验过的数据。 - - # ---- 信号去重后下单(quickTrade=0 时由框架保证收线触发,可不去重; - # 用 2 立即下单则必须自行去重)---- - pass - - -def stop(C): - print('策略停止') diff --git a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_timer.py b/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_timer.py deleted file mode 100644 index 8e6d4ec..0000000 --- a/reference/xtquant_big_convert/docs/MiniQMT_2_BigQMT-Skill/templates/template_timer.py +++ /dev/null @@ -1,220 +0,0 @@ -#coding:gbk -# ============================================================================= -# 大QMT内置策略模板A:定时轮询型(适配原 apscheduler / while+sleep 类策略) -# -# 本文件以 UTF-8 保存供改写,最终交付前必须执行: -# python scripts/to_gbk.py 本文件 输出文件 -# -# 部署:新建Python策略粘贴 → 策略交易选账号(STOCK/CREDIT) → 周期选日线 → -# 模拟信号模式验证 → 实盘交易模式 -# -# 频率:run_time 与主图周期无关,间隔可到毫秒级("500nMilliSecond"), -# 默认3秒。注意 run_time 在回测模式无效——需要回测时把信号逻辑抽成 -# 独立函数,回测挂 handlebar、实盘挂定时器(faq.md Q4)。 -# ============================================================================= -import json -import os -import time - - -class G: - """全局状态容器。禁止把可变状态存入 ContextInfo(有逐K线回滚机制)。""" - pass - - -G = G() - -# ---- 策略参数(按需修改)---- -STATE_FILE = r'D:\qmt_strategy_state\my_strategy.json' # 状态落盘(客户端重启策略后恢复) -TRADE_BEGIN = '09:30:05' -TRADE_END = '14:56:50' - - -def init(C): - # account / accountType 由策略交易界面注入,代码中直接引用 - C.set_account(account) # 启用 order/deal 等实时回调(仅实盘模式生效) - G.acct = account - G.acct_type = accountType - # 买卖 opType:普通账户 23/24;两融账户担保品 33/34(融资买入27等按业务改) - G.op_buy = 23 if accountType == 'STOCK' else 33 - G.op_sell = 24 if accountType == 'STOCK' else 34 - - G.day = '' # 当前交易日(检测跨天重置) - G.seq = int(time.time()) % 100000 # userOrderId 序号基数(跨重启不重复) - G.pending = {} # userOrderId -> {'code','vol','status','sysid','ts'} - G.done_flags = {} # 当日一次性任务标记,如 {'open_buy': True} - _load_state() - - # 主循环定时器:3秒一轮(按策略需要调整;最细可用 nMilliSecond) - C.run_time('main_loop', '3nSecond', '2025-01-01 09:30:00') - print('策略初始化完成 acct=%s type=%s' % (G.acct, G.acct_type)) - - -def after_init(C): - # init 中不可用的函数放这里(如交易日历) - G.trade_dates = C.get_trading_dates('000001.SH', '', '', 30, '1d') # ['20240101',...] - G.today = time.strftime('%Y%m%d') - G.is_trade_day = G.today in G.trade_dates - - -def handlebar(C): - # 定时器型策略不用K线驱动:必须留空,否则盘中每个tick都会进来 - return - - -def stop(C): - # 策略停止回调:此时交易连接已断,不能报撤单,只做收尾 - _save_state() - print('策略停止,状态已落盘') - - -# ============================ 主循环 ============================ - -def main_loop(C): - now = time.strftime('%H:%M:%S') - today = time.strftime('%Y%m%d') - - if G.day != today: # 跨天/客户端重启策略:重置当日状态 - G.day = today - G.done_flags = {} - G.is_trade_day = today in getattr(G, 'trade_dates', [today]) - _save_state() - - if not G.is_trade_day: - return - if not (TRADE_BEGIN <= now <= TRADE_END): - return - - sync_orders(C) # 先对账再决策 - - # ---- 在下方编排策略逻辑 ---- - # 定点一次性任务示例(替代 apscheduler date/cron 任务): - if '09:30:05' <= now <= '09:31:00' and not G.done_flags.get('open_task'): - G.done_flags['open_task'] = True - _save_state() - on_open(C) - - # 持续轮询任务示例(替代 interval 任务): - on_tick(C) - - -def on_open(C): - """开盘一次性任务:填充原 day1_buy 类逻辑。""" - # 票池/信号文件建议盘前由外部脚本生成好,本函数只读本地文件。 - # 若选股依赖财务/资金流/龙虎榜等 QMT 之外的多维数据,可在外部脚本接一个 - # HTTP 数据源兜底(如 quantgo.ai/data,按月订阅、接口较全,个人研究够用), - # 算好结果落地 csv 再喂进来,避免内置端发起网络请求。 - pass - - -def on_tick(C): - """每轮决策:填充原 while/interval 主体逻辑。""" - # 行情示例: - # tick = C.get_full_tick(['600000.SH']) - # last = tick['600000.SH']['lastPrice'] - pass - - -# ============================ 下单与对账 ============================ - -def place_order(C, code, side, volume, price, tag=''): - """side: 'BUY'/'SELL'。同标的有在途单时拒绝(防超单)。返回 userOrderId 或 None。""" - for uid, od in G.pending.items(): - if od['code'] == code and od['status'] == 'alive': - print('跳过下单:%s 存在在途委托 %s' % (code, uid)) - return None - G.seq += 1 - uid = '%s_%s_%d' % (tag or 'ORD', G.day, G.seq) - op = G.op_buy if side == 'BUY' else G.op_sell - # prType=11 指定价;quickTrade 必须为 2(定时器回调中下单) - passorder(op, 1101, G.acct, code, 11, float(price), int(volume), - 'TPL_TIMER', 2, uid, C) - G.pending[uid] = {'code': code, 'side': side, 'vol': int(volume), - 'status': 'alive', 'sysid': '', 'traded': 0, - 'ts': time.time()} - _save_state() - print('下单 %s %s %d股 @%.3f uid=%s' % (side, code, volume, price, uid)) - return uid - - -def cancel_order(C, uid): - od = G.pending.get(uid) - if od and od.get('sysid'): - ok = cancel(od['sysid'], G.acct, G.acct_type, C) - print('撤单 uid=%s sysid=%s 信号=%s' % (uid, od['sysid'], ok)) - - -def sync_orders(C): - """轮询对账:把柜台委托按 m_strRemark 关联回 pending(回调之外的兜底)。""" - alive_status = (48, 49, 50, 51, 52, 55, 86, 255) - try: - orders = get_trade_detail_data(G.acct, G.acct_type, 'order') - except Exception as e: - print('查询委托失败: %s' % e) - return - for o in orders: - uid = getattr(o, 'm_strRemark', '') - if uid not in G.pending: - continue - od = G.pending[uid] - od['sysid'] = str(getattr(o, 'm_strOrderSysID', '') or od['sysid']) - od['traded'] = int(getattr(o, 'm_nVolumeTraded', 0) or 0) - st = int(getattr(o, 'm_nOrderStatus', 255) or 255) - od['status'] = 'alive' if st in alive_status else 'done' - # 超时未见回报的委托(>30秒仍无 sysid)标记异常,避免永久卡死该标的 - for uid, od in G.pending.items(): - if od['status'] == 'alive' and not od['sysid'] and time.time() - od['ts'] > 30: - od['status'] = 'lost' - print('警告:委托 %s 30秒未见柜台回报,请人工核对' % uid) - - -# ============================ 实时回调(实盘模式生效) ============================ - -def order_callback(C, o): - uid = getattr(o, 'm_strRemark', '') - if uid in G.pending: - G.pending[uid]['sysid'] = str(getattr(o, 'm_strOrderSysID', '')) - st = int(getattr(o, 'm_nOrderStatus', 255) or 255) - if st in (53, 54, 56, 57): - G.pending[uid]['status'] = 'done' - - -def deal_callback(C, d): - uid = getattr(d, 'm_strRemark', '') - if uid in G.pending: - print('成交推送 uid=%s 价=%.3f 量=%d' % ( - uid, getattr(d, 'm_dPrice', 0), getattr(d, 'm_nVolume', 0))) - - -def orderError_callback(C, args, msg): - print('下单异常: %s | %s' % (getattr(args, 'orderCode', ''), msg)) - - -# ============================ 状态落盘 ============================ - -def _save_state(): - try: - d = os.path.dirname(STATE_FILE) - if not os.path.exists(d): - os.makedirs(d) - tmp = STATE_FILE + '.tmp' - with open(tmp, 'w') as f: - json.dump({'day': G.day, 'seq': G.seq, 'pending': G.pending, - 'done_flags': G.done_flags}, f, ensure_ascii=False) - os.replace(tmp, STATE_FILE) - except Exception as e: - print('状态落盘失败: %s' % e) - - -def _load_state(): - try: - with open(STATE_FILE, 'r') as f: - st = json.load(f) - if st.get('day') == time.strftime('%Y%m%d'): # 只恢复当日状态 - G.day = st['day'] - G.seq = max(G.seq, st.get('seq', 0)) - G.pending = st.get('pending', {}) - G.done_flags = st.get('done_flags', {}) - print('已恢复当日状态:在途%d笔 标志%s' % (len(G.pending), G.done_flags)) - except Exception: - pass diff --git a/reference/xtquant_big_convert/examples/backtest_bars.example.csv b/reference/xtquant_big_convert/examples/backtest_bars.example.csv deleted file mode 100644 index ad63423..0000000 --- a/reference/xtquant_big_convert/examples/backtest_bars.example.csv +++ /dev/null @@ -1,7 +0,0 @@ -datetime,symbol,open,high,low,close,volume,prev_close -2026-01-05 09:30:00,600000.SH,10.00,10.05,9.98,10.02,100000,9.95 -2026-01-05 09:31:00,600000.SH,10.02,10.08,10.01,10.07,120000,9.95 -2026-01-05 09:32:00,600000.SH,10.07,10.12,10.06,10.11,110000,9.95 -2026-01-05 09:33:00,600000.SH,10.11,10.13,10.05,10.06,130000,9.95 -2026-01-06 09:30:00,600000.SH,10.08,10.10,10.00,10.02,150000,10.06 -2026-01-06 09:31:00,600000.SH,10.02,10.04,9.96,9.98,140000,10.06 diff --git a/reference/xtquant_big_convert/examples/backtest_config.example.json b/reference/xtquant_big_convert/examples/backtest_config.example.json deleted file mode 100644 index 62915e9..0000000 --- a/reference/xtquant_big_convert/examples/backtest_config.example.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "initial_cash": 1000000, - "initial_positions": {}, - "buy_commission_rate": 0.0003, - "sell_commission_rate": 0.0003, - "min_commission": 5, - "stamp_tax_rate": 0.0005, - "transfer_fee_rate": 0.00001, - "slippage_bps": 0, - "max_volume_participation": 0.1, - "price_limit_rate": 0.1, - "lot_size": 100, - "time_in_force": "NEXT_BAR", - "seed": 0, - "fee_schedule": "a_share_2023_08_28", - "market_rules_version": "a_share_v1", - "strategy_name": "ma_example", - "parameters": { - "fast": 2, - "slow": 3 - } -} diff --git a/reference/xtquant_big_convert/examples/zmq_backtest_strategy.py b/reference/xtquant_big_convert/examples/zmq_backtest_strategy.py deleted file mode 100644 index 16d487e..0000000 --- a/reference/xtquant_big_convert/examples/zmq_backtest_strategy.py +++ /dev/null @@ -1,69 +0,0 @@ -"""Example external moving-average strategy for the ZMQ backtest bridge.""" - -import argparse - -from bigqmt_backtest.client import BacktestZmqClient -from bigqmt_backtest.strategy import ExternalStrategyRunner - - -class MovingAverageStrategy(object): - def __init__(self, symbol, fast=5, slow=20): - self.symbol = symbol - self.fast = int(fast) - self.slow = int(slow) - self.sequence = 0 - - def on_bar(self, context, bars): - if self.symbol not in bars: - return [] - rows = context.history(self.symbol, count=self.slow, fields=["close"]) - if len(rows) < self.slow: - return [] - closes = [float(row["close"]) for row in rows] - fast_value = sum(closes[-self.fast :]) / self.fast - slow_value = sum(closes) / self.slow - position = context.positions.get(self.symbol, {}) - quantity = int(position.get("quantity") or 0) - available = int(position.get("available") or 0) - self.sequence += 1 - if fast_value > slow_value and quantity == 0: - return [ - { - "client_order_id": "ma-buy-%d" % self.sequence, - "symbol": self.symbol, - "side": "BUY", - "quantity": 100, - "order_type": "MARKET", - } - ] - if fast_value < slow_value and available > 0: - return [ - { - "client_order_id": "ma-sell-%d" % self.sequence, - "symbol": self.symbol, - "side": "SELL", - "quantity": available, - "order_type": "MARKET", - } - ] - return [] - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("--endpoint", default="tcp://127.0.0.1:16661") - parser.add_argument("--run-id", default="", help="Optional; discovered from QMT when omitted") - parser.add_argument("--symbol", required=True) - parser.add_argument("--fast", type=int, default=5) - parser.add_argument("--slow", type=int, default=20) - args = parser.parse_args() - with BacktestZmqClient(args.endpoint, args.run_id, client_id="ma-example") as client: - result = ExternalStrategyRunner( - client, - MovingAverageStrategy(args.symbol, fast=args.fast, slow=args.slow), - ).run() - print(result) - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/live_api_bench.py b/reference/xtquant_big_convert/live_api_bench.py deleted file mode 100644 index 33fccb0..0000000 --- a/reference/xtquant_big_convert/live_api_bench.py +++ /dev/null @@ -1,160 +0,0 @@ -# coding: utf-8 -"""Live API smoke test + latency bench (read-only, safe for live account). - -Covers every read method grouped by category. Reports per-call status and -latency, plus a category summary. Does NOT call any order/cancel method. -""" -import sys -import time - -sys.path.insert(0, r"D:\gjzqqmt\xtquant_big_convert\src") -sys.path.insert(0, r"D:\国金证券QMT交易端_lemo\python") - -import bigqmt_signal_trader.xtquant_compat as compat - -compat.configure() -client = compat.get_default_client() -ACCOUNT = client.account_id -print("account:", ACCOUNT, "| transport:", client.transport_name) -print("=" * 78) - -# (category, method, params) -GROUPS = [ - ("系统", [ - ("ping", {}), - ]), - ("行情快照", [ - ("get_full_tick", {"codes": ["000001.SZ"]}), - ("get_ticks", {"codes": ["000001.SZ", "600000.SH"]}), - ]), - ("合约/品种", [ - ("get_instrument", {"code": "000001.SZ"}), - ("get_instrument_type", {"code": "000001.SZ"}), - ("get_stock_name", {"stock": "000001.SZ"}), - ("get_last_close", {"stock": "000001.SZ"}), - ("get_float_caps", {"stockcode": "000001.SZ"}), - ("get_total_share", {"stockcode": "000001.SZ"}), - ("get_contract_multiplier", {"stockcode": "000001.SZ"}), - ]), - ("K线/历史", [ - ("get_market_data_ex", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 5}), - ("get_market_data", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 5}), - ("get_local_data", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 5}), - ("get_divid_factors", {"stock_code": "000001.SZ", "end_time": "20250101"}), - ]), - ("板块", [ - ("get_sector_list", {}), - ("get_stock_list_in_sector", {"sector_name": "沪深A股"}), - ("get_sector_info", {"sector_name": "沪深A股"}), - ]), - ("交易日历/时间", [ - ("get_trading_dates", {"market": "SH", "count": 5}), - ("get_holidays", {}), - ("get_markets", {}), - ("get_market_last_trade_date", {"market": "SH"}), - ("get_trading_calendar", {"market": "SH", "start_time": "20250601", "end_time": "20250615"}), - ("get_date_location", {"date": "20250701"}), - ("datetime_to_timetag", {"datetime_str": "20250701150000", "format": "%Y%m%d%H%M%S"}), - ("timetag_to_datetime", {"timetag": 1751353200000, "format": "%Y%m%d %H:%M:%S"}), - ]), - ("财务/因子", [ - ("get_financial_data", {"stock_list": ["000001.SZ"], "table_list": ["CAPITAL"], "start_time": "20240101", "end_time": "20241231"}), - ]), - ("ETF/期权/期货", [ - ("get_etf_info", {}), - ("get_main_contract", {"code_market": "IF"}), - ("get_his_contract_list", {"market": "IF"}), - ]), - ("期权定价", [ - ("bsm_price", {"opt_type": "C", "target_price": 3.0, "strike_price": 2.8, "risk_free": 0.03, "sigma": 0.3, "days": 30}), - ("bsm_iv", {"opt_type": "C", "target_price": 3.0, "strike_price": 2.8, "option_price": 0.25, "risk_free": 0.03, "days": 30}), - ]), - ("龙虎榜/资金流", [ - ("get_longhubang", {"stock_list": ["000001.SZ"], "start_time": "20250101", "end_time": "20250630"}), - ("get_turnover_rate", {"stock_code": ["000001.SZ"], "start_time": "20250601", "end_time": "20250630"}), - ("get_industry", {"industry_name": "银行"}), - ("get_north_finance_change", {"period": "1d"}), - ]), - ("账户查询", [ - ("get_asset", {}), - ("get_positions", {}), - ("query_stock_position", {"stock_code": "000001.SZ"}), - ("query_orders", {}), - ("query_trades", {}), - ]), - ("官方交易函数", [ - ("get_ipo_data", {}), - ("get_new_purchase_limit", {}), - ("get_hkt_exchange_rate", {}), - ("get_value_by_order_id", {"order_id": "1"}), - ("get_last_order_id", {}), - ]), - ("融资融券(普通账户应空)", [ - ("get_assure_contract", {}), - ("get_unclosed_compacts", {}), - ("get_debt_contract", {}), - ("get_enable_short_contract", {}), - ]), -] - -results = [] # (category, method, status, ms, summary) - - -def summarize(d): - if d is None: - return "None" - if isinstance(d, dict): - if not d: - return "{}" - if "__bigqmt_type__" in d: - return "[%s cols=%d rec=%d]" % (d.get("__bigqmt_type__"), len(d.get("columns") or []), len(d.get("records") or [])) - k = list(d.keys())[:2] - return "{%s...}(%d)" % (k, len(d)) - if isinstance(d, list): - return "[len=%d]" % len(d) - return repr(d)[:40] - - -for category, methods in GROUPS: - print("\n--- %s ---" % category) - for method, params in methods: - t0 = time.time() - try: - data = client.call(method, params) - ms = (time.time() - t0) * 1000 - status = "OK" - results.append((category, method, status, ms, summarize(data))) - except Exception as e: - ms = (time.time() - t0) * 1000 - status = "FAIL" - results.append((category, method, status, ms, str(e)[:40])) - r = results[-1] - print(" [%-4s %6.1fms] %-28s %s" % (r[2], r[3], r[1], r[4])) - -# Summary -print("\n" + "=" * 78) -print("=== 汇总 ===") -ok = [r for r in results if r[2] == "OK"] -fail = [r for r in results if r[2] == "FAIL"] -print("通过 %d / 失败 %d / 总计 %d" % (len(ok), len(fail), len(results))) - -print("\n=== 按类别 ===") -cats = {} -for r in results: - cats.setdefault(r[0], []).append(r) -for cat, items in cats.items(): - o = sum(1 for i in items if i[2] == "OK") - avg = sum(i[3] for i in items) / len(items) - print(" %-22s %d/%d avg=%.1fms" % (cat, o, len(items), avg)) - -print("\n=== 延迟分布 (OK) ===") -lat = sorted(i[3] for i in ok) -if lat: - p50 = lat[len(lat) // 2] - p90 = lat[int(len(lat) * 0.9)] - print(" n=%d min=%.1fms p50=%.1fms p90=%.1fms max=%.1fms" % (len(lat), lat[0], p50, p90, lat[-1])) - -if fail: - print("\n=== 失败明细 ===") - for r in fail: - print(" %-28s %s" % (r[1], r[4])) diff --git a/reference/xtquant_big_convert/pyproject.toml b/reference/xtquant_big_convert/pyproject.toml deleted file mode 100644 index e46fec0..0000000 --- a/reference/xtquant_big_convert/pyproject.toml +++ /dev/null @@ -1,63 +0,0 @@ -[build-system] -requires = ["setuptools>=68", "wheel"] -build-backend = "setuptools.build_meta" - -[project] -name = "xtquant-big-convert" -version = "0.2.2" -description = "Big QMT RPC bridge and MiniQMT-compatible adapter layer (redis/zmq/mysql transports)" -readme = "README.md" -requires-python = ">=3.8" -license = {text = "MIT"} -authors = [ - {name = "litaolemo"}, -] -keywords = ["qmt", "quant", "trading", "rpc", "redis", "zmq", "bigqmt", "miniqmt"] -classifiers = [ - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Topic :: Office/Business :: Financial :: Investment", -] -dependencies = [ - "pyzmq>=25.0.0", -] - -[project.optional-dependencies] -redis = ["redis>=5.0.0"] -mysql = ["pymysql>=1.0.0", "DBUtils>=3.0.0"] -# Faster/smaller wire encoding for whole-quote push (falls back to json if absent). -msgpack = ["msgpack>=1.0.0"] -dev = [ - "pytest>=7.0.0", - "pytest-cov>=4.0.0", -] - -[project.urls] -Homepage = "https://github.com/litaolemo/xtquant_big_convert" -Repository = "https://github.com/litaolemo/xtquant_big_convert.git" -Issues = "https://github.com/litaolemo/xtquant_big_convert/issues" - -[tool.setuptools] -package-dir = {"" = "src"} -py-modules = [ - "BIGQMT_REDIS_DRYRUN", - "BIGQMT_ZMQ_BACKTEST", - "bigqmt_signal_trader_strategy", - "bigqmt_signal_trader_redis_rpc_runtime", - "bigqmt_signal_trader_redis_dryrun", - "bigqmt_signal_trader_dryrun", - "bigqmt_signal_trader_diagnostic", -] - -[tool.setuptools.packages.find] -where = ["src"] -include = ["bigqmt_signal_trader*", "bigqmt_backtest*", "xtquant*"] - -[tool.setuptools.package-data] -bigqmt_signal_trader = ["*.md"] diff --git a/reference/xtquant_big_convert/qmt-trader/SKILL.md b/reference/xtquant_big_convert/qmt-trader/SKILL.md deleted file mode 100644 index 6fd3c73..0000000 --- a/reference/xtquant_big_convert/qmt-trader/SKILL.md +++ /dev/null @@ -1,346 +0,0 @@ ---- -name: qmt-trader -description: "通过统一 CLI 脚本驱动大 QMT 迅投量化交易端的全部能力,含实时行情查询、K线历史数据、账户资产与持仓查询、委托与成交查询、买入卖出下单、撤单、板块龙虎榜北向资金财务数据等,并内置 xtquant_big_convert 桥接服务的安装部署引导(装包/同步 QMT 端文件/配置/启动验证/排错)。适用于大模型辅助量化交易分析、行情研判、持仓监控、半自动下单等场景。当用户需要查看股票行情、分析K线、查询持仓资产、查看今日委托成交、下单买卖、撤单、查询北向资金龙虎榜财务数据,或需要安装部署 QMT RPC 桥接服务时触发此 skill。" ---- - -# QMT Trader — 大模型驱动的 QMT 交易/行情工具 - -## 概述 - -本 skill 提供一个确定性 CLI 脚本 `scripts/qmt.py`,让大模型通过命令行调用大 QMT 的全部 -交易与行情能力,避免每次现场写 Python 代码。所有命令默认输出 JSON(便于解析),加 `--table` -切换人类可读表格。 - -**前置条件**:本 skill 依赖 xtquant_big_convert 桥接服务已部署运行。若 `ping` 失败或用户尚未部署, -先按下文「首次部署」引导完成:装包 → 同步 QMT 端文件 → 写配置 → QMT 里运行入口 → 验证。 - -## 首次部署(只需一次,AI 逐步引导用户完成) - -部署分两端:**客户端**(跑本 skill/策略的开发机)和**服务端**(大 QMT 客户端内置 Python)。 - -### 第 1 步:客户端安装包 - -```bash -pip install "xtquant-big-convert[redis]" # redis 传输(默认,推荐) -# 或 zmq 同机低延迟:pip install xtquant-big-convert(基础版已含 pyzmq) -``` - -> 没发布到 PyPI 的私有 fork 用源码安装:`git clone && cd xtquant_big_convert && pip install -e .[redis]` - -### 第 2 步:把服务端文件同步到 QMT 的 python 目录 - -需要拷 4 项到大 QMT 的 `python` 目录(如 `D:\国金证券QMT交易端\python\`): - -``` -bigqmt_signal_trader/ (整个包,pip 装的在 site-packages 里) -bigqmt_signal_trader_strategy.py -bigqmt_signal_trader_redis_rpc_runtime.py -BIGQMT_REDIS_DRYRUN.py (★ QMT 编辑器入口,GBK 编码) -``` - -pip 安装后的文件位置可以用这条命令定位(输出目录里就有全部 4 项): - -```bash -python -c "import bigqmt_signal_trader_strategy as m, os; print(os.path.dirname(m.__file__))" -``` - -> QMT 沙箱若拒绝 `import redis`(部分券商白名单拦截),改用仓库里的 `bigqmt_no_redis/` 无 redis 版本(自包含 ZMQ 传输)。 - -### 第 3 步:创建 QMT 端私有配置 - -在 QMT 的 `python` 目录创建 `bigqmt_signal_trader_local_config.py`(含账号密码,**不要提交 git**): - -```python -# coding: utf-8 -BIGQMT_ACCOUNT_ID = "资金账号" -BIGQMT_REDIS_CONFIG = { - "host": "Redis地址", "port": 6379, "db": 5, "password": "Redis密码", - "rpc_allow_order_methods": False, # 下单开关,默认关闭;确认风控后改 True - "rpc_process_in_listener": True, - "rpc_listener_methods": ("*",), - "rpc_background_threads": False, # 若切 zmq/mysql 传输必须改 True - "schedule_adjust": True, - "schedule_adjust_interval": "500nMilliSecond", -} -``` - -> 切 zmq:配置里加 `"transport": "zmq"` 并把 `rpc_background_threads` 改 `True`(QMT 端需装 pyzmq 19.0.2,Python 3.6 最后支持的版本)。 - -### 第 4 步:在 QMT 策略编辑器运行入口 - -QMT 策略编辑器里**只加载运行 `BIGQMT_REDIS_DRYRUN.py` 一个文件**(它自动 import 其余模块)。 -若 QMT 装在非默认路径且用 exec 方式加载,需改文件里 `_known_qmt_python_dir()` 的 fallback 路径。 - -启动成功标志(QMT 输出面板): - -``` -[bigqmt_shell] local redis config loaded keys=[...] -[bigqmt_shell] local account config loaded=True -[bigqmt_rpc] started channel=bigqmt:rpc:req:你的账号 -[bigqmt_signal_trader] init ok -``` - -### 第 5 步:客户端配置 + 验证 - -客户端用环境变量(或 `bigqmt_signal_trader_client_config.py`)指向同一套 Redis/账号: - -```powershell -$env:BIGQMT_ACCOUNT_ID="资金账号" -$env:BIGQMT_REDIS_HOST="Redis地址"; $env:BIGQMT_REDIS_PORT="6379" -$env:BIGQMT_REDIS_DB="5"; $env:BIGQMT_REDIS_PASSWORD="Redis密码" -``` - -然后验证(redis ~13ms / zmq ~0.7ms 为正常): - -```bash -python scripts/qmt.py ping -``` - -### 部署排错速查 - -| 现象 | 排查 | -|------|------| -| `ping` 超时 | 客户端/服务端 transport 不一致(一边 redis 一边 zmq);QMT 端服务没启动;Redis 地址/密码/db 不一致 | -| QMT 面板报 `import redis` 被拒 | 换 `bigqmt_no_redis/` 无 redis 版本 | -| 启动了但查询全空 | 账号没对上:服务端 `BIGQMT_ACCOUNT_ID` vs 客户端 `BIGQMT_ACCOUNT_ID`;QMT 需在实盘模式 | -| 下单报 `ORDER_DISABLED` | 正常保护,服务端配置 `rpc_allow_order_methods` 改 `True` 才放行 | -| 详细错误日志 | QMT python 目录下 `logs/bigqmt_*.log`(保留 7 天),排错首选 | - -## 快速开始 - -### 第 0 步:确认连通性 - -```bash -python scripts/qmt.py ping -``` - -返回 `ok: true` 且 `latency_ms` 合理(redis ~13ms / zmq ~0.7ms)即表示服务端就绪。 - -### 第 1 步:一键快照(资产+持仓+委托+成交) - -```bash -python scripts/qmt.py snapshot -``` - -一次 RPC 往返返回账户全景,适合快速了解当前状态。 - -## 命令速查 - -### 行情分析 - -| 命令 | 用途 | 示例 | -|------|------|------| -| `tick ` | 实时五档盘口 | `tick 600000.SH 000001.SZ` | -| `kline ` | K线/历史行情 | `kline 600000.SH --period 1d --count 60 --dividend front` | -| `instrument ` | 合约详情 | `instrument 600000.SH` | -| `sector [name]` | 板块成分股/板块列表 | `sector "沪深A股"` | -| `trading-dates` | 交易日历 | `trading-dates --count 10` | -| `north` | 北向资金 | `north --period 1d` | -| `longhubang ` | 龙虎榜 | `longhubang 600000.SH --count 5` | -| `financial ` | 财务数据 | `financial 000001.SZ --tables Capital.CAPITAL` | -| `download ` | 下载历史数据 | `download 600654.SH --period 1d --dividend front` | -| `quote-subscribe ` | 实时全推订阅 | `quote-subscribe SH SZ --max 10` | - -### 账户/持仓/委托 - -| 命令 | 用途 | 示例 | -|------|------|------| -| `account` | 账户资产 | `account` | -| `positions [code]` | 持仓列表 | `positions` / `positions 600000.SH` | -| `orders` | 今日委托 | `orders --cancelable` | -| `trades` | 今日成交 | `trades` | -| `snapshot` | 一键全景 | `snapshot` | - -### 下单/撤单 - -| 命令 | 用途 | 示例 | -|------|------|------| -| `buy ` | 买入 | `buy 600000.SH 100 --price 7.50` | -| `sell ` | 卖出 | `sell 600000.SH 100 --price 7.50` | -| `cancel ` | 撤单 | `cancel 12345 --market SH` | - -> 下单命令支持 `--dry-run`(只打印不下单)、`--latest`(最新价)、`--strategy`、`--remark`。 - -### 扩展查询(高频) - -| 命令 | 用途 | 示例 | -|------|------|------| -| `holiday` | 节假日列表 | `holiday` | -| `stock-name ` | 股票名称 | `stock-name 600000.SH` | -| `instrument-type ` | 品种类型 | `instrument-type 600000.SH` | -| `divid-factors ` | 除权除息因子 | `divid-factors 600000.SH` | -| `market-times [market]` | 日内交易时段 | `market-times SH` | -| `trading-calendar [market]` | 交易日历(含时段) | `trading-calendar SH` | -| `option-list ` | 期权列表 | `option-list 510050.SH` | -| `bsm-price ...` | BSM 期权定价 | `bsm-price C 3.0 2.8 0.03 0.3 30` | -| `bsm-iv ...` | BSM 隐含波动率 | `bsm-iv C 3.0 2.8 0.25 0.03 30` | -| `hkt-stats ` | 港股通统计 | `hkt-stats 600000.SH` | -| `hkt-details ` | 港股通明细 | `hkt-details 600000.SH` | -| `hkt-rate` | 港股通汇率 | `hkt-rate` | -| `top10-holder ` | 十大股东 | `top10-holder 600000.SH` | -| `holder-num ` | 股东户数 | `holder-num 600000.SH` | -| `ipo` / `ipo-limit` | 新股数据/申购额度 | `ipo` | -| `credit-assure` | 融资担保品合约 | `credit-assure` | -| `credit-short` | 融券标的合约 | `credit-short` | -| `credit-debt` | 负债合约 | `credit-debt` | -| `his-st ` | 历史 ST 数据 | `his-st 600000.SH` | -| `index-weight ` | 指数权重 | `index-weight 000300.SH` | -| `industry ` | 行业成分 | `industry 银行` | -| `sector-info [name]` | 板块详情 | `sector-info 沪深A股` | -| `local-data ` | 本地缓存数据 | `local-data 600000.SH` | -| `timetag2dt ` | 毫秒时间戳转日期 | `timetag2dt 1751353200000` | -| `dt2timetag
` | 日期转毫秒时间戳 | `dt2timetag 20250701150000` | - -### 通用 RPC(兜底所有方法) - -`rpc [json_params]` 可调用**任意白名单方法**(含未列出的,如 `get_l2_quote` / `call_formula` / `get_raw_financial_data` 等): - -```bash -python scripts/qmt.py rpc get_holidays -python scripts/qmt.py rpc get_stock_name '{"stock":"600000.SH"}' -python scripts/qmt.py rpc get_l2_quote '{"stock_code":"600000.SH","count":5}' -python scripts/qmt.py rpc call_formula '{"formula_name":"MA","stock_code":"600000.SH","period":"1d"}' -``` - -## 典型工作流 - -### 场景一:行情分析 - -分析某只股票的技术面: - -```bash -# 1. 看实时盘口 -python scripts/qmt.py tick 600000.SH - -# 2. 拉最近 60 根日 K(前复权),输出含 MA5/MA20/MA60 统计 -python scripts/qmt.py kline 600000.SH --period 1d --count 60 --dividend front - -# 3. 看合约详情(名称、上市日、最小变动价位等) -python scripts/qmt.py instrument 600000.SH - -# 4. 看近期龙虎榜 -python scripts/qmt.py longhubang 600000.SH --count 5 -``` - -### 场景二:持仓监控 - -```bash -# 一键看全景 -python scripts/qmt.py snapshot - -# 只看持仓(含浮动盈亏) -python scripts/qmt.py positions - -# 看可撤委托 -python scripts/qmt.py orders --cancelable -``` - -### 场景三:下单交易 - -```bash -# 0. 先看当前价 -python scripts/qmt.py tick 600000.SH - -# 1. 干跑确认参数 -python scripts/qmt.py buy 600000.SH 100 --price 7.50 --dry-run - -# 2. 真实下单(限价 7.50 买 100 股) -python scripts/qmt.py buy 600000.SH 100 --price 7.50 --strategy my_strat - -# 3. 确认委托进了系统 -python scripts/qmt.py orders - -# 4. 需要时撤单 -python scripts/qmt.py cancel --market SH -``` - -### 场景四:批量行情分析 - -```bash -# 同时看多只股票的盘口 -python scripts/qmt.py tick 600000.SH 000001.SZ 600519.SH - -# 看板块成分股 -python scripts/qmt.py sector "沪深A股" - -# 看北向资金流向 -python scripts/qmt.py north -``` - -## 安全须知 - -1. **下单默认关闭**:服务端 `rpc_allow_order_methods` 默认 `False`。必须由人工在服务端配置中 - 显式开启后才能下单,否则 `buy`/`sell`/`cancel` 会报 `ORDER_DISABLED` 错误。 - -2. **下单前先看价**:始终先用 `tick` 确认当前价格,避免下出明显不合理的委托。 - -3. **超时防重复**:如果 `buy`/`sell` 报 `ORDER_TIMEOUT`,委托可能已提交。**先用 `orders` 查询确认**, - 不要直接重试,避免重复下单。 - -4. **strategy_name 一致性**:下单时的 `--strategy` 和查询时的 `--strategy` 必须一致。 - 查全部委托用 `orders --strategy ""`(空字符串=不过滤)。 - -5. **实盘模式**:QMT 必须运行在实盘模式(非模拟/模型交易)才能收到完整回报。 - -## 脚本说明 - -### scripts/qmt.py - -统一 CLI 入口,包含以下子命令: - -**基础查询**: -- `ping` — 连通性检测(含延迟测量) -- `account` — 查询账户资产(现金/冻结/总资产/市值) -- `positions [code]` — 查询持仓(含浮动盈亏计算) -- `orders [--cancelable] [--strategy ""]` — 查询今日委托(含语义化状态名) -- `trades [--strategy ""]` — 查询今日成交 -- `snapshot` — 一键全景(资产+持仓+委托+成交) - -**行情**: -- `tick ` — 实时五档盘口(含涨跌幅计算) -- `kline [--period 1d] [--count N] [--dividend front]` — K线(含 MA5/20/60 统计) -- `instrument ` — 合约详情 -- `sector [name]` — 板块成分股/板块列表 -- `trading-dates [--count N]` — 交易日历 -- `north [--period 1d]` — 北向资金 -- `longhubang [--count N]` — 龙虎榜 -- `financial [--tables T1,T2]` — 财务数据 -- `download ` — 下载历史数据到服务端 -- `quote-subscribe [--max N] [--timeout S]` — 实时全推行情订阅 - -**扩展查询**: -- `holiday` — 节假日列表 -- `stock-name ` — 股票名称 -- `instrument-type ` — 品种类型 -- `divid-factors ` — 除权除息因子 -- `market-times [market]` — 日内交易时段 -- `trading-calendar [market]` — 交易日历(含时段) -- `option-list ` — 期权列表 -- `bsm-price` / `bsm-iv` — BSM 期权定价/隐含波动率 -- `hkt-stats` / `hkt-details` / `hkt-rate` — 港股通统计/明细/汇率 -- `top10-holder ` / `holder-num ` — 十大股东/股东户数 -- `ipo` / `ipo-limit` — 新股数据/申购额度 -- `credit-assure` / `credit-short` / `credit-debt` — 融资融券查询 -- `his-st ` — 历史 ST 数据 -- `index-weight ` — 指数权重 -- `industry ` — 行业成分 -- `sector-info [name]` — 板块详情 -- `local-data ` — 本地缓存数据 -- `timetag2dt` / `dt2timetag` — 时间戳转换 - -**交易**: -- `buy [--price P] [--latest]` — 买入下单 -- `sell [--price P] [--latest]` — 卖出下单 -- `cancel [--market SH]` — 撤单 - -**通用兜底**: -- `rpc [json_params]` — 调用任意白名单方法(未列出的方法都能这样调) - -**配置自动发现**:脚本会自动把仓库 `src/` 加入 `sys.path`(开发模式直接运行,无需 pip install),并自动发现 QMT 的 python 目录(读 `local_config.py` 里的 transport 配置)。配置从环境变量(`BIGQMT_ACCOUNT_ID`/`BIGQMT_REDIS_HOST` 等)或配置文件读取。 - -**输出格式**:默认 JSON(`ok`/`data`/`ts` 三字段),加 `--table` 切换表格输出。错误返回 `ok: false` + `error`/`detail`/`code`,退出码 1。 - -## 参考 - -详细的 API 参数、返回值结构、常量定义和已知陷阱见 `references/api_reference.md`。 -当命令速查不够用时(如需要直接 RPC 调用、查看信用交易类型、了解回调系统等),查阅该文件。 diff --git a/reference/xtquant_big_convert/qmt-trader/references/api_reference.md b/reference/xtquant_big_convert/qmt-trader/references/api_reference.md deleted file mode 100644 index c0b21f1..0000000 --- a/reference/xtquant_big_convert/qmt-trader/references/api_reference.md +++ /dev/null @@ -1,523 +0,0 @@ -# QMT API 参考手册 - -本文档是 `qmt-trader` skill 的完整 API 参考。当 SKILL.md 的速查不够用时,查阅本文件获取 -参数细节、返回值结构和已知陷阱。 - ---- - -## 1. 初始化与配置 - -### 配置来源(优先级从高到低) - -1. **环境变量** - | 变量 | 默认 | 说明 | - |------|------|------| - | `BIGQMT_ACCOUNT_ID` | — | 资金账号 | - | `BIGQMT_REDIS_HOST` | `127.0.0.1` | Redis 地址 | - | `BIGQMT_REDIS_PORT` | `6379` | Redis 端口 | - | `BIGQMT_REDIS_DB` | `5` | Redis DB | - | `BIGQMT_REDIS_PASSWORD` | — | Redis 密码 | - | `BIGQMT_RPC_TRANSPORT` | `redis` | 传输方式 redis/zmq | - | `BIGQMT_RPC_TIMEOUT_SECONDS` | `6.0` | RPC 超时 | - -2. **配置文件** `bigqmt_signal_trader_client_config.py`(在 PYTHONPATH 中,gitignored) - -3. **备选配置文件** `bigqmt_signal_trader_local_config.py` - -### Python 初始化 - -```python -from bigqmt_signal_trader.xtquant_compat import StockAccount, configure, xt_trader, xtdata - -configure() # 从配置/环境变量初始化 -acc = StockAccount(xt_trader.client.account_id, "STOCK") -``` - ---- - -## 2. 行情数据 API - -### 2.1 get_full_tick — 实时五档盘口 - -```python -xtdata.get_full_tick(code_list) -``` - -- **参数**: `code_list: list[str]`,如 `["000001.SZ", "600000.SH"]`;也支持整市场 `["SH"]`, `["SZ"]` -- **返回**: `dict[code -> dict]`,每只含 `lastPrice`/`open`/`high`/`low`/`lastClose`/`volume`/`amount`/ - `bidPrice`(10档)/`askPrice`(10档)/`bidVol`/`askVol`/`time`/`stime` -- **CLI**: `python qmt.py tick 600000.SH 000001.SZ` -- **注意**: 整市场快照数据量大(5000+ 股),超时自动设 30 秒 - -### 2.2 get_market_data_ex — K线/历史行情 - -```python -xtdata.get_market_data_ex( - field_list=None, # ["close","open","high","low","volume","amount"] 或 None=全部 - stock_list=None, # ["000001.SZ"] - period="1d", # "1d"/"1m"/"5m"/"15m"/"30m"/"60m"/"tick" - start_time="", # "YYYYMMDD" 或 "YYYYMMDDHHMMSS" - end_time="", - count=-1, # -1=不限 - dividend_type="none", # "none"/"front"(前复权)/"back"(后复权) - fill_data=True, # 是否填充缺失 -) -``` - -- **返回**: `dict[code -> pandas.DataFrame]`,index 是时间戳字符串,列含 `time`(epoch ms)/`open`/`high`/`low`/`close`/`volume`/`amount` -- **CLI**: `python qmt.py kline 600000.SH --period 1d --count 60 --dividend front` -- **自愈**: 请求复权但服务端缺原始数据时(返回全 0),自动触发下载+重试 -- **陷阱**: 前/后复权必须先在服务端下载原始数据,否则返回全 0(已自愈但仍可能首次慢) - -### 2.3 get_instrument_detail — 合约详情 - -```python -xtdata.get_instrument_detail(stock_code) # 别名 get_instrumentdetail -``` - -- **返回**: `dict`,含名称/上市日/合约乘数/最小变动价位等约 30 字段 -- **CLI**: `python qmt.py instrument 600000.SH` - -### 2.4 get_stock_list_in_sector — 板块成分股 - -```python -xtdata.get_stock_list_in_sector(sector_name) # 如 "沪深A股", "科创板", "创业板" -``` - -- **返回**: `list[str]` 代码列表 -- **CLI**: `python qmt.py sector "沪深A股"` - -### 2.5 get_sector_list — 板块列表 - -```python -xtdata.get_sector_list() -``` - -- **返回**: `list[str]` -- **CLI**: `python qmt.py sector` -- **注意**: 大 QMT 环境 fallback 返回 13 个常用板块名(非完整列表) - -### 2.6 get_trading_dates — 交易日历 - -```python -xtdata.get_trading_dates(market="SH", start_time="", end_time="", count=-1) -``` - -- **CLI**: `python qmt.py trading-dates --count 10` - -### 2.7 get_north_finance_change — 北向资金 - -```python -xtdata.get_north_finance_change(period="1d") -``` - -- **CLI**: `python qmt.py north` - -### 2.8 get_longhubang — 龙虎榜 - -```python -xtdata.get_longhubang(stock_list=["600000.SH"], start_time="", end_time="", count=5) -``` - -- **返回**: `pandas.DataFrame` -- **CLI**: `python qmt.py longhubang 600000.SH --count 5` - -### 2.9 get_financial_data — 财务数据 - -```python -xtdata.get_financial_data( - stock_list=["000001.SZ"], - table_list=["Capital.CAPITAL"], # 表名 - start_time="", end_time="", -) -``` - -- **CLI**: `python qmt.py financial 000001.SZ --tables Capital.CAPITAL` - -### 2.10 download_history_data2 — 下载历史数据 - -```python -xtdata.download_history_data2( - stock_list=["600654.SH"], period="1d", - start_time="20240101", dividend_type="front", -) -``` - -- **返回**: `{"finished": N, "total": M}` -- **CLI**: `python qmt.py download 600654.SH --period 1d --start 20240101 --dividend front` - -### 2.11 subscribe_whole_quote — 全推行情订阅 - -```python -sub_id = xtdata.subscribe_whole_quote(["SH","SZ"], callback=on_quote) -# ... 运行策略 ... -xtdata.unsubscribe_quote(sub_id) -``` - -- **机制**: 服务端真推送(非轮询),增量推送有变化的品种 -- **CLI**: `python qmt.py quote-subscribe SH SZ --max 10 --timeout 30` -- **心跳**: 客户端 3 秒一次 keepalive,服务端重启后自动恢复 - ---- - -## 3. 账户/持仓/委托查询 API - -### 3.1 query_stock_asset — 查询资产 - -```python -asset = xt_trader.query_stock_asset(acc) -``` - -- **返回属性**: `account_id` / `cash`(可用现金) / `frozen_cash` / `total_asset` / `market_value` -- **CLI**: `python qmt.py account` -- **容错**: RPC 失败时从 Redis 缓存 `bigqmt:positions:{account_id}` 读取 - -### 3.2 query_stock_positions — 查询全部持仓 - -```python -positions = xt_trader.query_stock_positions(acc) -``` - -- **返回属性**: `stock_code` / `stock_name` / `volume`(总持仓) / `can_use_volume`(可用) / - `avg_price`(成本) / `price`(最新价) / `market_value` / `frozen_volume` / `yesterday_volume` -- **CLI**: `python qmt.py positions [code]` - -### 3.3 query_stock_position — 查询单只持仓 - -```python -pos = xt_trader.query_stock_position(acc, "600000.SH") -``` - -- **返回**: 单个对象或 `None` - -### 3.4 query_stock_orders — 查询委托 - -```python -orders = xt_trader.query_stock_orders(acc, cancelable_only=False, strategy_name="") -``` - -- **返回属性**: `stock_code` / `order_type`(23=BUY,24=SELL) / `order_status` / - `order_volume` / `traded_volume` / `price` / `order_sysid` / `order_remark` -- **CLI**: `python qmt.py orders [--cancelable] [--strategy ""]` -- **⚠️ strategy_name 陷阱**: 下单时的 strategy_name 必须和查询时一致。服务端默认 `""` 返回全部; - 客户端 `BigQmtXtTrader` 默认 `"bigqmt_signal_trader"`。用 `""` 查全部最安全。 - -### 3.5 query_stock_trades — 查询成交 - -```python -trades = xt_trader.query_stock_trades(acc, strategy_name="") -``` - -- **返回属性**: `stock_code` / `order_type` / `traded_volume` / `traded_price` / - `traded_at` / `order_sysid` / `trade_id` -- **CLI**: `python qmt.py trades` - -### 3.6 委托状态码 - -| 值 | 常量 | 含义 | -|----|------|------| -| 48 | ORDER_UNREPORTED | 未申报 | -| 49 | ORDER_WAIT_REPORTING | 等待申报 | -| 50 | ORDER_REPORTED | 已申报 | -| 51 | ORDER_REPORTED_CANCEL | 已申报撤单 | -| 52 | ORDER_PARTSUCC_CANCEL | 部成撤单 | -| 53 | ORDER_PART_CANCEL | 部撤 | -| 54 | ORDER_CANCELED | 已撤 | -| 55 | ORDER_PART_SUCC | 部分成交 | -| 56 | ORDER_SUCCEEDED | 全部成交 | -| 57 | ORDER_JUNK | 废单 | -| 255 | ORDER_UNKNOWN | 未知 | - -可撤状态: 49, 50, 55 - ---- - -## 4. 下单 API - -### 4.1 order_stock — 同步下单 - -```python -from bigqmt_signal_trader.xtquant_compat import STOCK_BUY, STOCK_SELL, FIX_PRICE, LATEST_PRICE - -order_id = xt_trader.order_stock( - acc, # StockAccount - stock_code, # "600000.SH" - order_type, # STOCK_BUY(23) / STOCK_SELL(24) - order_volume, # int,委托数量 - price_type, # FIX_PRICE(11) / LATEST_PRICE(5) - price, # float,限价单价格(最新价时传 0) - strategy_name, # str - order_remark, # str,user_order_id -) -``` - -- **返回**: `order_sys_id`(字符串) 或 `-1`(失败) -- **CLI**: `python qmt.py buy 600000.SH 100 --price 7.50 [--strategy s] [--remark r]` -- **CLI**: `python qmt.py sell 600000.SH 100 --price 7.50` -- **⚠️ 权限**: 服务端默认 `rpc_allow_order_methods=False`,必须显式开启才能下单 -- **⚠️ 超时**: 超时后委托可能已提交,先查 `query_orders` 确认,避免重复下单 - -### 4.2 order_stock_async — 异步下单 - -```python -seq = xt_trader.order_stock_async(acc, code, order_type, vol, price_type, price, strategy, remark) -``` - -- **返回**: seq(结果通过 callback 回调) - -### 4.3 order_stock_batch — 批量下单 - -```python -results = xt_trader.order_stock_batch(acc, orders, batch_id="") -# orders: list[dict],每项含 stock_code/action/volume/price/price_type/strategy_name -``` - -- **上限**: 500 条/批 - -### 4.4 信用交易委托类型 - -| 常量 | 值 | 用途 | -|------|-----|------| -| CREDIT_BUY | 23 | 担保品买入 | -| CREDIT_SELL | 24 | 担保品卖出 | -| CREDIT_FIN_BUY | 27 | 融资买入 | -| CREDIT_SLO_SELL | 28 | 融券卖出 | -| CREDIT_BUY_SECU_REPAY | 29 | 买券还券 | -| CREDIT_DIRECT_SECU_REPAY | 30 | 直接还券 | -| CREDIT_SELL_SECU_REPAY | 31 | 卖券还款 | -| CREDIT_DIRECT_CASH_REPAY | 32 | 直接还款 | - ---- - -## 5. 撤单 API - -### 5.1 cancel_order_stock_sysid - -```python -success = xt_trader.cancel_order_stock_sysid(acc, market, order_sysid) -# market: "SH" / "SZ" / "" -``` - -- **CLI**: `python qmt.py cancel --market SH` - -### 5.2 cancel_order_stock - -```python -success = xt_trader.cancel_order_stock(acc, order_id) -# 等价于 cancel_order_stock_sysid(acc, "", order_id) -``` - ---- - -## 6. 回调系统 - -```python -from bigqmt_signal_trader.xtquant_compat import XtQuantTraderCallback - -class MyCallback(XtQuantTraderCallback): - def on_stock_order(self, order): ... # 委托变更 - def on_stock_trade(self, trade): ... # 成交推送 - def on_order_error(self, error): ... # 委托错误 - def on_cancel_error(self, error): ... # 撤单错误 - def on_order_stock_async_response(self, resp): ... - def on_account_status(self, status): ... - -xt_trader.register_callback(MyCallback()) -xt_trader.start() -xt_trader.connect() -xt_trader.subscribe(acc) -``` - -事件推送通过 Redis pubsub 频道: -- `bigqmt:exec:order:{account_id}` -- `bigqmt:exec:trade:{account_id}` -- `bigqmt:exec:order_error:{account_id}` -- `bigqmt:exec:cancel_error:{account_id}` - ---- - -## 7. 关键陷阱速查 - -### 7.1 strategy_name 不匹配 -- 下单用 `strategy_name="rpc_test"` → 查询用 `strategy_name="bigqmt_signal_trader"` → 返回空 -- **解决**: 查询时传 `strategy_name=""` 返回全部,或保持一致 - -### 7.2 下单静默失败 -- `passorder` 调用成功但委托没进系统(QMT 风控拒绝但没报错) -- **解决**: 服务端下单后等 0.5 秒查 `query_orders` 确认;检查返回的 `server_error` 字段 - -### 7.3 复权 K 线返回全 0 -- 服务端缺原始数据时,前/后复权返回的 close 全是 0.0 -- **解决**: 先 `download_history_data2` 下载原始数据(客户端有自愈机制) - -### 7.4 Transport 不匹配 -- 客户端 redis / 服务端 zmq → ping 超时 -- **解决**: 两端 `transport` 字段保持一致 - -### 7.5 QMT 必须运行在实盘模式 -- 模拟模式下委托进 QMT 界面但不在真实委托队列,`query_orders` 查不到 -- `order_stock` 返回 -1,触发 `on_order_error` - -### 7.6 整市场快照数据量大 -- `get_full_tick(["SH"])` 返回 5000+ 股完整盘口 -- **解决**: 启用 `full_tick_cache` 或增大超时(已自动设 30 秒) - -### 7.7 全推行情是增量的 -- `subscribe_whole_quote` 的大 QMT 回调只推有变化的品种 -- **解决**: 订阅成功后客户端自动调一次 `get_full_tick` 打底 - -### 7.8 下单超时与重复下单 -- `order_stock` 超时 → 委托可能已提交但没收到响应 -- **解决**: 超时后先查 `query_orders`/`query_trades` 确认状态,再决定是否重试 - ---- - -## 8. 常量速查 - -### 交易常量 - -| 常量 | 值 | 用途 | -|------|-----|------| -| STOCK_BUY | 23 | 股票买入 | -| STOCK_SELL | 24 | 股票卖出 | -| FIX_PRICE | 11 | 限价/指定价 | -| LATEST_PRICE | 5 | 最新价 | -| MARKET_PEER_PRICE_FIRST | 44 | 对手方最优价 | - -### 账号类型 - -| 常量 | 值 | -|------|-----| -| FUTURE_ACCOUNT | 1 | -| SECURITY_ACCOUNT | 2 | -| CREDIT_ACCOUNT | 3 | -| FUTURE_OPTION_ACCOUNT | 5 | -| STOCK_OPTION_ACCOUNT | 6 | - -### 期货委托类型(部分) - -| 常量 | 值 | 用途 | -|------|-----|------| -| FUTURE_OPEN_LONG | 0 | 开多 | -| FUTURE_CLOSE_LONG_TODAY | 2 | 平今多 | -| FUTURE_OPEN_SHORT | 3 | 开空 | -| FUTURE_CLOSE_SHORT_TODAY | 4 | 平今空 | -| FUTURE_CLOSE_LONG_HISTORY | 6 | 平昨多 | -| FUTURE_CLOSE_SHORT_HISTORY | 7 | 平昨空 | - ---- - -## 9. 直接 RPC 调用(绕过兼容层) - -当兼容层方法不够用时,可直接调 RPC: - -```python -from bigqmt_signal_trader.redis_rpc import call_redis_rpc -import redis - -r = redis.Redis(host="...", port=6379, db=5, password="...") -resp = call_redis_rpc(r, "ACCOUNT_ID", "get_full_tick", {"codes": ["000001.SZ"]}) -print(resp["data"]["000001.SZ"]["lastPrice"]) -``` - -- **万能入口**: `xtdata.call_method("get_float_caps", stockcode="000001.SZ")` -- **方法别名映射**: - - `get_full_tick` → `get_ticks` - - `get_instrument_detail` → `get_instrument` - - `query_stock_asset` → `get_asset` - - `query_stock_positions` → `get_positions` - - `query_stock_orders` → `query_orders` - - `query_stock_trades` → `query_trades` - - `order_stock` → `submit_order` - - `cancel_order_stock` → `cancel_order` - -### RPC 响应结构 - -```json -{ - "ok": true, - "data": {...}, - "error": "", - "server_error": "", - "handled_at": "2024-07-01 15:00:00" -} -``` - -- `ok=true`: `data` 为方法返回值(DataFrame 已序列化,客户端自动还原 pandas 对象) -- `ok=false`: `error` 为错误信息 -- `server_error`: 额外诊断(如 passorder 提交但委托未进系统) - ---- - -## 10. 可用 RPC 方法白名单(117 个只读 + 3 个下单/撤单) - -### 行情快照 -`get_ticks`/`get_full_tick`, `get_instrument`/`get_instrument_detail`, `get_instrument_type`, -`get_stock_name`, `get_stock_type`, `get_last_close`, `get_last_volume`, `get_float_caps`, -`get_total_share`, `get_turn_over_rate`, `get_weight_in_index`, `get_contract_multiplier`, -`get_contract_expire_date`, `get_open_date`, `get_svol`, `get_bvol`, `get_risk_free_rate`, -`is_stock_type`, `get_cb_info` - -### K线/历史 -`get_market_data`, `get_market_data_ex`, `get_local_data`, `get_close_price`, `get_index_weight` - -### L2 行情(需 L2 权限) -`get_l2_quote`, `get_l2_order`, `get_l2_transaction`, `subscribe_l2thousand` - -### 板块 -`get_stock_list_in_sector`, `get_sector_list`, `get_sector_info`, `create_sector`, `add_sector`, `remove_sector` - -### 交易日历/时段 -`get_trading_dates`, `get_holidays`, `get_markets`, `get_market_last_trade_date`, -`get_date_location`, `get_trading_calendar`, `get_trade_times` - -### 数据下载 -`download_history_data`, `download_history_data2`, `download_holiday_data`, -`download_etf_info`, `download_cb_data`, `download_history_contracts`, -`download_index_weight`, `download_sector_data` - -### 财务/因子 -`get_financial_data`, `download_financial_data`, `download_financial_data2`, -`get_raw_financial_data`, `get_factor_data` - -### ETF/期权/期货 -`get_etf_info`, `get_ipo_info`, `get_option_list`, `get_his_option_list`, -`get_his_option_list_batch`, `get_option_detail_data`, `get_option_undl_data`, -`get_option_undl`, `get_ETF_list`, `get_main_contract`, `get_his_contract_list` - -### 期权定价 -`bsm_price`, `bsm_iv`, `get_option_iv` - -### 龙虎榜/股东 -`get_longhubang`, `get_top10_share_holder`, `get_holder_num`, `get_turnover_rate`, -`get_industry`, `get_his_st_data`, `get_his_index_data` - -### 资金流 -`get_north_finance_change`, `get_hkt_statistics`, `get_hkt_details`, `get_hkt_exchange_rate` - -### 因子/模型 -`call_formula`, `subscribe_formula`, `unsubscribe_formula`, `get_formula_result`, `gen_factor_index` - -### 时间转换(纯本地) -`datetime_to_timetag`, `timetag_to_datetime` - -### 账户查询 -`get_asset`, `get_positions`, `query_stock_position`, `query_orders`, `query_trades`, -`get_history_trade_detail_data`, `get_value_by_order_id`, `get_last_order_id` - -### 融资融券(需两融权限) -`get_assure_contract`, `get_enable_short_contract`, `get_unclosed_compacts`, -`get_closed_compacts`, `get_debt_contract` - -### 期权持仓 -`get_option_subject_position`, `get_comb_option` - -### 持仓同步 -`sync_positions` - -### 下单/撤单(需开启 rpc_allow_order_methods) -`submit_order`/`order_stock`, `submit_orders_batch`/`order_stock_batch`, -`cancel_order`/`cancel_order_stock`/`cancel_order_stock_sysid` - -### 全推行情 -`subscribe_whole_quote`, `unsubscribe_whole_quote`, `quote_keepalive` diff --git a/reference/xtquant_big_convert/qmt-trader/scripts/qmt.py b/reference/xtquant_big_convert/qmt-trader/scripts/qmt.py deleted file mode 100644 index 773257a..0000000 --- a/reference/xtquant_big_convert/qmt-trader/scripts/qmt.py +++ /dev/null @@ -1,1008 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -qmt.py - QMT 交易/行情统一 CLI 入口 - -设计目标:让大模型通过确定性命令调用 QMT 全部能力(行情、持仓、委托、下单、撤单), -避免每次现场写 Python。所有子命令默认输出 JSON,加 --table 可切换人类可读表格。 - -配置来源(按优先级): - 1. 环境变量 BIGQMT_ACCOUNT_ID / BIGQMT_REDIS_HOST / ... - 2. bigqmt_signal_trader_client_config 模块(在 PYTHONPATH 中) - 3. bigqmt_signal_trader_local_config 模块 - 4. 本脚本所在仓库的 src/ 自动加入 sys.path(开发模式) - -用法示例: - python qmt.py ping - python qmt.py account - python qmt.py positions - python qmt.py orders --cancelable - python qmt.py trades - python qmt.py tick 600000.SH 000001.SZ - python qmt.py kline 600000.SH --period 1d --count 60 - python qmt.py instrument 600000.SH - python qmt.py sector "沪深A股" - python qmt.py trading-dates --count 10 - python qmt.py north - python qmt.py longhubang 600000.SH --count 5 - python qmt.py buy 600000.SH 100 --price 7.50 --strategy my_strat - python qmt.py sell 600000.SH 100 --price 7.50 - python qmt.py cancel 12345 --market SH -""" - -from __future__ import annotations - -import argparse -import json -import os -import sys -import time -import traceback -from datetime import datetime -from pathlib import Path - - -# --------------------------------------------------------------------------- -# 路径自动发现:把仓库 src/ 加到 sys.path,确保开发模式也能 import -# --------------------------------------------------------------------------- -def _ensure_src_on_path() -> None: - here = Path(__file__).resolve().parent - # skill/scripts/qmt.py -> 上溯到仓库根 - for ancestor in [here, *here.parents]: - candidate = ancestor / "src" - if (candidate / "bigqmt_signal_trader" / "__init__.py").exists(): - src_str = str(candidate) - if src_str not in sys.path: - sys.path.insert(0, src_str) - return - # 没找到仓库 src,假设用户已 pip install - return - - -def _ensure_qmt_python_on_path() -> None: - """把 QMT 的 python 目录加到 sys.path,让 local_config.py 能被发现。 - - 这样客户端能读到 QMT 端的 transport=zmq 配置(服务端用 zmq 时客户端也得用)。 - 通过环境变量 BIGQMT_QMT_PYTHON_DIR 指定,或自动从常见路径/当前目录探测。 - """ - candidates = [] - env_dir = os.environ.get("BIGQMT_QMT_PYTHON_DIR") - if env_dir: - candidates.append(env_dir) - # 常见 QMT 安装路径(国金证券、华泰等) - for root in ("D:\\", "C:\\", "E:\\"): - if not os.path.isdir(root): - continue - try: - for entry in os.listdir(root): - if "QMT" in entry.upper(): - p = os.path.join(root, entry, "python") - if os.path.isdir(p): - candidates.append(p) - except Exception: - continue - # 当前工作目录(如果就在 QMT python 目录里) - cwd = os.getcwd() - if cwd.endswith(r"\python") or cwd.endswith("/python"): - candidates.append(cwd) - - for c in candidates: - local_cfg = os.path.join(c, "bigqmt_signal_trader_local_config.py") - if os.path.isfile(local_cfg): - if c not in sys.path: - sys.path.insert(0, c) - return - - -_ensure_src_on_path() -_ensure_qmt_python_on_path() - - -# --------------------------------------------------------------------------- -# 工具函数 -# --------------------------------------------------------------------------- -def _json_default(o): - """JSON 序列化兜底:处理 pandas / numpy / CompatObject / Decimal 等。""" - # pandas DataFrame / Series - if hasattr(o, "to_dict"): - try: - if hasattr(o, "index") and hasattr(o, "columns"): - # DataFrame -> list of dict records - return o.reset_index().to_dict(orient="records") - return o.to_dict() - except Exception: - pass - # numpy - if hasattr(o, "tolist"): - return o.tolist() - if hasattr(o, "isoformat"): - return o.isoformat() - # CompatObject(xtquant_compat 的属性包对象) - if hasattr(o, "__dict__") and not isinstance(o, type): - return {k: v for k, v in o.__dict__.items() if not k.startswith("_")} - if isinstance(o, (set, tuple)): - return list(o) - if isinstance(o, bytes): - return o.decode("utf-8", errors="replace") - return str(o) - - -def _print_json(data, indent=2): - print(json.dumps(data, ensure_ascii=False, indent=indent, default=_json_default)) - - -def _print_table(rows, headers=None): - """简易表格输出。""" - if not rows: - print("(empty)") - return - if isinstance(rows, list) and rows and isinstance(rows[0], dict): - headers = headers or list(rows[0].keys()) - lines = [] - widths = [max(len(str(h)), *(len(str(r.get(h, ""))) for r in rows)) for h in headers] - lines.append(" ".join(str(h).ljust(w) for h, w in zip(headers, widths))) - lines.append(" ".join("-" * w for w in widths)) - for r in rows: - lines.append(" ".join(str(r.get(h, "")).ljust(w) for h, w in zip(headers, widths))) - print("\n".join(lines)) - else: - for r in rows: - print(r) - - -def _ok(data, table=False, headers=None): - out = {"ok": True, "data": data, "ts": datetime.now().isoformat(timespec="seconds")} - if table: - _print_table(data if isinstance(data, list) else [data], headers) - else: - _print_json(out) - - -def _err(msg, detail=None, code=None): - out = {"ok": False, "error": msg, "ts": datetime.now().isoformat(timespec="seconds")} - if detail: - out["detail"] = detail - if code: - out["code"] = code - _print_json(out) - sys.exit(1) - - -def _position_to_dict(p): - """持仓对象转 dict,补充计算字段。""" - d = {} - for attr in [ - "account_id", "stock_code", "stock_name", "volume", "can_use_volume", - "available_amount", "enable_amount", "avg_price", "price", "open_price", - "cost_price", "market_value", "frozen_volume", "yesterday_volume", - "direction", - ]: - d[attr] = getattr(p, attr, None) - vol = d.get("volume") or 0 - price = d.get("price") or 0 - d["market_value"] = d.get("market_value") or round(vol * price, 2) - d["profit"] = None - if d.get("avg_price") and vol: - d["profit"] = round((price - d["avg_price"]) * vol, 2) - d["profit_pct"] = round((price - d["avg_price"]) / d["avg_price"] * 100, 2) if d["avg_price"] else 0 - return d - - -def _order_to_dict(o): - d = {} - for attr in [ - "account_id", "stock_code", "order_type", "order_status", - "order_volume", "traded_volume", "price", "order_sysid", "order_id", - "strategy_name", "order_remark", "order_time", - ]: - d[attr] = getattr(o, attr, None) - # 语义化 - d["order_type_name"] = {23: "BUY", 24: "SELL"}.get(d.get("order_type"), str(d.get("order_type", ""))) - status_map = { - 48: "UNREPORTED", 49: "WAIT_REPORTING", 50: "REPORTED", - 51: "REPORTED_CANCEL", 52: "PARTSUCC_CANCEL", 53: "PART_CANCEL", - 54: "CANCELED", 55: "PART_SUCC", 56: "SUCCEEDED", 57: "JUNK", 255: "UNKNOWN", - } - d["order_status_name"] = status_map.get(d.get("order_status"), str(d.get("order_status", ""))) - d["cancelable"] = d.get("order_status") in (49, 50, 55) - return d - - -def _trade_to_dict(t): - d = {} - for attr in [ - "account_id", "stock_code", "order_type", "order_sysid", "order_id", - "trade_id", "traded_volume", "traded_price", "traded_at", "order_remark", - ]: - d[attr] = getattr(t, attr, None) - d["order_type_name"] = {23: "BUY", 24: "SELL"}.get(d.get("order_type"), str(d.get("order_type", ""))) - return d - - -# --------------------------------------------------------------------------- -# 延迟初始化的兼容层对象 -# --------------------------------------------------------------------------- -_xt_trader = None -_xtdata = None -_acc = None - - -def _init(): - """延迟初始化 xt_trader / xtdata / acc,避免 import 失败时整个 CLI 崩溃。""" - global _xt_trader, _xtdata, _acc - if _xt_trader is not None: - return _xt_trader, _xtdata, _acc - try: - from bigqmt_signal_trader.xtquant_compat import ( - StockAccount, configure, xt_trader, xtdata, - ) - except ImportError as e: - _err( - "无法导入 bigqmt_signal_trader。请确保:\n" - " 1) 已 pip install xtquant-big-convert,或\n" - " 2) 仓库 src/ 在 PYTHONPATH 中,或\n" - " 3) 在仓库目录下运行", - detail=str(e), - code="IMPORT_FAIL", - ) - try: - configure() - except Exception as e: - _err( - "configure() 失败。请检查配置:\n" - " - 环境变量 BIGQMT_ACCOUNT_ID / BIGQMT_REDIS_HOST 等\n" - " - 或 bigqmt_signal_trader_client_config.py 配置文件", - detail=str(e), - code="CONFIG_FAIL", - ) - _xt_trader = xt_trader - _xtdata = xtdata - try: - _acc = StockAccount(xt_trader.client.account_id, "STOCK") - except Exception: - _acc = None - return _xt_trader, _xtdata, _acc - - -def _acc_or(acc_arg): - """用传入的 account_id 或全局 _acc。""" - from bigqmt_signal_trader.xtquant_compat import StockAccount - if acc_arg: - return StockAccount(acc_arg, "STOCK") - _, _, acc = _init() - if acc is None: - _err("无法确定 account_id,请用 --account 显式指定") - return acc - - -# =========================================================================== -# 子命令实现 -# =========================================================================== - -def cmd_ping(args): - tr, _, _ = _init() - t0 = time.time() - try: - result = tr.client.call("ping", {}) - except Exception as e: - _err("ping 失败", detail=str(e), code="PING_FAIL") - elapsed = round((time.time() - t0) * 1000, 1) - _ok({"result": result, "latency_ms": elapsed}) - - -def cmd_account(args): - tr, _, _ = _init() - acc = _acc_or(args.account) - try: - asset = tr.query_stock_asset(acc) - except Exception as e: - _err("查询资产失败", detail=str(e), code="QUERY_FAIL") - if asset is None: - _err("查询资产返回空") - d = { - "account_id": getattr(asset, "account_id", None), - "cash": getattr(asset, "cash", None), - "available_cash": getattr(asset, "available_cash", None), - "frozen_cash": getattr(asset, "frozen_cash", 0), - "total_asset": getattr(asset, "total_asset", None), - "market_value": getattr(asset, "market_value", None), - } - _ok(d, table=args.table, headers=["account_id", "cash", "frozen_cash", "market_value", "total_asset"]) - - -def cmd_positions(args): - tr, _, _ = _init() - acc = _acc_or(args.account) - try: - positions = tr.query_stock_positions(acc) - except Exception as e: - _err("查询持仓失败", detail=str(e), code="QUERY_FAIL") - if args.code: - positions = [p for p in (positions or []) if getattr(p, "stock_code", "") == args.code] - rows = [_position_to_dict(p) for p in (positions or [])] - # 汇总 - summary = { - "count": len(rows), - "total_market_value": round(sum(r.get("market_value") or 0 for r in rows), 2), - "total_profit": round(sum(r.get("profit") or 0 for r in rows), 2), - } - _ok({"positions": rows, "summary": summary}, table=args.table, - headers=["stock_code", "stock_name", "volume", "can_use_volume", "avg_price", "price", "market_value", "profit", "profit_pct"]) - - -def cmd_orders(args): - tr, _, _ = _init() - acc = _acc_or(args.account) - try: - orders = tr.query_stock_orders( - acc, - cancelable_only=args.cancelable, - strategy_name=args.strategy or "", - ) - except Exception as e: - _err("查询委托失败", detail=str(e), code="QUERY_FAIL") - rows = [_order_to_dict(o) for o in (orders or [])] - _ok({"orders": rows, "count": len(rows)}, table=args.table, - headers=["stock_code", "order_type_name", "order_status_name", "order_volume", "traded_volume", "price", "order_sysid", "cancelable"]) - - -def cmd_trades(args): - tr, _, _ = _init() - acc = _acc_or(args.account) - try: - trades = tr.query_stock_trades(acc, strategy_name=args.strategy or "") - except Exception as e: - _err("查询成交失败", detail=str(e), code="QUERY_FAIL") - rows = [_trade_to_dict(t) for t in (trades or [])] - _ok({"trades": rows, "count": len(rows)}, table=args.table, - headers=["stock_code", "order_type_name", "traded_volume", "traded_price", "traded_at", "order_sysid"]) - - -def cmd_tick(args): - _, xtdata, _ = _init() - codes = args.codes - if not codes: - _err("请指定股票代码,如: tick 600000.SH 000001.SZ") - try: - ticks = xtdata.get_full_tick(codes) - except Exception as e: - _err("查询行情失败", detail=str(e), code="QUERY_FAIL") - # 精简输出 - result = {} - for code, tick in (ticks or {}).items(): - t = dict(tick) if hasattr(tick, "items") else {} - # 只保留关键字段 - compact = { - "code": code, - "lastPrice": t.get("lastPrice"), - "open": t.get("open"), - "high": t.get("high"), - "low": t.get("low"), - "lastClose": t.get("lastClose"), - "volume": t.get("volume"), - "amount": t.get("amount"), - "bidPrice": (t.get("bidPrice") or [])[:5], - "bidVol": (t.get("bidVol") or [])[:5], - "askPrice": (t.get("askPrice") or [])[:5], - "askVol": (t.get("askVol") or [])[:5], - "time": t.get("time"), - "stime": t.get("stime"), - } - # 涨跌幅 - if t.get("lastClose") and t.get("lastPrice"): - compact["change_pct"] = round( - (t["lastPrice"] - t["lastClose"]) / t["lastClose"] * 100, 2 - ) - result[code] = compact - _ok(result, table=args.table, - headers=["code", "lastPrice", "change_pct", "bidPrice", "askPrice", "volume"]) - - -def cmd_kline(args): - _, xtdata, _ = _init() - fields = args.fields.split(",") if args.fields else None - try: - result = xtdata.get_market_data_ex( - field_list=fields, - stock_list=[args.code], - period=args.period, - start_time=args.start or "", - end_time=args.end or "", - count=args.count, - dividend_type=args.dividend, - fill_data=not args.no_fill, - ) - except Exception as e: - _err("查询 K 线失败", detail=str(e), code="QUERY_FAIL") - if not result or args.code not in result: - _err("未获取到 K 线数据", code="NO_DATA") - df = result[args.code] - if df is None or len(df) == 0: - _err("K 线数据为空") - # 转为 list of dict - records = df.reset_index().to_dict(orient="records") - # 精简大数字字段 - for r in records: - for k, v in list(r.items()): - if hasattr(v, "item"): - r[k] = v.item() - # 统计 - closes = [r.get("close") for r in records if r.get("close") is not None] - stats = {} - if closes: - stats["count"] = len(closes) - stats["first_close"] = closes[0] - stats["last_close"] = closes[-1] - stats["high"] = max(closes) - stats["low"] = min(closes) - stats["change_pct"] = round((closes[-1] - closes[0]) / closes[0] * 100, 2) if closes[0] else None - # 简单均线 - if len(closes) >= 5: - stats["ma5"] = round(sum(closes[-5:]) / 5, 3) - if len(closes) >= 20: - stats["ma20"] = round(sum(closes[-20:]) / 20, 3) - if len(closes) >= 60: - stats["ma60"] = round(sum(closes[-60:]) / 60, 3) - _ok({"code": args.code, "period": args.period, "bars": records, "stats": stats}, - table=args.table, headers=["time", "open", "high", "low", "close", "volume"]) - - -def cmd_instrument(args): - _, xtdata, _ = _init() - try: - detail = xtdata.get_instrument_detail(args.code) - except Exception as e: - _err("查询合约详情失败", detail=str(e), code="QUERY_FAIL") - if detail is None: - _err("未找到合约: %s" % args.code) - _ok(detail, table=args.table) - - -def cmd_sector(args): - _, xtdata, _ = _init() - if args.name: - try: - stocks = xtdata.get_stock_list_in_sector(args.name) - except Exception as e: - _err("查询板块成分股失败", detail=str(e), code="QUERY_FAIL") - _ok({"sector": args.name, "count": len(stocks or []), "stocks": stocks or []}) - else: - try: - sectors = xtdata.get_sector_list() - except Exception as e: - _err("查询板块列表失败", detail=str(e), code="QUERY_FAIL") - _ok({"sectors": sectors or []}) - - -def cmd_trading_dates(args): - _, xtdata, _ = _init() - try: - dates = xtdata.get_trading_dates( - market=args.market, - start_time=args.start or "", - end_time=args.end or "", - count=args.count if args.count > 0 else -1, - ) - except Exception as e: - _err("查询交易日历失败", detail=str(e), code="QUERY_FAIL") - _ok({"dates": dates or []}) - - -def cmd_north(args): - _, xtdata, _ = _init() - try: - data = xtdata.get_north_finance_change(period=args.period or "1d") - except Exception as e: - _err("查询北向资金失败", detail=str(e), code="QUERY_FAIL") - # data 可能是 dict[code -> DataFrame] - if isinstance(data, dict): - result = {} - for k, v in data.items(): - if hasattr(v, "to_dict"): - result[k] = v.reset_index().to_dict(orient="records") - else: - result[k] = v - elif hasattr(data, "to_dict"): - result = data.reset_index().to_dict(orient="records") - else: - result = data - _ok({"north_finance": result}) - - -def cmd_longhubang(args): - _, xtdata, _ = _init() - try: - df = xtdata.get_longhubang( - stock_list=[args.code], - start_time=args.start or "", - end_time=args.end or "", - count=args.count if args.count > 0 else 5, - ) - except Exception as e: - _err("查询龙虎榜失败", detail=str(e), code="QUERY_FAIL") - if df is None: - _err("龙虎榜数据为空") - if hasattr(df, "to_dict"): - records = df.reset_index().to_dict(orient="records") - else: - records = df - _ok({"longhubang": records}) - - -def cmd_financial(args): - _, xtdata, _ = _init() - tables = args.tables.split(",") if args.tables else ["Capital.CAPITAL"] - try: - data = xtdata.get_financial_data( - stock_list=args.codes, - table_list=tables, - start_time=args.start or "", - end_time=args.end or "", - ) - except Exception as e: - _err("查询财务数据失败", detail=str(e), code="QUERY_FAIL") - result = {} - for code, df in (data or {}).items(): - if hasattr(df, "to_dict"): - result[code] = df.reset_index().to_dict(orient="records") - else: - result[code] = df - _ok({"financial": result}) - - -def cmd_download(args): - _, xtdata, _ = _init() - try: - result = xtdata.download_history_data2( - stock_list=args.codes, - period=args.period, - start_time=args.start or "", - end_time=args.end or "", - dividend_type=args.dividend, - ) - except Exception as e: - _err("下载数据失败", detail=str(e), code="DOWNLOAD_FAIL") - _ok({"download_result": result}) - - -def _place_order(args, action): - """下单通用逻辑。action = 'BUY' 或 'SELL'。""" - tr, _, _ = _init() - acc = _acc_or(args.account) - from bigqmt_signal_trader.xtquant_compat import ( - STOCK_BUY, STOCK_SELL, FIX_PRICE, LATEST_PRICE, - ) - order_type = STOCK_BUY if action == "BUY" else STOCK_SELL - if args.latest: - price_type = LATEST_PRICE - price = 0.0 - else: - if args.price is None: - _err("限价单必须指定 --price,或用 --latest 使用最新价") - price_type = FIX_PRICE - price = args.price - strategy = args.strategy or "llm_agent" - remark = args.remark or "llm_%s_%d" % (action.lower(), int(time.time())) - # 干跑模式 - if args.dry_run: - _ok({ - "dry_run": True, - "action": action, - "stock_code": args.code, - "volume": args.volume, - "price": price, - "price_type": "LATEST" if args.latest else "LIMIT", - "strategy_name": strategy, - "order_remark": remark, - }) - # 真实下单 - try: - order_id = tr.order_stock( - acc, args.code, order_type, args.volume, - price_type, price, strategy, remark, - ) - except PermissionError as e: - _err("下单被拒绝:服务端未开启下单权限(rpc_allow_order_methods=False)", detail=str(e), code="ORDER_DISABLED") - except TimeoutError as e: - _err( - "下单超时——委托可能已提交但未收到响应。请先用 query_orders 确认,避免重复下单", - detail=str(e), code="ORDER_TIMEOUT", - ) - except Exception as e: - _err("下单失败", detail=str(e), code="ORDER_FAIL") - if order_id == -1: - _err("下单返回 -1(失败),请检查:1) 账户权限 2) 价格范围 3) QMT 风控", code="ORDER_REJECTED") - # 等 0.5 秒后查委托确认 - time.sleep(0.5) - try: - orders = tr.query_stock_orders(acc, strategy_name=strategy) - except Exception: - orders = None - placed_order = None - if orders: - for o in orders: - if getattr(o, "order_remark", "") == remark or getattr(o, "order_sysid", "") == str(order_id): - placed_order = _order_to_dict(o) - break - _ok({ - "order_sys_id": str(order_id), - "action": action, - "stock_code": args.code, - "volume": args.volume, - "price": price, - "strategy_name": strategy, - "order_remark": remark, - "confirmed_order": placed_order, - }) - - -def cmd_buy(args): - _place_order(args, "BUY") - - -def cmd_sell(args): - _place_order(args, "SELL") - - -def cmd_cancel(args): - tr, _, _ = _init() - acc = _acc_or(args.account) - if args.dry_run: - _ok({"dry_run": True, "order_sysid": args.order_id, "market": args.market or ""}) - try: - success = tr.cancel_order_stock_sysid(acc, args.market or "", args.order_id) - except Exception as e: - _err("撤单失败", detail=str(e), code="CANCEL_FAIL") - _ok({"order_sysid": args.order_id, "market": args.market or "", "success": bool(success)}) - - -def cmd_snapshot(args): - """一键快照:资产+持仓+今日委托+今日成交,一次返回。""" - tr, _, _ = _init() - acc = _acc_or(args.account) - result = {} - # 资产 - try: - asset = tr.query_stock_asset(acc) - if asset: - result["asset"] = { - "account_id": getattr(asset, "account_id", None), - "cash": getattr(asset, "cash", None), - "frozen_cash": getattr(asset, "frozen_cash", 0), - "total_asset": getattr(asset, "total_asset", None), - "market_value": getattr(asset, "market_value", None), - } - except Exception as e: - result["asset_error"] = str(e) - # 持仓 - try: - positions = tr.query_stock_positions(acc) - result["positions"] = [_position_to_dict(p) for p in (positions or [])] - result["position_count"] = len(result["positions"]) - except Exception as e: - result["position_error"] = str(e) - # 委托 - try: - orders = tr.query_stock_orders(acc, strategy_name="") - result["orders"] = [_order_to_dict(o) for o in (orders or [])] - result["order_count"] = len(result["orders"]) - except Exception as e: - result["order_error"] = str(e) - # 成交 - try: - trades = tr.query_stock_trades(acc, strategy_name="") - result["trades"] = [_trade_to_dict(t) for t in (trades or [])] - result["trade_count"] = len(result["trades"]) - except Exception as e: - result["trade_error"] = str(e) - _ok(result, table=args.table) - - -def cmd_rpc(args): - """通用 RPC 调用入口:任意白名单方法 + JSON 参数。 - - 用于调用没有专用子命令的 API(如 get_holidays、get_sector_info、 - get_hkt_statistics、bsm_price 等)。 - - 用法: - python qmt.py rpc get_holidays - python qmt.py rpc get_stock_name '{"stock":"600000.SH"}' - python qmt.py rpc bsm_price '{"opt_type":"C","target_price":3.0,"strike_price":2.8,"risk_free":0.03,"sigma":0.3,"days":30}' - """ - tr, _, _ = _init() - params = {} - if args.params: - try: - params = json.loads(args.params) - if not isinstance(params, dict): - _err("params 必须是 JSON 对象(如 '{\"key\":\"value\"}')", code="PARAM_ERROR") - except json.JSONDecodeError as e: - _err("params JSON 解析失败", detail=str(e), code="PARAM_ERROR") - try: - result = tr.client.call(args.method, params) - except Exception as e: - _err("RPC 调用失败: %s" % args.method, detail=str(e), code="RPC_FAIL") - _ok({"method": args.method, "result": result}, table=args.table) - - -def cmd_quote_subscribe(args): - """订阅全推行情(打印前 N 条后退出)。""" - _, xtdata, _ = _init() - received = [] - - def on_quote(data): - for code, tick in (data or {}).items(): - entry = { - "code": code, - "lastPrice": tick.get("lastPrice"), - "volume": tick.get("volume"), - "time": tick.get("time"), - } - received.append(entry) - print(json.dumps(entry, ensure_ascii=False)) - if len(received) >= args.max: - xtdata.unsubscribe_quote(sub_id) - # 给一点时间让退订生效 - time.sleep(0.5) - os._exit(0) - - try: - sub_id = xtdata.subscribe_whole_quote(args.codes, callback=on_quote) - except Exception as e: - _err("订阅行情失败", detail=str(e), code="SUBSCRIBE_FAIL") - print("# subscribed id=%s, waiting for quotes (max %d)..." % (sub_id, args.max), file=sys.stderr) - # 等待 - timeout = args.timeout - t0 = time.time() - try: - while time.time() - t0 < timeout: - time.sleep(0.5) - except KeyboardInterrupt: - pass - try: - xtdata.unsubscribe_quote(sub_id) - except Exception: - pass - _ok({"sub_id": sub_id, "received": received, "count": len(received)}) - - -# =========================================================================== -# argparse 路由 -# =========================================================================== - -def build_parser(): - p = argparse.ArgumentParser( - prog="qmt.py", - description="QMT 交易/行情统一 CLI(给大模型用)", - formatter_class=argparse.RawDescriptionHelpFormatter, - epilog=__doc__, - ) - p.add_argument("--account", default=None, help="指定账号 ID(覆盖配置)") - p.add_argument("--table", action="store_true", help="输出表格而非 JSON") - sub = p.add_subparsers(dest="command", required=True) - - # ping - sub.add_parser("ping", help="连通性检测").set_defaults(func=cmd_ping) - - # account - sub.add_parser("account", help="查询账户资产").set_defaults(func=cmd_account) - - # positions - sp = sub.add_parser("positions", help="查询持仓") - sp.add_argument("code", nargs="?", default=None, help="可选:只查指定股票") - sp.set_defaults(func=cmd_positions) - - # orders - sp = sub.add_parser("orders", help="查询今日委托") - sp.add_argument("--cancelable", action="store_true", help="只查可撤委托") - sp.add_argument("--strategy", default=None, help="按策略名过滤(空=全部)") - sp.set_defaults(func=cmd_orders) - - # trades - sp = sub.add_parser("trades", help="查询今日成交") - sp.add_argument("--strategy", default=None, help="按策略名过滤") - sp.set_defaults(func=cmd_trades) - - # tick - sp = sub.add_parser("tick", help="实时五档盘口") - sp.add_argument("codes", nargs="+", help="股票代码,如 600000.SH 000001.SZ") - sp.set_defaults(func=cmd_tick) - - # kline - sp = sub.add_parser("kline", help="K线/历史行情") - sp.add_argument("code", help="股票代码") - sp.add_argument("--period", default="1d", help="周期: 1d/1m/5m/15m/30m/60m/tick") - sp.add_argument("--count", type=int, default=-1, help="获取根数(-1=全部)") - sp.add_argument("--start", default=None, help="开始日期 YYYYMMDD") - sp.add_argument("--end", default=None, help="结束日期 YYYYMMDD") - sp.add_argument("--fields", default=None, help="字段逗号分隔,如 close,open,volume") - sp.add_argument("--dividend", default="none", help="复权: none/front/back") - sp.add_argument("--no-fill", action="store_true", help="不填充缺失数据") - sp.set_defaults(func=cmd_kline) - - # instrument - sp = sub.add_parser("instrument", help="合约详情") - sp.add_argument("code", help="股票代码") - sp.set_defaults(func=cmd_instrument) - - # sector - sp = sub.add_parser("sector", help="板块查询") - sp.add_argument("name", nargs="?", default=None, help="板块名(不填则列板块)") - sp.set_defaults(func=cmd_sector) - - # trading-dates - sp = sub.add_parser("trading-dates", help="交易日历") - sp.add_argument("--market", default="SH", help="市场 SH/SZ") - sp.add_argument("--count", type=int, default=10, help="获取天数") - sp.add_argument("--start", default=None) - sp.add_argument("--end", default=None) - sp.set_defaults(func=cmd_trading_dates) - - # north - sp = sub.add_parser("north", help="北向资金") - sp.add_argument("--period", default="1d") - sp.set_defaults(func=cmd_north) - - # longhubang - sp = sub.add_parser("longhubang", help="龙虎榜") - sp.add_argument("code", help="股票代码") - sp.add_argument("--count", type=int, default=5) - sp.add_argument("--start", default=None) - sp.add_argument("--end", default=None) - sp.set_defaults(func=cmd_longhubang) - - # financial - sp = sub.add_parser("financial", help="财务数据") - sp.add_argument("codes", nargs="+", help="股票代码") - sp.add_argument("--tables", default=None, help="表名逗号分隔,如 Capital.CAPITAL,Performance.EXPRESS") - sp.add_argument("--start", default=None) - sp.add_argument("--end", default=None) - sp.set_defaults(func=cmd_financial) - - # download - sp = sub.add_parser("download", help="下载历史数据到服务端") - sp.add_argument("codes", nargs="+", help="股票代码") - sp.add_argument("--period", default="1d") - sp.add_argument("--start", default=None) - sp.add_argument("--end", default=None) - sp.add_argument("--dividend", default="none") - sp.set_defaults(func=cmd_download) - - # buy - sp = sub.add_parser("buy", help="买入下单") - sp.add_argument("code", help="股票代码") - sp.add_argument("volume", type=int, help="委托数量(股)") - sp.add_argument("--price", type=float, default=None, help="限价单价格") - sp.add_argument("--latest", action="store_true", help="用最新价下单") - sp.add_argument("--strategy", default=None, help="策略名") - sp.add_argument("--remark", default=None, help="委托备注/user_order_id") - sp.add_argument("--dry-run", action="store_true", help="只打印不下单") - sp.set_defaults(func=cmd_buy) - - # sell - sp = sub.add_parser("sell", help="卖出下单") - sp.add_argument("code", help="股票代码") - sp.add_argument("volume", type=int, help="委托数量(股)") - sp.add_argument("--price", type=float, default=None, help="限价单价格") - sp.add_argument("--latest", action="store_true", help="用最新价下单") - sp.add_argument("--strategy", default=None, help="策略名") - sp.add_argument("--remark", default=None, help="委托备注/user_order_id") - sp.add_argument("--dry-run", action="store_true", help="只打印不下单") - sp.set_defaults(func=cmd_sell) - - # cancel - sp = sub.add_parser("cancel", help="撤单") - sp.add_argument("order_id", help="委托号 order_sysid") - sp.add_argument("--market", default=None, help="市场 SH/SZ") - sp.add_argument("--dry-run", action="store_true", help="只打印不撤单") - sp.set_defaults(func=cmd_cancel) - - # snapshot - sub.add_parser("snapshot", help="一键快照:资产+持仓+委托+成交").set_defaults(func=cmd_snapshot) - - # rpc — 通用 RPC 调用(兜底所有白名单方法) - sp = sub.add_parser("rpc", help="通用 RPC 调用(任意白名单方法 + JSON 参数)") - sp.add_argument("method", help="方法名,如 get_holidays / get_stock_name / bsm_price") - sp.add_argument("params", nargs="?", default=None, help='JSON 参数,如 \'{"stock":"600000.SH"}\'') - sp.set_defaults(func=cmd_rpc) - - # ---- 高频快捷命令(转发到 xtdata 对应方法) ---- - def _quick(name, help_text, method, params_builder): - def make(args): - tr, xtdata, _ = _init() - params = params_builder(args) - try: - fn = getattr(xtdata, method) - result = fn(**params) if isinstance(params, dict) else fn(*params) - except AttributeError: - # 方法不存在时回退到 RPC 调用 - result = tr.client.call(method, params if isinstance(params, dict) else {}) - except Exception as e: - _err("%s 失败" % name, detail=str(e), code="QUERY_FAIL") - _ok({"result": result}, table=getattr(args, "table", False)) - sp2 = sub.add_parser(name, help=help_text) - sp2.add_argument("args", nargs="*", help="位置参数(按方法签名顺序)") - sp2.set_defaults(func=make) - return sp2 - - # 节假日 - _quick("holiday", "节假日列表", "get_holidays", lambda a: {}) - # 股票名称 - _quick("stock-name", "股票名称", "get_stock_name", lambda a: {"stock": a.args[0] if a.args else _err("需传股票代码")}) - # 品种类型 - _quick("instrument-type", "品种类型(stock/fund/etf/bond/index)", "get_instrument_type", lambda a: {"stock_code": a.args[0] if a.args else _err("需传代码")}) - # 除权除息因子 - _quick("divid-factors", "除权除息因子", "get_divid_factors", lambda a: {"stock_code": a.args[0] if a.args else _err("需传代码"), "start_time": a.args[1] if len(a.args) > 1 else "", "end_time": a.args[2] if len(a.args) > 2 else ""}) - # 交易时段 - _quick("market-times", "日内交易时段", "get_trade_times", lambda a: {"stockcode": a.args[0] if a.args else "SH"}) - # 交易日历(含时段) - _quick("trading-calendar", "交易日历(含时段)", "get_trading_calendar", lambda a: {"market": a.args[0] if a.args else "SH", "start_time": a.args[1] if len(a.args) > 1 else "", "end_time": a.args[2] if len(a.args) > 2 else ""}) - # 期权列表 - _quick("option-list", "期权列表", "get_option_list", lambda a: {"undl_code": a.args[0] if a.args else _err("需传标的代码"), "dedate": a.args[1] if len(a.args) > 1 else ""}) - # BSM 期权定价 - _quick("bsm-price", "BSM 期权定价", "bsm_price", lambda a: {"opt_type": a.args[0] if a.args else "C", "target_price": float(a.args[1]) if len(a.args) > 1 else 3.0, "strike_price": float(a.args[2]) if len(a.args) > 2 else 2.8, "risk_free": float(a.args[3]) if len(a.args) > 3 else 0.03, "sigma": float(a.args[4]) if len(a.args) > 4 else 0.3, "days": int(a.args[5]) if len(a.args) > 5 else 30}) - # BSM 隐含波动率 - _quick("bsm-iv", "BSM 隐含波动率", "bsm_iv", lambda a: {"opt_type": a.args[0] if a.args else "C", "target_price": float(a.args[1]) if len(a.args) > 1 else 3.0, "strike_price": float(a.args[2]) if len(a.args) > 2 else 2.8, "option_price": float(a.args[3]) if len(a.args) > 3 else 0.25, "risk_free": float(a.args[4]) if len(a.args) > 4 else 0.03, "days": int(a.args[5]) if len(a.args) > 5 else 30}) - # 港股通统计 - _quick("hkt-stats", "港股通统计", "get_hkt_statistics", lambda a: {"stock_code": a.args[0] if a.args else _err("需传代码")}) - # 港股通明细 - _quick("hkt-details", "港股通明细", "get_hkt_details", lambda a: {"stock_code": a.args[0] if a.args else _err("需传代码")}) - # 港股通汇率 - _quick("hkt-rate", "港股通汇率", "get_hkt_exchange_rate", lambda a: {}) - # 十大股东 - _quick("top10-holder", "十大股东", "get_top10_share_holder", lambda a: {"stock_list": [a.args[0] if a.args else _err("需传代码")], "data_name": "holder", "start_time": a.args[1] if len(a.args) > 1 else "", "end_time": a.args[2] if len(a.args) > 2 else ""}) - # 股东户数 - _quick("holder-num", "股东户数", "get_holder_num", lambda a: {"stock_list": [a.args[0] if a.args else _err("需传代码")]}) - # 新股数据 - _quick("ipo", "新股数据", "get_ipo_data", lambda a: {}) - # 新股申购额度 - _quick("ipo-limit", "新股申购额度", "get_new_purchase_limit", lambda a: {}) - # 融资融券担保品 - _quick("credit-assure", "融资融券担保品合约", "get_assure_contract", lambda a: {}) - # 融资融券融券标的 - _quick("credit-short", "融券标的合约", "get_enable_short_contract", lambda a: {}) - # 负债合约 - _quick("credit-debt", "负债合约", "get_debt_contract", lambda a: {}) - # 历史 ST - _quick("his-st", "历史 ST 数据", "get_his_st_data", lambda a: {"stock_code": a.args[0] if a.args else _err("需传代码")}) - # 指数权重 - _quick("index-weight", "指数权重", "get_index_weight", lambda a: {"index_code": a.args[0] if a.args else _err("需传指数代码")}) - # 行业 - _quick("industry", "行业成分", "get_industry", lambda a: {"industry_name": a.args[0] if a.args else _err("需传行业名")}) - # 板块信息 - _quick("sector-info", "板块详情", "get_sector_info", lambda a: {"sector_name": a.args[0] if a.args else ""}) - # 时间转换 - _quick("timetag2dt", "毫秒时间戳转日期", "timetag_to_datetime", lambda a: {"timetag": int(a.args[0]) if a.args else _err("需传毫秒时间戳"), "format": a.args[1] if len(a.args) > 1 else "%Y%m%d %H:%M:%S"}) - _quick("dt2timetag", "日期转毫秒时间戳", "datetime_to_timetag", lambda a: {"datetime_str": a.args[0] if a.args else _err("需传日期字符串"), "format": a.args[1] if len(a.args) > 1 else "%Y%m%d%H%M%S"}) - # 本地数据(缓存) - _quick("local-data", "本地缓存数据", "get_local_data", lambda a: {"field_list": ["close"], "stock_list": [a.args[0] if a.args else _err("需传代码")], "period": a.args[1] if len(a.args) > 1 else "1d", "count": -1}) - - # quote-subscribe - sp = sub.add_parser("quote-subscribe", help="订阅全推行情(实时推送)") - sp.add_argument("codes", nargs="+", help="代码或市场,如 SH SZ 600000.SH") - sp.add_argument("--max", type=int, default=10, help="收到 N 条后退出") - sp.add_argument("--timeout", type=int, default=30, help="超时秒数") - sp.set_defaults(func=cmd_quote_subscribe) - - return p - - -def main(): - parser = build_parser() - args = parser.parse_args() - if not hasattr(args, "func"): - parser.print_help() - sys.exit(1) - try: - args.func(args) - except SystemExit: - raise - except KeyboardInterrupt: - print("\n(interrupted)", file=sys.stderr) - sys.exit(130) - except Exception as e: - _err("未预期错误", detail=traceback.format_exc(), code="UNEXPECTED") - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/run_all_tests.py b/reference/xtquant_big_convert/run_all_tests.py deleted file mode 100644 index 544f154..0000000 --- a/reference/xtquant_big_convert/run_all_tests.py +++ /dev/null @@ -1,108 +0,0 @@ -# coding: utf-8 -"""Run the full test suite from a single entry point. - -Groups tests by area and prints a clear per-group + total report. Optional -live/API tests (need a running QMT + redis) are skipped by default. - -Usage: - python run_all_tests.py # all offline tests (default) - python run_all_tests.py -v # verbose - python run_all_tests.py --live # also run live RPC tests (needs QMT running) - python run_all_tests.py --group signal_trader # only one group - python run_all_tests.py --group backtest -""" -import argparse -import os -import subprocess -import sys -import time - -ROOT = os.path.dirname(os.path.abspath(__file__)) -SRC = os.path.join(ROOT, "src") - -# Test groups: (name, paths, requires_live) -GROUPS = [ - ("signal_trader", [os.path.join("tests", "bigqmt_signal_trader")], False), - ("backtest", [os.path.join("tests", "bigqmt_backtest")], False), -] -LIVE_GROUP = ("live_api", ["test_all_apis.py"], True) - - -def run_group(name, paths, verbose): - """Run a pytest group, return (passed, failed, skipped, seconds).""" - cmd = [sys.executable, "-m", "pytest"] + paths + ["-q" if not verbose else "-v"] - t0 = time.time() - proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True) - elapsed = time.time() - t0 - out = (proc.stdout or "") + (proc.stderr or "") - # Parse pytest summary like "290 passed in 8.5s" / "1 failed, 289 passed, 3 skipped" - passed = failed = skipped = 0 - for line in out.splitlines(): - line = line.strip() - if not any(tok in line for tok in ("passed", "failed", "skipped", "error")): - continue - for part in line.split(","): - words = part.strip().split() - if len(words) >= 2 and words[0].isdigit(): - num = int(words[0]) - if words[1].startswith("passed"): - passed += num - elif words[1].startswith("failed") or words[1].startswith("error"): - failed += num - elif words[1].startswith("skipped"): - skipped += num - return passed, failed, skipped, elapsed, out, proc.returncode - - -def main(): - parser = argparse.ArgumentParser(description="Run all bigqmt tests") - parser.add_argument("-v", "--verbose", action="store_true") - parser.add_argument("--live", action="store_true", help="also run live RPC tests (needs QMT)") - parser.add_argument("--group", help="run only this group (signal_trader/backtest/live_api)") - args = parser.parse_args() - - groups = list(GROUPS) - if args.live: - groups.append(LIVE_GROUP) - if args.group: - groups = [g for g in groups if g[0] == args.group] - if not groups: - print("Unknown group: %s (available: %s)" % (args.group, ", ".join(g[0] for g in GROUPS + [LIVE_GROUP]))) - return 1 - - print("=" * 70) - print("Big QMT Bridge - 全量测试") - print("=" * 70) - - total_passed = total_failed = total_skipped = 0 - total_time = 0.0 - failed_groups = [] - for name, paths, needs_live in groups: - print("\n--- %s ---" % name) - passed, failed, skipped, elapsed, out, rc = run_group(name, paths, args.verbose) - total_passed += passed - total_failed += failed - total_skipped += skipped - total_time += elapsed - status = "PASS" if failed == 0 and rc == 0 else "FAIL" - print(" %s: %d passed, %d failed, %d skipped (%.1fs)" % (status, passed, failed, skipped, elapsed)) - if failed or rc != 0: - failed_groups.append(name) - if not args.verbose: - # print the failing part of the output for visibility - tail = "\n".join(out.splitlines()[-20:]) - print(tail) - - print("\n" + "=" * 70) - print("=== 汇总 ===") - print("通过 %d / 失败 %d / 跳过 %d / 总计 %d" % (total_passed, total_failed, total_skipped, total_passed + total_failed + total_skipped)) - print("总耗时 %.1fs" % total_time) - if failed_groups: - print("失败分组: %s" % ", ".join(failed_groups)) - return 1 - print("全部通过 ✅") - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/reference/xtquant_big_convert/src/BIGQMT_REDIS_DRYRUN.py b/reference/xtquant_big_convert/src/BIGQMT_REDIS_DRYRUN.py deleted file mode 100644 index df706b1..0000000 --- a/reference/xtquant_big_convert/src/BIGQMT_REDIS_DRYRUN.py +++ /dev/null @@ -1,254 +0,0 @@ -#coding:gbk -"""QMT bridge entry using the same file-loader pattern as qmt_realtime strategies. - -Broker QMT strategy sandboxes may reject local package names through their -normal ``import`` allowlist. The realtime QMT strategies in gupiao_ztfx load -their colocated helpers through ``importlib.util.spec_from_file_location``. -This entry applies path-based loading to the bridge package, including its -internal relative imports, while leaving all standard-library and QMT imports -untouched. This terminal's spec loader ignores custom builtins for nested -package imports, so local bridge files are compiled explicitly after resolving -their path. -""" -import builtins as _builtins -import importlib as _importlib -import os -import sys -import types - - -_LOCAL_ROOTS = ( - "bigqmt_signal_trader", - "bigqmt_signal_trader_strategy", - "bigqmt_signal_trader_redis_rpc_runtime", - "bigqmt_signal_trader_local_config", -) -_ORIGINAL_IMPORT = _builtins.__import__ -_ORIGINAL_IMPORT_MODULE = _importlib.import_module -_ORIGINAL_RELOAD = _importlib.reload - - -def _known_qmt_python_dir(): - # Find the QMT python dir from sys.path instead of a hardcoded path, so - # the bridge loads regardless of broker install location or launch mode - # (editor / paste-run / exec). Falls back to empty when not found. - for p in sys.path: - if p and r"\python" in p and os.path.isdir(p): - return p - return "" - - -try: - _SOURCE_ROOT = os.path.dirname(os.path.abspath(__file__)) -except Exception: - _SOURCE_ROOT = _known_qmt_python_dir() -if not _SOURCE_ROOT: - _SOURCE_ROOT = _known_qmt_python_dir() - - -def _is_local_module(name): - return any(name == root or name.startswith(root + ".") for root in _LOCAL_ROOTS) - - -def _resolve_name(name, module_globals, level): - if not level: - return name - package = (module_globals or {}).get("__package__") or (module_globals or {}).get("__name__", "") - if not package: - raise ImportError("relative import without package") - for unused in range(level - 1): - if "." not in package: - raise ImportError("relative import beyond top-level package") - package = package.rsplit(".", 1)[0] - return package + ("." + name if name else "") - - -def _find_local_source(name): - relative = name.replace(".", os.sep) - dirs = [] - if _SOURCE_ROOT: - dirs.append(_SOURCE_ROOT) - for p in sys.path: - if p and os.path.isdir(p) and p not in dirs: - dirs.append(p) - for d in dirs: - package_init = os.path.join(d, relative, "__init__.py") - if os.path.isfile(package_init): - return package_init, True - module_file = os.path.join(d, relative + ".py") - if os.path.isfile(module_file): - return module_file, False - raise ModuleNotFoundError("local source not found: %s" % name, name=name) - - -def _set_parent_attribute(name, module): - if "." not in name: - return - parent_name, child_name = name.rsplit(".", 1) - parent = _load_local_module(parent_name) - setattr(parent, child_name, module) - - -def _load_local_module(name): - existing = sys.modules.get(name) - if existing is not None: - return existing - source_path, is_package = _find_local_source(name) - if "." in name: - _load_local_module(name.rsplit(".", 1)[0]) - module = types.ModuleType(name) - module.__file__ = source_path - module.__package__ = name if is_package else name.rpartition(".")[0] - if is_package: - module.__path__ = [os.path.dirname(source_path)] - module_builtins = dict(_builtins.__dict__) - module_builtins["__import__"] = _local_import - module.__dict__["__builtins__"] = module_builtins - module.__dict__["__bigqmt_load_local_module"] = _load_local_module - sys.modules[name] = module - # QMT native allowlist rejects the root package eager exports. - if name == "bigqmt_signal_trader": - return module - try: - with open(source_path, "rb") as source_file: - source = source_file.read() - exec(compile(source, source_path, "exec"), module.__dict__) - except Exception: - sys.modules.pop(name, None) - raise - _set_parent_attribute(name, module) - return module - - -def _local_import(name, module_globals=None, module_locals=None, fromlist=(), level=0): - absolute_name = _resolve_name(name, module_globals, level) - if not _is_local_module(absolute_name): - return _ORIGINAL_IMPORT(name, module_globals, module_locals, fromlist, level) - module = _load_local_module(absolute_name) - for child in fromlist or (): - if child != "*": - try: - _load_local_module(absolute_name + "." + child) - except ModuleNotFoundError: - pass - if fromlist: - return module - return _load_local_module(absolute_name.split(".", 1)[0]) - - -def _local_import_module(name, package=None): - if _is_local_module(name): - return _load_local_module(name) - return _ORIGINAL_IMPORT_MODULE(name, package) - - -def _local_reload(module): - if _is_local_module(getattr(module, "__name__", "")): - return _load_local_module(module.__name__) - return _ORIGINAL_RELOAD(module) - - -def _clear_local_modules(): - for name in list(sys.modules): - if _is_local_module(name): - sys.modules.pop(name, None) - - -def _stop_previous_rpc_service(): - """Release the previous QMT strategy's socket before clearing its module. - - QMT can re-execute this entry in the same Python process. The old strategy - module owns the RPC service and its ZMQ ROUTER socket, so dropping that - module from ``sys.modules`` first would make the service unreachable and - leave its port bound for the next strategy start. - """ - previous = sys.modules.get("bigqmt_signal_trader_strategy") - reset = getattr(previous, "reset_app", None) - if not callable(reset): - return - try: - reset() - print("[bigqmt_shell] previous rpc service stopped") - except Exception as exc: - # Continue the reload so a broken old instance does not prevent QMT - # from reporting its normal startup error. - print("[bigqmt_shell] previous rpc service stop failed: %s" % exc) - - -_stop_previous_rpc_service() -_clear_local_modules() -_importlib.import_module = _local_import_module -_importlib.reload = _local_reload -print("[bigqmt_shell] importlib entry source_root=%s" % _SOURCE_ROOT) - - -def _fallback_account_id(): - for name in ("BIGQMT_ACCOUNT_ID", "account", "account_id", "accountID"): - value = globals().get(name) - if value: - return str(value) - return "" - - -try: - _local_import("bigqmt_signal_trader.adapters.redis_common", globals(), fromlist=("*",)) - _local_import("bigqmt_signal_trader.redis_rpc", globals(), fromlist=("*",)) - _strategy = _local_import("bigqmt_signal_trader_strategy", globals(), fromlist=("*",)) - _strategy.reset_app() -except Exception as bridge_preload_error: - print("[bigqmt_shell] bridge preload failed: %s" % bridge_preload_error) - -_runtime = _local_import("bigqmt_signal_trader_redis_rpc_runtime", globals(), fromlist=("*",)) - - -def _load_local_config(): - return _local_import("bigqmt_signal_trader_local_config", globals(), fromlist=("*",)) - - -try: - _config = _load_local_config() - BIGQMT_REDIS_CONFIG = getattr(_config, "BIGQMT_REDIS_CONFIG", {}) - print("[bigqmt_shell] local redis config loaded keys=%s" % sorted((BIGQMT_REDIS_CONFIG or {}).keys())) - _runtime.configure_runtime_redis(BIGQMT_REDIS_CONFIG) -except Exception as redis_config_error: - print("[bigqmt_shell] local redis config load failed: %s" % redis_config_error) - -try: - _config = _load_local_config() - BIGQMT_ACCOUNT_ID = getattr(_config, "BIGQMT_ACCOUNT_ID", "") - print("[bigqmt_shell] local account config loaded=%s" % bool(BIGQMT_ACCOUNT_ID)) - _runtime.configure_runtime_account(BIGQMT_ACCOUNT_ID) -except Exception as account_config_error: - print("[bigqmt_shell] local account config load failed: %s" % account_config_error) - account_id = _fallback_account_id() - if account_id: - _runtime.configure_runtime_account(account_id) - -try: - qmt_extra = {} - for function_name in ( - "get_history_trade_detail_data", "get_value_by_order_id", "get_last_order_id", - "get_ipo_data", "get_new_purchase_limit", "get_assure_contract", - "get_enable_short_contract", "get_unclosed_compacts", "get_closed_compacts", - "get_debt_contract", "get_option_subject_position", "get_comb_option", - "get_hkt_exchange_rate", - "download_history_data", "download_history_data2", - ): - if function_name in globals(): - qmt_extra[function_name] = globals()[function_name] - print("[bigqmt_shell] down_history_data bound=%s" % ("down_history_data" in qmt_extra)) - _runtime.bind_runtime_api( - passorder_func=globals().get("passorder"), - cancel_func=globals().get("cancel"), - get_trade_detail_data_func=globals().get("get_trade_detail_data"), - extra_funcs=qmt_extra or None, - ) -except NameError: - pass - - -init = _runtime.init -handlebar = _runtime.handlebar -adjust = _runtime.adjust -order_callback = _runtime.order_callback -deal_callback = _runtime.deal_callback diff --git a/reference/xtquant_big_convert/src/BIGQMT_ZMQ_BACKTEST.py b/reference/xtquant_big_convert/src/BIGQMT_ZMQ_BACKTEST.py deleted file mode 100644 index 77a7cce..0000000 --- a/reference/xtquant_big_convert/src/BIGQMT_ZMQ_BACKTEST.py +++ /dev/null @@ -1,152 +0,0 @@ -#coding:gbk -"""Isolated QMT backtest entry for external ZMQ strategies. - -This file is ASCII-only. It loads only the bigqmt_backtest package and never -loads or mutates the live bridge package. -""" - -import builtins as _builtins -import os -import sys -import types - - -BACKTEST_ZMQ_CONFIG = { - "bind_endpoint": "tcp://127.0.0.1:16662", - "run_id": "", - "account_id": "", - "account_type": "STOCK", - "strategy_name": "ZMQ_BACKTEST", - "combo_type": 1101, - "quick_trade": 2, - "market_price_type": 5, - "limit_price_type": 11, - "bar_wait_timeout_seconds": 60, - "require_qmt_backtest": True, -} - - -_LOCAL_ROOT = "bigqmt_backtest" -_ORIGINAL_IMPORT = _builtins.__import__ - - -def _known_qmt_python_dir(): - for p in sys.path: - if p and r"\python" in p and os.path.isdir(p): - return p - return "" - - -try: - _SOURCE_ROOT = os.path.dirname(os.path.abspath(__file__)) -except Exception: - _SOURCE_ROOT = _known_qmt_python_dir() -if not _SOURCE_ROOT: - _SOURCE_ROOT = _known_qmt_python_dir() - - -def _is_local(name): - return name == _LOCAL_ROOT or name.startswith(_LOCAL_ROOT + ".") - - -def _resolve_name(name, module_globals, level): - if not level: - return name - package = (module_globals or {}).get("__package__") or "" - if not package: - raise ImportError("relative import without package") - for unused in range(level - 1): - package = package.rsplit(".", 1)[0] - return package + (("." + name) if name else "") - - -def _find_source(name): - relative = name.replace(".", os.sep) - dirs = [] - if _SOURCE_ROOT: - dirs.append(_SOURCE_ROOT) - for p in sys.path: - if p and os.path.isdir(p) and p not in dirs: - dirs.append(p) - for d in dirs: - package_init = os.path.join(d, relative, "__init__.py") - if os.path.isfile(package_init): - return package_init, True - module_file = os.path.join(d, relative + ".py") - if os.path.isfile(module_file): - return module_file, False - raise ModuleNotFoundError("local source not found: %s" % name, name=name) - - -def _load_local_module(name): - existing = sys.modules.get(name) - if existing is not None: - return existing - source_path, is_package = _find_source(name) - if "." in name: - _load_local_module(name.rsplit(".", 1)[0]) - module = types.ModuleType(name) - module.__file__ = source_path - module.__package__ = name if is_package else name.rpartition(".")[0] - if is_package: - module.__path__ = [os.path.dirname(source_path)] - module_builtins = dict(_builtins.__dict__) - module_builtins["__import__"] = _local_import - module.__dict__["__builtins__"] = module_builtins - sys.modules[name] = module - if name == _LOCAL_ROOT: - return module - try: - with open(source_path, "rb") as source_file: - source = source_file.read() - exec(compile(source, source_path, "exec"), module.__dict__) - except Exception: - sys.modules.pop(name, None) - raise - if "." in name: - parent_name, child_name = name.rsplit(".", 1) - setattr(_load_local_module(parent_name), child_name, module) - return module - - -def _local_import(name, module_globals=None, module_locals=None, fromlist=(), level=0): - absolute_name = _resolve_name(name, module_globals, level) - if not _is_local(absolute_name): - return _ORIGINAL_IMPORT(name, module_globals, module_locals, fromlist, level) - module = _load_local_module(absolute_name) - for child in fromlist or (): - if child != "*": - try: - _load_local_module(absolute_name + "." + child) - except ModuleNotFoundError: - pass - if fromlist: - return module - return _load_local_module(absolute_name.split(".", 1)[0]) - - -for _name in sorted( - [name for name in list(sys.modules) if _is_local(name)], - key=lambda item: item.count("."), - reverse=True, -): - sys.modules.pop(_name, None) - - -_runtime = _load_local_module("bigqmt_backtest.qmt_runtime") -_runtime.configure(**BACKTEST_ZMQ_CONFIG) -_runtime.bind_qmt_api( - passorder_func=globals().get("passorder") or getattr(_builtins, "passorder", None), - cancel_func=globals().get("cancel") or getattr(_builtins, "cancel", None), - get_trade_detail_data_func=( - globals().get("get_trade_detail_data") - or getattr(_builtins, "get_trade_detail_data", None) - ), -) - -init = _runtime.init -handlebar = _runtime.handlebar -order_callback = _runtime.order_callback -deal_callback = _runtime.deal_callback -stop = _runtime.stop -after_backtest = _runtime.after_backtest diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/__init__.py b/reference/xtquant_big_convert/src/bigqmt_backtest/__init__.py deleted file mode 100644 index 2a8a2b6..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/__init__.py +++ /dev/null @@ -1,23 +0,0 @@ -"""Isolated ZMQ bridge for QMT-native and standalone backtests. - -This package deliberately does not import ``bigqmt_signal_trader``. The live -bridge and both backtest backends therefore have separate module state, -identities, and order gateways. QMT-native mode never uses the local broker. -""" - -from .client import BacktestZmqClient -from .data_feed import CsvBarFeed, InMemoryBarFeed -from .engine import BacktestConfig, BacktestEngine -from .protocol import BacktestBridgeProtocol - - -__all__ = [ - "BacktestBridgeProtocol", - "BacktestConfig", - "BacktestEngine", - "BacktestZmqClient", - "CsvBarFeed", - "InMemoryBarFeed", -] - -__version__ = "1.0.0" diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/__main__.py b/reference/xtquant_big_convert/src/bigqmt_backtest/__main__.py deleted file mode 100644 index 5b9eec7..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/__main__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .server import main - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/broker.py b/reference/xtquant_big_convert/src/bigqmt_backtest/broker.py deleted file mode 100644 index 947d1b2..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/broker.py +++ /dev/null @@ -1,358 +0,0 @@ -"""A-share simulated broker used only by the standalone backtest runtime.""" - -import json -from decimal import Decimal - -from .models import ( - BacktestFill, - BacktestOrder, - Position, - ZERO, - decimal_value, - json_number, - money, - normalize_symbol, - round_price, -) - - -ACTIVE_ORDER_STATUSES = ("PENDING", "PARTIALLY_FILLED") - - -class SimulatedBroker(object): - def __init__(self, config): - self.config = config - self.cash = money(config.initial_cash) - self.positions = {} - for symbol, payload in dict(config.initial_positions or {}).items(): - data = dict(payload or {}) - position = Position( - symbol, - quantity=data.get("quantity", data.get("volume", 0)), - available=data.get("available"), - today_buy=data.get("today_buy", 0), - avg_cost=data.get("avg_cost", data.get("cost", 0)), - ) - if position.quantity > 0: - self.positions[position.symbol] = position - self.orders_list = [] - self.fills_list = [] - self._client_order_ids = {} - self._client_order_fingerprints = {} - self._order_sequence = 0 - self._fill_sequence = 0 - self._trading_date = None - self.total_fees = ZERO - self.turnover = ZERO - - def _new_order(self, payload, frame_index, submitted_at): - self._order_sequence += 1 - return BacktestOrder( - order_id="bt-order-%06d" % self._order_sequence, - client_order_id=payload.get("client_order_id"), - symbol=payload.get("symbol"), - side=payload.get("side"), - quantity=payload.get("quantity"), - order_type=payload.get("order_type", "MARKET"), - limit_price=payload.get("limit_price", payload.get("price")), - submitted_index=frame_index, - submitted_at=submitted_at, - time_in_force=payload.get("time_in_force", self.config.time_in_force), - ) - - def _reject(self, order, reason): - order.status = "REJECTED" - order.reject_reason = str(reason) - return order - - def _reserved_sell(self, symbol): - return sum( - order.remaining - for order in self.orders_list - if order.symbol == symbol and order.side == "SELL" and order.status in ACTIVE_ORDER_STATUSES - ) - - def submit(self, payload, frame_index, submitted_at): - payload = dict(payload or {}) - client_order_id = str(payload.get("client_order_id") or "") - if client_order_id and client_order_id in self._client_order_ids: - limit_value = payload.get("limit_price", payload.get("price")) - fingerprint_payload = { - "symbol": normalize_symbol(payload.get("symbol")), - "side": str(payload.get("side") or "").upper(), - "quantity": int(payload.get("quantity") or 0), - "order_type": str(payload.get("order_type") or "MARKET").upper(), - "limit_price": None if limit_value in (None, "") else float(decimal_value(limit_value)), - "time_in_force": str(payload.get("time_in_force", self.config.time_in_force)).upper(), - } - fingerprint = json.dumps(fingerprint_payload, sort_keys=True, separators=(",", ":")) - if self._client_order_fingerprints.get(client_order_id) != fingerprint: - raise ValueError("client_order_id reused with different order payload") - return self._client_order_ids[client_order_id] - try: - symbol = normalize_symbol(payload.get("symbol")) - side = str(payload.get("side") or "").upper() - quantity = int(payload.get("quantity") or 0) - order_type = str(payload.get("order_type") or "MARKET").upper() - if side not in ("BUY", "SELL"): - raise ValueError("side must be BUY or SELL") - if quantity <= 0: - raise ValueError("quantity must be positive") - if order_type not in ("MARKET", "LIMIT"): - raise ValueError("order_type must be MARKET or LIMIT") - if order_type == "LIMIT" and decimal_value(payload.get("limit_price", payload.get("price"))) <= 0: - raise ValueError("positive limit_price is required for LIMIT order") - payload.update({"symbol": symbol, "side": side, "quantity": quantity, "order_type": order_type}) - order = self._new_order(payload, frame_index, submitted_at) - except Exception as exc: - self._order_sequence += 1 - order = BacktestOrder( - "bt-order-%06d" % self._order_sequence, - client_order_id, - payload.get("symbol") or "UNKNOWN", - payload.get("side") or "UNKNOWN", - int(payload.get("quantity") or 0), - payload.get("order_type") or "MARKET", - payload.get("limit_price", payload.get("price")), - frame_index, - submitted_at, - payload.get("time_in_force", self.config.time_in_force), - ) - self._reject(order, "invalid_order:%s" % exc) - self.orders_list.append(order) - return order - - self.orders_list.append(order) - if client_order_id: - self._client_order_ids[client_order_id] = order - fingerprint_payload = { - "symbol": order.symbol, - "side": order.side, - "quantity": order.quantity, - "order_type": order.order_type, - "limit_price": None if order.limit_price is None else float(order.limit_price), - "time_in_force": order.time_in_force, - } - self._client_order_fingerprints[client_order_id] = json.dumps( - fingerprint_payload, sort_keys=True, separators=(",", ":") - ) - lot_size = self.config.lot_size - if order.side == "BUY" and order.quantity % lot_size: - return self._reject(order, "buy_quantity_not_round_lot") - if order.side == "SELL": - position = self.positions.get(order.symbol) - available = 0 if position is None else max(position.available - self._reserved_sell(order.symbol) + order.quantity, 0) - if available <= 0: - return self._reject(order, "t_plus_one_unavailable") - if order.quantity > available: - return self._reject(order, "insufficient_sellable") - if order.quantity % lot_size and order.quantity != available: - return self._reject(order, "sell_quantity_not_round_lot") - return order - - def cancel(self, order_id): - for order in self.orders_list: - if order.order_id == str(order_id) or order.client_order_id == str(order_id): - if order.status not in ACTIVE_ORDER_STATUSES: - return order - order.status = "CANCELLED" - order.reject_reason = "cancelled_by_strategy" - return order - raise KeyError("order not found: %s" % order_id) - - def _settle_trading_day(self, trading_date): - if self._trading_date == trading_date: - return - if self._trading_date is not None: - for position in self.positions.values(): - position.available = position.quantity - position.today_buy = 0 - self._trading_date = trading_date - - def _limits(self, order, bar): - prev_close = decimal_value(bar.get("prev_close") or bar.get("close")) - if bar.get("price_limit_rate") not in (None, ""): - rate = decimal_value(bar.get("price_limit_rate")) - else: - pure = order.symbol.split(".", 1)[0] - if order.symbol.endswith(".BJ"): - rate = Decimal("0.30") - elif pure.startswith(("300", "301", "688", "689")): - rate = Decimal("0.20") - else: - rate = decimal_value(self.config.price_limit_rate) - up_limit = bar.get("up_limit") - down_limit = bar.get("down_limit") - up_limit = round_price(order.symbol, up_limit if up_limit not in (None, "") else prev_close * (Decimal("1") + rate)) - down_limit = round_price(order.symbol, down_limit if down_limit not in (None, "") else prev_close * (Decimal("1") - rate)) - return up_limit, down_limit - - def _match_price(self, order, bar): - if bool(bar.get("suspended")) or float(bar.get("volume") or 0) <= 0: - return None, "suspended_or_no_volume" - open_price = round_price(order.symbol, bar["open"]) - high = round_price(order.symbol, bar["high"]) - low = round_price(order.symbol, bar["low"]) - up_limit, down_limit = self._limits(order, bar) - if order.side == "BUY" and open_price == high == low == up_limit: - return None, "limit_up_locked" - if order.side == "SELL" and open_price == high == low == down_limit: - return None, "limit_down_locked" - if order.order_type == "LIMIT" and not down_limit <= order.limit_price <= up_limit: - return None, "limit_price_outside_daily_range" - if order.order_type == "MARKET": - price = open_price - elif order.side == "BUY": - if low > order.limit_price: - return None, "limit_not_crossed" - price = min(open_price, order.limit_price) - else: - if high < order.limit_price: - return None, "limit_not_crossed" - price = max(open_price, order.limit_price) - slip = decimal_value(self.config.slippage_bps) / Decimal("10000") - if order.side == "BUY": - price = min(round_price(order.symbol, price * (Decimal("1") + slip)), up_limit) - else: - price = max(round_price(order.symbol, price * (Decimal("1") - slip)), down_limit) - return price, "" - - def _fees(self, side, amount): - rate = self.config.buy_commission_rate if side == "BUY" else self.config.sell_commission_rate - commission = max(amount * decimal_value(rate), decimal_value(self.config.min_commission)) if rate else ZERO - stamp = amount * decimal_value(self.config.stamp_tax_rate) if side == "SELL" else ZERO - transfer = amount * decimal_value(self.config.transfer_fee_rate) - return money(commission), money(stamp), money(transfer) - - def _volume_cap(self, order, bar, used_volume=0): - raw = int(float(bar.get("volume") or 0) * float(self.config.max_volume_participation)) - cap = max((raw // self.config.lot_size) * self.config.lot_size - int(used_volume), 0) - return min(order.remaining, cap) - - def _affordable_buy_quantity(self, quantity, price): - quantity = (int(quantity) // self.config.lot_size) * self.config.lot_size - while quantity > 0: - amount = money(price * quantity) - fees = sum(self._fees("BUY", amount), ZERO) - if self.cash >= amount + fees: - return quantity - quantity -= self.config.lot_size - return 0 - - def _apply_fill(self, order, quantity, price, frame_index, filled_at): - amount = money(price * quantity) - commission, stamp, transfer = self._fees(order.side, amount) - self._fill_sequence += 1 - fill = BacktestFill( - "bt-fill-%06d" % self._fill_sequence, - order, - quantity, - price, - commission, - stamp, - transfer, - frame_index, - filled_at, - ) - fees = fill.total_fee - position = self.positions.get(order.symbol) - if order.side == "BUY": - if position is None: - position = Position(order.symbol) - self.positions[order.symbol] = position - old_cost = position.avg_cost * position.quantity - self.cash = money(self.cash - amount - fees) - position.quantity += quantity - position.today_buy += quantity - position.avg_cost = (old_cost + amount + fees) / position.quantity - else: - if position is None or position.available < quantity: - raise RuntimeError("sellable quantity changed before fill") - self.cash = money(self.cash + amount - fees) - position.quantity -= quantity - position.available -= quantity - position.realized_pnl += amount - fees - position.avg_cost * quantity - if position.quantity <= 0: - self.positions.pop(order.symbol, None) - order.filled_quantity += quantity - order.status = "FILLED" if order.remaining == 0 else "PARTIALLY_FILLED" - self.total_fees += fees - self.turnover += amount - self.fills_list.append(fill) - return fill - - def advance(self, frame_index, frame): - trading_date = str(frame["datetime"])[:10] - self._settle_trading_day(trading_date) - fills = [] - used_volume = {} - for order in self.orders_list: - if order.status not in ACTIVE_ORDER_STATUSES or frame_index <= order.submitted_index: - continue - bar = frame["bars"].get(order.symbol) - if bar is None: - continue - if order.time_in_force == "DAY" and str(order.submitted_at)[:10] != trading_date: - order.status = "EXPIRED" - order.reject_reason = "day_order_expired" - continue - order.last_attempt_index = frame_index - price, reason = self._match_price(order, bar) - if price is None: - if order.time_in_force == "NEXT_BAR": - order.status = "EXPIRED" - order.reject_reason = reason - continue - quantity = self._volume_cap(order, bar, used_volume.get(order.symbol, 0)) - if quantity <= 0: - reason = "volume_participation_exhausted" - elif order.side == "BUY": - quantity = self._affordable_buy_quantity(quantity, price) - if quantity <= 0: - reason = "insufficient_cash" - else: - position = self.positions.get(order.symbol) - quantity = min(quantity, 0 if position is None else position.available) - if quantity <= 0: - reason = "t_plus_one_unavailable" - if quantity > 0: - fills.append(self._apply_fill(order, quantity, price, frame_index, frame["datetime"])) - used_volume[order.symbol] = used_volume.get(order.symbol, 0) + quantity - if order.time_in_force == "NEXT_BAR" and order.remaining > 0: - if order.filled_quantity == 0: - order.status = "EXPIRED" - else: - order.status = "PARTIALLY_FILLED_EXPIRED" - order.reject_reason = reason or "next_bar_remaining_expired" - return fills - - def expire_open_orders(self, reason="backtest_finished"): - for order in self.orders_list: - if order.status in ACTIVE_ORDER_STATUSES: - order.status = "EXPIRED" - order.reject_reason = reason - - def snapshot(self, bars): - positions = {} - market_value = ZERO - for symbol in sorted(self.positions): - position = self.positions[symbol] - bar = bars.get(symbol) or {} - mark = decimal_value(bar.get("close"), position.avg_cost) - market_value += mark * position.quantity - positions[symbol] = position.to_dict(mark) - total_asset = money(self.cash + market_value) - return { - "cash": json_number(self.cash, 2), - "market_value": json_number(money(market_value), 2), - "total_asset": json_number(total_asset, 2), - "positions": positions, - "total_fees": json_number(money(self.total_fees), 2), - "turnover": json_number(money(self.turnover), 2), - } - - def orders(self): - return [order.to_dict() for order in self.orders_list] - - def fills(self): - return [fill.to_dict() for fill in self.fills_list] diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/client.py b/reference/xtquant_big_convert/src/bigqmt_backtest/client.py deleted file mode 100644 index ee6804a..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/client.py +++ /dev/null @@ -1,134 +0,0 @@ -"""External-strategy client SDK for the ZMQ backtest bridge.""" - -import json -import uuid - - -class BacktestRemoteError(RuntimeError): - pass - - -class BacktestZmqClient(object): - def __init__( - self, - endpoint, - run_id, - client_id="external-strategy", - timeout_seconds=10.0, - ): - self.endpoint = str(endpoint) - self.run_id = str(run_id) - self.client_id = str(client_id) - self.timeout_seconds = float(timeout_seconds) - self._context = None - self._socket = None - - def _connect(self): - if self._socket is not None: - return self._socket - import zmq - - self._context = zmq.Context.instance() - self._socket = self._context.socket(zmq.REQ) - self._socket.setsockopt(zmq.LINGER, 0) - self._socket.connect(self.endpoint) - return self._socket - - def _reset_socket(self): - if self._socket is not None: - self._socket.close(linger=0) - self._socket = None - - def request(self, method, params=None, request_id=None): - import zmq - - request_id = str(request_id or uuid.uuid4().hex) - envelope = { - "schema_version": 1, - "request_id": request_id, - "run_id": self.run_id, - "client_id": self.client_id, - "method": str(method), - "params": dict(params or {}), - } - socket = self._connect() - socket.send(json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")) - poller = zmq.Poller() - poller.register(socket, zmq.POLLIN) - events = dict(poller.poll(int(self.timeout_seconds * 1000))) - if socket not in events: - self._reset_socket() - raise TimeoutError("backtest ZMQ request timed out: %s" % method) - response = json.loads(socket.recv().decode("utf-8")) - if str(response.get("request_id") or "") != request_id: - raise BacktestRemoteError("response request_id mismatch") - if not response.get("ok"): - raise BacktestRemoteError(str(response.get("error") or "remote request failed")) - return response.get("data") - - def ping(self): - return self.request("ping") - - def describe(self): - data = self.request("describe") - if not self.run_id and data.get("run_id"): - self.run_id = str(data["run_id"]) - return data - - def start(self): - return self.request("start") - - def next_bar(self): - return self.request("next_bar") - - def state(self): - return self.request("state") - - def submit_order( - self, - symbol, - side, - quantity, - order_type="MARKET", - limit_price=None, - client_order_id="", - time_in_force="NEXT_BAR", - ): - params = { - "symbol": symbol, - "side": side, - "quantity": int(quantity), - "order_type": order_type, - "client_order_id": client_order_id, - "time_in_force": time_in_force, - } - if limit_price is not None: - params["limit_price"] = limit_price - return self.request("submit_order", params) - - def cancel_order(self, order_id): - return self.request("cancel_order", {"order_id": order_id}) - - def history(self, symbol, count=100, fields=None): - params = {"symbol": symbol, "count": int(count)} - if fields is not None: - params["fields"] = list(fields) - return self.request("history", params) - - def orders(self): - return self.request("orders") - - def fills(self): - return self.request("fills") - - def finish(self): - return self.request("finish") - - def close(self): - self._reset_socket() - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): - self.close() diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/data_feed.py b/reference/xtquant_big_convert/src/bigqmt_backtest/data_feed.py deleted file mode 100644 index 165e87c..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/data_feed.py +++ /dev/null @@ -1,278 +0,0 @@ -"""Deterministic historical bar feeds for the backtest bridge.""" - -import csv -import datetime as dt -import hashlib -import io -import json -import os -import threading - -from .models import normalize_symbol - - -DATETIME_FIELDS = ("datetime", "timestamp", "time", "date", "stime") -SYMBOL_FIELDS = ("symbol", "stock_code", "code", "stock") -REQUIRED_PRICE_FIELDS = ("open", "high", "low", "close") - - -def _first(row, names, default=None): - for name in names: - value = row.get(name) - if value not in (None, ""): - return value - return default - - -def parse_datetime(value): - if isinstance(value, dt.datetime): - return value.replace(tzinfo=None) - if isinstance(value, dt.date): - return dt.datetime.combine(value, dt.time()) - text = str(value or "").strip() - if not text: - raise ValueError("bar datetime is required") - if text.isdigit(): - if len(text) == 8: - return dt.datetime.strptime(text, "%Y%m%d") - if len(text) == 14: - return dt.datetime.strptime(text, "%Y%m%d%H%M%S") - numeric = int(text) - if numeric > 10 ** 12: - numeric = numeric / 1000.0 - return dt.datetime.fromtimestamp(numeric) - normalized = text.replace("T", " ").replace("Z", "").strip() - from_isoformat = getattr(dt.datetime, "fromisoformat", None) - if from_isoformat is not None: - try: - return from_isoformat(normalized).replace(tzinfo=None) - except ValueError: - pass - for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y/%m/%d %H:%M:%S", "%Y-%m-%d"): - try: - return dt.datetime.strptime(normalized, fmt) - except ValueError: - continue - raise ValueError("unsupported bar datetime: %s" % text) - - -def _bool_value(value): - if isinstance(value, bool): - return value - return str(value or "").strip().lower() in ("1", "true", "yes", "y", "on") - - -def _optional_float(value): - return None if value in (None, "") else float(value) - - -def normalize_bar(row, default_symbol=""): - timestamp = parse_datetime(_first(row, DATETIME_FIELDS)) - symbol = normalize_symbol(_first(row, SYMBOL_FIELDS, default_symbol)) - bar = { - "datetime": timestamp.strftime("%Y-%m-%d %H:%M:%S"), - "symbol": symbol, - } - for field in REQUIRED_PRICE_FIELDS: - value = row.get(field) - if value in (None, ""): - raise ValueError("%s is required for %s at %s" % (field, symbol, bar["datetime"])) - bar[field] = float(value) - if bar[field] <= 0: - raise ValueError("%s must be positive for %s at %s" % (field, symbol, bar["datetime"])) - if bar["high"] < max(bar["open"], bar["close"], bar["low"]): - raise ValueError("bar high is inconsistent for %s at %s" % (symbol, bar["datetime"])) - if bar["low"] > min(bar["open"], bar["close"], bar["high"]): - raise ValueError("bar low is inconsistent for %s at %s" % (symbol, bar["datetime"])) - bar["volume"] = float(row.get("volume") or 0) - bar["amount"] = float(row.get("amount") or 0) - bar["prev_close"] = _optional_float(row.get("prev_close")) - bar["up_limit"] = _optional_float(row.get("up_limit")) - bar["down_limit"] = _optional_float(row.get("down_limit")) - bar["suspended"] = _bool_value(row.get("suspended")) - if row.get("price_limit_rate") not in (None, ""): - bar["price_limit_rate"] = float(row["price_limit_rate"]) - return timestamp, bar - - -class InMemoryBarFeed(object): - def __init__(self, rows, source="memory", data_hash=None, default_symbol=""): - normalized = [] - for row in rows: - timestamp, bar = normalize_bar(dict(row), default_symbol=default_symbol) - normalized.append((timestamp, bar)) - normalized.sort(key=lambda item: (item[0], item[1]["symbol"])) - seen = set() - frames = [] - current_timestamp = None - current_bars = None - previous_close = {} - for timestamp, bar in normalized: - identity = (timestamp, bar["symbol"]) - if identity in seen: - raise ValueError("duplicate bar for %s at %s" % (bar["symbol"], bar["datetime"])) - seen.add(identity) - if bar["prev_close"] is None: - bar["prev_close"] = previous_close.get(bar["symbol"]) - previous_close[bar["symbol"]] = bar["close"] - if current_timestamp != timestamp: - current_timestamp = timestamp - current_bars = {} - frames.append( - { - "datetime": timestamp.strftime("%Y-%m-%d %H:%M:%S"), - "bars": current_bars, - } - ) - current_bars[bar["symbol"]] = bar - if not frames: - raise ValueError("historical data is empty") - self._frames = frames - self.source = str(source) - if data_hash: - self.data_hash = str(data_hash) - else: - payload = json.dumps(frames, ensure_ascii=False, sort_keys=True, separators=(",", ":")) - self.data_hash = hashlib.sha256(payload.encode("utf-8")).hexdigest() - - def __len__(self): - return len(self._frames) - - def frame(self, index): - frame = self._frames[int(index)] - return {"datetime": frame["datetime"], "bars": {key: dict(value) for key, value in frame["bars"].items()}} - - def history(self, symbol, end_index, count=100, fields=None): - symbol = normalize_symbol(symbol) - end_index = min(int(end_index), len(self._frames) - 1) - count = max(int(count or 0), 0) - result = [] - for index in range(0, end_index + 1): - bar = self._frames[index]["bars"].get(symbol) - if bar is None: - continue - if fields: - item = {"datetime": bar["datetime"], "symbol": symbol} - for field in fields: - if field in bar: - item[str(field)] = bar[field] - else: - item = dict(bar) - result.append(item) - return result[-count:] if count else [] - - -class CsvBarFeed(InMemoryBarFeed): - def __init__(self, path, default_symbol="", encoding="utf-8-sig"): - absolute = os.path.abspath(path) - with open(absolute, "rb") as handle: - raw = handle.read() - digest = hashlib.sha256(raw).hexdigest() - text = raw.decode(encoding) - reader = csv.DictReader(io.StringIO(text, newline="")) - rows = list(reader) - super(CsvBarFeed, self).__init__( - rows, - source=absolute, - data_hash=digest, - default_symbol=default_symbol, - ) - - -class StreamingBarFeed(object): - """Thread-safe feed populated by QMT ``handlebar`` callbacks. - - The external strategy can only read through ``frame``/``history`` with an - engine-controlled end index, so bars already captured from QMT but not yet - advanced to remain inaccessible. - """ - - def __init__(self, source="qmt_native_backtest"): - self.source = str(source) - self._frames = [] - self._seen = set() - self._previous_close = {} - self._condition = threading.Condition() - self.closed = False - - @property - def data_hash(self): - with self._condition: - payload = json.dumps( - self._frames, - ensure_ascii=False, - sort_keys=True, - separators=(",", ":"), - ) - return hashlib.sha256(payload.encode("utf-8")).hexdigest() - - def __len__(self): - with self._condition: - return len(self._frames) - - def append(self, row, default_symbol=""): - timestamp, bar = normalize_bar(dict(row), default_symbol=default_symbol) - identity = (timestamp, bar["symbol"]) - with self._condition: - if identity in self._seen: - return False - if self.closed: - raise RuntimeError("streaming feed is closed") - self._seen.add(identity) - if bar["prev_close"] is None: - bar["prev_close"] = self._previous_close.get(bar["symbol"]) - self._previous_close[bar["symbol"]] = bar["close"] - timestamp_text = timestamp.strftime("%Y-%m-%d %H:%M:%S") - if self._frames and self._frames[-1]["datetime"] == timestamp_text: - self._frames[-1]["bars"][bar["symbol"]] = bar - elif self._frames and self._frames[-1]["datetime"] > timestamp_text: - raise ValueError("streaming bars must be appended chronologically") - else: - self._frames.append({"datetime": timestamp_text, "bars": {bar["symbol"]: bar}}) - self._condition.notify_all() - return True - - def close(self): - with self._condition: - self.closed = True - self._condition.notify_all() - - def wait_for_index(self, index, timeout_seconds=None): - index = int(index) - with self._condition: - if len(self._frames) > index: - return True - self._condition.wait_for( - lambda: len(self._frames) > index or self.closed, - timeout=timeout_seconds, - ) - return len(self._frames) > index - - def frame(self, index): - with self._condition: - frame = self._frames[int(index)] - return { - "datetime": frame["datetime"], - "bars": {key: dict(value) for key, value in frame["bars"].items()}, - } - - def history(self, symbol, end_index, count=100, fields=None): - symbol = normalize_symbol(symbol) - count = max(int(count or 0), 0) - with self._condition: - end_index = min(int(end_index), len(self._frames) - 1) - frames = self._frames[: end_index + 1] - result = [] - for frame in frames: - bar = frame["bars"].get(symbol) - if bar is None: - continue - if fields: - item = {"datetime": bar["datetime"], "symbol": symbol} - for field in fields: - if field in bar: - item[str(field)] = bar[field] - else: - item = dict(bar) - result.append(item) - return result[-count:] if count else [] diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/engine.py b/reference/xtquant_big_convert/src/bigqmt_backtest/engine.py deleted file mode 100644 index 9725fb4..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/engine.py +++ /dev/null @@ -1,372 +0,0 @@ -"""Deterministic bar-by-bar backtest engine.""" - -import csv -import datetime as dt -import hashlib -import json -import math -import os - -from .broker import ACTIVE_ORDER_STATUSES, SimulatedBroker -from .models import decimal_value, normalize_symbol - - -ENGINE_VERSION = "1.0.0" -PROTOCOL_VERSION = 1 - - -class BacktestConfig(object): - def __init__( - self, - run_id, - output_dir, - initial_cash=1000000, - initial_positions=None, - buy_commission_rate=0.0003, - sell_commission_rate=0.0003, - min_commission=5, - stamp_tax_rate=0.0005, - transfer_fee_rate=0.00001, - slippage_bps=0, - max_volume_participation=0.1, - price_limit_rate=0.10, - lot_size=100, - time_in_force="NEXT_BAR", - seed=0, - strategy_name="external_zmq_strategy", - parameters=None, - fee_schedule="a_share_2023_08_28", - market_rules_version="a_share_v1", - ): - self.run_id = str(run_id or "").strip() - if not self.run_id: - raise ValueError("run_id is required") - self.output_dir = os.path.abspath(output_dir) - self.initial_cash = decimal_value(initial_cash) - if self.initial_cash < 0: - raise ValueError("initial_cash cannot be negative") - self.initial_positions = dict(initial_positions or {}) - self.buy_commission_rate = decimal_value(buy_commission_rate) - self.sell_commission_rate = decimal_value(sell_commission_rate) - self.min_commission = decimal_value(min_commission) - self.stamp_tax_rate = decimal_value(stamp_tax_rate) - self.transfer_fee_rate = decimal_value(transfer_fee_rate) - self.slippage_bps = decimal_value(slippage_bps) - self.max_volume_participation = float(max_volume_participation) - if not 0 < self.max_volume_participation <= 1: - raise ValueError("max_volume_participation must be in (0, 1]") - self.price_limit_rate = decimal_value(price_limit_rate) - self.lot_size = int(lot_size) - if self.lot_size <= 0: - raise ValueError("lot_size must be positive") - self.time_in_force = str(time_in_force or "NEXT_BAR").upper() - if self.time_in_force not in ("NEXT_BAR", "DAY"): - raise ValueError("time_in_force must be NEXT_BAR or DAY") - self.seed = int(seed) - self.strategy_name = str(strategy_name or "external_zmq_strategy") - self.parameters = dict(parameters or {}) - self.fee_schedule = str(fee_schedule or "custom") - self.market_rules_version = str(market_rules_version or "custom") - - def to_dict(self, include_paths=True, include_identity=True): - payload = { - "initial_cash": float(self.initial_cash), - "initial_positions": self.initial_positions, - "buy_commission_rate": float(self.buy_commission_rate), - "sell_commission_rate": float(self.sell_commission_rate), - "min_commission": float(self.min_commission), - "stamp_tax_rate": float(self.stamp_tax_rate), - "transfer_fee_rate": float(self.transfer_fee_rate), - "slippage_bps": float(self.slippage_bps), - "max_volume_participation": self.max_volume_participation, - "price_limit_rate": float(self.price_limit_rate), - "lot_size": self.lot_size, - "time_in_force": self.time_in_force, - "seed": self.seed, - "strategy_name": self.strategy_name, - "parameters": self.parameters, - "fee_schedule": self.fee_schedule, - "market_rules_version": self.market_rules_version, - } - if include_identity: - payload["run_id"] = self.run_id - if include_paths: - payload["output_dir"] = self.output_dir - return payload - - -class BacktestEngine(object): - def __init__(self, feed, config): - self.feed = feed - self.config = config - self.created_at = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.started = False - self.finished = False - self.current_index = -1 - self.current_frame = None - self.last_fills = [] - self.equity_curve = [] - self.position_rows = [] - self._result = None - self.broker = SimulatedBroker(config) - - def _record_state(self): - snapshot = self.broker.snapshot(self.current_frame["bars"]) - equity_row = { - "frame_index": self.current_index, - "datetime": self.current_frame["datetime"], - "cash": snapshot["cash"], - "market_value": snapshot["market_value"], - "total_asset": snapshot["total_asset"], - } - self.equity_curve.append(equity_row) - for symbol, position in snapshot["positions"].items(): - row = {"frame_index": self.current_index, "datetime": self.current_frame["datetime"]} - row.update(position) - self.position_rows.append(row) - - def start(self): - if self.started: - return self.state() - self.started = True - self.current_index = 0 - self.current_frame = self.feed.frame(0) - self.broker._settle_trading_day(self.current_frame["datetime"][:10]) - self._record_state() - return self.state() - - def _require_started(self): - if not self.started: - raise RuntimeError("backtest has not started") - if self.finished: - raise RuntimeError("backtest is already finished") - - def submit_order(self, payload): - self._require_started() - return self.broker.submit( - payload, - frame_index=self.current_index, - submitted_at=self.current_frame["datetime"], - ).to_dict() - - def cancel_order(self, order_id): - self._require_started() - return self.broker.cancel(order_id).to_dict() - - def next_bar(self): - self._require_started() - if self.current_index >= len(self.feed) - 1: - return self.state() - self.current_index += 1 - self.current_frame = self.feed.frame(self.current_index) - self.last_fills = self.broker.advance(self.current_index, self.current_frame) - self._record_state() - return self.state() - - def history(self, symbol, count=100, fields=None): - self._require_started() - return self.feed.history(normalize_symbol(symbol), self.current_index, count=count, fields=fields) - - def orders(self): - return self.broker.orders() - - def fills(self): - return self.broker.fills() - - def state(self): - if not self.started: - return { - "run_id": self.config.run_id, - "started": False, - "finished": self.finished, - "done": False, - "frame_index": -1, - "frame_count": len(self.feed), - } - portfolio = self.broker.snapshot(self.current_frame["bars"]) - return { - "run_id": self.config.run_id, - "started": True, - "finished": self.finished, - "done": self.current_index >= len(self.feed) - 1, - "frame_index": self.current_index, - "frame_count": len(self.feed), - "datetime": self.current_frame["datetime"], - "bars": {key: dict(value) for key, value in self.current_frame["bars"].items()}, - "fills": [fill.to_dict() for fill in self.last_fills], - "cash": portfolio["cash"], - "market_value": portfolio["market_value"], - "total_asset": portfolio["total_asset"], - "positions": portfolio["positions"], - "total_fees": portfolio["total_fees"], - "turnover": portfolio["turnover"], - } - - def _metrics(self): - assets = [float(row["total_asset"]) for row in self.equity_curve] - initial = assets[0] if assets else float(self.config.initial_cash) - final = assets[-1] if assets else initial - peak = None - max_drawdown = 0.0 - for value in assets: - peak = value if peak is None else max(peak, value) - if peak > 0: - max_drawdown = min(max_drawdown, value / peak - 1.0) - dates = sorted(set(row["datetime"][:10] for row in self.equity_curve)) - total_return = 0.0 if initial == 0 else final / initial - 1.0 - annualized = None - if len(dates) > 1 and initial > 0 and final > 0: - annualized = math.pow(final / initial, 252.0 / len(dates)) - 1.0 - filled_orders = len([order for order in self.broker.orders_list if order.filled_quantity > 0]) - rejected_orders = len([order for order in self.broker.orders_list if order.status == "REJECTED"]) - return { - "initial_total_asset": round(initial, 2), - "final_total_asset": round(final, 2), - "total_return": round(total_return, 10), - "annualized_return": None if annualized is None else round(annualized, 10), - "max_drawdown": round(-max_drawdown, 10), - "trading_days": len(dates), - "bar_count": len(self.equity_curve), - "order_count": len(self.broker.orders_list), - "filled_order_count": filled_orders, - "rejected_order_count": rejected_orders, - "fill_count": len(self.broker.fills_list), - "total_fees": round(float(self.broker.total_fees), 2), - "turnover": round(float(self.broker.turnover), 2), - } - - def _signature_payload(self, metrics): - orders = [] - for item in self.orders(): - clean = dict(item) - clean.pop("client_order_id", None) - orders.append(clean) - return { - "engine_version": ENGINE_VERSION, - "data_hash": self.feed.data_hash, - "config": self.config.to_dict(include_paths=False, include_identity=False), - "orders": orders, - "fills": self.fills(), - "equity": self.equity_curve, - "metrics": metrics, - } - - def finish(self): - if self._result is not None: - return dict(self._result) - if not self.started: - self.start() - self.broker.expire_open_orders() - metrics = self._metrics() - signature_json = json.dumps( - self._signature_payload(metrics), ensure_ascii=False, sort_keys=True, separators=(",", ":") - ) - signature = hashlib.sha256(signature_json.encode("utf-8")).hexdigest() - self.finished = True - final_state = self.state() - self._result = { - "schema_version": 1, - "engine_version": ENGINE_VERSION, - "run_id": self.config.run_id, - "strategy_name": self.config.strategy_name, - "data_hash": self.feed.data_hash, - "deterministic_signature": signature, - "metrics": metrics, - "final_state": final_state, - } - self._write_artifacts() - return dict(self._result) - - @staticmethod - def _write_json(path, payload): - with open(path, "w", encoding="utf-8", newline="\n") as handle: - json.dump(payload, handle, ensure_ascii=False, sort_keys=True, indent=2) - handle.write("\n") - - @staticmethod - def _write_csv(path, rows, fieldnames): - with open(path, "w", encoding="utf-8-sig", newline="") as handle: - writer = csv.DictWriter(handle, fieldnames=fieldnames, extrasaction="ignore") - writer.writeheader() - writer.writerows(rows) - - def _write_artifacts(self): - output_dir = self.config.output_dir - os.makedirs(output_dir, exist_ok=True) - meta = { - "schema_version": 1, - "engine_version": ENGINE_VERSION, - "protocol_version": PROTOCOL_VERSION, - "run_id": self.config.run_id, - "created_at": self.created_at, - "data_source": self.feed.source, - "data_hash": self.feed.data_hash, - "frame_count": len(self.feed), - "config": self.config.to_dict(), - "live_ready": False, - "execution_channel": "backtest_zmq_only", - } - self._write_json(os.path.join(output_dir, "meta.json"), meta) - self._write_json(os.path.join(output_dir, "result.json"), self._result) - self._write_csv( - os.path.join(output_dir, "orders.csv"), - self.orders(), - ( - "order_id", "client_order_id", "symbol", "side", "quantity", "filled_quantity", - "remaining_quantity", "order_type", "limit_price", "submitted_index", "submitted_at", - "time_in_force", "status", "reject_reason", - ), - ) - self._write_csv( - os.path.join(output_dir, "fills.csv"), - self.fills(), - ( - "fill_id", "order_id", "client_order_id", "symbol", "side", "quantity", "price", - "amount", "commission", "stamp_tax", "transfer_fee", "total_fee", "filled_index", "filled_at", - ), - ) - self._write_csv( - os.path.join(output_dir, "equity.csv"), - self.equity_curve, - ("frame_index", "datetime", "cash", "market_value", "total_asset"), - ) - self._write_csv( - os.path.join(output_dir, "positions.csv"), - self.position_rows, - ( - "frame_index", "datetime", "symbol", "quantity", "available", "today_buy", "avg_cost", - "realized_pnl", "mark_price", "market_value", - ), - ) - - -class StreamingBacktestEngine(BacktestEngine): - """Backtest engine whose bars arrive from a QMT backtest callback thread.""" - - def __init__(self, feed, config, bar_wait_timeout_seconds=60.0): - super(StreamingBacktestEngine, self).__init__(feed, config) - self.bar_wait_timeout_seconds = float(bar_wait_timeout_seconds) - - def start(self): - if not self.started and not self.feed.wait_for_index(0, self.bar_wait_timeout_seconds): - raise TimeoutError("timed out waiting for the first QMT backtest bar") - return super(StreamingBacktestEngine, self).start() - - def next_bar(self): - self._require_started() - target = self.current_index + 1 - if not self.feed.wait_for_index(target, self.bar_wait_timeout_seconds): - if self.feed.closed: - return self.state() - raise TimeoutError("timed out waiting for QMT backtest bar index %d" % target) - self.current_index = target - self.current_frame = self.feed.frame(target) - self.last_fills = self.broker.advance(self.current_index, self.current_frame) - self._record_state() - return self.state() - - def state(self): - state = super(StreamingBacktestEngine, self).state() - if state.get("started"): - state["done"] = bool(self.feed.closed and self.current_index >= len(self.feed) - 1) - return state diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/models.py b/reference/xtquant_big_convert/src/bigqmt_backtest/models.py deleted file mode 100644 index c030857..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/models.py +++ /dev/null @@ -1,179 +0,0 @@ -"""Backtest-only domain models with JSON-safe serialization.""" - -from decimal import Decimal, ROUND_HALF_UP - - -ZERO = Decimal("0") -MONEY_QUANT = Decimal("0.01") - - -def decimal_value(value, default="0"): - if value in (None, ""): - value = default - if isinstance(value, Decimal): - return value - return Decimal(str(value)) - - -def money(value): - return decimal_value(value).quantize(MONEY_QUANT, rounding=ROUND_HALF_UP) - - -def json_number(value, digits=None): - if value is None: - return None - number = float(value) - return round(number, digits) if digits is not None else number - - -def normalize_symbol(value): - text = str(value or "").strip().upper() - if not text: - raise ValueError("symbol is required") - if "." in text: - pure, market = text.rsplit(".", 1) - if pure and market in ("SH", "SZ", "BJ"): - return "%s.%s" % (pure, market) - return text - if text.isdigit() and len(text) == 6: - if text.startswith(("4", "8")): - return text + ".BJ" - if text.startswith(("5", "6", "9")): - return text + ".SH" - return text + ".SZ" - return text - - -def price_precision(symbol): - pure = normalize_symbol(symbol).split(".", 1)[0] - return 3 if pure.startswith(("15", "16", "50", "51", "52", "56", "58")) else 2 - - -def price_quant(symbol): - return Decimal("0.001") if price_precision(symbol) == 3 else Decimal("0.01") - - -def round_price(symbol, value): - return decimal_value(value).quantize(price_quant(symbol), rounding=ROUND_HALF_UP) - - -class Position(object): - def __init__(self, symbol, quantity=0, available=None, today_buy=0, avg_cost=0, realized_pnl=0): - self.symbol = normalize_symbol(symbol) - self.quantity = int(quantity or 0) - self.available = self.quantity if available is None else int(available or 0) - self.today_buy = int(today_buy or 0) - self.avg_cost = decimal_value(avg_cost) - self.realized_pnl = decimal_value(realized_pnl) - - def to_dict(self, mark_price=None): - market_value = None if mark_price is None else money(decimal_value(mark_price) * self.quantity) - return { - "symbol": self.symbol, - "quantity": self.quantity, - "available": self.available, - "today_buy": self.today_buy, - "avg_cost": json_number(self.avg_cost, 6), - "realized_pnl": json_number(self.realized_pnl, 2), - "mark_price": json_number(mark_price, 6), - "market_value": json_number(market_value, 2), - } - - -class BacktestOrder(object): - def __init__( - self, - order_id, - client_order_id, - symbol, - side, - quantity, - order_type, - limit_price, - submitted_index, - submitted_at, - time_in_force="NEXT_BAR", - ): - self.order_id = str(order_id) - self.client_order_id = str(client_order_id or "") - self.symbol = normalize_symbol(symbol) - self.side = str(side).upper() - self.quantity = int(quantity) - self.filled_quantity = 0 - self.order_type = str(order_type).upper() - self.limit_price = decimal_value(limit_price) if limit_price not in (None, "") else None - self.submitted_index = int(submitted_index) - self.submitted_at = str(submitted_at) - self.time_in_force = str(time_in_force or "NEXT_BAR").upper() - self.status = "PENDING" - self.reject_reason = "" - self.last_attempt_index = None - - @property - def remaining(self): - return max(self.quantity - self.filled_quantity, 0) - - def to_dict(self): - return { - "order_id": self.order_id, - "client_order_id": self.client_order_id, - "symbol": self.symbol, - "side": self.side, - "quantity": self.quantity, - "filled_quantity": self.filled_quantity, - "remaining_quantity": self.remaining, - "order_type": self.order_type, - "limit_price": json_number(self.limit_price, 6), - "submitted_index": self.submitted_index, - "submitted_at": self.submitted_at, - "time_in_force": self.time_in_force, - "status": self.status, - "reject_reason": self.reject_reason, - } - - -class BacktestFill(object): - def __init__( - self, - fill_id, - order, - quantity, - price, - commission, - stamp_tax, - transfer_fee, - filled_index, - filled_at, - ): - self.fill_id = str(fill_id) - self.order_id = order.order_id - self.client_order_id = order.client_order_id - self.symbol = order.symbol - self.side = order.side - self.quantity = int(quantity) - self.price = decimal_value(price) - self.amount = money(self.price * self.quantity) - self.commission = money(commission) - self.stamp_tax = money(stamp_tax) - self.transfer_fee = money(transfer_fee) - self.total_fee = money(self.commission + self.stamp_tax + self.transfer_fee) - self.filled_index = int(filled_index) - self.filled_at = str(filled_at) - - def to_dict(self): - return { - "fill_id": self.fill_id, - "order_id": self.order_id, - "client_order_id": self.client_order_id, - "symbol": self.symbol, - "side": self.side, - "quantity": self.quantity, - "price": json_number(self.price, 6), - "amount": json_number(self.amount, 2), - "commission": json_number(self.commission, 2), - "stamp_tax": json_number(self.stamp_tax, 2), - "transfer_fee": json_number(self.transfer_fee, 2), - "total_fee": json_number(self.total_fee, 2), - "filled_index": self.filled_index, - "filled_at": self.filled_at, - } diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/protocol.py b/reference/xtquant_big_convert/src/bigqmt_backtest/protocol.py deleted file mode 100644 index 9e0eac5..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/protocol.py +++ /dev/null @@ -1,137 +0,0 @@ -"""Versioned request/response protocol for the backtest-only ZMQ bridge.""" - -import datetime as dt -import json - - -SCHEMA_VERSION = 1 - - -class BacktestBridgeProtocol(object): - def __init__(self, engine, request_cache_size=10000): - self.engine = engine - self.request_cache_size = int(request_cache_size) - self.client_id = None - self._responses = {} - self._request_fingerprints = {} - self._response_order = [] - - def _response(self, request, ok, data=None, error=""): - execution_backend = str(getattr(self.engine, "execution_backend", "LOCAL_SIM")) - return { - "schema_version": SCHEMA_VERSION, - "request_id": str(request.get("request_id") or ""), - "run_id": self.engine.config.run_id, - "client_id": str(request.get("client_id") or ""), - "method": str(request.get("method") or ""), - "ok": bool(ok), - "data": data, - "error": str(error or ""), - "execution_mode": "QMT_BACKTEST" if execution_backend == "QMT_NATIVE" else "BACKTEST", - "execution_backend": execution_backend, - "live_ready": False, - "handled_at": dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), - } - - @staticmethod - def _fingerprint(request): - return json.dumps(request, ensure_ascii=False, sort_keys=True, separators=(",", ":")) - - def _remember(self, request_id, request, response): - self._responses[request_id] = response - self._request_fingerprints[request_id] = self._fingerprint(request) - self._response_order.append(request_id) - while len(self._response_order) > self.request_cache_size: - oldest = self._response_order.pop(0) - self._responses.pop(oldest, None) - self._request_fingerprints.pop(oldest, None) - - def _validate(self, request): - if not isinstance(request, dict): - raise ValueError("request must be a JSON object") - if int(request.get("schema_version") or 0) != SCHEMA_VERSION: - raise ValueError("unsupported schema_version") - if not str(request.get("request_id") or ""): - raise ValueError("request_id is required") - method = str(request.get("method") or "").lower() - requested_run_id = str(request.get("run_id") or "") - discovery = method in ("ping", "describe") and not requested_run_id - if not discovery and requested_run_id != self.engine.config.run_id: - raise ValueError("run_id mismatch") - if not str(request.get("client_id") or ""): - raise ValueError("client_id is required") - if not str(request.get("method") or ""): - raise ValueError("method is required") - - def _claim_or_check_client(self, request): - client_id = str(request["client_id"]) - method = str(request["method"]).lower() - if self.client_id is None and method == "start": - self.client_id = client_id - if method not in ("ping", "describe") and self.client_id != client_id: - raise PermissionError("run is owned by another client_id") - - def _dispatch(self, request): - method = str(request["method"]).lower() - params = dict(request.get("params") or {}) - if method == "ping": - return { - "status": "ok", - "started": self.engine.started, - "finished": self.engine.finished, - } - if method == "describe": - execution_backend = str(getattr(self.engine, "execution_backend", "LOCAL_SIM")) - return { - "schema_version": SCHEMA_VERSION, - "run_id": self.engine.config.run_id, - "engine_version": str(getattr(self.engine, "engine_version", "1.0.0")), - "execution_backend": execution_backend, - "engine_owner": "QMT" if execution_backend == "QMT_NATIVE" else "LOCAL", - "matching_owner": "QMT" if execution_backend == "QMT_NATIVE" else "LOCAL", - "methods": [ - "ping", "describe", "start", "next_bar", "submit_order", "cancel_order", - "state", "history", "orders", "fills", "finish", - ], - "fill_timing": str(getattr(self.engine, "fill_timing", "next_symbol_bar")), - "live_ready": False, - } - if method == "start": - return self.engine.start() - if method == "next_bar": - return self.engine.next_bar() - if method == "submit_order": - return self.engine.submit_order(params) - if method == "cancel_order": - return self.engine.cancel_order(params.get("order_id")) - if method == "state": - return self.engine.state() - if method == "history": - return self.engine.history( - params.get("symbol"), - count=params.get("count", 100), - fields=params.get("fields"), - ) - if method == "orders": - return self.engine.orders() - if method == "fills": - return self.engine.fills() - if method == "finish": - return self.engine.finish() - raise ValueError("unsupported method: %s" % method) - - def handle(self, request): - request_id = str((request or {}).get("request_id") or "") - if request_id and request_id in self._responses: - if self._request_fingerprints.get(request_id) != self._fingerprint(request): - return self._response(request, False, None, "request_id reused with different payload") - return self._responses[request_id] - try: - self._validate(request) - self._claim_or_check_client(request) - response = self._response(request, True, self._dispatch(request)) - except Exception as exc: - response = self._response(request or {}, False, None, "%s: %s" % (exc.__class__.__name__, exc)) - if request_id: - self._remember(request_id, request, response) - return response diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/qmt_runtime.py b/reference/xtquant_big_convert/src/bigqmt_backtest/qmt_runtime.py deleted file mode 100644 index d6db0b0..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/qmt_runtime.py +++ /dev/null @@ -1,718 +0,0 @@ -"""QMT-native backtest service exposed to external strategies over ZMQ. - -QMT remains the only backtest engine and matching system. The ZMQ listener -thread only queues commands; every QMT API call is executed by ``handlebar`` on -QMT's callback thread. -""" - -import datetime as dt -import threading -import uuid - -from .data_feed import StreamingBarFeed, parse_datetime -from .models import normalize_symbol -from .protocol import BacktestBridgeProtocol -from .zmq_server import ZmqBacktestServer - - -_CONFIG = {} -_QMT_API = {} -_RUNTIME = None - - -def configure(**kwargs): - _CONFIG.update(kwargs) - - -def bind_qmt_api(passorder_func=None, cancel_func=None, get_trade_detail_data_func=None): - if passorder_func is not None: - _QMT_API["passorder"] = passorder_func - if cancel_func is not None: - _QMT_API["cancel"] = cancel_func - if get_trade_detail_data_func is not None: - _QMT_API["get_trade_detail_data"] = get_trade_detail_data_func - - -def _sequence(value): - if value is None: - return [] - if isinstance(value, dict): - for item in value.values(): - result = _sequence(item) - if result: - return result - return [] - if isinstance(value, (list, tuple)): - return list(value) - if hasattr(value, "tolist"): - try: - result = value.tolist() - return result if isinstance(result, list) else [result] - except Exception: - pass - if hasattr(value, "values"): - try: - return list(value.values) - except Exception: - pass - return [value] - - -def _last_value(value): - values = _sequence(value) - return values[-1] if values else None - - -def _attr(value, names, default=None): - for name in names: - if isinstance(value, dict) and name in value: - result = value.get(name) - else: - result = getattr(value, name, None) - if result is not None: - return result - return default - - -def _json_number(value): - if value in (None, ""): - return None - try: - return float(value) - except (TypeError, ValueError): - return None - - -def _full_symbol(row): - code = str(_attr(row, ("m_strInstrumentID", "instrument_id", "stock_code", "symbol"), "") or "") - market = str(_attr(row, ("m_strExchangeID", "exchange_id", "market"), "") or "").upper() - if "." not in code and market in ("SH", "SZ", "BJ"): - code = code + "." + market - return normalize_symbol(code) if code else "" - - -def _side_from_offset(value): - try: - return "BUY" if int(value or 0) == 48 else "SELL" - except (TypeError, ValueError): - return str(value or "") - - -def _is_qmt_backtest(context): - value = getattr(context, "do_back_test", None) - if callable(value): - try: - value = value() - except Exception: - value = None - if bool(value): - return True - for name in ("is_backtest", "is_back_test", "backtest"): - value = getattr(context, name, None) - if callable(value): - try: - value = value() - except Exception: - value = None - if bool(value): - return True - return False - - -class QmtBarExtractor(object): - def __init__(self): - self.previous_close = {} - - @staticmethod - def _symbol(context): - raw = "" - for name in ("stock", "symbol", "stockcode"): - value = getattr(context, name, None) - if value: - raw = str(value) - break - if not raw: - raise ValueError("QMT ContextInfo has no stock symbol") - if "." not in raw: - market = str(getattr(context, "market", "") or "").upper() - if market in ("SH", "SZ", "BJ"): - raw = raw + "." + market - return normalize_symbol(raw) - - @staticmethod - def _timestamp(context): - barpos = getattr(context, "barpos", getattr(context, "bar_index", None)) - getter = getattr(context, "get_bar_timetag", None) - if callable(getter) and barpos is not None: - return parse_datetime(getter(barpos)).strftime("%Y-%m-%d %H:%M:%S") - for name in ("bar_time", "datetime", "timestamp"): - value = getattr(context, name, None) - if value not in (None, ""): - return parse_datetime(value).strftime("%Y-%m-%d %H:%M:%S") - raise ValueError("QMT ContextInfo has no deterministic bar timestamp") - - @staticmethod - def _periods(context): - result = [] - for value in (getattr(context, "period", None), "1m", "1d"): - text = str(value or "").strip() - if text and text not in result: - result.append(text) - return result - - def _history_value(self, context, field): - getter = getattr(context, "get_history_data", None) - if not callable(getter): - return None - for period in self._periods(context): - for call in ( - lambda period=period: getter(1, period, field), - lambda period=period: getter(field, 1, period), - lambda: getter(field, 1), - ): - try: - value = _last_value(call()) - if value not in (None, ""): - return value - except Exception: - continue - return None - - def _field(self, context, field, aliases=()): - for name in (field,) + tuple(aliases): - value = _last_value(getattr(context, name, None)) - if value not in (None, ""): - return value - for name in (field,) + tuple(aliases): - value = self._history_value(context, name) - if value not in (None, ""): - return value - return None - - def extract(self, context): - symbol = self._symbol(context) - close = self._field(context, "close") - row = { - "datetime": self._timestamp(context), - "symbol": symbol, - "open": self._field(context, "open"), - "high": self._field(context, "high"), - "low": self._field(context, "low"), - "close": close, - "volume": self._field(context, "volume", ("vol",)) or 0, - "amount": self._field(context, "amount") or 0, - "prev_close": self._field(context, "prev_close", ("preClose", "lastClose")), - } - if row["prev_close"] in (None, ""): - row["prev_close"] = self.previous_close.get(symbol) - self.previous_close[symbol] = close - return row - - -class NativeSessionConfig(object): - def __init__(self, run_id, strategy_name, account_id): - self.run_id = str(run_id) - self.strategy_name = str(strategy_name) - self.account_id = str(account_id) - - -class QmtNativeBacktestSession(object): - """Engine-shaped adapter whose actual engine and broker are both QMT.""" - - engine_version = "qmt-native-1.0.0" - execution_backend = "QMT_NATIVE" - fill_timing = "qmt_native_matching" - - def __init__(self, config=None, qmt_api=None): - options = dict(config or {}) - run_id = str(options.get("run_id") or ("qmt-native-" + dt.datetime.now().strftime("%Y%m%d-%H%M%S"))) - self.config = NativeSessionConfig( - run_id=run_id, - strategy_name=options.get("strategy_name") or "ZMQ_BACKTEST", - account_id=options.get("account_id") or "", - ) - self.account_type = str(options.get("account_type") or "STOCK") - self.combo_type = int(options.get("combo_type") or 1101) - self.quick_trade = int(options.get("quick_trade") if options.get("quick_trade") is not None else 2) - self.market_price_type = int(options.get("market_price_type") or 5) - self.limit_price_type = int(options.get("limit_price_type") or 11) - self.bar_wait_timeout = float(options.get("bar_wait_timeout_seconds") or 60.0) - self.require_backtest = bool(options.get("require_qmt_backtest", True)) - self.qmt_api = dict(qmt_api or {}) - self.feed = StreamingBarFeed(source="qmt_native_backtest") - self.extractor = QmtBarExtractor() - self.created_at = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - self.started = False - self.finished = False - self.qmt_completed = False - self.current_index = -1 - self.current_frame = None - self._released_index = -1 - self._condition = threading.Condition() - self._pending_commands = [] - self._orders = {} - self._fills = {} - self._current_frame_fills = [] - self._published_fill_keys = set() - self._positions = {} - self._asset = {"cash": None, "total_asset": None} - self._context = None - self._failure = "" - - def bind_context(self, context): - if self.require_backtest and not _is_qmt_backtest(context): - raise RuntimeError("QMT native bridge refused to run outside QMT backtest mode") - if not self.config.account_id: - raise RuntimeError("account_id is required for the QMT native backtest bridge") - self._context = context - if self.config.account_id and hasattr(context, "set_account"): - context.set_account(self.config.account_id) - - def _require_api(self, name): - func = self.qmt_api.get(name) - if func is None: - raise RuntimeError("QMT runtime API is unavailable: %s" % name) - return func - - def _require_started(self): - if not self.started: - raise RuntimeError("external strategy has not attached") - if self.finished: - raise RuntimeError("external strategy session is already finished") - - def start(self): - with self._condition: - if not self.started: - self.started = True - self._condition.notify_all() - if self.current_index < 0 and not self.qmt_completed: - ready = self._condition.wait_for( - lambda: self.current_index >= 0 or self.qmt_completed or bool(self._failure), - timeout=self.bar_wait_timeout, - ) - if not ready: - raise TimeoutError("timed out waiting for QMT's first backtest bar") - if self._failure: - raise RuntimeError(self._failure) - return self._state_unlocked() - - def _queue_command(self, command): - with self._condition: - self._require_started() - if self.qmt_completed or self.current_index < 0: - raise RuntimeError("QMT backtest has no active bar") - if self._released_index >= self.current_index: - raise RuntimeError("current QMT bar has already been released") - command = dict(command) - command["frame_index"] = self.current_index - self._pending_commands.append(command) - return command - - def submit_order(self, payload): - payload = dict(payload or {}) - side = str(payload.get("side") or "").upper() - if side not in ("BUY", "SELL"): - raise ValueError("side must be BUY or SELL") - quantity = int(payload.get("quantity") or 0) - if quantity <= 0: - raise ValueError("quantity must be positive") - symbol = normalize_symbol(payload.get("symbol")) - order_type = str(payload.get("order_type") or "MARKET").upper() - if order_type not in ("MARKET", "LIMIT"): - raise ValueError("order_type must be MARKET or LIMIT") - limit_price = payload.get("limit_price") - if order_type == "LIMIT" and limit_price in (None, ""): - raise ValueError("limit_price is required for LIMIT order") - client_order_id = str(payload.get("client_order_id") or ("zmq:" + uuid.uuid4().hex[:20])) - record = { - "order_id": client_order_id, - "client_order_id": client_order_id, - "symbol": symbol, - "side": side, - "quantity": quantity, - "filled_quantity": 0, - "order_type": order_type, - "limit_price": None if limit_price in (None, "") else float(limit_price), - "status": "QUEUED", - "reject_reason": "", - "submitted_index": self.current_index, - "submitted_at": (self.current_frame or {}).get("datetime", ""), - "execution_backend": self.execution_backend, - } - with self._condition: - self._orders[client_order_id] = record - self._queue_command({"kind": "submit", "client_order_id": client_order_id}) - return dict(record) - - def cancel_order(self, order_id): - order_id = str(order_id or "").strip() - if not order_id: - raise ValueError("order_id is required") - command = self._queue_command({"kind": "cancel", "order_id": order_id}) - return {"order_id": order_id, "status": "CANCEL_QUEUED", "frame_index": command["frame_index"]} - - def _execute_submit(self, command, context): - client_order_id = command["client_order_id"] - with self._condition: - record = dict(self._orders[client_order_id]) - if not self.config.account_id: - raise RuntimeError("account_id is required for QMT native passorder") - passorder = self._require_api("passorder") - side = record["side"] - order_type = record["order_type"] - price_type = self.limit_price_type if order_type == "LIMIT" else self.market_price_type - price = float(record["limit_price"] or 0) - result = passorder( - 23 if side == "BUY" else 24, - self.combo_type, - self.config.account_id, - record["symbol"], - price_type, - price, - int(record["quantity"]), - self.config.strategy_name, - self.quick_trade, - client_order_id, - context, - ) - with self._condition: - target = self._orders[client_order_id] - target["status"] = "SUBMITTED" - if result not in (None, ""): - target["qmt_order_id"] = str(result) - - def _execute_cancel(self, command, context): - cancel = self._require_api("cancel") - order_id = command["order_id"] - with self._condition: - record = self._orders.get(order_id) - qmt_order_id = ( - (record or {}).get("qmt_order_id") - or (record or {}).get("order_id") - or order_id - ) - result = cancel(qmt_order_id, self.config.account_id, self.account_type, context) - with self._condition: - record = self._orders.get(order_id) - if record is not None: - record["status"] = "CANCEL_SUBMITTED" if result is not False else "CANCEL_REJECTED" - - def _execute_commands(self, commands, context): - for command in commands: - try: - if command["kind"] == "submit": - self._execute_submit(command, context) - elif command["kind"] == "cancel": - self._execute_cancel(command, context) - except Exception as exc: - key = command.get("client_order_id") or command.get("order_id") - with self._condition: - record = self._orders.get(key) - if record is not None: - record["status"] = "REJECTED" - record["reject_reason"] = "%s: %s" % (exc.__class__.__name__, exc) - print("[bigqmt_backtest] QMT command failed kind=%s error=%s" % (command.get("kind"), exc)) - - def on_bar(self, context): - self.bind_context(context) - self._refresh_qmt_state() - row = self.extractor.extract(context) - appended = self.feed.append(row) - if not appended: - return False - with self._condition: - self.current_index = len(self.feed) - 1 - self.current_frame = self.feed.frame(self.current_index) - new_fill_keys = [key for key in self._fills if key not in self._published_fill_keys] - self._current_frame_fills = [dict(self._fills[key]) for key in new_fill_keys] - self._published_fill_keys.update(new_fill_keys) - index = self.current_index - self._condition.notify_all() - released = self._condition.wait_for( - lambda: self._released_index >= index or self.finished or bool(self._failure), - timeout=self.bar_wait_timeout, - ) - if not released: - self._failure = "external strategy timed out on QMT bar index %d" % index - self._condition.notify_all() - raise TimeoutError(self._failure) - commands = [item for item in self._pending_commands if item.get("frame_index") == index] - self._pending_commands = [item for item in self._pending_commands if item.get("frame_index") != index] - self._execute_commands(commands, context) - self._refresh_qmt_state() - print( - "[bigqmt_backtest] QMT native bar released index=%d datetime=%s symbol=%s commands=%d" - % (index, row["datetime"], row["symbol"], len(commands)) - ) - return True - - def next_bar(self): - with self._condition: - self._require_started() - previous = self.current_index - if self.qmt_completed: - return self._state_unlocked() - self._released_index = max(self._released_index, previous) - self._condition.notify_all() - ready = self._condition.wait_for( - lambda: self.current_index > previous or self.qmt_completed or bool(self._failure), - timeout=self.bar_wait_timeout, - ) - if not ready: - raise TimeoutError("timed out waiting for QMT backtest bar after index %d" % previous) - if self._failure: - raise RuntimeError(self._failure) - return self._state_unlocked() - - def history(self, symbol, count=100, fields=None): - with self._condition: - self._require_started() - end_index = self.current_index - return self.feed.history(symbol, end_index, count=count, fields=fields) - - def orders(self): - with self._condition: - return [dict(value) for value in self._orders.values()] - - def fills(self): - with self._condition: - return [dict(value) for value in self._fills.values()] - - def _state_unlocked(self): - if self.current_frame is None: - return { - "run_id": self.config.run_id, - "started": self.started, - "finished": self.finished, - "done": self.qmt_completed, - "frame_index": -1, - "frame_count": len(self.feed), - "execution_backend": self.execution_backend, - } - return { - "run_id": self.config.run_id, - "started": self.started, - "finished": self.finished, - "done": self.qmt_completed, - "frame_index": self.current_index, - "frame_count": len(self.feed), - "datetime": self.current_frame["datetime"], - "bars": {key: dict(value) for key, value in self.current_frame["bars"].items()}, - "fills": [dict(value) for value in self._current_frame_fills], - "cash": self._asset.get("cash"), - "total_asset": self._asset.get("total_asset"), - "positions": {key: dict(value) for key, value in self._positions.items()}, - "execution_backend": self.execution_backend, - "qmt_completed": self.qmt_completed, - "failure": self._failure, - } - - def state(self): - with self._condition: - return self._state_unlocked() - - def finish(self): - with self._condition: - if self.finished: - return self._result_unlocked() - self._released_index = max(self._released_index, self.current_index) - self.finished = True - self._condition.notify_all() - return self._result_unlocked() - - def _result_unlocked(self): - return { - "schema_version": 1, - "engine_version": self.engine_version, - "run_id": self.config.run_id, - "strategy_name": self.config.strategy_name, - "execution_backend": self.execution_backend, - "qmt_completed": self.qmt_completed, - "order_count": len(self._orders), - "fill_count": len(self._fills), - "final_state": self._state_unlocked(), - "result_owner": "QMT", - } - - def on_qmt_stop(self): - self.feed.close() - with self._condition: - new_fill_keys = [key for key in self._fills if key not in self._published_fill_keys] - self._current_frame_fills = [dict(self._fills[key]) for key in new_fill_keys] - self._published_fill_keys.update(new_fill_keys) - self.qmt_completed = True - self._condition.notify_all() - print("[bigqmt_backtest] QMT native backtest completed bars=%d" % len(self.feed)) - - def on_order(self, order): - item = { - "order_id": str(_attr(order, ("m_strOrderSysID", "order_sys_id", "order_id"), "") or ""), - "client_order_id": str(_attr(order, ("m_strRemark", "remark", "user_order_id"), "") or ""), - "symbol": _full_symbol(order), - "side": _side_from_offset(_attr(order, ("m_nOffsetFlag", "offset_flag"), 0)), - "quantity": int(_attr(order, ("m_nVolumeTotalOriginal", "volume", "quantity"), 0) or 0), - "filled_quantity": int(_attr(order, ("m_nVolumeTraded", "traded_volume", "filled_quantity"), 0) or 0), - "price": _json_number(_attr(order, ("m_dLimitPrice", "m_dPrice", "price"))), - "status": str(_attr(order, ("m_nOrderStatus", "status"), "") or ""), - } - key = item["client_order_id"] or item["order_id"] or ("order:" + uuid.uuid4().hex) - with self._condition: - existing = self._orders.get(key, {}) - existing.update(item) - self._orders[key] = existing - self._condition.notify_all() - return dict(existing) - - def on_trade(self, trade): - item = { - "fill_id": str(_attr(trade, ("m_strTradeID", "trade_id", "fill_id"), "") or ""), - "order_id": str(_attr(trade, ("m_strOrderSysID", "order_sys_id", "order_id"), "") or ""), - "client_order_id": str(_attr(trade, ("m_strRemark", "remark", "user_order_id"), "") or ""), - "symbol": _full_symbol(trade), - "side": _side_from_offset(_attr(trade, ("m_nOffsetFlag", "offset_flag"), 0)), - "quantity": int(_attr(trade, ("m_nVolume", "volume", "quantity"), 0) or 0), - "price": _json_number(_attr(trade, ("m_dPrice", "m_dTradePrice", "price"))), - "filled_at": str(_attr(trade, ("m_strTradeTime", "trade_time", "filled_at"), "") or ""), - } - key = item["fill_id"] or "%s:%s:%s" % (item["order_id"], item["quantity"], item["price"]) - with self._condition: - self._fills[key] = item - self._condition.notify_all() - return dict(item) - - def _query(self, detail_type): - query = self.qmt_api.get("get_trade_detail_data") - if query is None or not self.config.account_id: - return [] - calls = [] - if detail_type in ("ORDER", "DEAL", "TRADE"): - calls.append(lambda: query( - self.config.account_id, self.account_type, detail_type, self.config.strategy_name - )) - calls.append(lambda: query(self.config.account_id, self.account_type, detail_type)) - last_error = None - for call in calls: - try: - return list(call() or []) - except Exception as exc: - last_error = exc - print("[bigqmt_backtest] QMT query failed type=%s error=%s" % (detail_type, last_error)) - return [] - - def _refresh_qmt_state(self): - positions = {} - for row in self._query("POSITION"): - symbol = _full_symbol(row) - if not symbol: - continue - positions[symbol] = { - "symbol": symbol, - "quantity": int(_attr(row, ("m_nVolume", "volume", "quantity"), 0) or 0), - "available": int(_attr(row, ("m_nCanUseVolume", "available", "can_use_volume"), 0) or 0), - "avg_cost": _json_number(_attr(row, ("m_dOpenPrice", "m_dCostPrice", "cost", "avg_cost"))), - } - asset_rows = self._query("ACCOUNT") or self._query("ASSET") - asset = {"cash": None, "total_asset": None} - if asset_rows: - row = asset_rows[0] - asset = { - "cash": _json_number(_attr(row, ("m_dAvailable", "m_dAvailableCash", "available_cash", "cash"))), - "total_asset": _json_number(_attr(row, ("m_dBalance", "m_dAsset", "total_asset", "asset"))), - } - order_rows = self._query("ORDER") - trade_rows = self._query("DEAL") or self._query("TRADE") - with self._condition: - self._positions = positions - self._asset = asset - for row in order_rows: - self.on_order(row) - for row in trade_rows: - self.on_trade(row) - - -class QmtBacktestBridgeRuntime(object): - def __init__(self, config=None, qmt_api=None): - config = dict(config or {}) - bind_endpoint = str(config.pop("bind_endpoint", "tcp://127.0.0.1:16662")) - self.engine = QmtNativeBacktestSession(config=config, qmt_api=qmt_api) - self.protocol = BacktestBridgeProtocol(self.engine) - self.server = ZmqBacktestServer(self.protocol, endpoint=bind_endpoint, exit_on_finish=True) - self.server_thread = None - - def start(self, context): - if self.server_thread is not None: - return - self.engine.bind_context(context) - self.server_thread = threading.Thread( - target=self.server.serve_forever, - name="bigqmt-native-backtest-zmq", - daemon=True, - ) - self.server_thread.start() - if not self.server.wait_until_ready(5.0) or not self.server.actual_endpoint: - raise RuntimeError("QMT native backtest ZMQ service failed to bind") - print( - "[bigqmt_backtest] QMT native service started run_id=%s endpoint=%s account=%s live_ready=False" - % (self.engine.config.run_id, self.server.actual_endpoint, self.engine.config.account_id) - ) - - def on_bar(self, context): - return self.engine.on_bar(context) - - def on_order(self, order): - return self.engine.on_order(order) - - def on_trade(self, trade): - return self.engine.on_trade(trade) - - def on_qmt_stop(self): - self.engine.on_qmt_stop() - - def stop_server(self): - self.server.stop() - - -def reset_runtime(): - global _RUNTIME - if _RUNTIME is not None: - _RUNTIME.stop_server() - _RUNTIME = None - - -def get_runtime(): - return _RUNTIME - - -def init(ContextInfo): - global _RUNTIME - reset_runtime() - _RUNTIME = QmtBacktestBridgeRuntime(_CONFIG, _QMT_API) - _RUNTIME.start(ContextInfo) - return _RUNTIME - - -def handlebar(ContextInfo): - if _RUNTIME is None: - init(ContextInfo) - return _RUNTIME.on_bar(ContextInfo) - - -def order_callback(ContextInfo, orderInfo): - if _RUNTIME is not None: - return _RUNTIME.on_order(orderInfo) - return None - - -def deal_callback(ContextInfo, dealInfo): - if _RUNTIME is not None: - return _RUNTIME.on_trade(dealInfo) - return None - - -def stop(ContextInfo=None): - if _RUNTIME is not None: - _RUNTIME.on_qmt_stop() - - -def after_backtest(ContextInfo=None): - return stop(ContextInfo) diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/server.py b/reference/xtquant_big_convert/src/bigqmt_backtest/server.py deleted file mode 100644 index f5d63d9..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/server.py +++ /dev/null @@ -1,80 +0,0 @@ -"""Command-line entry for the standalone ZMQ backtest bridge.""" - -import argparse -import json -import os -import sys -import uuid - -from .data_feed import CsvBarFeed -from .engine import BacktestConfig, BacktestEngine -from .protocol import BacktestBridgeProtocol -from .zmq_server import ZmqBacktestServer - - -def _load_config(path): - if not path: - return {} - with open(os.path.abspath(path), encoding="utf-8") as handle: - payload = json.load(handle) - if not isinstance(payload, dict): - raise ValueError("config JSON must be an object") - return payload - - -def _parser(): - parser = argparse.ArgumentParser(description="Standalone ZMQ backtest bridge") - parser.add_argument("--data", required=True, help="UTF-8 CSV historical bar file") - parser.add_argument("--config", default="", help="Optional UTF-8 JSON backtest config") - parser.add_argument("--run-id", default="", help="Unique run identity") - parser.add_argument("--output-dir", default="", help="Artifact directory") - parser.add_argument("--bind", default="tcp://127.0.0.1:16661", help="ZMQ REP bind endpoint") - parser.add_argument("--default-symbol", default="", help="Used when CSV has no symbol column") - parser.add_argument("--initial-cash", type=float, default=None) - parser.add_argument("--slippage-bps", type=float, default=None) - parser.add_argument("--max-volume-participation", type=float, default=None) - parser.add_argument("--keep-running", action="store_true", help="Do not stop server after finish") - return parser - - -def build_engine(args): - payload = _load_config(args.config) - run_id = str(args.run_id or payload.pop("run_id", "") or ("bt-" + uuid.uuid4().hex[:12])) - output_dir = args.output_dir or payload.pop("output_dir", "") or os.path.join("backtest_runs", run_id) - if args.initial_cash is not None: - payload["initial_cash"] = args.initial_cash - if args.slippage_bps is not None: - payload["slippage_bps"] = args.slippage_bps - if args.max_volume_participation is not None: - payload["max_volume_participation"] = args.max_volume_participation - config = BacktestConfig(run_id=run_id, output_dir=output_dir, **payload) - feed = CsvBarFeed(args.data, default_symbol=args.default_symbol) - return BacktestEngine(feed, config) - - -def main(argv=None): - args = _parser().parse_args(argv) - engine = build_engine(args) - server = ZmqBacktestServer( - BacktestBridgeProtocol(engine), - endpoint=args.bind, - exit_on_finish=not args.keep_running, - ) - startup = { - "event": "backtest_bridge_starting", - "run_id": engine.config.run_id, - "bind": args.bind, - "data_hash": engine.feed.data_hash, - "output_dir": engine.config.output_dir, - "live_ready": False, - } - print(json.dumps(startup, ensure_ascii=False, sort_keys=True), flush=True) - try: - server.serve_forever() - except KeyboardInterrupt: - server.stop() - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/strategy.py b/reference/xtquant_big_convert/src/bigqmt_backtest/strategy.py deleted file mode 100644 index 5a04355..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/strategy.py +++ /dev/null @@ -1,75 +0,0 @@ -"""Small external-strategy contract and a synchronous ZMQ runner.""" - - -class StrategyContext(object): - def __init__(self, client): - self.client = client - self.state = None - - @property - def now(self): - return None if self.state is None else self.state.get("datetime") - - @property - def cash(self): - return 0 if self.state is None else self.state.get("cash", 0) - - @property - def positions(self): - return {} if self.state is None else self.state.get("positions", {}) - - def history(self, symbol, count=100, fields=None): - return self.client.history(symbol, count=count, fields=fields) - - -class ExternalStrategyRunner(object): - """Drive a user strategy without exposing future bars. - - Strategy methods are optional: - - * ``on_start(context)`` - * ``on_bar(context, bars) -> iterable[order dict]`` - * ``on_fill(context, fill)`` - * ``on_finish(context, result)`` - """ - - def __init__(self, client, strategy): - self.client = client - self.strategy = strategy - self.context = StrategyContext(client) - - def _call(self, name, *args): - callback = getattr(self.strategy, name, None) - return callback(*args) if callback is not None else None - - def _apply_orders(self, orders): - for order in list(orders or []): - payload = dict(order) - self.client.submit_order( - symbol=payload["symbol"], - side=payload["side"], - quantity=payload["quantity"], - order_type=payload.get("order_type", "MARKET"), - limit_price=payload.get("limit_price"), - client_order_id=payload.get("client_order_id", ""), - time_in_force=payload.get("time_in_force", "NEXT_BAR"), - ) - - def run(self): - if not self.client.run_id: - self.client.describe() - state = self.client.start() - self.context.state = state - self._call("on_start", self.context) - while True: - for fill in state.get("fills", []): - self._call("on_fill", self.context, fill) - orders = self._call("on_bar", self.context, state.get("bars", {})) - self._apply_orders(orders) - if state.get("done"): - break - state = self.client.next_bar() - self.context.state = state - result = self.client.finish() - self._call("on_finish", self.context, result) - return result diff --git a/reference/xtquant_big_convert/src/bigqmt_backtest/zmq_server.py b/reference/xtquant_big_convert/src/bigqmt_backtest/zmq_server.py deleted file mode 100644 index fbb916a..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_backtest/zmq_server.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Minimal REQ/REP ZMQ server for one isolated backtest run.""" - -import json -import threading - - -class ZmqBacktestServer(object): - def __init__(self, protocol, endpoint="tcp://127.0.0.1:16661", exit_on_finish=False, poll_ms=100): - self.protocol = protocol - self.endpoint = str(endpoint) - self.exit_on_finish = bool(exit_on_finish) - self.poll_ms = int(poll_ms) - self._stop_event = threading.Event() - self._ready_event = threading.Event() - self.actual_endpoint = None - - def wait_until_ready(self, timeout_seconds=None): - return self._ready_event.wait(timeout_seconds) - - def stop(self): - self._stop_event.set() - - def serve_forever(self): - import zmq - - context = zmq.Context.instance() - socket = context.socket(zmq.REP) - socket.setsockopt(zmq.LINGER, 0) - socket.setsockopt(zmq.RCVHWM, 1000) - socket.setsockopt(zmq.SNDHWM, 1000) - try: - if self.endpoint.endswith(":0"): - base = self.endpoint.rsplit(":", 1)[0] - port = socket.bind_to_random_port(base) - self.actual_endpoint = "%s:%d" % (base, port) - else: - socket.bind(self.endpoint) - self.actual_endpoint = self.endpoint - self._ready_event.set() - poller = zmq.Poller() - poller.register(socket, zmq.POLLIN) - while not self._stop_event.is_set(): - events = dict(poller.poll(self.poll_ms)) - if socket not in events: - continue - try: - request = json.loads(socket.recv().decode("utf-8")) - response = self.protocol.handle(request) - except Exception as exc: - response = { - "schema_version": 1, - "request_id": "", - "run_id": self.protocol.engine.config.run_id, - "client_id": "", - "method": "", - "ok": False, - "data": None, - "error": "%s: %s" % (exc.__class__.__name__, exc), - } - socket.send(json.dumps(response, ensure_ascii=False, separators=(",", ":")).encode("utf-8")) - if self.exit_on_finish and self.protocol.engine.finished: - break - finally: - self._ready_event.set() - socket.close(linger=0) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/README.md b/reference/xtquant_big_convert/src/bigqmt_signal_trader/README.md deleted file mode 100644 index 1e86dcf..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/README.md +++ /dev/null @@ -1,50 +0,0 @@ -# bigqmt_signal_trader - -大 QMT 信号交易包的核心骨架。当前版本只完成可替换包边界和 dry-run 运行入口,不会发送真实委托。 - -## 已完成 - -- `TradeSignal`、`OrderRequest`、`PositionSnapshot`、`AccountSnapshot` 等核心数据模型。 -- `SignalSource`、`MarketDataProvider`、`PositionProvider`、`OrderGateway`、`PositionSyncSink`、`StateStore` 等替换接口。 -- `SignalTradingApp.tick()` 编排流程: - 1. 读取信号。 - 2. 原子 claim。 - 3. 读取持仓。 - 4. 计算买卖数量。 - 5. 生成价格。 - 6. 调用可替换 `OrderGateway`。 - 7. 写回状态。 - 8. 同步持仓快照。 -- `DryRunOrderGateway`:记录委托请求,不调用真实 `passorder`。 -- `bigqmt_signal_trader_strategy.py`:大 QMT 运行文件骨架,响应 `init`、`adjust`、`order_callback`、`deal_callback`、`sync_positions`。 - -## 当前安全状态 - -默认 `adapter_factory.build_app()` 使用: - -- 空信号源。 -- 空行情源。 -- 空持仓源。 -- dry-run 下单 gateway。 -- no-op 状态存储。 -- 内存持仓同步 sink。 - -因此即使大 QMT 加载该运行文件,也不会真实下单。 - -## 后续接入顺序 - -1. 实现 `BigQmtMarketDataProvider` 和 `BigQmtPositionProvider`。 -2. 实现 `BigQmtOrderGateway(passorder/cancel/get_trade_detail_data)`。 -3. 实现 Redis Stream / MySQL outbox 信号源。 -4. 实现 Redis / MySQL 状态写回。 -5. 实现 Redis / MySQL 持仓同步 sink。 -6. dry-run 跑通后,再按账户灰度切换真实下单。 - -## 测试 - -```powershell -cd -python -m unittest discover -s tests\bigqmt_signal_trader -``` - - diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/__init__.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/__init__.py deleted file mode 100644 index 394d740..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/__init__.py +++ /dev/null @@ -1,32 +0,0 @@ -"""可替换的大 QMT 信号下单包核心模块。""" - -__version__ = "0.2.0" - -from .app import SignalTradingApp -from .models import ( - AccountSnapshot, - AssetSnapshot, - OrderRequest, - OrderSubmitResult, - PositionSnapshot, - SignalAction, - SignalStatus, - TradeSignal, -) -from .xtquant_compat import BigQmtRpcClient, BigQmtXtData, BigQmtXtTrader - -__all__ = [ - "AccountSnapshot", - "AssetSnapshot", - "BigQmtRpcClient", - "BigQmtXtData", - "BigQmtXtTrader", - "OrderRequest", - "OrderSubmitResult", - "PositionSnapshot", - "SignalAction", - "SignalStatus", - "SignalTradingApp", - "TradeSignal", - "__version__", -] diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapter_factory.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapter_factory.py deleted file mode 100644 index f1154d5..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapter_factory.py +++ /dev/null @@ -1,159 +0,0 @@ -"""根据配置装配 SignalTradingApp。 - -当前第一版只提供安全的空信号 + dry-run 默认实现,后续再接 Redis/MySQL/大 QMT -真实 adapter。这样大 QMT 运行文件可以先加载和响应调度,不会误发真实委托。 -""" - -from .adapters.order_dryrun import DryRunOrderGateway -from .app import SignalTradingApp -from .models import AssetSnapshot - - -class EmptySignalSource: - def fetch(self, account_id, limit): - return [] - - def ack(self, signal): - return None - - -class EmptyMarketDataProvider: - def get_ticks(self, codes): - return {} - - def get_instrument(self, code): - return {} - - -class EmptyPositionProvider: - def get_positions(self, account_id): - return {} - - def get_asset(self, account_id): - return AssetSnapshot(account_id=account_id, cash=None, total_asset=None) - - -class NoopPositionSyncSink: - def __init__(self): - self.snapshots = [] - - def publish(self, snapshot): - self.snapshots.append(snapshot) - - -class NoopStateStore: - def claim(self, signal, consumer_id): - return False - - def mark_submitted(self, signal_id, result): - return None - - def mark_finished(self, signal_id, status, message=""): - return None - - -def _config_bool(value, default=False): - if value is None: - return default - if isinstance(value, bool): - return value - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -def build_app(context_info=None, config=None): - config = config or {} - mode = str(config.get("mode") or "dryrun").lower() - account_id = config.get("account_id", "default") - source_type = str(config.get("signal_source_type") or config.get("source_type") or "").lower() - state_type = str(config.get("state_store_type") or "").lower() - position_sync_type = str(config.get("position_sync_type") or "").lower() - - signal_source = config.get("signal_source") - market_data = config.get("market_data") - position_provider = config.get("position_provider") - order_gateway = config.get("order_gateway") - position_sync_sink = config.get("position_sync_sink") - state_store = config.get("state_store") - redis_client = config.get("redis_client") - - if source_type == "redis" or state_type == "redis" or position_sync_type == "redis": - from .adapters.redis_common import build_redis_client - - redis_client = redis_client or build_redis_client(config.get("redis") or {}) - - if source_type == "redis": - from .adapters.signal_redis import RedisStreamSignalSource - - redis_cfg = config.get("redis") or {} - signal_source = signal_source or RedisStreamSignalSource( - redis_client=redis_client, - stream_key_template=redis_cfg.get("stream_key_template", "bigqmt:signals:{account_id}"), - group_name=redis_cfg.get("group_name", "bigqmt-signal-trader"), - consumer_name=redis_cfg.get("consumer_name", "bigqmt-consumer"), - block_ms=int(redis_cfg.get("block_ms", 0)), - ) - - if state_type == "redis" or (source_type == "redis" and state_store is None): - from .adapters.state_redis import RedisStateStore - - redis_cfg = config.get("redis") or {} - state_store = state_store or RedisStateStore( - redis_client=redis_client, - account_id=account_id, - claim_key_template=redis_cfg.get("claim_key_template", "bigqmt:signal_claim:{account_id}:{signal_id}"), - status_key_template=redis_cfg.get("status_key_template", "bigqmt:signal_status:{account_id}:{signal_id}"), - claim_ttl_seconds=int(redis_cfg.get("claim_ttl_seconds", 3600)), - status_ttl_seconds=int(redis_cfg.get("status_ttl_seconds", 86400)), - ) - - if position_sync_type == "redis": - from .adapters.position_sync_redis import RedisPositionSyncSink - - redis_cfg = config.get("redis") or {} - position_sync_sink = position_sync_sink or RedisPositionSyncSink( - redis_client=redis_client, - key_template=redis_cfg.get("position_key_template", "bigqmt:positions:{account_id}"), - event_stream_template=redis_cfg.get("position_event_stream_template", "bigqmt:position_events:{account_id}"), - ttl_seconds=int(redis_cfg.get("position_ttl_seconds", 120)), - publish_events=_config_bool(redis_cfg.get("position_publish_events"), True), - ) - - if mode == "bigqmt": - from .adapters.market_bigqmt import BigQmtMarketDataProvider - from .adapters.order_bigqmt import BigQmtOrderGateway - from .adapters.position_bigqmt import BigQmtPositionProvider - - qmt_api = config.get("qmt_api") or {} - get_trade_detail_data_func = qmt_api.get("get_trade_detail_data") - market_data = market_data or BigQmtMarketDataProvider(context_info, qmt_api=qmt_api) - position_provider = position_provider or BigQmtPositionProvider( - get_trade_detail_data_func=get_trade_detail_data_func, - account_type=config.get("account_type", "STOCK"), - ) - # passorder / cancel need the RAW QMT ContextInfo as their last arg -- QMT's - # injected passorder reads internals off it (e.g. .request_id). Our runtime - # wrapper (BigQmtRuntimeAdapter) doesn't have those, so unwrap it here. - raw_context_info = getattr(context_info, "context_info", context_info) - order_gateway = order_gateway or BigQmtOrderGateway( - context_info=raw_context_info, - account_id=account_id, - passorder_func=qmt_api.get("passorder"), - cancel_func=qmt_api.get("cancel"), - get_trade_detail_data_func=get_trade_detail_data_func, - account_type=config.get("account_type", "STOCK"), - combo_type=int(config.get("combo_type", 1101)), - price_type=int(config.get("order_price_type", 11)), - quick_trade=int(config.get("quick_trade", 2)), - ) - - return SignalTradingApp( - account_id=account_id, - signal_source=signal_source or EmptySignalSource(), - market_data=market_data or EmptyMarketDataProvider(), - position_provider=position_provider or EmptyPositionProvider(), - order_gateway=order_gateway or DryRunOrderGateway(), - position_sync_sink=position_sync_sink or NoopPositionSyncSink(), - state_store=state_store or NoopStateStore(), - consumer_id=config.get("consumer_id", "bigqmt-signal-trader"), - fetch_limit=config.get("fetch_limit", 20), - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/__init__.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/__init__.py deleted file mode 100644 index 1ebebd5..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""具体外部系统 adapter。""" diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/market_bigqmt.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/market_bigqmt.py deleted file mode 100644 index 97e0128..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/market_bigqmt.py +++ /dev/null @@ -1,1115 +0,0 @@ -"""Big QMT market data adapter. - -This module wraps two QMT runtime objects: - -* ``ContextInfo`` — the strategy-scoped object exposed inside ``handlebar`` / - ``init``. It carries methods that operate on the *current* subscribed context - (``get_market_data_ex``, ``get_full_tick``, ``get_instrumentdetail`` ...). -* the **native xtdata SDK** (``bin.x64/Lib/site-packages/xtquant/xtdata.py``) — - a module of global functions that talk to the local quote service directly. - Some APIs only exist here, never as ContextInfo methods. - -The split matters. Per the official docs and the ContextInfo IDE stub -(``_PyContextInfo.py``): - -* ``get_sector_list`` / ``get_holidays`` are **xtdata module functions** - (SDK xtdata.py lines 784 / 1197). They are *not* ContextInfo methods, so - calling ``ContextInfo.get_sector_list()`` raises NotImplementedError. -* ``get_markets`` / ``get_market_last_trade_date`` do not exist in either the - ContextInfo stub or the xtdata SDK — they are MiniQMT-only conveniences that - must be synthesized from ``get_trading_dates``. -* ``get_trading_dates`` exists on BOTH objects but with **different first - arguments**: the ContextInfo method takes ``stockcode`` while the xtdata - module function takes ``market``. We pass ``market`` (that is what every - caller in this codebase supplies), so we route through xtdata. - -This module does not make trading decisions. -""" - -import importlib -import importlib.util - -from ..code_utils import normalize_stock_code - - -MARKET_CODES = {"SH", "SZ", "BJ", "HK"} - - -def normalize_market_or_stock_code(code): - text = str(code or "").strip().upper() - if text in MARKET_CODES: - return text - return normalize_stock_code(text) - - -def _as_list(value): - if value is None: - return [] - if isinstance(value, str): - return [value] - return list(value) - - -def _raw_frame_columns(field_list): - columns = [str(field) for field in (field_list or [])] - if columns and "stime" not in columns: - columns.insert(0, "stime") - return columns - - -def _raw_market_data_payload(payload, field_list, stock_list): - if not isinstance(payload, dict): - return payload - source = {str(code): records for code, records in payload.items()} - codes = [] - for code in list(stock_list or []) + list(source): - text = str(code) - if text not in codes: - codes.append(text) - columns = _raw_frame_columns(field_list) - return { - code: { - "__bigqmt_type__": "DataFrame", - "columns": columns, - "records": source.get(code) or [], - } - for code in codes - } - - -_NATIVE_XTDATA = None # cached native xtdata SDK module (None = not yet tried) -_NATIVE_XTDATA_UNAVAILABLE = object() # sentinel: looked, not importable - - -def _load_native_xtdata(): - """Return the *native* xtdata SDK module shipped with the QMT install. - - The Big QMT process ships two ``xtquant.xtdata`` modules: - - * ``python/xtquant/xtdata.py`` — our RPC shim (forwards back over Redis). - * ``bin.x64/Lib/site-packages/xtquant/xtdata.py`` — the real SDK that - connects to the local quote service via ``get_client()``. - - In the server-side adapter we need the real SDK because the global-data - functions (sectors, holidays, trading dates) only exist there. We load it - by absolute path so our shim (which may shadow it on ``sys.path``) never - wins. Returns ``None`` when the SDK is unavailable (e.g. running outside - QMT, or in a unit test) so callers can degrade gracefully. - """ - global _NATIVE_XTDATA - if _NATIVE_XTDATA is _NATIVE_XTDATA_UNAVAILABLE: - return None - if _NATIVE_XTDATA is not None: - return _NATIVE_XTDATA - try: - import os - import sys - - # Locate /bin.x64/{lib,Lib}/site-packages that holds the REAL - # xtquant package. Walk up from this file (works whether we live under - # python/bigqmt_signal_trader/adapters/ in QMT or src/... in the repo). - real_sp = None - start = os.path.abspath(__file__) - for _ in range(8): - parent = os.path.dirname(start) - if parent == start: - break - for libdir in ("lib", "Lib"): - candidate = os.path.join(parent, "bin.x64", libdir, "site-packages") - if os.path.isdir(os.path.join(candidate, "xtquant")): - real_sp = candidate - break - if real_sp: - break - start = parent - - loaded = None - if real_sp: - # Import the real xtquant PACKAGE (not xtdata.py standalone) so its - # package-relative imports (xtbson etc.) resolve. Un-shadow our RPC - # shim (python/xtquant, src/xtquant) which otherwise wins on sys.path: - # put the real site-packages first and drop any already-imported shim - # xtquant modules (their __file__ is not under bin.x64/). - if real_sp not in sys.path: - sys.path.insert(0, real_sp) - for name in [n for n in list(sys.modules) if n == "xtquant" or n.startswith("xtquant.")]: - mod_file = getattr(sys.modules.get(name), "__file__", "") or "" - if "bin.x64" not in mod_file: - del sys.modules[name] - try: - module = importlib.import_module("xtquant.xtdata") - if "bin.x64" in (getattr(module, "__file__", "") or ""): - loaded = module - except Exception: - loaded = None - _NATIVE_XTDATA = loaded if loaded is not None else _NATIVE_XTDATA_UNAVAILABLE - except Exception: - _NATIVE_XTDATA = _NATIVE_XTDATA_UNAVAILABLE - return None if _NATIVE_XTDATA is _NATIVE_XTDATA_UNAVAILABLE else _NATIVE_XTDATA - - -class BigQmtMarketDataProvider: - def __init__(self, context_info, native_xtdata=None, qmt_api=None): - self.context_info = context_info - # Allow injection for tests; otherwise resolve lazily on first use. - self._native_xtdata = native_xtdata - self.qmt_api = dict(qmt_api or {}) - - def _context_method(self, method_name): - method = getattr(self.context_info, method_name, None) - if method is None: - raise NotImplementedError("ContextInfo.%s is not available" % method_name) - return method - - def _call_context(self, method_name, *args, **kwargs): - return self._context_method(method_name)(*args, **kwargs) - - def _native(self): - """Return the native xtdata SDK, resolving it lazily on first use. - - Returns None when the SDK is not importable. NOTE: in a Big QMT - (full trading terminal) process the SDK loads but its get_client() - cannot connect to a quote service — there is no MiniQMT process - writing ~/.xtquant/*/xtdata.cfg. Callers must therefore be ready for - the SDK call itself to raise "无法连接行情服务" and fall back. - """ - if self._native_xtdata is None: - self._native_xtdata = _load_native_xtdata() - return self._native_xtdata - - def _native_or_context(self, func_name, context_caller, *args, **kwargs): - """Prefer the xtdata SDK function, fall back to a ContextInfo call. - - Several data APIs exist only as xtdata module functions. When the SDK - is available AND its quote service is reachable we use it. Otherwise - we fall back to ContextInfo so callers get a best-effort answer. - """ - module = self._native() - if module is not None: - fn = getattr(module, func_name, None) - if fn is not None: - try: - return fn(*args, **kwargs) - except Exception as exc: - # Big QMT path: SDK present but no quote service to talk - # to ("无法连接行情服务"). Don't crash — let the ContextInfo - # fallback have a turn. - pass - return context_caller() - - def _call_first_supported(self, shapes): - last_error = None - for method_name, args, kwargs in shapes: - method = getattr(self.context_info, method_name, None) - if method is None: - continue - try: - return method(*args, **kwargs) - except TypeError as exc: - last_error = exc - continue - if last_error is not None: - raise last_error - raise NotImplementedError("none of the ContextInfo methods is available") - - def _market_data_shapes(self, method_name, **params): - field_list = list(params.get("field_list") or params.get("fields") or []) - stock_list = _as_list(params.get("stock_list") or params.get("stock_code")) - period = params.get("period", "1d") - start_time = params.get("start_time", "") - end_time = params.get("end_time", "") - count = params.get("count", -1) - dividend_type = params.get("dividend_type", "none") - fill_data = params.get("fill_data", True) - data_dir = params.get("data_dir") - - mini_kwargs = { - "field_list": field_list, - "stock_list": stock_list, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - "fill_data": fill_data, - } - big_kwargs = { - "fields": field_list, - "stock_code": stock_list, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - } - if method_name == "get_local_data" and data_dir is not None: - mini_kwargs["data_dir"] = data_dir - big_kwargs["data_dir"] = data_dir - positional_tail_kwargs = { - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - } - if method_name == "get_local_data" and data_dir is not None: - positional_tail_kwargs["data_dir"] = data_dir - - return [ - (method_name, (), big_kwargs), - (method_name, (), mini_kwargs), - ( - method_name, - (field_list, stock_list, period, start_time, end_time, count, dividend_type, fill_data), - {}, - ), - (method_name, (field_list, stock_list), positional_tail_kwargs), - ( - method_name, - (field_list,), - { - "stock_code": stock_list, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - }, - ), - ( - method_name, - (field_list,), - { - "stock_list": stock_list, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - "fill_data": fill_data, - }, - ), - ] - - def get_ticks(self, codes): - normalized_codes = [normalize_market_or_stock_code(code) for code in codes] - data = self.context_info.get_full_tick(normalized_codes) - return data or {} - - def get_instrument(self, code): - normalized = normalize_stock_code(code) - data = self.context_info.get_instrumentdetail(normalized) - return data or {} - - def get_instrument_type(self, code, variety_list=None): - if hasattr(self.context_info, "get_instrument_type"): - return self.context_info.get_instrument_type(code, variety_list) - normalized = normalize_stock_code(code) - pure = normalized.split(".")[0] - result = { - "stock": pure.startswith(("000", "001", "002", "003", "300", "301", "600", "601", "603", "605", "688", "689")), - "fund": pure.startswith(("15", "16", "50", "51", "56", "58")), - "etf": pure.startswith(("15", "51", "56", "58")), - "bond": pure.startswith(("11", "12")), - "index": pure.startswith(("000", "399")) and not normalized.startswith(("000001.SZ", "000002.SZ")), - } - if variety_list: - return {str(name): bool(result.get(str(name), False)) for name in variety_list} - return result - - def get_stock_list_in_sector(self, sector_name, real_timetag=-1): - shapes = [ - ("get_stock_list_in_sector", (sector_name, real_timetag), {}), - ("get_stock_list_in_sector", (sector_name,), {}), - ] - data = self._call_first_supported(shapes) - return data or [] - - def get_market_data( - self, - field_list=None, - stock_list=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - fill_data=True, - ): - return self._call_first_supported( - self._market_data_shapes( - "get_market_data", - field_list=field_list, - stock_list=stock_list, - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - fill_data=fill_data, - ) - ) - - def get_market_data_ex(self, **kwargs): - raw_method = getattr(self.context_info, "get_market_data_ex_ori", None) - if callable(raw_method): - raw_data = self._call_first_supported( - self._market_data_shapes("get_market_data_ex_ori", **kwargs) - ) - return _raw_market_data_payload( - raw_data, - kwargs.get("field_list") or kwargs.get("fields"), - kwargs.get("stock_list") or kwargs.get("stock_code"), - ) - shapes = self._market_data_shapes("get_market_data_ex", **kwargs) - if hasattr(self.context_info, "get_market_data"): - shapes.extend(self._market_data_shapes("get_market_data", **kwargs)) - return self._call_first_supported(shapes) - - def get_local_data(self, **kwargs): - shapes = self._market_data_shapes("get_local_data", **kwargs) - if hasattr(self.context_info, "get_market_data"): - shapes.extend(self._market_data_shapes("get_market_data", **kwargs)) - return self._call_first_supported(shapes) - - def get_divid_factors(self, stock_code, start_time="", end_time=""): - # ContextInfo stub: get_divid_factors(marketAndStock, date='') — only 2 - # positional args (code + a single date). The xtdata SDK has the same - # 2-arg shape. We accept start_time/end_time for API compatibility but - # pass end_time (or start_time) as the single date when supplied. - date = end_time or start_time - if date: - return self._call_context("get_divid_factors", stock_code, date) - return self._call_context("get_divid_factors", stock_code) - - def _download(self, func_name, sdk_args, sdk_kwargs, ctx_call): - """Download history in Big QMT. - - Full Big QMT exposes historical-data supplementation as the injected - global ``down_history_data`` function. Prefer it directly; the MiniQMT - ``xtdata`` client usually cannot connect inside the full terminal. - """ - down_history_data = self.qmt_api.get("down_history_data") - if callable(down_history_data): - if func_name == "download_history_data": - stock_code, period, start_time, end_time = sdk_args - return down_history_data(stock_code, period, start_time, end_time) - if func_name == "download_history_data2": - stock_list, period, start_time, end_time = sdk_args - result = None - for stock_code in stock_list: - result = down_history_data(stock_code, period, start_time, end_time) - return result - - if getattr(self.context_info, func_name, None) is not None: - return ctx_call() - raise NotImplementedError( - "%s unavailable (down_history_data unavailable; ContextInfo has no %s)" - % (func_name, func_name) - ) - - def download_history_data(self, stock_code, period, start_time="", end_time="", incrementally=None): - def _via_context(): - kwargs = {"stock_code": stock_code, "period": period, "start_time": start_time, "end_time": end_time} - if incrementally is not None: - kwargs["incrementally"] = incrementally - return self._call_context("download_history_data", **kwargs) - - sdk_kwargs = {"incrementally": incrementally} if incrementally is not None else {} - return self._download( - "download_history_data", (stock_code, period, start_time, end_time), sdk_kwargs, _via_context - ) - - def download_history_data2(self, stock_list, period, start_time="", end_time="", incrementally=None): - stock_list = _as_list(stock_list) - - def _via_context(): - kwargs = {"stock_list": stock_list, "period": period, "start_time": start_time, "end_time": end_time} - if incrementally is not None: - kwargs["incrementally"] = incrementally - return self._call_context("download_history_data2", **kwargs) - - sdk_kwargs = {"incrementally": incrementally} if incrementally is not None else {} - return self._download( - "download_history_data2", (stock_list, period, start_time, end_time), sdk_kwargs, _via_context - ) - - def get_trading_dates(self, market, start_time="", end_time="", count=-1): - # xtdata SDK signature: get_trading_dates(market, start_time, end_time, count) - # ContextInfo stub signature: get_trading_dates(stockcode, start_date, end_date, count, period) - # — note the FIRST argument differs (market vs stockcode). Every caller in - # this codebase passes a market code, so the xtdata SDK is the correct path. - def _via_context(): - # ContextInfo's first arg is stockcode; pass market through anyway so - # backtest contexts still return something rather than crashing. - return self._call_context("get_trading_dates", market, start_time, end_time, count) - - return self._native_or_context( - "get_trading_dates", _via_context, market, start_time, end_time, count - ) - - def get_holidays(self): - """Return the holiday (non-trading) date list. - - Authoritative source is the xtdata SDK (xtdata.py line 1197). In a Big - QMT (full terminal) process the SDK is present but cannot reach its - quote service, and ContextInfo has no get_holidays method. In that - case we derive the holidays from the A-share trading calendar: any - weekday in a recent window that is NOT a trading day is a holiday. - This is slower than the SDK (it walks the calendar) but correct. - """ - def _via_context(): - return self._call_context("get_holidays") - - try: - result = self._native_or_context("get_holidays", _via_context) - if result: - return result - except Exception: - pass - # Big QMT fallback: derive holidays from the trading calendar. - return self._holidays_from_trading_calendar() - - def _holidays_from_trading_calendar(self, years_back=1): - """Derive holiday dates (YYYYMMDD strings) from trading dates. - - Walks business days across [today - years_back, today] and collects - those that are absent from the A-share trading calendar. Requires - get_trading_dates to work (it does in Big QMT via ContextInfo). - """ - import datetime - - try: - trading = set(str(d) for d in (self.get_trading_dates("SH", "", "", -1) or [])) - except Exception: - return [] - today = datetime.date.today() - start = today.replace(year=today.year - years_back, month=1, day=1) - holidays = [] - cur = start - one_day = datetime.timedelta(days=1) - while cur <= today: - if cur.weekday() < 5: # Mon-Fri - ymd = cur.strftime("%Y%m%d") - if ymd not in trading: - holidays.append(ymd) - cur += one_day - return holidays - - def download_holiday_data(self, incrementally=True): - def _via_context(): - return self._call_context("download_holiday_data", incrementally=incrementally) - - module = self._native() - if module is not None and hasattr(module, "download_holiday_data"): - try: - return module.download_holiday_data(incrementally) - except TypeError: - # older SDKs may not accept the keyword - return module.download_holiday_data() - return _via_context() - - def get_ipo_info(self, start_time="", end_time=""): - return self._call_context("get_ipo_info", start_time, end_time) - - def get_etf_info(self): - # xtdata SDK 函数(SDK 893 行),ContextInfo 无此方法,走 native SDK。 - def _via_context(): - return self._raise_unavailable("get_etf_info") - return self._native_or_context("get_etf_info", _via_context) - - def download_etf_info(self): - return self._call_context("download_etf_info") - - def get_option_list(self, undl_code, dedate, opttype="", isavailavle=False): - return self._call_context("get_option_list", undl_code, dedate, opttype, isavailavle) - - def get_his_option_list(self, undl_code, dedate): - return self._call_context("get_his_option_list", undl_code, dedate) - - def get_his_option_list_batch(self, undl_code, start_time="", end_time=""): - return self._call_context("get_his_option_list_batch", undl_code, start_time, end_time) - - def get_financial_data(self, stock_list, table_list=None, start_time="", end_time="", report_type="report_time"): - # ContextInfo stub signature: get_financial_data(fieldList, stockList, startDate, endDate, report_type) - # — fieldList (table_list) comes FIRST, stockList SECOND. Our public API keeps - # the xtdata order (stock_list, table_list) so callers don't change, but we - # must swap when forwarding to ContextInfo. - return self._call_context( - "get_financial_data", - table_list or [], - stock_list, - start_time, - end_time, - report_type, - ) - - def download_financial_data(self, stock_list, table_list=None, start_time="", end_time="", incrementally=None): - # download_financial_data is an xtdata SDK function, not a ContextInfo method. - # Try native SDK first, fall back to ContextInfo (may raise NotImplementedError). - kwargs = { - "stock_list": stock_list, - "table_list": table_list or [], - "start_time": start_time, - "end_time": end_time, - } - if incrementally is not None: - kwargs["incrementally"] = incrementally - def _via_context(): - return self._call_context("download_financial_data", **kwargs) - return self._native_or_context("download_financial_data", _via_context, **kwargs) - - def download_financial_data2(self, stock_list, table_list=None, start_time="", end_time=""): - # download_financial_data2 is an xtdata SDK function, not a ContextInfo method. - def _via_context(): - return self._call_context("download_financial_data2", stock_list, table_list or [], start_time, end_time) - return self._native_or_context( - "download_financial_data2", _via_context, stock_list, table_list or [], start_time, end_time - ) - - # Well-known sector names that Big QMT's ContextInfo recognises for - # get_stock_list_in_sector / get_sector. Used as a fallback when the full - # sector list is not enumerable (Big QMT has no get_sector_list method and - # the xtdata SDK's quote service is unreachable inside the full terminal). - _FALLBACK_SECTORS = ( - "沪深A股", "沪市A股", "深市A股", "科创板", "创业板", - "上证期权", "深证期权", "中金所", - "沪市债券", "深市债券", - "沪市基金", "深市基金", "沪深ETF", - ) - - def get_sector_list(self): - """Return the list of sector names. - - Authoritative source is the xtdata SDK (xtdata.py line 784). In a Big - QMT (full terminal) process the SDK is present but cannot reach its - quote service, and ContextInfo has no get_sector_list method either. - In that case we fall back to a curated list of well-known sector names - so callers can still drive get_stock_list_in_sector(name). - """ - def _via_context(): - return self._call_context("get_sector_list") - - try: - result = self._native_or_context("get_sector_list", _via_context) - if result: - return result - except (NotImplementedError, Exception): - pass - return list(self._FALLBACK_SECTORS) - - def get_sector_info(self, sector_name=""): - # xtdata SDK 函数,ContextInfo 无此方法,走 native SDK。 - def _via_context(): - return self._raise_unavailable("get_sector_info") - return self._native_or_context("get_sector_info", _via_context, sector_name) - - def get_markets(self): - # No such function exists in either ContextInfo or the xtdata SDK. - # MiniQMT-only convenience; synthesize from the known A-share markets. - return list(MARKET_CODES) - - def get_market_last_trade_date(self, market): - # No such function exists in either ContextInfo or the xtdata SDK. - # Derive it from get_trading_dates(market, count=1) — last entry. - try: - dates = self.get_trading_dates(market, "", "", 1) or [] - except Exception: - dates = [] - if not dates: - return None - # xtdata returns millisecond timestamps (long list); take the last one. - try: - return dates[-1] - except Exception: - return None - - def call_formula(self, formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param=None): - return self._call_context( - "call_formula", - formula_name, - stock_code, - period, - start_time, - end_time, - count, - dividend_type, - extend_param or {}, - ) - - def subscribe_formula(self, formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param=None): - return self._call_context( - "subscribe_formula", - formula_name, - stock_code, - period, - start_time, - end_time, - count, - dividend_type, - extend_param or {}, - ) - - def unsubscribe_formula(self, request_id): - return self._call_context("unsubscribe_formula", request_id) - - def get_formula_result(self, request_id, start_time="", end_time="", count=-1, timeout_second=-1): - return self._call_context("get_formula_result", request_id, start_time, end_time, count, timeout_second) - - def gen_factor_index(self, data_name, formula_name, vars, sector_list, start_time="", end_time="", period="1d", dividend_type="none"): - return self._call_context( - "gen_factor_index", - data_name, - formula_name, - vars, - sector_list, - start_time, - end_time, - period, - dividend_type, - ) - - # ------------------------------------------------------------------ - # 龙虎榜 / 股东 / 换手率(参考 Rockyzsu/QMT 暴露的 ContextInfo 方法) - # 签名严格按 _PyContextInfo.py 桩核对,避免参数错位。 - # ------------------------------------------------------------------ - - def get_longhubang(self, stock_list=None, start_time="", end_time="", count=-1): - # ContextInfo stub: get_longhubang(stock_list=[], startTime='', endTime='', count=-1) - # 桩里有特殊逻辑:endTime 传 int 时当作 count + endTime=startTime + startTime='0'。 - # 我们直接按 4 参数语义透传,避免触发桩的 int 歧义分支。 - return self._call_context( - "get_longhubang", - list(stock_list or []), - start_time, - end_time, - count, - ) - - def get_top10_share_holder(self, stock_list, data_name, start_time, end_time, report_type="report_time"): - # ContextInfo stub: get_top10_share_holder(stock_list, data_name, start_time, end_time, report_type='report_time') - # data_name 只接受 'holder' 或 'flow_holder';report_type 只接受 'report_time' 或 'announce_time'。 - return self._call_context( - "get_top10_share_holder", - stock_list, - data_name, - start_time, - end_time, - report_type, - ) - - def get_holder_num(self, stock_list=None, start_time="", end_time="", report_type="report_time"): - # ContextInfo stub: get_holder_num(stock_list=[], startTime='', endTime='', report_type='report_time') - # 返回股东户数 DataFrame。 - return self._call_context( - "get_holder_num", - list(stock_list or []), - start_time, - end_time, - report_type, - ) - - def get_turnover_rate(self, stock_code=None, start_time="19720101", end_time="22010101"): - # ContextInfo stub: get_turnover_rate(stock_code=[], start_time='19720101', end_time='22010101') - # 注意:start_time/end_time 必须是 8 位日期串(YYYYMMDD),否则返回空 DataFrame。 - return self._call_context( - "get_turnover_rate", - list(stock_code or []), - start_time, - end_time, - ) - - def get_industry(self, industry_name): - # ContextInfo stub: get_industry(industry_name, real_timetag = -1) - # 注意桩签名有第二个可选参数 real_timetag,默认 -1(最新)。 - return self._call_context("get_industry", industry_name, -1) - - def get_close_price(self, market, stock_code, real_timetag, period=86400000, divid_type=0): - # ContextInfo stub: get_close_price(market, stockCode, realTimetag, period=86400000, dividType=0) - return self._call_context("get_close_price", market, stock_code, real_timetag, period, divid_type) - - # ------------------------------------------------------------------ - # 期权定价(BSM)/ 隐含波动率 - # ------------------------------------------------------------------ - - def bsm_price(self, opt_type, target_price, strike_price, risk_free, sigma, days, dividend=0): - # ContextInfo stub: bsm_price(optType, targetPrice, strikePrice, riskFree, sigma, days, dividend=0) - # opt_type: 'C'(call) / 'P'(put)。target_price 可为 list(批量)。 - return self._call_context( - "bsm_price", - opt_type, - target_price, - strike_price, - risk_free, - sigma, - days, - dividend, - ) - - def bsm_iv(self, opt_type, target_price, strike_price, option_price, risk_free, days, dividend=0): - # ContextInfo stub: bsm_iv(optType, targetPrice, strikePrice, optionPrice, riskFree, days, dividend=0) - return self._call_context( - "bsm_iv", - opt_type, - target_price, - strike_price, - option_price, - risk_free, - days, - dividend, - ) - - def get_option_iv(self, opt_code): - # ContextInfo stub: get_option_iv(opt_code) — 计算单只期权的隐含波动率。 - return self._call_context("get_option_iv", opt_code) - - def get_option_detail_data(self, stockcode): - # ContextInfo stub: get_option_detail_data(stockcode) - return self._call_context("get_option_detail_data", stockcode) - - def get_option_undl_data(self, undl_code_ref=""): - # ContextInfo stub: get_option_undl_data(undl_code_ref='') — 标的下所有期权。 - # 传空串返回全市场期权-标的映射 dict。 - return self._call_context("get_option_undl_data", undl_code_ref) - - def get_option_undl(self, opt_code): - # ContextInfo stub: get_option_undl(opt_code) — 期权的标的代码。 - return self._call_context("get_option_undl", opt_code) - - # ------------------------------------------------------------------ - # 财务扩展 / 因子数据 - # ------------------------------------------------------------------ - - def get_raw_financial_data(self, field_list, stock_list, start_time, end_time, report_type="report_time", data_type="dict"): - # ContextInfo stub: get_raw_financial_data(fieldList, stockList, startDate, endDate, report_type='report_time', data_type='dict') - # 返回原始财务数据(未做字段对齐),data_type 可为 'dict'/'frame'。 - return self._call_context( - "get_raw_financial_data", - field_list, - stock_list, - start_time, - end_time, - report_type, - data_type, - ) - - def get_factor_data(self, field_list, stock_list, start_date, end_date): - # ContextInfo stub: get_factor_data(field_list, stock_list, start_date, end_date) - # 返回因子库数据。 - return self._call_context( - "get_factor_data", - field_list, - stock_list, - start_date, - end_date, - ) - - # ------------------------------------------------------------------ - # 历史 ST / 指数权重 - # ------------------------------------------------------------------ - - def get_his_st_data(self, stock_code): - # ContextInfo stub: get_his_st_data(stockCode) — 历史 ST 状态。 - return self._call_context("get_his_st_data", stock_code) - - def get_his_index_data(self, stock_code): - # ContextInfo stub: get_his_index_data(stockCode) — 历史指数权重。 - return self._call_context("get_his_index_data", stock_code) - - # ------------------------------------------------------------------ - # 期货 / 合约 - # ------------------------------------------------------------------ - - def get_main_contract(self, code_market): - # ContextInfo stub: get_main_contract(codemarket) - return self._call_context("get_main_contract", code_market) - - def get_his_contract_list(self, market): - # ContextInfo stub: get_his_contract_list(market) - return self._call_context("get_his_contract_list", market) - - def get_date_location(self, date): - # ContextInfo stub: get_date_location(date) — 日期在交易日历中的位置。 - return self._call_context("get_date_location", date) - - def get_ETF_list(self, market, stock_code, type_list=None): - # ContextInfo stub: get_ETF_list(market, stockcode, typeList=[]) - return self._call_context("get_ETF_list", market, stock_code, list(type_list or [])) - - # ------------------------------------------------------------------ - # 北向资金 / 港股通 - # ------------------------------------------------------------------ - - def get_north_finance_change(self, period): - # ContextInfo stub: get_north_finance_change(period) — 北向资金流入流出。 - return self._call_context("get_north_finance_change", period) - - def get_hkt_statistics(self, stock_code): - # ContextInfo stub: get_hkt_statistics(stock_code) — 港股通统计。 - return self._call_context("get_hkt_statistics", stock_code) - - def get_hkt_details(self, stock_code): - # ContextInfo stub: get_hkt_details(stock_code) — 港股通明细。 - return self._call_context("get_hkt_details", stock_code) - - # ------------------------------------------------------------------ - # 自定义板块管理(写操作,仅 ContextInfo 支持) - # ------------------------------------------------------------------ - - def create_sector(self, sector_name, stock_list): - # ContextInfo stub: create_sector(sectorname, stocklist) — 创建/更新自定义板块。 - return self._call_context("create_sector", sector_name, list(stock_list or [])) - - # ------------------------------------------------------------------ - # 基础查询辅助 - # ------------------------------------------------------------------ - - def get_stock_name(self, stock): - # ContextInfo stub: get_stock_name(stock) - return self._call_context("get_stock_name", stock) - - def get_stock_type(self, stock): - # ContextInfo stub: get_stock_type(stock) - return self._call_context("get_stock_type", stock) - - def get_last_close(self, stock): - # ContextInfo stub: get_last_close(stock) - return self._call_context("get_last_close", stock) - - def get_last_volume(self, stock): - # ContextInfo stub: get_last_volume(stock) - return self._call_context("get_last_volume", stock) - - def get_open_date(self, stock): - # ContextInfo stub: get_open_date(stock) — 上市日期。 - return self._call_context("get_open_date", stock) - - def get_contract_expire_date(self, stock): - # ContextInfo stub: get_contract_expire_date(stock) — 到期日。 - return self._call_context("get_contract_expire_date", stock) - - def get_contract_multiplier(self, stockcode): - # ContextInfo stub: get_contract_multiplier(stockcode) — 合约乘数。 - return self._call_context("get_contract_multiplier", stockcode) - - def get_float_caps(self, stockcode): - # ContextInfo stub: get_float_caps(stockcode) — 流通市值。 - return self._call_context("get_float_caps", stockcode) - - def get_total_share(self, stockcode): - # ContextInfo stub: get_total_share(stockcode) — 总股本。 - return self._call_context("get_total_share", stockcode) - - def get_turn_over_rate(self, stockcode): - # ContextInfo stub: get_turn_over_rate(stockcode) — 换手率(单值版,区别于上面的 get_turnover_rate 区间版)。 - return self._call_context("get_turn_over_rate", stockcode) - - def get_weight_in_index(self, mtkindexcode, stockcode): - # ContextInfo stub: get_weight_in_index(mtkindexcode, stockcode) — 指数中权重。 - return self._call_context("get_weight_in_index", mtkindexcode, stockcode) - - def get_svol(self, stock): - # ContextInfo stub: get_svol(stock) - return self._call_context("get_svol", stock) - - def get_bvol(self, stock): - # ContextInfo stub: get_bvol(stock) - return self._call_context("get_bvol", stock) - - def get_risk_free_rate(self, index=-1): - # ContextInfo stub: get_risk_free_rate(index) — 无风险利率。 - return self._call_context("get_risk_free_rate", index) - - # ------------------------------------------------------------------ - # L2 行情(需 L2 权限) - # ------------------------------------------------------------------ - - def get_l2_quote(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - # xtdata SDK: get_l2_quote(field_list=[], stock_code='', start_time='', end_time='', count=-1) - # ContextInfo 无此方法;走原生 xtdata SDK,连不上则 NotImplementedError。 - return self._native_or_context( - "get_l2_quote", - lambda: self._raise_unavailable("get_l2_quote"), - list(field_list or []), stock_code, start_time, end_time, count, - ) - - def get_l2_order(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - # xtdata SDK: get_l2_order(...) — L2 逐笔委托。 - return self._native_or_context( - "get_l2_order", - lambda: self._raise_unavailable("get_l2_order"), - list(field_list or []), stock_code, start_time, end_time, count, - ) - - def get_l2_transaction(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - # xtdata SDK: get_l2_transaction(...) — L2 逐笔成交。 - return self._native_or_context( - "get_l2_transaction", - lambda: self._raise_unavailable("get_l2_transaction"), - list(field_list or []), stock_code, start_time, end_time, count, - ) - - def subscribe_l2thousand(self, stock_code, gear_num=0, callback=None): - # xtdata SDK: subscribe_l2thousand(stock_code, gear_num=0, callback=None) — 千档盘口订阅。 - # callback 在 RPC 模型下无意义(无回调通道),忽略。 - module = self._native() - if module is not None and hasattr(module, "subscribe_l2thousand"): - try: - return module.subscribe_l2thousand(stock_code, gear_num, callback) - except Exception: - pass - return self._raise_unavailable("subscribe_l2thousand") - - # ------------------------------------------------------------------ - # 指数权重 / 交易日历 / 交易时段 / 可转债 - # ------------------------------------------------------------------ - - def get_index_weight(self, index_code): - # xtdata SDK: get_index_weight(index_code) — 指数成分权重。 - # ContextInfo 有 get_weight_in_index(indexcode, stockcode) 但语义不同(单股权重)。 - return self._native_or_context( - "get_index_weight", - lambda: self._raise_unavailable("get_index_weight"), - index_code, - ) - - def get_trading_calendar(self, market, start_time="", end_time="", tradetimes=False): - # xtdata SDK: get_trading_calendar(market, start_time='', end_time='', tradetimes=False) - # ContextInfo 无此方法。SDK 不可用时从 get_trading_dates 派生(不含 tradetimes 时段)。 - def _fallback(): - try: - dates = self.get_trading_dates(market, start_time, end_time, -1) or [] - return [str(d) for d in dates] - except Exception: - return self._raise_unavailable("get_trading_calendar") - return self._native_or_context( - "get_trading_calendar", _fallback, market, start_time, end_time, tradetimes - ) - - def get_trade_times(self, stockcode): - # xtdata SDK: get_trade_times(stockcode) — 日内交易时段。 - # 传市场('SH')或代码('600000.SH')。返回 [[开始,结束,类型], ...]。 - return self._native_or_context( - "get_trade_times", - lambda: self._raise_unavailable("get_trade_times"), - stockcode, - ) - - def get_cb_info(self, stockcode): - # xtdata SDK: get_cb_info(stockcode) — 可转债信息。 - return self._native_or_context( - "get_cb_info", - lambda: self._raise_unavailable("get_cb_info"), - stockcode, - ) - - def is_stock_type(self, stock, tag): - # xtdata SDK: is_stock_type(stock, tag) — 品种判断(tag 如 'stock'/'fund'/'bond')。 - # ContextInfo 有 is_stock/is_fund/is_future 但签名不同,这里走 SDK。 - return self._native_or_context( - "is_stock_type", - lambda: self._raise_unavailable("is_stock_type"), - stock, tag, - ) - - # ------------------------------------------------------------------ - # 板块增删(自定义板块管理) - # ------------------------------------------------------------------ - - def add_sector(self, sector_name, stock_list): - # xtdata SDK: add_sector(sector_name, stock_list) — 向自定义板块追加股票。 - # ContextInfo 用 create_sector(覆盖式),SDK 用 add_sector(追加式)。 - module = self._native() - if module is not None and hasattr(module, "add_sector"): - try: - return module.add_sector(sector_name, list(stock_list or [])) - except Exception: - pass - # ContextInfo fallback:create_sector 是覆盖式,语义略不同但可用。 - return self._call_context("create_sector", sector_name, list(stock_list or [])) - - def remove_sector(self, sector_name): - # xtdata SDK: remove_sector(sector_name) — 删除自定义板块。 - module = self._native() - if module is not None and hasattr(module, "remove_sector"): - try: - return module.remove_sector(sector_name) - except Exception: - pass - return self._raise_unavailable("remove_sector") - - # ------------------------------------------------------------------ - # 数据下载扩展 - # ------------------------------------------------------------------ - - def download_cb_data(self): - # xtdata SDK: download_cb_data() — 下载可转债数据。 - module = self._native() - if module is not None and hasattr(module, "download_cb_data"): - try: - return module.download_cb_data() - except Exception: - pass - return self._raise_unavailable("download_cb_data") - - def download_history_contracts(self): - # xtdata SDK: download_history_contracts() — 下载过期合约数据。 - module = self._native() - if module is not None and hasattr(module, "download_history_contracts"): - try: - return module.download_history_contracts() - except Exception: - pass - return self._raise_unavailable("download_history_contracts") - - def download_index_weight(self): - # xtdata SDK: download_index_weight() — 下载指数权重数据。 - module = self._native() - if module is not None and hasattr(module, "download_index_weight"): - try: - return module.download_index_weight() - except Exception: - pass - return self._raise_unavailable("download_index_weight") - - def download_sector_data(self): - # xtdata SDK: download_sector_data() — 下载行业板块数据。 - module = self._native() - if module is not None and hasattr(module, "download_sector_data"): - try: - return module.download_sector_data() - except Exception: - pass - return self._raise_unavailable("download_sector_data") - - # ------------------------------------------------------------------ - # 时间戳转换(纯计算,无需 QMT,服务端本地实现) - # ------------------------------------------------------------------ - - @staticmethod - def datetime_to_timetag(datetime_str, format="%Y%m%d%H%M%S"): - # xtdata SDK: datetime_to_timetag(datetime, format="%Y%m%d%H%M%S") - # 把日期时间字符串转成毫秒时间戳。纯本地计算。 - import datetime as _dt - try: - dt = _dt.datetime.strptime(str(datetime_str), format) - return int(dt.timestamp() * 1000) - except Exception: - return 0 - - @staticmethod - def timetag_to_datetime(timetag, format): - # xtdata SDK: timetag_to_datetime(timetag, format) — 毫秒时间戳转字符串。 - import datetime as _dt - try: - dt = _dt.datetime.fromtimestamp(int(timetag) / 1000.0) - return dt.strftime(format) - except Exception: - return "" - - @staticmethod - def _raise_unavailable(method_name): - raise NotImplementedError( - "%s is unavailable: needs native xtdata SDK quote service " - "(not reachable in Big QMT full terminal)" % method_name - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_bigqmt.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_bigqmt.py deleted file mode 100644 index 08b3216..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_bigqmt.py +++ /dev/null @@ -1,254 +0,0 @@ -"""Big QMT order gateway. - -The passorder signature follows src/api/qmt_jq_trade. -""" - -import hashlib - -from ..code_utils import normalize_stock_code -from ..models import CancelResult, OrderSnapshot, OrderSubmitResult, SignalAction, TradeSnapshot -from .position_bigqmt import _attr, _full_code - - -PRICE_TYPE_ALIASES = { - "LIMIT": 11, - "FIX_PRICE": 11, - "LATEST_PRICE": 5, - "MARKET_PEER_PRICE_FIRST": 44, - "MARKET_SH_CONVERT_5_LIMIT": 43, - "MARKET_SZ_CONVERT_5_CANCEL": 47, -} - - -def _action_from_offset_flag(offset_flag): - return SignalAction.BUY.value if int(offset_flag or 0) == 48 else SignalAction.SELL.value - - -# 报单时间。大 QMT 的 ORDER 行把日期和时间分成两个字段, MiniQMT 的 -# XtOrder.order_time 是 Unix 秒, 所以要拼接后转换。成交那条路径早就读了 -# m_strTradeTime, 委托这边一直漏掉 (issue #48)。 -_ORDER_DATE_FIELDS = ("m_strInsertDate", "m_strOrderDate", "insert_date", "order_date") -_ORDER_TIME_FIELDS = ("m_strInsertTime", "m_strOrderTime", "insert_time", "order_time") - -# 取不到时打印该行实际有哪些 m_*, 每进程一次。字段名无法离线核实, -# 猜一个然后静默返回 0 正是订单方向那个 bug 的成因。 -_missing_order_time_reported = [] - - -def _report_missing_order_time(row): - if _missing_order_time_reported: - return - _missing_order_time_reported.append(True) - try: - available = sorted(n for n in dir(row) if n.startswith("m_")) - except Exception: - available = [] - print( - "[bigqmt_order] order_time not found (tried %s / %s); ORDER row exposes: %s" - % (", ".join(_ORDER_DATE_FIELDS), ", ".join(_ORDER_TIME_FIELDS), - ", ".join(available) or "") - ) - - -def _order_time_seconds(row): - """把 ORDER 行的报单日期+时间转成 Unix 秒, 拿不到返回 0。 - - 容忍几种实际会遇到的写法: 日期 '20260819' 或 '2026-08-19', - 时间 '093015'、'09:30:15' 或 '09:30:15.123'。已经是数字时间戳的直接用 - (毫秒会被归一到秒)。 - """ - raw_time = _attr(row, _ORDER_TIME_FIELDS) - raw_date = _attr(row, _ORDER_DATE_FIELDS) - if raw_time is None and raw_date is None: - _report_missing_order_time(row) - return 0 - - # 已是数字: 当成时间戳 (>1e11 视为毫秒)。 - if isinstance(raw_time, (int, float)) and not isinstance(raw_time, bool): - value = float(raw_time) - if value > 1e11: - value /= 1000.0 - if value > 1e8: # 像时间戳而不是 093015 这种时分秒 - return int(value) - - date_text = "".join(ch for ch in str(raw_date or "") if ch.isdigit()) - time_text = "".join(ch for ch in str(raw_time or "") if ch.isdigit()) - if not date_text or len(date_text) < 8: - return 0 - time_text = (time_text + "000000")[:6] # 补齐到 HHMMSS, 丢掉毫秒 - try: - import time as _time - - parsed = _time.strptime(date_text[:8] + time_text, "%Y%m%d%H%M%S") - return int(_time.mktime(parsed)) - except Exception: - return 0 - - -def _price_type_value(value, default): - if value is None or value == "": - return int(default) - try: - return int(value) - except (TypeError, ValueError): - text = str(value).strip().upper() - return int(PRICE_TYPE_ALIASES.get(text, default)) - - -class BigQmtOrderGateway: - def __init__( - self, - context_info, - account_id="", - passorder_func=None, - cancel_func=None, - get_trade_detail_data_func=None, - account_type="STOCK", - combo_type=1101, - price_type=11, - quick_trade=2, - ): - self.context_info = context_info - self.account_id = account_id - self.passorder = passorder_func - self.cancel_func = cancel_func - self.get_trade_detail_data = get_trade_detail_data_func - self.account_type = account_type - self.combo_type = combo_type - self.price_type = price_type - self.quick_trade = quick_trade - - def _require_passorder(self): - if self.passorder is None: - raise RuntimeError("passorder is not available in Big QMT runtime") - return self.passorder - - def _require_cancel(self): - if self.cancel_func is None: - raise RuntimeError("cancel is not available in Big QMT runtime") - return self.cancel_func - - def _require_query_func(self): - if self.get_trade_detail_data is None: - raise RuntimeError("get_trade_detail_data is not available in Big QMT runtime") - return self.get_trade_detail_data - - @staticmethod - def build_user_order_id(signal_id): - text = str(signal_id or "") - digest = hashlib.sha1(text.encode("utf-8")).hexdigest()[:10] - return "bq:%s:%s" % (digest, text[:30]) - - def submit(self, request): - passorder = self._require_passorder() - action = str(request.action).upper() - if action == SignalAction.BUY.value: - op_type = 23 - elif action == SignalAction.SELL.value: - op_type = 24 - else: - raise ValueError("unsupported order action: %s" % request.action) - - user_order_id = str(request.remark or "").strip() or self.build_user_order_id(request.signal_id) - account_id = request.account_id or self.account_id - passorder( - op_type, - self.combo_type, - account_id, - normalize_stock_code(request.stock_code), - _price_type_value(request.price_type, self.price_type), - float(request.price), - int(request.volume), - request.strategy_name, - self.quick_trade, - user_order_id, - self.context_info, - ) - return OrderSubmitResult( - status="SUBMITTED", - user_order_id=user_order_id, - order_sys_id=None, - message="passorder submitted", - ) - - def cancel(self, order_ref): - cancel_func = self._require_cancel() - ok = cancel_func(order_ref.order_sys_id, self.account_id, self.account_type, self.context_info) - return CancelResult(success=bool(ok), message="" if ok else "cancel returned false") - - def query_orders(self, account_id, strategy_name): - try: - return self.query_orders_strict(account_id, strategy_name) - except Exception: - return [] - - def query_orders_strict(self, account_id, strategy_name): - query = self._require_query_func() - rows = query(account_id, self.account_type, "ORDER", strategy_name) or [] - result = [] - for row in rows: - result.append( - OrderSnapshot( - order_sys_id=str(_attr(row, ("m_strOrderSysID", "order_sys_id"), "") or ""), - user_order_id=str(_attr(row, ("m_strRemark", "user_order_id", "remark"), "") or ""), - stock_code=_full_code( - _attr(row, ("m_strInstrumentID", "instrument_id", "stock_code")), - _attr(row, ("m_strExchangeID", "exchange_id", "market")), - ), - action=_action_from_offset_flag(_attr(row, ("m_nOffsetFlag", "offset_flag"), 0)), - volume=int(_attr(row, ("m_nVolumeTotalOriginal", "volume"), 0) or 0), - traded_volume=int(_attr(row, ("m_nVolumeTraded", "traded_volume"), 0) or 0), - status=str(_attr(row, ("m_nOrderStatus", "status"), "") or ""), - price=float(_attr(row, ("m_dLimitPrice", "m_dPrice", "price"), 0.0) or 0.0), - strategy_name=str(_attr(row, ("m_strStrategyName", "strategy_name"), "") or ""), - remark=str(_attr(row, ("m_strRemark", "remark"), "") or ""), - order_time=_order_time_seconds(row), - ) - ) - return result - - def query_trades(self, account_id, strategy_name): - try: - return self.query_trades_strict(account_id, strategy_name) - except Exception: - return [] - - def query_trades_strict(self, account_id, strategy_name): - query = self._require_query_func() - rows = [] - last_error = None - for detail_type in ("DEAL", "TRADE"): - try: - if str(strategy_name or "").strip(): - rows = query(account_id, self.account_type, detail_type, strategy_name) or [] - else: - rows = query(account_id, self.account_type, detail_type) or [] - if rows: - break - except Exception as exc: - last_error = exc - if not rows and last_error is not None: - raise last_error - result = [] - for row in rows: - result.append( - TradeSnapshot( - trade_id=str(_attr(row, ("m_strTradeID", "trade_id"), "") or ""), - order_sys_id=str(_attr(row, ("m_strOrderSysID", "order_sys_id"), "") or ""), - stock_code=_full_code( - _attr(row, ("m_strInstrumentID", "instrument_id", "stock_code")), - _attr(row, ("m_strExchangeID", "exchange_id", "market")), - ), - action=_action_from_offset_flag(_attr(row, ("m_nOffsetFlag", "offset_flag"), 0)), - volume=int(_attr(row, ("m_nVolume", "volume"), 0) or 0), - price=float(_attr(row, ("m_dPrice", "m_dTradePrice", "price"), 0.0) or 0.0), - traded_at=str(_attr(row, ("m_strTradeTime", "trade_time", "traded_at"), "") or ""), - user_order_id=str(_attr(row, ("m_strRemark", "user_order_id", "remark"), "") or ""), - ) - ) - return result - - def query_submission_identities_strict(self, account_id, strategy_name): - orders = self.query_orders_strict(account_id, strategy_name) - trades = self.query_trades_strict(account_id, strategy_name) - return orders, trades diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_dryrun.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_dryrun.py deleted file mode 100644 index 6623bb3..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/order_dryrun.py +++ /dev/null @@ -1,30 +0,0 @@ -"""不发真实委托的下单 gateway,用于联调和回放。""" - -import hashlib - -from ..models import OrderSubmitResult - - -class DryRunOrderGateway: - def __init__(self): - self.submitted = [] - self.cancelled = [] - - def submit(self, request): - self.submitted.append(request) - digest = hashlib.sha1(request.signal_id.encode("utf-8")).hexdigest()[:10] - return OrderSubmitResult( - status="DRY_RUN", - user_order_id=f"dryrun:bq:{digest}:{request.signal_id}", - order_sys_id=None, - ) - - def cancel(self, order_ref): - self.cancelled.append(order_ref) - return None - - def query_orders(self, account_id, strategy_name): - return [] - - def query_trades(self, account_id, strategy_name): - return [] diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_bigqmt.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_bigqmt.py deleted file mode 100644 index 11e4120..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_bigqmt.py +++ /dev/null @@ -1,146 +0,0 @@ -"""Big QMT position and asset adapters.""" - -from ..code_utils import normalize_stock_code -from ..models import AssetSnapshot, PositionSnapshot - - -def _attr(obj, names, default=None): - for name in names: - if hasattr(obj, name): - value = getattr(obj, name) - if value is not None: - return value - return default - - -def _float_or_none(value): - if value is None: - return None - try: - return float(value) - except (TypeError, ValueError): - return None - - -# Candidate ThinkTrader field names on the ACCOUNT row of get_trade_detail_data. -# The MiniQMT SDK only documents the normalized name (XtAsset.frozen_cash); the -# big QMT ACCOUNT struct is a different surface and brokers vary, so probe the -# plausible spellings the way cash/total_asset already do. -_FROZEN_CASH_FIELDS = ( - "m_dFrozenCash", - "m_dFrozen", - "m_dFrozenBalance", - "m_dFrozenMargin", - "frozen_cash", - "frozen", -) -_MARKET_VALUE_FIELDS = ( - "m_dInstrumentValue", - "m_dStockValue", - "m_dMarketValue", - "market_value", -) - -# Printed once per process when the frozen field is not found, listing what the -# row actually carries. Guessing a field name and shipping it unverified is how -# the order-direction bug happened; this makes the real name self-reporting. -_missing_field_reported = set() - - -def _report_missing_field(label, row, candidates): - if label in _missing_field_reported: - return - _missing_field_reported.add(label) - try: - available = sorted(name for name in dir(row) if name.startswith("m_")) - except Exception: - available = [] - print( - "[bigqmt_asset] %s not found (tried %s); ACCOUNT row exposes: %s" - % (label, ", ".join(candidates), ", ".join(available) or "") - ) - - -def _full_code(instrument_id, exchange_id): - code = str(instrument_id or "").strip().upper() - market = str(exchange_id or "").strip().upper() - if "." in code: - return normalize_stock_code(code) - if market in ("SH", "SZ"): - return normalize_stock_code("%s.%s" % (code, market)) - return normalize_stock_code(code) - - -class BigQmtPositionProvider: - def __init__(self, get_trade_detail_data_func, account_type="STOCK"): - self.get_trade_detail_data = get_trade_detail_data_func - self.account_type = account_type - - def _require_query_func(self): - if self.get_trade_detail_data is None: - raise RuntimeError("get_trade_detail_data is not available in Big QMT runtime") - return self.get_trade_detail_data - - def get_positions(self, account_id): - query = self._require_query_func() - # QMT's get_trade_detail_data can raise on POSITION queries in some - # states (e.g. context not bound). Degrade to empty like get_asset does. - try: - rows = query(account_id, self.account_type, "POSITION") or [] - except Exception: - return {} - positions = {} - for row in rows: - code = _full_code( - _attr(row, ("m_strInstrumentID", "instrument_id", "stock_code")), - _attr(row, ("m_strExchangeID", "exchange_id", "market")), - ) - positions[code] = PositionSnapshot( - stock_code=code, - volume=int(_attr(row, ("m_nVolume", "volume"), 0) or 0), - available=int(_attr(row, ("m_nCanUseVolume", "available", "can_use_volume"), 0) or 0), - cost=float(_attr(row, ("m_dOpenPrice", "m_dCostPrice", "cost"), 0.0) or 0.0), - stock_name=str(_attr(row, ("m_strInstrumentName", "stock_name"), "") or ""), - market_value=_float_or_none(_attr(row, ("m_dMarketValue", "m_dInstrumentValue", "market_value"))), - price=_float_or_none(_attr(row, ("m_dLastPrice", "m_dSettlementPrice", "price", "last_price"))), - open_price=_float_or_none(_attr(row, ("m_dOpenPrice", "m_dCostPrice", "open_price", "cost"))), - frozen_volume=int(_attr(row, ("m_nFrozenVolume", "frozen_volume"), 0) or 0), - on_road_volume=int(_attr(row, ("m_nOnRoadVolume", "on_road_volume"), 0) or 0), - yesterday_volume=int(_attr(row, ("m_nYesterdayVolume", "yesterday_volume"), 0) or 0), - direction=int(_attr(row, ("m_nDirection", "direction"), 48) or 48), - ) - return positions - - def get_asset(self, account_id): - query = self._require_query_func() - rows = [] - for detail_type in ("ACCOUNT", "ASSET"): - try: - rows = query(account_id, self.account_type, detail_type) or [] - if rows: - break - except Exception: - rows = [] - if not rows: - return AssetSnapshot(account_id=account_id, cash=None, total_asset=None) - - row = rows[0] - cash = _attr(row, ("m_dAvailable", "m_dAvailableCash", "available_cash", "cash")) - total_asset = _attr(row, ("m_dBalance", "m_dAsset", "total_asset", "asset")) - frozen_cash = _attr(row, _FROZEN_CASH_FIELDS) - market_value = _attr(row, _MARKET_VALUE_FIELDS) - if frozen_cash is None: - _report_missing_field("frozen_cash", row, _FROZEN_CASH_FIELDS) - if market_value is None and cash is not None and total_asset is not None: - # Derive only as a last resort. Without frozen_cash this overstates - # market value by the frozen amount, so subtract it when known. - market_value = float(total_asset) - float(cash) - if frozen_cash is not None: - market_value -= float(frozen_cash) - return AssetSnapshot( - account_id=account_id, - cash=float(cash) if cash is not None else None, - total_asset=float(total_asset) if total_asset is not None else None, - frozen_cash=float(frozen_cash) if frozen_cash is not None else None, - market_value=float(market_value) if market_value is not None else None, - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_sync_redis.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_sync_redis.py deleted file mode 100644 index 8421673..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/position_sync_redis.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Publish Big QMT position snapshots to Redis.""" - -import datetime as _dt -import json - - -class RedisPositionSyncSink: - def __init__( - self, - redis_client, - key_template="bigqmt:positions:{account_id}", - event_stream_template="bigqmt:position_events:{account_id}", - ttl_seconds=120, - publish_events=True, - ): - self.redis = redis_client - self.key_template = key_template - self.event_stream_template = event_stream_template - self.ttl_seconds = int(ttl_seconds) - self.publish_events = bool(publish_events) - - @staticmethod - def _time_text(value): - if isinstance(value, _dt.datetime): - return value.strftime("%Y-%m-%d %H:%M:%S") - return str(value) - - def _snapshot_to_dict(self, snapshot): - return { - "account_id": snapshot.account_id, - "reason": snapshot.reason, - "updated_at": self._time_text(snapshot.updated_at), - "asset": { - "cash": snapshot.asset.cash, - "total_asset": snapshot.asset.total_asset, - # Carried so the client's cached-asset fallback exposes the same - # fields as a live query_stock_asset. - "frozen_cash": getattr(snapshot.asset, "frozen_cash", None), - "market_value": getattr(snapshot.asset, "market_value", None), - }, - "positions": { - code: { - "stock_code": position.stock_code, - "volume": position.volume, - "available": position.available, - "cost": position.cost, - "stock_name": position.stock_name, - } - for code, position in snapshot.positions.items() - }, - } - - def publish(self, snapshot): - payload = json.dumps(self._snapshot_to_dict(snapshot), ensure_ascii=False) - key = self.key_template.format(account_id=snapshot.account_id) - if self.ttl_seconds > 0: - self.redis.setex(key, self.ttl_seconds, payload) - else: - self.redis.set(key, payload) - if self.publish_events: - stream_key = self.event_stream_template.format(account_id=snapshot.account_id) - # Cap the stream to prevent unbounded memory growth. Order/trade - # events already use maxlen=2000; position events were missing it, - # causing 4.2GB+ streams in production (issue #21). - self.redis.xadd(stream_key, {"payload": payload}, maxlen=2000, approximate=True) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/redis_common.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/redis_common.py deleted file mode 100644 index d049720..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/redis_common.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Redis client helpers for Big QMT signal trader.""" - -import os - - -def _float_or_none(value, default=None): - if value is None: - return default - if value == "": - return default - text = str(value).strip() - if text.lower() in ("none", "null"): - return None - return float(value) - - -def build_redis_client(config=None): - config = config or {} - try: - import redis - except ImportError as exc: # pragma: no cover - raise RuntimeError("redis package is required when Redis adapters are enabled") from exc - - url = config.get("url") or os.environ.get("BIGQMT_REDIS_URL") - if url: - return redis.Redis.from_url( - url, - socket_connect_timeout=_float_or_none(config.get("socket_connect_timeout", 1.5), 1.5), - socket_timeout=_float_or_none(config.get("socket_timeout", 1.5), 1.5), - ) - - host = config.get("host") or os.environ.get("BIGQMT_REDIS_HOST") or "127.0.0.1" - port = int(config.get("port") or os.environ.get("BIGQMT_REDIS_PORT") or 6379) - db = int(config.get("db") or os.environ.get("BIGQMT_REDIS_DB") or 5) - username = config.get("username") or os.environ.get("BIGQMT_REDIS_USERNAME") or None - password = config.get("password") or os.environ.get("BIGQMT_REDIS_PASSWORD") or None - return redis.Redis( - host=host, - port=port, - db=db, - username=username, - password=password, - socket_connect_timeout=_float_or_none(config.get("socket_connect_timeout", 1.5), 1.5), - socket_timeout=_float_or_none(config.get("socket_timeout", 1.5), 1.5), - health_check_interval=int(config.get("health_check_interval", 30)), - ) - - -def decode_text(value): - if isinstance(value, bytes): - return value.decode("utf-8") - return str(value) - - -def redis_mapping_to_text(mapping): - return {decode_text(key): decode_text(value) for key, value in (mapping or {}).items()} diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/signal_redis.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/signal_redis.py deleted file mode 100644 index d4d7324..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/signal_redis.py +++ /dev/null @@ -1,115 +0,0 @@ -"""Redis Stream signal source. - -Redis is only a transport here. It must never call passorder or inspect QMT. -""" - -import datetime as _dt -import json - -from ..models import TradeSignal -from .redis_common import decode_text, redis_mapping_to_text - - -DEFAULT_STREAM_KEY_TEMPLATE = "bigqmt:signals:{account_id}" -DEFAULT_GROUP = "bigqmt-signal-trader" - - -def _json_default(value): - if isinstance(value, (_dt.datetime, _dt.date)): - return value.strftime("%Y-%m-%d %H:%M:%S") - return str(value) - - -def _coerce_scalar(value): - text = decode_text(value).strip() - lowered = text.lower() - if lowered == "true": - return True - if lowered == "false": - return False - if lowered in ("none", "null"): - return None - return text - - -def parse_stream_payload(fields): - text_fields = redis_mapping_to_text(fields) - payload_text = text_fields.get("payload") or text_fields.get("data") - if payload_text: - payload = json.loads(payload_text) - if not isinstance(payload, dict): - raise ValueError("Redis stream payload must be a JSON object") - return payload - return {decode_text(key): _coerce_scalar(value) for key, value in fields.items()} - - -class RedisStreamSignalSource: - def __init__( - self, - redis_client, - stream_key_template=DEFAULT_STREAM_KEY_TEMPLATE, - group_name=DEFAULT_GROUP, - consumer_name="bigqmt-consumer", - block_ms=0, - ): - self.redis = redis_client - self.stream_key_template = stream_key_template - self.group_name = group_name - self.consumer_name = consumer_name - self.block_ms = int(block_ms or 0) - self._stream_ids_by_signal_id = {} - self._created_groups = set() - - def _stream_key(self, account_id): - return self.stream_key_template.format(account_id=account_id) - - def _ensure_group(self, stream_key): - if stream_key in self._created_groups: - return - try: - self.redis.xgroup_create(stream_key, self.group_name, id="0-0", mkstream=True) - except Exception as exc: - if "BUSYGROUP" not in str(exc): - raise - self._created_groups.add(stream_key) - - def fetch(self, account_id, limit): - stream_key = self._stream_key(account_id) - self._ensure_group(stream_key) - kwargs = { - "groupname": self.group_name, - "consumername": self.consumer_name, - "streams": {stream_key: ">"}, - "count": int(limit), - } - if self.block_ms > 0: - kwargs["block"] = self.block_ms - rows = self.redis.xreadgroup(**kwargs) or [] - signals = [] - for _, entries in rows: - for stream_id, fields in entries: - payload = parse_stream_payload(fields) - signal = TradeSignal.from_dict(payload) - self._stream_ids_by_signal_id[signal.signal_id] = (stream_key, stream_id) - signals.append(signal) - return signals - - def ack(self, signal): - ref = self._stream_ids_by_signal_id.pop(signal.signal_id, None) - if not ref: - return None - stream_key, stream_id = ref - return self.redis.xack(stream_key, self.group_name, stream_id) - - -def push_trade_signal(redis_client, payload, account_id=None, stream_key_template=DEFAULT_STREAM_KEY_TEMPLATE): - if isinstance(payload, TradeSignal): - account_id = account_id or payload.account_id - raw_payload = dict(payload.raw_payload) - else: - raw_payload = dict(payload) - account_id = account_id or raw_payload.get("account_id") - if not account_id: - raise ValueError("account_id is required") - stream_key = stream_key_template.format(account_id=account_id) - return redis_client.xadd(stream_key, {"payload": json.dumps(raw_payload, ensure_ascii=False, default=_json_default)}) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/state_redis.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/state_redis.py deleted file mode 100644 index db8489b..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/adapters/state_redis.py +++ /dev/null @@ -1,90 +0,0 @@ -"""Redis signal state store.""" - -import datetime as _dt - - -class RedisStateStore: - def __init__( - self, - redis_client, - account_id="default", - claim_key_template="bigqmt:signal_claim:{account_id}:{signal_id}", - status_key_template="bigqmt:signal_status:{account_id}:{signal_id}", - claim_ttl_seconds=3600, - status_ttl_seconds=86400, - ): - self.redis = redis_client - self.account_id = account_id - self.claim_key_template = claim_key_template - self.status_key_template = status_key_template - self.claim_ttl_seconds = int(claim_ttl_seconds) - self.status_ttl_seconds = int(status_ttl_seconds) - self._accounts_by_signal_id = {} - - def _account_for(self, signal_id): - return self._accounts_by_signal_id.get(signal_id) or self.account_id - - def _claim_key(self, account_id, signal_id): - return self.claim_key_template.format(account_id=account_id, signal_id=signal_id) - - def _status_key(self, account_id, signal_id): - return self.status_key_template.format(account_id=account_id, signal_id=signal_id) - - @staticmethod - def _now_text(): - return _dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - - def _write_status(self, account_id, signal_id, mapping): - key = self._status_key(account_id, signal_id) - fields = { - "signal_id": signal_id, - "account_id": account_id, - "updated_at": self._now_text(), - } - fields.update({k: "" if v is None else str(v) for k, v in mapping.items()}) - self.redis.hset(key, mapping=fields) - if self.status_ttl_seconds > 0: - self.redis.expire(key, self.status_ttl_seconds) - - def claim(self, signal, consumer_id): - account_id = signal.account_id or self.account_id - self._accounts_by_signal_id[signal.signal_id] = account_id - key = self._claim_key(account_id, signal.signal_id) - ok = self.redis.set(key, consumer_id, nx=True, ex=self.claim_ttl_seconds) - if ok: - self._write_status( - account_id, - signal.signal_id, - { - "status": "CLAIMED", - "consumer_id": consumer_id, - "stock_code": signal.stock_code, - "action": signal.action.value, - "message": "", - }, - ) - return bool(ok) - - def mark_submitted(self, signal_id, result): - account_id = self._account_for(signal_id) - self._write_status( - account_id, - signal_id, - { - "status": result.status, - "user_order_id": result.user_order_id, - "order_sys_id": result.order_sys_id, - "message": result.message, - }, - ) - - def mark_finished(self, signal_id, status, message=""): - account_id = self._account_for(signal_id) - self._write_status( - account_id, - signal_id, - { - "status": status, - "message": message, - }, - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/app.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/app.py deleted file mode 100644 index 893750a..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/app.py +++ /dev/null @@ -1,98 +0,0 @@ -"""信号交易应用编排层。""" - -import datetime as _dt - -from .models import AccountSnapshot, OrderRequest, SignalAction -from .price_engine import build_order_price -from .risk_guard import validate_signal - - -class SignalTradingApp: - def __init__( - self, - account_id, - signal_source, - market_data, - position_provider, - order_gateway, - position_sync_sink, - state_store, - consumer_id="bigqmt-signal-trader", - fetch_limit=20, - ): - self.account_id = account_id - self.signal_source = signal_source - self.market_data = market_data - self.position_provider = position_provider - self.order_gateway = order_gateway - self.position_sync_sink = position_sync_sink - self.state_store = state_store - self.consumer_id = consumer_id - self.fetch_limit = int(fetch_limit) - - def tick(self, now=None): - now = now or _dt.datetime.now() - signals = self.signal_source.fetch(self.account_id, self.fetch_limit) - positions = self.position_provider.get_positions(self.account_id) - - for signal in signals: - if not self.state_store.claim(signal, self.consumer_id): - continue - try: - self._handle_signal(signal, now, positions) - except Exception as exc: - self.state_store.mark_finished(signal.signal_id, "FAILED", str(exc)) - self.signal_source.ack(signal) - - self.sync_positions("tick", now=now) - - def _handle_signal(self, signal, now, positions): - decision = validate_signal(signal, now, positions) - if not decision.allowed: - self.state_store.mark_finished(signal.signal_id, "SKIPPED", decision.reason) - self.signal_source.ack(signal) - return - - price = build_order_price( - self.market_data, - decision.stock_code, - signal.action.value, - price_type=signal.price_type, - fixed_price=signal.price, - ) - request = OrderRequest( - signal_id=signal.signal_id, - account_id=signal.account_id, - action=signal.action.value, - stock_code=decision.stock_code, - volume=decision.volume, - price=price, - price_type="LIMIT", - strategy_name=signal.strategy_name, - remark=signal.remark, - ) - result = self.order_gateway.submit(request) - self.state_store.mark_submitted(signal.signal_id, result) - self.signal_source.ack(signal) - - def on_init(self, runtime): - return None - - def on_order_event(self, event): - return None - - def on_trade_event(self, event): - self.sync_positions("trade_event") - - def sync_positions(self, reason, now=None): - now = now or _dt.datetime.now() - asset = self.position_provider.get_asset(self.account_id) - positions = self.position_provider.get_positions(self.account_id) - snapshot = AccountSnapshot( - account_id=self.account_id, - asset=asset, - positions=positions, - reason=reason, - updated_at=now, - ) - self.position_sync_sink.publish(snapshot) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/code_utils.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/code_utils.py deleted file mode 100644 index 8ddbad0..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/code_utils.py +++ /dev/null @@ -1,48 +0,0 @@ -"""证券代码标准化和委托数量处理。""" - -import re - - -_DIGIT_CODE_RE = re.compile(r"^\d{6}$") - - -def normalize_stock_code(code): - text = str(code or "").strip().upper() - if not text: - return "" - if text.startswith("SH") and _DIGIT_CODE_RE.match(text[2:]): - return f"{text[2:]}.SH" - if text.startswith("SZ") and _DIGIT_CODE_RE.match(text[2:]): - return f"{text[2:]}.SZ" - if text.endswith(".SH") or text.endswith(".SZ"): - prefix = text[:6] - if _DIGIT_CODE_RE.match(prefix): - return text - if _DIGIT_CODE_RE.match(text): - market = "SH" if text.startswith(("5", "6")) else "SZ" - return f"{text}.{market}" - raise ValueError(f"invalid stock code: {code}") - - -def min_lot(stock_code): - normalized = normalize_stock_code(stock_code) - pure = normalized.split(".")[0] - return 200 if pure.startswith("688") else 100 - - -def round_buy_volume(stock_code, amount): - lot = min_lot(stock_code) - value = int(amount or 0) - if value <= 0: - return 0 - return (value // lot) * lot - - -def round_sell_volume(stock_code, amount, sell_all=False): - value = int(amount or 0) - if value <= 0: - return 0 - if sell_all: - return value - lot = min_lot(stock_code) - return (value // lot) * lot diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/contracts.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/contracts.py deleted file mode 100644 index 332c0f9..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/contracts.py +++ /dev/null @@ -1,81 +0,0 @@ -"""可替换 adapter 的接口定义。""" - -import datetime as _dt -from typing import Dict, List - -try: - from typing import Protocol -except ImportError: # pragma: no cover - from typing_extensions import Protocol - -from .models import ( - AccountSnapshot, - AssetSnapshot, - CancelResult, - OrderRef, - OrderRequest, - OrderSnapshot, - OrderSubmitResult, - PositionSnapshot, - TradeSignal, - TradeSnapshot, -) - - -class SignalSource(Protocol): - def fetch(self, account_id: str, limit: int) -> List[TradeSignal]: - ... - - def ack(self, signal: TradeSignal) -> None: - ... - - -class MarketDataProvider(Protocol): - def get_ticks(self, codes: List[str]) -> Dict[str, dict]: - ... - - def get_instrument(self, code: str) -> dict: - ... - - -class PositionProvider(Protocol): - def get_positions(self, account_id: str) -> Dict[str, PositionSnapshot]: - ... - - def get_asset(self, account_id: str) -> AssetSnapshot: - ... - - -class OrderGateway(Protocol): - def submit(self, request: OrderRequest) -> OrderSubmitResult: - ... - - def cancel(self, order_ref: OrderRef) -> CancelResult: - ... - - def query_orders(self, account_id: str, strategy_name: str) -> List[OrderSnapshot]: - ... - - def query_trades(self, account_id: str, strategy_name: str) -> List[TradeSnapshot]: - ... - - -class PositionSyncSink(Protocol): - def publish(self, snapshot: AccountSnapshot) -> None: - ... - - -class StateStore(Protocol): - def claim(self, signal: TradeSignal, consumer_id: str) -> bool: - ... - - def mark_submitted(self, signal_id: str, result: OrderSubmitResult) -> None: - ... - - def mark_finished(self, signal_id: str, status: str, message: str = "") -> None: - ... - - -class RuntimeAdapter(Protocol): - def now(self) -> _dt.datetime: - ... diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/download_jobs.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/download_jobs.py deleted file mode 100644 index 36ecb67..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/download_jobs.py +++ /dev/null @@ -1,252 +0,0 @@ -"""Async, chunked download jobs for Big QMT. - -A client submits a download job (fire-and-forget) into a Redis queue and polls -its status. The Big QMT strategy thread drains one job at a time and downloads a -bounded slice of symbols per tick (``chunk_size`` symbols, capped by a wall-clock -budget), so a long ``download_history_data2`` never blocks the strategy thread / -RPC pump. Historical bars land in the Big QMT machine's local store; clients then -read them back with fast ``get_local_data`` / ``get_market_data`` calls. - -Redis layout (per account). All stored VALUES are digit-free encoded (see _enc) -so the QMT terminal's redis compliance filter never trips on stock codes in the -job data the pump reads back: -- ``bigqmt:dljob:pending:{account_id}`` list of pending job ids (RPUSH/LPOP) -- ``bigqmt:dljob:item:{account_id}:{job_id}`` encoded job blob incl. progress -- ``bigqmt:dljob:active:{account_id}`` id of the job being processed now -""" - -import json -import time -import uuid - - -# Dedicated "bigqmt:dljob:*" namespace for the client<->pump protocol. -QUEUE_KEY_TEMPLATE = "bigqmt:dljob:pending:{account_id}" -JOB_KEY_TEMPLATE = "bigqmt:dljob:item:{account_id}:{job_id}" -CURRENT_KEY_TEMPLATE = "bigqmt:dljob:active:{account_id}" - -DEFAULT_JOB_TTL_SECONDS = 3600 -DEFAULT_CHUNK_SIZE = 10 -DEFAULT_MAX_WALL_SECONDS = 0.5 - -# Terminal + in-flight states. -PENDING = "pending" -RUNNING = "running" -DONE = "done" -FAILED = "failed" -_ACTIVE_STATES = (PENDING, RUNNING) - - -def queue_key(account_id): - return QUEUE_KEY_TEMPLATE.format(account_id=str(account_id or "")) - - -def job_key(account_id, job_id): - return JOB_KEY_TEMPLATE.format(account_id=str(account_id or ""), job_id=str(job_id or "")) - - -def current_key(account_id): - return CURRENT_KEY_TEMPLATE.format(account_id=str(account_id or "")) - - -def _text(value): - if value is None: - return "" - if isinstance(value, bytes): - return value.decode("utf-8") - return str(value) - - -# The 国金证券 QMT terminal ships a redis client whose check_response() raises -# "Sensitive Data Detected, Forbidden!" whenever a Redis *response* contains a -# stock-code + operation-code DIGIT pattern (a brokerage control against trading -# signals flowing through Redis). The pump runs inside that terminal and must read -# job data (which contains stock codes) back from Redis. So every value the pump -# reads is stored as a DIGIT-FREE token: hex-encode, then shift digits 0-9 -> the -# letters g-p, making the stored value all letters (a-p). The stock-code regex -# requires digits, so it can never match. Reversible; writes are never filtered -# (only responses are), so only read-back values need this. -_DIGIT_TO_ALPHA = str.maketrans("0123456789", "ghijklmnop") -_ALPHA_TO_DIGIT = str.maketrans("ghijklmnop", "0123456789") - - -def _enc(text_value): - return _text(text_value).encode("utf-8").hex().translate(_DIGIT_TO_ALPHA) - - -def _dec(token): - text = _text(token) - if not text: - return None - try: - return bytes.fromhex(text.translate(_ALPHA_TO_DIGIT)).decode("utf-8") - except Exception: - return None - - -def submit_download_job( - redis_client, - account_id, - stock_list, - period, - method="download_history_data2", - start_time="", - end_time="", - incrementally=None, - chunk_size=DEFAULT_CHUNK_SIZE, - job_ttl_seconds=DEFAULT_JOB_TTL_SECONDS, -): - """Queue a download job and return its initial status dict (non-blocking).""" - codes = [str(code) for code in (stock_list or []) if str(code or "").strip()] - if not codes: - raise ValueError("stock_list is required for a download job") - job_id = uuid.uuid4().hex[:16] - now = time.time() - job = { - "job_id": job_id, - "method": str(method or "download_history_data2"), - "stock_list": codes, - "period": period, - "start_time": start_time or "", - "end_time": end_time or "", - "incrementally": incrementally, - "chunk_size": int(chunk_size or DEFAULT_CHUNK_SIZE), - "total": len(codes), - "done": 0, - "state": PENDING, - "error": "", - "created_at_ts": now, - "updated_at_ts": now, - } - ttl = int(max(1, job_ttl_seconds)) - redis_client.setex(job_key(account_id, job_id), ttl, _enc(json.dumps(job, ensure_ascii=False))) - redis_client.rpush(queue_key(account_id), _enc(job_id)) - try: - redis_client.expire(queue_key(account_id), ttl) - except Exception: - pass - return job - - -def read_download_status(redis_client, account_id, job_id): - """Return the current job status dict, or None if unknown/expired.""" - decoded = _dec(redis_client.get(job_key(account_id, job_id))) - if not decoded: - return None - try: - job = json.loads(decoded) - except Exception: - return None - return job if isinstance(job, dict) else None - - -def wait_download_job( - redis_client, - account_id, - job_id, - wait_seconds=600.0, - poll_interval_seconds=0.5, -): - """Block (client-side only) until the job reaches a terminal state or timeout.""" - deadline = time.time() + max(0.0, float(wait_seconds)) - while True: - status = read_download_status(redis_client, account_id, job_id) - if status and status.get("state") in (DONE, FAILED): - return status - if time.time() >= deadline: - return status - time.sleep(max(0.05, float(poll_interval_seconds))) - - -def _write_job(redis_client, account_id, job, job_ttl_seconds): - job["updated_at_ts"] = time.time() - ttl = int(max(1, job_ttl_seconds)) - redis_client.setex(job_key(account_id, job["job_id"]), ttl, _enc(json.dumps(job, ensure_ascii=False))) - - -def _acquire_current_job(redis_client, account_id): - ckey = current_key(account_id) - current_id = _dec(redis_client.get(ckey)) - if current_id: - job = read_download_status(redis_client, account_id, current_id) - if job and job.get("state") in _ACTIVE_STATES: - return job - # Stale pointer (job done/failed/expired): drop it and pick the next one. - redis_client.delete(ckey) - while True: - job_id = _dec(redis_client.lpop(queue_key(account_id))) - if not job_id: - return None - job = read_download_status(redis_client, account_id, job_id) - if job and job.get("state") in _ACTIVE_STATES: - redis_client.set(ckey, _enc(job_id)) - return job - # Skip unknown/expired/finished ids left in the queue. - - -def _download_chunk(market_data, method, chunk, period, start_time, end_time, incrementally): - if method == "download_history_data": - for code in chunk: - market_data.download_history_data(code, period, start_time, end_time, incrementally) - else: - market_data.download_history_data2(chunk, period, start_time, end_time, incrementally) - - -def pump_download_jobs( - redis_client, - market_data, - account_id, - chunk_size=DEFAULT_CHUNK_SIZE, - max_wall_seconds=DEFAULT_MAX_WALL_SECONDS, - job_ttl_seconds=DEFAULT_JOB_TTL_SECONDS, -): - """Advance the active download job by a bounded slice. Call once per tick. - - Downloads at least one chunk (so progress is always made) and keeps going - until the wall-clock budget is spent. Returns a small status summary, or None - when there is no active job. Runs on the caller (strategy) thread. - """ - job = _acquire_current_job(redis_client, account_id) - if job is None: - return None - stock_list = job.get("stock_list") or [] - total = int(job.get("total") or len(stock_list)) - done = int(job.get("done") or 0) - step = int(job.get("chunk_size") or chunk_size or DEFAULT_CHUNK_SIZE) - if step <= 0: - step = DEFAULT_CHUNK_SIZE - method = str(job.get("method") or "download_history_data2") - period = job.get("period") - start_time = job.get("start_time") or "" - end_time = job.get("end_time") or "" - incrementally = job.get("incrementally") - - started_at = time.time() - processed_this_tick = 0 - try: - while done < total: - # Always run one chunk; only the budget check (after the first) can - # stop the tick, so a single heavy chunk is the smallest block unit. - if max_wall_seconds and processed_this_tick and (time.time() - started_at) > float(max_wall_seconds): - break - chunk = stock_list[done:done + step] - _download_chunk(market_data, method, chunk, period, start_time, end_time, incrementally) - done += len(chunk) - processed_this_tick += len(chunk) - except Exception as exc: - job["state"] = FAILED - job["error"] = "%s: %s" % (exc.__class__.__name__, exc) - job["done"] = done - _write_job(redis_client, account_id, job, job_ttl_seconds) - redis_client.delete(current_key(account_id)) - return {"job_id": job["job_id"], "state": FAILED, "done": done, "total": total, "error": job["error"]} - - job["done"] = done - if done >= total: - job["state"] = DONE - _write_job(redis_client, account_id, job, job_ttl_seconds) - redis_client.delete(current_key(account_id)) - return {"job_id": job["job_id"], "state": DONE, "done": done, "total": total} - job["state"] = RUNNING - _write_job(redis_client, account_id, job, job_ttl_seconds) - return {"job_id": job["job_id"], "state": RUNNING, "done": done, "total": total} diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/exec_events.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/exec_events.py deleted file mode 100644 index f724f98..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/exec_events.py +++ /dev/null @@ -1,449 +0,0 @@ -"""Real-time order/trade (execution) event push over Redis. - -Big QMT fires ``order_callback(ContextInfo, orderInfo)`` and -``deal_callback(ContextInfo, dealInfo)`` inside the strategy process. We normalize -the QMT order/deal object (ThinkTrader ``m_*`` fields) into a plain dict and -publish it to a Redis channel, so clients receive ``on_stock_order`` / -``on_stock_trade`` callbacks in real time (MiniQMT style) instead of polling. - -Channels (also used as capped streams for short replay, xadd + publish): -- ``bigqmt:order_events:{account_id}`` -- ``bigqmt:trade_events:{account_id}`` - -The normalized field names match ``BigQmtXtTrader._order_from_dict`` / -``_trade_from_dict`` so the client can shape them straight into MiniQMT objects. -""" - -import json -import time - - -ORDER_CHANNEL_TEMPLATE = "bigqmt:order_events:{account_id}" -TRADE_CHANNEL_TEMPLATE = "bigqmt:trade_events:{account_id}" -ORDER_ERROR_CHANNEL_TEMPLATE = "bigqmt:order_error_events:{account_id}" -CANCEL_ERROR_CHANNEL_TEMPLATE = "bigqmt:cancel_error_events:{account_id}" -ORDER_IDENTITY_KEY_TEMPLATE = "bigqmt:order_identity:{account_id}:{user_order_id}" - -EVENT_ORDER = "order" -EVENT_TRADE = "trade" -EVENT_ORDER_ERROR = "order_error" -EVENT_CANCEL_ERROR = "cancel_error" - -# ThinkTrader enum_EEntrustBS (买卖方向, the m_nDirection field), universal across -# 股票/期货/期权. Ref: https://dict.thinktrader.net/innerApi/enum_constants.html -ENTRUST_BUY = 48 # 买入 / 多 -ENTRUST_SELL = 49 # 卖出 / 空 -ENTRUST_PLEDGE_IN = 81 # 质押入库 -ENTRUST_PLEDGE_OUT = 66 # 质押出库 - -# enum_EEntrustBS (买卖方向, the m_nDirection field), per QMT enum docs. -# 48=买, 49=卖. Universal across 股票/期货/期权. -# -# Real-world findings from live COrderDetail/CDealDetail callbacks -# (diagnosed via exec_events_debug_raw_fields=True, 2026-07-29): -# QMT returns m_nDirection=48 **unconditionally** — even for sell orders. -# m_nOffsetFlag correctly reflects direction (48=买, 49=卖 for stocks). -# m_nOpType correctly reflects direction (23=买, 24=卖) on orders. -# query_orders uses m_nOffsetFlag and works correctly in production. -# -# Therefore _extract_direction uses an arbitration chain: -# Preferred: m_nOffsetFlag (most reliable in live callbacks, matches query_orders) -# Fallback: m_nDirection (traditional EEntrustBS; can be stuck at 48 in calls) -# Arbiter: when direction≠offset (futures: sell+open=49+48), -# consult m_nOpType (23/24) to resolve the conflict; for trades -# (no m_nOpType) trust m_nOffsetFlag (QMT docs confirm stock -# direction=offset). -# Last: order_type (MiniQMT STOCK_BUY=23 / STOCK_SELL=24) and plain text -# Unknown -> "" (the raw value is always preserved so callers can refine). -OFFSET_OPEN = 48 -OFFSET_CLOSE = 49 -OFFSET_CLOSE_TODAY = 51 -OFFSET_CLOSE_YESTERDAY = 52 - -_BUY_DIRECTIONS = {ENTRUST_BUY, str(ENTRUST_BUY), OFFSET_OPEN, str(OFFSET_OPEN), 23, "23", "BUY", "buy", "B"} -_SELL_DIRECTIONS = {ENTRUST_SELL, str(ENTRUST_SELL), OFFSET_CLOSE, str(OFFSET_CLOSE), OFFSET_CLOSE_TODAY, str(OFFSET_CLOSE_TODAY), OFFSET_CLOSE_YESTERDAY, str(OFFSET_CLOSE_YESTERDAY), 24, "24", "SELL", "sell", "S"} - - -def order_channel(account_id): - return ORDER_CHANNEL_TEMPLATE.format(account_id=str(account_id or "")) - - -def trade_channel(account_id): - return TRADE_CHANNEL_TEMPLATE.format(account_id=str(account_id or "")) - - -def order_error_channel(account_id): - return ORDER_ERROR_CHANNEL_TEMPLATE.format(account_id=str(account_id or "")) - - -def cancel_error_channel(account_id): - return CANCEL_ERROR_CHANNEL_TEMPLATE.format(account_id=str(account_id or "")) - - -def order_identity_key(account_id, user_order_id): - return ORDER_IDENTITY_KEY_TEMPLATE.format( - account_id=str(account_id or ""), - user_order_id=str(user_order_id or ""), - ) - - -def _attr(obj, names, default=None): - for name in names: - if isinstance(obj, dict): - if name in obj and obj[name] is not None: - return obj[name] - else: - value = getattr(obj, name, None) - if value is not None: - return value - return default - - -def _action_from_direction(direction): - if direction in _BUY_DIRECTIONS: - return "BUY" - if direction in _SELL_DIRECTIONS: - return "SELL" - return "" - - -def _is_buy(val): - v = int(val) - return v in _BUY_DIRECTIONS - - -def _is_sell(val): - v = int(val) - return v in _SELL_DIRECTIONS - - -def _conflict_resolve(d_val, o_val, obj): - """When m_nDirection and m_nOffsetFlag disagree, arbitrate via m_nOpType. - - Live diagnosis confirms: - - Stock sell: direction=48(buy), offset=49(sell), op_type=24(sell) → sell - - Futures sell+open: direction=49(sell), offset=48(open), op_type=24(sell) → sell - - Futures buy+close: direction=48(buy), offset=49(close), op_type=23(buy) → buy - - Returns a resolved value, or None if no arbiter can decide. - """ - op = _attr(obj, ["m_nOpType", "op_type", "order_type"]) - if op is not None: - try: - op_int = int(op) - if op_int in _BUY_DIRECTIONS: - return d_val if _is_buy(d_val) else o_val if _is_buy(o_val) else op - if op_int in _SELL_DIRECTIONS: - return d_val if _is_sell(d_val) else o_val if _is_sell(o_val) else op - except (TypeError, ValueError): - if op in _BUY_DIRECTIONS: - return d_val if _is_buy(d_val) else o_val if _is_buy(o_val) else op - if op in _SELL_DIRECTIONS: - return d_val if _is_sell(d_val) else o_val if _is_sell(o_val) else op - # no arbiter — trust offset (QMT docs confirm stock direction=offset) - return o_val - - -def _extract_direction(obj): - """Extract buy/sell direction, matching query_orders' reliable logic. - - Priority chain (documented with live-diagnosis justification): - 1. m_nOffsetFlag — most reliable in live callbacks (matches query_orders) - 2. m_nDirection — traditional EEntrustBS (can be stuck at 48) - 3. Arbitration: when direction≠offset, consult m_nOpType (orders: 23/24) - to resolve correctly for both stocks AND futures. - 4. m_nOpType / order_type — last resort fallback. - - The raw value is always returned (even pledge=81) so callers can inspect it; - _action_from_direction maps only known buy/sell values, leaving others "". - - References - ---------- - - Live diagnosis 2026-07-29 (COrderDetail/CDealDetail): - m_nDirection=48 unconditionally, m_nOffsetFlag=48(buy)/49(sell) correct, - m_nOpType=23(buy)/24(sell) correct (orders only). - - QMT enum docs: enum_EEntrustBS (48=买,49=卖), enum_EOffset_Flag_Type - (48=开仓,49=平仓). For stocks direction=offset; for futures they differ. - - query_orders uses m_nOffsetFlag and works correctly in production. - """ - offset = _attr(obj, ["m_nOffsetFlag", "offset_flag"]) - direction = _attr(obj, ["m_nDirection", "direction"]) - - # 1. offset alone — use it directly (matches query_orders) - if offset is not None and direction is None: - try: - o = int(offset) - if o in _BUY_DIRECTIONS or o in _SELL_DIRECTIONS: - return offset - except (TypeError, ValueError): - if offset in _BUY_DIRECTIONS or offset in _SELL_DIRECTIONS: - return offset - - # 2. direction alone — use it - if direction is not None and offset is None: - try: - d = int(direction) - if d in _BUY_DIRECTIONS or d in _SELL_DIRECTIONS: - return direction - if d != 0: - return direction - except (TypeError, ValueError): - if direction in _BUY_DIRECTIONS or direction in _SELL_DIRECTIONS: - return direction - return direction - - # 3. both present - if direction is not None and offset is not None: - try: - d = int(direction) - o = int(offset) - d_valid = (d in _BUY_DIRECTIONS or d in _SELL_DIRECTIONS) - o_valid = (o in _BUY_DIRECTIONS or o in _SELL_DIRECTIONS) - - if d_valid and o_valid: - if d == o: - return direction # agree → use either - # disagree → arbitrate via m_nOpType - return _conflict_resolve(d, o, obj) - - if d_valid and not o_valid: - return direction - if o_valid and not d_valid: - return offset - # neither valid — fall through - except (TypeError, ValueError): - pass - - # 4. last resort: m_nOpType / order_type - return _attr(obj, ["m_nOpType", "op_type", "order_type"]) - - -# Fields we care about when diagnosing a direction misread. Anything starting -# with "m_" is captured automatically; these are the MiniQMT-style names that -# do not match that prefix. -_RAW_SNAPSHOT_EXTRA_FIELDS = ( - "stock_code", - "order_type", - "op_type", - "direction", - "offset_flag", - "order_status", - "order_volume", - "traded_volume", - "price", - "order_id", - "order_sysid", - "order_sys_id", - "trade_id", - "traded_id", - "strategy_name", - "strategyName", - "user_order_id", - "order_remark", - "remark", -) - - -def raw_field_snapshot(obj, max_repr=120): - """Capture every readable field of a live QMT callback object. - - Direction extraction relies on understanding what ``m_nDirection``, - ``m_nOffsetFlag`` and ``m_nOpType`` carry in live callbacks. This dumps - every readable field so one live order settles the question. - - Returns ``{name: " "}``. Never raises: a callback that dies - while being diagnosed would be worse than no diagnosis. - """ - snapshot = {} - try: - if isinstance(obj, dict): - names = list(obj.keys()) - else: - names = [name for name in dir(obj) if name.startswith("m_")] - names.extend(_RAW_SNAPSHOT_EXTRA_FIELDS) - except Exception: - return {"__error__": "dir() failed"} - seen = set() - for name in names: - key = str(name) - if key in seen or key.startswith("__"): - continue - seen.add(key) - try: - if isinstance(obj, dict): - if key not in obj: - continue - value = obj[key] - else: - if not hasattr(obj, key): - continue - value = getattr(obj, key) - if callable(value): - continue - text = repr(value) - if len(text) > max_repr: - text = text[:max_repr] + "..." - snapshot[key] = "%s %s" % (type(value).__name__, text) - except Exception as exc: # noqa: BLE001 - diagnostics must not break callbacks - snapshot[key] = "" % exc.__class__.__name__ - return snapshot - - -def format_raw_snapshot(kind, obj): - """One-line, GBK-safe rendering of :func:`raw_field_snapshot` for the QMT panel.""" - snapshot = raw_field_snapshot(obj) - parts = ["%s=%s" % (name, snapshot[name]) for name in sorted(snapshot)] - return "[bigqmt_exec_raw] %s type=%s %s" % ( - kind, - type(obj).__name__, - " | ".join(parts) or "", - ) - - -def normalize_order_event(order, account_id=""): - """Build a JSON-able order event dict from a Big QMT orderInfo object.""" - direction = _extract_direction(order) - return { - "event_type": EVENT_ORDER, - "account_id": str(_attr(order, ["m_strAccountID", "account_id"], account_id) or account_id or ""), - "stock_code": str(_attr(order, ["m_strInstrumentID", "stock_code", "m_strInstrument"], "") or ""), - "order_sys_id": str(_attr(order, ["m_strOrderSysID", "order_sys_id", "order_sysid", "order_id"], "") or ""), - "order_volume": _attr(order, ["m_nVolumeTotal", "order_volume", "volume"]), - "traded_volume": _attr(order, ["m_nVolumeTraded", "traded_volume"]), - "price": _attr(order, ["m_dLimitPrice", "price", "limit_price"]), - "status": _attr(order, ["m_nOrderStatus", "order_status", "status"]), - "direction": direction, - "action": _action_from_direction(direction), - "offset_flag": _attr(order, ["m_nOffsetFlag", "offset_flag"]), - "strategy_name": str(_attr(order, ["strategyName", "m_strStrategyName", "strategy_name"], "") or ""), - "remark": str(_attr(order, ["m_strRemark", "order_remark", "remark", "user_order_id"], "") or ""), - "user_order_id": str(_attr(order, ["m_strRemark", "user_order_id", "order_remark", "remark"], "") or ""), - "opt_name": str(_attr(order, ["m_strOptName", "opt_name"], "") or ""), - "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), - "created_at_ts": time.time(), - } - - -def remember_order_identity(redis_client, account_id, user_order_id, strategy_name="", stock_code="", ttl_seconds=86400): - user_order_id = str(user_order_id or "").strip() - if not user_order_id or redis_client is None: - return None - payload = { - "account_id": str(account_id or ""), - "user_order_id": user_order_id, - "strategy_name": str(strategy_name or ""), - "stock_code": str(stock_code or ""), - "created_at_ts": time.time(), - } - try: - redis_client.setex( - order_identity_key(account_id, user_order_id), - int(ttl_seconds or 86400), - json.dumps(payload, ensure_ascii=False, default=str), - ) - except Exception: - pass - return payload - - -def enrich_order_identity(redis_client, account_id, event): - if redis_client is None or not isinstance(event, dict): - return event - user_order_id = str(event.get("user_order_id") or event.get("remark") or "").strip() - if not user_order_id: - return event - try: - raw = redis_client.get(order_identity_key(account_id, user_order_id)) - except Exception: - raw = None - if not raw: - return event - try: - identity = json.loads(raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else str(raw)) - except Exception: - return event - if not event.get("strategy_name") and identity.get("strategy_name"): - event["strategy_name"] = str(identity.get("strategy_name") or "") - if not event.get("stock_code") and identity.get("stock_code"): - event["stock_code"] = str(identity.get("stock_code") or "") - return event - - -def normalize_trade_event(trade, account_id=""): - """Build a JSON-able trade (成交) event dict from a Big QMT dealInfo object.""" - direction = _extract_direction(trade) - return { - "event_type": EVENT_TRADE, - "account_id": str(_attr(trade, ["m_strAccountID", "account_id"], account_id) or account_id or ""), - "stock_code": str(_attr(trade, ["m_strInstrumentID", "stock_code"], "") or ""), - "order_sys_id": str(_attr(trade, ["m_strOrderSysID", "order_sys_id", "order_sysid", "order_id"], "") or ""), - "trade_id": str(_attr(trade, ["m_strTradeID", "trade_id"], "") or ""), - "volume": _attr(trade, ["m_nVolume", "volume", "traded_volume"]), - "price": _attr(trade, ["m_dPrice", "price", "traded_price"]), - "amount": _attr(trade, ["m_dTradeAmount", "amount"]), - "commission": _attr(trade, ["m_dComssion", "m_dCommission", "commission"]), - "direction": direction, - "action": _action_from_direction(direction), - "offset_flag": _attr(trade, ["m_nOffsetFlag", "offset_flag"]), - "traded_at": str(_attr(trade, ["m_strTradeTime", "traded_at", "trade_time"], "") or ""), - "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), - "created_at_ts": time.time(), - } - - -def _publish(redis_client, channel, event, maxlen=2000): - raw = json.dumps(event, ensure_ascii=False, default=str) - try: - redis_client.xadd(channel, {"payload": raw}, maxlen=maxlen, approximate=True) - except Exception: - pass - redis_client.publish(channel, raw) - return event - - -def publish_order_event(redis_client, account_id, event): - return _publish(redis_client, order_channel(account_id), event) - - -def publish_trade_event(redis_client, account_id, event): - return _publish(redis_client, trade_channel(account_id), event) - - -def publish_order_error_event(redis_client, account_id, event): - return _publish(redis_client, order_error_channel(account_id), event) - - -def publish_cancel_error_event(redis_client, account_id, event): - return _publish(redis_client, cancel_error_channel(account_id), event) - - -def normalize_order_error_event(order_error, account_id=""): - """Build a JSON-able order-error event dict (废单/拒单). - - QMT order callbacks carry the failed order via m_strOrderSysID / error info. - MiniQMT's on_order_error receives an XtOrderError with error_id/error_msg. - """ - return { - "event_type": EVENT_ORDER_ERROR, - "account_id": str(_attr(order_error, ["m_strAccountID", "account_id"], account_id) or account_id or ""), - "stock_code": str(_attr(order_error, ["m_strInstrumentID", "stock_code"], "") or ""), - "order_sys_id": str(_attr(order_error, ["m_strOrderSysID", "order_sys_id", "order_sysid", "order_id"], "") or ""), - "error_id": _attr(order_error, ["m_nErrorID", "error_id", "m_nOrderStatus"]), - "error_msg": str(_attr(order_error, ["m_strErrorMsg", "error_msg", "m_strMsg"], "") or ""), - "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), - "created_at_ts": time.time(), - } - - -def normalize_cancel_error_event(cancel_error, account_id=""): - """Build a JSON-able cancel-error event dict (撤单失败).""" - return { - "event_type": EVENT_CANCEL_ERROR, - "account_id": str(_attr(cancel_error, ["m_strAccountID", "account_id"], account_id) or account_id or ""), - "stock_code": str(_attr(cancel_error, ["m_strInstrumentID", "stock_code"], "") or ""), - "order_sys_id": str(_attr(cancel_error, ["m_strOrderSysID", "order_sys_id", "order_sysid", "order_id"], "") or ""), - "error_id": _attr(cancel_error, ["m_nErrorID", "error_id"]), - "error_msg": str(_attr(cancel_error, ["m_strErrorMsg", "error_msg", "m_strMsg"], "") or ""), - "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), - "created_at_ts": time.time(), - } diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/formula_server.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/formula_server.py deleted file mode 100644 index 8e01204..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/formula_server.py +++ /dev/null @@ -1,718 +0,0 @@ -"""Direct client for the Big QMT FormulaServer RPC (default port 58600). - -Why this exists ---------------- -The RPC bridge in :mod:`redis_rpc` routes every read through the QMT *strategy* -process: client -> redis/zmq -> QMT python thread -> ContextInfo -> back. That -costs ~13ms (redis) or ~0.7ms-with-500ms-GIL-spikes (zmq), and every read -competes for the QMT main-thread GIL against the strategy itself. - -FormulaServer is the C++ quote/reference-data service inside the same QMT -terminal, listening on the port named in ``config/formulaserver/formulaserver.ini`` -(``[server_formula] address``, default 58600). QMT ships its own client for it at -``bin.x64/Lib/site-packages/qmt_api``. Talking to it directly bypasses the -strategy process entirely: measured p50 **0.07ms**, and zero GIL contention. - -What it can and cannot do -------------------------- -FormulaServer serves market/reference data ONLY. Every account, position, order -and trade method answers ``ErrorID 200005 未找到该服务``, as do ``getFullTick`` -and ``getQuote``. So this is a read fast-path, never a replacement for the RPC -bridge — trading, account queries and 五档 snapshots stay on it. - -Deliberately NOT routed here, despite FormulaServer exposing something similar: - -* ``get_trading_dates`` — FormulaServer wants a *stock code* (``000001.SZ``); - passing a market (``SH``) silently returns ``[]``. Our callers pass markets. -* ``get_divid_factors`` / ``get_risk_free_rate`` — parameter semantics differ - (range vs single date, index vs timetag). A wrong calendar or dividend factor - is worse than a slow one. -* Adjusted bars — see :func:`_market_data_params`; ``dividendType`` appears to - be ignored by the server, so only unadjusted requests are routed. - -Every failure here is non-fatal: :class:`FormulaServerRouter` reports the method -as unroutable and the caller falls back to the normal RPC path. -""" - -import os -import socket -import struct -import threading -import time -import zlib - - -DEFAULT_HOST = "127.0.0.1" -DEFAULT_PORT = 58600 -DEFAULT_TIMEOUT_SECONDS = 3.0 -# After a transport failure, stop trying for this long so a dead/absent -# FormulaServer costs one timeout rather than one per call. -DEFAULT_FAILURE_COOLDOWN_SECONDS = 30.0 - -NET_CMD_RPC = 3 -COMPRESS_ZLIB = 1 -COMPRESS_DOUBLE_ZLIB = 2 - -# FormulaServer's "method not found" code. Distinct from a transport failure: -# it means the server is healthy and simply does not implement the call. -ERROR_METHOD_NOT_FOUND = 200005 - - -class FormulaServerError(RuntimeError): - """FormulaServer answered with a non-zero status (bad params, no such method).""" - - def __init__(self, message, error_id=None): - RuntimeError.__init__(self, message) - self.error_id = error_id - - -class FormulaServerUnavailable(RuntimeError): - """The FormulaServer could not be reached (connect/IO/protocol failure).""" - - -# --------------------------------------------------------------------------- -# BSON codec -# --------------------------------------------------------------------------- -# FormulaServer frames BSON documents. pymongo's ``bson`` is used when present -# (faster, battle-tested); otherwise the minimal codec below covers the types -# this wire actually carries. Keeping a built-in path means an external client -# needs no pymongo just to read market data. - -def _load_bson(): - for module_name in ("bson", "xtquant.xtbson.bson36"): - try: - module = __import__(module_name, fromlist=["BSON"]) - except Exception: - continue - if hasattr(module, "BSON"): - return module - return None - - -_BSON = _load_bson() - - -def _encode_document(pairs): - body = b"".join(_encode_element(str(key), value) for key, value in pairs) - return struct.pack(" ``formulaserver.ini`` under - ``qmt_root`` > ``BIGQMT_FORMULA_HOST``/``BIGQMT_FORMULA_PORT`` > defaults. - The address binds ``0.0.0.0`` in QMT's shipped config, so a remote client - can reach it too when the firewall allows. - """ - config = dict(config or {}) - host = str(config.get("host") or os.environ.get("BIGQMT_FORMULA_HOST") or DEFAULT_HOST) - port = config.get("port") - if not port: - port = read_formulaserver_port(config.get("qmt_root")) - if not port: - port = os.environ.get("BIGQMT_FORMULA_PORT") - try: - port = int(port) - except (TypeError, ValueError): - port = DEFAULT_PORT - return host, port - - -# --------------------------------------------------------------------------- -# Client -# --------------------------------------------------------------------------- - -class FormulaServerClient(object): - """Thread-safe BSON-over-TCP client for FormulaServer. - - One socket is shared under a lock. FormulaServer matches responses by - sequence number, so concurrent use of a single socket would require - demultiplexing; serializing is simpler and, at 0.07ms per call, ample. - """ - - def __init__( - self, - host=DEFAULT_HOST, - port=DEFAULT_PORT, - timeout_seconds=DEFAULT_TIMEOUT_SECONDS, - print_prefix="[bigqmt_formula]", - ): - self.host = str(host or DEFAULT_HOST) - self.port = int(port or DEFAULT_PORT) - self.timeout_seconds = float(timeout_seconds or DEFAULT_TIMEOUT_SECONDS) - self.print_prefix = print_prefix - self._lock = threading.Lock() - self._socket = None - self._seq = 0 - - # -- wire ------------------------------------------------------------ - def _connect_locked(self): - if self._socket is not None: - return self._socket - try: - sock = socket.create_connection((self.host, self.port), self.timeout_seconds) - sock.settimeout(self.timeout_seconds) - except Exception as exc: - raise FormulaServerUnavailable( - "connect %s:%s failed: %s" % (self.host, self.port, exc) - ) - self._socket = sock - return sock - - def _close_locked(self): - if self._socket is not None: - try: - self._socket.close() - except Exception: - pass - self._socket = None - - def close(self): - with self._lock: - self._close_locked() - - def _recv_exactly(self, sock, length): - chunks = [] - remaining = length - while remaining > 0: - more = sock.recv(remaining) - if not more: - raise FormulaServerUnavailable("socket closed mid-message") - chunks.append(more) - remaining -= len(more) - return b"".join(chunks) - - def _request_locked(self, func, params): - sock = self._connect_locked() - self._seq += 1 - seq = self._seq - body = bson_encode({"func": str(func), "params": dict(params or {})}) - tag = ((seq >> 32) & 0x0F) << 8 - packet = struct.pack( - "!IIHH%ds" % len(body), - len(body) + 12, - seq & 0xFFFFFFFF, - NET_CMD_RPC, - tag, - body, - ) - try: - sock.sendall(packet) - except Exception as exc: - raise FormulaServerUnavailable("send failed: %s" % exc) - # Subscription pushes share the socket; skip anything that is not our seq. - while True: - try: - header = self._recv_exactly(sock, 4) - pack_len = struct.unpack_from("!I", header, 0)[0] - rest = self._recv_exactly(sock, pack_len - 4) - except FormulaServerUnavailable: - raise - except Exception as exc: - raise FormulaServerUnavailable("recv failed: %s" % exc) - raw = header + rest - try: - got_seq, _cmd, got_tag, payload_bytes = struct.unpack_from( - "!IHH%ds" % (pack_len - 12), raw, 4 - ) - if (got_tag & 7) in (COMPRESS_ZLIB, COMPRESS_DOUBLE_ZLIB): - payload_bytes = zlib.decompress(payload_bytes) - got_seq = ((got_tag >> 8) & 0x0F) << 32 | got_seq - payload = bson_decode(payload_bytes) - except Exception as exc: - raise FormulaServerUnavailable("decode failed: %s" % exc) - if got_seq != seq: - continue - if payload.get("status") == 0: - return payload.get("params") - detail = payload.get("params") - error_id = None - if isinstance(detail, dict): - error_id = detail.get("ErrorID") - raise FormulaServerError( - "%s failed: %r" % (func, detail), error_id=error_id - ) - - def request(self, func, params=None): - """Call ``func`` and return its ``params`` payload. - - Retries once on a transport failure, since QMT restarts (or an idle - socket reaped by the server) show up as a dead socket on first use. - """ - with self._lock: - try: - return self._request_locked(func, params) - except FormulaServerError: - raise - except FormulaServerUnavailable: - self._close_locked() - return self._request_locked(func, params) - - def ping(self): - """Cheap liveness probe. True when FormulaServer answers at all. - - A ``FormulaServerError`` still counts as alive — the server replied. - """ - try: - self.request("getLastVolume", {"stockCode": "000001.SZ"}) - return True - except FormulaServerError: - return True - except FormulaServerUnavailable: - return False - - def __repr__(self): - return "" % (self.host, self.port) - - -# --------------------------------------------------------------------------- -# Method mapping -# --------------------------------------------------------------------------- -# FormulaServer misspells two instrument fields relative to the xtdata SDK -# (``FloatVolume``/``TotalVolume``). Downstream code reads the SDK spelling, so -# alias them rather than let the lookup silently miss. -_INSTRUMENT_ALIASES = ( - ("FloatVolumn", "FloatVolume"), - ("TotalVolumn", "TotalVolume"), -) - - -def _first(params, names, default=None): - for name in names: - if name in params and params[name] is not None: - return params[name] - return default - - -def _as_list(value): - if value is None: - return [] - if isinstance(value, str): - return [value] - return list(value) - - -def _require_code(params, names): - code = _first(params, names) - text = str(code or "").strip() - if not text: - raise ValueError("a stock code is required (one of %s)" % ", ".join(names)) - return text - - -def _instrument_params(params): - return {"strOptionCode": _require_code(params, ("code", "stock_code", "stockcode"))} - - -def _instrument_result(raw, params): - detail = (raw or {}).get("result") - if not isinstance(detail, dict): - return detail or {} - out = dict(detail) - for wire_name, sdk_name in _INSTRUMENT_ALIASES: - if wire_name in out and sdk_name not in out: - out[sdk_name] = out[wire_name] - return out - - -def _scalar_result(raw, params): - return (raw or {}).get("result") - - -def _list_result(raw, params): - return (raw or {}).get("result") or [] - - -def _last_volume_params(params): - return {"stockCode": _require_code(params, ("stock", "code", "stock_code", "stockcode"))} - - -def _total_share_params(params): - return {"stockCode": _require_code(params, ("stockcode", "code", "stock_code", "stock"))} - - -def _contract_multiplier_params(params): - return {"contractCode": _require_code(params, ("stockcode", "code", "stock_code", "contract_code"))} - - -def _main_contract_params(params): - return {"codeMarket": _require_code(params, ("code_market", "codeMarket", "code"))} - - -def _sector_params(params): - name = str(_first(params, ("sector_name", "sectorName", "sector"), "") or "").strip() - if not name: - raise ValueError("sector_name is required") - # ContextInfo's real_timetag defaults to -1; FormulaServer's realtime - # defaults to 0. Both return identical constituents (verified), so normalize - # the sentinel rather than forward a value the server never documents. - realtime = _first(params, ("real_timetag", "realtime"), 0) - try: - realtime = int(realtime) - except (TypeError, ValueError): - realtime = 0 - if realtime < 0: - realtime = 0 - return {"sectorName": name, "realtime": realtime} - - -def _weight_in_index_params(params): - index_code = _first(params, ("mtkindexcode", "index_code", "indexCode")) - stock_code = _first(params, ("stockcode", "stock_code", "code")) - if not index_code or not stock_code: - raise ValueError("mtkindexcode and stockcode are required") - return {"indexCode": str(index_code), "stockCode": str(stock_code)} - - -def _market_data_params(params): - fields = _as_list(_first(params, ("field_list", "fields"), None)) - codes = _as_list(_first(params, ("stock_list", "stock_code", "stockCodes"), None)) - if not fields or not codes: - raise ValueError("field_list and stock_list are required") - dividend_type = str(params.get("dividend_type") or "none").lower() - # FormulaServer returns byte-identical bars for dividendType none/front, so - # adjustment is not applied here. Serving an adjusted request from this path - # would silently hand back unadjusted prices — refuse and let RPC answer. - if dividend_type not in ("", "none"): - raise ValueError("adjusted bars (dividend_type=%s) are not served here" % dividend_type) - period = str(params.get("period") or "1d") - count = params.get("count", -1) - try: - count = int(count) - except (TypeError, ValueError): - count = -1 - return { - "fields": [str(field) for field in fields], - "stockCodes": [str(code) for code in codes], - "startTime": str(params.get("start_time") or ""), - "endTime": str(params.get("end_time") or ""), - "period": period, - "dividendType": "none", - "count": count, - } - - -def _market_data_result(raw, params): - """Translate FormulaServer's flat bar list into the RPC path's payload. - - Wire shape is ``[code, [time, [field, value, ...], time, [...]], code, ...]``. - We emit the same ``__bigqmt_type__: DataFrame`` envelope the QMT-side adapter - builds, so the client's ``_restore_jsonable`` rebuilds identical DataFrames - whichever path answered. - """ - flat = (raw or {}).get("result") or [] - fields = [str(field) for field in (_first(params, ("field_list", "fields"), None) or [])] - columns = list(fields) - if columns and "stime" not in columns: - columns.insert(0, "stime") - - parsed = {} - for index in range(0, len(flat) - 1, 2): - code = str(flat[index]) - timeline = flat[index + 1] or [] - records = [] - for offset in range(0, len(timeline) - 1, 2): - stamp = timeline[offset] - pairs = timeline[offset + 1] or [] - record = {"stime": stamp} - for cursor in range(0, len(pairs) - 1, 2): - record[str(pairs[cursor])] = pairs[cursor + 1] - records.append(record) - parsed[code] = records - - requested = [str(code) for code in _as_list(_first(params, ("stock_list", "stock_code"), None))] - for code in parsed: - if code not in requested: - requested.append(code) - return { - code: { - "__bigqmt_type__": "DataFrame", - "columns": columns, - "records": parsed.get(code) or [], - } - for code in requested - } - - -# our RPC method -> (FormulaServer func, param builder, result adapter) -METHOD_MAP = { - "get_instrument": ("getInstrumentDetail", _instrument_params, _instrument_result), - "get_instrumentdetail": ("getInstrumentDetail", _instrument_params, _instrument_result), - "get_instrument_detail": ("getInstrumentDetail", _instrument_params, _instrument_result), - "get_last_volume": ("getLastVolume", _last_volume_params, _scalar_result), - "get_total_share": ("getTotalShare", _total_share_params, _scalar_result), - "get_contract_multiplier": ("getContractMultiplier", _contract_multiplier_params, _scalar_result), - "get_main_contract": ("getMainContract", _main_contract_params, _scalar_result), - "get_weight_in_index": ("getWeightInIndex", _weight_in_index_params, _scalar_result), - "get_stock_list_in_sector": ("getStockListInSector", _sector_params, _list_result), - "get_market_data_ex": ("getMarketData", _market_data_params, _market_data_result), -} - -SUPPORTED_METHODS = tuple(sorted(METHOD_MAP)) - - -class Unroutable(Exception): - """This call cannot be served by FormulaServer — use the RPC bridge.""" - - -class FormulaServerRouter(object): - """Routes supported read methods to FormulaServer, or declines. - - :meth:`call` raises :class:`Unroutable` for anything it cannot serve — - method not mapped, params that do not translate, server down, feature - disabled. Callers treat that as "fall back to RPC". - """ - - def __init__( - self, - client=None, - enabled=True, - methods=None, - failure_cooldown_seconds=DEFAULT_FAILURE_COOLDOWN_SECONDS, - print_prefix="[bigqmt_formula]", - config=None, - ): - self.enabled = bool(enabled) - self.print_prefix = print_prefix - self.failure_cooldown_seconds = float( - failure_cooldown_seconds or DEFAULT_FAILURE_COOLDOWN_SECONDS - ) - if client is None and self.enabled: - host, port = resolve_address(config) - timeout = float((config or {}).get("timeout_seconds") or DEFAULT_TIMEOUT_SECONDS) - client = FormulaServerClient( - host=host, port=port, timeout_seconds=timeout, print_prefix=print_prefix - ) - self.client = client - if methods: - self.methods = set(str(name) for name in methods) & set(METHOD_MAP) - else: - self.methods = set(METHOD_MAP) - self._unavailable_until = 0.0 - self._announced = False - # Methods the server itself rejected as unimplemented — never retried. - self._unimplemented = set() - self.hits = 0 - self.misses = 0 - - def _available(self): - if not self.enabled or self.client is None: - return False - return time.time() >= self._unavailable_until - - def _mark_unavailable(self, reason): - self._unavailable_until = time.time() + self.failure_cooldown_seconds - print( - "%s unavailable, falling back to RPC for %.0fs: %s" - % (self.print_prefix, self.failure_cooldown_seconds, reason) - ) - - def supports(self, method): - return ( - str(method) in self.methods - and str(method) not in self._unimplemented - and self._available() - ) - - def call(self, method, params=None): - """Serve ``method`` from FormulaServer, or raise :class:`Unroutable`.""" - method = str(method) - if not self.supports(method): - raise Unroutable(method) - func, build_params, adapt_result = METHOD_MAP[method] - try: - wire_params = build_params(dict(params or {})) - except Exception as exc: - # Params that do not translate are a per-call condition, not a - # server fault — do not trip the breaker. - self.misses += 1 - raise Unroutable("%s: %s" % (method, exc)) - try: - raw = self.client.request(func, wire_params) - except FormulaServerError as exc: - self.misses += 1 - if exc.error_id == ERROR_METHOD_NOT_FOUND: - self._unimplemented.add(method) - print( - "%s %s not implemented by this terminal, using RPC from now on" - % (self.print_prefix, method) - ) - raise Unroutable("%s: %s" % (method, exc)) - except FormulaServerUnavailable as exc: - self.misses += 1 - self._mark_unavailable(str(exc)) - raise Unroutable("%s: %s" % (method, exc)) - except Exception as exc: - self.misses += 1 - self._mark_unavailable("%s: %s" % (exc.__class__.__name__, exc)) - raise Unroutable("%s: %s" % (method, exc)) - try: - result = adapt_result(raw, dict(params or {})) - except Exception as exc: - self.misses += 1 - raise Unroutable("%s: result adaptation failed: %s" % (method, exc)) - self.hits += 1 - if not self._announced: - self._announced = True - print( - "%s active at %s:%s (%d methods routed direct)" - % (self.print_prefix, self.client.host, self.client.port, len(self.methods)) - ) - return result - - def stats(self): - return { - "enabled": self.enabled, - "hits": self.hits, - "misses": self.misses, - "available": self._available(), - "unimplemented": sorted(self._unimplemented), - "methods": sorted(self.methods), - } - - def close(self): - if self.client is not None: - self.client.close() - - -def build_router(config=None, print_prefix="[bigqmt_formula]"): - """Build a router from a ``formula_server`` config dict. - - Recognised keys: ``enabled`` (default True), ``host``, ``port``, - ``qmt_root``, ``timeout_seconds``, ``methods``, ``failure_cooldown_seconds``. - ``enabled=False`` yields a router that declines everything, so callers need - no None checks. - """ - config = dict(config or {}) - enabled = config.get("enabled", True) - if isinstance(enabled, str): - enabled = enabled.strip().lower() not in ("0", "false", "no", "off") - return FormulaServerRouter( - enabled=bool(enabled), - methods=config.get("methods"), - failure_cooldown_seconds=config.get("failure_cooldown_seconds"), - print_prefix=print_prefix, - config=config, - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/full_tick_cache.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/full_tick_cache.py deleted file mode 100644 index 7c9b916..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/full_tick_cache.py +++ /dev/null @@ -1,219 +0,0 @@ -"""Demand-driven Redis cache for Big QMT full tick snapshots.""" - -import hashlib -import json -import pickle -import time - -from .code_utils import normalize_stock_code - - -MARKET_CODES = {"SH", "SZ", "BJ", "HK"} -DEMAND_KEY_TEMPLATE = "bigqmt:full_tick:demand:{account_id}" -CACHE_KEY_TEMPLATE = "bigqmt:full_tick:cache:{account_id}:{request_id}" - - -def _decode_text(value): - if isinstance(value, bytes): - return value.decode("utf-8") - return str(value) - - -def _loads_json(value): - return json.loads(_decode_text(value)) - - -def normalize_full_tick_codes(codes): - normalized = [] - seen = set() - for code in codes or []: - text = str(code or "").strip().upper() - if not text: - continue - if text in MARKET_CODES: - item = text - else: - item = normalize_stock_code(text) - if item not in seen: - seen.add(item) - normalized.append(item) - return sorted(normalized) - - -def full_tick_request_id(codes): - normalized = normalize_full_tick_codes(codes) - digest = hashlib.sha1("|".join(normalized).encode("utf-8")).hexdigest() - return digest[:20] - - -def full_tick_demand_key(account_id): - return DEMAND_KEY_TEMPLATE.format(account_id=str(account_id or "")) - - -def full_tick_cache_key(account_id, codes=None, request_id=None): - rid = str(request_id or full_tick_request_id(codes or [])) - return CACHE_KEY_TEMPLATE.format(account_id=str(account_id or ""), request_id=rid) - - -def _dump_snapshot(payload): - return pickle.dumps(payload, protocol=4) - - -def _load_snapshot(raw): - if not raw: - return None - try: - return pickle.loads(raw) - except Exception: - try: - return _loads_json(raw) - except Exception: - return None - - -def request_full_tick_cache(redis_client, account_id, codes, demand_ttl_seconds=10, cache_ttl_seconds=10): - normalized = normalize_full_tick_codes(codes) - if not normalized: - raise ValueError("full tick codes are required") - now = time.time() - request_id = full_tick_request_id(normalized) - payload = { - "request_id": request_id, - "codes": normalized, - "requested_at_ts": now, - "expires_at_ts": now + float(demand_ttl_seconds), - "cache_ttl_seconds": float(cache_ttl_seconds), - } - key = full_tick_demand_key(account_id) - redis_client.hset(key, request_id, json.dumps(payload, ensure_ascii=False, sort_keys=True)) - try: - redis_client.expire(key, max(30, int(float(demand_ttl_seconds) * 3))) - except Exception: - pass - return payload - - -def write_full_tick_cache(redis_client, account_id, codes, data, cache_ttl_seconds=10): - normalized = normalize_full_tick_codes(codes) - request_id = full_tick_request_id(normalized) - now = time.time() - payload = { - "request_id": request_id, - "codes": normalized, - "updated_at_ts": now, - "updated_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)), - "data": data or {}, - } - key = full_tick_cache_key(account_id, request_id=request_id) - ttl = int(max(1, float(cache_ttl_seconds))) - redis_client.setex(key, ttl, _dump_snapshot(payload)) - return payload - - -def read_full_tick_cache(redis_client, account_id, codes, max_age_seconds=10): - normalized = normalize_full_tick_codes(codes) - key = full_tick_cache_key(account_id, codes=normalized) - snapshot = _load_snapshot(redis_client.get(key)) - if not isinstance(snapshot, dict): - return None - if normalize_full_tick_codes(snapshot.get("codes") or []) != normalized: - return None - updated_at = float(snapshot.get("updated_at_ts") or 0) - if updated_at <= 0: - return None - if time.time() - updated_at > float(max_age_seconds): - return None - data = snapshot.get("data") - return data if isinstance(data, dict) else None - - -def wait_full_tick_cache(redis_client, account_id, codes, max_age_seconds=10, wait_seconds=3.5, poll_interval_seconds=0.2): - deadline = time.time() + max(0.0, float(wait_seconds)) - while True: - data = read_full_tick_cache(redis_client, account_id, codes, max_age_seconds=max_age_seconds) - if data is not None: - return data - if time.time() >= deadline: - return None - time.sleep(max(0.05, float(poll_interval_seconds))) - - -def iter_active_full_tick_demands(redis_client, account_id, demand_ttl_seconds=10, max_requests=8): - key = full_tick_demand_key(account_id) - raw_mapping = redis_client.hgetall(key) or {} - now = time.time() - active = [] - for field, raw_payload in list(raw_mapping.items()): - field_text = _decode_text(field) - try: - payload = _loads_json(raw_payload) - except Exception: - try: - redis_client.hdel(key, field_text) - except Exception: - pass - continue - expires_at = float(payload.get("expires_at_ts") or 0) - if expires_at <= now: - try: - redis_client.hdel(key, field_text) - except Exception: - pass - continue - codes = normalize_full_tick_codes(payload.get("codes") or []) - if not codes: - try: - redis_client.hdel(key, field_text) - except Exception: - pass - continue - payload["codes"] = codes - payload["cache_ttl_seconds"] = float(payload.get("cache_ttl_seconds") or demand_ttl_seconds) - active.append(payload) - active.sort(key=lambda item: float(item.get("requested_at_ts") or 0), reverse=True) - return active[: int(max_requests)] - - -def _demand_is_market(codes): - return any(str(code).strip().upper() in MARKET_CODES for code in codes or []) - - -def refresh_full_tick_cache( - redis_client, - context_info, - account_id, - demand_ttl_seconds=10, - cache_ttl_seconds=10, - max_requests=8, - kind=None, - max_wall_seconds=None, -): - """Refresh cached snapshots for active demands. - - ``kind`` selects which demands to refresh: ``None`` (all), ``"symbol"`` - (only symbol-list demands), or ``"market"`` (only whole-market demands such - as SH/SZ/BJ/HK). ``max_wall_seconds`` caps how long one refresh round may run - on the caller (strategy) thread; the in-flight demand always completes and at - least one demand is always refreshed before the budget can cut the round. - """ - started_at = time.time() - refreshed = 0 - for demand in iter_active_full_tick_demands( - redis_client, - account_id, - demand_ttl_seconds=demand_ttl_seconds, - max_requests=max_requests, - ): - codes = demand.get("codes") or [] - is_market = _demand_is_market(codes) - if kind == "symbol" and is_market: - continue - if kind == "market" and not is_market: - continue - if max_wall_seconds and refreshed and (time.time() - started_at) > float(max_wall_seconds): - break - tick_data = context_info.get_full_tick(codes) or {} - ttl = demand.get("cache_ttl_seconds") or cache_ttl_seconds - write_full_tick_cache(redis_client, account_id, codes, tick_data, cache_ttl_seconds=ttl) - refreshed += 1 - return refreshed diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/local_cache.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/local_cache.py deleted file mode 100644 index ecbcdf2..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/local_cache.py +++ /dev/null @@ -1,202 +0,0 @@ -"""Client-side local cache for Big QMT market data. - -Pull bars from Big QMT once over RPC, persist them on the client, then read them -back with ``get_local_data`` without touching Big QMT again — for offline / local -analysis. One file per (period, dividend_type, code); incremental merge + dedupe -by time. Default storage is Parquet (columnar, compressed, cross-language); falls -back to pickle when pyarrow is unavailable. A cache written in one format is read -+ migrated transparently if the configured format changes. -""" - -import os - - -# Candidate time-column names produced by the RPC market-data path. -_TIME_COLS = ("stime", "time", "index", "date", "datetime", "timetag") - - -def _time_col(df): - cols = list(getattr(df, "columns", [])) - for name in _TIME_COLS: - if name in cols: - return name - return None - - -def _pad_end(value): - text = str(value) - return text + "9" * (14 - len(text)) if 0 < len(text) < 14 else text - - -def _drop_placeholder_rows(df): - """Big QMT fills dates it has no local data for with all-zero rows. A real bar - never has close/open == 0, so drop those placeholders — the cache should hold - only real bars, not 0-fill padding.""" - for col in ("close", "open", "price", "lastPrice"): - if col in getattr(df, "columns", []): - try: - return df[df[col] != 0].reset_index(drop=True) - except Exception: - return df - return df - - -def _pyarrow_available(): - try: - import pyarrow # noqa: F401 - - return True - except Exception: - return False - - -def _resolve_format(fmt): - fmt = str(fmt or "auto").lower() - if fmt in ("parquet", "pq"): - return "parquet" - if fmt in ("pkl", "pickle"): - return "pkl" - # auto / unknown - return "parquet" if _pyarrow_available() else "pkl" - - -class LocalMarketCache: - def __init__(self, cache_dir=None, fmt="auto"): - self.cache_dir = str(cache_dir or os.path.join(os.path.expanduser("~"), ".bigqmt_cache")) - self.fmt = _resolve_format(fmt) - - def _ext(self): - return ".parquet" if self.fmt == "parquet" else ".pkl" - - def path(self, code, period, dividend_type="none"): - safe_code = str(code or "").replace("/", "_").replace("\\", "_") - div = str(dividend_type or "none") - return os.path.join(self.cache_dir, str(period or "1d"), div, safe_code + self._ext()) - - def _existing_path(self, code, period, dividend_type): - """Return the on-disk file for this key in the configured format, else the - other format (so switching format still finds + migrates the old cache).""" - primary = self.path(code, period, dividend_type) - if os.path.isfile(primary): - return primary - base = primary[: -len(self._ext())] - for ext in (".parquet", ".pkl"): - alt = base + ext - if os.path.isfile(alt): - return alt - return None - - @staticmethod - def _read_file(path): - import pandas as pd - - # Read by actual file extension (an existing cache may be either format). - if path.endswith(".pkl"): - return pd.read_pickle(path) - return pd.read_parquet(path) - - def _write_file(self, df, path): - # Write in the configured format regardless of the path (the temp file ends - # with ".tmp", not the format extension). - if self.fmt == "parquet": - df.to_parquet(path, index=False) - else: - df.to_pickle(path) - - def write(self, code, period, df, dividend_type="none"): - """Merge ``df`` into the cache for (code, period, dividend_type). - - Dedupe is by time keeping the LAST write, so re-pulling a range overwrites - stale values — which is exactly what front-adjusted (前复权) data needs after - a new dividend re-scales history. Returns total rows stored. - """ - if df is None or not hasattr(df, "shape") or df.shape[0] == 0: - return 0 - import pandas as pd - - incoming = _drop_placeholder_rows(df.copy()) - primary = self.path(code, period, dividend_type) - existing = self._existing_path(code, period, dividend_type) - if incoming.shape[0] == 0: - # Nothing real to add (all 0-fill placeholders); keep existing cache. - if existing: - try: - return self._read_file(existing).shape[0] - except Exception: - return 0 - return 0 - directory = os.path.dirname(primary) - if directory and not os.path.isdir(directory): - os.makedirs(directory, exist_ok=True) - merged = incoming - tcol = _time_col(merged) - if existing: - try: - old = self._read_file(existing) - merged = pd.concat([old, merged], ignore_index=True) - except Exception: - pass - if tcol and tcol in merged.columns: - merged = merged.drop_duplicates(subset=[tcol], keep="last").sort_values(tcol).reset_index(drop=True) - else: - merged = merged.drop_duplicates().reset_index(drop=True) - # Atomic-ish write (temp + replace) so a crash mid-write can't corrupt the file. - tmp = primary + ".tmp" - self._write_file(merged, tmp) - os.replace(tmp, primary) - # Migrated from the other format? drop the stale file. - if existing and existing != primary: - try: - os.remove(existing) - except Exception: - pass - return merged.shape[0] - - def read(self, code, period, start_time="", end_time="", count=-1, dividend_type="none"): - """Return the cached DataFrame for (code, period, dividend_type), filtered.""" - existing = self._existing_path(code, period, dividend_type) - if not existing: - return None - try: - df = self._read_file(existing) - except Exception: - return None - tcol = _time_col(df) - if tcol and tcol in df.columns: - series = df[tcol].astype(str) - if start_time: - df = df[series >= str(start_time)] - if end_time: - df = df[series <= _pad_end(end_time)] - df = df.sort_values(tcol).reset_index(drop=True) - try: - n = int(count) - except (TypeError, ValueError): - n = -1 - if n > 0 and df.shape[0] > n: - df = df.tail(n).reset_index(drop=True) - return df - - def covered(self, code, period, dividend_type="none"): - """Return (first_time, last_time, rows) for the cache, or None if empty.""" - df = self.read(code, period, dividend_type=dividend_type) - if df is None or df.shape[0] == 0: - return None - tcol = _time_col(df) - if not tcol: - return (None, None, df.shape[0]) - series = df[tcol].astype(str) - return (series.iloc[0], series.iloc[-1], df.shape[0]) - - def stats(self): - """Return (files, periods) currently cached across all dividend types.""" - files = 0 - periods = set() - if os.path.isdir(self.cache_dir): - for root, _dirs, fnames in os.walk(self.cache_dir): - cached = [f for f in fnames if f.endswith(".parquet") or f.endswith(".pkl")] - if cached: - files += len(cached) - rel = os.path.relpath(root, self.cache_dir) - periods.add(rel.split(os.sep)[0] if rel != "." else rel) - return files, sorted(periods) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/logging_setup.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/logging_setup.py deleted file mode 100644 index d4736f4..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/logging_setup.py +++ /dev/null @@ -1,171 +0,0 @@ -"""File-based logging for the Big QMT bridge. - -Diagnostics used to be print()-only, which is lost when the QMT output panel -scrolls or the terminal restarts. This module wires Python's stdlib ``logging`` -to a rotating file so errors survive restarts and can be reviewed after a -crash. - -Usage (any module, both server and client): - - from bigqmt_signal_trader.logging_setup import get_logger - log = get_logger("rpc") - log.info("started") - log.error("download failed: %s", exc) - -Behavior: -- Log directory resolves to ``/logs`` when running inside QMT - (found via a sys.path entry ending in ``\\python``), else ``~/.cache/bigqmt/logs``. -- Rotates at midnight into ``bigqmt.log.YYYY-MM-DD`` backups, keeping the last - 7 days by default (override with env BIGQMT_LOG_RETENTION_DAYS). -- Each record is also printed to stdout so the QMT output panel still shows it. -- Thread-safe (logging is; the print side is best-effort wrapped). -- Never raises: a logging failure must not bring down the strategy. -- Opt out via env BIGQMT_LOG_ENABLED=0 / BIGQMT_LOG_TO_STDOUT=0. -""" - -import datetime as _dt -import logging -import logging.handlers -import os -import sys -import time - -_LOGGER_NAME = "bigqmt" -_initialized = False - - -def _env_bool(name, default=True): - value = os.environ.get(name) - if value in (None, ""): - return default - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -def _resolve_log_dir(): - """Pick a writable log dir. Prefers the QMT python dir (the sys.path entry - ending in ``\\python``) so logs sit beside the deployed strategy; falls back - to a user cache dir otherwise. Deliberately does NOT use this package's own - src/ directory (a repo checkout is not a writable runtime location).""" - candidates = [] - for entry in sys.path: - try: - if entry and entry.endswith(r"\python") and os.path.isdir(entry): - candidates.append(entry) - except Exception: - continue - candidates.append(os.path.join(os.path.expanduser("~"), ".cache", "bigqmt")) - for base in candidates: - try: - path = os.path.join(base, "logs") - os.makedirs(path, exist_ok=True) - # probe writability - probe = os.path.join(path, ".write_test") - with open(probe, "w"): - pass - try: - os.remove(probe) - except Exception: - pass - return path - except Exception: - continue - return None - - -class _SafeStreamHandler(logging.Handler): - """print() the record so the QMT output panel shows it; never raises.""" - - def emit(self, record): - try: - print(self.format(record)) - except Exception: - pass - - -def _cleanup_old_logs(log_dir, retention_days): - """Delete rotated log files older than retention_days. - - TimedRotatingFileHandler only prunes backups at rotation time; this sweeps - stale files on startup too (e.g. after a weekend gap or a config change). - """ - try: - cutoff = time.time() - retention_days * 86400 - for name in os.listdir(log_dir): - if not (name.startswith("bigqmt") and name.endswith(".log") or ".log." in name): - continue - path = os.path.join(log_dir, name) - try: - if os.path.getmtime(path) < cutoff: - os.remove(path) - except Exception: - continue - except Exception: - pass - - -def _setup(): - global _initialized - if _initialized: - return - _initialized = True - logger = logging.getLogger(_LOGGER_NAME) - logger.setLevel(logging.DEBUG) - logger.propagate = False - if not _env_bool("BIGQMT_LOG_ENABLED", True): - logger.addHandler(logging.NullHandler()) - return - - fmt = logging.Formatter( - fmt="%(asctime)s [%(levelname)s] [%(name)s] %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - ) - - # File handler: rotate at midnight, keep the last 7 days only. - log_dir = _resolve_log_dir() - if log_dir is not None: - try: - fname = os.path.join(log_dir, "bigqmt.log") - file_handler = logging.handlers.TimedRotatingFileHandler( - fname, - when="midnight", - interval=1, - backupCount=int(os.environ.get("BIGQMT_LOG_RETENTION_DAYS", 7)), - encoding="utf-8", - utc=False, - ) - file_handler.setFormatter(fmt) - logger.addHandler(file_handler) - _cleanup_old_logs(log_dir, int(os.environ.get("BIGQMT_LOG_RETENTION_DAYS", 7))) - except Exception: - pass - - # Stdout handler so the QMT panel still shows logs. - if _env_bool("BIGQMT_LOG_TO_STDOUT", True): - stream = _SafeStreamHandler() - stream.setLevel(logging.INFO) - stream.setFormatter(fmt) - logger.addHandler(stream) - - if not logger.handlers: - logger.addHandler(logging.NullHandler()) - - -def get_logger(name=""): - """Return a module logger under the shared ``bigqmt`` root. - - ``get_logger("rpc")`` -> logger named ``bigqmt.rpc``; the tag is shown in - each log line so the old ``[bigqmt_rpc]`` prefixes remain visible. - """ - _setup() - suffix = str(name or "").strip(".") - full = _LOGGER_NAME if not suffix else "%s.%s" % (_LOGGER_NAME, suffix) - return logging.getLogger(full) - - -def log_file_path(): - """Return the current log file path (or None if file logging is off).""" - _setup() - log_dir = _resolve_log_dir() - if log_dir is None: - return None - return os.path.join(log_dir, "bigqmt.log") diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/models.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/models.py deleted file mode 100644 index b3974f9..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/models.py +++ /dev/null @@ -1,308 +0,0 @@ -"""交易信号、委托请求和账户快照的数据模型。""" - -import datetime as _dt -from enum import Enum -from typing import Any, Dict, List, Optional - - -class SignalAction(str, Enum): - BUY = "BUY" - SELL = "SELL" - CLEAR = "CLEAR" - CANCEL = "CANCEL" - - -class SignalStatus(str, Enum): - PENDING = "PENDING" - CLAIMED = "CLAIMED" - SUBMITTED = "SUBMITTED" - SKIPPED = "SKIPPED" - FAILED = "FAILED" - FILLED = "FILLED" - - -def parse_datetime(value: Any, field_name: str) -> _dt.datetime: - if isinstance(value, _dt.datetime): - return value - if isinstance(value, str) and value: - try: - return _dt.datetime.strptime(value, "%Y-%m-%d %H:%M:%S") - except ValueError as exc: - raise ValueError(f"{field_name} must use format YYYY-MM-DD HH:MM:SS") from exc - raise ValueError(f"{field_name} is required") - - -def _optional_int(value: Any) -> Optional[int]: - if value is None or value == "": - return None - return int(value) - - -def _optional_float(value: Any) -> Optional[float]: - if value is None or value == "": - return None - return float(value) - - -def _bool_value(value: Any) -> bool: - if isinstance(value, bool): - return value - if value is None or value == "": - return False - if isinstance(value, (int, float)): - return bool(value) - text = str(value).strip().lower() - return text in ("1", "true", "yes", "y", "on") - - -class TradeSignal: - def __init__( - self, - signal_id, - account_id, - action, - created_at, - expire_at, - schema_version, - stock_code="", - stock_name="", - amount=None, - percentage=None, - price_type="AUTO_LIMIT", - price=None, - strategy_name="bigqmt_signal_trader", - remark="", - source="", - source_type="auto", - force=False, - bypass_stop_buy=False, - bypass_stop_sell=False, - bypass_daily_limit=False, - status=SignalStatus.PENDING, - raw_payload=None, - ): - self.signal_id = signal_id - self.account_id = account_id - self.action = action - self.created_at = created_at - self.expire_at = expire_at - self.schema_version = schema_version - self.stock_code = stock_code - self.stock_name = stock_name - self.amount = amount - self.percentage = percentage - self.price_type = price_type - self.price = price - self.strategy_name = strategy_name - self.remark = remark - self.source = source - self.source_type = source_type - self.force = force - self.bypass_stop_buy = bypass_stop_buy - self.bypass_stop_sell = bypass_stop_sell - self.bypass_daily_limit = bypass_daily_limit - self.status = status - self.raw_payload = dict(raw_payload or {}) - - @classmethod - def from_dict(cls, payload: Dict[str, Any]) -> "TradeSignal": - required = ("signal_id", "account_id", "action", "created_at", "expire_at", "schema_version") - for field_name in required: - if payload.get(field_name) in (None, ""): - raise ValueError(f"{field_name} is required") - - try: - action = SignalAction(str(payload["action"]).upper()) - except ValueError as exc: - raise ValueError(f"unsupported action: {payload.get('action')}") from exc - - amount = _optional_int(payload.get("amount")) - percentage = _optional_float(payload.get("percentage")) - stock_code = str(payload.get("stock_code") or "").strip().upper() - - if action == SignalAction.BUY: - if not stock_code: - raise ValueError("stock_code is required for BUY") - if amount is None or amount <= 0: - raise ValueError("amount must be positive for BUY") - elif action == SignalAction.SELL: - if not stock_code: - raise ValueError("stock_code is required for SELL") - if amount is None and percentage is None: - raise ValueError("amount or percentage is required for SELL") - elif action == SignalAction.CLEAR and percentage is None: - percentage = 100.0 - - return cls( - signal_id=str(payload["signal_id"]), - account_id=str(payload["account_id"]), - action=action, - stock_code=stock_code, - stock_name=str(payload.get("stock_name") or ""), - amount=amount, - percentage=percentage, - price_type=str(payload.get("price_type") or "AUTO_LIMIT").upper(), - price=_optional_float(payload.get("price")), - strategy_name=str(payload.get("strategy_name") or "bigqmt_signal_trader"), - remark=str(payload.get("remark") or ""), - source=str(payload.get("source") or ""), - source_type=str(payload.get("source_type") or "auto"), - force=_bool_value(payload.get("force", False)), - bypass_stop_buy=_bool_value(payload.get("bypass_stop_buy", False)), - bypass_stop_sell=_bool_value(payload.get("bypass_stop_sell", False)), - bypass_daily_limit=_bool_value(payload.get("bypass_daily_limit", False)), - created_at=parse_datetime(payload.get("created_at"), "created_at"), - expire_at=parse_datetime(payload.get("expire_at"), "expire_at"), - schema_version=int(payload["schema_version"]), - raw_payload=dict(payload), - ) - - def is_expired(self, now: _dt.datetime) -> bool: - return now > self.expire_at - - -class PositionSnapshot: - def __init__( - self, - stock_code, - volume, - available, - cost=0.0, - stock_name="", - market_value=None, - price=None, - open_price=None, - frozen_volume=0, - on_road_volume=0, - yesterday_volume=None, - direction=48, - ): - self.stock_code = stock_code - self.volume = volume - self.available = available - self.cost = cost - self.stock_name = stock_name - self.market_value = market_value - self.price = price - self.open_price = open_price - self.frozen_volume = frozen_volume - self.on_road_volume = on_road_volume - self.yesterday_volume = yesterday_volume - self.direction = direction - - -class AssetSnapshot: - """Account funds, mirroring MiniQMT's ``XtAsset``. - - Field names follow ``xtquant.xttype.XtAsset(account_id, cash, frozen_cash, - market_value, total_asset)`` so ``query_stock_asset`` can hand callers the - same attributes they get from MiniQMT. - - ``cash`` is 可用 (available), NOT the full 资金余额: - ``total_asset == cash + frozen_cash + market_value``. New fields are - appended with None defaults so existing positional callers keep working, - and None means "the terminal did not report it" — distinct from 0.0. - """ - - def __init__(self, account_id, cash=None, total_asset=None, frozen_cash=None, market_value=None): - self.account_id = account_id - self.cash = cash - self.total_asset = total_asset - self.frozen_cash = frozen_cash - self.market_value = market_value - - -class AccountSnapshot: - def __init__(self, account_id, asset, positions, reason, updated_at): - self.account_id = account_id - self.asset = asset - self.positions = positions - self.reason = reason - self.updated_at = updated_at - - -class OrderRequest: - def __init__( - self, - signal_id, - account_id, - action, - stock_code, - volume, - price, - price_type, - strategy_name, - remark="", - ): - self.signal_id = signal_id - self.account_id = account_id - self.action = action - self.stock_code = stock_code - self.volume = volume - self.price = price - self.price_type = price_type - self.strategy_name = strategy_name - self.remark = remark - - -class OrderSubmitResult: - def __init__(self, status, user_order_id, order_sys_id=None, message=""): - self.status = status - self.user_order_id = user_order_id - self.order_sys_id = order_sys_id - self.message = message - - -class OrderSnapshot: - def __init__( - self, - order_sys_id, - user_order_id, - stock_code, - action, - volume, - traded_volume, - status, - price=0.0, - strategy_name="", - remark="", - order_time=0, - ): - self.order_sys_id = order_sys_id - self.user_order_id = user_order_id - self.stock_code = stock_code - self.action = action - self.volume = volume - self.traded_volume = traded_volume - self.status = status - self.price = price - self.strategy_name = strategy_name - self.remark = remark - # 报单时间, Unix 秒 -- MiniQMT XtOrder.order_time 的语义。0 = 未上报。 - # 追加在末尾并给默认值, 保持既有位置参数调用不受影响。 - self.order_time = order_time - - -class TradeSnapshot: - def __init__(self, trade_id, order_sys_id, stock_code, action, volume, price, - traded_at="", user_order_id=""): - self.trade_id = trade_id - self.order_sys_id = order_sys_id - self.stock_code = stock_code - self.action = action - self.volume = volume - self.price = price - self.traded_at = traded_at - self.user_order_id = user_order_id - - -class OrderRef: - def __init__(self, order_sys_id, user_order_id=""): - self.order_sys_id = order_sys_id - self.user_order_id = user_order_id - - -class CancelResult: - def __init__(self, success, message=""): - self.success = success - self.message = message diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/price_engine.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/price_engine.py deleted file mode 100644 index aeda1cd..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/price_engine.py +++ /dev/null @@ -1,53 +0,0 @@ -"""订单价格生成逻辑。""" - -from .code_utils import normalize_stock_code - - -def _price_precision(stock_code): - pure = normalize_stock_code(stock_code).split(".")[0] - return 3 if pure.startswith(("15", "16", "51", "52")) else 2 - - -def _second_level(values): - if isinstance(values, (list, tuple)) and len(values) > 1: - try: - value = float(values[1]) - return value if value > 0 else 0.0 - except (TypeError, ValueError): - return 0.0 - return 0.0 - - -def build_order_price(market_data, stock_code, action, price_type="AUTO_LIMIT", fixed_price=None): - if str(price_type or "AUTO_LIMIT").upper() == "FIX_PRICE": - if fixed_price is None: - raise ValueError("fixed_price is required when price_type is FIX_PRICE") - return float(fixed_price) - - code = normalize_stock_code(stock_code) - ticks = market_data.get_ticks([code]) - tick = ticks.get(code) - if not tick: - raise ValueError(f"missing tick data for {code}") - - last_price = float(tick.get("lastPrice") or 0) - if last_price <= 0: - raise ValueError(f"invalid lastPrice for {code}") - - instrument = market_data.get_instrument(code) - if int(instrument.get("InstrumentStatus") or 0) > 0: - raise ValueError(f"{code} is suspended") - - precision = _price_precision(code) - action_text = str(action).upper() - if action_text == "BUY": - up_stop = float(instrument.get("UpStopPrice") or last_price * 1.1) - calculated = min(round(last_price * 1.002, precision), up_stop) - ask2 = _second_level(tick.get("askPrice")) - return round(ask2, precision) if ask2 and ask2 < calculated else calculated - if action_text == "SELL": - down_stop = float(instrument.get("DownStopPrice") or last_price * 0.9) - calculated = max(round(last_price * 0.998, precision), down_stop) - bid2 = _second_level(tick.get("bidPrice")) - return round(bid2, precision) if bid2 and bid2 > calculated else calculated - raise ValueError(f"unsupported action for price: {action}") diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/qmt_launcher.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/qmt_launcher.py deleted file mode 100644 index 3b6f2f6..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/qmt_launcher.py +++ /dev/null @@ -1,484 +0,0 @@ -# coding: utf-8 -"""Start and stop a Big QMT terminal (issue #45). - -Big QMT has to be restarted most mornings, and the login dialog is the reason -it cannot simply be dropped into a scheduler. Two ways past it: - -* **Passwordless (preferred)** -- ``XtMiniQmt.exe linkMini`` starts MiniQMT - against an existing session with no dialog at all. This is what - ``免密登录qmt.bat`` does. No UI automation, so nothing here depends on a - desktop being visible. -* **Credential entry** -- for the full terminal (``XtItClient.exe``) the dialog - is unavoidable. We drive it with ``win32api.SendMessage`` posted straight to - the window handle, NOT with pyautogui/pywinauto. That distinction is the - answer to the question in issue #45: pyautogui replays physical input at - screen coordinates, so it needs the window focused and the desktop unlocked; - SendMessage delivers to a handle and works on a background -- or locked -- - session, as long as the session still exists (an RDP disconnect is fine, a - full logout is not). - -Everything is scoped to one install directory. A machine here runs several QMT -copies side by side, so an unscoped ``taskkill /im XtItClient.exe`` would take -down someone else's trading session. - -Waits are on observed readiness, never a fixed sleep: startup is complete when -the FormulaServer port accepts a connection, which is also exactly what the -rest of this package needs before it can do anything. - -Windows only. ``psutil`` is used when importable, otherwise we shell out to -``wmic``/``taskkill``. -""" - -import os -import socket -import subprocess -import sys -import time - -from .logging_setup import get_logger - - -log = get_logger("launcher") - -# Processes a QMT install owns. miniquote/BrokerProxy/minibroker are children -# that survive the main window and hold the ports we need to rebind. -QMT_PROCESS_NAMES = ( - "XtItClient.exe", - "XtMiniQmt.exe", - "miniquote.exe", - "BrokerProxy.exe", - "minibroker.exe", -) - -# FormulaServer. Listening means the terminal is far enough up to answer. -DEFAULT_READY_PORT = 58600 - -__all__ = [ - "QmtLauncherError", - "close_qmt", - "find_qmt_processes", - "is_qmt_running", - "open_qmt", - "restart_qmt", - "wait_until_ready", -] - - -class QmtLauncherError(RuntimeError): - """Launching or stopping a QMT terminal failed.""" - - -def _normalize_dir(path): - if not path: - return "" - return os.path.normcase(os.path.normpath(os.path.abspath(str(path)))) - - -def resolve_install_dir(install_dir): - """Accept an install root, its bin.x64, or a path to an exe inside it. - - Returns the normalized ``bin.x64`` directory, which is what process paths - are compared against. - """ - path = str(install_dir or "").strip().strip('"').strip("'") - if not path: - raise QmtLauncherError("install_dir is required (QMT root, bin.x64, or an exe path)") - if os.path.isfile(path) or path.lower().endswith(".exe"): - path = os.path.dirname(path) - normalized = os.path.normpath(os.path.abspath(path)) - if os.path.basename(normalized).lower() != "bin.x64": - candidate = os.path.join(normalized, "bin.x64") - if os.path.isdir(candidate): - normalized = candidate - return normalized - - -# ---------------------------------------------------------------- discovery -def _iter_processes_psutil(): - import psutil - - for proc in psutil.process_iter(["pid", "name", "exe"]): - try: - info = proc.info - yield int(info["pid"]), str(info.get("name") or ""), str(info.get("exe") or "") - except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): - continue - - -def _iter_processes_wmic(): - """psutil-free fallback. wmic still ships on the Windows builds QMT runs on.""" - try: - raw = subprocess.check_output( - ["wmic", "process", "get", "ProcessId,Name,ExecutablePath", "/format:csv"], - stderr=subprocess.STDOUT, - ) - except Exception as exc: - raise QmtLauncherError( - "cannot enumerate processes: psutil is not installed and wmic failed (%s)" % exc - ) - text = raw.decode("utf-8", "replace") if isinstance(raw, bytes) else str(raw) - for row in text.splitlines(): - parts = [p.strip() for p in row.split(",")] - # CSV columns: Node,ExecutablePath,Name,ProcessId - if len(parts) < 4 or parts[3].lower() in ("processid", ""): - continue - try: - pid = int(parts[3]) - except ValueError: - continue - yield pid, parts[2], parts[1] - - -def _iter_processes(): - try: - import psutil # noqa: F401 - except ImportError: - return _iter_processes_wmic() - return _iter_processes_psutil() - - -def find_qmt_processes(install_dir, names=QMT_PROCESS_NAMES): - """Return ``[(pid, name, exe), ...]`` for QMT processes under ``install_dir``. - - A process with no readable exe path is skipped rather than guessed at: on a - machine running several QMT copies, killing by name alone is how you take - down the wrong account. - """ - target = _normalize_dir(resolve_install_dir(install_dir)) - wanted = set(str(n).lower() for n in names) - found = [] - for pid, name, exe in _iter_processes(): - if name.lower() not in wanted or not exe: - continue - if _normalize_dir(os.path.dirname(exe)) == target: - found.append((pid, name, exe)) - return found - - -def is_qmt_running(install_dir): - return bool(find_qmt_processes(install_dir)) - - -# ------------------------------------------------------------------ readiness -def port_is_listening(port=DEFAULT_READY_PORT, host="127.0.0.1", timeout=1.0): - sock = socket.socket() - sock.settimeout(timeout) - try: - sock.connect((host, port)) - return True - except Exception: - return False - finally: - try: - sock.close() - except Exception: - pass - - -def wait_until_ready(port=DEFAULT_READY_PORT, host="127.0.0.1", timeout_seconds=180.0, - poll_interval=2.0): - """Block until ``port`` accepts a connection. Returns seconds waited. - - Raises :class:`QmtLauncherError` on timeout rather than returning False, so - a scheduled restart fails loudly instead of letting the next step run - against a terminal that never came up. - """ - deadline = time.time() + float(timeout_seconds) - started = time.time() - while time.time() < deadline: - if port_is_listening(port, host): - waited = time.time() - started - log.info("qmt ready after %.1fs (%s:%d listening)", waited, host, port) - return waited - time.sleep(poll_interval) - raise QmtLauncherError( - "QMT did not become ready within %.0fs (%s:%d never listened)" - % (timeout_seconds, host, port) - ) - - -def wait_until_stopped(install_dir, timeout_seconds=60.0, poll_interval=1.0): - deadline = time.time() + float(timeout_seconds) - while time.time() < deadline: - if not find_qmt_processes(install_dir): - return True - time.sleep(poll_interval) - return False - - -# --------------------------------------------------------------------- close -def _terminate(pid, force=False): - try: - import psutil - - proc = psutil.Process(pid) - if force: - proc.kill() - else: - proc.terminate() - return True - except ImportError: - pass - except Exception: - return False - cmd = ["taskkill", "/pid", str(pid)] - if force: - cmd.append("/f") - try: - subprocess.check_output(cmd, stderr=subprocess.STDOUT) - return True - except Exception: - return False - - -def close_qmt(install_dir, timeout_seconds=60.0, force_after_seconds=20.0): - """Stop every QMT process under ``install_dir``. Returns how many were stopped. - - Asks politely first: the terminal flushes local data on a clean exit, and - killing it outright is how the K-line store ends up truncated. Escalates to - a hard kill only after ``force_after_seconds``. - """ - targets = find_qmt_processes(install_dir) - if not targets: - log.info("no QMT process under %s; nothing to close", install_dir) - return 0 - - for pid, name, _exe in targets: - log.info("closing %s (pid=%s)", name, pid) - _terminate(pid, force=False) - - if wait_until_stopped(install_dir, timeout_seconds=force_after_seconds): - log.info("closed %d process(es) cleanly", len(targets)) - return len(targets) - - remaining = find_qmt_processes(install_dir) - log.warning("%d process(es) still alive after %.0fs; forcing", - len(remaining), force_after_seconds) - for pid, name, _exe in remaining: - _terminate(pid, force=True) - - grace = max(timeout_seconds - force_after_seconds, 5.0) - if not wait_until_stopped(install_dir, timeout_seconds=grace): - still = find_qmt_processes(install_dir) - raise QmtLauncherError( - "could not stop: %s" % ", ".join("%s(pid=%s)" % (n, p) for p, n, _ in still) - ) - return len(targets) - - -# ---------------------------------------------------------------------- open -def _spawn(command, cwd=None, shell=False): - log.info("launching: %s", command if isinstance(command, str) else " ".join(command)) - kwargs = {"cwd": cwd, "shell": shell, - "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL} - if os.name == "nt": - # Detach so the terminal outlives this process -- otherwise a scheduled - # task exiting takes QMT with it. - detached = getattr(subprocess, "DETACHED_PROCESS", 0x00000008) - new_group = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0x00000200) - kwargs["creationflags"] = detached | new_group - return subprocess.Popen(command, **kwargs) - - -def open_qmt(install_dir, mode="auto", bat_path=None, exe_name=None, - ready_port=DEFAULT_READY_PORT, ready_timeout_seconds=180.0, - wait_ready=True, credentials=None, window_title_prefix=None): - """Start a QMT terminal under ``install_dir`` and wait until it answers. - - ``mode``: - ``"linkmini"`` -- ``XtMiniQmt.exe linkMini``, no login dialog. - ``"bat"`` -- run ``bat_path`` (e.g. 免密登录qmt.bat). - ``"exe"`` -- start ``exe_name`` (default XtItClient.exe) as-is; use - when the terminal restores its own session. - ``"login"`` -- start the exe, then type credentials into the dialog. - ``"auto"`` -- bat if given, else linkmini if XtMiniQmt.exe exists, - else exe. - - ``credentials`` (mode="login") is ``{"user": ..., "password": ...}``. Pass - it from your local config or environment; never hardcode it, and note the - values are typed into a window, so anything that can read that window can - read them. - """ - bin_dir = resolve_install_dir(install_dir) - if not os.path.isdir(bin_dir): - raise QmtLauncherError("no such directory: %s" % bin_dir) - - mode = str(mode or "auto").lower() - if mode == "auto": - if bat_path: - mode = "bat" - elif os.path.isfile(os.path.join(bin_dir, "XtMiniQmt.exe")): - mode = "linkmini" - else: - mode = "exe" - - if mode == "bat": - if not bat_path or not os.path.isfile(bat_path): - raise QmtLauncherError("bat_path is required for mode='bat': %r" % bat_path) - _spawn([bat_path], cwd=os.path.dirname(bat_path), shell=True) - elif mode == "linkmini": - exe = os.path.join(bin_dir, "XtMiniQmt.exe") - if not os.path.isfile(exe): - raise QmtLauncherError("XtMiniQmt.exe not found in %s" % bin_dir) - _spawn([exe, "linkMini"], cwd=bin_dir) - elif mode in ("exe", "login"): - exe = os.path.join(bin_dir, str(exe_name or "XtItClient.exe")) - if not os.path.isfile(exe): - raise QmtLauncherError("%s not found in %s" % (os.path.basename(exe), bin_dir)) - _spawn([exe], cwd=bin_dir) - if mode == "login": - _login_via_window(credentials or {}, window_title_prefix) - else: - raise QmtLauncherError("unknown mode %r (bat/linkmini/exe/login/auto)" % mode) - - if not wait_ready: - return 0.0 - return wait_until_ready(ready_port, timeout_seconds=ready_timeout_seconds) - - -def _login_via_window(credentials, window_title_prefix=None, appear_timeout_seconds=90.0): - """Type credentials into the QMT login dialog via SendMessage. - - Matches the window by title PREFIX. The reference implementation pinned the - full title including a build number ("国金证券QMT交易端 1.0.0.29456"), which - stops finding the window on the next terminal update. - """ - user = str(credentials.get("user") or credentials.get("account") or "") - password = str(credentials.get("password") or "") - if not user or not password: - raise QmtLauncherError( - "mode='login' needs credentials={'user':..., 'password':...}" - ) - try: - import win32api - import win32con - import win32gui - except ImportError: - raise QmtLauncherError( - "mode='login' needs pywin32 (pip install pywin32); " - "prefer mode='linkmini' or mode='bat', which need no UI automation" - ) - - prefix = str(window_title_prefix or "QMT") - - def _collect(hwnd, acc): - if not win32gui.IsWindowVisible(hwnd): - return - title = win32gui.GetWindowText(hwnd) or "" - if title.strip().startswith(prefix): - acc.append(hwnd) - - def _find(): - matches = [] - win32gui.EnumWindows(_collect, matches) - return matches[0] if matches else None - - deadline = time.time() + appear_timeout_seconds - handle = None - while time.time() < deadline: - handle = _find() - if handle: - break - time.sleep(2.0) - if not handle: - raise QmtLauncherError( - "login window starting with %r did not appear within %.0fs" - % (prefix, appear_timeout_seconds) - ) - - def _send_text(text): - for ch in str(text): - win32api.SendMessage(handle, win32con.WM_KEYDOWN, ord(ch), 0) - win32api.SendMessage(handle, win32con.WM_KEYUP, ord(ch), 0) - time.sleep(0.2) - - def _send_enter(): - win32api.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) - win32api.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0) - time.sleep(1.0) - - # Never log the values themselves. - log.info("entering credentials into window %r", prefix) - _send_text(user) - _send_enter() - _send_text(password) - _send_enter() - _send_enter() - - -def restart_qmt(install_dir, settle_seconds=5.0, **open_kwargs): - """Close, wait for the ports to be released, then start again. - - ``settle_seconds`` matters: the FormulaServer and RPC sockets linger briefly - after the process dies, and the ZMQ transport binds its configured port - exactly (no scanning), so restarting too eagerly fails the rebind. - """ - closed = close_qmt(install_dir) - if closed: - time.sleep(settle_seconds) - waited = open_qmt(install_dir, **open_kwargs) - log.info("restart complete (closed=%d, ready in %.1fs)", closed, waited) - return waited - - -# ----------------------------------------------------------------------- CLI -def main(argv=None): - import argparse - - parser = argparse.ArgumentParser( - prog="python -m bigqmt_signal_trader.qmt_launcher", - description="Start/stop a Big QMT terminal, scoped to one install directory.", - ) - parser.add_argument("action", choices=("open", "close", "restart", "status")) - parser.add_argument("--dir", required=True, - help="QMT root, its bin.x64, or a path to an exe inside it") - parser.add_argument("--mode", default="auto", - choices=("auto", "bat", "linkmini", "exe", "login")) - parser.add_argument("--bat", default=None, help="batch file for --mode bat") - parser.add_argument("--exe", default=None, help="exe name for --mode exe/login") - parser.add_argument("--port", type=int, default=DEFAULT_READY_PORT) - parser.add_argument("--timeout", type=float, default=180.0) - parser.add_argument("--no-wait", action="store_true") - parser.add_argument("--title-prefix", default=None, - help="login window title prefix (--mode login)") - args = parser.parse_args(argv) - - if args.action == "status": - procs = find_qmt_processes(args.dir) - if not procs: - print("not running (%s)" % resolve_install_dir(args.dir)) - return 1 - for pid, name, exe in procs: - print("%-16s pid=%-8s %s" % (name, pid, exe)) - print("ready port %d: %s" % ( - args.port, "listening" if port_is_listening(args.port) else "not listening")) - return 0 - - credentials = None - if args.mode == "login": - # Read from the environment so a password never reaches argv, where it - # would be visible to any process listing. - credentials = {"user": os.environ.get("BIGQMT_LOGIN_USER", ""), - "password": os.environ.get("BIGQMT_LOGIN_PASSWORD", "")} - - try: - if args.action == "close": - print("closed %d process(es)" % close_qmt(args.dir)) - else: - kwargs = dict(mode=args.mode, bat_path=args.bat, exe_name=args.exe, - ready_port=args.port, ready_timeout_seconds=args.timeout, - wait_ready=not args.no_wait, credentials=credentials, - window_title_prefix=args.title_prefix) - if args.action == "restart": - restart_qmt(args.dir, **kwargs) - else: - open_qmt(args.dir, **kwargs) - print("ok") - except QmtLauncherError as exc: - print("error: %s" % exc, file=sys.stderr) - return 2 - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_push_channel.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_push_channel.py deleted file mode 100644 index 604f36f..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_push_channel.py +++ /dev/null @@ -1,270 +0,0 @@ -"""Server→client whole-quote push channel. - -The RPC transport is request/response only; whole-quote data needs the opposite -direction — the server pushes each incremental tick batch to every client -subscribed to that combination. This module provides one abstract channel with -two interchangeable implementations: - -* :class:`ZmqQuotePushChannel` — a ``PUB`` socket on the server, a ``SUB`` socket - per client. Native to no-redis deployments. Fire-and-forget: a client that is - down simply misses frames (acceptable for incremental quote pushes). -* :class:`RedisQuotePushChannel` — redis ``publish``/``subscribe`` on a - per-account, per-combination channel, for redis deployments. - -Wire encoding is msgpack when available (smaller + faster for the -``{code: {field: number}}`` payload shape), falling back to stdlib json so the -channel stays usable without the optional dependency. -""" - -import json -import threading - -try: - import msgpack - - _HAS_MSGPACK = True -except Exception: # pragma: no cover - depends on optional dependency - msgpack = None - _HAS_MSGPACK = False - - -def encode_push_payload(payload): - """Encode a push payload dict to bytes (msgpack preferred, json fallback).""" - if _HAS_MSGPACK: - return msgpack.packb(payload, use_bin_type=True) - return json.dumps(payload, ensure_ascii=False, default=str).encode("utf-8") - - -def decode_push_payload(blob): - """Inverse of :func:`encode_push_payload`. Accepts bytes or str. - - Encoding is not symmetric across deployments: a server without msgpack - falls back to json while a client with msgpack installed decodes with - msgpack — ``msgpack.unpackb`` then raises ``ExtraData`` on the json text - (its first byte ``{`` parses as an int, leaving trailing bytes). So try - msgpack first, and fall back to json when the bytes are not a single - valid msgpack object. - """ - if blob is None: - return None - if isinstance(blob, str): - blob = blob.encode("utf-8") - if _HAS_MSGPACK: - try: - return msgpack.unpackb(blob, raw=False) - except Exception: - pass - return json.loads(blob.decode("utf-8")) - - -class QuotePushChannel(object): - """Abstract push channel. Server side: ``start_publisher`` + ``publish``. - Client side: ``start_subscriber(topics, on_msg)``. A single instance may act - as publisher or subscriber depending on which start method is called.""" - - def start_publisher(self): - raise NotImplementedError - - def start_subscriber(self, topics, on_msg): - raise NotImplementedError - - def publish(self, topic, data): - raise NotImplementedError - - def stop(self): - raise NotImplementedError - - -class ZmqQuotePushChannel(QuotePushChannel): - def __init__(self, bind_address=None, connect_address=None, context=None, print_prefix="[bigqmt_quote_push]"): - self.bind_address = bind_address - self.connect_address = connect_address - self.print_prefix = print_prefix - self._zmq = None - self._context = context - self._pub = None - self._pub_lock = threading.Lock() - self._sub = None - self._sub_thread = None - self._running = False - - def _ensure_context(self): - if self._zmq is None: - import zmq - - self._zmq = zmq - if self._context is None: - self._context = zmq.Context.instance() - return self._zmq, self._context - - # -- server side --------------------------------------------------------- - def start_publisher(self): - zmq, ctx = self._ensure_context() - if not self.bind_address: - raise ValueError("bind_address is required to start a publisher") - self._pub = ctx.socket(zmq.PUB) - self._pub.bind(self.bind_address) - self._running = True - - def publish(self, topic, data): - payload = encode_push_payload({"combo_key": topic, "data": data}) - frame = [str(topic).encode("utf-8"), payload] - # PUB socket is not thread-safe; serialize under the lock and read the - # socket inside it so a concurrent stop() (which nulls _pub) can't hand - # us a closed socket. - with self._pub_lock: - pub = self._pub - if pub is None: - return - try: - pub.send_multipart(frame) - except Exception as exc: - print("%s zmq publish failed: %s" % (self.print_prefix, exc)) - - # -- client side --------------------------------------------------------- - def start_subscriber(self, topics, on_msg): - zmq, ctx = self._ensure_context() - if not self.connect_address: - raise ValueError("connect_address is required to start a subscriber") - sub = ctx.socket(zmq.SUB) - sub.connect(self.connect_address) - for topic in topics or []: - sub.setsockopt(zmq.SUBSCRIBE, str(topic).encode("utf-8")) - self._sub = sub - self._running = True - self._sub_thread = threading.Thread( - target=self._sub_loop, args=(sub, on_msg), name="bigqmt-quote-push-sub", daemon=True - ) - self._sub_thread.start() - - def _sub_loop(self, sub, on_msg): - # The SUB socket is owned by THIS thread; it must be closed HERE (in a - # finally) and never from another thread. Closing a ZMQ socket cross- - # thread trips a Windows signaler assertion and aborts the whole QMT - # process (the "auto-exit" users hit). - poller = self._zmq.Poller() - poller.register(sub, self._zmq.POLLIN) - try: - while self._running: - try: - events = dict(poller.poll(200)) - except Exception: - break - if sub not in events: - continue - try: - frames = sub.recv_multipart(self._zmq.NOBLOCK) - except Exception: - continue - if len(frames) < 2: - continue - topic = frames[0].decode("utf-8", errors="ignore") - data = decode_push_payload(frames[-1]) - payload_data = data.get("data") if isinstance(data, dict) else data - try: - on_msg(topic, payload_data) - except Exception as exc: - print("%s subscriber callback failed: %s" % (self.print_prefix, exc)) - finally: - try: - sub.close(linger=0) - except Exception: - pass - - def stop(self): - # Signal the sub thread to exit and let IT close its own socket (see - # _sub_loop). Closing the SUB socket from this (foreign) thread would - # trip the Windows ZMQ signaler abort and crash QMT. - self._running = False - thread = self._sub_thread - if thread is not None and thread.is_alive(): - thread.join(1.0) - self._sub_thread = None - self._sub = None - # The PUB socket is only touched by publisher threads under _pub_lock; - # null it first so a racing publish() sees None and bails, then close. - with self._pub_lock: - pub = self._pub - self._pub = None - if pub is not None: - try: - pub.close(linger=0) - except Exception: - pass - - -class RedisQuotePushChannel(QuotePushChannel): - def __init__(self, redis_client, account_id="", channel_template="bigqmt:quote_push:{account_id}:{topic}", print_prefix="[bigqmt_quote_push]"): - self.redis = redis_client - self.account_id = str(account_id or "") - self.channel_template = channel_template - self.print_prefix = print_prefix - self._running = False - self._pubsub = None - self._thread = None - - def _channel(self, topic): - return self.channel_template.format(account_id=self.account_id, topic=topic) - - # -- server side --------------------------------------------------------- - def start_publisher(self): - # Redis publish needs no setup; present for interface symmetry. - self._running = True - - def publish(self, topic, data): - payload = encode_push_payload({"combo_key": topic, "data": data}) - try: - self.redis.publish(self._channel(topic), payload) - except Exception as exc: - print("%s redis publish failed: %s" % (self.print_prefix, exc)) - - # -- client side --------------------------------------------------------- - def start_subscriber(self, topics, on_msg): - self._running = True - self._thread = threading.Thread( - target=self._sub_loop, args=(list(topics or []), on_msg), name="bigqmt-quote-push-sub", daemon=True - ) - self._thread.start() - - def _sub_loop(self, topics, on_msg): - # The pubsub connection is owned by THIS thread and closed HERE so a - # concurrent stop() can't close it out from under us. - pubsub = self.redis.pubsub(ignore_subscribe_messages=True) - self._pubsub = pubsub - channels = [self._channel(topic) for topic in topics] - try: - pubsub.subscribe(*channels) - except Exception as exc: - print("%s redis subscribe failed: %s" % (self.print_prefix, exc)) - return - try: - while self._running: - try: - message = pubsub.get_message(timeout=0.2) - except Exception: - break - if not message or message.get("type") != "message": - continue - channel = message.get("channel") - if isinstance(channel, bytes): - channel = channel.decode("utf-8", errors="ignore") - topic = str(channel).rsplit(":", 1)[-1] - data = decode_push_payload(message.get("data")) - payload_data = data.get("data") if isinstance(data, dict) else data - try: - on_msg(topic, payload_data) - except Exception as exc: - print("%s subscriber callback failed: %s" % (self.print_prefix, exc)) - finally: - try: - pubsub.close() - except Exception: - pass - - def stop(self): - self._running = False - thread = self._thread - if thread is not None and thread.is_alive(): - thread.join(1.0) - self._thread = None - self._pubsub = None diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_subscription_manager.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_subscription_manager.py deleted file mode 100644 index 1e9d690..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/quote_subscription_manager.py +++ /dev/null @@ -1,258 +0,0 @@ -"""Reference-counted whole-quote subscription manager (server side). - -One big-QMT ``ContextInfo.subscribe_whole_quote`` subscription is shared by every -client that asked for the same (normalized) code combination. The big-QMT -subscription is only created for the first client of a combination and only torn -down after the last client either unsubscribes or goes silent (keepalive timeout). - -The manager talks to big QMT exclusively through a :class:`QuoteSourceAdapter`; -it never touches ``ContextInfo`` directly so the real-environment wiring (method -names / handle shape) stays isolated to the adapter. - -Threading: ``subscribe``/``unsubscribe``/``keepalive`` run on the RPC thread, -``reap_expired`` on the scheduler thread and ``on_push`` on big QMT's quote -thread. Shared state is guarded by one re-entrant lock; calls out to the quote -source and to the push publisher happen OUTSIDE the lock so a slow/blocking -publish never stalls quote-thread state, and no callback can deadlock. -""" - -import threading - - -def combo_key(code_list): - """Normalize a code list into an order-independent combination key. - - Uppercases, strips whitespace, drops empties and duplicates, sorts. So - ``["SH","SZ"]``, ``["sz","sh"]`` and ``["SH","SH","SZ"]`` all map to - ``"SH,SZ"`` and share one big-QMT subscription. - """ - normalized = {str(code).strip().upper() for code in (code_list or []) if str(code or "").strip()} - return ",".join(sorted(normalized)) - - -class QuoteSourceAdapter(object): - """Big-QMT whole-quote source. ContextInfo-backed implementation lives in the - server runtime; tests substitute a fake. ``subscribe`` must return a handle - usable by ``unsubscribe``.""" - - def subscribe(self, codes, on_push): - raise NotImplementedError - - def unsubscribe(self, handle): - raise NotImplementedError - - -class ContextInfoQuoteSource(QuoteSourceAdapter): - """Real big-QMT source backed by the strategy's ``ContextInfo``. - - Verified against the real environment: ``ContextInfo.subscribe_whole_quote( - code_list, callback)`` returns an int subscription id (``< 0`` on failure) and - pushes INCREMENTAL ``{code: tick}`` batches on a dedicated quote thread; - ``ContextInfo.unsubscribe_quote(sub_id)`` cancels it. - """ - - def __init__(self, context_info): - self._context = context_info - - def subscribe(self, codes, on_push): - sub_id = self._context.subscribe_whole_quote(list(codes), callback=on_push) - if sub_id is None or int(sub_id) < 0: - raise RuntimeError("ContextInfo.subscribe_whole_quote failed for codes=%s" % (list(codes),)) - return int(sub_id) - - def unsubscribe(self, handle): - try: - self._context.unsubscribe_quote(handle) - except Exception: - pass - - -class _Combo(object): - __slots__ = ("key", "codes", "handle", "topic", "clients") - - def __init__(self, key, codes, handle, topic): - self.key = key - self.codes = codes - self.handle = handle - self.topic = topic - # (client_id, sub_id) -> last_seen. Sub-id granularity: one client may - # hold several subscriptions to the same combination, and each one keeps - # the shared big-QMT subscription alive independently. - self.clients = {} # (client_id, sub_id) -> last_seen timestamp - - -class QuoteSubscriptionManager(object): - def __init__(self, source, heartbeat_timeout_seconds=30.0, time_func=None, on_push_publisher=None, push_endpoint=""): - self._source = source - self._heartbeat_timeout = float(heartbeat_timeout_seconds) - self._now = time_func or _monotonic - # Optional callable(topic, data) invoked when big QMT pushes a tick batch. - # Wired to the QuotePushChannel in a later stage; None keeps dispatch inert. - self._on_push_publisher = on_push_publisher - # Advertised to clients in subscribe responses so a zmq subscriber knows - # where to connect (redis subscribers derive the channel locally instead). - self._push_endpoint = str(push_endpoint or "") - self._lock = threading.RLock() - self._combos = {} # combo_key -> _Combo - self._sub_index = {} # (client_id, sub_id) -> combo_key - - # -- subscription lifecycle --------------------------------------------- - def subscribe(self, client_id, sub_id, code_list): - """Register (client_id, sub_id) against its combination; create the shared - big-QMT subscription on first use. Idempotent for replayed subscribes.""" - client_id = str(client_id or "") - sub_id = str(sub_id or "") - key = combo_key(code_list) - now = self._now() - - with self._lock: - combo = self._combos.get(key) - if combo is None: - codes = sorted({str(c).strip().upper() for c in (code_list or []) if str(c or "").strip()}) - # source.subscribe registers the on_push callback with big QMT; it - # does not call back into the manager, so it is safe under the lock. - handle = self._source.subscribe(codes, self._make_on_push(key)) - combo = _Combo(key, codes, handle, key) - self._combos[key] = combo - - combo.clients[(client_id, sub_id)] = now - self._sub_index[(client_id, sub_id)] = key - return {"combo_key": key, "topic": combo.topic, "push_endpoint": self._push_endpoint} - - def unsubscribe(self, client_id, sub_id): - """Drop (client_id, sub_id); tear the big-QMT subscription down when the - last subscription of the combination leaves. Unknown sub_ids are a no-op.""" - client_id = str(client_id or "") - sub_id = str(sub_id or "") - with self._lock: - key = self._sub_index.pop((client_id, sub_id), None) - if key is None: - return - handle_to_close = self._remove_subscription_locked(key, client_id, sub_id) - self._close_source(handle_to_close) - - def keepalive(self, client_id, sub_id): - """Refresh last_seen for (client_id, sub_id). Unknown sub_ids are a no-op.""" - client_id = str(client_id or "") - key = self._sub_index.get((client_id, str(sub_id or ""))) - if key is None: - return - with self._lock: - combo = self._combos.get(key) - if combo is None: - return - combo.clients[(client_id, str(sub_id or ""))] = self._now() - - # -- reaper --------------------------------------------------------------- - def reap_expired(self, now=None): - """Remove subscriptions silent for longer than the keepalive timeout; - tear down combos that end up empty. Returns the number reaped.""" - now = self._now() if now is None else now - reaped = 0 - handles_to_close = [] - with self._lock: - for key in list(self._combos.keys()): - combo = self._combos.get(key) - if combo is None: - continue - for (client_id, sub_id), last_seen in list(combo.clients.items()): - if now - last_seen > self._heartbeat_timeout: - self._sub_index.pop((client_id, sub_id), None) - handle = self._remove_subscription_locked(key, client_id, sub_id) - if handle is not None: - handles_to_close.append(handle) - reaped += 1 - for handle in handles_to_close: - self._close_source(handle) - return reaped - - # -- internals ------------------------------------------------------------- - def _make_on_push(self, key): - def on_push(data): - publisher = self._on_push_publisher - if publisher is None: - return - with self._lock: - combo = self._combos.get(key) - topic = combo.topic if combo is not None else None - if topic is None: - return - # Publish outside the lock: it is network IO and must not stall the - # quote thread or block reaper/RPC threads waiting on the lock. - publisher(topic, data) - - return on_push - - def _remove_subscription_locked(self, key, client_id, sub_id): - """Remove one (client_id, sub_id) from a combo. If the combo has no - subscriptions left, detach it and return its source handle for the - caller to close OUTSIDE the lock; else return None. Caller must hold - the lock.""" - combo = self._combos.get(key) - if combo is None: - return None - combo.clients.pop((client_id, sub_id), None) - if combo.clients: - return None - self._combos.pop(key, None) - return combo.handle - - def _close_source(self, handle): - if handle is None: - return - try: - self._source.unsubscribe(handle) - except Exception: - pass - - -def _monotonic(): - import time - - return time.monotonic() - - -def build_quote_subscription_service( - context_info, - transport_name="redis", - account_id="", - redis_client=None, - zmq_bind_address=None, - enabled=True, - heartbeat_timeout_seconds=30.0, - time_func=None, -): - """Assemble the server-side whole-quote service: a ContextInfo-backed source, - a push channel matching the RPC transport, and a QuoteSubscriptionManager - wired so big-QMT pushes publish to the channel. Returns ``(manager, channel)`` - or ``None`` when disabled. The caller starts the channel publisher and feeds - ``manager.reap_expired`` from the scheduler loop.""" - if not enabled: - return None - from .quote_push_channel import RedisQuotePushChannel, ZmqQuotePushChannel - - source = ContextInfoQuoteSource(context_info) - transport_name = str(transport_name or "redis").lower() - if transport_name == "zmq": - bind_address = zmq_bind_address or _default_quote_push_zmq_bind(account_id) - channel = ZmqQuotePushChannel(bind_address=bind_address) - push_endpoint = bind_address - else: - channel = RedisQuotePushChannel(redis_client, account_id=account_id) - push_endpoint = "" - manager = QuoteSubscriptionManager( - source, - heartbeat_timeout_seconds=heartbeat_timeout_seconds, - time_func=time_func, - on_push_publisher=channel.publish, - push_endpoint=push_endpoint, - ) - return manager, channel - - -def _default_quote_push_zmq_bind(account_id): - """Default server PUB bind address: loopback, RPC zmq port + 1 (client side - derives the same host/port + 1 to connect).""" - from .transports.zmq_transport import _default_zmq_port - - return "tcp://0.0.0.0:%d" % (_default_zmq_port(account_id) + 1) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/redis_rpc.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/redis_rpc.py deleted file mode 100644 index 95c5fbf..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/redis_rpc.py +++ /dev/null @@ -1,1719 +0,0 @@ -"""Redis Pub/Sub RPC for the Big QMT runtime. - -By default the service can process selected requests directly in the Redis -listener thread. The in-memory queue and ``drain_pending`` are kept as a -runtime fallback for environments where a QMT API must run from a strategy -callback thread. -""" - -import base64 -import datetime as _dt -import json -import math -import queue -import threading -import time -import traceback -import uuid - -from .adapters.redis_common import decode_text -from .code_utils import normalize_stock_code -from .models import AccountSnapshot, OrderRef, OrderRequest - - -# time.monotonic: unaffected by wall-clock jumps, so a settle deadline -# survives an NTP correction mid-session. Python 3.3+, fine on QMT's 3.6. -_monotonic = time.monotonic - - -RPC_REVISION = "20260715-execution-snapshot-v1" - - -READ_METHODS = { - "ping", - "get_ticks", - "get_instrument", - "get_instrument_type", - "get_market_data", - "get_market_data_ex", - "get_local_data", - "get_stock_list_in_sector", - "get_sector_list", - "get_sector_info", - "get_markets", - "get_market_last_trade_date", - "get_divid_factors", - "download_history_data", - "download_history_data2", - "get_trading_dates", - "get_holidays", - "download_holiday_data", - "get_ipo_info", - "get_etf_info", - "download_etf_info", - "get_option_list", - "get_his_option_list", - "get_his_option_list_batch", - "get_financial_data", - "download_financial_data", - "download_financial_data2", - "call_formula", - "subscribe_formula", - "unsubscribe_formula", - "get_formula_result", - "gen_factor_index", - "get_positions", - "get_asset", - "query_orders", - "query_trades", - "query_execution_snapshot", - "query_stock_position", - "sync_positions", - "submit_download_history_data", - "submit_download_history_data2", - "get_download_status", - "wait_download", - # 账户 / 融资融券 / 交易扩展查询(官方全局函数 + detail types) - "query_account_infos", - "query_account_status", - "query_credit_detail", - "query_stk_compacts", - "query_credit_subjects", - "query_credit_slo_code", - "query_credit_assure", - "query_appointment_info", - "query_smt_secu_info", - "query_smt_secu_rate", - "smt_appointment", - # 官方交易查询函数(直接暴露,运行时注入的全局函数) - "get_value_by_order_id", - "get_last_order_id", - "get_ipo_data", - "get_new_purchase_limit", - "get_history_trade_detail_data", - "get_assure_contract", - "get_enable_short_contract", - "get_unclosed_compacts", - "get_closed_compacts", - "get_debt_contract", - "get_option_subject_position", - "get_comb_option", - "get_hkt_exchange_rate", -} - -ORDER_METHODS = { - "submit_order", - "submit_orders_batch", - "cancel_order", -} - -# Whole-quote push subscription control methods. These drive a server-side -# QuoteSubscriptionManager (reference-counted ContextInfo.subscribe_whole_quote) -# rather than a market_data read; the data itself flows over the push channel. -QUOTE_SUBSCRIPTION_METHODS = { - "subscribe_whole_quote", - "unsubscribe_whole_quote", - "quote_keepalive", -} - -LISTENER_DEFERRED_METHODS = { - "sync_positions", - # Trade-context queries route through QMT's get_trade_detail_data, which - # returns EMPTY when called from the background RPC thread (it needs the main - # strategy thread's context). Defer them so the adjust drain runs them on the - # main thread -- costs up to one adjust interval (~500ms) but returns real - # data. Asset queries use the same QMT detail API and must follow this rule. - "get_asset", - "get_positions", - "query_stock_position", - "query_orders", - "query_trades", - "query_account_infos", - "query_account_status", - "query_credit_detail", - "query_stk_compacts", - "query_credit_subjects", - "query_credit_slo_code", - "query_credit_assure", - "query_appointment_info", - "query_smt_secu_info", - "query_smt_secu_rate", - "get_value_by_order_id", - "get_last_order_id", - "get_history_trade_detail_data", -} - -# Trade-context queries route through QMT's get_trade_detail_data, which -# returns EMPTY when called from the background RPC thread (it needs the main -# strategy thread's context). Defer them so the adjust drain runs them on the -# main thread -- costs up to one adjust interval (~500ms) but returns real -# data. Asset queries use the same QMT detail API and must follow this rule. -# -# NOTE: do NOT blanket-defer all READ_METHODS here. Market-data reads -# (get_full_tick, get_market_data, ...) are thread-safe in the embedded -# terminal and must stay inline for low latency; the ZMQ transport has no -# adjust-driven drain for pending requests (its drain_request_queue is a -# no-op when the router thread exists), so deferring everything would stall -# them forever. Only the trade-context methods listed above go through drain. - - -METHOD_ALIASES = { - "get_full_tick": "get_ticks", - "get_instrument_detail": "get_instrument", - "get_instrumentdetail": "get_instrument", - "getDividFactors": "get_divid_factors", - "query_stock_asset": "get_asset", - "query_stock_positions": "get_positions", - "query_stock_orders": "query_orders", - "query_stock_trades": "query_trades", - "order_stock": "submit_order", - "order_stock_async": "submit_order", - "order_stock_batch": "submit_orders_batch", - "cancel_order_stock": "cancel_order", - "cancel_order_stock_sysid": "cancel_order", -} - -BUY_ORDER_TYPES = {"23", "STOCK_BUY", "BUY", "B"} -SELL_ORDER_TYPES = {"24", "STOCK_SELL", "SELL", "S"} -CANCELABLE_ORDER_STATUSES = {"50", "55"} -SAFE_B64_PREFIX = "b64s:" -SAFE_B64_DIGIT_ENCODE = str.maketrans("0123456789", "!#$%&()*~?") -SAFE_B64_DIGIT_DECODE = str.maketrans("!#$%&()*~?", "0123456789") -MARKET_DATA_METHODS = { - "get_instrument_type", - "get_market_data", - "get_market_data_ex", - "get_local_data", - "get_stock_list_in_sector", - "get_sector_list", - "get_sector_info", - "get_markets", - "get_market_last_trade_date", - "get_divid_factors", - "download_history_data", - "download_history_data2", - "get_trading_dates", - "get_holidays", - "download_holiday_data", - "get_ipo_info", - "get_etf_info", - "download_etf_info", - "get_option_list", - "get_his_option_list", - "get_his_option_list_batch", - "get_financial_data", - "download_financial_data", - "download_financial_data2", - "call_formula", - "subscribe_formula", - "unsubscribe_formula", - "get_formula_result", - "gen_factor_index", - # 龙虎榜 / 股东 / 换手率 / 行业 / 收盘价 - "get_longhubang", - "get_top10_share_holder", - "get_holder_num", - "get_turnover_rate", - "get_industry", - "get_close_price", - # 期权定价 / 隐含波动率 - "bsm_price", - "bsm_iv", - "get_option_iv", - "get_option_detail_data", - "get_option_undl_data", - "get_option_undl", - # 财务扩展 / 因子 - "get_raw_financial_data", - "get_factor_data", - # 历史 ST / 指数权重 - "get_his_st_data", - "get_his_index_data", - # 期货 / 合约 - "get_main_contract", - "get_his_contract_list", - "get_date_location", - "get_ETF_list", - # 北向资金 / 港股通 - "get_north_finance_change", - "get_hkt_statistics", - "get_hkt_details", - # 自定义板块(写) - "create_sector", - # 基础查询辅助 - "get_stock_name", - "get_stock_type", - "get_last_close", - "get_last_volume", - "get_open_date", - "get_contract_expire_date", - "get_contract_multiplier", - "get_float_caps", - "get_total_share", - "get_turn_over_rate", - "get_weight_in_index", - "get_svol", - "get_bvol", - "get_risk_free_rate", - # L2 行情(需 L2 权限 + 原生 xtdata SDK 行情服务) - "get_l2_quote", - "get_l2_order", - "get_l2_transaction", - "subscribe_l2thousand", - # 指数权重 / 交易日历 / 交易时段 / 可转债 / 品种判断 - "get_index_weight", - "get_trading_calendar", - "get_trade_times", - "get_cb_info", - "is_stock_type", - # 板块增删 - "add_sector", - "remove_sector", - # 数据下载扩展 - "download_cb_data", - "download_history_contracts", - "download_index_weight", - "download_sector_data", - # 时间戳转换(纯计算,服务端本地) - "datetime_to_timetag", - "timetag_to_datetime", -} - -# Keep READ_METHODS in sync with MARKET_DATA_METHODS: every market-data method -# forwarded to the adapter is also callable over RPC. (create_sector is a write -# op — creates/updates a custom sector — but it is harmless to expose; trading -# order writes stay gated behind ORDER_METHODS + allow_order_methods.) -READ_METHODS |= MARKET_DATA_METHODS -READ_METHODS |= QUOTE_SUBSCRIPTION_METHODS - - -def _maybe_scalar(value): - item = getattr(value, "item", None) - if callable(item): - try: - return item() - except Exception: - return value - return value - - -def _is_redis_timeout(exc): - name = exc.__class__.__name__.lower() - module = getattr(exc.__class__, "__module__", "") - text = str(exc).lower() - return ("redis" in module and "timeout" in name) or "timeout reading from socket" in text - - -def to_jsonable(value): - value = _maybe_scalar(value) - if value is None or isinstance(value, (str, int, float, bool)): - if isinstance(value, float) and (math.isnan(value) or math.isinf(value)): - return None - return value - if isinstance(value, (_dt.datetime, _dt.date)): - return value.strftime("%Y-%m-%d %H:%M:%S") - if hasattr(value, "isoformat") and value.__class__.__module__.startswith("pandas"): - try: - return value.isoformat() - except Exception: - return str(value) - if hasattr(value, "to_dict") and hasattr(value, "columns") and hasattr(value, "index"): - try: - frame = value.reset_index() - return { - "__bigqmt_type__": "DataFrame", - "columns": [str(col) for col in frame.columns], - "records": to_jsonable(frame.to_dict("records")), - } - except Exception: - return str(value) - if hasattr(value, "to_dict") and hasattr(value, "index") and not isinstance(value, dict): - try: - return { - "__bigqmt_type__": "Series", - "data": to_jsonable(value.to_dict()), - } - except Exception: - return str(value) - if hasattr(value, "tolist") and not isinstance(value, (str, bytes, bytearray)): - try: - return to_jsonable(value.tolist()) - except Exception: - pass - if isinstance(value, dict): - return {str(key): to_jsonable(item) for key, item in value.items()} - if isinstance(value, (list, tuple, set)): - return [to_jsonable(item) for item in value] - enum_value = getattr(value, "value", None) - if isinstance(enum_value, (str, int, float, bool)): - return enum_value - if hasattr(value, "__dict__"): - return { - key: to_jsonable(item) - for key, item in vars(value).items() - if not key.startswith("_") - } - return str(value) - - -class OrderSettlement(object): - """One order awaiting its order_sys_id. - - Why this cannot simply run on a background thread: get_trade_detail_data - returns EMPTY off the main strategy thread (see LISTENER_DEFERRED_METHODS), - so a background poll would find nothing and report every order as silently - rejected. The retries have to happen on the adjust thread -- just without - holding it. - - server_error is carried here rather than on handlers._last_server_error: - that slot belongs to whichever request is in flight, and settling writes to - it long after this request left the handler (issue #43). - """ - - __slots__ = ("order_request", "result", "deadline", "attempts", "server_error", - "request", "response") - - def __init__(self, order_request, result, deadline): - self.order_request = order_request - self.result = result - self.deadline = deadline - self.attempts = 0 - self.server_error = "" - self.request = None - self.response = None - - -class BigQmtRpcHandlers: - """Whitelisted RPC method handlers backed by replaceable adapters.""" - - def __init__( - self, - account_id, - market_data, - position_provider, - order_gateway=None, - position_sync_sink=None, - allow_order_methods=False, - allowed_methods=None, - qmt_api=None, - settle_orders_inline=False, - order_settle_timeout_seconds=3.0, - quote_subscription_manager=None, - ): - self.account_id = str(account_id or "") - self.market_data = market_data - self.position_provider = position_provider - self.order_gateway = order_gateway - self.position_sync_sink = position_sync_sink - self.allow_order_methods = bool(allow_order_methods) - self.quote_subscription_manager = quote_subscription_manager - # QMT runtime-injected global functions (passorder/get_trade_detail_data/ - # 融资融券查询等)。由 strategy._build_config 解析注入。 - self.qmt_api = dict(qmt_api or {}) - self._submit_journal = {} - # Order settlement. Async by default: blocking here holds the QMT main - # strategy thread, which serializes every other request behind it and - # caps throughput at ~2 orders/sec (issue #44). - self._pending_settlement = None - self.settle_orders_inline = bool(settle_orders_inline) - self.order_settle_timeout_seconds = float(order_settle_timeout_seconds) - # Server-side diagnostic for silent failures (e.g. passorder submitted - # but order not found in system). Surfaced to client via server_error. - self._last_server_error = "" - if allowed_methods is None: - allowed = set(READ_METHODS) - if self.allow_order_methods: - allowed.update(ORDER_METHODS) - self.allowed_methods = allowed - else: - self.allowed_methods = {str(method) for method in allowed_methods} - - def _request_account_id(self, params): - params = params or {} - account = params.get("account") - if isinstance(account, dict): - account = account.get("account_id") or account.get("accountID") or account.get("id") - account_id = str(params.get("account_id") or account or self.account_id or "") - if not account_id: - raise ValueError("account_id is required") - return account_id - - def _canonical_method(self, method): - return METHOD_ALIASES.get(method, method) - - def handle(self, method, params=None): - requested_method = str(method or "").strip() - method = self._canonical_method(requested_method) - params = dict(params or {}) - # Clear the diagnostic slot per request. It is instance state read by - # EVERY response (see _build_response), so without this a single failed - # submit_order stamps its server_error onto every later ping/query until - # the next order runs -- reporting a stale failure on requests that - # succeeded (issue #43). - self._last_server_error = "" - self._pending_settlement = None - if not requested_method: - raise ValueError("method is required") - if method not in self.allowed_methods: - raise ValueError("rpc method is not allowed: %s" % requested_method) - if method in ORDER_METHODS and not self.allow_order_methods: - raise PermissionError("order rpc methods are disabled") - handler = getattr(self, "_handle_%s" % method, None) - if handler is None and method in MARKET_DATA_METHODS: - return self._handle_market_data_method(method, params) - elif handler is None: - raise ValueError("rpc method is not implemented: %s" % requested_method) - return handler(params) - - def _handle_ping(self, params): - return { - "pong": True, - "account_id": self.account_id, - "allow_order_methods": bool(self.allow_order_methods), - "rpc_revision": RPC_REVISION, - "server_time": _dt.datetime.now(), - } - - # ------------------------------------------------------------------ - # 全推行情订阅控制(引用计数共享 ContextInfo.subscribe_whole_quote)。 - # 数据本身走推送通道;这里只负责订阅生命周期 + 心跳。 - # ------------------------------------------------------------------ - - def _require_quote_manager(self): - manager = self.quote_subscription_manager - if manager is None: - raise RuntimeError("whole-quote push subscription is not configured on this server") - return manager - - @staticmethod - def _quote_params(params, require_codes=False): - params = params or {} - client_id = str(params.get("client_id") or "").strip() - sub_id = str(params.get("sub_id") or "").strip() - if not client_id: - raise ValueError("client_id is required") - if not sub_id: - raise ValueError("sub_id is required") - codes = [str(c) for c in (params.get("codes") or []) if str(c or "").strip()] - if require_codes and not codes: - raise ValueError("codes is required") - return client_id, sub_id, codes - - def _handle_subscribe_whole_quote(self, params): - manager = self._require_quote_manager() - client_id, sub_id, codes = self._quote_params(params, require_codes=True) - return manager.subscribe(client_id, sub_id, codes) - - def _handle_unsubscribe_whole_quote(self, params): - manager = self._require_quote_manager() - client_id, sub_id, _codes = self._quote_params(params) - manager.unsubscribe(client_id, sub_id) - return {} - - def _handle_quote_keepalive(self, params): - manager = self._require_quote_manager() - client_id, sub_id, _codes = self._quote_params(params) - manager.keepalive(client_id, sub_id) - return {} - - def _download_job_redis(self): - redis_client = getattr(self, "download_job_redis_client", None) - if redis_client is None: - raise RuntimeError("download jobs require a Redis client") - return redis_client - - def _handle_submit_download_history_data2(self, params): - from .download_jobs import submit_download_job - - stock_list = params.get("stock_list") or params.get("stock_code") or [] - if isinstance(stock_list, str): - stock_list = [stock_list] - return submit_download_job( - self._download_job_redis(), - self.account_id, - stock_list, - params.get("period"), - method="download_history_data2", - start_time=params.get("start_time", ""), - end_time=params.get("end_time", ""), - incrementally=params.get("incrementally"), - chunk_size=int(params.get("chunk_size") or getattr(self, "download_job_chunk_size", 10)), - job_ttl_seconds=int(params.get("job_ttl_seconds") or getattr(self, "download_job_ttl_seconds", 3600)), - ) - - def _handle_submit_download_history_data(self, params): - stock_code = params.get("stock_code") or params.get("code") - next_params = dict(params or {}) - next_params["stock_list"] = [stock_code] if stock_code else [] - return self._handle_submit_download_history_data2(next_params) - - def _handle_get_download_status(self, params): - from .download_jobs import read_download_status - - job_id = params.get("job_id") - if not job_id: - raise ValueError("job_id is required") - status = read_download_status(self._download_job_redis(), self.account_id, job_id) - if status is None: - raise KeyError("download job not found or expired: %s" % job_id) - return status - - def _handle_wait_download(self, params): - from .download_jobs import wait_download_job - - job_id = params.get("job_id") - if not job_id: - raise ValueError("job_id is required") - return wait_download_job( - self._download_job_redis(), - self.account_id, - job_id, - wait_seconds=float(params.get("wait_seconds", 600.0)), - poll_interval_seconds=float(params.get("poll_interval_seconds", 0.5)), - ) - - - def _handle_get_ticks(self, params): - codes = params.get("codes") - if isinstance(codes, str): - codes = [codes] - if not codes: - code = params.get("code") - codes = [code] if code else [] - if not codes: - raise ValueError("codes or code is required") - return self.market_data.get_ticks(codes) - - def _handle_get_instrument(self, params): - code = params.get("code") - if not code: - raise ValueError("code is required") - return self.market_data.get_instrument(code) - - def _handle_market_data_method(self, method, params): - handler = getattr(self.market_data, method, None) - if handler is None: - raise NotImplementedError("market data method is not available: %s" % method) - return handler(**dict(params or {})) - - def _handle_get_positions(self, params): - return self.position_provider.get_positions(self._request_account_id(params)) - - def _handle_query_stock_position(self, params): - stock_code = str(params.get("stock_code") or params.get("code") or "").strip() - if not stock_code: - raise ValueError("stock_code is required") - normalized_code = normalize_stock_code(stock_code) - positions = self.position_provider.get_positions(self._request_account_id(params)) - return positions.get(normalized_code) - - def _handle_get_asset(self, params): - return self.position_provider.get_asset(self._request_account_id(params)) - - def _handle_query_orders(self, params): - if self.order_gateway is None: - raise RuntimeError("order_gateway is not configured") - # strategy_name filters orders by the name used in passorder. An empty - # string returns ALL orders for the account (verified via diagnostic: - # st="" -> 9 orders, st="bigqmt_signal_trader" -> 0). Default to "" - # so callers see every order unless they explicitly filter. - orders = self.order_gateway.query_orders( - self._request_account_id(params), - str(params.get("strategy_name") or ""), - ) - if _bool_value(params.get("cancelable_only"), False): - return [ - order - for order in orders - if str(getattr(order, "status", "") or "") in CANCELABLE_ORDER_STATUSES - ] - return orders - - def _handle_query_trades(self, params): - if self.order_gateway is None: - raise RuntimeError("order_gateway is not configured") - # Empty strategy_name returns ALL deals for the account (see query_orders - # note). Default "" so callers see every trade unless they filter. - strategy_name = params.get("strategy_name") - if strategy_name is None: - strategy_name = "" - return self.order_gateway.query_trades( - self._request_account_id(params), - str(strategy_name), - ) - - def _handle_query_execution_snapshot(self, params): - if self.order_gateway is None: - raise RuntimeError("order_gateway is not configured") - account_id = self._request_account_id(params) - order_name = params.get("order_strategy_name") - if order_name is None: - order_name = params.get("strategy_name", "bigqmt_signal_trader") - trade_name = params.get("trade_strategy_name") - if trade_name is None: - trade_name = "" - return { - "account_id": account_id, - "server_time": _dt.datetime.now(), - "rpc_revision": RPC_REVISION, - "orders": self.order_gateway.query_orders(account_id, str(order_name)), - "trades": self.order_gateway.query_trades(account_id, str(trade_name)), - } - - def _handle_sync_positions(self, params): - account_id = self._request_account_id(params) - snapshot = AccountSnapshot( - account_id=account_id, - asset=self.position_provider.get_asset(account_id), - positions=self.position_provider.get_positions(account_id), - reason=str(params.get("reason") or "rpc"), - updated_at=_dt.datetime.now(), - ) - if self.position_sync_sink is not None: - self.position_sync_sink.publish(snapshot) - return snapshot - - # ------------------------------------------------------------------ - # 账户 / 融资融券 / 交易扩展查询 - # 这些是 Big QMT 运行时注入的全局函数(同 passorder),不在 ContextInfo 桩里。 - # 函数名严格按官方文档(trading_function.html),通过 self.qmt_api 调用。 - # 无该权限/函数未注入时降级为空列表。 - # ------------------------------------------------------------------ - - def _call_qmt_global(self, func_name, *args, **kwargs): - """Call a QMT runtime-injected global function, returning [] on failure. - - These functions (get_assure_contract / get_unclosed_compacts / ...) - are injected by QMT into the process global namespace, same as - passorder. When unavailable (no margin account, function not bound) - we degrade to [] rather than crashing the RPC. - """ - func = self.qmt_api.get(func_name) - if func is None: - return [] - try: - return _normalize_detail_rows(func(*args, **kwargs)) - except Exception: - return [] - - def _query_trade_detail(self, params, detail_type, strategy_name=""): - """get_trade_detail_data with one of the 6 official detail types. - - Official strDatatype values: ACCOUNT / POSITION / POSITION_STATISTICS / - ORDER / DEAL / TASK. Other strings (CREDIT etc.) are NOT supported by - this API — use the dedicated functions below for margin queries. - """ - account_id = self._request_account_id(params) - gateway = self.order_gateway - if gateway is None or gateway.get_trade_detail_data is None: - return [] - try: - rows = gateway.get_trade_detail_data(account_id, gateway.account_type, detail_type, strategy_name) - return _normalize_detail_rows(rows) - except Exception: - return [] - - def _handle_query_account_infos(self, params): - # 账户信息 — get_trade_detail_data(ACCOUNT) - return self._query_trade_detail(params, "ACCOUNT") - - def _handle_query_account_status(self, params): - # 账户状态 — 用 TASK detail type 近似(委托任务状态) - return self._query_trade_detail(params, "TASK") - - def _handle_query_credit_detail(self, params): - # 融资融券账户明细 — 官方独立函数 get_debt_contract - return self._call_qmt_global("get_debt_contract", self._request_account_id(params)) - - def _handle_query_stk_compacts(self, params): - # 未平仓合约(负债)— 官方 get_unclosed_compacts - return self._call_qmt_global("get_unclosed_compacts", self._request_account_id(params)) - - def _handle_query_credit_subjects(self, params): - # 融资标的(担保品)— 官方 get_assure_contract - return self._call_qmt_global("get_assure_contract", self._request_account_id(params)) - - def _handle_query_credit_slo_code(self, params): - # 融券标的 — 官方 get_enable_short_contract - return self._call_qmt_global("get_enable_short_contract", self._request_account_id(params)) - - def _handle_query_credit_assure(self, params): - # 担保品合约 — 同 query_credit_subjects(get_assure_contract) - return self._call_qmt_global("get_assure_contract", self._request_account_id(params)) - - def _handle_query_appointment_info(self, params): - # 新股数据 — 官方 get_ipo_data - return self._call_qmt_global("get_ipo_data", self._request_account_id(params)) - - def _handle_query_smt_secu_info(self, params): - # 期权标的持仓 — 官方 get_option_subject_position - return self._call_qmt_global("get_option_subject_position", self._request_account_id(params)) - - def _handle_query_smt_secu_rate(self, params): - # 组合期权 — 官方 get_comb_option - return self._call_qmt_global("get_comb_option", self._request_account_id(params)) - - def _handle_smt_appointment(self, params): - # SMB/预约打新属于交易类,需要下单通道;当前不支持。 - raise NotImplementedError("smt_appointment is not supported via Big QMT RPC") - - # 官方交易查询函数(直接暴露) - def _handle_get_value_by_order_id(self, params): - order_id = str(params.get("order_id") or params.get("order_sysid") or "") - if not order_id: - raise ValueError("order_id is required") - return self._call_qmt_global("get_value_by_order_id", order_id) - - def _handle_get_last_order_id(self, params): - return self._call_qmt_global("get_last_order_id", self._request_account_id(params)) - - def _handle_get_ipo_data(self, params): - return self._call_qmt_global("get_ipo_data", self._request_account_id(params)) - - def _handle_get_new_purchase_limit(self, params): - return self._call_qmt_global("get_new_purchase_limit", self._request_account_id(params)) - - def _handle_get_history_trade_detail_data(self, params): - account_id = self._request_account_id(params) - detail_type = str(params.get("detail_type") or params.get("datatype") or "DEAL") - start_date = str(params.get("start_date") or params.get("start_time") or "") - end_date = str(params.get("end_date") or params.get("end_time") or "") - result = self._call_qmt_global( - "get_history_trade_detail_data", account_id, detail_type, start_date, end_date - ) - return result - - def _handle_get_assure_contract(self, params): - return self._call_qmt_global("get_assure_contract", self._request_account_id(params)) - - def _handle_get_enable_short_contract(self, params): - return self._call_qmt_global("get_enable_short_contract", self._request_account_id(params)) - - def _handle_get_unclosed_compacts(self, params): - return self._call_qmt_global("get_unclosed_compacts", self._request_account_id(params)) - - def _handle_get_closed_compacts(self, params): - return self._call_qmt_global("get_closed_compacts", self._request_account_id(params)) - - def _handle_get_debt_contract(self, params): - return self._call_qmt_global("get_debt_contract", self._request_account_id(params)) - - def _handle_get_option_subject_position(self, params): - return self._call_qmt_global("get_option_subject_position", self._request_account_id(params)) - - def _handle_get_comb_option(self, params): - return self._call_qmt_global("get_comb_option", self._request_account_id(params)) - - def _handle_get_hkt_exchange_rate(self, params): - return self._call_qmt_global("get_hkt_exchange_rate") - - def _handle_download_history_data(self, params): - """download_history_data is a QMT global function (issue #32). - - It is NOT a ContextInfo method — the adapter's _call_context path - always raised NotImplementedError. Now route through qmt_api (the - injected global), falling back to the adapter (which tries native - xtdata SDK then ContextInfo). - """ - func = self.qmt_api.get("download_history_data") - if func is not None: - try: - stock_code = str(params.get("stock_code") or "") - period = str(params.get("period") or "1d") - start_time = str(params.get("start_time") or "") - end_time = str(params.get("end_time") or "") - result = func(stock_code, period, start_time, end_time) - return bool(result) if result is not None else True - except Exception as exc: - raise RuntimeError("download_history_data failed: %s" % exc) - # Fallback: adapter tries native xtdata SDK then ContextInfo. - # If the adapter lacks the method, return False (not crash). - try: - return self._handle_market_data_method("download_history_data", params) - except (NotImplementedError, AttributeError): - return False - - def _handle_download_history_data2(self, params): - """download_history_data2 is a QMT global function (issue #32). - - Native signature includes an optional callback for progress; the QMT - global may require it, so pass a no-op when the client didn't. - """ - func = self.qmt_api.get("download_history_data2") - if func is not None: - try: - stock_list = list(params.get("stock_list") or []) - period = str(params.get("period") or "1d") - start_time = str(params.get("start_time") or "") - end_time = str(params.get("end_time") or "") - # Try with a no-op callback first (some QMT builds require it); - # fall back to 4-arg call if that raises TypeError. - try: - result = func(stock_list, period, start_time, end_time, lambda data: None) - except TypeError: - result = func(stock_list, period, start_time, end_time) - return bool(result) if result is not None else True - except Exception as exc: - raise RuntimeError("download_history_data2 failed: %s" % exc) - try: - return self._handle_market_data_method("download_history_data2", params) - except (NotImplementedError, AttributeError): - return False - - def _order_action_from_params(self, params): - action = str(params.get("action") or "").upper() - if action: - return action - order_type = str(params.get("order_type") or "").upper() - if order_type in BUY_ORDER_TYPES: - return "BUY" - if order_type in SELL_ORDER_TYPES: - return "SELL" - raise ValueError("action or order_type is required") - - def _handle_submit_order(self, params): - if self.order_gateway is None: - raise RuntimeError("order_gateway is not configured") - price = params.get("price") - signal_id = str(params.get("signal_id") or "rpc-%s" % uuid.uuid4().hex) - order_tag = str(params.get("remark") or params.get("order_remark") or "").strip() - if not order_tag: - order_tag = "bqrpc:%s" % signal_id - request = OrderRequest( - signal_id=signal_id, - account_id=self._request_account_id(params), - action=self._order_action_from_params(params), - stock_code=str(params.get("stock_code") or ""), - volume=int(params.get("volume") or params.get("order_volume") or 0), - price=float(price if price not in (None, "") else 0), - price_type=params.get("price_type") or "LIMIT", - strategy_name=str(params.get("strategy_name") or "bigqmt_rpc"), - remark=order_tag, - ) - if request.action not in ("BUY", "SELL"): - raise ValueError("action must be BUY or SELL") - if not request.stock_code: - raise ValueError("stock_code is required") - if request.volume <= 0: - raise ValueError("volume must be positive") - - try: - from .exec_events import remember_order_identity - - remember_order_identity( - getattr(self, "download_job_redis_client", None), - request.account_id, - request.remark, - strategy_name=request.strategy_name, - stock_code=request.stock_code, - ) - except Exception: - pass - - result = self.order_gateway.submit(request) - - # 委托后校验:确认委托是否真的进了系统。passorder 调用成功但委托没进 - # 系统时(静默失败),记录 server_error 让客户端知道。匹配严格按 - # user_order_id(remark) 精确比对,不做 stock_code+action 的模糊兜底。 - # QMT 的委托号是异步分配的(passorder 无返回值),这里按唯一 - # user_order_id(remark) 精确匹配并回填 order_sys_id,避免客户端把 - # 「已提交但暂无委托号」误判为下单失败(issue #38)。 - self._last_server_error = "" - - # Async callers opt out of waiting for the order id. MiniQMT's - # order_stock_async returns a seq immediately and delivers the id through - # order_callback, so holding the reply until settlement is exactly the - # latency the async API exists to avoid (issue #50). The order_callback - # push already carries order_sys_id, so nothing is lost -- only the - # post-submit "did it land?" check is skipped, and a silent rejection - # surfaces as the absence of that push rather than as server_error. - if not _bool_value(params.get("wait_settlement"), True): - return result - - if self.settle_orders_inline: - # Opt-out: block here the way this used to. Kept only for runtimes - # with no adjust drain to retry on. - try: - import time as _time - _time.sleep(self.order_settle_timeout_seconds) - self._apply_order_lookup( - OrderSettlement(request, result, 0.0), final=True, inline=True) - except Exception: - pass - return result - # Hand the settlement to the caller rather than raising: handle() stays - # a plain function for anyone driving handlers directly, and only the - # service defers its reply. - self._pending_settlement = OrderSettlement( - request, result, _monotonic() + self.order_settle_timeout_seconds - ) - return result - - def take_pending_settlement(self): - """Pop the settlement the last submit_order registered, if any.""" - settlement = self._pending_settlement - self._pending_settlement = None - return settlement - - def _apply_order_lookup(self, settlement, final=False, inline=False): - """Look the order up by remark. True when settled, False to retry. - - MUST run on the main strategy thread -- get_trade_detail_data returns - empty anywhere else. - """ - request = settlement.order_request - settlement.attempts += 1 - try: - orders = self.order_gateway.query_orders(request.account_id, "") or [] - by_remark = [ - o for o in orders - if str(getattr(o, "user_order_id", "") or "").strip() == request.remark.strip() - ] - if by_remark: - sysid = str(getattr(by_remark[0], "order_sys_id", "") or "") - if sysid: - try: - settlement.result.order_sys_id = sysid - except Exception: - pass - return True - if not final: - # Not there yet. QMT assigns the id asynchronously, so an early - # miss is normal -- only a miss at the deadline is a real one. - return False - # Deadline reached with no remark match -> not in the system. Do NOT - # fall back to matching stock_code+action: order_tag is a unique id - # we generated, so a miss is always a real miss, while an unrelated - # order on the same stock and side (a manual one, or an earlier - # unfilled order) would silently suppress this warning and leave - # order_sys_id unfilled with no signal at all (issue #41). - message = ( - "passorder submitted but order not found in system " - "(stock=%s action=%s price=%.2f volume=%d, %d lookup(s)). " - "QMT may have silently rejected it (check price range / permissions)." - % (request.stock_code, request.action, request.price, - request.volume, settlement.attempts) - ) - settlement.server_error = message - if inline: - self._last_server_error = message - return True - except Exception: - # A failed lookup must not lose the order -- it is already submitted. - return True - - def _handle_submit_orders_batch(self, params): - orders = params.get("orders") or [] - if not isinstance(orders, list) or not orders: - raise ValueError("orders must be a non-empty list") - if len(orders) > 500: - raise ValueError("orders exceeds batch limit 500") - batch_id = str(params.get("batch_id") or uuid.uuid4().hex) - account_id = self._request_account_id(params) - strategy_name = str( - params.get("strategy_name") - or (orders[0] or {}).get("strategy_name") - or "bigqmt_rpc" - ) - existing_by_tag = {} - lookup_ok = True - requires_lookup = any(bool((item or {}).get("require_idempotency_check")) for item in orders) - if requires_lookup: - try: - identity_query = getattr(self.order_gateway, "query_submission_identities_strict", None) - if callable(identity_query): - existing, trades = identity_query(account_id, strategy_name) - else: - query = getattr(self.order_gateway, "query_orders_strict", None) - existing = query(account_id, strategy_name) if callable(query) else self.order_gateway.query_orders(account_id, strategy_name) - trades = [] - existing_by_tag = { - str(getattr(order, "user_order_id", "") or ""): order - for order in existing or [] - if str(getattr(order, "user_order_id", "") or "") - } - for trade in trades or []: - tag = str(getattr(trade, "user_order_id", "") or "") - if tag and tag not in existing_by_tag: - existing_by_tag[tag] = trade - except Exception: - lookup_ok = False - results = [] - for index, item in enumerate(orders): - item = dict(item or {}) - order_tag = str(item.get("order_remark") or item.get("remark") or item.get("signal_id") or "") - if not order_tag: - results.append({ - "index": index, - "batch_id": batch_id, - "success": False, - "accepted": False, - "explicit_failure": True, - "code": -3, - "error": "ORDER_TAG_REQUIRED", - "user_order_id": "", - }) - continue - known = existing_by_tag.get(order_tag) - journal_key = (account_id, strategy_name, order_tag) - journal = self._submit_journal.get(journal_key) - if known is not None or journal is not None: - results.append({ - "index": index, - "batch_id": batch_id, - "success": True, - "accepted": True, - "confirmed": known is not None, - "idempotent": True, - "code": 0, - "order_sys_id": str(getattr(known, "order_sys_id", "") or (journal or {}).get("order_sys_id") or ""), - "user_order_id": order_tag, - }) - continue - if bool(item.get("require_idempotency_check")) and not lookup_ok: - results.append({ - "index": index, - "batch_id": batch_id, - "success": False, - "accepted": False, - "explicit_failure": False, - "code": -2, - "error": "IDEMPOTENCY_CHECK_UNAVAILABLE", - "user_order_id": order_tag, - }) - continue - try: - result = self._handle_submit_order(item) - response = { - "index": index, - "batch_id": batch_id, - "success": True, - "accepted": True, - "confirmed": False, - "idempotent": False, - "code": 0, - "order_sys_id": str(getattr(result, "order_sys_id", None) or ""), - "user_order_id": str(getattr(result, "user_order_id", None) or ""), - } - if order_tag: - self._submit_journal[journal_key] = dict(response) - results.append(response) - except Exception as exc: - results.append({ - "index": index, - "batch_id": batch_id, - "success": False, - "accepted": False, - "explicit_failure": True, - "code": -1, - "error": "%s: %s" % (exc.__class__.__name__, exc), - "user_order_id": order_tag, - }) - return results - - def _handle_cancel_order(self, params): - if self.order_gateway is None: - raise RuntimeError("order_gateway is not configured") - order_sys_id = str(params.get("order_sys_id") or params.get("order_sysid") or params.get("order_id") or "") - if not order_sys_id: - raise ValueError("order_sys_id or order_id is required") - return self.order_gateway.cancel( - OrderRef(order_sys_id=order_sys_id, user_order_id=str(params.get("user_order_id") or "")) - ) - - -def _bool_value(value, default=False): - if value is None or value == "": - return default - if isinstance(value, bool): - return value - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -def _normalize_detail_rows(rows): - """Convert get_trade_detail_data row objects into JSON-serializable dicts. - - QMT returns objects with m_strXxx / m_nXxx / m_dXxx attributes. We map - each to its public attributes so the result survives JSON encoding. - """ - if not rows: - return [] - result = [] - for row in rows: - if isinstance(row, dict): - result.append(row) - continue - item = {} - for name in dir(row): - if name.startswith("_"): - continue - try: - value = getattr(row, name) - except Exception: - continue - if callable(value): - continue - item[name] = value - result.append(item) - return result - - -def encode_rpc_request_payload(request): - """Encode request JSON so patched QMT Redis clients do not inspect stock-code text.""" - - raw = json.dumps(request, ensure_ascii=False).encode("utf-8") - encoded = base64.b64encode(raw).decode("ascii").translate(SAFE_B64_DIGIT_ENCODE) - return SAFE_B64_PREFIX + encoded - - -def decode_rpc_request_payload(text): - text = str(text) - if not text.startswith(SAFE_B64_PREFIX): - return text - encoded = text[len(SAFE_B64_PREFIX):].translate(SAFE_B64_DIGIT_DECODE) - return base64.b64decode(encoded.encode("ascii")).decode("utf-8") - - -class RedisPubSubRpcService: - """Receive RPC requests from Redis and write responses back to Redis.""" - - def __init__( - self, - redis_client, - handlers, - account_id="", - response_redis_client=None, - request_channel_template="bigqmt:rpc:req:{account_id}", - request_queue_template="bigqmt:rpc:queue:{account_id}", - response_channel_template="bigqmt:rpc:resp:{account_id}:{request_id}", - response_list_template="bigqmt:rpc:respq:{account_id}:{request_id}", - response_key_template="bigqmt:rpc:resp:{account_id}:{request_id}", - response_ttl_seconds=60, - max_queue_size=200, - process_in_listener=False, - listener_methods=None, - background_threads=True, - queue_poll_interval_seconds=0.02, - debug_log_limit=0, - print_prefix="[bigqmt_rpc]", - transport=None, - ): - self.listen_redis = redis_client - self.redis = response_redis_client or redis_client - self.handlers = handlers - self.account_id = str(account_id or "") - self.request_channel_template = request_channel_template - self.request_queue_template = request_queue_template - self.response_channel_template = response_channel_template - self.response_list_template = response_list_template - self.response_key_template = response_key_template - self.response_ttl_seconds = int(response_ttl_seconds) - self.process_in_listener = bool(process_in_listener) - self.background_threads = bool(background_threads) - if listener_methods is None: - listener_methods = ("ping",) - self.listener_methods = self._expand_listener_methods(listener_methods) - self.queue_poll_interval_seconds = max(0.001, float(queue_poll_interval_seconds)) - self.debug_log_limit = int(debug_log_limit) - self._received_count = 0 - self._processed_count = 0 - self._published_count = 0 - self._deferred_count = 0 - self.print_prefix = print_prefix - self.pending = queue.Queue(maxsize=int(max_queue_size)) - # Orders whose reply is waiting on QMT assigning an order id. Unbounded - # on purpose: every entry is an order that already reached the broker, - # so dropping one would strand a live order with no reply. - self._pending_settlements = queue.Queue() - self._running = threading.Event() - self._thread = None - self._queue_thread = None - self._pubsub = None - # Transport owns the wire. Default to a RedisTransport built from the - # same clients/templates so behavior is unchanged. An explicit - # ``transport`` (e.g. ZmqTransport) overrides the Redis path entirely. - if transport is None: - from .transports.redis_transport import RedisTransport - - transport = RedisTransport( - redis_client, - account_id=self.account_id, - response_redis_client=response_redis_client, - request_channel_template=request_channel_template, - request_queue_template=request_queue_template, - response_channel_template=response_channel_template, - response_list_template=response_list_template, - response_key_template=response_key_template, - response_ttl_seconds=response_ttl_seconds, - queue_poll_interval_seconds=queue_poll_interval_seconds, - debug_log_limit=debug_log_limit, - print_prefix=print_prefix, - ) - self._transport = transport - # Route inbound raw payloads through the service's dispatch (which - # applies the inline-vs-deferred fork) instead of transport.deliver(). - self._transport.on_raw_payload = self._handle_received_payload - - @property - def request_channel(self): - return self.request_channel_template.format(account_id=self.account_id) - - @property - def request_queue(self): - return self.request_queue_template.format(account_id=self.account_id) - - def start(self): - self._running.set() - # Delegate thread lifecycle to the transport. The transport invokes the - # on_request callback with a decoded request dict; enqueue_payload routes - # it through the inline-vs-deferred fork and publishes the response - # itself (returns None so the transport's deliver() does not double-send). - # RedisTransport additionally routes raw bytes through on_raw_payload - # (set in __init__) for its own receive loops. - self._transport.start_receiving( - self.enqueue_payload, - background_threads=self.background_threads, - ) - # Mirror transport threads onto the service for stop()/diagnostics. - self._thread = getattr(self._transport, "_thread", None) - self._queue_thread = getattr(self._transport, "_queue_thread", None) - if not self.background_threads: - print("%s started queue=%s background_threads=False" % (self.print_prefix, self.request_queue)) - return - print("%s started channel=%s queue=%s" % (self.print_prefix, self.request_channel, self.request_queue)) - - def stop(self): - self._running.clear() - try: - self._transport.stop() - except Exception: - pass - # The transport owns the threads now; keep the attributes for back-compat. - self._thread = None - self._queue_thread = None - self._pubsub = None - - def _listen_loop(self): - while self._running.is_set(): - try: - pubsub = self.listen_redis.pubsub(ignore_subscribe_messages=True) - self._pubsub = pubsub - pubsub.subscribe(self.request_channel) - if self.debug_log_limit > 0: - print("%s subscribed channel=%s" % (self.print_prefix, self.request_channel)) - while self._running.is_set(): - message = pubsub.get_message(timeout=1.0) - if not self._running.is_set(): - break - if not message or message.get("type") != "message": - continue - self._handle_received_payload(message.get("data"), "pubsub") - except Exception: - print("%s listener failed:\n%s" % (self.print_prefix, traceback.format_exc())) - time.sleep(1.0) - finally: - try: - if self._pubsub is not None: - self._pubsub.close() - except Exception: - pass - self._pubsub = None - - def _queue_loop(self): - while self._running.is_set(): - try: - if self.debug_log_limit > 0: - print("%s queue polling key=%s" % (self.print_prefix, self.request_queue)) - while self._running.is_set(): - # brpop blocks server-side until an item arrives (or the - # short timeout fires), so a request is picked up within - # ~1ms of being pushed instead of waiting up to - # queue_poll_interval_seconds. The 1s ceiling lets us - # re-check _running for a clean shutdown. - item = self.listen_redis.brpop(self.request_queue, timeout=1) - if not self._running.is_set(): - break - if not item: - continue - raw = item[1] if isinstance(item, (list, tuple)) and len(item) >= 2 else item - self._handle_received_payload(raw, "queue") - except Exception: - print("%s queue listener failed:\n%s" % (self.print_prefix, traceback.format_exc())) - time.sleep(1.0) - - def _handle_received_payload(self, raw_payload, source): - self._received_count += 1 - if self._received_count <= self.debug_log_limit: - try: - preview = self._loads(raw_payload) - method = str(preview.get("method") or "") - print( - "%s received source=%s method=%s inline=%s" - % (self.print_prefix, source, method, self._should_process_in_listener(preview)) - ) - self.enqueue_payload(preview) - return - except Exception: - print("%s receive preview failed:\n%s" % (self.print_prefix, traceback.format_exc())) - self.enqueue_payload(raw_payload) - - def enqueue_payload(self, raw_payload): - payload = self._loads(raw_payload) - if self._should_process_in_listener(payload): - self.process_request(payload) - return - self._deferred_count += 1 - if self._deferred_count <= self.debug_log_limit: - print( - "%s deferred method=%s pending_before=%s" - % (self.print_prefix, payload.get("method"), self.pending.qsize()) - ) - try: - self.pending.put_nowait(payload) - except queue.Full: - # A full pending queue (client polling storm) must not raise into the - # adjust thread — QMT stops the strategy on a callback raise. Drop the - # oldest request and keep the newest instead of crashing. - try: - self.pending.get_nowait() - except Exception: - pass - try: - self.pending.put_nowait(payload) - except Exception: - pass - - def _should_process_in_listener(self, payload): - if not self.process_in_listener: - return False - method = str((payload or {}).get("method") or "") - if method in self.listener_methods: - return True - canonical = getattr(self.handlers, "_canonical_method", lambda value: value)(method) - return canonical in self.listener_methods - - def _expand_listener_methods(self, listener_methods): - methods = set() - for method in listener_methods or (): - method = str(method) - if method in ("*", "all", "read", "readonly"): - methods.update(READ_METHODS - LISTENER_DEFERRED_METHODS) - else: - methods.add(method) - canonical = getattr(self.handlers, "_canonical_method", lambda value: value)(method) - methods.add(canonical) - return methods - - def _loads(self, raw_payload): - if isinstance(raw_payload, dict): - return dict(raw_payload) - text = decode_text(raw_payload) - text = decode_rpc_request_payload(text) - payload = json.loads(text) - if not isinstance(payload, dict): - raise ValueError("rpc payload must be a json object") - return payload - - def settle_pending_orders(self, max_items=100): - """Retry parked order lookups. MUST be called from the adjust thread. - - A queue rather than a list because rpc_listener_methods is configurable: - if submit_order is ever put in it, the producer becomes the listener - thread while this consumer stays on adjust. - - Unsettled entries go back on the queue, so each order costs one lookup - per adjust tick until it resolves or its deadline passes. - """ - settled = 0 - # Snapshot the size first. Unsettled entries go back on the same queue, - # so draining until empty would keep re-picking them and spin one adjust - # tick into many lookups per order. - batch = min(int(max_items), self._pending_settlements.qsize()) - for _ in range(batch): - try: - settlement = self._pending_settlements.get_nowait() - except queue.Empty: - break - expired = _monotonic() >= settlement.deadline - try: - done = self.handlers._apply_order_lookup(settlement, final=expired) - except Exception: - done = True # never strand a submitted order in the queue - if not done: - self._pending_settlements.put(settlement) - continue - response = settlement.response - response["data"] = to_jsonable(settlement.result) - response["ok"] = True - if settlement.server_error: - response["server_error"] = settlement.server_error - response["handled_at"] = _dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - try: - self._publish_response(settlement.request, response) - except Exception: - pass - settled += 1 - return settled - - def pending_settlement_count(self): - return self._pending_settlements.qsize() - - def drain_pending(self, max_items=20): - # Settle carry-overs from earlier ticks before taking on new work. - self.settle_pending_orders() - processed = 0 - for _ in range(int(max_items)): - try: - request = self.pending.get_nowait() - except queue.Empty: - break - if self._processed_count < self.debug_log_limit: - print( - "%s draining method=%s pending_after=%s" - % (self.print_prefix, request.get("method"), self.pending.qsize()) - ) - self.process_request(request) - processed += 1 - # Settle again so an order submitted in THIS drain still replies on this - # tick. One lookup, no sleep -- if QMT has not assigned the id yet we - # simply retry next tick rather than holding the thread (issue #44). - self.settle_pending_orders() - return processed - - def drain_request_queue(self, max_items=20): - # Delegate to the transport when it owns the wire directly; for Redis - # the transport's drain drives _handle_received_payload (which honors - # the inline-vs-deferred fork), matching the original semantics. - transport_drain = getattr(self._transport, "drain_request_queue", None) - if transport_drain is not None and not isinstance(self._transport, type(None)): - return transport_drain(max_items=max_items) - processed = 0 - for _ in range(int(max_items)): - item = self.listen_redis.lpop(self.request_queue) - if not item: - break - self.process_request(self._loads(item)) - processed += 1 - return processed - - def process_request(self, request): - request = dict(request or {}) - request_id = str(request.get("request_id") or request.get("id") or uuid.uuid4().hex) - account_id = str(request.get("account_id") or self.account_id or "") - method = str(request.get("method") or "") - response = { - "schema_version": 1, - "request_id": request_id, - "account_id": account_id, - "method": method, - "ok": False, - "data": None, - "error": "", - # server_error carries QMT-side diagnostic info (e.g. passorder - # submitted but order not found in system, get_trade_detail_data - # returned empty) that doesn't raise an exception but indicates a - # problem. Lets clients see why an operation silently failed. - "server_error": "", - "handled_at": _dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), - } - try: - if self.account_id and account_id and account_id != self.account_id: - raise PermissionError("account_id mismatch") - result = self.handlers.handle(method, request.get("params") or {}) - response["data"] = to_jsonable(result) - response["ok"] = True - # Surface server-side diagnostics when the handler recorded one. - server_error = getattr(self.handlers, "_last_server_error", None) - if server_error: - response["server_error"] = str(server_error) - # passorder already ran, but QMT assigns the order id asynchronously. - # Park the reply instead of sleeping on this thread; a later adjust - # tick settles and publishes it (issue #44). - take = getattr(self.handlers, "take_pending_settlement", None) - settlement = take() if callable(take) else None - if settlement is not None: - settlement.request = request - settlement.response = response - self._pending_settlements.put(settlement) - self._deferred_count += 1 - return response - except Exception as exc: - response["error"] = "%s: %s" % (exc.__class__.__name__, exc) - try: - self._publish_response(request, response) - except Exception: - # A response-publish failure (e.g. redis outage) must not propagate - # to the adjust thread — QMT stops the strategy on a callback raise. - # The request already ran; the client will just see a timeout. - import traceback as _tb - try: - from .logging_setup import get_logger - get_logger("rpc").error( - "publish response failed method=%s:\n%s", method, _tb.format_exc() - ) - except Exception: - pass - self._processed_count += 1 - if self._processed_count <= self.debug_log_limit: - print("%s responded method=%s ok=%s" % (self.print_prefix, method, response["ok"])) - return response - - def _format_response_target(self, template, account_id, request_id): - if not template: - return "" - return template.format(account_id=account_id, request_id=request_id) - - def _publish_response(self, request, response): - # Delegate to the transport (RedisTransport fans out to key/list/channel; - # ZMQ/MySQL transports use their native reply path). - self._transport.send_response(request, response) - self._published_count = getattr(self._transport, "_published_count", self._published_count) - - def _response_clients(self): - clients = [self.redis] - if self.listen_redis is not self.redis: - clients.append(self.listen_redis) - return clients - - def _write_response_key(self, response_key, ttl_seconds, payload): - first_error = None - wrote = 0 - for client in self._response_clients(): - try: - if ttl_seconds > 0: - client.setex(response_key, ttl_seconds, payload) - else: - client.set(response_key, payload) - wrote += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if wrote <= 0 and first_error is not None: - raise first_error - return wrote - - def _push_response_list(self, response_list, ttl_seconds, payload): - first_error = None - pushed = 0 - for client in self._response_clients(): - try: - client.rpush(response_list, payload) - if ttl_seconds > 0: - client.expire(response_list, ttl_seconds) - pushed += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if pushed <= 0 and first_error is not None: - raise first_error - return pushed - - def _publish_response_channel(self, response_channel, payload): - first_error = None - receivers = 0 - published = 0 - for client in self._response_clients(): - try: - receivers += int(client.publish(response_channel, payload) or 0) - published += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if published <= 0 and first_error is not None: - raise first_error - self._published_count += 1 - if self._published_count <= self.debug_log_limit: - print("%s published response receivers=%s" % (self.print_prefix, receivers)) - return receivers - - -def call_redis_rpc( - redis_client, - account_id, - method, - params=None, - request_channel_template="bigqmt:rpc:req:{account_id}", - request_queue_template="bigqmt:rpc:queue:{account_id}", - response_channel_template="bigqmt:rpc:resp:{account_id}:{request_id}", - response_list_template="bigqmt:rpc:respq:{account_id}:{request_id}", - response_key_template="bigqmt:rpc:resp:{account_id}:{request_id}", - timeout_seconds=3.0, - ttl_seconds=60, - transport="queue", -): - """Small external client helper for tests and admin scripts.""" - - request_id = uuid.uuid4().hex - request_channel = request_channel_template.format(account_id=account_id) - request_queue = request_queue_template.format(account_id=account_id) - response_channel = response_channel_template.format(account_id=account_id, request_id=request_id) - response_list = response_list_template.format(account_id=account_id, request_id=request_id) - response_key = response_key_template.format(account_id=account_id, request_id=request_id) - request = { - "schema_version": 1, - "request_id": request_id, - "account_id": account_id, - "method": method, - "params": params or {}, - "reply_channel": response_channel, - "reply_list": response_list, - "reply_key": response_key, - "ttl_seconds": ttl_seconds, - } - payload = encode_rpc_request_payload(request) - if str(transport or "queue").lower() in ("queue", "list", "blpop"): - redis_client.rpush(request_queue, payload) - redis_client.expire(request_queue, max(60, int(ttl_seconds))) - deadline = time.time() + float(timeout_seconds) - while True: - raw_response = redis_client.get(response_key) - if raw_response: - return json.loads(decode_text(raw_response)) - remaining = deadline - time.time() - if remaining <= 0: - break - wait_timeout = max(1, int(min(remaining, 1.0) + 0.999)) - try: - item = redis_client.blpop(response_list, timeout=wait_timeout) - except Exception as exc: - if _is_redis_timeout(exc): - continue - raise - if item: - raw_response = item[1] if isinstance(item, (list, tuple)) and len(item) >= 2 else item - try: - redis_client.delete(response_list) - except Exception: - pass - return json.loads(decode_text(raw_response)) - raw_response = redis_client.get(response_key) - if raw_response: - return json.loads(decode_text(raw_response)) - raise TimeoutError( - "redis rpc timeout: %s account_id=%s request_queue=%s" % (method, account_id, request_queue) - ) - - pubsub = redis_client.pubsub(ignore_subscribe_messages=True) - try: - pubsub.subscribe(response_channel) - redis_client.publish(request_channel, payload) - deadline = time.time() + float(timeout_seconds) - while True: - remaining = deadline - time.time() - if remaining <= 0: - break - message = pubsub.get_message(timeout=remaining) - if not message or message.get("type") != "message": - continue - response = json.loads(decode_text(message.get("data"))) - if response.get("request_id") == request_id: - return response - raw_response = redis_client.get(response_key) - if raw_response: - return json.loads(decode_text(raw_response)) - raise TimeoutError("redis rpc timeout: %s" % method) - finally: - try: - pubsub.close() - except Exception: - pass diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/risk_guard.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/risk_guard.py deleted file mode 100644 index 3176e76..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/risk_guard.py +++ /dev/null @@ -1,45 +0,0 @@ -"""信号执行前的轻量风控和数量计算。""" - -from .code_utils import normalize_stock_code, round_buy_volume, round_sell_volume -from .models import SignalAction, TradeSignal - - -class RiskDecision: - def __init__(self, allowed, reason="", volume=0, stock_code=""): - self.allowed = allowed - self.reason = reason - self.volume = volume - self.stock_code = stock_code - - -def build_trade_volume(signal: TradeSignal, positions): - code = normalize_stock_code(signal.stock_code) - if signal.action == SignalAction.BUY: - return RiskDecision(True, volume=round_buy_volume(code, signal.amount), stock_code=code) - - if signal.action == SignalAction.SELL: - position = positions.get(code) - if not position or position.available <= 0: - return RiskDecision(False, "no_available_position", stock_code=code) - if signal.amount is not None: - raw_volume = min(int(signal.amount), int(position.available)) - sell_all = raw_volume == int(position.available) - else: - pct = float(signal.percentage or 100) - raw_volume = int(int(position.available) * pct / 100.0) - sell_all = pct >= 100 - volume = round_sell_volume(code, raw_volume, sell_all=sell_all) - if volume <= 0: - return RiskDecision(False, "volume_below_min_lot", stock_code=code) - return RiskDecision(True, volume=volume, stock_code=code) - - return RiskDecision(False, f"unsupported_action:{signal.action}", stock_code=code) - - -def validate_signal(signal, now, positions): - if signal.is_expired(now): - return RiskDecision(False, "expired", stock_code=signal.stock_code) - decision = build_trade_volume(signal, positions) - if decision.allowed and decision.volume <= 0: - return RiskDecision(False, "invalid_volume", stock_code=decision.stock_code) - return decision diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/runner.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/runner.py deleted file mode 100644 index 0806be5..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/runner.py +++ /dev/null @@ -1,71 +0,0 @@ -"""大 QMT 运行文件可复用的转发入口。""" - -import datetime as _dt -import traceback - -from .logging_setup import get_logger - -_log = get_logger("runner") - -_APP = None - - -def reset_app(): - global _APP - _APP = None - - -def get_app(): - return _APP - - -def init_app(context_info, app_factory): - global _APP - _APP = app_factory(context_info) - if hasattr(_APP, "on_init"): - _APP.on_init(context_info) - return _APP - - -def tick_app(context_info, now=None): - if _APP is None: - return None - now = now or _dt.datetime.now() - try: - return _APP.tick(now) - except Exception: - _log.error("tick_app failed:\n%s", traceback.format_exc()) - return None - - -def forward_order_event(event): - # Unguarded events reach QMT's order_callback, which stops the strategy on - # raise. Guard like tick_app so a bad event (e.g. redis outage during the - # position-sync publish) never stops the strategy. - if _APP is None: - return None - try: - return _APP.on_order_event(event) - except Exception: - _log.error("forward_order_event failed:\n%s", traceback.format_exc()) - return None - - -def forward_trade_event(event): - if _APP is None: - return None - try: - return _APP.on_trade_event(event) - except Exception: - _log.error("forward_trade_event failed:\n%s", traceback.format_exc()) - return None - - -def sync_positions_app(reason="manual"): - if _APP is None: - return None - try: - return _APP.sync_positions(reason) - except Exception: - _log.error("sync_positions_app failed:\n%s", traceback.format_exc()) - return None diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/runtime_bigqmt.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/runtime_bigqmt.py deleted file mode 100644 index 7e58d97..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/runtime_bigqmt.py +++ /dev/null @@ -1,19 +0,0 @@ -"""大 QMT 运行环境适配器骨架。""" - -import datetime as _dt - - -class BigQmtRuntimeAdapter: - def __init__(self, context_info): - self.context_info = context_info - - def now(self): - return _dt.datetime.now() - - @staticmethod - def to_order_event(order): - return order - - @staticmethod - def to_trade_event(trade): - return trade diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/__init__.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/__init__.py deleted file mode 100644 index 10b0be5..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Pluggable transport layer for the BigQMT RPC bridge. - -The :class:`~bigqmt_signal_trader.transports.base.RpcTransport` interface owns -the wire: how a request dict travels from the client to the QMT server and how -the response dict travels back. ``redis`` is the reference implementation; the -same business layer (handlers / ``process_request`` / ``to_jsonable``) runs -unchanged over any transport. - -Select a transport with ``rpc.transport`` in the config (default ``"redis"``). -See :mod:`~bigqmt_signal_trader.transports.factory`. -""" - -from .base import RpcTransport, TransportError, TransportTimeout -from .factory import build_transport - -__all__ = ["RpcTransport", "TransportError", "TransportTimeout", "build_transport"] diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/base.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/base.py deleted file mode 100644 index 1860b99..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/base.py +++ /dev/null @@ -1,121 +0,0 @@ -"""Abstract transport interface for the BigQMT RPC bridge. - -A transport owns the request/response wire. The business layer (handlers, -``process_request``, ``to_jsonable``, ``enqueue_payload``, ``drain_pending``) -is transport-agnostic; it only deals with request/response dicts. - -Two roles, one interface ------------------------- -* **Client side** — :meth:`RpcTransport.send_request`: send a request dict and - block for the matching response dict (matched by ``request_id``). -* **Server side** — :meth:`RpcTransport.start_receiving` registers a callback - ``on_request(request_dict)`` invoked per inbound request; the callback returns - the response dict. :meth:`RpcTransport.send_response` delivers a response - back to the client that sent ``request_dict`` (reply routing info is read - from the request). - -The request dict always carries the existing envelope (``schema_version``, -``request_id``, ``account_id``, ``method``, ``params``). It MAY carry reply -routing hints (``reply_key``/``reply_channel``/``reply_list``/``ttl_seconds``); -Redis uses them, other transports may ignore them and use native routing. -""" - - -class TransportError(RuntimeError): - """A transport failed (connection lost, encode error, etc.).""" - - -class TransportTimeout(TimeoutError): - """A request did not complete within the timeout window.""" - - -class RpcTransport(object): - """Abstract request/response transport. Concrete implementations own the wire. - - Subclasses MUST override :meth:`send_request`, - :meth:`start_receiving`, :meth:`send_response`, and :meth:`stop`. - """ - - name = "abstract" - - def __init__(self, account_id="", print_prefix="[bigqmt_rpc]"): - self.account_id = str(account_id or "") - self.print_prefix = print_prefix - self._on_request = None - self._running = False - - # -- client side ------------------------------------------------------- - def send_request(self, request, timeout_seconds): - """Send a request dict and block for the response dict. - - ``request`` is the full request envelope. Returns the response dict - (with ``request_id`` matching). Raises :class:`TransportTimeout` if no - response arrives within ``timeout_seconds``. - """ - raise NotImplementedError - - # -- server side ------------------------------------------------------- - def start_receiving(self, on_request): - """Begin accepting inbound requests on the server side. - - ``on_request(request_dict)`` is invoked per inbound request and MUST - return the response dict. Implementations may spawn a background - thread. Safe to call once per transport instance. - """ - self._on_request = on_request - self._running = True - - def send_response(self, request, response): - """Deliver ``response`` back to the client that sent ``request``. - - Reply routing is read from ``request`` (e.g. ``reply_key`` / - ``reply_channel`` / ``reply_list`` for Redis, or a native peer handle - for ZMQ). Must be safe to call from the request-handling callback. - """ - raise NotImplementedError - - def stop(self): - """Stop receiving and release any sockets/connections/threads.""" - self._running = False - self._on_request = None - - def deliver(self, request): - """Internal: invoke the registered ``on_request`` callback. - - Concrete transports call this when an inbound request arrives. If the - callback returns a non-None response dict, it is delivered back to the - client via :meth:`send_response` automatically — so a callback only - needs to ``return response``. Callbacks that send the response - themselves (e.g. the Redis service path, which routes through - ``_publish_response``) should return ``None`` to suppress the auto-send. - - Handler exceptions are turned into an ``ok=False`` response envelope so - the receive loop keeps running. - """ - callback = self._on_request - if callback is None: - return None - try: - response = callback(request) - except Exception as exc: # noqa: BLE001 - transport must survive - import datetime as _dt - - response = { - "schema_version": 1, - "request_id": str((request or {}).get("request_id") or ""), - "account_id": str((request or {}).get("account_id") or self.account_id or ""), - "method": str((request or {}).get("method") or ""), - "ok": False, - "data": None, - "error": "%s: %s" % (exc.__class__.__name__, exc), - "handled_at": _dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), - } - if response is not None: - try: - self.send_response(request, response) - except Exception: - pass - return response - - def __repr__(self): - return "<%s account_id=%r>" % (self.__class__.__name__, self.account_id) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/factory.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/factory.py deleted file mode 100644 index b644755..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/factory.py +++ /dev/null @@ -1,126 +0,0 @@ -"""Transport factory: pick a transport backend by name. - -``build_transport(name, config, ...)`` returns a ready transport instance. -``name`` is the ``rpc.transport`` config value (default ``"redis"``). Unknown -names raise :class:`ValueError`. Optional dependencies (``zmq``, a mysql -driver) are imported lazily; a missing dependency surfaces as a clear -``ImportError`` only when that transport is actually selected. -""" - -from .base import RpcTransport -from .redis_transport import RedisTransport - - -KNOWN_TRANSPORTS = ("redis", "zmq", "mysql", "shm") - - -def build_transport( - name, - config=None, - account_id="", - print_prefix="[bigqmt_rpc]", -): - """Construct a transport by name. - - ``config`` is the ``rpc`` config dict. Each backend reads its own sub-keys - (``config["zmq"]``, ``config["mysql"]``); Redis reads the legacy keys - (``request_channel_template`` etc.) plus ``redis_client``/ - ``response_redis_client`` that the caller may inject. - """ - config = dict(config or {}) - name = str(name or "redis").lower() - - if name in ("redis", "", "default"): - return _build_redis(config, account_id, print_prefix) - if name == "zmq": - return _build_zmq(config, account_id, print_prefix) - if name == "mysql": - return _build_mysql(config, account_id, print_prefix) - if name == "shm": - return _build_shm(config, account_id, print_prefix) - raise ValueError( - "unknown rpc transport %r (known: %s)" % (name, ", ".join(KNOWN_TRANSPORTS)) - ) - - -def _build_redis(config, account_id, print_prefix): - redis_client = config.get("redis_client") - if redis_client is None: - from ..adapters.redis_common import build_redis_client - - redis_config = dict(config.get("redis") or {}) - redis_client = build_redis_client(redis_config) - response_redis_client = config.get("response_redis_client") - if response_redis_client is None: - response_redis_client = redis_client - return RedisTransport( - redis_client, - account_id=account_id, - response_redis_client=response_redis_client, - request_channel_template=config.get( - "request_channel_template", "bigqmt:rpc:req:{account_id}" - ), - request_queue_template=config.get( - "request_queue_template", "bigqmt:rpc:queue:{account_id}" - ), - response_channel_template=config.get( - "response_channel_template", "bigqmt:rpc:resp:{account_id}:{request_id}" - ), - response_list_template=config.get( - "response_list_template", "bigqmt:rpc:respq:{account_id}:{request_id}" - ), - response_key_template=config.get( - "response_key_template", "bigqmt:rpc:resp:{account_id}:{request_id}" - ), - response_ttl_seconds=int(config.get("response_ttl_seconds", 60)), - queue_poll_interval_seconds=float(config.get("queue_poll_interval_seconds", 0.02)), - debug_log_limit=int(config.get("debug_log_limit", 0)), - print_prefix=print_prefix, - ) - - -def _build_zmq(config, account_id, print_prefix): - from .zmq_transport import ZmqTransport - - zmq_config = dict(config.get("zmq") or {}) - # Wire up service discovery: if the caller injected a redis_client (server - # side) or provided redis connection settings, the ZMQ transport can - # publish/look up the actual bound port when the default port is taken. - discovery_client = zmq_config.get("discovery_redis_client") - if discovery_client is None and config.get("redis_client") is not None: - discovery_client = config.get("redis_client") - zmq_config["discovery_redis_client"] = discovery_client - if discovery_client is None and config.get("redis"): - # Build a small client just for discovery from the redis config block. - try: - from ..adapters.redis_common import build_redis_client - - discovery_client = build_redis_client(dict(config.get("redis") or {})) - zmq_config["discovery_redis_client"] = discovery_client - except Exception: - pass - return ZmqTransport.from_config( - zmq_config, - account_id=account_id, - print_prefix=print_prefix, - ) - - -def _build_mysql(config, account_id, print_prefix): - from .mysql_transport import MysqlTransport - - return MysqlTransport.from_config( - config.get("mysql") or {}, - account_id=account_id, - print_prefix=print_prefix, - ) - - -def _build_shm(config, account_id, print_prefix): - from .shm_transport import SharedMemoryTransport - - return SharedMemoryTransport( - account_id=account_id, - print_prefix=print_prefix, - **dict(config.get("shm") or {}) - ) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/mysql_transport.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/mysql_transport.py deleted file mode 100644 index 4a332be..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/mysql_transport.py +++ /dev/null @@ -1,462 +0,0 @@ -"""MySQL transport for the BigQMT RPC bridge. - -A compatibility-oriented backend for environments where Redis/ZMQ are -unavailable but a relational DB is. Latency is dominated by polling cadence, -so this is NOT a low-latency path (expect tens of ms); use it when the -deployment constraints rule out the others. - -Schema (auto-created on first connect):: - - CREATE TABLE bigqmt_rpc_requests ( - request_id VARCHAR(64) PRIMARY KEY, - account_id VARCHAR(64) NOT NULL, - payload MEDIUMTEXT NOT NULL, - created_at DOUBLE NOT NULL, - claimed_at DOUBLE NULL, - INDEX idx_account_created (account_id, created_at) - ); - CREATE TABLE bigqmt_rpc_responses ( - request_id VARCHAR(64) PRIMARY KEY, - payload MEDIUMTEXT NOT NULL, - created_at DOUBLE NOT NULL - ); - -The client ``INSERT``s a request row and polls ``bigqmt_rpc_responses`` by -``request_id``; the server ``SELECT ... FOR UPDATE SKIP LOCKED`` (or a -``claimed_at`` flag on older engines) claims a request, invokes the handler, -then ``INSERT``s the response row. Rows are cleaned up lazily by TTL. - -Any DB-API 2.0 driver works (pymysql, mysql-connector, sqlite3 for tests). -Pass ``driver="pymysql"`` / ``"sqlite3"`` etc. via config. -""" - -import json -import threading -import time -import uuid - -from ..adapters.redis_common import decode_text -from ..redis_rpc import encode_rpc_request_payload, decode_rpc_request_payload -from .base import RpcTransport, TransportError, TransportTimeout - - -REQUESTS_TABLE = "bigqmt_rpc_requests" -RESPONSES_TABLE = "bigqmt_rpc_responses" - - -_SCHEMA = [ - """CREATE TABLE IF NOT EXISTS {requests} ( - request_id VARCHAR(64) PRIMARY KEY, - account_id VARCHAR(64) NOT NULL, - payload MEDIUMTEXT NOT NULL, - created_at DOUBLE NOT NULL, - claimed_at DOUBLE NULL - )""", - """CREATE TABLE IF NOT EXISTS {responses} ( - request_id VARCHAR(64) PRIMARY KEY, - payload MEDIUMTEXT NOT NULL, - created_at DOUBLE NOT NULL - )""", - "CREATE INDEX IF NOT EXISTS idx_{requests}_account_created ON {requests} (account_id, created_at)", -] - - -def _loads(raw): - if isinstance(raw, dict): - return dict(raw) - text = decode_text(raw) - text = decode_rpc_request_payload(text) - return json.loads(text) - - -class MysqlTransport(RpcTransport): - """Polling-based transport over a relational DB. - - Both client and server open a short-lived connection per operation to keep - the implementation driver-agnostic and avoid cross-thread cursor state. - For high throughput a connection pool would help; this backend targets - compatibility, not throughput. - """ - - name = "mysql" - - def __init__( - self, - driver="pymysql", - connect_kwargs=None, - requests_table=REQUESTS_TABLE, - responses_table=RESPONSES_TABLE, - account_id="", - print_prefix="[bigqmt_rpc]", - poll_interval_seconds=0.02, - row_ttl_seconds=120, - background_threads=True, - pool_config=None, - use_pool=True, - ): - super(MysqlTransport, self).__init__(account_id=account_id, print_prefix=print_prefix) - self.driver = driver - self.connect_kwargs = dict(connect_kwargs or {}) - self.requests_table = requests_table - self.responses_table = responses_table - self.poll_interval_seconds = max(0.001, float(poll_interval_seconds)) - self.row_ttl_seconds = int(row_ttl_seconds) - self.background_threads = bool(background_threads) - self._thread = None - self._schema_ready = False - self.use_pool = bool(use_pool) - self.pool_config = dict(pool_config or {}) - self._pool = None - # paramstyle: mysql drivers use "format" (%s), sqlite3 uses "qmark" (?). - # Resolved lazily on first connect. - self._placeholder = None - - def _resolve_placeholder(self, mod): - style = getattr(mod, "paramstyle", "format") - if style == "qmark": - return "?" - return "%s" # format / pyformat / default - - def _ph(self): - # Return the placeholder char (resolving lazily). - if self._placeholder is None: - try: - mod = __import__(self.driver) - self._placeholder = self._resolve_placeholder(mod) - except ImportError: - self._placeholder = "%s" - return self._placeholder - - def _sql(self, template): - """Render a SQL template: fill {t}/{requests}/{responses} table names - and swap the standard ``%s`` placeholder for the driver's paramstyle.""" - return template.format( - t=None, # not used; callers format table names themselves - requests=self.requests_table, - responses=self.responses_table, - ).replace("__PH__", self._ph()) - - @classmethod - def from_config(cls, config, account_id="", print_prefix="[bigqmt_rpc]"): - config = dict(config or {}) - driver = config.get("driver", "pymysql") - connect_kwargs = dict(config.get("connect_kwargs") or {}) - # Allow flat keys (host/port/user/...) as a convenience. - for key in ("host", "port", "user", "password", "database", "charset"): - if key in config and key not in connect_kwargs: - connect_kwargs[key] = config[key] - return cls( - driver=driver, - connect_kwargs=connect_kwargs, - requests_table=config.get("requests_table", REQUESTS_TABLE), - responses_table=config.get("responses_table", RESPONSES_TABLE), - account_id=config.get("account_id", account_id), - print_prefix=print_prefix, - poll_interval_seconds=float(config.get("poll_interval_seconds", 0.02)), - row_ttl_seconds=int(config.get("row_ttl_seconds", 120)), - background_threads=bool(config.get("background_threads", True)), - pool_config=config.get("pool_config"), - use_pool=bool(config.get("use_pool", True)), - ) - - # -- driver access / connection pool ---------------------------------- - def _import_driver(self): - try: - return __import__(self.driver) - except ImportError as exc: # pragma: no cover - depends on env - raise TransportError( - "db driver %r is required for the mysql transport: %s" - % (self.driver, exc) - ) - - def _build_pool(self): - """Create a DBUtils PooledDB backed by the configured driver. - - Works with any DB-API 2.0 driver (pymysql, mysql.connector, sqlite3, - ...). Pool sizing comes from ``pool_config``; connection kwargs are - forwarded to the driver's ``connect()``. - """ - try: - from dbutils.pooled_db import PooledDB - except ImportError as exc: - raise TransportError( - "DBUtils is required for the mysql transport connection pool: %s" % exc - ) - driver = self._import_driver() - cfg = dict(self.pool_config) - # Sensible defaults for an RPC workload: small idle pool, modest cap, - # reuse connections across threads. Callers override via pool_config. - mincached = cfg.pop("mincached", 1) - maxcached = cfg.pop("maxcached", 4) - maxshared = cfg.pop("maxshared", 3) - maxconnections = cfg.pop("maxconnections", 8) - blocking = cfg.pop("blocking", True) - maxusage = cfg.pop("maxusage", 0) - reset = cfg.pop("reset", True) - # Whatever remains in cfg is treated as extra creator kwargs (e.g. - # ping, setsession) and merged under the connect kwargs. - extra = cfg - connect_kwargs = self._pooled_connect_args() - connect_kwargs.update(extra) - return PooledDB( - creator=driver, - mincached=mincached, - maxcached=maxcached, - maxshared=maxshared, - maxconnections=maxconnections, - blocking=blocking, - maxusage=maxusage, - reset=reset, - **connect_kwargs - ) - - def _pooled_connect_args(self): - """Return the kwargs to forward to the driver's connect(). - - Stripped of empty credential fields so drivers that reject empty - username/password (e.g. pymysql with auth plugin) don't choke. - """ - cfg = dict(self.connect_kwargs) - if not cfg.get("user") and "user" in cfg: - cfg.pop("user") - if not cfg.get("password") and "password" in cfg: - cfg.pop("password") - return cfg - - def _connect(self): - if not self.use_pool: - return self._import_driver().connect(**self.connect_kwargs) - if self._pool is None: - self._pool = self._build_pool() - # PooledDB.connection() hands out a pooled connection; calling .close() - # on it returns it to the pool rather than closing the underlying socket. - return self._pool.connection() - - def _ensure_schema(self): - if self._schema_ready: - return - ctx = {"requests": self.requests_table, "responses": self.responses_table} - conn = self._connect() - try: - cur = conn.cursor() - for stmt in _SCHEMA: - try: - cur.execute(stmt.format(**ctx)) - except Exception: - # "CREATE INDEX IF NOT EXISTS" is not supported on some - # MySQL versions; the index is an optimization, ignore failure. - pass - conn.commit() - self._schema_ready = True - finally: - try: - conn.close() - except Exception: - pass - - def _now(self): - return time.time() - - # -- client side ------------------------------------------------------ - def send_request(self, request, timeout_seconds): - self._ensure_schema() - request = dict(request) - request.setdefault("request_id", uuid.uuid4().hex) - request_id = str(request["request_id"]) - request.setdefault("account_id", self.account_id) - payload = encode_rpc_request_payload(request) - conn = self._connect() - try: - cur = conn.cursor() - cur.execute( - self._sql( - "INSERT INTO {requests} (request_id, account_id, payload, created_at, claimed_at) " - "VALUES (__PH__, __PH__, __PH__, __PH__, NULL)" - ), - (request_id, str(request.get("account_id") or self.account_id), payload, self._now()), - ) - conn.commit() - finally: - try: - conn.close() - except Exception: - pass - - deadline = time.time() + float(timeout_seconds) - while time.time() < deadline: - conn = self._connect() - try: - cur = conn.cursor() - cur.execute( - self._sql("SELECT payload FROM {responses} WHERE request_id = __PH__"), - (request_id,), - ) - row = cur.fetchone() - if row: - payload = row[0] - try: - cur.execute( - self._sql("DELETE FROM {responses} WHERE request_id = __PH__"), - (request_id,), - ) - conn.commit() - except Exception: - pass - return _loads(payload) - finally: - try: - conn.close() - except Exception: - pass - time.sleep(self.poll_interval_seconds) - raise TransportTimeout("mysql rpc timeout: %s" % request.get("method")) - - # -- server side ------------------------------------------------------ - def start_receiving(self, on_request, background_threads=None): - super(MysqlTransport, self).start_receiving(on_request) - self._ensure_schema() - if background_threads is None: - background_threads = self.background_threads - if not background_threads: - print("%s mysql polling table=%s background_threads=False" % ( - self.print_prefix, self.requests_table)) - return - self._thread = threading.Thread( - target=self._poll_loop, name="bigqmt-mysql-rpc", daemon=True - ) - self._thread.start() - print("%s mysql started polling table=%s" % ( - self.print_prefix, self.requests_table)) - - def _poll_loop(self): - while self._running: - try: - self._claim_and_handle_batch() - except Exception as exc: - if not self._running: - break - print("%s mysql poll failed: %s" % (self.print_prefix, exc)) - time.sleep(0.5) - continue - time.sleep(self.poll_interval_seconds) - - def _claim_and_handle_batch(self, max_items=20): - conn = self._connect() - claimed = [] - try: - cur = conn.cursor() - # Claim rows: mark claimed_at so concurrent servers skip them. - # Uses an atomic UPDATE ... WHERE claimed_at IS NULL with a cap. - cur.execute( - self._sql( - "SELECT request_id, payload FROM {requests} WHERE account_id = __PH__ " - "AND claimed_at IS NULL ORDER BY created_at ASC LIMIT __PH__" - ), - (self.account_id, int(max_items)), - ) - rows = cur.fetchall() - now = self._now() - for request_id, payload in rows: - cur.execute( - self._sql( - "UPDATE {requests} SET claimed_at = __PH__ WHERE request_id = __PH__ " - "AND claimed_at IS NULL" - ), - (now, request_id), - ) - if cur.rowcount > 0: - claimed.append((request_id, payload)) - conn.commit() - finally: - try: - conn.close() - except Exception: - pass - for request_id, payload in claimed: - try: - request = _loads(payload) - request["request_id"] = request_id - except Exception as exc: - print("%s mysql decode failed: %s" % (self.print_prefix, exc)) - self._delete_request(request_id) - continue - try: - self.deliver(request) - except Exception as exc: - print("%s mysql deliver failed: %s" % (self.print_prefix, exc)) - self._delete_request(request_id) - - def _delete_request(self, request_id): - conn = self._connect() - try: - cur = conn.cursor() - cur.execute( - self._sql("DELETE FROM {requests} WHERE request_id = __PH__"), - (request_id,), - ) - conn.commit() - except Exception: - pass - finally: - try: - conn.close() - except Exception: - pass - - def send_response(self, request, response): - request_id = str( - response.get("request_id") or request.get("request_id") or "" - ) - payload = encode_rpc_request_payload(response) - # DELETE-then-INSERT is portable across MySQL and sqlite (avoids the - # MySQL-only ON DUPLICATE KEY / REPLACE syntax). One connection, one txn. - conn = self._connect() - try: - cur = conn.cursor() - cur.execute( - self._sql("DELETE FROM {responses} WHERE request_id = __PH__"), - (request_id,), - ) - cur.execute( - self._sql( - "INSERT INTO {responses} (request_id, payload, created_at) " - "VALUES (__PH__, __PH__, __PH__)" - ), - (request_id, payload, self._now()), - ) - conn.commit() - except Exception as exc: - try: - conn.rollback() - except Exception: - pass - print("%s mysql response write failed: %s" % (self.print_prefix, exc)) - finally: - try: - conn.close() - except Exception: - pass - conn.close() - except Exception: - pass - - # -- non-background drain (strategy adjust thread) -------------------- - def drain_request_queue(self, max_items=20): - if not self._running: - return 0 - before = 0 # _claim_and_handle_batch handles its own count - self._claim_and_handle_batch(max_items=max_items) - return 0 - - def stop(self): - super(MysqlTransport, self).stop() - if self._thread is not None and self._thread.is_alive(): - self._thread.join(1.0) - self._thread = None - # Close the connection pool so background connections are released. - if self._pool is not None: - try: - self._pool.close() - except Exception: - pass - self._pool = None diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/redis_transport.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/redis_transport.py deleted file mode 100644 index 3095136..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/redis_transport.py +++ /dev/null @@ -1,424 +0,0 @@ -"""Redis transport for the BigQMT RPC bridge. - -This is the reference transport and the default. It preserves the exact wire -behavior of the original ``RedisPubSubRpcService``: - -* Client ``send_request``: ``RPUSH`` the (base64-obfuscated) request onto the - per-account request queue, then ``BLPOP`` the per-request response list with - a ``GET response_key`` fallback. A ``pubsub`` transport variant is kept for - callers that pass ``transport="pubsub"`` to ``call_redis_rpc``. -* Server receive: two background loops — a ``pubsub.subscribe`` loop and a - ``brpop`` queue loop. Either delivers inbound payloads to the registered - ``on_request`` callback. -* Server ``send_response``: fan-out writes to ``reply_key`` (``SETEX``), - ``reply_list`` (``RPUSH`` + ``EXPIRE``) and ``reply_channel`` (``PUBLISH``). - -The module-level :func:`call_redis_rpc` helper keeps its original signature and -delegates here so existing callers and ``bench_latency.py`` are unchanged. -""" - -import threading -import time -import traceback -import uuid - -from ..adapters.redis_common import decode_text -from ..redis_rpc import ( - decode_rpc_request_payload, - encode_rpc_request_payload, -) -from .base import RpcTransport, TransportTimeout - -import json # noqa: E402 (kept here so transport owns all wire encoding) - - -REQUEST_CHANNEL_TEMPLATE = "bigqmt:rpc:req:{account_id}" -REQUEST_QUEUE_TEMPLATE = "bigqmt:rpc:queue:{account_id}" -RESPONSE_CHANNEL_TEMPLATE = "bigqmt:rpc:resp:{account_id}:{request_id}" -RESPONSE_LIST_TEMPLATE = "bigqmt:rpc:respq:{account_id}:{request_id}" -RESPONSE_KEY_TEMPLATE = "bigqmt:rpc:resp:{account_id}:{request_id}" - - -def _format(template, account_id, request_id): - if not template: - return "" - return template.format(account_id=account_id, request_id=request_id) - - -def _loads(raw_payload): - """Decode a wire payload (bytes/str/dict) into a request dict.""" - if isinstance(raw_payload, dict): - return dict(raw_payload) - text = decode_text(raw_payload) - text = decode_rpc_request_payload(text) - payload = json.loads(text) - if not isinstance(payload, dict): - raise ValueError("rpc payload must be a json object") - return payload - - -def _is_redis_timeout(exc): - name = exc.__class__.__name__.lower() - module = getattr(exc.__class__, "__module__", "") - text = str(exc).lower() - return ("redis" in module and "timeout" in name) or "timeout reading from socket" in text - - -class RedisTransport(RpcTransport): - """Redis-backed transport. Owns rpush/blpop/brpop/publish/setex.""" - - name = "redis" - - def __init__( - self, - redis_client, - account_id="", - response_redis_client=None, - request_channel_template=REQUEST_CHANNEL_TEMPLATE, - request_queue_template=REQUEST_QUEUE_TEMPLATE, - response_channel_template=RESPONSE_CHANNEL_TEMPLATE, - response_list_template=RESPONSE_LIST_TEMPLATE, - response_key_template=RESPONSE_KEY_TEMPLATE, - response_ttl_seconds=60, - queue_poll_interval_seconds=0.02, - debug_log_limit=0, - print_prefix="[bigqmt_rpc]", - ): - super(RedisTransport, self).__init__(account_id=account_id, print_prefix=print_prefix) - self.listen_redis = redis_client - self.redis = response_redis_client or redis_client - self.request_channel_template = request_channel_template - self.request_queue_template = request_queue_template - self.response_channel_template = response_channel_template - self.response_list_template = response_list_template - self.response_key_template = response_key_template - self.response_ttl_seconds = int(response_ttl_seconds) - self.queue_poll_interval_seconds = max(0.001, float(queue_poll_interval_seconds)) - self.debug_log_limit = int(debug_log_limit) - self._received_count = 0 - self._published_count = 0 - self._pubsub = None - self._thread = None - self._queue_thread = None - # Hooks so the service can observe/intercept received payloads (debug - # logging, inline-vs-deferred dispatch). When None, the request is - # delivered straight to the on_request callback. - self.on_raw_payload = None - - # -- properties mirroring the original service ------------------------- - @property - def request_channel(self): - return self.request_channel_template.format(account_id=self.account_id) - - @property - def request_queue(self): - return self.request_queue_template.format(account_id=self.account_id) - - def _response_clients(self): - clients = [self.redis] - if self.listen_redis is not self.redis: - clients.append(self.listen_redis) - return clients - - # -- client side ------------------------------------------------------- - def send_request(self, request, timeout_seconds, transport="queue"): - """Send ``request`` and block for the response dict. - - ``transport`` selects the Redis sub-transport: ``"queue"`` (default, - RPUSH+BLPOP) or ``"pubsub"`` (PUBLISH+subscribe). Kept for parity with - the original ``call_redis_rpc`` signature. - """ - return _call_redis_rpc( - self.listen_redis, - self.account_id, - request, - timeout_seconds=float(timeout_seconds), - transport=transport, - request_channel_template=self.request_channel_template, - request_queue_template=self.request_queue_template, - response_channel_template=self.response_channel_template, - response_list_template=self.response_list_template, - response_key_template=self.response_key_template, - ) - - # -- server side ------------------------------------------------------- - def start_receiving(self, on_request, background_threads=True): - """Spawn the pubsub + queue receive loops (unless ``background_threads``).""" - super(RedisTransport, self).start_receiving(on_request) - if not background_threads: - print( - "%s started queue=%s background_threads=False" - % (self.print_prefix, self.request_queue) - ) - return - if ( - self._thread is not None - and self._thread.is_alive() - and self._queue_thread is not None - and self._queue_thread.is_alive() - ): - return - self._thread = threading.Thread( - target=self._listen_loop, name="bigqmt-redis-rpc", daemon=True - ) - self._queue_thread = threading.Thread( - target=self._queue_loop, name="bigqmt-redis-rpc-queue", daemon=True - ) - self._thread.start() - self._queue_thread.start() - print( - "%s started channel=%s queue=%s" - % (self.print_prefix, self.request_channel, self.request_queue) - ) - - def _listen_loop(self): - while self._running: - try: - pubsub = self.listen_redis.pubsub(ignore_subscribe_messages=True) - self._pubsub = pubsub - pubsub.subscribe(self.request_channel) - if self.debug_log_limit > 0: - print( - "%s subscribed channel=%s" % (self.print_prefix, self.request_channel) - ) - while self._running: - message = pubsub.get_message(timeout=1.0) - if not self._running: - break - if not message or message.get("type") != "message": - continue - self._handle_received_payload(message.get("data"), "pubsub") - except Exception: - print( - "%s listener failed:\n%s" % (self.print_prefix, traceback.format_exc()) - ) - time.sleep(1.0) - finally: - try: - if self._pubsub is not None: - self._pubsub.close() - except Exception: - pass - self._pubsub = None - - def _queue_loop(self): - while self._running: - try: - if self.debug_log_limit > 0: - print( - "%s queue polling key=%s" % (self.print_prefix, self.request_queue) - ) - while self._running: - item = self.listen_redis.brpop(self.request_queue, timeout=1) - if not self._running: - break - if not item: - continue - raw = ( - item[1] - if isinstance(item, (list, tuple)) and len(item) >= 2 - else item - ) - self._handle_received_payload(raw, "queue") - except Exception: - print( - "%s queue listener failed:\n%s" - % (self.print_prefix, traceback.format_exc()) - ) - time.sleep(1.0) - - def _handle_received_payload(self, raw_payload, source): - self._received_count += 1 - if self.on_raw_payload is not None: - # Service wants to observe/intercept (e.g. debug log + dispatch fork). - self.on_raw_payload(raw_payload, source) - return - # Default: decode and deliver straight to the registered callback. - request = _loads(raw_payload) - self.deliver(request) - - def send_response(self, request, response): - """Fan out the response to reply_key/reply_list/reply_channel.""" - request_id = response.get("request_id") or request.get("request_id") or "" - account_id = response.get("account_id") or request.get("account_id") or self.account_id - payload = json.dumps(response, ensure_ascii=False) - ttl_seconds = int(request.get("ttl_seconds") or self.response_ttl_seconds) - response_key = request.get("reply_key") or _format( - self.response_key_template, account_id, request_id - ) - response_channel = request.get("reply_channel") or _format( - self.response_channel_template, account_id, request_id - ) - response_list = request.get("reply_list") - if response_key: - self._write_response_key(response_key, ttl_seconds, payload) - if response_list: - self._push_response_list(response_list, ttl_seconds, payload) - if response_channel: - self._publish_response_channel(response_channel, payload) - - def _write_response_key(self, response_key, ttl_seconds, payload): - first_error = None - wrote = 0 - for client in self._response_clients(): - try: - if ttl_seconds > 0: - client.setex(response_key, ttl_seconds, payload) - else: - client.set(response_key, payload) - wrote += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if wrote <= 0 and first_error is not None: - raise first_error - return wrote - - def _push_response_list(self, response_list, ttl_seconds, payload): - first_error = None - pushed = 0 - for client in self._response_clients(): - try: - client.rpush(response_list, payload) - if ttl_seconds > 0: - client.expire(response_list, ttl_seconds) - pushed += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if pushed <= 0 and first_error is not None: - raise first_error - return pushed - - def _publish_response_channel(self, response_channel, payload): - first_error = None - receivers = 0 - published = 0 - for client in self._response_clients(): - try: - receivers += int(client.publish(response_channel, payload) or 0) - published += 1 - except Exception as exc: - if first_error is None: - first_error = exc - if published <= 0 and first_error is not None: - raise first_error - self._published_count += 1 - if self._published_count <= self.debug_log_limit: - print("%s published response receivers=%s" % (self.print_prefix, receivers)) - return receivers - - # -- non-background drain helpers (used by the strategy adjust thread) - - def drain_request_queue(self, max_items=20): - processed = 0 - for _ in range(int(max_items)): - try: - item = self.listen_redis.lpop(self.request_queue) - except Exception as exc: - if _is_redis_timeout(exc): - print("%s ERROR drain timeout on LPOP queue=%s; skip this tick" % (self.print_prefix, self.request_queue)) - break - raise - if not item: - break - if self.on_raw_payload is not None: - self.on_raw_payload(item, "queue-drain") - else: - self.deliver(_loads(item)) - processed += 1 - return processed - - def stop(self): - super(RedisTransport, self).stop() - pubsub = self._pubsub - if pubsub is not None: - try: - pubsub.close() - except Exception: - pass - thread = self._thread - if thread is not None and thread.is_alive(): - thread.join(1.0) - queue_thread = self._queue_thread - if queue_thread is not None and queue_thread.is_alive(): - queue_thread.join(1.0) - self._thread = None - self._queue_thread = None - self._pubsub = None - - -def _call_redis_rpc( - redis_client, - account_id, - request, - timeout_seconds=3.0, - transport="queue", - request_channel_template=REQUEST_CHANNEL_TEMPLATE, - request_queue_template=REQUEST_QUEUE_TEMPLATE, - response_channel_template=RESPONSE_CHANNEL_TEMPLATE, - response_list_template=RESPONSE_LIST_TEMPLATE, - response_key_template=RESPONSE_KEY_TEMPLATE, - ttl_seconds=60, -): - """Client-side round trip. Accepts a pre-built request envelope.""" - request_id = request.get("request_id") or uuid.uuid4().hex - request_channel = request_channel_template.format(account_id=account_id) - request_queue = request_queue_template.format(account_id=account_id) - response_channel = response_channel_template.format( - account_id=account_id, request_id=request_id - ) - response_list = response_list_template.format(account_id=account_id, request_id=request_id) - response_key = response_key_template.format(account_id=account_id, request_id=request_id) - # Ensure reply routing is present (the original helper filled these in). - request = dict(request) - request.setdefault("request_id", request_id) - request.setdefault("reply_channel", response_channel) - request.setdefault("reply_list", response_list) - request.setdefault("reply_key", response_key) - request.setdefault("ttl_seconds", ttl_seconds) - request["request_id"] = request_id - payload = encode_rpc_request_payload(request) - - if str(transport or "queue").lower() in ("queue", "list", "blpop"): - redis_client.rpush(request_queue, payload) - redis_client.expire(request_queue, max(60, int(ttl_seconds))) - wait_timeout = max(1, int(float(timeout_seconds) + 0.999)) - item = redis_client.blpop(response_list, timeout=wait_timeout) - if item: - raw_response = ( - item[1] if isinstance(item, (list, tuple)) and len(item) >= 2 else item - ) - try: - redis_client.delete(response_list) - except Exception: - pass - return json.loads(decode_text(raw_response)) - raw_response = redis_client.get(response_key) - if raw_response: - return json.loads(decode_text(raw_response)) - raise TransportTimeout("redis rpc timeout: %s" % request.get("method")) - - pubsub = redis_client.pubsub(ignore_subscribe_messages=True) - try: - pubsub.subscribe(response_channel) - redis_client.publish(request_channel, payload) - deadline = time.time() + float(timeout_seconds) - while True: - remaining = deadline - time.time() - if remaining <= 0: - break - message = pubsub.get_message(timeout=remaining) - if not message or message.get("type") != "message": - continue - response = json.loads(decode_text(message.get("data"))) - if response.get("request_id") == request_id: - return response - raw_response = redis_client.get(response_key) - if raw_response: - return json.loads(decode_text(raw_response)) - raise TransportTimeout("redis rpc timeout: %s" % request.get("method")) - finally: - try: - pubsub.close() - except Exception: - pass diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/shm_transport.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/shm_transport.py deleted file mode 100644 index c3d4c7f..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/shm_transport.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Shared-memory transport stub. - -Reserved for a future low-latency same-host backend. Not implemented because -the QMT runtime ships Python 3.6, where ``multiprocessing.shared_memory`` is -unavailable (added in 3.8). A ``mmap``-plus-named-mutex implementation is -possible but non-trivial; until it lands, selecting this transport raises a -clear error so misconfiguration fails fast. -""" - -from .base import RpcTransport, TransportError - - -class SharedMemoryTransport(RpcTransport): - name = "shm" - - def __init__(self, account_id="", print_prefix="[bigqmt_rpc]", **kwargs): - super(SharedMemoryTransport, self).__init__( - account_id=account_id, print_prefix=print_prefix - ) - - def _unsupported(self): - raise TransportError( - "shared-memory transport is not implemented yet " - "(requires Python 3.8+ shared_memory or a custom mmap ring buffer)" - ) - - def send_request(self, request, timeout_seconds): - self._unsupported() - - def send_response(self, request, response): - self._unsupported() - - def start_receiving(self, on_request, **kwargs): - self._unsupported() diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/zmq_transport.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/zmq_transport.py deleted file mode 100644 index 09c94ed..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/transports/zmq_transport.py +++ /dev/null @@ -1,459 +0,0 @@ -"""ZeroMQ transport for the BigQMT RPC bridge. - -Designed for same-host low latency. Topology: - -* **Server** binds a ``ROUTER`` socket. Each inbound message arrives as - ``[identity, payload]``; the server remembers ``identity`` keyed by - ``request_id`` and replies with ``[identity, payload]`` so ZMQ routes the - response back to the originating client automatically. -* **Client** connects a ``DEALER`` socket (with a unique random identity), sends - ``[payload]``, then ``poll``/``recv`` for the response. DEALER gives each - client an asymmetric async path that pairs naturally with ROUTER. - -Wire framing is a single JSON payload per message. The original b64 stock-code -obfuscation (``encode_rpc_request_payload``) is applied too, so payloads stay -opaque even though ZMQ does not need it — keeps the wire uniform with Redis. - -Two threads on the server: the ROUTER recv loop, and a per-client is implicit -(ZMQ handles multiplexing). One thread on the client for recv is avoided by -using DEALER + ``poll`` (synchronous request/response fits the RPC model). -""" - -import json -import queue -import threading -import time -import uuid - -from ..adapters.redis_common import decode_text -from ..redis_rpc import ( - decode_rpc_request_payload, - encode_rpc_request_payload, -) -from .base import RpcTransport, TransportError, TransportTimeout - - -# ZMQ does not support ipc:// on Windows (it trips a signaler abort), so the -# default endpoint is tcp loopback. The port is derived from the account_id so -# distinct accounts don't collide on the same port; override via config when -# needed. Base 15560 keeps it clear of common dev ports. -DEFAULT_ZMQ_HOST = "127.0.0.1" -DEFAULT_ZMQ_BASE_PORT = 15560 -DEFAULT_ZMQ_PORT_RANGE = 100 # derived port = base + (account_id_int mod range) - - -def _default_zmq_port(account_id): - """Derive a stable port from account_id so each account gets its own socket.""" - text = str(account_id or "") - digits = "".join(ch for ch in text if ch.isdigit()) - try: - offset = int(digits) % DEFAULT_ZMQ_PORT_RANGE if digits else 0 - except ValueError: - offset = 0 - return DEFAULT_ZMQ_BASE_PORT + offset - - -def _default_zmq_address(account_id, host=None): - host = host or DEFAULT_ZMQ_HOST - return "tcp://%s:%d" % (host, _default_zmq_port(account_id)) - - -def _loads(raw): - if isinstance(raw, dict): - return dict(raw) - text = decode_text(raw) - text = decode_rpc_request_payload(text) - return json.loads(text) - - -class ZmqTransport(RpcTransport): - """ZMQ ROUTER/DEALER transport. - - The same instance plays both roles depending on method called: - ``send_request`` acts as a client (DEALER connect), ``start_receiving`` + - ``send_response`` act as a server (ROUTER bind). A deployment normally uses - one instance per role (the QMT process is the server; the external client - is the client). - """ - - name = "zmq" - - def __init__( - self, - bind_address=None, - connect_address=None, - host=None, - port=None, - account_id="", - print_prefix="[bigqmt_rpc]", - io_threads=1, - recv_timeout_seconds=1.0, - server_hwm=10000, - client_linger_ms=0, - discovery_redis_client=None, - discovery_key_template="bigqmt:zmq:addr:{account_id}", - discovery_ttl_seconds=300, - port_scan_range=50, - ): - super(ZmqTransport, self).__init__(account_id=account_id, print_prefix=print_prefix) - # Address resolution order: explicit bind_address/connect_address win; - # otherwise build tcp://host:port from host/port (port defaults to a - # value derived from account_id so distinct accounts don't collide). - resolved_host = host or DEFAULT_ZMQ_HOST - if port is not None: - resolved_port = int(port) - else: - resolved_port = _default_zmq_port(account_id) - default_addr = "tcp://%s:%d" % (resolved_host, resolved_port) - self.bind_address = bind_address or default_addr - self.connect_address = connect_address - self.bind_host = resolved_host - self.base_port = resolved_port - self.io_threads = int(io_threads) - self.recv_timeout_seconds = float(recv_timeout_seconds) - self.server_hwm = int(server_hwm) - self.client_linger_ms = int(client_linger_ms) - # Discovery remains available for clients, but a server must bind the - # configured address exactly. ``port_scan_range`` is retained only for - # backward-compatible config loading and is intentionally not used. - self.discovery_redis_client = discovery_redis_client - self.discovery_key_template = discovery_key_template - self.discovery_ttl_seconds = int(discovery_ttl_seconds) - self.port_scan_range = int(port_scan_range) - - self._zmq = None # imported lazily - self._ctx = None - # server state - self._router = None - self._router_thread = None - self._actual_bind_address = None # set after start_receiving() - self._pending_identities = {} # request_id -> client identity bytes - self._identity_lock = threading.Lock() - self._response_queue = queue.Queue() - self._queued_response_count = 0 - self._sent_response_count = 0 - # client state - self._dealer = None - self._client_lock = threading.Lock() - - # -- construction helper ---------------------------------------------- - @classmethod - def from_config(cls, config, account_id="", print_prefix="[bigqmt_rpc]"): - config = dict(config or {}) - return cls( - bind_address=config.get("bind_address"), - connect_address=config.get("connect_address"), - host=config.get("host"), - port=config.get("port"), - account_id=config.get("account_id", account_id), - print_prefix=print_prefix, - io_threads=int(config.get("io_threads", 1)), - recv_timeout_seconds=float(config.get("recv_timeout_seconds", 1.0)), - server_hwm=int(config.get("server_hwm", 10000)), - client_linger_ms=int(config.get("client_linger_ms", 0)), - discovery_redis_client=config.get("discovery_redis_client"), - discovery_key_template=config.get( - "discovery_key_template", "bigqmt:zmq:addr:{account_id}" - ), - discovery_ttl_seconds=int(config.get("discovery_ttl_seconds", 300)), - port_scan_range=int(config.get("port_scan_range", 50)), - ) - - # -- shared zmq context ----------------------------------------------- - def _ensure_zmq(self): - if self._zmq is None: - try: - import zmq # noqa: F401 - except ImportError as exc: # pragma: no cover - depends on env - raise TransportError( - "pyzmq is required for the zmq transport: %s" % exc - ) - self._zmq = zmq - if self._ctx is None: - self._ctx = self._zmq.Context.instance(self.io_threads) - return self._zmq, self._ctx - - # -- server side ------------------------------------------------------ - def _bind_configured_address(self): - """Bind exactly one configured address and reject duplicate servers.""" - zmq, ctx = self._ensure_zmq() - sock = ctx.socket(zmq.ROUTER) - sock.setsockopt(zmq.RCVHWM, self.server_hwm) - sock.setsockopt(zmq.SNDHWM, self.server_hwm) - sock.setsockopt(zmq.RCVTIMEO, int(self.recv_timeout_seconds * 1000)) - try: - sock.bind(self.bind_address) - except self._zmq.ZMQError as exc: - try: - sock.close(linger=0) - except Exception: - pass - if getattr(exc, "errno", None) == zmq.EADDRINUSE: - # 端口被占——通常是之前策略实例没正常停止。给出友好提示和解决步骤。 - print( - "%s ZMQ_BIND_CONFLICT: 端口 %s 被占用!" - % (self.print_prefix, self.bind_address) - ) - print( - "%s 原因:之前的 QMT 策略实例没正常停止,仍占着这个端口。" - % self.print_prefix - ) - print( - "%s 解决:1) 在 QMT 里停止旧策略再运行;2) 或等 60s 让系统释放端口;" - % self.print_prefix - ) - print( - "%s 3) 或改配置用别的端口(BIGQMT_REDIS_CONFIG.zmq.port)" - % self.print_prefix - ) - raise TransportError( - "ZMQ_BIND_CONFLICT address=%s; another bridge instance " - "already owns the configured endpoint" % self.bind_address - ) - raise - self._router = sock - self._actual_bind_address = self.bind_address - self._publish_discovery(self.bind_address) - - def _publish_discovery(self, address): - if self.discovery_redis_client is None: - return - key = self.discovery_key_template.format(account_id=self.account_id) - try: - self.discovery_redis_client.setex( - key, self.discovery_ttl_seconds, address - ) - except Exception as exc: - print("%s zmq discovery publish failed: %s" % (self.print_prefix, exc)) - - def _clear_discovery(self): - if self.discovery_redis_client is None: - return - key = self.discovery_key_template.format(account_id=self.account_id) - try: - self.discovery_redis_client.delete(key) - except Exception: - pass - - def start_receiving(self, on_request, background_threads=True): - super(ZmqTransport, self).start_receiving(on_request) - zmq, ctx = self._ensure_zmq() - self._bind_configured_address() - bound = self._actual_bind_address or self.bind_address - if not background_threads: - print( - "%s zmq bound=%s background_threads=False" - % (self.print_prefix, bound) - ) - return - self._router_thread = threading.Thread( - target=self._router_loop, name="bigqmt-zmq-rpc", daemon=True - ) - self._router_thread.start() - print( - "%s zmq started bound=%s" % (self.print_prefix, self.bind_address) - ) - - def _router_loop(self): - try: - while self._running: - self._drain_response_queue() - request = self._receive_request() - if request is not None: - self._deliver_request(request) - finally: - # Close the ROUTER socket on the thread that owns it. On Windows, - # closing a ZMQ socket from a different thread trips a signaler - # assertion (abort); closing it here is safe because this thread - # created and exclusively used it. - try: - self._router.close(linger=0) - except Exception: - pass - self._router = None - - def _receive_request(self, flags=0): - try: - frames = self._router.recv_multipart(flags=flags) - except self._zmq.Again: - return None - except Exception as exc: - if self._running: - print("%s zmq recv failed: %s" % (self.print_prefix, exc)) - if not flags: - time.sleep(0.5) - return None - if len(frames) < 2: - return None - identity, payload = frames[0], frames[-1] - try: - request = _loads(payload) - except Exception as exc: - print("%s zmq decode failed: %s" % (self.print_prefix, exc)) - return None - request_id = str(request.get("request_id") or uuid.uuid4().hex) - with self._identity_lock: - self._pending_identities[request_id] = identity - return request - - def _deliver_request(self, request): - started = time.perf_counter() - try: - self.deliver(request) - except Exception as exc: - print("%s zmq deliver failed: %s" % (self.print_prefix, exc)) - elapsed_ms = (time.perf_counter() - started) * 1000.0 - if elapsed_ms > 50.0: - print("%s zmq slow handler method=%s %.0fms" - % (self.print_prefix, request.get("method"), elapsed_ms)) - - def _drain_response_queue(self): - while True: - try: - identity, payload = self._response_queue.get_nowait() - except queue.Empty: - return - try: - self._router.send_multipart([identity, payload]) - self._sent_response_count += 1 - if self._sent_response_count <= 5: - print("%s zmq queued response sent" % self.print_prefix) - except Exception as exc: - print("%s zmq send failed: %s" % (self.print_prefix, exc)) - - def send_response(self, request, response): - if self._router is None: - raise TransportError("zmq server socket is not bound") - request_id = str( - response.get("request_id") or request.get("request_id") or "" - ) - with self._identity_lock: - identity = self._pending_identities.pop(request_id, None) - if identity is None: - # No matching peer — drop silently (client may have gone away). - return - payload = encode_rpc_request_payload(response).encode("utf-8") - if self._router_thread is not None and threading.current_thread() is not self._router_thread: - self._queued_response_count += 1 - if self._queued_response_count <= 5: - print("%s zmq response queued for router thread" % self.print_prefix) - self._response_queue.put((identity, payload)) - return - try: - self._router.send_multipart([identity, payload]) - except Exception as exc: - print("%s zmq send failed: %s" % (self.print_prefix, exc)) - - def drain_request_queue(self, max_items=20): - """Drain requests from the scheduled QMT thread when no receiver thread exists.""" - if self._router_thread is not None or self._router is None: - return 0 - processed = 0 - for _index in range(max(int(max_items), 0)): - request = self._receive_request(flags=self._zmq.NOBLOCK) - if request is None: - break - self._deliver_request(request) - processed += 1 - return processed - - # -- client side ------------------------------------------------------ - def _resolve_connect_address(self): - """Resolve the address to connect to. - - Order: explicit connect_address > discovery lookup > default derived. - Discovery lets the client find a server that had to move off the - default port because of a collision. - """ - if self.connect_address: - return self.connect_address - discovered = self._lookup_discovery() - if discovered: - return discovered - return _default_zmq_address(self.account_id) - - def _lookup_discovery(self): - if self.discovery_redis_client is None: - return None - key = self.discovery_key_template.format(account_id=self.account_id) - try: - raw = self.discovery_redis_client.get(key) - except Exception: - return None - if not raw: - return None - try: - text = raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else str(raw) - except Exception: - return None - return text or None - - def _ensure_dealer(self): - zmq, ctx = self._ensure_zmq() - if self._dealer is None: - address = self._resolve_connect_address() - sock = ctx.socket(zmq.DEALER) - # Unique identity so ROUTER can route replies back to us. - sock.setsockopt(zmq.IDENTITY, uuid.uuid4().hex.encode("utf-8")[:16]) - sock.setsockopt(zmq.LINGER, self.client_linger_ms) - sock.connect(address) - self._dealer = sock - self.connect_address = address - return self._dealer - - def send_request(self, request, timeout_seconds, **_kwargs): - zmq = self._zmq or self._ensure_zmq()[0] - with self._client_lock: - dealer = self._ensure_dealer() - request = dict(request) - request.setdefault("request_id", uuid.uuid4().hex) - request_id = request["request_id"] - payload = encode_rpc_request_payload(request) - try: - dealer.send(payload.encode("utf-8")) - except Exception as exc: - raise TransportError("zmq send failed: %s" % exc) - deadline = time.time() + float(timeout_seconds) - poller = self._zmq.Poller() - poller.register(dealer, self._zmq.POLLIN) - while True: - remaining = deadline - time.time() - if remaining <= 0: - break - events = dict(poller.poll(timeout=int(remaining * 1000))) - if dealer in events: - frames = dealer.recv_multipart() - raw = frames[-1] - response = _loads(raw) - if response.get("request_id") == request_id: - return response - raise TransportTimeout("zmq rpc timeout: %s" % request.get("method")) - - # -- lifecycle -------------------------------------------------------- - def stop(self): - super(ZmqTransport, self).stop() - # Clear _running so the router loop exits; the loop closes its own - # socket (closing cross-thread trips a Windows signaler abort). - thread = self._router_thread - if thread is not None and thread.is_alive(): - thread.join(2.0) - if thread is None and self._router is not None: - try: - self._router.close(linger=0) - except Exception: - pass - self._router = None - self._router_thread = None - # If we were a server that published a discovery address, clear it so - # clients don't keep hitting a dead endpoint. - if self._actual_bind_address is not None: - self._clear_discovery() - self._actual_bind_address = None - with self._client_lock: - if self._dealer is not None: - try: - self._dealer.close(linger=self.client_linger_ms) - except Exception: - pass - self._dealer = None - # Do NOT terminate the shared context — other sockets/users may rely on it. diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/whole_quote_session.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/whole_quote_session.py deleted file mode 100644 index 2de841c..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/whole_quote_session.py +++ /dev/null @@ -1,204 +0,0 @@ -"""Client-side whole-quote subscription session. - -Owns the per-process state for ``subscribe_whole_quote``: the local -subscription table, the shared push-channel subscriber thread, and the -keepalive heartbeat thread. One session is shared by every ``subscribe_whole_quote`` -call in the process (``BigQmtXtData`` delegates here), so all subscriptions ride -a single push-channel connection and a single heartbeat loop. - -The big-QMT whole-quote callback is INCREMENTAL (only changed symbols), so a -subscription does not by itself deliver an initial full snapshot — callers layer -a ``get_full_tick`` prime on top (done in ``BigQmtXtData.subscribe_whole_quote``). -""" - -import threading - - -def _norm_topic(code_list): - return ",".join(sorted({str(c).strip().upper() for c in (code_list or []) if str(c or "").strip()})) - - -class WholeQuoteClientSession(object): - def __init__(self, rpc_call, push_channel, client_id, heartbeat_interval_seconds=3.0, sub_id_func=None, - push_silence_replay_heartbeats=10): - """``rpc_call`` is ``client.call``-shaped: fn(method, params) -> dict. - ``push_channel`` is a QuotePushChannel used purely as a subscriber. - ``sub_id_func`` (optional) mints subscription ids; defaults to a counter. - ``push_silence_replay_heartbeats``: after this many heartbeat rounds - without any push, replay subscriptions (covers server restarts where - keepalive keeps succeeding because the redis request queue buffers - during the restart window but the subscription table was reset).""" - self._rpc = rpc_call - self._channel = push_channel - self.client_id = str(client_id or "") - self._heartbeat_interval = float(heartbeat_interval_seconds) - self._push_silence_replay_heartbeats = int(push_silence_replay_heartbeats) - self._sub_id_func = sub_id_func - self._seq = 0 - self._lock = threading.RLock() - self._subscriptions = {} # sub_id -> {"topic": str, "callback": fn, "codes": [...]} - self._started = False - self._subscriber_active = False - self._subscribed_topics = frozenset() # topic set the subscriber covers now - self._heartbeat_thread = None - self._last_push_time = None # monotonic time of last incoming push - - # -- subscription lifecycle --------------------------------------------- - def subscribe_whole_quote(self, code_list, callback=None): - codes = [str(c) for c in (code_list or []) if str(c or "").strip()] - if not codes: - raise ValueError("code_list is required") - with self._lock: - sub_id = self._next_sub_id() - result = self._rpc( - "subscribe_whole_quote", - {"client_id": self.client_id, "sub_id": sub_id, "codes": codes}, - ) or {} - topic = str(result.get("topic") or result.get("combo_key") or _norm_topic(codes)) - with self._lock: - self._subscriptions[sub_id] = {"topic": topic, "callback": callback, "codes": codes} - self._sync_subscriber_locked() - return sub_id - - def unsubscribe_quote(self, sub_id): - with self._lock: - entry = self._subscriptions.pop(sub_id, None) - if entry is None: - return 0 - try: - self._rpc("unsubscribe_whole_quote", {"client_id": self.client_id, "sub_id": sub_id}) - finally: - with self._lock: - self._sync_subscriber_locked() - return 0 - - def has_subscription(self, sub_id): - with self._lock: - return sub_id in self._subscriptions - - def replay_subscriptions(self): - """Re-send subscribe for every active sub_id (server restart recovery). - Idempotent on the server (keyed by client_id+combo), so replays are safe.""" - with self._lock: - items = [(sid, dict(entry)) for sid, entry in self._subscriptions.items()] - for sub_id, entry in items: - self._rpc( - "subscribe_whole_quote", - {"client_id": self.client_id, "sub_id": sub_id, "codes": entry["codes"]}, - ) - - # -- heartbeat ------------------------------------------------------------- - def start(self): - with self._lock: - if self._started: - return - self._started = True - self._heartbeat_thread = threading.Thread( - target=self._heartbeat_loop, name="bigqmt-quote-keepalive", daemon=True - ) - self._heartbeat_thread.start() - - def stop(self): - with self._lock: - self._started = False - thread = self._heartbeat_thread - if thread is not None: - thread.join(timeout=1.0) - self._heartbeat_thread = None - - def _heartbeat_loop(self): - import time - - consecutive_failures = 0 - silence_rounds = 0 - prev_last_push = None - while True: - with self._lock: - if not self._started: - return - sub_ids = list(self._subscriptions.keys()) - last_push = self._last_push_time - if not sub_ids: - time.sleep(self._heartbeat_interval) - continue - failures = 0 - for sub_id in sub_ids: - try: - self._rpc("quote_keepalive", {"client_id": self.client_id, "sub_id": sub_id}) - except Exception: - failures += 1 - if failures: - consecutive_failures += 1 - elif consecutive_failures >= 3: - # Server is back after a restart window: replay subscriptions so - # the restarted server re-creates the big-QMT subscriptions (its - # state is gone). Idempotent on the server, so replays are safe. - self.replay_subscriptions() - consecutive_failures = 0 - else: - consecutive_failures = 0 - # Push-silence detection: a server restart can survive with keepalive - # succeeding (the redis request queue buffers during the restart - # window) while the subscription table was reset, so pushes stop. - # Replay when no push arrived for several heartbeat rounds (also - # covers the case where the very first prime push never arrived). - if last_push != prev_last_push: - silence_rounds = 0 # a push arrived since the last round - else: - silence_rounds += 1 - prev_last_push = last_push - if silence_rounds >= self._push_silence_replay_heartbeats: - self.replay_subscriptions() - silence_rounds = 0 - time.sleep(self._heartbeat_interval) - - # -- push routing ------------------------------------------------------------ - def _on_push(self, topic, data): - import time - - now = time.monotonic() - with self._lock: - self._last_push_time = now - callbacks = [ - entry["callback"] - for entry in self._subscriptions.values() - if entry["topic"] == topic and entry["callback"] is not None - ] - for callback in callbacks: - try: - callback(data) - except Exception: - pass - - def _sync_subscriber_locked(self): - """(Re)start the push-channel subscriber to cover exactly the active - topics. Reuses an existing subscriber when the topic set is unchanged; - stops it before restarting when the set changed. No-op when nothing is - subscribed (and stops the running subscriber in that case).""" - topics = sorted({entry["topic"] for entry in self._subscriptions.values()}) - active = frozenset(topics) - if active == self._subscribed_topics: - return - if not active: - if self._subscriber_active: - try: - self._channel.stop() - except Exception: - pass - self._subscriber_active = False - self._subscribed_topics = active - return - if self._subscriber_active: - try: - self._channel.stop() - except Exception: - pass - self._channel.start_subscriber(topics, self._on_push) - self._subscriber_active = True - self._subscribed_topics = active - - def _next_sub_id(self): - if self._sub_id_func is not None: - return self._sub_id_func() - self._seq += 1 - return self._seq diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader/xtquant_compat.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader/xtquant_compat.py deleted file mode 100644 index 8db1f66..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader/xtquant_compat.py +++ /dev/null @@ -1,2626 +0,0 @@ -"""MiniQMT-style client objects backed by Big QMT Redis RPC. - -This module is the replacement edge for existing code that already calls -``xt_trader.query_stock_positions(...)`` or ``xtdata.get_full_tick(...)``. -The Big QMT process remains the only place that touches QMT runtime APIs. -""" - -import os -import json -import time -import uuid -import queue as _queue -import threading -import importlib -import datetime as _dt -from typing import Any, Dict, Iterable, List, Optional - -from .full_tick_cache import request_full_tick_cache, wait_full_tick_cache -from .local_cache import LocalMarketCache -from .redis_rpc import call_redis_rpc - - -# Default OHLCV fields pulled + cached by get_local_data fallback_rpc. -DEFAULT_DOWNLOAD_FIELDS = ["open", "high", "low", "close", "volume", "amount"] -# Codes per get_market_data_ex request. One request carries a single RPC timeout, -# so a wide stock_list either fits or loses everything (issue #47). -DEFAULT_MARKET_DATA_CHUNK = 100 -_TIME_COL_NAMES = ("stime", "time", "index", "date", "datetime", "timetag") - - -def _as_list(value): - if value is None: - return [] - if isinstance(value, str): - return [value] - return list(value) - - -STOCK_BUY = 23 -STOCK_SELL = 24 -FIX_PRICE = 11 -LATEST_PRICE = 5 -MARKET_PEER_PRICE_FIRST = 44 -MARKET_SH_CONVERT_5_LIMIT = 43 -MARKET_SZ_CONVERT_5_CANCEL = 47 -SZ_MARKET = 1 -SH_MARKET = 0 - -CLIENT_CONFIG_MODULE_ENV = "BIGQMT_CLIENT_CONFIG_MODULE" -DEFAULT_CLIENT_CONFIG_MODULES = ( - "bigqmt_signal_trader_client_config", - "bigqmt_signal_trader_local_config", -) - -ORDER_UNREPORTED = 48 -ORDER_WAIT_REPORTING = 49 -ORDER_REPORTED = 50 -ORDER_REPORTED_CANCEL = 51 -ORDER_PARTSUCC_CANCEL = 52 -ORDER_PART_CANCEL = 53 -ORDER_CANCELED = 54 -ORDER_PART_SUCC = 55 -ORDER_SUCCEEDED = 56 -ORDER_JUNK = 57 -ORDER_UNKNOWN = 255 - -# --------------------------------------------------------------------------- -# xtconstant 枚举常量(对齐原生 MiniQMT xtquant/xtconstant.py,91 个全量) -# --------------------------------------------------------------------------- - -# 账号类型 -FUTURE_ACCOUNT = 1 # 期货 -SECURITY_ACCOUNT = 2 # 股票 -CREDIT_ACCOUNT = 3 # 信用 -FUTURE_OPTION_ACCOUNT = 5 # 期货期权 -STOCK_OPTION_ACCOUNT = 6 # 股票期权 -HUGANGTONG_ACCOUNT = 7 # 沪港通 -SHENGANGTONG_ACCOUNT = 11 # 深港通 - -# 委托类型 - 期货六键风格 -FUTURE_OPEN_LONG = 0 # 开多 -FUTURE_CLOSE_LONG_HISTORY = 1 # 平昨多 -FUTURE_CLOSE_LONG_TODAY = 2 # 平今多 -FUTURE_OPEN_SHORT = 3 # 开空 -FUTURE_CLOSE_SHORT_HISTORY = 4 # 平昨空 -FUTURE_CLOSE_SHORT_TODAY = 5 # 平今空 -# 委托类型 - 期货四键风格 -FUTURE_CLOSE_LONG_TODAY_FIRST = 6 # 平多,优先平今 -FUTURE_CLOSE_LONG_HISTORY_FIRST = 7 # 平多,优先平昨 -FUTURE_CLOSE_SHORT_TODAY_FIRST = 8 # 平空,优先平今 -FUTURE_CLOSE_SHORT_HISTORY_FIRST = 9 # 平空,优先平昨 -# 委托类型 - 期货两键风格 -FUTURE_CLOSE_LONG_TODAY_HISTORY_THEN_OPEN_SHORT = 10 # 卖出,优先平仓平今,余量开空 -FUTURE_CLOSE_LONG_HISTORY_TODAY_THEN_OPEN_SHORT = 11 # 卖出,优先平仓平昨,余量开空 -FUTURE_CLOSE_SHORT_TODAY_HISTORY_THEN_OPEN_LONG = 12 # 买入,优先平仓平今,余量开多 -FUTURE_CLOSE_SHORT_HISTORY_TODAY_THEN_OPEN_LONG = 13 # 买入,优先平仓平昨,余量开多 -FUTURE_OPEN = 14 # 买入,不优先平仓 -FUTURE_CLOSE = 15 # 卖出,不优先平仓 -# 委托类型 - 期货跨商品套利 -FUTURE_ARBITRAGE_OPEN = 16 # 开仓 -FUTURE_ARBITRAGE_CLOSE_HISTORY_FIRST = 17 # 平,优先平昨 -FUTURE_ARBITRAGE_CLOSE_TODAY_FIRST = 18 # 平,优先平今 -# 委托类型 - 期货展期 -FUTURE_RENEW_LONG_CLOSE_HISTORY_FIRST = 19 # 看多,优先平昨 -FUTURE_RENEW_LONG_CLOSE_TODAY_FIRST = 20 # 看多,优先平今 -FUTURE_RENEW_SHORT_CLOSE_HISTORY_FIRST = 21 # 看空,优先平昨 -FUTURE_RENEW_SHORT_CLOSE_TODAY_FIRST = 22 # 看空,优先平今 - -# 委托类型 - 股票 -STOCK_BUY = 23 -STOCK_SELL = 24 -# 委托类型 - 信用交易 -CREDIT_BUY = 23 # 担保品买入 -CREDIT_SELL = 24 # 担保品卖出 -CREDIT_FIN_BUY = 27 # 融资买入 -CREDIT_SLO_SELL = 28 # 融券卖出 -CREDIT_BUY_SECU_REPAY = 29 # 买券还券 -CREDIT_DIRECT_SECU_REPAY = 30 # 直接还券 -CREDIT_SELL_SECU_REPAY = 31 # 卖券还款 -CREDIT_DIRECT_CASH_REPAY = 32 # 直接还款 -CREDIT_FIN_BUY_SPECIAL = 40 # 专项融资买入 -CREDIT_SLO_SELL_SPECIAL = 41 # 专项融券卖出 -CREDIT_BUY_SECU_REPAY_SPECIAL = 42 # 专项买券还券 -CREDIT_DIRECT_SECU_REPAY_SPECIAL = 43 # 专项直接还券 -CREDIT_SELL_SECU_REPAY_SPECIAL = 44 # 专项卖券还款 -CREDIT_DIRECT_CASH_REPAY_SPECIAL = 45 # 专项直接还款 - -# 委托类型 - 股票期权 -STOCK_OPTION_BUY_OPEN = 48 # 买入开仓 -STOCK_OPTION_SELL_CLOSE = 49 # 卖出平仓 -STOCK_OPTION_SELL_OPEN = 50 # 卖出开仓 -STOCK_OPTION_BUY_CLOSE = 51 # 买入平仓 -STOCK_OPTION_COVERED_OPEN = 52 # 备兑开仓 -STOCK_OPTION_COVERED_CLOSE = 53 # 备兑平仓 -STOCK_OPTION_CALL_EXERCISE = 54 # 认购行权 -STOCK_OPTION_PUT_EXERCISE = 55 # 认沽行权 -STOCK_OPTION_SECU_LOCK = 56 # 证券锁定 -STOCK_OPTION_SECU_UNLOCK = 57 # 证券解锁 - -# 委托类型 - 期货期权 -OPTION_FUTURE_OPTION_EXERCISE = 100 # 期货期权行权 - -# 报价类型(市价) -LATEST_PRICE = 5 # 最新价 -FIX_PRICE = 11 # 指定价/限价 -MARKET_SH_CONVERT_5_CANCEL = 42 # 最优五档即时成交剩余撤销[上交所][股票] -MARKET_SH_CONVERT_5_LIMIT = 43 # 最优五档即时成交剩转限价[上交所][股票] -MARKET_PEER_PRICE_FIRST = 44 # 对手方最优价格委托 -MARKET_MINE_PRICE_FIRST = 45 # 本方最优价格委托 -MARKET_SZ_INSTBUSI_RESTCANCEL = 46 # 即时成交剩余撤销委托[深交所][股票][期权] -MARKET_SZ_CONVERT_5_CANCEL = 47 # 最优五档即时成交剩余撤销[深交所][股票][期权] -MARKET_SZ_FULL_OR_CANCEL = 48 # 全额成交或撤销委托[深交所][股票][期权] - -# 市场代码 -SH_MARKET = 0 -SZ_MARKET = 1 - -# 委托状态 -ORDER_UNREPORTED = 48 -ORDER_WAIT_REPORTING = 49 -ORDER_REPORTED = 50 -ORDER_REPORTED_CANCEL = 51 -ORDER_PARTSUCC_CANCEL = 52 -ORDER_PART_CANCEL = 53 -ORDER_CANCELED = 54 -ORDER_PART_SUCC = 55 -ORDER_SUCCEEDED = 56 -ORDER_JUNK = 57 -ORDER_UNKNOWN = 255 - -# 账号状态 -ACCOUNT_STATUS_INVALID = -1 # 无效 -ACCOUNT_STATUS_OK = 0 # 正常 -ACCOUNT_STATUS_WAITING_LOGIN = 1 # 连接中 -ACCOUNT_STATUSING = 2 # 登陆中 -ACCOUNT_STATUS_FAIL = 3 # 失败 -ACCOUNT_STATUS_INITING = 4 # 初始化中 -ACCOUNT_STATUS_CORRECTING = 5 # 数据刷新校正中 -ACCOUNT_STATUS_CLOSED = 6 # 收盘后 -ACCOUNT_STATUS_ASSIS_FAIL = 7 # 穿透副链接断开 -ACCOUNT_STATUS_DISABLEBYSYS = 8 # 系统停用 -ACCOUNT_STATUS_DISABLEBYUSER = 9 # 用户停用 - - -class CompatObject: - """Small attribute object matching xtquant's object-style returns.""" - - def __init__(self, **kwargs): - self.__dict__.update(kwargs) - - def __repr__(self): - items = ", ".join("%s=%r" % (key, value) for key, value in sorted(self.__dict__.items())) - return "%s(%s)" % (self.__class__.__name__, items) - - -class StockAccount: - def __init__(self, account_id, account_type="STOCK"): - self.account_id = str(account_id or "") - self.account_type = str(account_type or "STOCK") - - -class XtQuantTraderCallback: - def on_disconnected(self): - pass - - def on_stock_order(self, order): - pass - - def on_stock_trade(self, trade): - pass - - def on_order_error(self, order_error): - pass - - def on_cancel_error(self, cancel_error): - pass - - def on_order_stock_async_response(self, response): - pass - - def on_cancel_order_stock_async_response(self, response): - pass - - def on_account_status(self, status): - pass - - -def _env_int(name, default): - value = os.environ.get(name) - if value in (None, ""): - return default - return int(value) - - -def _env_float(name, default): - value = os.environ.get(name) - if value in (None, ""): - return default - return float(value) - - -def _env_bool(name, default=False): - value = os.environ.get(name) - if value in (None, ""): - return default - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -def _bool_value(value, default=False): - if value is None or value == "": - return default - if isinstance(value, bool): - return value - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -def _import_optional_module(module_name): - try: - return importlib.import_module(module_name) - except ModuleNotFoundError as exc: - if exc.name == module_name: - return None - raise - - -def _quote_client_id(): - """Process-stable client id for whole-quote subscriptions. Config or env wins; - otherwise read/create a persisted id so a restarted client is recognised as - the same subscriber by the server.""" - client_config = load_client_config() - configured = client_config.get("quote_client_id") or os.environ.get("BIGQMT_QUOTE_CLIENT_ID") - if configured: - return str(configured) - cache_path = os.path.join(os.path.expanduser("~"), ".cache", "bigqmt", "quote_client_id") - try: - with open(cache_path, "r") as handle: - existing = handle.read().strip() - if existing: - return existing - except OSError: - pass - new_id = uuid.uuid4().hex - try: - os.makedirs(os.path.dirname(cache_path), exist_ok=True) - with open(cache_path, "w") as handle: - handle.write(new_id) - except OSError: - pass - return new_id - - -def _quote_push_zmq_address(client): - """Derive the server whole-quote PUB address: same host as the RPC zmq - endpoint, RPC port + 1 (the PUB socket binds a distinct port).""" - from .transports.zmq_transport import DEFAULT_ZMQ_HOST, _default_zmq_port - - zmq_config = dict(getattr(client, "zmq_config", {}) or {}) - explicit = zmq_config.get("quote_push_connect_address") - if explicit: - return str(explicit) - host = zmq_config.get("host") or DEFAULT_ZMQ_HOST - port = zmq_config.get("port") - base_port = int(port) if port is not None else _default_zmq_port(client.account_id) - return "tcp://%s:%d" % (host, base_port + 1) - - -def load_client_config(module_name=None): - """Load local private client config without requiring environment variables.""" - candidates = [] - selected = module_name or os.environ.get(CLIENT_CONFIG_MODULE_ENV) - if selected: - candidates.append(str(selected)) - candidates.extend(name for name in DEFAULT_CLIENT_CONFIG_MODULES if name not in candidates) - - for candidate in candidates: - module = _import_optional_module(candidate) - if module is None: - continue - redis_config = dict(getattr(module, "BIGQMT_REDIS_CONFIG", {}) or {}) - account_id = getattr(module, "BIGQMT_ACCOUNT_ID", None) or redis_config.get("account_id") - timeout_seconds = getattr(module, "BIGQMT_RPC_TIMEOUT_SECONDS", None) - if timeout_seconds is None: - timeout_seconds = redis_config.get("rpc_timeout_seconds") - download_wait_seconds = getattr(module, "BIGQMT_DOWNLOAD_WAIT_SECONDS", None) - if download_wait_seconds is None: - download_wait_seconds = redis_config.get("download_wait_seconds") - download_poll_interval_seconds = getattr(module, "BIGQMT_DOWNLOAD_POLL_INTERVAL_SECONDS", None) - if download_poll_interval_seconds is None: - download_poll_interval_seconds = redis_config.get("download_poll_interval_seconds") - full_tick_cache_config = dict(getattr(module, "BIGQMT_FULL_TICK_CACHE_CONFIG", {}) or {}) - for key in ( - "full_tick_cache_enabled", - "full_tick_demand_ttl_seconds", - "full_tick_cache_ttl_seconds", - "full_tick_wait_seconds", - "full_tick_poll_interval_seconds", - ): - if key in redis_config: - full_tick_cache_config[key] = redis_config[key] - local_cache_config = dict(getattr(module, "BIGQMT_LOCAL_CACHE_CONFIG", {}) or {}) - for key in ("local_cache_enabled", "local_cache_dir", "local_cache_fallback_rpc", "local_cache_format"): - if key in redis_config: - local_cache_config[key.replace("local_cache_", "")] = redis_config[key] - formula_server_config = dict(getattr(module, "BIGQMT_FORMULA_SERVER_CONFIG", {}) or {}) - formula_server_config.update(dict(redis_config.get("formula_server") or {})) - return { - "module": candidate, - "account_id": account_id, - "redis_config": redis_config, - "timeout_seconds": timeout_seconds, - "download_wait_seconds": download_wait_seconds, - "download_poll_interval_seconds": download_poll_interval_seconds, - "full_tick_cache_config": full_tick_cache_config, - "local_cache_config": local_cache_config, - "formula_server_config": formula_server_config, - "quote_client_id": getattr(module, "BIGQMT_QUOTE_CLIENT_ID", None), - } - return {} - - -def _account_id(account, fallback=""): - if account is None: - return str(fallback or "") - if isinstance(account, str): - return account - for name in ("account_id", "m_strAccountID", "id"): - value = getattr(account, name, None) - if value: - return str(value) - if isinstance(account, dict): - return str(account.get("account_id") or account.get("id") or fallback or "") - return str(fallback or "") - - -def _action_to_order_type(action): - text = str(action or "").upper() - if text in ("BUY", str(STOCK_BUY)): - return STOCK_BUY - if text in ("SELL", str(STOCK_SELL)): - return STOCK_SELL - return 0 - - -def _safe_int(value, default=0): - try: - return int(value) - except (TypeError, ValueError): - return default - - -def _safe_float(value, default=0.0): - try: - return float(value) - except (TypeError, ValueError): - return default - - -def _as_list(value): - if value is None: - return [] - if isinstance(value, dict): - return list(value.values()) - if isinstance(value, list): - return value - return [value] - - -def _restore_jsonable(value): - if isinstance(value, dict): - marker = value.get("__bigqmt_type__") - if marker == "DataFrame": - try: - import pandas as pd - - return pd.DataFrame(value.get("records") or [], columns=value.get("columns") or None) - except Exception: - return value.get("records") or [] - if marker == "Series": - try: - import pandas as pd - - return pd.Series(value.get("data") or {}) - except Exception: - return value.get("data") or {} - return {key: _restore_jsonable(item) for key, item in value.items()} - if isinstance(value, list): - return [_restore_jsonable(item) for item in value] - return value - - -def _digits_only(value): - return "".join(ch for ch in str(value or "") if ch.isdigit()) - - -def _parse_qmt_stime(value): - digits = _digits_only(value) - if len(digits) >= 14: - try: - return _dt.datetime.strptime(digits[:14], "%Y%m%d%H%M%S") - except ValueError: - return None - if len(digits) >= 8: - try: - return _dt.datetime.strptime(digits[:8], "%Y%m%d") - except ValueError: - return None - return None - - -def _qmt_stime_index(value): - digits = _digits_only(value) - if len(digits) >= 14: - return digits[:14] - if len(digits) >= 8: - return digits[:8] - return str(value or "") - - -def _qmt_datetime_to_epoch_ms(dt_value): - # QMT bar labels are China local time; MiniQMT's time column is epoch ms. - china_tz = _dt.timezone(_dt.timedelta(hours=8)) - return int(dt_value.replace(tzinfo=china_tz).timestamp() * 1000) - - -def _normalize_market_data_frame(df, field_list=None): - try: - columns = list(df.columns) - except Exception: - return df - if "stime" not in columns: - return df - - requested = [str(field) for field in (field_list or [])] - try: - out = df.copy() - stimes = list(out["stime"]) - out.index = [_qmt_stime_index(value) for value in stimes] - if "time" in out.columns or "time" in requested: - out["time"] = [ - _qmt_datetime_to_epoch_ms(parsed) if parsed is not None else None - for parsed in (_parse_qmt_stime(value) for value in stimes) - ] - if requested: - keep = [field for field in requested if field in out.columns] - if keep: - return out[keep] - if "stime" in out.columns: - return out.drop(columns=["stime"]) - return out - except Exception: - return df - - -def _normalize_market_data_result(data, field_list=None): - if not isinstance(data, dict): - return data - return { - code: _normalize_market_data_frame(frame, field_list=field_list) - for code, frame in data.items() - } - - -def _normalize_code_for_filter(code): - text = str(code or "").strip().upper() - if "." not in text: - return text - return text.split(".", 1)[0] - - -def _is_hs_a_share(code): - text = str(code or "").strip().upper() - pure = _normalize_code_for_filter(text) - if not (len(pure) == 6 and pure.isdigit()): - return False - if text.endswith(".SH"): - return pure.startswith(("600", "601", "603", "605", "688", "689")) - if text.endswith(".SZ"): - return pure.startswith(("000", "001", "002", "003", "300", "301")) - return pure.startswith( - ("000", "001", "002", "003", "300", "301", "600", "601", "603", "605", "688", "689") - ) - - -class BigQmtRpcClient: - def __init__( - self, - account_id=None, - redis_client=None, - redis_config=None, - timeout_seconds=None, - transport=None, - ): - client_config = load_client_config() - config_redis = dict(client_config.get("redis_config") or {}) - redis_config = dict(redis_config or {}) - merged_redis_config = dict(config_redis) - merged_redis_config.update(redis_config) - self.account_id = str( - account_id - or merged_redis_config.get("account_id") - or client_config.get("account_id") - or os.environ.get("BIGQMT_ACCOUNT_ID") - or "" - ) - self.redis_client = redis_client - self.redis_config = { - "host": merged_redis_config.get("host") or os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1"), - "port": int(merged_redis_config.get("port") or _env_int("BIGQMT_REDIS_PORT", 6379)), - "db": int(merged_redis_config.get("db") or _env_int("BIGQMT_REDIS_DB", 5)), - "username": merged_redis_config.get("username", os.environ.get("BIGQMT_REDIS_USERNAME") or ""), - "password": merged_redis_config.get("password", os.environ.get("BIGQMT_REDIS_PASSWORD") or ""), - } - config_timeout = client_config.get("timeout_seconds") - self.timeout_seconds = float( - timeout_seconds - if timeout_seconds is not None - else config_timeout - if config_timeout is not None - else _env_float("BIGQMT_RPC_TIMEOUT_SECONDS", 6.0) - ) - config_download_wait = client_config.get("download_wait_seconds") - self.download_wait_seconds = float( - config_download_wait - if config_download_wait is not None - else _env_float("BIGQMT_DOWNLOAD_WAIT_SECONDS", 1800.0) - ) - config_download_poll = client_config.get("download_poll_interval_seconds") - self.download_poll_interval_seconds = float( - config_download_poll - if config_download_poll is not None - else _env_float("BIGQMT_DOWNLOAD_POLL_INTERVAL_SECONDS", 0.5) - ) - full_tick_cache_config = dict(client_config.get("full_tick_cache_config") or {}) - self.full_tick_cache_config = { - "enabled": _bool_value( - full_tick_cache_config.get("enabled", full_tick_cache_config.get("full_tick_cache_enabled")), - _env_bool("BIGQMT_FULL_TICK_CACHE_ENABLED", False), - ), - "demand_ttl_seconds": float( - full_tick_cache_config.get("demand_ttl_seconds") - or full_tick_cache_config.get("full_tick_demand_ttl_seconds") - or _env_float("BIGQMT_FULL_TICK_DEMAND_TTL_SECONDS", 10.0) - ), - "cache_ttl_seconds": float( - full_tick_cache_config.get("cache_ttl_seconds") - or full_tick_cache_config.get("full_tick_cache_ttl_seconds") - or _env_float("BIGQMT_FULL_TICK_CACHE_TTL_SECONDS", 10.0) - ), - "wait_seconds": float( - full_tick_cache_config.get("wait_seconds") - or full_tick_cache_config.get("full_tick_wait_seconds") - or _env_float("BIGQMT_FULL_TICK_WAIT_SECONDS", 3.5) - ), - "poll_interval_seconds": float( - full_tick_cache_config.get("poll_interval_seconds") - or full_tick_cache_config.get("full_tick_poll_interval_seconds") - or _env_float("BIGQMT_FULL_TICK_POLL_INTERVAL_SECONDS", 0.2) - ), - } - # Client-side local market-data cache. get_market_data_ex is cache-through; - # fallback_rpc=True lets get_local_data fetch+cache a cache miss. - local_cache_config = dict(client_config.get("local_cache_config") or {}) - self.local_cache_config = { - "enabled": _bool_value( - local_cache_config.get("enabled", merged_redis_config.get("local_cache_enabled")), - _env_bool("BIGQMT_LOCAL_CACHE_ENABLED", True), - ), - "dir": ( - local_cache_config.get("dir") - or merged_redis_config.get("local_cache_dir") - or os.environ.get("BIGQMT_LOCAL_CACHE_DIR") - or None - ), - "fallback_rpc": _bool_value( - local_cache_config.get("fallback_rpc", merged_redis_config.get("local_cache_fallback_rpc")), - _env_bool("BIGQMT_LOCAL_CACHE_FALLBACK_RPC", False), - ), - "format": str( - local_cache_config.get("format") - or merged_redis_config.get("local_cache_format") - or os.environ.get("BIGQMT_LOCAL_CACHE_FORMAT") - or "auto" # parquet if pyarrow is available, else pickle - ), - } - # Transport selection. Default "redis" keeps the legacy call_redis_rpc - # path (so existing client configs are unchanged). Setting transport to - # "zmq"/"mysql"/"shm" (via config or constructor) routes calls through - # the swappable transport layer instead. - self.transport_name = str( - transport - or merged_redis_config.get("transport") - or os.environ.get("BIGQMT_RPC_TRANSPORT") - or "redis" - ).lower() - self.zmq_config = dict(merged_redis_config.get("zmq") or {}) - self.mysql_config = dict(merged_redis_config.get("mysql") or {}) - self._transport_instance = None # lazily built by _transport() - # FormulaServer read fast-path. QMT's C++ quote service (port 58600) - # answers reference/history reads in ~0.07ms without touching the QMT - # python thread. Enabled by default; every miss falls back to RPC, so a - # client that cannot reach it just runs as before. - formula_config = dict( - client_config.get("formula_server_config") - or merged_redis_config.get("formula_server") - or {} - ) - if "enabled" not in formula_config: - formula_config["enabled"] = _env_bool("BIGQMT_FORMULA_ENABLED", True) - self.formula_server_config = formula_config - self._formula_router_instance = None # lazily built by _formula_router() - - def _redis(self): - if self.redis_client is None: - import redis - - cfg = dict(self.redis_config) - if not cfg.get("username"): - cfg.pop("username", None) - if not cfg.get("password"): - cfg.pop("password", None) - self.redis_client = redis.Redis(**cfg) - return self.redis_client - - def _transport(self): - if self._transport_instance is None: - if self.transport_name in ("redis", "", "default"): - # Legacy path: call_redis_rpc builds its own request envelope. - return None - from .transports.factory import build_transport - - client_config = load_client_config() - config_redis = dict(client_config.get("redis_config") or {}) - zmq_config = dict(config_redis.get("zmq") or {}) - zmq_config.update(self.zmq_config) - # ZMQ must work without Redis. Discovery is opt-in and unnecessary - # when connect_address is explicitly configured. - if ( - not zmq_config.get("connect_address") - and bool(zmq_config.get("redis_discovery_enabled", False)) - ): - zmq_config.setdefault("discovery_redis_client", self._redis()) - factory_config = { - "zmq": zmq_config, - "mysql": dict(config_redis.get("mysql") or {}, **self.mysql_config), - } - self._transport_instance = build_transport( - self.transport_name, - factory_config, - account_id=self.account_id, - print_prefix="[bigqmt_client]", - ) - return self._transport_instance - - def _formula_router(self): - """Lazily build the FormulaServer router. Never raises — a router that - cannot be built simply means every read goes over RPC.""" - if self._formula_router_instance is None: - try: - from .formula_server import build_router - - self._formula_router_instance = build_router( - self.formula_server_config, print_prefix="[bigqmt_formula]" - ) - except Exception as exc: - print("[bigqmt_formula] disabled (%s: %s)" % (exc.__class__.__name__, exc)) - - class _Disabled(object): - def supports(self, method): - return False - - self._formula_router_instance = _Disabled() - return self._formula_router_instance - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - target_account = str(account_id or self.account_id or "") - if not target_account: - raise ValueError("Big QMT account_id is required") - wait_seconds = self.timeout_seconds if timeout_seconds is None else timeout_seconds - # Fast path: reference/history reads answered straight by QMT's - # FormulaServer, bypassing the strategy process and its GIL. Anything it - # declines (unmapped method, untranslatable params, server down) raises - # Unroutable and drops through to the RPC bridge below. - router = self._formula_router() - if router.supports(method): - from .formula_server import Unroutable - - try: - return _restore_jsonable(router.call(method, params or {})) - except Unroutable: - pass - transport = self._transport() - if transport is not None: - # Swappable transport path (zmq/mysql/...). Build the request - # envelope the same way call_redis_rpc does. - request = { - "schema_version": 1, - "request_id": uuid.uuid4().hex, - "account_id": target_account, - "method": method, - "params": params or {}, - "ttl_seconds": 60, - } - response = transport.send_request(request, wait_seconds) - else: - response = call_redis_rpc( - self._redis(), - account_id=target_account, - method=method, - params=params or {}, - timeout_seconds=wait_seconds, - ) - if not response.get("ok"): - raise RuntimeError(response.get("error") or "Big QMT RPC failed: %s" % method) - # server_error 携带 QMT 端诊断(如 passorder 提交但委托没进系统)。 - # 只在交易类方法上设置(读取类恒为空),转成异常让调用方看到真实原因, - # 而不是把「无委托号」误判为 -1 失败(issue #38)。 - server_error = str(response.get("server_error") or "") - if server_error: - raise RuntimeError("Big QMT %s server_error: %s" % (method, server_error)) - return _restore_jsonable(response.get("data")) - - def publish_event(self, event_type, payload, stream_template="bigqmt:quote_events:{account_id}"): - account_id = str(self.account_id or "") - event = { - "event_type": str(event_type), - "account_id": account_id, - "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), - "payload": payload or {}, - } - raw = json.dumps(event, ensure_ascii=False, default=str) - stream_key = stream_template.format(account_id=account_id) - redis_client = self._redis() - try: - redis_client.xadd(stream_key, {"payload": raw}, maxlen=1000, approximate=True) - except Exception: - pass - try: - redis_client.publish(stream_key, raw) - except Exception: - pass - return event - - def save_quote_subscription(self, seq, payload, active=True): - account_id = str(self.account_id or "") - key = "bigqmt:quote_subscriptions:%s" % account_id - redis_client = self._redis() - if active: - value = json.dumps(payload or {}, ensure_ascii=False, default=str) - try: - redis_client.hset(key, str(seq), value) - except Exception: - pass - else: - try: - redis_client.hdel(key, str(seq)) - except Exception: - pass - - -class BigQmtXtData: - def __init__(self, client): - self.client = client - self._subscribe_seq = int(time.time() * 1000) - self._cache_obj = None - self._quote_session = None # lazily built WholeQuoteClientSession - self._quote_session_factory = None # test hook: returns a session-like object - - def _next_seq(self): - self._subscribe_seq += 1 - return self._subscribe_seq - - def _local_cache(self): - cfg = dict(getattr(self.client, "local_cache_config", {}) or {}) - if not _bool_value(cfg.get("enabled"), True): - return None - if self._cache_obj is None: - self._cache_obj = LocalMarketCache(cache_dir=cfg.get("dir"), fmt=cfg.get("format", "auto")) - return self._cache_obj - - def _call(self, method, **params): - return self.client.call(method, params) - - def get_full_tick(self, code_list): - codes = list(code_list or []) - if not codes: - return {} - cache_config = dict(getattr(self.client, "full_tick_cache_config", {}) or {}) - if _bool_value(cache_config.get("enabled"), False): - redis_client = self.client._redis() - request_full_tick_cache( - redis_client, - self.client.account_id, - codes, - demand_ttl_seconds=cache_config.get("demand_ttl_seconds", 10), - cache_ttl_seconds=cache_config.get("cache_ttl_seconds", 10), - ) - data = wait_full_tick_cache( - redis_client, - self.client.account_id, - codes, - max_age_seconds=cache_config.get("cache_ttl_seconds", 10), - wait_seconds=cache_config.get("wait_seconds", 3.5), - poll_interval_seconds=cache_config.get("poll_interval_seconds", 0.2), - ) - if data is not None: - return data - upper_codes = {str(code).strip().upper() for code in codes} - if upper_codes & {"SH", "SZ", "BJ", "HK"}: - # Whole-market snapshots must stay on the demand cache. A live RPC - # here would ship ~50k rows on every miss, so surface the timeout. - raise TimeoutError("full tick redis cache timeout: %s" % ",".join(str(code) for code in codes)) - # Symbol-list miss (cold start / expired snapshot): fall back to a live - # RPC so the first call is ~ms instead of a hard wait_seconds stall. - return self.client.call("get_full_tick", {"codes": codes}) or {} - upper_codes = {str(code).strip().upper() for code in codes} - timeout_seconds = 30 if upper_codes & {"SH", "SZ", "BJ", "HK"} else None - return self.client.call("get_full_tick", {"codes": codes}, timeout_seconds=timeout_seconds) or {} - - def get_instrument_detail(self, stock_code): - return self.client.call("get_instrument_detail", {"code": stock_code}) or {} - - def get_instrumentdetail(self, stock_code): - return self.get_instrument_detail(stock_code) - - def get_instrument_type(self, stock_code, variety_list=None): - return self._call("get_instrument_type", code=stock_code, variety_list=variety_list) - - def get_stock_list_in_sector(self, sector_name, real_timetag=-1): - name = str(sector_name or "") - try: - return self._call("get_stock_list_in_sector", sector_name=sector_name, real_timetag=real_timetag) or [] - except Exception: - pass - if name in ("沪深A股", "沪深A股".encode("utf-8", errors="ignore").decode("utf-8", errors="ignore")): - ticks = self.get_full_tick(["SH", "SZ"]) - return sorted(code for code in ticks.keys() if _is_hs_a_share(code)) - raise NotImplementedError("sector is not supported by BigQMT compat: %s" % sector_name) - - def get_market_data( - self, - field_list=None, - stock_list=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - fill_data=True, - ): - params = dict( - field_list=list(field_list or []), - stock_list=list(stock_list or []), - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - fill_data=fill_data, - ) - data = self._call("get_market_data", **params) - # Self-heal adjusted reads (all-zero bars -> server raw download + retry). - return self._heal_adjusted("get_market_data", params, data) - - def _get_market_data_ex_batch(self, params, timeout_seconds=None): - """One RPC's worth of bars, healed and normalized. No caching.""" - data = self._call("get_market_data_ex", timeout_seconds=timeout_seconds, **params) - # Self-heal adjusted reads (all-zero bars -> server raw download + retry). - data = self._heal_adjusted("get_market_data_ex", params, data) - # Normalize Big QMT's stime-indexed frame to MiniQMT shape (time-indexed). - if isinstance(data, dict): - data = _normalize_market_data_result(data, field_list=params.get("field_list")) - return data - - def get_market_data_ex( - self, - field_list=None, - stock_list=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - fill_data=True, - chunk_size=None, - timeout_seconds=None, - ): - """Pull bars over RPC, in batches of ``chunk_size`` codes. - - Cache-through: whatever is fetched is written to the local cache (keyed - by dividend_type), so it stays the latest -- important for 前复权 data, - whose history re-scales on each dividend. - - Batching exists because one request carrying every code shares a single - RPC timeout (6s by default), so a wide stock_list times out and loses - the whole pull rather than degrading (issue #47). Splitting keeps each - request small enough to answer, and a batch that still fails only costs - its own codes -- the rest are returned. - - ``chunk_size=0`` restores the old single-request behaviour. - """ - codes = list(stock_list or []) - base = dict( - field_list=list(field_list or []), - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - fill_data=fill_data, - ) - step = DEFAULT_MARKET_DATA_CHUNK if chunk_size is None else int(chunk_size) - - if step <= 0 or len(codes) <= step: - data = self._get_market_data_ex_batch( - dict(base, stock_list=codes), timeout_seconds=timeout_seconds - ) - else: - data = {} - failures = [] - for index in range(0, len(codes), step): - batch = codes[index:index + step] - try: - part = self._get_market_data_ex_batch( - dict(base, stock_list=batch), timeout_seconds=timeout_seconds - ) - except Exception as exc: - # Losing one batch must not lose the others: a partial - # result beats an exception when 500 codes were asked for. - failures.append((batch, exc)) - continue - if isinstance(part, dict): - data.update(part) - if failures and not data: - # Nothing came back at all -- surface the first cause rather - # than returning a silent empty dict. - raise failures[0][1] - for batch, exc in failures: - print("[bigqmt_client] get_market_data_ex batch failed (%d codes, first=%s): %s" - % (len(batch), batch[0] if batch else "", exc)) - - cache = self._local_cache() - if cache is not None and isinstance(data, dict): - for code, df in data.items(): - try: - cache.write(code, period, df, dividend_type=dividend_type) - except Exception: - pass - return data - - def get_local_data( - self, - field_list=None, - stock_list=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - fill_data=True, - data_dir=None, - ): - """Read bars from the CLIENT-side local cache — no RPC to Big QMT. - - Returns a dict {code: DataFrame}. A cache-missed code is omitted, unless - local_cache_fallback_rpc is enabled (then it is fetched + cached over RPC). - """ - codes = [str(c) for c in (stock_list or []) if str(c or "").strip()] - cache = self._local_cache() - if cache is None: - # Cache disabled -> behave like a plain RPC local-data read. - return self._call( - "get_local_data", - field_list=_as_list(field_list), - stock_list=codes, - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - fill_data=fill_data, - data_dir=data_dir, - ) - fields = list(field_list or []) - result = {} - missing = [] - for code in codes: - df = cache.read(code, period, start_time, end_time, count, dividend_type=dividend_type) - if df is not None and getattr(df, "shape", (0,))[0] > 0: - result[code] = self._select_fields( - _normalize_market_data_frame(df, field_list=fields), - fields, - ) - else: - missing.append(code) - if missing and _bool_value(self.client.local_cache_config.get("fallback_rpc"), False): - fetched = self._pull_and_cache(missing, period, start_time, end_time, count, dividend_type) - for code in missing: - df = fetched.get(code) - if df is not None and getattr(df, "shape", (0,))[0] > 0: - result[code] = self._select_fields( - _normalize_market_data_frame(df, field_list=fields), - fields, - ) - return result - - @staticmethod - def _select_fields(df, fields): - if not fields: - return df - try: - keep = [c for c in df.columns if c in fields or (c in _TIME_COL_NAMES and c != "stime")] - return df[keep] if keep else df - except Exception: - return df - - @staticmethod - def _is_all_zero_any(data): - """Detect the all-zero adjusted-bars symptom (server lacks raw data). - - Big QMT computes front/back-adjusted bars from raw bars + dividend - factors; when those are missing server-side the price columns come - back all 0.0 (only the last bar may hold the live price). Recursively - handles DataFrame, {code: DataFrame} and {field: {code: [..]}} shapes. - """ - try: - if data is None: - return False - cols = getattr(data, "columns", None) - if cols is not None: # pandas DataFrame - if "close" not in list(cols): - return False - closes = data["close"] - if len(closes) == 0: - return False - head = closes.iloc[:-1] if len(closes) > 1 else closes - return bool((head == 0).all()) - if isinstance(data, dict): - return any(BigQmtXtData._is_all_zero_any(v) for v in data.values()) - if isinstance(data, (list, tuple)) and data and all( - isinstance(x, (int, float)) for x in data - ): - head = data[:-1] if len(data) > 1 else data - return bool(head) and all(x == 0 for x in head) - return False - except Exception: - return False - - def _ensure_server_raw(self, codes, period, start_time, end_time): - """Trigger a server-side raw download so adjusted bars can be computed.""" - try: - self.client.call( - "download_history_data2", - { - "stock_list": list(codes), - "period": period, - "start_time": start_time, - "end_time": end_time, - }, - timeout_seconds=60.0, - ) - except Exception: - pass - - def _heal_adjusted(self, method, params, data, wait_seconds=2.0): - """Self-heal adjusted reads: if the adjusted pull came back all-zero, - trigger a server-side raw download, wait for async landing, retry once.""" - dividend_type = str(params.get("dividend_type") or "none").lower() - if dividend_type in ("", "none"): - return data - if not self._is_all_zero_any(data): - return data - codes = list(params.get("stock_list") or params.get("stock_code") or []) - if not codes: - return data - self._ensure_server_raw( - codes, - params.get("period", "1d"), - params.get("start_time", ""), - params.get("end_time", ""), - ) - time.sleep(wait_seconds) - return self._call(method, **params) - - def _pull_and_cache(self, codes, period, start_time, end_time, count, dividend_type="none"): - """Fetch codes over RPC (get_market_data_ex already caches them).""" - data = self.get_market_data_ex( - field_list=DEFAULT_DOWNLOAD_FIELDS, - stock_list=list(codes), - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - ) - out = {} - for code in codes: - df = data.get(code) if isinstance(data, dict) else None - if df is not None and getattr(df, "shape", (0,))[0] > 0: - out[code] = df - return out - - def subscribe_quote(self, stock_code, period="1d", start_time="", end_time="", count=0, callback=None): - seq = self._next_seq() - payload = { - "seq": seq, - "stock_code": stock_code, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - } - self.client.save_quote_subscription(seq, payload, active=True) - self.client.publish_event("subscribe_quote", payload) - if callback is not None: - try: - if str(period).lower() in ("tick", "full_tick"): - callback(self.get_full_tick([stock_code])) - else: - callback( - self.get_market_data_ex( - stock_list=[stock_code], - period=period, - start_time=start_time, - end_time=end_time, - count=count, - ) - ) - except Exception: - pass - return seq - - def subscribe_quote2(self, stock_code, period="1d", start_time="", end_time="", count=0, dividend_type=None, callback=None): - return self.subscribe_quote( - stock_code=stock_code, - period=period, - start_time=start_time, - end_time=end_time, - count=count, - callback=callback, - ) - - def _whole_quote_session(self): - if self._quote_session is None: - if self._quote_session_factory is not None: - self._quote_session = self._quote_session_factory() - else: - self._quote_session = self._build_quote_session() - return self._quote_session - - def _build_quote_session(self): - from .whole_quote_session import WholeQuoteClientSession - - client = self.client - - def rpc_call(method, params): - return client.call(method, params) - - return WholeQuoteClientSession( - rpc_call=rpc_call, - push_channel=self._build_quote_push_channel(), - client_id=_quote_client_id(), - heartbeat_interval_seconds=_env_float("BIGQMT_QUOTE_HEARTBEAT_SECONDS", 3.0), - sub_id_func=self._next_seq, - ) - - def _build_quote_push_channel(self): - """Build the push-channel subscriber matching the RPC transport: redis - deployments derive the channel locally; zmq deployments connect to the - server PUB socket (host from zmq config, RPC port + 1).""" - client = self.client - from .quote_push_channel import RedisQuotePushChannel, ZmqQuotePushChannel - - transport_name = str(getattr(client, "transport_name", "redis") or "redis").lower() - if transport_name in ("zmq",): - address = _quote_push_zmq_address(client) - return ZmqQuotePushChannel(connect_address=address) - return RedisQuotePushChannel(client._redis(), account_id=client.account_id) - - def subscribe_whole_quote(self, code_list, callback=None): - session = self._whole_quote_session() - session.start() - sub_id = session.subscribe_whole_quote(code_list, callback=callback) - # The big-QMT whole-quote callback is incremental (changed symbols only), - # so prime the callback once with a full get_full_tick snapshot. - if callback is not None: - try: - callback(self.get_full_tick(code_list)) - except Exception: - pass - return sub_id - - def unsubscribe_quote(self, seq): - # subscribe_whole_quote handles are owned by the push session; single-stock - # subscribe_quote seqs still retire through the legacy redis-event path. - session = self._quote_session - if session is not None and session.has_subscription(seq): - session.unsubscribe_quote(seq) - else: - payload = {"seq": seq} - self.client.save_quote_subscription(seq, payload, active=False) - self.client.publish_event("unsubscribe_quote", payload) - return 0 - - def run(self): - while True: - time.sleep(3600) - - def get_divid_factors(self, stock_code, start_time="", end_time=""): - return self._call("get_divid_factors", stock_code=stock_code, start_time=start_time, end_time=end_time) - - def download_history_data2(self, stock_list, period, start_time="", end_time="", callback=None, incrementally=None, dividend_type="none", chunk_size=None, download_timeout_seconds=180.0): - """Pull bars from Big QMT over RPC and cache them locally, in batches. - - Mirrors xtdata.download_history_data2: after this, get_local_data(..., the - same dividend_type) reads the data locally with no further RPC. Each batch - re-pulls live, so re-running keeps the cache latest — needed for 前复权 - (front-adjusted) data. ``callback`` (optional) is invoked once per stock with - {finished, total, stockcode} — xtdata-style. Returns {finished, total}. - - The server-side download runs for EVERY dividend_type, matching - xtdata semantics ("populate the local QMT store"). It used to be skipped - for unadjusted pulls, which made an unadjusted download a no-op that - still reported progress (issue #47). - - Adjusted data (dividend_type != none) additionally depends on it: Big QMT - computes adjusted bars from the RAW history + dividend factors, and - without both, get_market_data_ex(dividend_type='front') returns all-zero - closes (verified live). - - ``download_timeout_seconds`` covers the server-side download only; it is - generous because a cold code with a wide window can take minutes. - """ - codes = [str(c) for c in (stock_list or []) if str(c or "").strip()] - if not codes: - return {"finished": 0, "total": 0} - if self._local_cache() is None: - raise RuntimeError("local cache is disabled (set local_cache_enabled=True to download)") - - # Server-side download first, for EVERY dividend_type. - # - # This used to run only when adjustment was requested, on the reasoning - # that an unadjusted pull can be served straight from get_market_data_ex. - # That reads whatever Big QMT already has -- it does not fetch anything. - # So an unadjusted "download" left the QMT-side store untouched while - # still reporting {finished: N} through the callback: a progress bar for - # work that never happened (issue #47, and the real cause behind #39, - # which was closed on an incomplete reading of this function). - # - # xtdata.download_history_data means "populate the local QMT store", and - # callers depend on that: FormulaServer and get_local_data both read it, - # and codes "downloaded" this way had zero bars there. - # - # Adjusted data additionally NEEDS this: QMT computes front/back-adjusted - # bars from raw bars + dividend factors, and both must exist server-side - # or the result is all zeros. - try: - self.client.call( - "download_history_data2", - { - "stock_list": codes, - "period": period, - "start_time": start_time, - "end_time": end_time, - }, - timeout_seconds=float(download_timeout_seconds), - ) - except Exception: - # Best-effort: some deployments lack the QMT global; the pull below - # may still work if the data already exists server-side. - pass - - total = len(codes) - step = int(chunk_size or 300) - if step <= 0: - step = 300 - finished = 0 - for i in range(0, total, step): - batch = codes[i:i + step] - # get_market_data_ex is cache-through: it writes each code to the cache. - self.get_market_data_ex( - field_list=DEFAULT_DOWNLOAD_FIELDS, - stock_list=batch, - period=period, - start_time=start_time, - end_time=end_time, - count=-1, - dividend_type=dividend_type, - ) - for code in batch: - finished += 1 - if callback is not None: - try: - callback({"finished": finished, "total": total, "stockcode": code}) - except Exception: - pass - return {"finished": finished, "total": total} - - def download_history_data(self, stock_code, period, start_time="", end_time="", incrementally=None, dividend_type="none"): - return self.download_history_data2([stock_code], period, start_time, end_time, dividend_type=dividend_type) - - def local_cache_stats(self): - """Return (cached files, periods) for the client-side local cache.""" - cache = self._local_cache() - return cache.stats() if cache is not None else (0, []) - - def get_trading_dates(self, market, start_time="", end_time="", count=-1): - return self._call("get_trading_dates", market=market, start_time=start_time, end_time=end_time, count=count) - - def get_holidays(self): - return self._call("get_holidays") - - def download_holiday_data(self, incrementally=True): - return self._call("download_holiday_data", incrementally=incrementally) - - def get_ipo_info(self, start_time="", end_time=""): - return self._call("get_ipo_info", start_time=start_time, end_time=end_time) - - def get_etf_info(self): - return self._call("get_etf_info") - - def download_etf_info(self): - return self._call("download_etf_info") - - def get_option_list(self, undl_code, dedate, opttype="", isavailavle=False): - return self._call("get_option_list", undl_code=undl_code, dedate=dedate, opttype=opttype, isavailavle=isavailavle) - - def get_his_option_list(self, undl_code, dedate): - return self._call("get_his_option_list", undl_code=undl_code, dedate=dedate) - - def get_his_option_list_batch(self, undl_code, start_time="", end_time=""): - return self._call("get_his_option_list_batch", undl_code=undl_code, start_time=start_time, end_time=end_time) - - def get_financial_data(self, stock_list, table_list=None, start_time="", end_time="", report_type="report_time"): - return self._call( - "get_financial_data", - stock_list=list(stock_list or []), - table_list=list(table_list or []), - start_time=start_time, - end_time=end_time, - report_type=report_type, - ) - - def download_financial_data(self, stock_list, table_list=None, start_time="", end_time="", incrementally=None): - return self._call( - "download_financial_data", - stock_list=list(stock_list or []), - table_list=list(table_list or []), - start_time=start_time, - end_time=end_time, - incrementally=incrementally, - ) - - def download_financial_data2(self, stock_list, table_list=None, start_time="", end_time="", callback=None): - result = self._call( - "download_financial_data2", - stock_list=list(stock_list or []), - table_list=list(table_list or []), - start_time=start_time, - end_time=end_time, - ) - if callback is not None: - callback(result) - return result - - def get_sector_list(self): - return self._call("get_sector_list") - - def get_sector_info(self, sector_name=""): - return self._call("get_sector_info", sector_name=sector_name) - - def get_markets(self): - return self._call("get_markets") - - def get_market_last_trade_date(self, market): - return self._call("get_market_last_trade_date", market=market) - - def call_formula(self, formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param=None): - return self._call( - "call_formula", - formula_name=formula_name, - stock_code=stock_code, - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - extend_param=extend_param or {}, - ) - - def subscribe_formula(self, formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param=None, callback=None): - result = self._call( - "subscribe_formula", - formula_name=formula_name, - stock_code=stock_code, - period=period, - start_time=start_time, - end_time=end_time, - count=count, - dividend_type=dividend_type, - extend_param=extend_param or {}, - ) - if callback is not None: - callback(result) - return result - - def unsubscribe_formula(self, request_id): - return self._call("unsubscribe_formula", request_id=request_id) - - def get_formula_result(self, request_id, start_time="", end_time="", count=-1, timeout_second=-1): - return self._call( - "get_formula_result", - request_id=request_id, - start_time=start_time, - end_time=end_time, - count=count, - timeout_second=timeout_second, - ) - - def gen_factor_index(self, data_name, formula_name, vars, sector_list, start_time="", end_time="", period="1d", dividend_type="none"): - return self._call( - "gen_factor_index", - data_name=data_name, - formula_name=formula_name, - vars=vars, - sector_list=list(sector_list or []), - start_time=start_time, - end_time=end_time, - period=period, - dividend_type=dividend_type, - ) - - # ------------------------------------------------------------------ - # 扩展行情/基本面方法(对应 ContextInfo 方法,走 RPC 白名单)。 - # 仅对最常用的显式声明签名;其余通过 __getattr__ 自动转发。 - # ------------------------------------------------------------------ - - def get_longhubang(self, stock_list=None, start_time="", end_time="", count=-1): - return self._call( - "get_longhubang", - stock_list=list(stock_list or []), - start_time=start_time, - end_time=end_time, - count=count, - ) - - def get_top10_share_holder(self, stock_list, data_name, start_time, end_time, report_type="report_time"): - return self._call( - "get_top10_share_holder", - stock_list=list(stock_list or []), - data_name=data_name, - start_time=start_time, - end_time=end_time, - report_type=report_type, - ) - - def get_holder_num(self, stock_list=None, start_time="", end_time="", report_type="report_time"): - return self._call( - "get_holder_num", - stock_list=list(stock_list or []), - start_time=start_time, - end_time=end_time, - report_type=report_type, - ) - - def get_turnover_rate(self, stock_code=None, start_time="19720101", end_time="22010101"): - return self._call( - "get_turnover_rate", - stock_code=list(stock_code or []), - start_time=start_time, - end_time=end_time, - ) - - def get_industry(self, industry_name): - return self._call("get_industry", industry_name=industry_name) - - def bsm_price(self, opt_type, target_price, strike_price, risk_free, sigma, days, dividend=0): - return self._call( - "bsm_price", - opt_type=opt_type, - target_price=target_price, - strike_price=strike_price, - risk_free=risk_free, - sigma=sigma, - days=days, - dividend=dividend, - ) - - def bsm_iv(self, opt_type, target_price, strike_price, option_price, risk_free, days, dividend=0): - return self._call( - "bsm_iv", - opt_type=opt_type, - target_price=target_price, - strike_price=strike_price, - option_price=option_price, - risk_free=risk_free, - days=days, - dividend=dividend, - ) - - def get_option_iv(self, opt_code): - return self._call("get_option_iv", opt_code=opt_code) - - def get_option_detail_data(self, stockcode): - return self._call("get_option_detail_data", stockcode=stockcode) - - def get_option_undl_data(self, undl_code_ref=""): - return self._call("get_option_undl_data", undl_code_ref=undl_code_ref) - - def get_option_undl(self, opt_code): - return self._call("get_option_undl", opt_code=opt_code) - - def get_raw_financial_data(self, field_list, stock_list, start_time, end_time, report_type="report_time", data_type="dict"): - return self._call( - "get_raw_financial_data", - field_list=list(field_list or []), - stock_list=list(stock_list or []), - start_time=start_time, - end_time=end_time, - report_type=report_type, - data_type=data_type, - ) - - def get_factor_data(self, field_list, stock_list, start_date, end_date): - return self._call( - "get_factor_data", - field_list=list(field_list or []), - stock_list=list(stock_list or []), - start_date=start_date, - end_date=end_date, - ) - - def get_north_finance_change(self, period): - return self._call("get_north_finance_change", period=period) - - def get_hkt_statistics(self, stock_code): - return self._call("get_hkt_statistics", stock_code=stock_code) - - def get_hkt_details(self, stock_code): - return self._call("get_hkt_details", stock_code=stock_code) - - def create_sector(self, sector_name, stock_list): - return self._call("create_sector", sector_name=sector_name, stock_list=list(stock_list or [])) - - def get_stock_name(self, stock): - return self._call("get_stock_name", stock=stock) - - def get_close_price(self, market, stock_code, real_timetag, period=86400000, divid_type=0): - return self._call( - "get_close_price", - market=market, - stock_code=stock_code, - real_timetag=real_timetag, - period=period, - divid_type=divid_type, - ) - - def get_main_contract(self, code_market): - return self._call("get_main_contract", code_market=code_market) - - def get_his_contract_list(self, market): - return self._call("get_his_contract_list", market=market) - - def get_date_location(self, date): - return self._call("get_date_location", date=date) - - def get_his_st_data(self, stock_code): - return self._call("get_his_st_data", stock_code=stock_code) - - def get_his_index_data(self, stock_code): - return self._call("get_his_index_data", stock_code=stock_code) - - def call_method(self, method, **params): - """Generic escape hatch: call any RPC market-data method by name. - - Use this for ContextInfo methods that don't have an explicit wrapper - above (e.g. ``xtdata.call_method("get_last_close", stock="000001.SZ")``, - ``xtdata.call_method("get_float_caps", stockcode="000001.SZ")``). The - full list of callable methods is in ``MARKET_DATA_METHODS``. - """ - return self._call(method, **params) - - # ------------------------------------------------------------------ - # L2 行情(需 L2 权限 + 原生 xtdata SDK 行情服务) - # ------------------------------------------------------------------ - - def get_l2_quote(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - return self._call("get_l2_quote", field_list=list(field_list or []), - stock_code=stock_code, start_time=start_time, end_time=end_time, count=count) - - def get_l2_order(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - return self._call("get_l2_order", field_list=list(field_list or []), - stock_code=stock_code, start_time=start_time, end_time=end_time, count=count) - - def get_l2_transaction(self, field_list=None, stock_code="", start_time="", end_time="", count=-1): - return self._call("get_l2_transaction", field_list=list(field_list or []), - stock_code=stock_code, start_time=start_time, end_time=end_time, count=count) - - # ------------------------------------------------------------------ - # 指数权重 / 交易日历 / 交易时段 / 可转债 / 品种判断 - # ------------------------------------------------------------------ - - def get_index_weight(self, index_code): - return self._call("get_index_weight", index_code=index_code) - - def get_trading_calendar(self, market, start_time="", end_time="", tradetimes=False): - return self._call("get_trading_calendar", market=market, start_time=start_time, - end_time=end_time, tradetimes=tradetimes) - - def get_trade_times(self, stockcode): - return self._call("get_trade_times", stockcode=stockcode) - - def get_cb_info(self, stockcode): - return self._call("get_cb_info", stockcode=stockcode) - - def is_stock_type(self, stock, tag): - return self._call("is_stock_type", stock=stock, tag=tag) - - # ------------------------------------------------------------------ - # 板块增删 - # ------------------------------------------------------------------ - - def add_sector(self, sector_name, stock_list): - return self._call("add_sector", sector_name=sector_name, stock_list=list(stock_list or [])) - - def remove_sector(self, sector_name): - return self._call("remove_sector", sector_name=sector_name) - - # ------------------------------------------------------------------ - # 时间戳转换(纯计算) - # ------------------------------------------------------------------ - - @staticmethod - def datetime_to_timetag(datetime_str, format="%Y%m%d%H%M%S"): - import datetime as _dt - try: - return int(_dt.datetime.strptime(str(datetime_str), format).timestamp() * 1000) - except Exception: - return 0 - - @staticmethod - def timetag_to_datetime(timetag, format): - import datetime as _dt - try: - return _dt.datetime.fromtimestamp(int(timetag) / 1000.0).strftime(format) - except Exception: - return "" - - @staticmethod - def timetagToDateTime(timetag, format): - return BigQmtXtData.timetag_to_datetime(timetag, format) - - -class BigQmtXtTrader: - def __init__( - self, - path=None, - session_id=None, - account_id=None, - redis_client=None, - redis_config=None, - timeout_seconds=None, - ): - self.path = path - self.session_id = session_id - self.client = BigQmtRpcClient( - account_id=account_id, - redis_client=redis_client, - redis_config=redis_config, - timeout_seconds=timeout_seconds, - ) - self.callback = None - self._event_thread = None - self._event_running = False - # Async order submission (issue #50). One worker, started on first use, - # so a client that never calls order_stock_async pays nothing. - self._async_order_queue = _queue.Queue() - self._async_order_thread = None - self._async_order_lock = threading.Lock() - - def _cached_position_snapshot(self, account_id): - key = "bigqmt:positions:%s" % str(account_id or self.client.account_id or "") - try: - raw = self.client._redis().get(key) - except Exception: - return {} - if not raw: - return {} - try: - return json.loads(raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else str(raw)) - except Exception: - return {} - - def _cached_positions(self, account_id): - snapshot = self._cached_position_snapshot(account_id) - positions = snapshot.get("positions") if isinstance(snapshot, dict) else None - if isinstance(positions, dict): - return positions - if isinstance(positions, list): - return {str(item.get("stock_code") or idx): item for idx, item in enumerate(positions)} - return {} - - def _cached_asset(self, account_id): - snapshot = self._cached_position_snapshot(account_id) - asset = snapshot.get("asset") if isinstance(snapshot, dict) else None - return asset if isinstance(asset, dict) else {} - - def _redis_cache_enabled(self): - return str(getattr(self.client, "transport_name", "redis") or "redis").lower() in ( - "redis", - "", - "default", - ) - - def register_callback(self, callback): - self.callback = callback - return 0 - - def start(self): - # Launch the real-time execution-event listener so a registered callback's - # on_stock_order / on_stock_trade fire as soon as Big QMT pushes them. - self._start_event_listener() - return 0 - - def connect(self): - if self.client.account_id: - self.client.call("ping") - self._fire_account_status() - return 0 - - def subscribe(self, account): - if not self.client.account_id: - self.client.account_id = _account_id(account) - # (Re)start the listener now that the account is known; the loop resubscribes - # to the account's channels within ~1s if the account changed. - self._start_event_listener() - self._fire_account_status() - return 0 - - def stop(self): - self._event_running = False - thread = self._event_thread - if thread is not None and thread.is_alive(): - thread.join(1.0) - self._event_thread = None - return 0 - - def _start_event_listener(self): - if self._event_thread is not None and self._event_thread.is_alive(): - return - self._event_running = True - self._event_thread = threading.Thread( - target=self._event_loop, name="bigqmt-exec-events", daemon=True - ) - self._event_thread.start() - - def _fire_account_status(self): - """Fire on_account_status after connect/subscribe (MiniQMT parity). - - Big QMT has no per-strategy account-status push; we synthesize a - CONNECTED status once the RPC link is up so client code that waits - for on_account_status before trading keeps working. - """ - callback = self.callback - if callback is None: - return - try: - callback.on_account_status( - CompatObject( - account_id=str(self.client.account_id or ""), - account_type="STOCK", - status=1, # ACCOUNT_STATUS_ONLINE (MiniQMT XtAccountStatus) - ) - ) - except Exception: - pass - - def _event_loop(self): - from .exec_events import ( - order_channel, - trade_channel, - order_error_channel, - cancel_error_channel, - ) - - while self._event_running: - account_id = str(self.client.account_id or "") - pubsub = None - try: - pubsub = self.client._redis().pubsub(ignore_subscribe_messages=True) - pubsub.subscribe( - order_channel(account_id), - trade_channel(account_id), - order_error_channel(account_id), - cancel_error_channel(account_id), - ) - while self._event_running: - if str(self.client.account_id or "") != account_id: - break # account changed -> reconnect and resubscribe - message = pubsub.get_message(timeout=1.0) - if not message or message.get("type") != "message": - continue - self._dispatch_event(message.get("data")) - except Exception: - time.sleep(1.0) - finally: - try: - if pubsub is not None: - pubsub.close() - except Exception: - pass - - def _dispatch_event(self, raw): - callback = self.callback - if callback is None: - return - try: - text = raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else str(raw) - event = json.loads(text) - except Exception: - return - if not isinstance(event, dict): - return - account_id = str(event.get("account_id") or self.client.account_id or "") - try: - event_type = event.get("event_type") - if event_type == "trade": - callback.on_stock_trade(self._trade_from_dict(account_id, event)) - elif event_type == "order": - callback.on_stock_order(self._order_from_dict(account_id, event)) - elif event_type == "order_error": - callback.on_order_error( - CompatObject( - error_id=event.get("error_id"), - error_msg=event.get("error_msg") or "", - order_sys_id=event.get("order_sys_id") or "", - order_id=event.get("order_sys_id") or "", - stock_code=event.get("stock_code") or "", - ) - ) - elif event_type == "cancel_error": - callback.on_cancel_error( - CompatObject( - error_id=event.get("error_id"), - error_msg=event.get("error_msg") or "", - order_sys_id=event.get("order_sys_id") or "", - order_id=event.get("order_sys_id") or "", - stock_code=event.get("stock_code") or "", - ) - ) - except Exception: - pass - - def run_forever(self): - while True: - time.sleep(3600) - - def query_stock_asset(self, account): - account_id = _account_id(account, self.client.account_id) - try: - data = self.client.call("query_stock_asset", {"account_id": account_id}, account_id=account_id) or {} - except Exception: - if not self._redis_cache_enabled(): - raise - data = self._cached_asset(account_id) - if not data: - raise - if ( - self._redis_cache_enabled() - and data.get("cash") is None - and data.get("total_asset") is None - ): - data = self._cached_asset(account_id) or data - cash = data.get("cash") - total_asset = data.get("total_asset") - frozen_cash = data.get("frozen_cash") - market_value = data.get("market_value") - if market_value is None and cash is not None and total_asset is not None: - # total_asset = cash(available) + frozen_cash + market_value. Older - # servers send neither frozen_cash nor market_value; deriving without - # frozen_cash overstates market value by the frozen amount, so - # subtract it whenever the server did report it. - market_value = _safe_float(total_asset) - _safe_float(cash) - if frozen_cash is not None: - market_value -= _safe_float(frozen_cash) - return CompatObject( - account_id=account_id, - cash=_safe_float(cash, 0.0) if cash is not None else None, - available_cash=_safe_float(cash, 0.0) if cash is not None else None, - # MiniQMT's XtAsset always exposes frozen_cash, so default to 0.0 - # rather than None: callers do arithmetic on it. - frozen_cash=_safe_float(frozen_cash, 0.0) if frozen_cash is not None else 0.0, - total_asset=_safe_float(total_asset, 0.0) if total_asset is not None else None, - market_value=_safe_float(market_value, 0.0) if market_value is not None else 0.0, - ) - - def _position_object(self, account_id, item): - volume = _safe_int(item.get("volume")) - available = _safe_int(item.get("available", item.get("can_use_volume"))) - cost = _safe_float(item.get("cost", item.get("avg_price"))) - price = _safe_float(item.get("price", item.get("last_price")), cost) - market_value = item.get("market_value") - if market_value is None: - market_value = price * volume - return CompatObject( - account_type=2, - account_id=account_id, - stock_code=str(item.get("stock_code") or ""), - stock_name=str(item.get("stock_name") or ""), - volume=volume, - can_use_volume=available, - enable_amount=available, - available_amount=available, - avg_price=cost, - price=price, - open_price=_safe_float(item.get("open_price"), cost), - cost_price=cost, - market_value=_safe_float(market_value, 0.0), - frozen_volume=_safe_int(item.get("frozen_volume")), - on_road_volume=_safe_int(item.get("on_road_volume")), - yesterday_volume=_safe_int(item.get("yesterday_volume"), volume), - direction=_safe_int(item.get("direction"), 48), - ) - - @staticmethod - def _position_items(data): - if isinstance(data, dict): - return list(data.values()) - return _as_list(data) - - def query_stock_positions(self, account): - account_id = _account_id(account, self.client.account_id) - try: - data = self.client.call("query_stock_positions", {"account_id": account_id}, account_id=account_id) or {} - except Exception: - if not self._redis_cache_enabled(): - raise - data = self._cached_positions(account_id) - if not data: - raise - return [self._position_object(account_id, item) for item in self._position_items(data)] - - def query_stock_position(self, account, stock_code): - account_id = _account_id(account, self.client.account_id) - try: - data = self.client.call( - "query_stock_position", - {"account_id": account_id, "stock_code": stock_code}, - account_id=account_id, - ) - except Exception: - if not self._redis_cache_enabled(): - raise - normalized = str(stock_code or "").strip().upper() - data = None - for code, item in self._cached_positions(account_id).items(): - if str(code).upper() == normalized or str(code).split(".", 1)[0].upper() == normalized: - data = item - break - if data is None: - raise - if not data: - return None - return [ - self._position_object(account_id, item) - for item in [data] - ][0] - - def query_stock_orders(self, account, cancelable_only=False, strategy_name=""): - # strategy_name 默认 ""(返回全部):与服务端一致,避免下单用的策略名 - # 与查询默认值不匹配导致委托查不到(strategy_name 陷阱)。 - account_id = _account_id(account, self.client.account_id) - data = self.client.call( - "query_stock_orders", - { - "account_id": account_id, - "cancelable_only": bool(cancelable_only), - "strategy_name": strategy_name, - }, - account_id=account_id, - ) or [] - return [self._order_from_dict(account_id, item) for item in _as_list(data)] - - def query_stock_order(self, account, order_id): - order_id = str(order_id or "") - for order in self.query_stock_orders(account, cancelable_only=False): - if str(order.order_id) == order_id or str(order.order_sysid) == order_id: - return order - return None - - def query_stock_trades(self, account, strategy_name="bigqmt_signal_trader"): - account_id = _account_id(account, self.client.account_id) - data = self.client.call( - "query_stock_trades", - {"account_id": account_id, "strategy_name": strategy_name}, - account_id=account_id, - ) or [] - return [self._trade_from_dict(account_id, item) for item in _as_list(data)] - - def query_execution_snapshot( - self, - account, - order_strategy_name="bigqmt_signal_trader", - trade_strategy_name="", - ): - """Query orders and account-wide trades in one RPC round trip.""" - account_id = _account_id(account, self.client.account_id) - data = self.client.call( - "query_execution_snapshot", - { - "account_id": account_id, - "order_strategy_name": order_strategy_name, - "trade_strategy_name": trade_strategy_name, - }, - account_id=account_id, - ) or {} - result = dict(data) if isinstance(data, dict) else {} - result["orders"] = [ - self._order_from_dict(account_id, item) - for item in _as_list(result.get("orders")) - ] - result["trades"] = [ - self._trade_from_dict(account_id, item) - for item in _as_list(result.get("trades")) - ] - return result - - def order_stock( - self, - account, - stock_code, - order_type, - order_volume, - price_type, - price, - strategy_name, - order_remark, - ): - data = self.order_stock_result( - account, stock_code, order_type, order_volume, price_type, - price, strategy_name, order_remark, - ) - return data.get("order_sys_id") or -1 - - def order_stock_result( - self, account, stock_code, order_type, order_volume, price_type, - price, strategy_name, order_remark, wait_settlement=True, - ): - """Submit one order over RPC. - - ``wait_settlement=False`` tells the server to reply as soon as passorder - returns instead of holding the reply until QMT assigns the order id. - The async path uses it; the id then arrives through order_callback - (issue #50). - """ - account_id = _account_id(account, self.client.account_id) - user_order_id = str(order_remark or "").strip() - if not user_order_id: - user_order_id = "bqrpc:%s:%s" % (int(time.time() * 1000), uuid.uuid4().hex[:10]) - payload = { - "account_id": account_id, - "stock_code": stock_code, - "order_type": order_type, - "order_volume": order_volume, - "price_type": price_type, - "price": price, - "strategy_name": strategy_name, - "order_remark": user_order_id, - } - if not wait_settlement: - payload["wait_settlement"] = False - try: - return self.client.call("order_stock", payload, account_id=account_id) or {} - except TimeoutError as exc: - raise TimeoutError( - "order_stock rpc timeout; user_order_id=%s. Query orders/trades before retrying to avoid duplicate orders. %s" - % (user_order_id, exc) - ) - - def _async_order_worker(self): - """Drain queued async orders, one at a time. - - A single worker rather than a pool: the server handles order RPCs on - the QMT adjust thread serially anyway, so concurrency here buys little, - while serializing keeps on_order_stock_async_response arriving in - submission order. For real batch throughput use order_stock_batch. - """ - while True: - job = self._async_order_queue.get() - if job is None: # shutdown sentinel - self._async_order_queue.task_done() - return - seq, args, kwargs = job - try: - self._run_async_order(seq, args, kwargs) - except Exception: - # A worker that dies takes every later async order with it. - pass - finally: - self._async_order_queue.task_done() - - def _ensure_async_order_worker(self): - with self._async_order_lock: - if self._async_order_thread is not None and self._async_order_thread.is_alive(): - return - thread = threading.Thread( - target=self._async_order_worker, name="bigqmt-async-order", daemon=True - ) - self._async_order_thread = thread - thread.start() - - def _run_async_order(self, seq, args, kwargs): - """Do the actual submit and fire the matching callback. Worker thread.""" - stock_code = str(kwargs.get("stock_code") or (args[1] if len(args) > 1 else "")) - callback = self.callback - try: - # wait_settlement=False: return as soon as passorder ran. The order - # id arrives via order_callback, which is what MiniQMT does too. - result = self.order_stock_result(*args, wait_settlement=False, **kwargs) - except Exception as exc: - if callback is not None: - try: - callback.on_order_error( - CompatObject( - error_id=getattr(exc, "errno", 0), - error_msg=str(exc), - order_sys_id="", - order_id="", - stock_code=stock_code, - seq=seq, - ) - ) - except Exception: - pass - return - - order_sys_id = "" - user_order_id = "" - if isinstance(result, dict): - order_sys_id = str(result.get("order_sys_id") or result.get("order_sysid") or "") - user_order_id = str(result.get("user_order_id") or "") - elif result is not None: - order_sys_id = str(result) - - # order_stock returns -1 when the submit itself failed. The server also - # pushes an order_error for a 废单; the two carry different information - # (RPC submit failure vs QMT rejection detail), so both stay available. - if order_sys_id == "-1" or result == -1: - if callback is not None: - try: - callback.on_order_error( - CompatObject( - error_id=-1, - error_msg="order submit failed (order_stock returned -1)", - order_sys_id="", - order_id="", - stock_code=stock_code, - seq=seq, - ) - ) - except Exception: - pass - return - - if callback is not None: - try: - # Native XtOrderResponse shape: one argument carrying - # account_id/order_id/seq/error_msg. order_id may be empty here - # -- the id is assigned asynchronously and lands in the - # order_callback push (issue #50). - callback.on_order_stock_async_response( - CompatObject( - account_id=self.client.account_id, - seq=seq, - order_id=order_sys_id or user_order_id, - order_sys_id=order_sys_id, - stock_code=stock_code, - strategy_name=str(kwargs.get("strategy_name") or (args[6] if len(args) > 6 else "")), - order_remark=str(kwargs.get("order_remark") or (args[7] if len(args) > 7 else "")), - error_msg="", - ), - ) - except Exception: - pass - - def order_stock_async(self, *args, **kwargs): - """Queue an order and return its seq immediately (MiniQMT semantics). - - This used to call order_stock inline, so it blocked for the full RPC - round trip plus -- after the issue #44 change -- however long the server - waited for QMT to assign an order id. That is 0.5-1s per order, which - defeats the point of an async API (issue #50). - - Now the submit runs on a worker thread and the outcome arrives through - on_order_stock_async_response / on_order_error, both carrying the seq so - callers can correlate. Returns the seq without touching the network. - """ - seq = self._next_async_seq() - self._ensure_async_order_worker() - self._async_order_queue.put((seq, args, kwargs)) - return seq - - def wait_async_orders(self, timeout=10.0): - """Block until every queued async order has been submitted. - - For tests and for shutdown; the API itself is fire-and-forget. Returns - False on timeout rather than hanging. Uses task_done bookkeeping, so it - waits for the in-flight job too, not merely for the queue to drain. - """ - queue_obj = getattr(self, "_async_order_queue", None) - if queue_obj is None: - return True - deadline = time.time() + float(timeout) - while queue_obj.unfinished_tasks: - if time.time() > deadline: - return False - time.sleep(0.005) - return True - - def order_stock_batch(self, account, orders, batch_id=""): - account_id = _account_id(account, self.client.account_id) - payload = [] - for item in orders or []: - entry = dict(item or {}) - entry.setdefault("account_id", account_id) - payload.append(entry) - params = {"account_id": account_id, "orders": payload} - if batch_id: - params["batch_id"] = str(batch_id) - return self.client.call( - "order_stock_batch", - params, - account_id=account_id, - ) or [] - - def cancel_order_stock_sysid(self, account, market, order_sysid): - account_id = _account_id(account, self.client.account_id) - data = self.client.call( - "cancel_order_stock_sysid", - { - "account_id": account_id, - "market": market, - "order_sysid": order_sysid, - }, - account_id=account_id, - ) or {} - return bool(data.get("success", data)) - - def cancel_order_stock(self, account, order_id): - return self.cancel_order_stock_sysid(account, "", order_id) - - def unsubscribe(self, account): - # MiniQMT xttrader.unsubscribe(account) — 取消账户订阅。 - # Big QMT RPC 模式下账户是被动响应,unsubscribe 为 no-op。 - return 0 - - # ------------------------------------------------------------------ - # 账户 / 融资融券扩展查询 - # 这些在 MiniQMT 走 XtQuantServer RPC;Big QMT 经 - # get_trade_detail_data 查询,需相应账户权限(两融账户等)。 - # 无权限/上下文未绑定时服务端降级为 []。 - # ------------------------------------------------------------------ - - def _query_account_list(self, account, method): - account_id = _account_id(account, self.client.account_id) - try: - return self.client.call(method, {"account_id": account_id}, account_id=account_id) or [] - except Exception: - return [] - - def query_account_infos(self, account=None): - return self._query_account_list(account, "query_account_infos") - - def query_account_status(self, account=None): - return self._query_account_list(account, "query_account_status") - - def query_credit_detail(self, account): - return self._query_account_list(account, "query_credit_detail") - - def query_stk_compacts(self, account): - return self._query_account_list(account, "query_stk_compacts") - - def query_credit_subjects(self, account): - return self._query_account_list(account, "query_credit_subjects") - - def query_credit_slo_code(self, account): - return self._query_account_list(account, "query_credit_slo_code") - - def query_credit_assure(self, account): - return self._query_account_list(account, "query_credit_assure") - - def query_appointment_info(self, account): - return self._query_account_list(account, "query_appointment_info") - - def query_smt_secu_info(self, account): - return self._query_account_list(account, "query_smt_secu_info") - - def query_smt_secu_rate(self, account, stock_code, max_term, fare_way, credit_type, trade_type): - account_id = _account_id(account, self.client.account_id) - try: - return self.client.call( - "query_smt_secu_rate", - {"account_id": account_id, "stock_code": stock_code, "max_term": max_term, - "fare_way": fare_way, "credit_type": credit_type, "trade_type": trade_type}, - account_id=account_id, - ) or [] - except Exception: - return [] - - def query_ipo_data(self, account=None): - return self._query_account_list(account, "query_appointment_info") - - def query_new_purchase_limit(self, account): - return {} - - # ------------------------------------------------------------------ - # async 变体:MiniQMT 的 *_async 方法返回 seq 后异步回调。 - # 在 RPC 模型里请求-响应本就是同步的,这里直接转发到同步实现并 - # 返回一个递增 seq,让旧代码 ``xt_trader.query_stock_positions_async(acc)`` - # 不报错(回调仍由 register_callback 注册的回调在事件来时触发)。 - # ------------------------------------------------------------------ - - _async_seq = 0 - - def _next_async_seq(self): - BigQmtXtTrader._async_seq += 1 - return BigQmtXtTrader._async_seq - - def _async_query(self, sync_call, account, callback, *args, **kwargs): - """Shared async query helper. - - MiniQMT's *_async query methods take a callback and hand the result to - it (they return None). We accept an OPTIONAL callback for compat: when - given, we call callback(result) synchronously (our RPC is already - synchronous) and return None like MiniQMT; when omitted, we keep our - seq-returning extension so existing callers don't break. - """ - result = sync_call(account, *args, **kwargs) - if callback is not None: - try: - callback(result) - except Exception: - pass - return None - return self._next_async_seq() - - def query_stock_asset_async(self, account, callback=None): - return self._async_query(self.query_stock_asset, account, callback) - - def query_stock_positions_async(self, account, callback=None): - return self._async_query(self.query_stock_positions, account, callback) - - def query_stock_orders_async(self, account, cancelable_only=False, callback=None): - if callback is not None: - result = self.query_stock_orders(account, cancelable_only) - try: - callback(result) - except Exception: - pass - return None - return self._next_async_seq() - - def query_stock_trades_async(self, account, callback=None): - return self._async_query(self.query_stock_trades, account, callback) - - def query_account_infos_async(self, account=None, callback=None): - if callback is not None: - result = self.query_account_infos(account) - try: - callback(result) - except Exception: - pass - return None - return self._next_async_seq() - - def query_account_status_async(self, account=None, callback=None): - if callback is not None: - result = self.query_account_status(account) - try: - callback(result) - except Exception: - pass - return None - return self._next_async_seq() - - def query_credit_detail_async(self, account, callback=None): - return self._async_query(self.query_credit_detail, account, callback) - - def query_stk_compacts_async(self, account, callback=None): - return self._async_query(self.query_stk_compacts, account, callback) - - def query_credit_subjects_async(self, account, callback=None): - return self._async_query(self.query_credit_subjects, account, callback) - - def query_credit_slo_code_async(self, account, callback=None): - return self._async_query(self.query_credit_slo_code, account, callback) - - def query_credit_assure_async(self, account, callback=None): - return self._async_query(self.query_credit_assure, account, callback) - - def query_ipo_data_async(self, account=None, callback=None): - if callback is not None: - result = self.query_ipo_data(account) - try: - callback(result) - except Exception: - pass - return None - return self._next_async_seq() - - def query_new_purchase_limit_async(self, account, callback=None): - return self._async_query(self.query_new_purchase_limit, account, callback) - - def query_appointment_info_async(self, account, callback=None): - return self._async_query(self.query_appointment_info, account, callback) - - def cancel_order_stock_async(self, account, order_id): - # MiniQMT: returns seq, result comes back via on_cancel_order_stock_async_response. - seq = self._next_async_seq() - try: - ok = self.cancel_order_stock(account, order_id) - except Exception as exc: - callback = self.callback - if callback is not None: - try: - callback.on_cancel_error( - CompatObject( - error_id=getattr(exc, "errno", 0), - error_msg=str(exc), - order_sys_id=str(order_id or ""), - stock_code="", - ) - ) - except Exception: - pass - return seq - callback = self.callback - if callback is not None: - try: - callback.on_cancel_order_stock_async_response( - CompatObject( - account_id=self.client.account_id, - seq=seq, - success=bool(ok), - order_sys_id=str(order_id or ""), - order_id=str(order_id or ""), - ), - ) - except Exception: - pass - return seq - - def cancel_order_stock_sysid_async(self, account, market, order_sysid): - seq = self._next_async_seq() - try: - ok = self.cancel_order_stock_sysid(account, market, order_sysid) - except Exception as exc: - callback = self.callback - if callback is not None: - try: - callback.on_cancel_error( - CompatObject( - error_id=getattr(exc, "errno", 0), - error_msg=str(exc), - order_sys_id=str(order_sysid or ""), - stock_code="", - ) - ) - except Exception: - pass - return seq - callback = self.callback - if callback is not None: - try: - callback.on_cancel_order_stock_async_response( - CompatObject( - account_id=self.client.account_id, - seq=seq, - success=bool(ok), - order_sys_id=str(order_sysid or ""), - order_id=str(order_sysid or ""), - ), - ) - except Exception: - pass - return seq - - def set_relaxed_response_order_enabled(self, enabled=True): - # 内部行为开关,RPC 模式下无意义,no-op。 - return 0 - - def smt_appointment_async(self, account, stock_code, apt_days, apt_volume, - fare_ratio, sub_rare_ratio, fine_ratio, begin_date): - # SMB/预约打新走独立通道,RPC 桥不支持;返回 -1 表示失败(对齐 MiniQMT - # 语义:seq 为 -1 表示委托失败)。 - return -1 - - def _order_from_dict(self, account_id, item): - action = item.get("action") - order_type = _action_to_order_type(action) - order_sysid = str(item.get("order_sys_id") or item.get("order_sysid") or item.get("order_id") or "") - return CompatObject( - account_id=account_id, - stock_code=str(item.get("stock_code") or ""), - order_type=order_type, - order_status=_safe_int(item.get("status", item.get("order_status")), ORDER_UNKNOWN), - order_volume=_safe_int(item.get("volume", item.get("order_volume"))), - traded_volume=_safe_int(item.get("traded_volume")), - price=_safe_float(item.get("price")), - order_sysid=order_sysid, - order_id=order_sysid or str(item.get("user_order_id") or ""), - strategy_name=str(item.get("strategy_name") or ""), - order_remark=str(item.get("remark") or item.get("user_order_id") or ""), - # MiniQMT XtOrder.order_time 是 Unix 秒。0 = 服务端未上报 - # (旧服务端不带这个字段), 不要当成 1970 年 (issue #48)。 - order_time=_safe_int(item.get("order_time"), 0), - ) - - def _trade_from_dict(self, account_id, item): - action = item.get("action") - order_type = _action_to_order_type(action) - order_sysid = str(item.get("order_sys_id") or item.get("order_sysid") or "") - trade_id = str(item.get("trade_id") or "") - return CompatObject( - account_id=account_id, - stock_code=str(item.get("stock_code") or ""), - order_type=order_type, - order_sysid=order_sysid, - order_id=order_sysid, - trade_id=trade_id, - traded_volume=_safe_int(item.get("volume", item.get("traded_volume"))), - traded_price=_safe_float(item.get("price", item.get("traded_price"))), - traded_at=str(item.get("traded_at") or ""), - order_remark=str(item.get("user_order_id") or item.get("remark") or ""), - ) - - -XtQuantTrader = BigQmtXtTrader - - -_default_client = None -xt_trader = None -xtdata = None - - -def configure(account_id=None, redis_client=None, redis_config=None, timeout_seconds=None): - global _default_client, xt_trader, xtdata - _default_client = BigQmtRpcClient( - account_id=account_id, - redis_client=redis_client, - redis_config=redis_config, - timeout_seconds=timeout_seconds, - ) - if xt_trader is None: - xt_trader = BigQmtXtTrader(account_id=_default_client.account_id, redis_client=_default_client.redis_client) - xt_trader.client = _default_client - if xtdata is None: - xtdata = BigQmtXtData(_default_client) - else: - xtdata.client = _default_client - return xt_trader, xtdata - - -def get_default_client(): - global _default_client - if _default_client is None: - configure() - return _default_client - - -configure() - - -__all__ = [ - "BigQmtRpcClient", - "BigQmtXtData", - "BigQmtXtTrader", - "CompatObject", - "StockAccount", - "XtQuantTrader", - "XtQuantTraderCallback", - "configure", - "get_default_client", - "load_client_config", - "xt_trader", - "xtdata", -] diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_client_config.example.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_client_config.example.py deleted file mode 100644 index 50b8805..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_client_config.example.py +++ /dev/null @@ -1,102 +0,0 @@ -# coding: utf-8 -"""Client-side private config example for MiniQMT-compatible replacement. - -Copy this file to: - - src/bigqmt_signal_trader_client_config.py - -Do not commit the real file. It may contain account ids and Redis credentials. -""" - -BIGQMT_ACCOUNT_ID = "YOUR_ACCOUNT_ID" -BIGQMT_RPC_TIMEOUT_SECONDS = 6.0 -BIGQMT_DOWNLOAD_WAIT_SECONDS = 1800 -BIGQMT_DOWNLOAD_POLL_INTERVAL_SECONDS = 0.5 - -BIGQMT_REDIS_CONFIG = { - "host": "YOUR_REDIS_HOST", - "port": 6379, - "db": 5, - "username": "", - "password": "", - # Transport selection. Must match the QMT-side server config. Default - # "redis" works with the standard DRYRUN; use "zmq" when the server runs - # with transport=zmq (e.g. the no-redis version or explicit zmq mode). - "transport": "redis", - # ZMQ-specific settings (only used when transport=zmq): - # "zmq": { - # # Explicit connect address. The QMT-side server binds a port derived - # # from account_id (default 15563 for account 8886800503). If you know - # # the exact address, set it here to skip service discovery. - # "connect_address": "tcp://127.0.0.1:15563", - # # "host": "127.0.0.1", - # # "port": 15563, - # }, -} - -# Default direct mode calls get_full_tick through RPC. Set enabled=True only when -# you want client-side get_full_tick to read demand-driven Redis snapshots. -BIGQMT_FULL_TICK_CACHE_CONFIG = { - "enabled": False, - "demand_ttl_seconds": 10, - "cache_ttl_seconds": 10, - "wait_seconds": 3.5, - "poll_interval_seconds": 0.2, -} - -# Client-side LOCAL market-data cache. -# get_market_data_ex(...) writes returned bars under `dir`; get_local_data(...) -# then reads them locally with NO RPC to Big QMT (for offline / repeated local -# analysis). download_history_data* submits a server-side Big QMT download job. -# - dir: cache folder (default ~/.bigqmt_cache), one pickle per (period, code). -# - fallback_rpc: if True, get_local_data auto-fetches+caches a cache miss; -# if False (default), a cache-missed code is simply omitted (download first). -BIGQMT_LOCAL_CACHE_CONFIG = { - "enabled": True, - "dir": None, # None -> ~/.bigqmt_cache - "fallback_rpc": False, - # Storage format: "auto" (parquet if pyarrow installed, else pickle), - # "parquet" (columnar/compressed/cross-language — recommended), or "pkl". - # One file per (period, dividend_type, code); switching format auto-migrates. - "format": "auto", -} - -# FormulaServer direct read fast-path (port 58600). -# Big QMT's built-in C++ quote/reference service. Routing reads straight to it -# bypasses the RPC bridge AND the QMT python thread's GIL: ~0.07ms vs ~13ms -# over redis. Enabled by default; you normally do not need this block. -# -# Covers reference/history reads only. Account, position, order, trade and -# 五档 (get_full_tick) calls are NOT served by FormulaServer and always go over -# RPC. Every miss — unmapped method, untranslatable params, server down — -# falls back to RPC automatically, so an unreachable 58600 changes nothing. -BIGQMT_FORMULA_SERVER_CONFIG = { - "enabled": True, # or set BIGQMT_FORMULA_ENABLED=0 in the environment - # "host": "127.0.0.1", # FormulaServer binds 0.0.0.0, so cross-machine works - # # if the firewall allows it - # "port": 58600, # unset -> read from qmt_root's formulaserver.ini, - # # then fall back to 58600 - # "qmt_root": r"D:\国金证券QMT交易端", - # "timeout_seconds": 3.0, - # "methods": [...], # restrict routing to a subset (default: all mapped) - # "failure_cooldown_seconds": 30.0, # pause routing this long after a failure -} - -# Whole-quote PUSH subscription (xtdata.subscribe_whole_quote, aligned with MiniQMT). -# Server pushes each incremental tick batch to every client subscribed to the -# same combination; the RPC methods above only manage the subscription lifecycle. -# Data flows over a separate push channel matching `transport` above -# (redis pub/sub, or zmq PUB/SUB when transport="zmq"), msgpack-encoded -# (install the `msgpack` extra; falls back to json if absent). -# -# quote_client_id: process-stable subscriber id. The server counts references -# per (client_id, sub_id) and only tears down the shared big-QMT subscription -# after EVERY client of a combination unsubscribes or times out. Unset -> a -# persisted id is created at ~/.cache/bigqmt/quote_client_id so a restarted -# client is recognised as the same subscriber (needed for replay recovery). -# Heartbeat: client sends quote_keepalive every BIGQMT_QUOTE_HEARTBEAT_SECONDS -# (default 3.0). Server reaps a client after heartbeat_timeout_seconds -# (default 30s = 10 periods, configured server-side). -BIGQMT_QUOTE_CLIENT_ID = None # e.g. "my-strategy-1"; None -> persisted auto id -# BIGQMT_QUOTE_HEARTBEAT_SECONDS = 3.0 # env var; must be < server timeout/periods - diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_diagnostic.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_diagnostic.py deleted file mode 100644 index 19e891f..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_diagnostic.py +++ /dev/null @@ -1,131 +0,0 @@ -# coding: utf-8 -"""Big QMT diagnostics for market data and positions. - -This strategy entry never submits orders. It only probes QMT runtime APIs. -""" - - -_ACCOUNT_ID = "" -_PROBED = False - - -def _resolve_runtime_name(name): - if name in globals(): - return globals()[name] - try: - import builtins - return getattr(builtins, name) - except Exception: - return None - - -def _safe_attr(obj, name, default=None): - return getattr(obj, name, default) - - -def _detect_account(): - account_value = _resolve_runtime_name("account") - return str(account_value or "") - - -def _probe_market(ContextInfo): - code = "000300.SH" - try: - ticks = ContextInfo.get_full_tick([code]) - tick = (ticks or {}).get(code) - if not tick: - print("[bigqmt_diagnostic] market tick missing code=%s raw=%s" % (code, ticks)) - else: - print( - "[bigqmt_diagnostic] market tick ok code=%s lastPrice=%s bid1=%s ask1=%s" - % ( - code, - tick.get("lastPrice"), - (tick.get("bidPrice") or [None])[0], - (tick.get("askPrice") or [None])[0], - ) - ) - except Exception as exc: - print("[bigqmt_diagnostic] market tick failed: %s" % exc) - - try: - detail = ContextInfo.get_instrumentdetail(code) - if not detail: - print("[bigqmt_diagnostic] instrument missing code=%s" % code) - else: - print( - "[bigqmt_diagnostic] instrument ok code=%s status=%s up=%s down=%s" - % ( - code, - detail.get("InstrumentStatus"), - detail.get("UpStopPrice"), - detail.get("DownStopPrice"), - ) - ) - except Exception as exc: - print("[bigqmt_diagnostic] instrument failed: %s" % exc) - - -def _probe_positions(account_id): - query = _resolve_runtime_name("get_trade_detail_data") - if query is None: - print("[bigqmt_diagnostic] position failed: get_trade_detail_data missing") - return - if not account_id: - print("[bigqmt_diagnostic] position skipped: account is empty") - return - - try: - positions = query(account_id, "STOCK", "POSITION") or [] - print("[bigqmt_diagnostic] position ok account=%s count=%s" % (account_id, len(positions))) - for pos in positions[:8]: - print( - "[bigqmt_diagnostic] position item code=%s.%s name=%s volume=%s available=%s" - % ( - _safe_attr(pos, "m_strInstrumentID", ""), - _safe_attr(pos, "m_strExchangeID", ""), - _safe_attr(pos, "m_strInstrumentName", ""), - _safe_attr(pos, "m_nVolume", ""), - _safe_attr(pos, "m_nCanUseVolume", ""), - ) - ) - except Exception as exc: - print("[bigqmt_diagnostic] position failed account=%s error=%s" % (account_id, exc)) - - -def _probe(ContextInfo, reason): - global _PROBED - if _PROBED: - return - _PROBED = True - print("[bigqmt_diagnostic] probe start reason=%s account=%s" % (reason, _ACCOUNT_ID)) - _probe_market(ContextInfo) - _probe_positions(_ACCOUNT_ID) - print("[bigqmt_diagnostic] probe end") - - -def init(ContextInfo): - global _ACCOUNT_ID - _ACCOUNT_ID = _detect_account() - if _ACCOUNT_ID and hasattr(ContextInfo, "set_account"): - ContextInfo.set_account(_ACCOUNT_ID) - print("[bigqmt_diagnostic] init ok account=%s" % _ACCOUNT_ID) - _probe(ContextInfo, "init") - - -def handlebar(ContextInfo): - if hasattr(ContextInfo, "is_last_bar") and not ContextInfo.is_last_bar(): - return None - return _probe(ContextInfo, "handlebar") - - -def adjust(ContextInfo): - return handlebar(ContextInfo) - - -def order_callback(ContextInfo, orderInfo): - return None - - -def deal_callback(ContextInfo, dealInfo): - return None diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_dryrun.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_dryrun.py deleted file mode 100644 index 6e61c90..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_dryrun.py +++ /dev/null @@ -1,28 +0,0 @@ -# coding: utf-8 -"""Big QMT signal trader dry-run entry. - -Put this file into QMT's python strategy directory and load it from QMT. -Current default uses empty signal source and DryRunOrderGateway, so it will not -submit real orders. -""" - -from bigqmt_signal_trader_strategy import ( # noqa: E402 - adjust, - configure, - deal_callback, - handlebar, - init, - order_callback, - set_account_id, - sync_positions, -) - - -# Fill this before real account testing. Leave empty for dry-run loading tests. -ACCOUNT_ID = "" - - -if ACCOUNT_ID: - set_account_id(ACCOUNT_ID) - -configure(mode="dryrun", account_id=ACCOUNT_ID or "dryrun") diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_local_config.example.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_local_config.example.py deleted file mode 100644 index 924bcf7..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_local_config.example.py +++ /dev/null @@ -1,64 +0,0 @@ -# coding: utf-8 -"""Local private config example for the QMT python directory. - -Copy this file to the QMT python directory as: - - bigqmt_signal_trader_local_config.py - -Do not commit the real file. It may contain account ids and Redis credentials. -""" - -BIGQMT_ACCOUNT_ID = "YOUR_ACCOUNT_ID" - -BIGQMT_REDIS_CONFIG = { - "host": "127.0.0.1", - "port": 6379, - "db": 5, - "username": "", - "password": "", - # Keep order RPC disabled unless you explicitly want remote order/cancel. - "rpc_allow_order_methods": False, - # Redis and ZMQ can both drain requests through QMT's official - # run_time("adjust", ...) callback. This avoids GIL stalls in QMT's process. - "rpc_process_in_listener": True, - "rpc_listener_methods": ("*",), - "rpc_background_threads": False, - "schedule_adjust": True, - "schedule_adjust_interval": "100nMilliSecond", - # The default mode calls get_full_tick through RPC. Enable this cache only - # if full-market payloads are too large for your latency/CPU budget. - # When a client calls get_full_tick, it renews demand for 10 seconds. - # Symbol-list demands refresh every full_tick_refresh_interval_seconds; whole-market - # (SH/SZ/BJ/HK) demands refresh on the slower market interval so a ~50k row snapshot - # is not pulled every fast tick. - "full_tick_cache_enabled": False, - "full_tick_demand_ttl_seconds": 10, - "full_tick_cache_ttl_seconds": 10, - "full_tick_refresh_interval_seconds": 0.5, - "full_tick_market_refresh_interval_seconds": 3, - # Wall-clock budget for one refresh round; keeps a slow round from stalling the - # strategy thread (the in-flight demand always completes). - "full_tick_refresh_max_wall_seconds": 0.3, - "full_tick_max_requests": 8, - # Async download jobs: clients submit download_history_data(2) as a job; the - # strategy thread downloads download_job_chunk_size symbols per tick (capped by - # download_job_max_wall_seconds), so a long download never blocks the RPC pump. - # chunk_size is the smallest per-tick block — keep it modest if downloads are slow. - # Disabled: the full terminal's xtdata SDK can't reach a data service to - # download. Supplement history via the terminal's 数据管理/补充数据 UI, then read - # it over RPC (get_market_data_ex/get_local_data). Enable only where a - # MiniQMT/xtdata data service is connectable. - "download_jobs_enabled": False, - "download_job_chunk_size": 10, - "download_job_max_wall_seconds": 0.5, - "download_job_ttl_seconds": 3600, - # Push order_callback/deal_callback details to Redis so clients get real-time - # on_stock_order / on_stock_trade callbacks (MiniQMT style) instead of polling. - "exec_events_enabled": True, - # Dump the raw order_callback/deal_callback object fields to the QMT output - # panel, and attach them to the published event as "raw_fields". Prints on - # every callback, so keep it off outside a diagnosis window. Turn it on to - # observe what m_nDirection / m_nOffsetFlag actually carry in live callbacks - # — the buy/sell mapping in exec_events.py currently assumes 48/49 there. - "exec_events_debug_raw_fields": False, -} diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_dryrun.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_dryrun.py deleted file mode 100644 index 6584216..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_dryrun.py +++ /dev/null @@ -1,63 +0,0 @@ -# coding: utf-8 -"""Big QMT Redis dry-run strategy entry. - -This entry reads Redis db5 test signals and writes Redis state, but orders are -DryRunOrderGateway orders only. It does not submit real QMT orders. -""" - -from bigqmt_signal_trader_strategy import ( # noqa: E402 - adjust, - configure, - deal_callback, - handlebar, - init, - order_callback, - set_account_id, - sync_positions, -) - - -ACCOUNT_ID = "bigqmt_probe" -REDIS_HOST = "127.0.0.1" -REDIS_PORT = 6379 -REDIS_DB = 5 -REDIS_USERNAME = "" -REDIS_PASSWORD = "" - -try: - from bigqmt_signal_trader_local_config import BIGQMT_REDIS_CONFIG -except Exception: - BIGQMT_REDIS_CONFIG = {} - -REDIS_HOST = BIGQMT_REDIS_CONFIG.get("host", REDIS_HOST) -REDIS_PORT = int(BIGQMT_REDIS_CONFIG.get("port", REDIS_PORT)) -REDIS_DB = int(BIGQMT_REDIS_CONFIG.get("db", REDIS_DB)) -REDIS_USERNAME = BIGQMT_REDIS_CONFIG.get("username", REDIS_USERNAME) -REDIS_PASSWORD = BIGQMT_REDIS_CONFIG.get("password", REDIS_PASSWORD) - - -if ACCOUNT_ID: - set_account_id(ACCOUNT_ID) - -configure( - mode="dryrun", - account_id=ACCOUNT_ID, - signal_source_type="redis", - state_store_type="redis", - position_sync_type="redis", - redis={ - "host": REDIS_HOST, - "port": REDIS_PORT, - "db": REDIS_DB, - "username": REDIS_USERNAME, - "password": REDIS_PASSWORD, - "stream_key_template": "bigqmt:signals:{account_id}", - "group_name": "bigqmt-signal-trader", - "consumer_name": "bigqmt-probe", - "block_ms": 0, - "claim_key_template": "bigqmt:signal_claim:{account_id}:{signal_id}", - "status_key_template": "bigqmt:signal_status:{account_id}:{signal_id}", - "position_key_template": "bigqmt:positions:{account_id}", - "position_event_stream_template": "bigqmt:position_events:{account_id}", - }, -) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_rpc_runtime.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_rpc_runtime.py deleted file mode 100644 index 557d44a..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_redis_rpc_runtime.py +++ /dev/null @@ -1,301 +0,0 @@ -# coding: utf-8 -"""Big QMT Redis Pub/Sub RPC strategy entry. - -This entry does not consume trade signals. RPC order methods are disabled by -default; read-only methods and position sync are enabled. -""" - -import os -import sys - - -# QMT loads strategy scripts via exec, so __file__ may be undefined. Build a -# list of candidate directories (script dir guesses + cwd) and put any that -# holds bigqmt_signal_trader_strategy.py on sys.path[0]. This keeps the package -# and bigqmt_signal_trader_local_config importable regardless of how QMT -# invokes the script. -_CANDIDATE_DIRS = [] -try: - _CANDIDATE_DIRS.append(os.path.dirname(os.path.abspath(__file__))) -except Exception: - pass -_CANDIDATE_DIRS.append(os.getcwd()) -for _up in (".", ".."): - _CANDIDATE_DIRS.append(os.path.abspath(os.path.join(os.getcwd(), _up))) -for _dir in _CANDIDATE_DIRS: - if os.path.exists(os.path.join(_dir, "bigqmt_signal_trader_strategy.py")): - if _dir not in sys.path: - sys.path.insert(0, _dir) - break - - -try: - _load_bridge_module = __bigqmt_load_local_module -except NameError: - _load_bridge_module = None - -if _load_bridge_module is not None: - _strategy_module = _load_bridge_module("bigqmt_signal_trader_strategy") - adjust = _strategy_module.adjust - bind_qmt_api = _strategy_module.bind_qmt_api - configure = _strategy_module.configure - deal_callback = _strategy_module.deal_callback - handlebar = _strategy_module.handlebar - init = _strategy_module.init - order_callback = _strategy_module.order_callback - set_account_id = _strategy_module.set_account_id - sync_positions = _strategy_module.sync_positions -else: - from bigqmt_signal_trader_strategy import ( # noqa: E402 - adjust, - bind_qmt_api, - configure, - deal_callback, - handlebar, - init, - order_callback, - set_account_id, - sync_positions, - ) - - -ACCOUNT_ID = "" -REDIS_HOST = "127.0.0.1" -REDIS_PORT = 6379 -REDIS_DB = 5 -REDIS_USERNAME = "" -REDIS_PASSWORD = "" -RPC_ALLOW_ORDER_METHODS = False -RPC_PROCESS_IN_LISTENER = True -RPC_BACKGROUND_THREADS = False -# "*" expands to read-only RPC methods only. Order/cancel/sync methods still go -# through the queue fallback and require schedule_adjust=True when enabled. -RPC_LISTENER_METHODS = ("*",) -# Transport selection. Default "redis" (zero behavior change). Set to "zmq" / -# "mysql" / "shm" in the local config to switch the wire. zmq/mysql sub-config -# (bind/connect address, pool sizing, ...) is forwarded verbatim to the factory. -RPC_TRANSPORT = "redis" -RPC_ZMQ_CONFIG = {} -RPC_MYSQL_CONFIG = {} -SCHEDULE_ADJUST_ENABLED = True -# How often the strategy thread drains the RPC queue (via adjust). Lower = less -# queue wait for read RPCs. Verify on the live box that run_time honors sub-3s -# intervals (see the adjust cadence log) before trusting a low value. -SCHEDULE_ADJUST_INTERVAL = "500nMilliSecond" -FULL_TICK_CACHE_ENABLED = False -FULL_TICK_DEMAND_TTL_SECONDS = 10 -FULL_TICK_CACHE_TTL_SECONDS = 10 -# Symbol-list demands refresh fast; whole-market (SH/SZ/BJ/HK) demands refresh on -# a slower cadence so a ~50k row snapshot is not pulled on every fast tick. -FULL_TICK_REFRESH_INTERVAL_SECONDS = 0.5 -FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS = 3.0 -# Wall-clock budget for one refresh round to avoid stalling the strategy thread. -FULL_TICK_REFRESH_MAX_WALL_SECONDS = 0.3 -FULL_TICK_MAX_REQUESTS = 8 -# Async download jobs: the strategy thread drains one queued job at a time, -# downloading DOWNLOAD_JOB_CHUNK_SIZE symbols per tick (capped by the wall-clock -# budget), so a long download never blocks the RPC pump. chunk_size is the -# smallest per-tick block, so keep it modest if per-symbol downloads are slow. -# DISABLED by default: the full Big QMT terminal's embedded xtdata SDK has no -# reachable data service to download through (raises "无法连接行情服务"). Supplement -# history via the terminal's 数据管理/补充数据 UI, then read it over RPC with -# get_market_data_ex / get_local_data. Re-enable only where a MiniQMT/xtdata data -# service is connectable (set download_jobs_enabled=True in the local config). -DOWNLOAD_JOBS_ENABLED = False -DOWNLOAD_JOB_CHUNK_SIZE = 10 -DOWNLOAD_JOB_MAX_WALL_SECONDS = 0.5 -DOWNLOAD_JOB_TTL_SECONDS = 3600 -# Push order_callback/deal_callback details to Redis so clients get real-time -# on_stock_order/on_stock_trade callbacks (MiniQMT style) instead of polling. -EXEC_EVENTS_ENABLED = True -# Dump the raw order_callback/deal_callback object fields to the QMT output panel -# (and into the published event as "raw_fields"). Off by default — it prints on -# every callback. Turn on to settle what m_nDirection/m_nOffsetFlag actually -# carry live, which the buy/sell direction mapping currently assumes. -EXEC_EVENTS_DEBUG_RAW_FIELDS = False - -try: - from bigqmt_signal_trader_local_config import BIGQMT_ACCOUNT_ID, BIGQMT_REDIS_CONFIG -except Exception: - BIGQMT_ACCOUNT_ID = "" - BIGQMT_REDIS_CONFIG = {} - -ACCOUNT_ID = str(BIGQMT_ACCOUNT_ID or ACCOUNT_ID or "") -REDIS_HOST = BIGQMT_REDIS_CONFIG.get("host", REDIS_HOST) -REDIS_PORT = int(BIGQMT_REDIS_CONFIG.get("port", REDIS_PORT)) -REDIS_DB = int(BIGQMT_REDIS_CONFIG.get("db", REDIS_DB)) -REDIS_USERNAME = BIGQMT_REDIS_CONFIG.get("username", REDIS_USERNAME) -REDIS_PASSWORD = BIGQMT_REDIS_CONFIG.get("password", REDIS_PASSWORD) -RPC_ALLOW_ORDER_METHODS = bool(BIGQMT_REDIS_CONFIG.get("rpc_allow_order_methods", RPC_ALLOW_ORDER_METHODS)) -RPC_PROCESS_IN_LISTENER = bool( - BIGQMT_REDIS_CONFIG.get("rpc_process_in_listener", RPC_PROCESS_IN_LISTENER and not RPC_ALLOW_ORDER_METHODS) -) -RPC_BACKGROUND_THREADS = bool(BIGQMT_REDIS_CONFIG.get("rpc_background_threads", RPC_BACKGROUND_THREADS)) -RPC_LISTENER_METHODS = tuple(BIGQMT_REDIS_CONFIG.get("rpc_listener_methods", RPC_LISTENER_METHODS)) -SCHEDULE_ADJUST_ENABLED = bool(BIGQMT_REDIS_CONFIG.get("schedule_adjust", SCHEDULE_ADJUST_ENABLED)) -if not RPC_BACKGROUND_THREADS: - SCHEDULE_ADJUST_ENABLED = True -SCHEDULE_ADJUST_INTERVAL = str(BIGQMT_REDIS_CONFIG.get("schedule_adjust_interval", SCHEDULE_ADJUST_INTERVAL)) -FULL_TICK_CACHE_ENABLED = bool(BIGQMT_REDIS_CONFIG.get("full_tick_cache_enabled", FULL_TICK_CACHE_ENABLED)) -FULL_TICK_DEMAND_TTL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("full_tick_demand_ttl_seconds", FULL_TICK_DEMAND_TTL_SECONDS) -) -FULL_TICK_CACHE_TTL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("full_tick_cache_ttl_seconds", FULL_TICK_CACHE_TTL_SECONDS) -) -FULL_TICK_REFRESH_INTERVAL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("full_tick_refresh_interval_seconds", FULL_TICK_REFRESH_INTERVAL_SECONDS) -) -FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("full_tick_market_refresh_interval_seconds", FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS) -) -FULL_TICK_REFRESH_MAX_WALL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("full_tick_refresh_max_wall_seconds", FULL_TICK_REFRESH_MAX_WALL_SECONDS) -) -FULL_TICK_MAX_REQUESTS = int(BIGQMT_REDIS_CONFIG.get("full_tick_max_requests", FULL_TICK_MAX_REQUESTS)) -DOWNLOAD_JOBS_ENABLED = bool(BIGQMT_REDIS_CONFIG.get("download_jobs_enabled", DOWNLOAD_JOBS_ENABLED)) -DOWNLOAD_JOB_CHUNK_SIZE = int(BIGQMT_REDIS_CONFIG.get("download_job_chunk_size", DOWNLOAD_JOB_CHUNK_SIZE)) -DOWNLOAD_JOB_MAX_WALL_SECONDS = float( - BIGQMT_REDIS_CONFIG.get("download_job_max_wall_seconds", DOWNLOAD_JOB_MAX_WALL_SECONDS) -) -DOWNLOAD_JOB_TTL_SECONDS = int(BIGQMT_REDIS_CONFIG.get("download_job_ttl_seconds", DOWNLOAD_JOB_TTL_SECONDS)) -EXEC_EVENTS_ENABLED = bool(BIGQMT_REDIS_CONFIG.get("exec_events_enabled", EXEC_EVENTS_ENABLED)) -EXEC_EVENTS_DEBUG_RAW_FIELDS = bool( - BIGQMT_REDIS_CONFIG.get("exec_events_debug_raw_fields", EXEC_EVENTS_DEBUG_RAW_FIELDS) -) - - -def _apply_config(account_id): - account_id = str(account_id or "") - if account_id: - set_account_id(account_id) - configure( - mode="bigqmt", - account_id=account_id, - position_sync_type="redis" if RPC_TRANSPORT in ("redis", "", "default") else "", - enable_rpc=True, - schedule_adjust=SCHEDULE_ADJUST_ENABLED, - schedule_adjust_interval=SCHEDULE_ADJUST_INTERVAL, - redis={ - "host": REDIS_HOST, - "port": REDIS_PORT, - "db": REDIS_DB, - "username": REDIS_USERNAME, - "password": REDIS_PASSWORD, - "position_key_template": "bigqmt:positions:{account_id}", - "position_event_stream_template": "bigqmt:position_events:{account_id}", - }, - rpc={ - "enabled": True, - "account_id": account_id, - "allow_order_methods": RPC_ALLOW_ORDER_METHODS, - "request_channel_template": "bigqmt:rpc:req:{account_id}", - "response_channel_template": "bigqmt:rpc:resp:{account_id}:{request_id}", - "response_key_template": "bigqmt:rpc:resp:{account_id}:{request_id}", - "response_ttl_seconds": 60, - "drain_max_items": 20, - "process_in_listener": RPC_PROCESS_IN_LISTENER, - "listener_methods": RPC_LISTENER_METHODS, - "background_threads": RPC_BACKGROUND_THREADS, - # Transport selection (default redis). Forwarded from the local - # config so the factory can pick zmq/mysql/shm. - "transport": RPC_TRANSPORT, - "zmq": RPC_ZMQ_CONFIG, - "mysql": RPC_MYSQL_CONFIG, - }, - full_tick_cache={ - "enabled": FULL_TICK_CACHE_ENABLED, - "account_id": account_id, - "demand_ttl_seconds": FULL_TICK_DEMAND_TTL_SECONDS, - "cache_ttl_seconds": FULL_TICK_CACHE_TTL_SECONDS, - "refresh_interval_seconds": FULL_TICK_REFRESH_INTERVAL_SECONDS, - "market_refresh_interval_seconds": FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS, - "refresh_max_wall_seconds": FULL_TICK_REFRESH_MAX_WALL_SECONDS, - "max_requests": FULL_TICK_MAX_REQUESTS, - }, - download_jobs={ - "enabled": DOWNLOAD_JOBS_ENABLED, - "account_id": account_id, - "chunk_size": DOWNLOAD_JOB_CHUNK_SIZE, - "max_wall_seconds": DOWNLOAD_JOB_MAX_WALL_SECONDS, - "job_ttl_seconds": DOWNLOAD_JOB_TTL_SECONDS, - }, - exec_events={ - "enabled": EXEC_EVENTS_ENABLED, - "account_id": account_id, - "debug_raw_fields": EXEC_EVENTS_DEBUG_RAW_FIELDS, - }, - ) - - -def configure_runtime_account(account_id): - _apply_config(account_id) - - -def configure_runtime_redis(redis_config): - global REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_USERNAME, REDIS_PASSWORD, RPC_ALLOW_ORDER_METHODS, RPC_PROCESS_IN_LISTENER, RPC_BACKGROUND_THREADS, RPC_LISTENER_METHODS, SCHEDULE_ADJUST_ENABLED, SCHEDULE_ADJUST_INTERVAL, FULL_TICK_CACHE_ENABLED, FULL_TICK_DEMAND_TTL_SECONDS, FULL_TICK_CACHE_TTL_SECONDS, FULL_TICK_REFRESH_INTERVAL_SECONDS, FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS, FULL_TICK_REFRESH_MAX_WALL_SECONDS, FULL_TICK_MAX_REQUESTS, RPC_TRANSPORT, RPC_ZMQ_CONFIG, RPC_MYSQL_CONFIG, DOWNLOAD_JOBS_ENABLED, DOWNLOAD_JOB_CHUNK_SIZE, DOWNLOAD_JOB_MAX_WALL_SECONDS, DOWNLOAD_JOB_TTL_SECONDS, EXEC_EVENTS_ENABLED, EXEC_EVENTS_DEBUG_RAW_FIELDS - redis_config = dict(redis_config or {}) - REDIS_HOST = redis_config.get("host", REDIS_HOST) - REDIS_PORT = int(redis_config.get("port", REDIS_PORT)) - REDIS_DB = int(redis_config.get("db", REDIS_DB)) - REDIS_USERNAME = redis_config.get("username", REDIS_USERNAME) - REDIS_PASSWORD = redis_config.get("password", REDIS_PASSWORD) - RPC_ALLOW_ORDER_METHODS = bool(redis_config.get("rpc_allow_order_methods", RPC_ALLOW_ORDER_METHODS)) - RPC_PROCESS_IN_LISTENER = bool( - redis_config.get("rpc_process_in_listener", RPC_PROCESS_IN_LISTENER and not RPC_ALLOW_ORDER_METHODS) - ) - RPC_BACKGROUND_THREADS = bool(redis_config.get("rpc_background_threads", RPC_BACKGROUND_THREADS)) - RPC_LISTENER_METHODS = tuple(redis_config.get("rpc_listener_methods", RPC_LISTENER_METHODS)) - RPC_TRANSPORT = str(redis_config.get("transport", RPC_TRANSPORT)).lower() - RPC_ZMQ_CONFIG = dict(redis_config.get("zmq", RPC_ZMQ_CONFIG)) - RPC_MYSQL_CONFIG = dict(redis_config.get("mysql", RPC_MYSQL_CONFIG)) - # schedule_adjust must stay ON for ALL transports — including zmq. - # run_time("adjust", interval) is what THROTTLES QMT's strategy callback: with - # it, adjust fires on the configured cadence (e.g. 500ms); WITHOUT it QMT calls - # adjust in a hot loop (~2500/s) that pegs the GIL and starves the zmq ROUTER - # background thread (RPC then times out entirely). It also sets the GIL-release - # rhythm the background transport threads rely on. So keep the original rule: - # honor an explicit value, default on, and force on when not background-threaded. - SCHEDULE_ADJUST_ENABLED = bool(redis_config.get("schedule_adjust", SCHEDULE_ADJUST_ENABLED)) - if not RPC_BACKGROUND_THREADS: - SCHEDULE_ADJUST_ENABLED = True - SCHEDULE_ADJUST_INTERVAL = str(redis_config.get("schedule_adjust_interval", SCHEDULE_ADJUST_INTERVAL)) - FULL_TICK_CACHE_ENABLED = bool(redis_config.get("full_tick_cache_enabled", FULL_TICK_CACHE_ENABLED)) - FULL_TICK_DEMAND_TTL_SECONDS = float( - redis_config.get("full_tick_demand_ttl_seconds", FULL_TICK_DEMAND_TTL_SECONDS) - ) - FULL_TICK_CACHE_TTL_SECONDS = float(redis_config.get("full_tick_cache_ttl_seconds", FULL_TICK_CACHE_TTL_SECONDS)) - FULL_TICK_REFRESH_INTERVAL_SECONDS = float( - redis_config.get("full_tick_refresh_interval_seconds", FULL_TICK_REFRESH_INTERVAL_SECONDS) - ) - FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS = float( - redis_config.get("full_tick_market_refresh_interval_seconds", FULL_TICK_MARKET_REFRESH_INTERVAL_SECONDS) - ) - FULL_TICK_REFRESH_MAX_WALL_SECONDS = float( - redis_config.get("full_tick_refresh_max_wall_seconds", FULL_TICK_REFRESH_MAX_WALL_SECONDS) - ) - FULL_TICK_MAX_REQUESTS = int(redis_config.get("full_tick_max_requests", FULL_TICK_MAX_REQUESTS)) - DOWNLOAD_JOBS_ENABLED = bool(redis_config.get("download_jobs_enabled", DOWNLOAD_JOBS_ENABLED)) - DOWNLOAD_JOB_CHUNK_SIZE = int(redis_config.get("download_job_chunk_size", DOWNLOAD_JOB_CHUNK_SIZE)) - DOWNLOAD_JOB_MAX_WALL_SECONDS = float( - redis_config.get("download_job_max_wall_seconds", DOWNLOAD_JOB_MAX_WALL_SECONDS) - ) - DOWNLOAD_JOB_TTL_SECONDS = int(redis_config.get("download_job_ttl_seconds", DOWNLOAD_JOB_TTL_SECONDS)) - EXEC_EVENTS_ENABLED = bool(redis_config.get("exec_events_enabled", EXEC_EVENTS_ENABLED)) - EXEC_EVENTS_DEBUG_RAW_FIELDS = bool( - redis_config.get("exec_events_debug_raw_fields", EXEC_EVENTS_DEBUG_RAW_FIELDS) - ) - _apply_config(ACCOUNT_ID) - - -def bind_runtime_api(passorder_func=None, cancel_func=None, get_trade_detail_data_func=None, - extra_funcs=None): - bind_qmt_api( - passorder_func=passorder_func, - cancel_func=cancel_func, - get_trade_detail_data_func=get_trade_detail_data_func, - extra_funcs=extra_funcs, - ) - - -_apply_config(ACCOUNT_ID) diff --git a/reference/xtquant_big_convert/src/bigqmt_signal_trader_strategy.py b/reference/xtquant_big_convert/src/bigqmt_signal_trader_strategy.py deleted file mode 100644 index 71f9009..0000000 --- a/reference/xtquant_big_convert/src/bigqmt_signal_trader_strategy.py +++ /dev/null @@ -1,1056 +0,0 @@ -# coding: utf-8 -"""ThinkTrader Big QMT strategy entry. - -Keep this entry file ASCII-only because QMT's strategy editor may save the -generated strategy file with a local code page while preserving this coding -header. Business logic stays in the importable package. -""" - -import datetime -import importlib as _importlib -import sys -import threading -import time - -# The DRYRUN entry reloads strategy/runtime/redis_rpc/redis_common but NOT the -# other package submodules. Without this, the "from adapter_factory import build_app" -# below re-binds the STALE cached module on every strategy re-run, so edits to -# adapter_factory never take effect until a full terminal restart. Force-reload it -# (only it -- reloading the adapter classes would break isinstance elsewhere) so a -# plain strategy re-run picks up build_app fixes. build_app imports the adapter -# classes lazily, so their identity is preserved. -_af_mod = sys.modules.get("bigqmt_signal_trader.adapter_factory") -if _af_mod is not None: - try: - _importlib.reload(_af_mod) - except Exception as _reload_err: - print("[bigqmt_signal_trader] reload adapter_factory failed: %s" % _reload_err) - -try: - _load_bridge_module = __bigqmt_load_local_module -except NameError: - _load_bridge_module = None - -if _load_bridge_module is not None: - _adapter_factory = _load_bridge_module("bigqmt_signal_trader.adapter_factory") - _runner = _load_bridge_module("bigqmt_signal_trader.runner") - _runtime_bigqmt = _load_bridge_module("bigqmt_signal_trader.runtime_bigqmt") - _default_build_app = _adapter_factory.build_app - forward_order_event = _runner.forward_order_event - forward_trade_event = _runner.forward_trade_event - init_app = _runner.init_app - _reset_runner_app = _runner.reset_app - sync_positions_app = _runner.sync_positions_app - tick_app = _runner.tick_app - BigQmtRuntimeAdapter = _runtime_bigqmt.BigQmtRuntimeAdapter -else: - from bigqmt_signal_trader.adapter_factory import build_app as _default_build_app - from bigqmt_signal_trader.runner import ( - forward_order_event, - forward_trade_event, - init_app, - reset_app as _reset_runner_app, - sync_positions_app, - tick_app, - ) - from bigqmt_signal_trader.runtime_bigqmt import BigQmtRuntimeAdapter - - -_app_factory = None -_account_id = "" -_config = {} -_qmt_api = {} -_adjust_logged = False -_rpc_service = None -_quote_subscription_service = None # (QuoteSubscriptionManager, QuotePushChannel) -_exec_event_redis_client = None # reused; building a new client per trade callback leaks -_scheduled_adjust = False -# Latency tuning / diagnostics (server side, in the Big QMT process). -# - switch interval: hand the GIL to the background RPC thread ~5x more often -# than the 5ms default so it is not starved as long during Python contention. -# - GIL probe: a heartbeat thread that measures how long the interpreter was -# unable to run it (i.e. the process was stalled), independent of any request. -_GIL_SWITCH_INTERVAL = 0.001 -_LATENCY_PROBE_ENABLED = False -_LATENCY_PROBE_THRESHOLD_MS = 50.0 -_latency_probe_started = False -_last_full_tick_refresh_at = 0.0 -_last_full_tick_market_refresh_at = 0.0 -# Observed adjust cadence, so a mis-scheduled run_time (e.g. clamped to bar -# cadence) is visible in the logs instead of silently costing latency. -_adjust_tick_stats = {"last_ts": 0.0, "count": 0, "window_start": 0.0, "sum": 0.0, "min": 0.0, "max": 0.0} - - -def set_app_factory(factory): - global _app_factory - _app_factory = factory - - -def set_account_id(account_id): - global _account_id - _account_id = str(account_id or "") - - -def configure(**kwargs): - _config.update(kwargs) - - -def bind_qmt_api(passorder_func=None, cancel_func=None, get_trade_detail_data_func=None, - extra_funcs=None): - if passorder_func is not None: - _qmt_api["passorder"] = passorder_func - if cancel_func is not None: - _qmt_api["cancel"] = cancel_func - if get_trade_detail_data_func is not None: - _qmt_api["get_trade_detail_data"] = get_trade_detail_data_func - # 捕获 QMT 运行时注入的额外全局函数(融资融券查询、IPO、期权持仓等)。 - # 这些函数和 passorder 一样由 Big QMT 进程在运行时注入到全局命名空间, - # 不在 _PyContextInfo.py 桩里,需在 DRYRUN 入口捕获后传入。 - if extra_funcs: - for name, func in extra_funcs.items(): - if func is not None: - _qmt_api[name] = func - - -def reset_app(): - global _adjust_logged, _rpc_service, _scheduled_adjust, _last_full_tick_refresh_at, _last_full_tick_market_refresh_at - global _quote_subscription_service, _exec_event_redis_client - _adjust_logged = False - _scheduled_adjust = False - _last_full_tick_refresh_at = 0.0 - _last_full_tick_market_refresh_at = 0.0 - _adjust_tick_stats.update({"last_ts": 0.0, "count": 0, "window_start": 0.0, "sum": 0.0, "min": 0.0, "max": 0.0}) - if _rpc_service is not None: - try: - _rpc_service.stop() - except Exception: - pass - _rpc_service = None - # Stop the quote-push channel + unsubscribe big-QMT whole-quote subs. Without - # this a strategy re-run leaks the PUB port (next run's start_publisher hits - # EADDRINUSE, silently dropping quotes forever) and leaves stale QMT - # subscriptions firing into dead manager objects. - if _quote_subscription_service is not None: - try: - manager, channel = _quote_subscription_service - for key in list(getattr(manager, "_combos", {}).keys()): - combo = manager._combos.get(key) - if combo is not None: - try: - manager._close_source(combo.handle) - except Exception: - pass - manager._combos.clear() - manager._sub_index.clear() - channel.stop() - except Exception: - pass - _quote_subscription_service = None - # Drop the reused exec-event redis client so the next run rebuilds it fresh. - _exec_event_redis_client = None - _reset_runner_app() - - -def _resolve_runtime_name(name): - if name in _qmt_api: - return _qmt_api[name] - if name in globals(): - return globals()[name] - try: - import builtins - return getattr(builtins, name) - except Exception: - return None - - -def _detect_account_id(context_info=None): - if _account_id: - return _account_id - try: - import importlib - import bigqmt_signal_trader_local_config as _local_config - - _local_config = importlib.reload(_local_config) - value = str( - getattr(_local_config, "BIGQMT_ACCOUNT_ID", "") - or (getattr(_local_config, "BIGQMT_REDIS_CONFIG", {}) or {}).get("account_id") - or "" - ) - if value: - return value - except Exception: - pass - for name in ("account", "account_id", "accountID"): - value = _resolve_runtime_name(name) - if value: - return str(value) - if context_info is not None: - for name in ("account", "account_id", "accountID", "m_strAccountID"): - value = getattr(context_info, name, None) - if value: - return str(value) - for name in ("get_account", "get_account_id", "getAccountID"): - func = getattr(context_info, name, None) - if callable(func): - try: - value = func() - except Exception: - value = None - if value: - return str(value) - return "" - - -# Official Big QMT runtime-injected global functions (like passorder) that we -# expose over RPC. These are not ContextInfo methods and not in the IDE stub; -# QMT injects them into the process global namespace at startup. We resolve -# them lazily so the module imports cleanly outside QMT (tests/dev). -_EXTRA_QMT_GLOBAL_FUNCS = ( - "get_history_trade_detail_data", # 历史成交明细 - "get_value_by_order_id", # 按 order_id 查委托详情 - "get_last_order_id", # 最近委托号 - "get_ipo_data", # 新股数据 - "get_new_purchase_limit", # 新股申购额度 - "get_assure_contract", # 融资标的(担保品)合约 - "get_enable_short_contract", # 融券标的合约 - "get_unclosed_compacts", # 未平仓合约(负债) - "get_closed_compacts", # 已平仓合约 - "get_debt_contract", # 负债合约 - "get_option_subject_position", # 期权标的持仓 - "get_comb_option", # 组合期权 - "get_hkt_exchange_rate", # 港股通汇率 - # download_history_data / download_history_data2 are global functions - # injected by QMT (not ContextInfo methods), same as passorder. They - # must be captured here so the adapter can call them. Issue #32. - "download_history_data", - "download_history_data2", -) - - -def _build_config(): - config = dict(_config) - if _account_id: - config["account_id"] = _account_id - qmt_api = dict(config.get("qmt_api") or {}) - for name in ("passorder", "cancel", "get_trade_detail_data"): - if qmt_api.get(name) is None: - qmt_api[name] = _resolve_runtime_name(name) - # 解析其余官方全局函数(存在则注入,不存在保持 None)。 - for name in _EXTRA_QMT_GLOBAL_FUNCS: - if qmt_api.get(name) is None: - qmt_api[name] = _resolve_runtime_name(name) - config["qmt_api"] = qmt_api - return config - - -def _build_app(context_info): - if _app_factory is not None: - return _app_factory(context_info) - return _default_build_app(context_info, _build_config()) - - -def _config_bool(value, default=False): - if value is None: - return default - if isinstance(value, bool): - return value - return str(value).strip().lower() in ("1", "true", "yes", "y", "on") - - -_REDIS_TRANSPORT_NAMES = ("redis", "", "default") - - -def _is_redis_transport(transport_name): - return str(transport_name or "redis").lower() in _REDIS_TRANSPORT_NAMES - - -def _resolve_background_threads(transport_name, configured): - """Decide whether the RPC service runs its own background receive threads. - - Redis honors the configured value because it has a blocking brpop path AND - an adjust-driven lpop drain. ZMQ and all other transports MUST run their - receiver threads — without the background router loop, requests are never - received (ZMQ's start_receiving(background_threads=False) only binds the - socket and does not poll it). - """ - normalized = str(transport_name or "redis").lower() - if _is_redis_transport(normalized): - return bool(configured) - return True - - -def _build_quote_subscription_service(context_info, config, transport_name, account_id, redis_client): - """Assemble the server-side whole-quote push service (manager + channel). - - Returns ``(manager, channel)`` or ``None`` when disabled. The channel publisher - is started in ``_start_rpc_service`` once the RPC service is up; the manager's - reaper is fed from ``_drain_rpc_service``. - """ - quote_config = dict(config.get("quote_push") or {}) - enabled = _config_bool(quote_config.get("enabled"), True) - if not enabled: - return None - if _load_bridge_module is not None: - _qsm = _load_bridge_module("bigqmt_signal_trader.quote_subscription_manager") - else: - from bigqmt_signal_trader import quote_subscription_manager as _qsm - import importlib - - _qsm = importlib.reload(_qsm) - heartbeat_timeout = float(quote_config.get("heartbeat_timeout_seconds", 30.0)) - zmq_bind_address = quote_config.get("zmq_bind_address") - return _qsm.build_quote_subscription_service( - context_info, - transport_name=transport_name, - account_id=account_id, - redis_client=redis_client, - zmq_bind_address=zmq_bind_address, - enabled=True, - heartbeat_timeout_seconds=heartbeat_timeout, - ) - - -def _build_rpc_service(context_info, app, config): - rpc_config = dict(config.get("rpc") or {}) - enabled = _config_bool(config.get("enable_rpc"), False) or _config_bool(rpc_config.get("enabled"), False) - if not enabled: - return None - transport_name = str(rpc_config.get("transport") or "redis").lower() - redis_transport = transport_name in ("redis", "", "default") - - import importlib - if _load_bridge_module is not None: - _market_bigqmt = _load_bridge_module("bigqmt_signal_trader.adapters.market_bigqmt") - _position_bigqmt = _load_bridge_module("bigqmt_signal_trader.adapters.position_bigqmt") - _redis_rpc = _load_bridge_module("bigqmt_signal_trader.redis_rpc") - BigQmtRpcHandlers = _redis_rpc.BigQmtRpcHandlers - RedisPubSubRpcService = _redis_rpc.RedisPubSubRpcService - else: - from bigqmt_signal_trader.adapters import market_bigqmt as _market_bigqmt - from bigqmt_signal_trader.adapters import position_bigqmt as _position_bigqmt - from bigqmt_signal_trader.redis_rpc import BigQmtRpcHandlers, RedisPubSubRpcService - - # QMT keeps strategy modules in the same process between editor reruns. - # Reload adapters here so synced local package fixes take effect immediately. - _market_bigqmt = importlib.reload(_market_bigqmt) - _position_bigqmt = importlib.reload(_position_bigqmt) - # Reload the lazily-imported helper modules too, so edits to them take effect on - # an editor rerun (QMT persists sys.modules across reruns; a plain lazy import - # would otherwise keep the stale cached version). - for _mod_name in ( - "bigqmt_signal_trader.full_tick_cache", - "bigqmt_signal_trader.download_jobs", - "bigqmt_signal_trader.exec_events", - ): - try: - importlib.reload(importlib.import_module(_mod_name)) - except Exception as _reload_err: - print("[bigqmt_rpc] reload %s failed: %s" % (_mod_name, _reload_err)) - BigQmtMarketDataProvider = _market_bigqmt.BigQmtMarketDataProvider - BigQmtPositionProvider = _position_bigqmt.BigQmtPositionProvider - - qmt_api = dict(config.get("qmt_api") or {}) - redis_client = None - response_redis_client = None - if redis_transport: - if _load_bridge_module is not None: - _redis_common = _load_bridge_module("bigqmt_signal_trader.adapters.redis_common") - else: - from bigqmt_signal_trader.adapters import redis_common as _redis_common - - _redis_common = importlib.reload(_redis_common) - redis_config = dict(config.get("redis") or {}) - redis_config.update(dict(rpc_config.get("redis") or {})) - listen_redis_config = dict(redis_config) - # Never use socket_timeout=None on the listen client: the same client also - # serves the adjust-thread LPOP drain, and a None timeout makes a hung - # (not refused) redis block the QMT main thread forever. brpop's own 1s - # command timeout is unaffected by a bounded socket timeout. - if listen_redis_config.get("socket_timeout") in (None, ""): - listen_redis_config["socket_timeout"] = 10 - redis_client = rpc_config.get("redis_client") or config.get("redis_client") or _redis_common.build_redis_client(listen_redis_config) - response_redis_client = ( - rpc_config.get("response_redis_client") - or config.get("response_redis_client") - or _redis_common.build_redis_client(redis_config) - ) - account_id = str(rpc_config.get("account_id") or config.get("account_id") or _account_id or "") - if not account_id: - print("[bigqmt_rpc] disabled: account_id is empty") - return None - allow_order_methods = _config_bool(rpc_config.get("allow_order_methods"), False) - global _quote_subscription_service - _quote_subscription_service = _build_quote_subscription_service( - context_info, config, transport_name, account_id, redis_client - ) - quote_manager = ( - _quote_subscription_service[0] if _quote_subscription_service is not None else None - ) - handlers = BigQmtRpcHandlers( - account_id=account_id, - market_data=BigQmtMarketDataProvider(context_info, qmt_api=qmt_api), - position_provider=BigQmtPositionProvider( - get_trade_detail_data_func=qmt_api.get("get_trade_detail_data"), - account_type=config.get("account_type", "STOCK"), - ), - order_gateway=getattr(app, "order_gateway", None), - position_sync_sink=getattr(app, "position_sync_sink", None), - allow_order_methods=allow_order_methods, - allowed_methods=rpc_config.get("allowed_methods"), - qmt_api=qmt_api, - # Async settlement keeps passorder off the adjust thread's critical - # path; set rpc_settle_orders_inline=True only for a runtime with no - # adjust drain to retry on. - settle_orders_inline=_config_bool(rpc_config.get("settle_orders_inline"), False), - order_settle_timeout_seconds=float(rpc_config.get("order_settle_timeout_seconds", 3.0)), - quote_subscription_manager=quote_manager, - ) - handlers.download_job_redis_client = response_redis_client or redis_client - handlers.download_job_chunk_size = int((config.get("download_jobs") or {}).get("chunk_size") or 10) - handlers.download_job_ttl_seconds = int((config.get("download_jobs") or {}).get("job_ttl_seconds") or 3600) - process_in_listener = _config_bool(rpc_config.get("process_in_listener"), True) - listener_methods = rpc_config.get("listener_methods") or ("*",) - configured_bg = _config_bool(rpc_config.get("background_threads"), False) - background_threads = _resolve_background_threads(transport_name, configured_bg) - if background_threads and not configured_bg: - print("[bigqmt_rpc] transport=%s -> background_threads auto-enabled" % transport_name) - # Build the transport. Redis is the default and reuses the existing clients/ - # templates (zero behavior change). zmq/mysql/shm go through the factory and - # bypass the Redis clients entirely. - transport = None - if transport_name not in ("redis", "", "default"): - if _load_bridge_module is not None: - build_transport = _load_bridge_module("bigqmt_signal_trader.transports.factory").build_transport - else: - from bigqmt_signal_trader.transports.factory import build_transport - - factory_config = dict(rpc_config) - factory_config["account_id"] = account_id - factory_config["print_prefix"] = "[bigqmt_rpc]" - transport = build_transport(transport_name, factory_config, account_id=account_id, print_prefix="[bigqmt_rpc]") - print( - "[bigqmt_rpc] transport=%s mode process_in_listener=%s listener_methods=%s allow_order_methods=%s background_threads=%s" - % (transport_name, process_in_listener, listener_methods, allow_order_methods, background_threads) - ) - return RedisPubSubRpcService( - redis_client=redis_client, - response_redis_client=response_redis_client, - handlers=handlers, - account_id=account_id, - request_channel_template=rpc_config.get("request_channel_template", "bigqmt:rpc:req:{account_id}"), - response_channel_template=rpc_config.get("response_channel_template", "bigqmt:rpc:resp:{account_id}:{request_id}"), - response_key_template=rpc_config.get("response_key_template", "bigqmt:rpc:resp:{account_id}:{request_id}"), - response_ttl_seconds=int(rpc_config.get("response_ttl_seconds", 60)), - max_queue_size=int(rpc_config.get("max_queue_size", 200)), - process_in_listener=process_in_listener, - listener_methods=listener_methods, - background_threads=background_threads, - debug_log_limit=int(rpc_config.get("debug_log_limit", 5)), - transport=transport, - ) - - -def _start_rpc_service(context_info, app, config): - global _rpc_service - if _rpc_service is not None: - return _rpc_service - _rpc_service = _build_rpc_service(context_info, app, config) - if _rpc_service is not None: - _rpc_service.start() - if _quote_subscription_service is not None: - try: - _quote_subscription_service[1].start_publisher() - print("[bigqmt_quote_push] publisher started transport=%s" - % str(dict(config.get("rpc") or {}).get("transport") or "redis")) - except Exception as exc: - _log_err("quote_push", "publisher start failed: %s" % exc) - return _rpc_service - - -def _drain_rpc_service(config): - if _rpc_service is None: - return 0 - rpc_config = dict(config.get("rpc") or {}) - max_items = int(rpc_config.get("drain_max_items", 20)) - processed = 0 - if hasattr(_rpc_service, "drain_request_queue"): - processed += _rpc_service.drain_request_queue(max_items=max_items) - processed += _rpc_service.drain_pending(max_items=max_items) - if _quote_subscription_service is not None: - try: - _quote_subscription_service[0].reap_expired() - except Exception as exc: - _log_err("quote_push", "reap failed: %s" % exc) - return processed - - -def _refresh_full_tick_cache(context_info, config): - global _last_full_tick_refresh_at, _last_full_tick_market_refresh_at - cache_config = dict(config.get("full_tick_cache") or {}) - if not _config_bool(cache_config.get("enabled"), True): - return 0 - account_id = str(cache_config.get("account_id") or config.get("account_id") or _account_id or "") - if not account_id: - return 0 - # Symbol-list demands are cheap and refresh on the fast interval; whole-market - # (SH/SZ/BJ/HK) demands are heavy and refresh on a slower cadence so a ~50k row - # snapshot is not pulled every fast tick. - symbol_interval = float(cache_config.get("refresh_interval_seconds") or 0.5) - market_interval = float(cache_config.get("market_refresh_interval_seconds") or 3.0) - max_wall = cache_config.get("refresh_max_wall_seconds") - max_wall = float(max_wall) if max_wall else None - now = time.time() - do_symbol = now - _last_full_tick_refresh_at >= symbol_interval - do_market = now - _last_full_tick_market_refresh_at >= market_interval - if not do_symbol and not do_market: - return 0 - redis_client = getattr(_rpc_service, "redis", None) - if redis_client is None: - redis_config = dict(config.get("redis") or {}) - if not redis_config: - return 0 - from bigqmt_signal_trader.adapters.redis_common import build_redis_client - - redis_client = build_redis_client(redis_config) - demand_ttl = float(cache_config.get("demand_ttl_seconds") or 10) - cache_ttl = float(cache_config.get("cache_ttl_seconds") or 10) - max_requests = int(cache_config.get("max_requests") or 8) - from bigqmt_signal_trader.full_tick_cache import refresh_full_tick_cache - - refreshed = 0 - # Symbol and market refreshes are throttled independently, so each advances its - # own timestamp and runs in its own try: a symbol-refresh error must not starve - # the market refresh nor leave it retrying every fast tick (unthrottled). - if do_symbol: - _last_full_tick_refresh_at = now - try: - refreshed += refresh_full_tick_cache( - redis_client, - context_info, - account_id, - demand_ttl_seconds=demand_ttl, - cache_ttl_seconds=cache_ttl, - max_requests=max_requests, - kind="symbol", - max_wall_seconds=max_wall, - ) - except Exception as exc: - _log_err("full_tick_cache", "symbol refresh failed: %s" % exc) - if do_market: - _last_full_tick_market_refresh_at = now - try: - refreshed += refresh_full_tick_cache( - redis_client, - context_info, - account_id, - demand_ttl_seconds=demand_ttl, - cache_ttl_seconds=cache_ttl, - max_requests=max_requests, - kind="market", - max_wall_seconds=max_wall, - ) - except Exception as exc: - _log_err("full_tick_cache", "market refresh failed: %s" % exc) - return refreshed - - -def _schedule_adjust_if_needed(context_info, config): - global _scheduled_adjust - if _scheduled_adjust: - return - if not _config_bool(config.get("schedule_adjust"), False): - return - interval = str(config.get("schedule_adjust_interval") or "3000nMilliSecond") - if not hasattr(context_info, "run_time"): - print( - "[bigqmt_signal_trader] WARNING: ContextInfo.run_time unavailable; RPC drain " - "falls back to bar cadence (requested interval=%s not applied)" % interval - ) - return - start_time = (datetime.datetime.now() + datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M:%S") - try: - context_info.run_time("adjust", interval, start_time) - _scheduled_adjust = True - print( - "[bigqmt_signal_trader] scheduled adjust interval=%s " - "(verify observed cadence in the 'adjust cadence' log line)" % interval - ) - except Exception as exc: - print( - "[bigqmt_signal_trader] WARNING: schedule adjust failed (%s); RPC drain falls back " - "to bar cadence, requested interval=%s not applied" % (exc, interval) - ) - - -# Where each adjust() call came from, and what tick_app actually costs. -# The question this answers: handlebar is documented as tick-driven in live -# trading, but the observed cadence is a flat ~50/10s -- exactly the run_time -# timer and nothing else. Splitting the counters shows whether handlebar fires -# at all once the historical replay ends, and the tick_app histogram shows what -# it would cost to let it drive the strategy body rather than just the drain. -_adjust_source_stats = {"handlebar": 0, "timer": 0, "window_start": 0.0} -# Bucket upper bounds in ms; the last bucket is everything above. -_TICK_APP_BUCKETS = (1, 5, 20, 50, 100, 250, 500, 1000, 2000) -_tick_app_hist = [0] * (len(_TICK_APP_BUCKETS) + 1) -_tick_app_max_ms = [0.0] - - -def _record_adjust_source(source): - """Count adjust() calls per trigger source, logged on the cadence window.""" - stats = _adjust_source_stats - now = time.time() - if stats["window_start"] <= 0: - stats["window_start"] = now - stats[source] = stats.get(source, 0) + 1 - if now - stats["window_start"] >= 10.0: - print( - "[adjust_source] handlebar=%d timer=%d over %.0fs" - % (stats["handlebar"], stats["timer"], now - stats["window_start"]) - ) - stats.update({"handlebar": 0, "timer": 0, "window_start": now}) - - -def _record_tick_app_ms(ms): - """Histogram of tick_app cost. - - tick_app is the expensive half of adjust() and is skipped while - is_last_bar() is False, so the replay-time cost (~0.2ms/call) says nothing - about what it costs at the live edge. Before letting ticks drive it we need - the real distribution, not just the >50ms outliers the phase logger prints. - """ - for index, bound in enumerate(_TICK_APP_BUCKETS): - if ms <= bound: - _tick_app_hist[index] += 1 - break - else: - _tick_app_hist[-1] += 1 - if ms > _tick_app_max_ms[0]: - _tick_app_max_ms[0] = ms - - -def _format_tick_app_hist(): - labels = [] - previous = 0 - for index, bound in enumerate(_TICK_APP_BUCKETS): - labels.append("%d-%dms=%d" % (previous, bound, _tick_app_hist[index])) - previous = bound - labels.append(">%dms=%d" % (_TICK_APP_BUCKETS[-1], _tick_app_hist[-1])) - return " ".join(labels) - - -def _record_adjust_tick(): - """Track and periodically log the real interval between adjust triggers.""" - stats = _adjust_tick_stats - now = time.time() - last = stats["last_ts"] - stats["last_ts"] = now - if last <= 0: - stats["window_start"] = now - return - delta = now - last - stats["count"] += 1 - stats["sum"] += delta - stats["min"] = delta if stats["min"] <= 0 else min(stats["min"], delta) - stats["max"] = max(stats["max"], delta) - if now - stats["window_start"] >= 10.0 and stats["count"] > 0: - avg = stats["sum"] / stats["count"] - print( - "[bigqmt_signal_trader] adjust cadence: ticks=%d avg=%.3fs min=%.3fs max=%.3fs over %.0fs" - % (stats["count"], avg, stats["min"], stats["max"], now - stats["window_start"]) - ) - if sum(_tick_app_hist) > 0: - print("[tick_app_hist] %s max=%.0fms" - % (_format_tick_app_hist(), _tick_app_max_ms[0])) - stats.update({"count": 0, "sum": 0.0, "min": 0.0, "max": 0.0, "window_start": now}) - - -def _gil_probe_loop(): - """Heartbeat: sleep 5ms in a loop and measure the ACTUAL elapsed time. sleep() - releases the GIL; if returning from it takes much longer than 5ms, the thread - was starved -- i.e. the interpreter (this whole process) was stalled holding - the GIL elsewhere. Summarize gaps over a 10s window so we can see how often / - how long the process freezes, independent of any RPC request.""" - step = 0.005 - threshold = _LATENCY_PROBE_THRESHOLD_MS / 1000.0 - window_start = time.time() - gaps = [] - while True: - t0 = time.time() - time.sleep(step) - gap = time.time() - t0 - step - if gap > threshold: - gaps.append(gap * 1000.0) - now = time.time() - if now - window_start >= 10.0: - if gaps: - gaps.sort() - print( - "[gil_probe] over %.0fs: %d stalls>%.0fms max=%.0fms p50=%.0fms total=%.0fms" - % (now - window_start, len(gaps), _LATENCY_PROBE_THRESHOLD_MS, - gaps[-1], gaps[len(gaps) // 2], sum(gaps)) - ) - else: - print("[gil_probe] over %.0fs: 0 stalls>%.0fms (clean)" % (now - window_start, _LATENCY_PROBE_THRESHOLD_MS)) - window_start = now - gaps = [] - - -def _start_latency_probe(): - global _latency_probe_started - if _latency_probe_started or not _LATENCY_PROBE_ENABLED: - return - _latency_probe_started = True - t = threading.Thread(target=_gil_probe_loop, name="bigqmt-gil-probe", daemon=True) - t.start() - print("[gil_probe] started (threshold=%.0fms)" % _LATENCY_PROBE_THRESHOLD_MS) - - -def _apply_gil_tuning(): - try: - sys.setswitchinterval(_GIL_SWITCH_INTERVAL) - print("[bigqmt_signal_trader] gil switch interval set to %.4fs" % _GIL_SWITCH_INTERVAL) - except Exception as exc: - print("[bigqmt_signal_trader] setswitchinterval failed: %s" % exc) - - -def init(ContextInfo): - detected_account_id = _detect_account_id(ContextInfo) - if detected_account_id and not _account_id: - set_account_id(detected_account_id) - if _account_id and hasattr(ContextInfo, "set_account"): - try: - ContextInfo.set_account(_account_id) - except Exception as exc: - _log_startup_error("set_account failed: %s" % exc) - _apply_gil_tuning() - _start_latency_probe() - config = _build_config() - runtime = BigQmtRuntimeAdapter(ContextInfo) - app = None - try: - app = init_app(runtime, _build_app) - except Exception as exc: - # A build failure (e.g. missing redis package) must not kill the strategy - # before the RPC service even starts — log it and continue without app. - _log_startup_error("init_app/build_app failed: %s" % exc) - try: - _start_rpc_service(ContextInfo, app, config) - except Exception as exc: - # e.g. zmq port conflict -> TransportError. Log it so the user sees why - # the RPC service didn't start instead of QMT silently exiting. - _log_startup_error("rpc service start failed: %s" % exc) - try: - _schedule_adjust_if_needed(ContextInfo, config) - except Exception as exc: - _log_startup_error("schedule adjust failed: %s" % exc) - print("[bigqmt_signal_trader] init ok") - - # 启动时自动诊断:检测服务状态 + 关键函数绑定,方便发现问题 - _diag_startup(ContextInfo, config) - return app - - -def _log_startup_error(message): - """Log a startup error to file AND the QMT panel; never raises.""" - try: - from bigqmt_signal_trader.logging_setup import get_logger - get_logger("init").error("%s", message) - except Exception: - pass - try: - print("[bigqmt_signal_trader] INIT ERROR: %s" % message) - except Exception: - pass - - -def _log_err(tag, message): - """Log a runtime error to the rotating file AND the QMT panel; never raises. - - Centralizes error visibility: the QMT output panel scrolls away, but the - log file (logs/bigqmt.log, kept 7 days) survives restarts and crashes. - """ - try: - from bigqmt_signal_trader.logging_setup import get_logger - get_logger(tag).error("%s", message) - except Exception: - pass - try: - print("[bigqmt_signal_trader] %s: %s" % (tag, message)) - except Exception: - pass - - -def _diag_bar_driver(context_info): - """Report what drives handlebar: the strategy's own symbol, period, and - whether any quote subscription exists. - - handlebar is documented as firing per incoming tick in live trading, but it - goes quiet here once the historical replay ends. This strategy never calls - subscribe_quote / subscribe_whole_quote / set_universe, so the leading - suspect is that nothing is feeding it ticks. Print what QMT actually has so - the next live session settles it instead of us guessing. - """ - fields = ( - ("stockcode", "品种"), - ("stock_code", "品种(alt)"), - ("period", "周期"), - ("do_back_test", "回测模式"), - ("start", "起始"), - ("end", "结束"), - ) - parts = [] - for name, label in fields: - try: - value = getattr(context_info, name, None) - if callable(value): - value = value() - if value not in (None, ""): - parts.append("%s=%s" % (label, value)) - except Exception: - continue - print("[bigqmt_diag] bar driver: %s" % (" ".join(parts) or "<无法读取>")) - - for name in ("subscribe_quote", "subscribe_whole_quote", "set_universe", "is_last_bar"): - print("[bigqmt_diag] %-22s %s" - % (name, "可用" if callable(getattr(context_info, name, None)) else "不可用")) - try: - print("[bigqmt_diag] is_last_bar() 当前值 %s" % context_info.is_last_bar()) - except Exception as exc: - print("[bigqmt_diag] is_last_bar() 调用失败 %s" % exc) - print("[bigqmt_diag] 本策略未订阅任何行情 -> handlebar 预计仅由历史回放驱动") - - -def _diag_startup(ContextInfo, config): - """Startup diagnostics: check service status and key function bindings. - - Prints a summary to the QMT log so users can quickly see if the RPC service - is up, which transport is active, and whether key QMT functions (passorder, - get_trade_detail_data) are bound. Helps diagnose "service won't start" issues. - """ - print("=" * 60) - print("[bigqmt_diag] startup diagnostics") - print("=" * 60) - - _diag_bar_driver(ContextInfo) - - # 1. RPC service status - rpc_config = dict(config.get("rpc") or {}) - transport = rpc_config.get("transport", "redis") - print("[bigqmt_diag] transport=%s" % transport) - if _rpc_service is not None: - print("[bigqmt_diag] rpc_service=running (type=%s)" % type(_rpc_service).__name__) - else: - print("[bigqmt_diag] rpc_service=NOT STARTED (check enable_rpc / errors above)") - - # 2. Key QMT function bindings - qmt_api = dict(config.get("qmt_api") or {}) - for name in ("passorder", "cancel", "get_trade_detail_data", "down_history_data"): - bound = qmt_api.get(name) is not None - print("[bigqmt_diag] %s bound=%s" % (name, bound)) - - # 3. Quick connectivity test (get_full_tick) - try: - tick = ContextInfo.get_full_tick(["000001.SZ"]) - if tick: - print("[bigqmt_diag] get_full_tick=OK (keys=%d)" % len(tick)) - else: - print("[bigqmt_diag] get_full_tick=EMPTY (market may be closed)") - except Exception as e: - print("[bigqmt_diag] get_full_tick=FAIL: %s" % str(e)[:60]) - - print("[bigqmt_diag] diagnostics complete") - print("=" * 60) - - -def _pump_download_jobs(context_info, config): - """Advance any queued async download job by a bounded slice on this thread.""" - job_config = dict(config.get("download_jobs") or {}) - if not _config_bool(job_config.get("enabled"), True): - return None - account_id = str(job_config.get("account_id") or config.get("account_id") or _account_id or "") - if not account_id: - return None - redis_client = getattr(_rpc_service, "redis", None) - if redis_client is None: - redis_config = dict(config.get("redis") or {}) - if not redis_config: - return None - from bigqmt_signal_trader.adapters.redis_common import build_redis_client - - redis_client = build_redis_client(redis_config) - market_data = getattr(getattr(_rpc_service, "handlers", None), "market_data", None) - if market_data is None: - from bigqmt_signal_trader.adapters.market_bigqmt import BigQmtMarketDataProvider - - market_data = BigQmtMarketDataProvider(context_info, qmt_api=dict(config.get("qmt_api") or {})) - try: - from bigqmt_signal_trader.download_jobs import pump_download_jobs - - return pump_download_jobs( - redis_client, - market_data, - account_id, - chunk_size=int(job_config.get("chunk_size") or 10), - max_wall_seconds=float(job_config.get("max_wall_seconds") or 0.5), - job_ttl_seconds=int(job_config.get("job_ttl_seconds") or 3600), - ) - except Exception as exc: - _log_err("download_jobs", "pump failed: %s" % exc) - return None - - -def _adjust_phase(name, fn, *args): - """Time one adjust phase; log only if it exceeds 50ms. Pinpoints which part of - the 500ms adjust cycle holds the GIL (the gil_probe shows the stall exists; - this shows WHERE). The finally-log never alters the call's result/exception. - - Guard with except: an exception here (e.g. redis outage inside the LPOP - drain) must NOT propagate into adjust/handlebar — QMT stops the strategy on - a callback raise, which is the 'auto-exit' users report. Log and continue. - """ - t0 = time.perf_counter() - try: - return fn(*args) - except Exception: - import traceback as _tb - try: - from bigqmt_signal_trader.logging_setup import get_logger - get_logger("adjust").error("adjust phase %s failed:\n%s", name, _tb.format_exc()) - except Exception: - pass - return None - finally: - ms = (time.perf_counter() - t0) * 1000.0 - if ms > 50.0: - print("[adjust_phase] %s %.0fms" % (name, ms)) - - -def adjust(ContextInfo, _source="timer"): - global _adjust_logged - _record_adjust_tick() - _record_adjust_source(_source) - config = _build_config() - _adjust_phase("drain", _drain_rpc_service, config) - _adjust_phase("full_tick", _refresh_full_tick_cache, ContextInfo, config) - _adjust_phase("download", _pump_download_jobs, ContextInfo, config) - try: - if hasattr(ContextInfo, "is_last_bar") and not ContextInfo.is_last_bar(): - return None - except Exception: - pass - if not _adjust_logged: - print("[bigqmt_signal_trader] adjust ok") - _adjust_logged = True - _tick_app_t0 = time.perf_counter() - try: - return _adjust_phase("tick_app", tick_app, ContextInfo, datetime.datetime.now()) - finally: - # Every call, not just the >50ms ones _adjust_phase prints: deciding - # whether ticks may drive this needs the whole distribution. - _record_tick_app_ms((time.perf_counter() - _tick_app_t0) * 1000.0) - - -def handlebar(ContextInfo): - """Standard Big QMT bar callback. - - Documented as tick-driven during live trading ("再在每个tick数据来后驱动运行 - 一次"), but the observed cadence is a flat ~50/10s -- the run_time timer - alone. Tagging the source tells us whether this ever fires once the - historical replay ends; the strategy subscribes to no quote, which is the - leading suspect. - """ - return adjust(ContextInfo, _source="handlebar") - - -def _exec_event_redis(config): - """Return a redis client for exec-event publishing, reusing one instance. - - Previously a new client was built per order/trade callback when the RPC - service had none (the zmq-transport case), leaking a connection pool per - event. Reuse one; build failure returns None so publishing just skips. - """ - global _exec_event_redis_client - existing = getattr(_rpc_service, "redis", None) if _rpc_service is not None else None - if existing is not None: - return existing - if _exec_event_redis_client is not None: - return _exec_event_redis_client - redis_config = dict(config.get("redis") or {}) - if not redis_config: - return None - try: - from bigqmt_signal_trader.adapters.redis_common import build_redis_client - - _exec_event_redis_client = build_redis_client(redis_config) - except Exception: - return None - return _exec_event_redis_client - - -def _publish_exec_event(kind, obj): - """Push a normalized order/trade event to Redis for real-time client callbacks.""" - config = _build_config() - event_config = dict(config.get("exec_events") or {}) - # Raw-field diagnostics run BEFORE every other check (and before the - # enabled/account_id early returns), because the point is to observe the - # object exactly as QMT handed it over — even when publishing is off. - raw_fields = None - if _config_bool(event_config.get("debug_raw_fields"), False): - try: - from bigqmt_signal_trader import exec_events - - print(exec_events.format_raw_snapshot(kind, obj)) - raw_fields = exec_events.raw_field_snapshot(obj) - except Exception as exc: - print("[bigqmt_exec_raw] snapshot %s failed: %s" % (kind, exc)) - if not _config_bool(event_config.get("enabled"), True): - return - account_id = str(event_config.get("account_id") or config.get("account_id") or _account_id or "") - if not account_id: - return - redis_client = _exec_event_redis(config) - if redis_client is None: - return - try: - from bigqmt_signal_trader import exec_events - - if kind == "trade": - event = exec_events.normalize_trade_event(obj, account_id) - if raw_fields: - event["raw_fields"] = raw_fields - exec_events.publish_trade_event(redis_client, account_id, event) - else: - event = exec_events.normalize_order_event(obj, account_id) - event = exec_events.enrich_order_identity(redis_client, account_id, event) - if raw_fields: - event["raw_fields"] = raw_fields - exec_events.publish_order_event(redis_client, account_id, event) - # 废单 (status=57 ENTRUST_STATUS_JUNK) 推送 order_error,让客户端 - # on_order_error 能感知下单被拒。 - try: - status = int(event.get("status") or 0) - except (TypeError, ValueError): - status = 0 - if status == 57: - err_event = exec_events.normalize_order_error_event(obj, account_id) - if raw_fields: - err_event["raw_fields"] = raw_fields - exec_events.publish_order_error_event(redis_client, account_id, err_event) - except Exception as exc: - _log_err("exec_events", "publish %s failed: %s" % (kind, exc)) - - -def order_callback(ContextInfo, orderInfo): - """Standard Big QMT order callback.""" - _publish_exec_event("order", orderInfo) - return forward_order_event(BigQmtRuntimeAdapter.to_order_event(orderInfo)) - - -def deal_callback(ContextInfo, dealInfo): - """Standard Big QMT deal callback.""" - _publish_exec_event("trade", dealInfo) - return forward_trade_event(BigQmtRuntimeAdapter.to_trade_event(dealInfo)) - - -def sync_positions(ContextInfo): - return sync_positions_app("manual") diff --git a/reference/xtquant_big_convert/src/xtquant/__init__.py b/reference/xtquant_big_convert/src/xtquant/__init__.py deleted file mode 100644 index ed76111..0000000 --- a/reference/xtquant_big_convert/src/xtquant/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Optional xtquant import shim backed by Big QMT Redis RPC. - -Put this package before the real xtquant package on PYTHONPATH only when the -caller intentionally wants Big QMT RPC compatibility. -""" - -from . import xtconstant, xtdata, xttrader, xttype - -__all__ = ["xtconstant", "xtdata", "xttrader", "xttype"] diff --git a/reference/xtquant_big_convert/src/xtquant/xtconstant.py b/reference/xtquant_big_convert/src/xtquant/xtconstant.py deleted file mode 100644 index 3144a53..0000000 --- a/reference/xtquant_big_convert/src/xtquant/xtconstant.py +++ /dev/null @@ -1,128 +0,0 @@ -"""MiniQMT-compatible constant definitions (xtconstant). - -Mirrors the native ``xtquant/xtconstant.py`` so code that does -``from xtquant.xtconstant import STOCK_BUY`` keeps working against the -Big QMT bridge. Values are defined once in -``bigqmt_signal_trader.xtquant_compat`` and re-exported here. -""" - -from bigqmt_signal_trader.xtquant_compat import ( - # 账号类型 - CREDIT_ACCOUNT, - FUTURE_ACCOUNT, - FUTURE_OPTION_ACCOUNT, - HUGANGTONG_ACCOUNT, - SECURITY_ACCOUNT, - SHENGANGTONG_ACCOUNT, - STOCK_OPTION_ACCOUNT, - # 委托类型 - 期货 - FUTURE_ARBITRAGE_CLOSE_HISTORY_FIRST, - FUTURE_ARBITRAGE_CLOSE_TODAY_FIRST, - FUTURE_ARBITRAGE_OPEN, - FUTURE_CLOSE, - FUTURE_CLOSE_LONG_HISTORY, - FUTURE_CLOSE_LONG_HISTORY_FIRST, - FUTURE_CLOSE_LONG_HISTORY_TODAY_THEN_OPEN_SHORT, - FUTURE_CLOSE_LONG_TODAY, - FUTURE_CLOSE_LONG_TODAY_FIRST, - FUTURE_CLOSE_LONG_TODAY_HISTORY_THEN_OPEN_SHORT, - FUTURE_CLOSE_SHORT_HISTORY, - FUTURE_CLOSE_SHORT_HISTORY_FIRST, - FUTURE_CLOSE_SHORT_HISTORY_TODAY_THEN_OPEN_LONG, - FUTURE_CLOSE_SHORT_TODAY, - FUTURE_CLOSE_SHORT_TODAY_FIRST, - FUTURE_CLOSE_SHORT_TODAY_HISTORY_THEN_OPEN_LONG, - FUTURE_OPEN, - FUTURE_OPEN_LONG, - FUTURE_OPEN_SHORT, - FUTURE_RENEW_LONG_CLOSE_HISTORY_FIRST, - FUTURE_RENEW_LONG_CLOSE_TODAY_FIRST, - FUTURE_RENEW_SHORT_CLOSE_HISTORY_FIRST, - FUTURE_RENEW_SHORT_CLOSE_TODAY_FIRST, - # 委托类型 - 股票 / 信用 - CREDIT_BUY, - CREDIT_BUY_SECU_REPAY, - CREDIT_BUY_SECU_REPAY_SPECIAL, - CREDIT_DIRECT_CASH_REPAY, - CREDIT_DIRECT_CASH_REPAY_SPECIAL, - CREDIT_DIRECT_SECU_REPAY, - CREDIT_DIRECT_SECU_REPAY_SPECIAL, - CREDIT_FIN_BUY, - CREDIT_FIN_BUY_SPECIAL, - CREDIT_SELL, - CREDIT_SELL_SECU_REPAY, - CREDIT_SELL_SECU_REPAY_SPECIAL, - CREDIT_SLO_SELL, - CREDIT_SLO_SELL_SPECIAL, - STOCK_BUY, - STOCK_SELL, - # 委托类型 - 股票期权 / 期货期权 - OPTION_FUTURE_OPTION_EXERCISE, - STOCK_OPTION_BUY_CLOSE, - STOCK_OPTION_BUY_OPEN, - STOCK_OPTION_CALL_EXERCISE, - STOCK_OPTION_COVERED_CLOSE, - STOCK_OPTION_COVERED_OPEN, - STOCK_OPTION_PUT_EXERCISE, - STOCK_OPTION_SECU_LOCK, - STOCK_OPTION_SECU_UNLOCK, - STOCK_OPTION_SELL_CLOSE, - STOCK_OPTION_SELL_OPEN, - # 报价类型(市价) - FIX_PRICE, - LATEST_PRICE, - MARKET_MINE_PRICE_FIRST, - MARKET_PEER_PRICE_FIRST, - MARKET_SH_CONVERT_5_CANCEL, - MARKET_SH_CONVERT_5_LIMIT, - MARKET_SZ_CONVERT_5_CANCEL, - MARKET_SZ_FULL_OR_CANCEL, - MARKET_SZ_INSTBUSI_RESTCANCEL, - # 市场代码 - SH_MARKET, - SZ_MARKET, - # 委托状态 - ORDER_CANCELED, - ORDER_JUNK, - ORDER_PARTSUCC_CANCEL, - ORDER_PART_CANCEL, - ORDER_PART_SUCC, - ORDER_REPORTED, - ORDER_REPORTED_CANCEL, - ORDER_SUCCEEDED, - ORDER_UNKNOWN, - ORDER_UNREPORTED, - ORDER_WAIT_REPORTING, - # 账号状态 - ACCOUNT_STATUS_ASSIS_FAIL, - ACCOUNT_STATUS_CLOSED, - ACCOUNT_STATUS_CORRECTING, - ACCOUNT_STATUS_DISABLEBYSYS, - ACCOUNT_STATUS_DISABLEBYUSER, - ACCOUNT_STATUS_FAIL, - ACCOUNT_STATUS_INITING, - ACCOUNT_STATUS_INVALID, - ACCOUNT_STATUS_OK, - ACCOUNT_STATUS_WAITING_LOGIN, - ACCOUNT_STATUSING, -) - -# 合法委托类型集合(对齐原生 ORDER_TYPE_SET) -ORDER_TYPE_SET = { - STOCK_BUY, - STOCK_SELL, - CREDIT_BUY, - CREDIT_SELL, - CREDIT_FIN_BUY, - CREDIT_SLO_SELL, - CREDIT_BUY_SECU_REPAY, - CREDIT_DIRECT_SECU_REPAY, - CREDIT_SELL_SECU_REPAY, - CREDIT_DIRECT_CASH_REPAY, - CREDIT_FIN_BUY_SPECIAL, - CREDIT_SLO_SELL_SPECIAL, - CREDIT_BUY_SECU_REPAY_SPECIAL, - CREDIT_DIRECT_SECU_REPAY_SPECIAL, - CREDIT_SELL_SECU_REPAY_SPECIAL, - CREDIT_DIRECT_CASH_REPAY_SPECIAL, -} diff --git a/reference/xtquant_big_convert/src/xtquant/xtdata.py b/reference/xtquant_big_convert/src/xtquant/xtdata.py deleted file mode 100644 index a73840d..0000000 --- a/reference/xtquant_big_convert/src/xtquant/xtdata.py +++ /dev/null @@ -1,165 +0,0 @@ -import bigqmt_signal_trader.xtquant_compat as _compat - - -def __getattr__(name): - return getattr(_compat.xtdata, name) - - -def get_full_tick(code_list): - return _compat.xtdata.get_full_tick(code_list) - - -def get_market_data(field_list=[], stock_list=[], period="1d", start_time="", end_time="", count=-1, dividend_type="none", fill_data=True): - return _compat.xtdata.get_market_data(field_list, stock_list, period, start_time, end_time, count, dividend_type, fill_data) - - -def get_market_data_ex(field_list=[], stock_list=[], period="1d", start_time="", end_time="", count=-1, dividend_type="none", fill_data=True): - return _compat.xtdata.get_market_data_ex(field_list, stock_list, period, start_time, end_time, count, dividend_type, fill_data) - - -def get_local_data(field_list=[], stock_list=[], period="1d", start_time="", end_time="", count=-1, dividend_type="none", fill_data=True, data_dir=None): - return _compat.xtdata.get_local_data(field_list, stock_list, period, start_time, end_time, count, dividend_type, fill_data, data_dir) - - -def get_instrument_detail(stock_code): - return _compat.xtdata.get_instrument_detail(stock_code) - - -def get_instrumentdetail(stock_code): - return _compat.xtdata.get_instrumentdetail(stock_code) - - -def get_instrument_type(stock_code, variety_list=None): - return _compat.xtdata.get_instrument_type(stock_code, variety_list) - - -def get_stock_list_in_sector(sector_name, real_timetag=-1): - return _compat.xtdata.get_stock_list_in_sector(sector_name, real_timetag=real_timetag) - - -def get_sector_list(): - return _compat.xtdata.get_sector_list() - - -def get_sector_info(sector_name=""): - return _compat.xtdata.get_sector_info(sector_name) - - -def subscribe_quote(stock_code, period="1d", start_time="", end_time="", count=0, callback=None): - return _compat.xtdata.subscribe_quote(stock_code, period, start_time, end_time, count, callback) - - -def subscribe_quote2(stock_code, period="1d", start_time="", end_time="", count=0, dividend_type=None, callback=None): - return _compat.xtdata.subscribe_quote2(stock_code, period, start_time, end_time, count, dividend_type, callback) - - -def subscribe_whole_quote(code_list, callback=None): - return _compat.xtdata.subscribe_whole_quote(code_list, callback=callback) - - -def unsubscribe_quote(seq): - return _compat.xtdata.unsubscribe_quote(seq) - - -def run(): - return _compat.xtdata.run() - - -def get_divid_factors(stock_code, start_time="", end_time=""): - return _compat.xtdata.get_divid_factors(stock_code, start_time, end_time) - - -def getDividFactors(*args, **kwargs): - return _compat.xtdata.get_divid_factors(*args, **kwargs) - - -def submit_download_history_data(stock_code, period, start_time="", end_time="", incrementally=None): - return _compat.xtdata.submit_download_history_data(stock_code, period, start_time, end_time, incrementally) - - -def submit_download_history_data2(stock_list, period, start_time="", end_time="", incrementally=None): - return _compat.xtdata.submit_download_history_data2(stock_list, period, start_time, end_time, incrementally) - - -def get_download_status(job_id): - return _compat.xtdata.get_download_status(job_id) - - -def wait_download(job_id, timeout=None, poll_interval=None, callback=None): - return _compat.xtdata.wait_download(job_id, timeout, poll_interval, callback) - - -def download_history_data(stock_code, period, start_time="", end_time="", incrementally=None): - return _compat.xtdata.download_history_data(stock_code, period, start_time, end_time, incrementally) - - -def download_history_data2(stock_list, period, start_time="", end_time="", callback=None, incrementally=None): - return _compat.xtdata.download_history_data2(stock_list, period, start_time, end_time, callback, incrementally) - - -def get_trading_dates(market, start_time="", end_time="", count=-1): - return _compat.xtdata.get_trading_dates(market, start_time, end_time, count) - - -def get_holidays(): - return _compat.xtdata.get_holidays() - - -def download_holiday_data(incrementally=True): - return _compat.xtdata.download_holiday_data(incrementally) - - -def get_ipo_info(start_time="", end_time=""): - return _compat.xtdata.get_ipo_info(start_time, end_time) - - -def get_etf_info(): - return _compat.xtdata.get_etf_info() - - -def download_etf_info(): - return _compat.xtdata.download_etf_info() - - -def get_option_list(undl_code, dedate, opttype="", isavailavle=False): - return _compat.xtdata.get_option_list(undl_code, dedate, opttype, isavailavle) - - -def get_his_option_list(undl_code, dedate): - return _compat.xtdata.get_his_option_list(undl_code, dedate) - - -def get_his_option_list_batch(undl_code, start_time="", end_time=""): - return _compat.xtdata.get_his_option_list_batch(undl_code, start_time, end_time) - - -def get_financial_data(stock_list, table_list=[], start_time="", end_time="", report_type="report_time"): - return _compat.xtdata.get_financial_data(stock_list, table_list, start_time, end_time, report_type) - - -def download_financial_data(stock_list, table_list=[], start_time="", end_time="", incrementally=None): - return _compat.xtdata.download_financial_data(stock_list, table_list, start_time, end_time, incrementally) - - -def download_financial_data2(stock_list, table_list=[], start_time="", end_time="", callback=None): - return _compat.xtdata.download_financial_data2(stock_list, table_list, start_time, end_time, callback) - - -def call_formula(formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param={}): - return _compat.xtdata.call_formula(formula_name, stock_code, period, start_time, end_time, count, dividend_type, extend_param) - - -def subscribe_formula(formula_name, stock_code, period, start_time="", end_time="", count=-1, dividend_type=None, extend_param={}, callback=None): - return _compat.xtdata.subscribe_formula(formula_name, stock_code, period, start_time, end_time, count, dividend_type, extend_param, callback) - - -def unsubscribe_formula(request_id): - return _compat.xtdata.unsubscribe_formula(request_id) - - -def get_formula_result(request_id, start_time="", end_time="", count=-1, timeout_second=-1): - return _compat.xtdata.get_formula_result(request_id, start_time, end_time, count, timeout_second) - - -def gen_factor_index(data_name, formula_name, vars, sector_list, start_time="", end_time="", period="1d", dividend_type="none"): - return _compat.xtdata.gen_factor_index(data_name, formula_name, vars, sector_list, start_time, end_time, period, dividend_type) diff --git a/reference/xtquant_big_convert/src/xtquant/xttrader.py b/reference/xtquant_big_convert/src/xtquant/xttrader.py deleted file mode 100644 index 128fde7..0000000 --- a/reference/xtquant_big_convert/src/xtquant/xttrader.py +++ /dev/null @@ -1,7 +0,0 @@ -from bigqmt_signal_trader.xtquant_compat import ( - BigQmtXtTrader, - XtQuantTrader, - XtQuantTraderCallback, -) - -__all__ = ["BigQmtXtTrader", "XtQuantTrader", "XtQuantTraderCallback"] diff --git a/reference/xtquant_big_convert/src/xtquant/xttype.py b/reference/xtquant_big_convert/src/xtquant/xttype.py deleted file mode 100644 index 8fbbed9..0000000 --- a/reference/xtquant_big_convert/src/xtquant/xttype.py +++ /dev/null @@ -1,3 +0,0 @@ -from bigqmt_signal_trader.xtquant_compat import StockAccount - -__all__ = ["StockAccount"] diff --git a/reference/xtquant_big_convert/test_all_apis.py b/reference/xtquant_big_convert/test_all_apis.py deleted file mode 100644 index e931ebc..0000000 --- a/reference/xtquant_big_convert/test_all_apis.py +++ /dev/null @@ -1,300 +0,0 @@ -# coding: utf-8 -"""Systematically test all RPC APIs and MiniQMT alias mapping (end-to-end validation). - -For each method: call it with sensible params, report ok/error, and VALIDATE the -result is actually correct (not just "call succeeded"). Catches silent failures -like: - - get_positions returns {} when the account HAS positions - - submit_order returns SUBMITTED but the order never entered the system - - query_orders returns [] because strategy_name didn't match - - client transport (redis) doesn't match server (zmq) → timeout - -Config is read from bigqmt_signal_trader_local_config (gitignored) or env vars; -no credentials are hard-coded here. Run from a dir where that config module -resolves, e.g.: - - PYTHONPATH="src;D:\\国金证券QMT交易端\\python" python test_all_apis.py - -or set BIGQMT_ACCOUNT_ID / BIGQMT_REDIS_HOST / BIGQMT_REDIS_PORT / -BIGQMT_REDIS_DB / BIGQMT_REDIS_PASSWORD env vars. -""" -import os -import sys -import time - -# Add src to path so bigqmt_signal_trader resolves when run from repo root. -sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")) - -import redis - -from bigqmt_signal_trader.redis_rpc import call_redis_rpc - - -def _load_account_and_redis(): - cfg = {} - try: - import bigqmt_signal_trader_local_config as _c # noqa - cfg = getattr(_c, "BIGQMT_REDIS_CONFIG", {}) or {} - account = getattr(_c, "BIGQMT_ACCOUNT_ID", None) or cfg.get("account_id") - except Exception: - account = None - account = account or os.environ.get("BIGQMT_ACCOUNT_ID", "") - redis_cfg = dict( - host=cfg.get("host") or os.environ.get("BIGQMT_REDIS_HOST", "127.0.0.1"), - port=int(cfg.get("port") or os.environ.get("BIGQMT_REDIS_PORT", 6379)), - db=int(cfg.get("db") or os.environ.get("BIGQMT_REDIS_DB", 5)), - password=cfg.get("password", os.environ.get("BIGQMT_REDIS_PASSWORD", "")), - socket_timeout=15, - ) - if not redis_cfg["password"]: - redis_cfg.pop("password") - return str(account), redis_cfg - - -ACCOUNT, REDIS = _load_account_and_redis() -# account_id placeholder filled in main() once ACCOUNT is confirmed. -_ACCT_PARAM = {"account_id": None} - -# (method, params, label) — params chosen to be valid during/after market hours -TESTS = [ - # --- 行情快照 --- - ("get_full_tick", {"codes": ["000001.SZ"]}, "tick"), - ("get_ticks", {"codes": ["000001.SZ"]}, "ticks-alias"), - # --- 合约/品种 --- - ("get_instrument", {"code": "000001.SZ"}, "instrument"), - ("get_instrument_detail", {"code": "000001.SZ"}, "instrument-alias"), - ("get_instrumentdetail", {"code": "000001.SZ"}, "instrument-alias2"), - ("get_instrument_type", {"code": "000001.SZ", "variety_list": ["stock", "fund"]}, "inst-type"), - # --- K线/历史 --- - ("get_market_data_ex", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 3}, "md-ex"), - ("get_market_data", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 3}, "md"), - ("get_local_data", {"field_list": ["close"], "stock_list": ["000001.SZ"], "period": "1d", "count": 3}, "local-data"), - # --- 板块 --- - ("get_sector_list", {}, "sector-list"), - ("get_stock_list_in_sector", {"sector_name": "沪深A股"}, "sector-stocks"), - # --- 交易日历 --- - ("get_trading_dates", {"market": "SH", "count": 3}, "trade-dates"), - ("get_holidays", {}, "holidays"), - ("get_markets", {}, "markets"), - ("get_market_last_trade_date", {"market": "SH"}, "last-trade-date"), - # --- 账户 --- - ("get_asset", {}, "asset"), - ("get_positions", {}, "positions"), - ("query_stock_asset", dict(_ACCT_PARAM), "asset-alias"), - ("query_stock_positions", dict(_ACCT_PARAM), "positions-alias"), - ("query_stock_position", dict(stock_code="000001.SZ", **_ACCT_PARAM), "position-single"), -] - - -def data_summary(data): - """One-line summary of returned data for readability.""" - if data is None: - return "None" - if isinstance(data, dict): - if not data: - return "{}" - if "__bigqmt_type__" in data: - return "[%s cols=%s records=%d]" % ( - data.get("__bigqmt_type__"), - data.get("columns"), - len(data.get("records") or []), - ) - keys = list(data.keys())[:3] - return "{%s%s: ...}(%d keys)" % (keys, "" if len(keys) < 3 else ", ...", len(data)) - if isinstance(data, list): - return "[list len=%d]" % len(data) - return repr(data)[:60] - - -def _is_empty(data): - return data is None or data == {} or data == [] or data == "" - - -def _call(r, method, params, timeout=12): - """Call and return (response, latency_ms, error_str).""" - t0 = time.time() - try: - resp = call_redis_rpc(r, ACCOUNT, method, params, timeout_seconds=timeout) - return resp, (time.time() - t0) * 1000, None - except Exception as e: - return None, (time.time() - t0) * 1000, str(e) - - -def main(): - if not ACCOUNT: - raise SystemExit("ACCOUNT is empty: set BIGQMT_ACCOUNT_ID or configure bigqmt_signal_trader_local_config") - # Fill the account_id into the account-query test params now that we know it. - for i, (method, params, label) in enumerate(TESTS): - if "account_id" in params and params["account_id"] is None: - params["account_id"] = ACCOUNT - - r = redis.Redis(**REDIS) - - print("=" * 90) - print("全量 API 测试 (account=%s) — 端到端验证" % ACCOUNT) - print("=" * 90) - - # === 端到端验证 0: 客户端/服务端 transport 一致性 === - print("\n--- 端到端验证: 客户端/服务端一致性 ---") - # 检测客户端配置里的 transport - client_transport = "redis" # 默认 - try: - import bigqmt_signal_trader_local_config as _c - client_transport = str(getattr(_c, "BIGQMT_REDIS_CONFIG", {}).get("transport", "redis")).lower() - except Exception: - pass - print("客户端配置 transport: %s" % client_transport) - - # 如果客户端是 zmq 但服务端不是, ping 会超时 - ping_resp, ping_ms, ping_err = _call(r, "ping", {}, timeout=8) - if ping_err: - print("❌ ping 失败: %s" % ping_err) - if "timeout" in ping_err.lower(): - print(" 可能原因: 客户端 transport 和服务端不匹配") - print(" - 客户端配置 transport=%s" % client_transport) - print(" - 如果服务端是 zmq, 客户端也要设 transport=zmq") - print(" - 如果服务端是 redis, 客户端保持 redis 即可") - return - print("✅ ping OK (%.0fms) — 客户端/服务端连通" % ping_ms) - - # === 端到端验证 2: 账户有持仓时 get_positions 必须返回非空 === - print("\n--- 端到端验证: 持仓查询 ---") - pos_resp, pos_ms, pos_err = _call(r, "get_positions", {}, timeout=12) - if pos_err: - print("❌ get_positions 失败: %s" % pos_err) - elif not pos_resp.get("ok"): - print("❌ get_positions 返回错误: %s" % pos_resp.get("error")) - else: - positions = pos_resp.get("data") or {} - if len(positions) > 0: - print("✅ get_positions OK (%.0fms) — 返回 %d 只持仓" % (pos_ms, len(positions))) - else: - print("⚠️ get_positions 返回空 — 账户可能真的没持仓, 或查询失败 (检查 QMT 上下文)") - - # === 端到端验证 3: query_orders 验证 (strategy_name 陷阱) === - print("\n--- 端到端验证: 委托查询 ---") - ord_resp, ord_ms, ord_err = _call(r, "query_orders", {}, timeout=12) - if ord_err: - print("❌ query_orders 失败: %s" % ord_err) - elif not ord_resp.get("ok"): - print("❌ query_orders 返回错误: %s" % ord_resp.get("error")) - else: - orders = ord_resp.get("data") or [] - if len(orders) > 0: - print("✅ query_orders OK (%.0fms) — 返回 %d 条委托" % (ord_ms, len(orders))) - else: - print("⚠️ query_orders 返回空 — 可能 strategy_name 不匹配 (默认应为 '' 返回全部)") - - # === 端到端验证 4: 买入/卖出后委托必须进系统 === - print("\n--- 端到端验证: 买入/卖出 (仅交易时段) ---") - # 用极低价格买入 (确保不成交), 然后查委托确认进了系统 - # 先拿一只股票的现价 - tick_resp, _, tick_err = _call(r, "get_full_tick", {"codes": ["600654.SH"]}, timeout=12) - if tick_err or not tick_resp.get("ok"): - print("⚠️ 跳过买入测试 (get_full_tick 失败: %s)" % (tick_err or tick_resp.get("error"))) - else: - d = (tick_resp.get("data") or {}).get("600654.SH", {}) - last_close = float(d.get("lastClose") or d.get("lastPrice") or 3.0) - buy_price = round(last_close * 0.8, 2) # 跌停价, 确保不成交 - print(" 用 600654.SH @%.2f 买入 100 股 (跌停价, 不成交)" % buy_price) - - # 下单前委托数 - ord_before, _, _ = _call(r, "query_orders", {}, timeout=12) - before_count = len((ord_before or {}).get("data") or []) if ord_before else 0 - - # 下单 - sub_resp, sub_ms, sub_err = _call(r, "submit_order", { - "stock_code": "600654.SH", "action": "BUY", "volume": 100, - "price": buy_price, "price_type": "LIMIT", "strategy_name": "rpc_test", - "signal_id": "e2e-test-%d" % int(time.time()), - }, timeout=15) - if sub_err: - print("❌ submit_order 失败: %s" % sub_err) - elif not sub_resp.get("ok"): - print("❌ submit_order 返回错误: %s" % sub_resp.get("error")) - else: - server_err = sub_resp.get("server_error") or "" - print("✅ submit_order OK (%.0fms)" % sub_ms) - if server_err: - print(" ⚠️ server_error: %s" % server_err) - - # 等 1s 让 QMT 处理, 然后查委托确认进了系统 - time.sleep(1) - ord_after, _, _ = _call(r, "query_orders", {}, timeout=12) - after_orders = (ord_after or {}).get("data") or [] if ord_after else [] - found = any( - str(o.get("stock_code") or "").upper() == "600654.SH" - and str(o.get("action") or "").upper() == "BUY" - and abs(float(o.get("price") or 0) - buy_price) < 0.01 - for o in after_orders - ) - if found: - print("✅ 委托已进系统 (query_orders 确认)") - # 尝试撤单 - oid = None - for o in after_orders: - if (str(o.get("stock_code") or "").upper() == "600654.SH" - and str(o.get("action") or "").upper() == "BUY" - and abs(float(o.get("price") or 0) - buy_price) < 0.01): - oid = str(o.get("order_sys_id") or "") - break - if oid: - cancel_resp, cancel_ms, cancel_err = _call(r, "cancel_order", { - "order_sys_id": oid, "market": "SH" - }, timeout=15) - if cancel_err: - print("⚠️ cancel_order 失败: %s" % cancel_err) - elif cancel_resp and cancel_resp.get("ok"): - print("✅ cancel_order OK (%.0fms) — 已撤单" % cancel_ms) - else: - print("⚠️ cancel_order 返回: %s" % (cancel_resp or {})) - else: - print("❌ 委托没进系统 — submit_order 成功但 query_orders 找不到") - print(" 这是静默失败 (passorder 被 QMT 拒绝但没报错)") - print(" 检查: 1) 价格是否超出范围 2) 账户权限 3) QMT 风控") - - # === 全量 API 测试 === - print("\n" + "=" * 90) - print("全量 API 测试") - print("=" * 90) - print("%-22s %-8s %-8s %s" % ("method", "ok", "ms", "data summary")) - print("-" * 90) - - results = {"ok": [], "ok_empty": [], "fail": [], "timeout": []} - for method, params, label in TESTS: - resp, dt, err = _call(r, method, params, timeout=12) - if err: - is_timeout = "timeout" in err.lower() - bucket = "timeout" if is_timeout else "fail" - results[bucket].append((method, err[:60])) - print("%-22s %-8s %6.0f %s" % (method, "TIMEOUT" if is_timeout else "ERROR", dt, err[:50])) - continue - ok = resp.get("ok") - data = resp.get("data") - error = resp.get("error", "") - server_err = resp.get("server_error", "") - empty = _is_empty(data) - if ok and not empty: - results["ok"].append(method) - status = "OK" - elif ok and empty: - results["ok_empty"].append(method) - status = "EMPTY" - else: - results["fail"].append((method, error)) - status = "FAIL" - summary = data_summary(data) if ok else error[:50] - if server_err: - summary += " [server_error: %s]" % server_err[:40] - print("%-22s %-8s %6.0f %s" % (method, status, dt, summary)) - - print("-" * 90) - print("\n=== 汇总 ===") - print("有数据 (OK): %d 个" % len(results["ok"])) - print("成功但空 (EMPTY): %d 个 %s" % (len(results["ok_empty"]), results["ok_empty"])) - print("失败 (FAIL): %d 个 %s" % (len(results["fail"]), [m for m, _ in results["fail"]])) - print("超时 (TIMEOUT): %d 个 %s" % (len(results["timeout"]), [m for m, _ in results["timeout"]])) - - -if __name__ == "__main__": - main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_engine.py b/reference/xtquant_big_convert/tests/bigqmt_backtest/test_engine.py deleted file mode 100644 index d49ddd1..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_engine.py +++ /dev/null @@ -1,286 +0,0 @@ -import csv -import json -import os -import sys -import tempfile -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_backtest.data_feed import CsvBarFeed -from bigqmt_backtest.engine import BacktestConfig, BacktestEngine - - -FIELDS = ( - "datetime", - "symbol", - "open", - "high", - "low", - "close", - "volume", - "prev_close", -) - - -def _write_bars(path, rows): - with open(path, "w", encoding="utf-8", newline="") as handle: - writer = csv.DictWriter(handle, fieldnames=FIELDS) - writer.writeheader() - writer.writerows(rows) - - -def _rows(): - return [ - { - "datetime": "2026-01-05 09:30:00", - "symbol": "600000.SH", - "open": 10.00, - "high": 10.10, - "low": 9.95, - "close": 10.05, - "volume": 100000, - "prev_close": 9.90, - }, - { - "datetime": "2026-01-05 09:31:00", - "symbol": "600000.SH", - "open": 10.10, - "high": 10.20, - "low": 10.00, - "close": 10.15, - "volume": 100000, - "prev_close": 9.90, - }, - { - "datetime": "2026-01-05 09:32:00", - "symbol": "600000.SH", - "open": 10.20, - "high": 10.25, - "low": 10.10, - "close": 10.18, - "volume": 100000, - "prev_close": 9.90, - }, - { - "datetime": "2026-01-06 09:30:00", - "symbol": "600000.SH", - "open": 10.30, - "high": 10.35, - "low": 10.20, - "close": 10.25, - "volume": 100000, - "prev_close": 10.18, - }, - { - "datetime": "2026-01-06 09:31:00", - "symbol": "600000.SH", - "open": 10.25, - "high": 10.30, - "low": 10.15, - "close": 10.20, - "volume": 100000, - "prev_close": 10.18, - }, - ] - - -def _config(run_id, output_dir): - return BacktestConfig( - run_id=run_id, - output_dir=output_dir, - initial_cash=100000, - buy_commission_rate=0.0003, - sell_commission_rate=0.0003, - min_commission=5, - stamp_tax_rate=0.0005, - transfer_fee_rate=0.00001, - max_volume_participation=1.0, - slippage_bps=0, - ) - - -class CsvBarFeedTest(unittest.TestCase): - def test_loads_chronologically_and_never_exposes_future_rows(self): - with tempfile.TemporaryDirectory() as tmp: - path = os.path.join(tmp, "bars.csv") - rows = list(reversed(_rows())) - _write_bars(path, rows) - - feed = CsvBarFeed(path) - - self.assertEqual(len(feed), 5) - self.assertEqual(feed.frame(0)["datetime"], "2026-01-05 09:30:00") - self.assertEqual( - list(feed.history("600000.SH", end_index=1, count=10, fields=["close"]))[-1]["close"], - 10.15, - ) - self.assertNotIn(10.18, [item["close"] for item in feed.history("600000.SH", 1, 10)]) - self.assertEqual(len(feed.data_hash), 64) - - def test_duplicate_symbol_timestamp_is_rejected(self): - with tempfile.TemporaryDirectory() as tmp: - path = os.path.join(tmp, "bars.csv") - rows = _rows() - _write_bars(path, rows + [dict(rows[0])]) - - with self.assertRaisesRegex(ValueError, "duplicate bar"): - CsvBarFeed(path) - - -class BacktestEngineTest(unittest.TestCase): - def _engine(self, tmp, run_id="run-a"): - path = os.path.join(tmp, "bars.csv") - _write_bars(path, _rows()) - return BacktestEngine(CsvBarFeed(path), _config(run_id, os.path.join(tmp, run_id))) - - def test_order_is_filled_at_next_bar_open_not_current_close(self): - with tempfile.TemporaryDirectory() as tmp: - engine = self._engine(tmp) - started = engine.start() - order = engine.submit_order( - {"symbol": "600000.SH", "side": "BUY", "quantity": 100, "order_type": "MARKET"} - ) - - self.assertEqual(started["frame_index"], 0) - self.assertEqual(order["status"], "PENDING") - self.assertEqual(engine.state()["positions"], {}) - - advanced = engine.next_bar() - - self.assertEqual(advanced["frame_index"], 1) - self.assertEqual(advanced["fills"][0]["price"], 10.10) - self.assertEqual(advanced["fills"][0]["commission"], 5.0) - self.assertEqual(advanced["fills"][0]["transfer_fee"], 0.01) - self.assertEqual(advanced["cash"], 98984.99) - self.assertEqual(advanced["positions"]["600000.SH"]["quantity"], 100) - self.assertEqual(advanced["positions"]["600000.SH"]["available"], 0) - - def test_t_plus_one_rejects_same_day_sell_and_allows_next_day(self): - with tempfile.TemporaryDirectory() as tmp: - engine = self._engine(tmp) - engine.start() - engine.submit_order( - {"symbol": "600000.SH", "side": "BUY", "quantity": 100, "order_type": "MARKET"} - ) - engine.next_bar() - - rejected = engine.submit_order( - {"symbol": "600000.SH", "side": "SELL", "quantity": 100, "order_type": "MARKET"} - ) - self.assertEqual(rejected["status"], "REJECTED") - self.assertEqual(rejected["reject_reason"], "t_plus_one_unavailable") - - engine.next_bar() - next_day = engine.next_bar() - self.assertEqual(next_day["positions"]["600000.SH"]["available"], 100) - accepted = engine.submit_order( - {"symbol": "600000.SH", "side": "SELL", "quantity": 100, "order_type": "MARKET"} - ) - self.assertEqual(accepted["status"], "PENDING") - filled = engine.next_bar() - self.assertEqual(filled["fills"][0]["side"], "SELL") - self.assertEqual(filled["fills"][0]["stamp_tax"], 0.51) - self.assertEqual(filled["total_fees"], 10.53) - self.assertEqual(filled["cash"], 100004.47) - self.assertEqual(filled["positions"], {}) - - def test_limit_locked_bar_does_not_assume_fill(self): - with tempfile.TemporaryDirectory() as tmp: - path = os.path.join(tmp, "locked.csv") - rows = _rows()[:2] - rows[1].update({"open": 10.89, "high": 10.89, "low": 10.89, "close": 10.89, "prev_close": 9.90}) - _write_bars(path, rows) - engine = BacktestEngine(CsvBarFeed(path), _config("locked", os.path.join(tmp, "locked"))) - engine.start() - engine.submit_order( - {"symbol": "600000.SH", "side": "BUY", "quantity": 100, "order_type": "MARKET"} - ) - - state = engine.next_bar() - - self.assertEqual(state["fills"], []) - self.assertEqual(engine.orders()[0]["status"], "EXPIRED") - self.assertEqual(engine.orders()[0]["reject_reason"], "limit_up_locked") - - def test_client_order_id_is_idempotent_but_cannot_change_payload(self): - with tempfile.TemporaryDirectory() as tmp: - engine = self._engine(tmp) - engine.start() - payload = { - "client_order_id": "stable-1", - "symbol": "600000.SH", - "side": "BUY", - "quantity": 100, - "order_type": "MARKET", - } - first = engine.submit_order(payload) - repeated = engine.submit_order(dict(payload)) - - self.assertEqual(first["order_id"], repeated["order_id"]) - self.assertEqual(len(engine.orders()), 1) - changed = dict(payload, quantity=200) - with self.assertRaisesRegex(ValueError, "different order payload"): - engine.submit_order(changed) - - def test_volume_participation_is_shared_across_orders_on_the_same_bar(self): - with tempfile.TemporaryDirectory() as tmp: - path = os.path.join(tmp, "volume.csv") - rows = _rows()[:2] - rows[1]["volume"] = 1000 - _write_bars(path, rows) - config = _config("volume", os.path.join(tmp, "volume")) - config.max_volume_participation = 0.1 - engine = BacktestEngine(CsvBarFeed(path), config) - engine.start() - for order_id in ("first", "second"): - engine.submit_order( - { - "client_order_id": order_id, - "symbol": "600000.SH", - "side": "BUY", - "quantity": 100, - "order_type": "MARKET", - } - ) - - advanced = engine.next_bar() - - self.assertEqual(sum(fill["quantity"] for fill in advanced["fills"]), 100) - self.assertEqual(engine.orders()[1]["status"], "EXPIRED") - self.assertEqual(engine.orders()[1]["reject_reason"], "volume_participation_exhausted") - - def test_finish_writes_evidence_and_is_deterministic(self): - signatures = [] - with tempfile.TemporaryDirectory() as tmp: - for run_id in ("det-a", "det-b"): - engine = self._engine(tmp, run_id=run_id) - engine.start() - engine.submit_order( - { - "client_order_id": "buy-1", - "symbol": "600000.SH", - "side": "BUY", - "quantity": 100, - "order_type": "MARKET", - } - ) - while not engine.next_bar()["done"]: - pass - result = engine.finish() - signatures.append(result["deterministic_signature"]) - output_dir = os.path.join(tmp, run_id) - for name in ("meta.json", "result.json", "orders.csv", "fills.csv", "equity.csv", "positions.csv"): - self.assertTrue(os.path.isfile(os.path.join(output_dir, name)), name) - with open(os.path.join(output_dir, "meta.json"), encoding="utf-8") as handle: - meta = json.load(handle) - self.assertEqual(meta["data_hash"], engine.feed.data_hash) - self.assertFalse(meta["live_ready"]) - - self.assertEqual(signatures[0], signatures[1]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_qmt_runtime.py b/reference/xtquant_big_convert/tests/bigqmt_backtest/test_qmt_runtime.py deleted file mode 100644 index 6095c7c..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_qmt_runtime.py +++ /dev/null @@ -1,162 +0,0 @@ -import datetime as dt -import os -import sys -import tempfile -import threading -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -SRC = os.path.join(ROOT, "src") -sys.path.insert(0, SRC) - -from bigqmt_backtest.data_feed import StreamingBarFeed -from bigqmt_backtest.engine import BacktestConfig, StreamingBacktestEngine -from bigqmt_backtest.qmt_runtime import QmtBarExtractor, QmtNativeBacktestSession - - -class FakeQmtContext(object): - stockcode = "600000" - market = "SH" - period = "1m" - barpos = 0 - do_back_test = True - - values = { - "open": [10.0], - "high": [10.2], - "low": [9.9], - "close": [10.1], - "volume": [10000], - "amount": [101000], - "preClose": [9.8], - } - - def get_bar_timetag(self, barpos): - value = dt.datetime(2026, 1, 5, 9, 30) - return int(value.timestamp() * 1000) - - def get_history_data(self, count, period, field): - return {"600000.SH": self.values.get(field, [])} - - def set_account(self, account_id): - self.account_id = account_id - - -class QmtBarExtractorTest(unittest.TestCase): - def test_extracts_qmt_bar_without_live_account_or_order_api(self): - row = QmtBarExtractor().extract(FakeQmtContext()) - - self.assertEqual(row["symbol"], "600000.SH") - self.assertEqual(row["datetime"], "2026-01-05 09:30:00") - self.assertEqual(row["open"], 10.0) - self.assertEqual(row["prev_close"], 9.8) - - def test_qmt_entry_is_isolated_and_binds_native_order_callbacks(self): - path = os.path.join(SRC, "BIGQMT_ZMQ_BACKTEST.py") - with open(path, "r", encoding="gbk") as handle: - source = handle.read() - - self.assertNotIn("bigqmt_signal_trader", source) - self.assertIn("passorder", source) - self.assertIn("get_trade_detail_data", source) - self.assertIn("order_callback", source) - self.assertIn("deal_callback", source) - self.assertNotIn("_importlib.import_module =", source) - - runtime_path = os.path.join(SRC, "bigqmt_backtest", "qmt_runtime.py") - with open(runtime_path, "r", encoding="utf-8") as handle: - runtime_source = handle.read() - self.assertNotIn("StreamingBacktestEngine", runtime_source) - self.assertNotIn("SimulatedBroker", runtime_source) - - def test_native_session_executes_passorder_on_qmt_callback_thread(self): - calls = [] - - def fake_passorder(*args): - calls.append((threading.get_ident(), args)) - return "qmt-order-1" - - session = QmtNativeBacktestSession( - config={ - "run_id": "qmt-native-test", - "account_id": "test-account", - "bar_wait_timeout_seconds": 1, - }, - qmt_api={"passorder": fake_passorder}, - ) - qmt_thread = threading.Thread(target=session.on_bar, args=(FakeQmtContext(),)) - qmt_thread.start() - - state = session.start() - queued = session.submit_order({ - "symbol": "600000.SH", - "side": "BUY", - "quantity": 100, - "order_type": "MARKET", - "client_order_id": "external-1", - }) - session.finish() - qmt_thread.join(timeout=2) - - self.assertFalse(qmt_thread.is_alive()) - self.assertEqual(state["execution_backend"], "QMT_NATIVE") - self.assertEqual(queued["status"], "QUEUED") - self.assertEqual(len(calls), 1) - self.assertEqual(calls[0][0], qmt_thread.ident) - self.assertEqual(calls[0][1][0], 23) - self.assertEqual(calls[0][1][2], "test-account") - self.assertEqual(calls[0][1][-2], "external-1") - self.assertEqual(session.orders()[0]["status"], "SUBMITTED") - - def test_native_session_rejects_non_backtest_qmt_context(self): - context = FakeQmtContext() - context.do_back_test = False - session = QmtNativeBacktestSession(config={"run_id": "guard"}) - - with self.assertRaisesRegex(RuntimeError, "outside QMT backtest mode"): - session.bind_context(context) - - -class StreamingEngineTest(unittest.TestCase): - def test_stream_does_not_report_done_until_qmt_closes_feed(self): - with tempfile.TemporaryDirectory() as tmp: - feed = StreamingBarFeed() - feed.append( - { - "datetime": "2026-01-05 09:30:00", - "symbol": "600000.SH", - "open": 10, - "high": 10.1, - "low": 9.9, - "close": 10, - "volume": 10000, - "prev_close": 9.9, - } - ) - engine = StreamingBacktestEngine( - feed, - BacktestConfig(run_id="stream", output_dir=os.path.join(tmp, "out")), - bar_wait_timeout_seconds=0.1, - ) - - self.assertFalse(engine.start()["done"]) - feed.append( - { - "datetime": "2026-01-05 09:31:00", - "symbol": "600000.SH", - "open": 10.1, - "high": 10.2, - "low": 10, - "close": 10.15, - "volume": 10000, - "prev_close": 9.9, - } - ) - self.assertFalse(engine.next_bar()["done"]) - feed.close() - self.assertTrue(engine.state()["done"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_zmq_bridge.py b/reference/xtquant_big_convert/tests/bigqmt_backtest/test_zmq_bridge.py deleted file mode 100644 index bfffd0f..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_backtest/test_zmq_bridge.py +++ /dev/null @@ -1,250 +0,0 @@ -import csv -import os -import socket -import sys -import tempfile -import threading -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_backtest.client import BacktestZmqClient -from bigqmt_backtest.data_feed import CsvBarFeed -from bigqmt_backtest.engine import BacktestConfig, BacktestEngine -from bigqmt_backtest.protocol import BacktestBridgeProtocol -from bigqmt_backtest.qmt_runtime import QmtNativeBacktestSession -from bigqmt_backtest.zmq_server import ZmqBacktestServer - - -def _free_port(): - sock = socket.socket() - sock.bind(("127.0.0.1", 0)) - port = sock.getsockname()[1] - sock.close() - return port - - -def _feed(tmp): - path = os.path.join(tmp, "bars.csv") - with open(path, "w", encoding="utf-8", newline="") as handle: - writer = csv.DictWriter( - handle, - fieldnames=("datetime", "symbol", "open", "high", "low", "close", "volume", "prev_close"), - ) - writer.writeheader() - writer.writerows( - [ - { - "datetime": "2026-01-05 09:30:00", - "symbol": "000001.SZ", - "open": 10, - "high": 10.1, - "low": 9.9, - "close": 10, - "volume": 10000, - "prev_close": 9.9, - }, - { - "datetime": "2026-01-05 09:31:00", - "symbol": "000001.SZ", - "open": 10.1, - "high": 10.2, - "low": 10, - "close": 10.15, - "volume": 10000, - "prev_close": 9.9, - }, - ] - ) - return CsvBarFeed(path) - - -class BacktestProtocolTest(unittest.TestCase): - def test_run_and_client_identity_are_enforced_and_requests_are_idempotent(self): - with tempfile.TemporaryDirectory() as tmp: - engine = BacktestEngine( - _feed(tmp), - BacktestConfig(run_id="identity", output_dir=os.path.join(tmp, "out")), - ) - protocol = BacktestBridgeProtocol(engine) - start = { - "schema_version": 1, - "request_id": "req-start", - "run_id": "identity", - "client_id": "client-a", - "method": "start", - "params": {}, - } - first = protocol.handle(start) - repeated = protocol.handle(dict(start)) - - self.assertTrue(first["ok"]) - self.assertEqual(first, repeated) - - reused = dict(start, method="state") - reused_response = protocol.handle(reused) - self.assertFalse(reused_response["ok"]) - self.assertIn("different payload", reused_response["error"]) - - wrong_run = dict(start, request_id="wrong-run", run_id="other", method="state") - self.assertFalse(protocol.handle(wrong_run)["ok"]) - wrong_client = dict(start, request_id="wrong-client", client_id="client-b", method="next_bar") - self.assertFalse(protocol.handle(wrong_client)["ok"]) - - -class ZmqRoundTripTest(unittest.TestCase): - def test_external_client_can_complete_a_backtest_over_zmq(self): - with tempfile.TemporaryDirectory() as tmp: - engine = BacktestEngine( - _feed(tmp), - BacktestConfig( - run_id="zmq-run", - output_dir=os.path.join(tmp, "out"), - max_volume_participation=1.0, - ), - ) - endpoint = "tcp://127.0.0.1:%d" % _free_port() - server = ZmqBacktestServer( - BacktestBridgeProtocol(engine), endpoint=endpoint, exit_on_finish=True - ) - thread = threading.Thread(target=server.serve_forever, daemon=True) - thread.start() - self.assertTrue(server.wait_until_ready(3.0)) - - client = BacktestZmqClient( - endpoint=endpoint, - run_id="zmq-run", - client_id="external-strategy", - timeout_seconds=3.0, - ) - started = client.start() - order = client.submit_order( - symbol="000001.SZ", side="BUY", quantity=100, order_type="MARKET" - ) - advanced = client.next_bar() - history = client.history("000001.SZ", count=10, fields=["close"]) - result = client.finish() - client.close() - thread.join(timeout=3.0) - - self.assertEqual(started["frame_index"], 0) - self.assertEqual(order["status"], "PENDING") - self.assertEqual(advanced["fills"][0]["price"], 10.1) - self.assertEqual([row["close"] for row in history], [10.0, 10.15]) - self.assertEqual(result["run_id"], "zmq-run") - self.assertFalse(thread.is_alive()) - - def test_qmt_native_service_bridges_orders_to_qmt_matching(self): - endpoint = "tcp://127.0.0.1:%d" % _free_port() - calls = [] - holder = {} - - class Context(object): - do_back_test = True - stockcode = "600000" - market = "SH" - period = "1m" - - def __init__(self, barpos, close): - self.barpos = barpos - self.close_value = close - - def set_account(self, account_id): - self.account_id = account_id - - def get_bar_timetag(self, barpos): - return int((1704072600 + barpos * 60) * 1000) - - def get_history_data(self, count, period, field): - values = { - "open": self.close_value, - "high": self.close_value + 0.1, - "low": self.close_value - 0.1, - "close": self.close_value, - "volume": 10000, - "amount": self.close_value * 10000, - "preClose": self.close_value - 0.1, - } - return {"600000.SH": [values[field]]} - - def fake_passorder(*args): - calls.append((threading.get_ident(), args)) - session = holder["session"] - session.on_order({ - "m_strOrderSysID": "qmt-order-1", - "m_strRemark": args[-2], - "m_strInstrumentID": "600000", - "m_strExchangeID": "SH", - "m_nOffsetFlag": 48, - "m_nVolumeTotalOriginal": args[6], - "m_nVolumeTraded": args[6], - "m_nOrderStatus": "FILLED", - }) - session.on_trade({ - "m_strTradeID": "qmt-fill-1", - "m_strOrderSysID": "qmt-order-1", - "m_strRemark": args[-2], - "m_strInstrumentID": "600000", - "m_strExchangeID": "SH", - "m_nOffsetFlag": 48, - "m_nVolume": args[6], - "m_dPrice": 10.1, - "m_strTradeTime": "09:31:00", - }) - return "qmt-order-1" - - session = QmtNativeBacktestSession( - config={ - "run_id": "qmt-zmq-native", - "account_id": "backtest-account", - "bar_wait_timeout_seconds": 2, - }, - qmt_api={"passorder": fake_passorder}, - ) - holder["session"] = session - server = ZmqBacktestServer( - BacktestBridgeProtocol(session), endpoint=endpoint, exit_on_finish=True - ) - server_thread = threading.Thread(target=server.serve_forever, daemon=True) - server_thread.start() - self.assertTrue(server.wait_until_ready(2)) - - def qmt_backtest_loop(): - session.bind_context(Context(0, 10.0)) - session.on_bar(Context(0, 10.0)) - session.on_bar(Context(1, 10.1)) - session.on_qmt_stop() - - qmt_thread = threading.Thread(target=qmt_backtest_loop) - qmt_thread.start() - - client = BacktestZmqClient(endpoint, run_id="", timeout_seconds=2) - description = client.describe() - first = client.start() - queued = client.submit_order("600000.SH", "BUY", 100, client_order_id="native-buy-1") - second = client.next_bar() - done = client.next_bar() - result = client.finish() - client.close() - qmt_thread.join(timeout=2) - server_thread.join(timeout=2) - - self.assertEqual(description["engine_owner"], "QMT") - self.assertEqual(description["matching_owner"], "QMT") - self.assertEqual(client.run_id, "qmt-zmq-native") - self.assertEqual(first["frame_index"], 0) - self.assertEqual(queued["status"], "QUEUED") - self.assertEqual(second["frame_index"], 1) - self.assertEqual(second["fills"][0]["fill_id"], "qmt-fill-1") - self.assertTrue(done["done"]) - self.assertEqual(result["result_owner"], "QMT") - self.assertEqual(len(calls), 1) - self.assertEqual(calls[0][0], qmt_thread.ident) - self.assertFalse(qmt_thread.is_alive()) - self.assertFalse(server_thread.is_alive()) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_app.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_app.py deleted file mode 100644 index a32391e..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_app.py +++ /dev/null @@ -1,176 +0,0 @@ -import datetime -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.app import SignalTradingApp -from bigqmt_signal_trader.models import AssetSnapshot, PositionSnapshot, TradeSignal - - -def _signal(**kwargs): - payload = { - "signal_id": "sig-buy-001", - "account_id": "test", - "action": "BUY", - "stock_code": "000001.SZ", - "amount": 100, - "price_type": "AUTO_LIMIT", - "remark": "web_buy_command", - "created_at": "2026-06-30 09:31:00", - "expire_at": "2026-06-30 09:36:00", - "schema_version": 1, - } - payload.update(kwargs) - return TradeSignal.from_dict(payload) - - -class FakeSignalSource: - def __init__(self, items): - self.items = items - self.acked = [] - - def fetch(self, account_id, limit): - return self.items[:limit] - - def ack(self, signal): - self.acked.append(signal.signal_id) - - -class FakeMarketDataProvider: - def get_ticks(self, codes): - return { - code: { - "lastPrice": 10.0, - "askPrice": [10.01, 10.02], - "bidPrice": [9.99, 9.98], - } - for code in codes - } - - def get_instrument(self, code): - return {"InstrumentStatus": 0, "UpStopPrice": 11.0, "DownStopPrice": 9.0} - - -class FakePositionProvider: - def __init__(self): - self.positions = { - "000001.SZ": PositionSnapshot( - stock_code="000001.SZ", - volume=1000, - available=500, - cost=9.5, - ) - } - - def get_positions(self, account_id): - return self.positions - - def get_asset(self, account_id): - return AssetSnapshot(account_id=account_id, cash=100000.0, total_asset=200000.0) - - -class FakeOrderGateway: - def __init__(self): - self.submitted = [] - - def submit(self, request): - self.submitted.append(request) - from bigqmt_signal_trader.models import OrderSubmitResult - - return OrderSubmitResult(status="SUBMITTED", user_order_id="bq:sig:1") - - def cancel(self, order_ref): - return None - - def query_orders(self, account_id, strategy_name): - return [] - - def query_trades(self, account_id, strategy_name): - return [] - - -class FakePositionSyncSink: - def __init__(self): - self.snapshots = [] - - def publish(self, snapshot): - self.snapshots.append(snapshot) - - -class FakeStateStore: - def __init__(self, claim_result=True): - self.claim_result = claim_result - self.claimed = [] - self.submitted = [] - self.finished = [] - - def claim(self, signal, consumer_id): - self.claimed.append((signal.signal_id, consumer_id)) - return self.claim_result - - def mark_submitted(self, signal_id, result): - self.submitted.append((signal_id, result.status)) - - def mark_finished(self, signal_id, status, message=""): - self.finished.append((signal_id, status, message)) - - -class SignalTradingAppTest(unittest.TestCase): - def test_tick_submits_buy_signal_with_replaceable_adapters(self): - source = FakeSignalSource([_signal()]) - state = FakeStateStore() - orders = FakeOrderGateway() - sync = FakePositionSyncSink() - app = SignalTradingApp( - account_id="test", - signal_source=source, - market_data=FakeMarketDataProvider(), - position_provider=FakePositionProvider(), - order_gateway=orders, - position_sync_sink=sync, - state_store=state, - consumer_id="consumer-a", - ) - - app.tick(datetime.datetime(2026, 6, 30, 9, 31)) - - self.assertEqual(orders.submitted[0].stock_code, "000001.SZ") - self.assertEqual(orders.submitted[0].volume, 100) - self.assertEqual(state.submitted, [("sig-buy-001", "SUBMITTED")]) - self.assertEqual(source.acked, ["sig-buy-001"]) - self.assertEqual(sync.snapshots[0].account_id, "test") - - def test_tick_sells_by_percentage_using_available_position(self): - source = FakeSignalSource([ - _signal( - signal_id="sig-sell-001", - action="SELL", - amount=None, - percentage=50, - remark="web_sell_command", - ) - ]) - orders = FakeOrderGateway() - app = SignalTradingApp( - account_id="test", - signal_source=source, - market_data=FakeMarketDataProvider(), - position_provider=FakePositionProvider(), - order_gateway=orders, - position_sync_sink=FakePositionSyncSink(), - state_store=FakeStateStore(), - consumer_id="consumer-a", - ) - - app.tick(datetime.datetime(2026, 6, 30, 9, 31)) - - self.assertEqual(orders.submitted[0].action, "SELL") - self.assertEqual(orders.submitted[0].volume, 200) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_asset_frozen_cash.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_asset_frozen_cash.py deleted file mode 100644 index 385df63..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_asset_frozen_cash.py +++ /dev/null @@ -1,226 +0,0 @@ -"""frozen_cash must survive the whole chain: QMT row -> AssetSnapshot -> RPC -serialization -> client CompatObject, plus the Redis cached-asset fallback. - -Field names follow MiniQMT's XtAsset(account_id, cash, frozen_cash, -market_value, total_asset), where total_asset = cash + frozen_cash + market_value. -""" - -import datetime as _dt -import json -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapters import position_bigqmt -from bigqmt_signal_trader.adapters.position_bigqmt import BigQmtPositionProvider -from bigqmt_signal_trader.adapters.position_sync_redis import RedisPositionSyncSink -from bigqmt_signal_trader.models import AccountSnapshot, AssetSnapshot, PositionSnapshot -from bigqmt_signal_trader.redis_rpc import to_jsonable -from bigqmt_signal_trader.xtquant_compat import BigQmtXtTrader, StockAccount - - -class Row: - def __init__(self, **kwargs): - self.__dict__.update(kwargs) - - -def _provider(row): - def query(account_id, account_type, detail_type): - return [row] if detail_type in ("ACCOUNT", "ASSET") else [] - - return BigQmtPositionProvider(query) - - -class AssetSnapshotModelTest(unittest.TestCase): - def test_defaults_keep_existing_positional_callers_working(self): - snapshot = AssetSnapshot("acct", 100.0, 1000.0) - - self.assertEqual(snapshot.cash, 100.0) - self.assertEqual(snapshot.total_asset, 1000.0) - self.assertIsNone(snapshot.frozen_cash) - self.assertIsNone(snapshot.market_value) - - def test_carries_the_full_xtasset_field_set(self): - snapshot = AssetSnapshot("acct", 100.0, 1000.0, frozen_cash=50.0, market_value=850.0) - - self.assertEqual(snapshot.frozen_cash, 50.0) - self.assertEqual(snapshot.market_value, 850.0) - - -class QmtCollectionTest(unittest.TestCase): - def setUp(self): - position_bigqmt._missing_field_reported.clear() - - def test_reads_frozen_cash_from_the_account_row(self): - provider = _provider( - Row(m_dAvailable=100.0, m_dBalance=1000.0, m_dFrozenCash=50.0, m_dInstrumentValue=850.0) - ) - - asset = provider.get_asset("acct") - - self.assertEqual(asset.cash, 100.0) - self.assertEqual(asset.frozen_cash, 50.0) - self.assertEqual(asset.market_value, 850.0) - self.assertEqual(asset.total_asset, 1000.0) - - def test_accepts_alternate_broker_spellings(self): - for field in ("m_dFrozenCash", "m_dFrozen", "m_dFrozenBalance", "frozen_cash"): - provider = _provider(Row(m_dAvailable=100.0, m_dBalance=1000.0, **{field: 50.0})) - - self.assertEqual(provider.get_asset("acct").frozen_cash, 50.0, field) - - def test_derived_market_value_excludes_frozen_cash(self): - """Without this, market value is overstated by the frozen amount.""" - provider = _provider(Row(m_dAvailable=100.0, m_dBalance=1000.0, m_dFrozenCash=50.0)) - - self.assertEqual(provider.get_asset("acct").market_value, 850.0) - - def test_derivation_falls_back_when_frozen_is_absent(self): - provider = _provider(Row(m_dAvailable=100.0, m_dBalance=1000.0)) - asset = provider.get_asset("acct") - - self.assertIsNone(asset.frozen_cash) - self.assertEqual(asset.market_value, 900.0) # legacy behaviour preserved - - def test_missing_frozen_field_reports_what_the_row_actually_has(self): - """The ThinkTrader spelling is unverified offline; make it self-reporting - instead of silently returning None forever.""" - import io - import contextlib - - provider = _provider(Row(m_dAvailable=100.0, m_dBalance=1000.0, m_dWhateverElse=1.0)) - buffer = io.StringIO() - with contextlib.redirect_stdout(buffer): - provider.get_asset("acct") - output = buffer.getvalue() - - self.assertIn("frozen_cash not found", output) - self.assertIn("m_dWhateverElse", output) # names the real fields - - def test_missing_field_is_reported_once_not_per_call(self): - import io - import contextlib - - provider = _provider(Row(m_dAvailable=100.0, m_dBalance=1000.0)) - buffer = io.StringIO() - with contextlib.redirect_stdout(buffer): - for _ in range(5): - provider.get_asset("acct") - - self.assertEqual(buffer.getvalue().count("frozen_cash not found"), 1) - - def test_empty_rows_still_degrade_to_all_none(self): - provider = BigQmtPositionProvider(lambda *args: []) - - asset = provider.get_asset("acct") - - self.assertIsNone(asset.cash) - self.assertIsNone(asset.frozen_cash) - - -class RpcSerializationTest(unittest.TestCase): - def test_to_jsonable_carries_frozen_cash_over_the_wire(self): - snapshot = AssetSnapshot("acct", 100.0, 1000.0, frozen_cash=50.0, market_value=850.0) - - payload = to_jsonable(snapshot) - - self.assertEqual(payload["frozen_cash"], 50.0) - self.assertEqual(payload["market_value"], 850.0) - - -class FakeRedis: - def __init__(self): - self.kv = {} - self.streams = {} - - def set(self, key, value): - self.kv[key] = value - - def setex(self, key, ttl_seconds, value): - self.kv[key] = value - - def xadd(self, key, fields, maxlen=None, approximate=None): - self.streams.setdefault(key, []).append(fields) - return b"1-0" - - def publish(self, key, value): - return 1 - - -class PositionSyncTest(unittest.TestCase): - def test_cached_snapshot_includes_frozen_cash(self): - redis_client = FakeRedis() - RedisPositionSyncSink(redis_client).publish( - AccountSnapshot( - account_id="acct", - asset=AssetSnapshot("acct", 100.0, 1000.0, frozen_cash=50.0, market_value=850.0), - positions={"600000.SH": PositionSnapshot("600000.SH", 100, 100, 10.0, "PF")}, - reason="test", - updated_at=_dt.datetime(2026, 7, 1, 9, 31), - ) - ) - - payload = json.loads(redis_client.kv["bigqmt:positions:acct"]) - - self.assertEqual(payload["asset"]["frozen_cash"], 50.0) - self.assertEqual(payload["asset"]["market_value"], 850.0) - - -class ClientSurfaceTest(unittest.TestCase): - """The reported AttributeError: asset.frozen_cash must simply exist.""" - - def _trader(self, response): - trader = BigQmtXtTrader(account_id="acct") - trader.client.call = lambda method, params=None, account_id=None, **kw: response - return trader - - def test_frozen_cash_is_exposed(self): - asset = self._trader( - {"cash": 100.0, "total_asset": 1000.0, "frozen_cash": 50.0, "market_value": 850.0} - ).query_stock_asset(StockAccount("acct")) - - self.assertEqual(asset.frozen_cash, 50.0) - self.assertEqual(asset.cash, 100.0) - self.assertEqual(asset.market_value, 850.0) - self.assertEqual(asset.total_asset, 1000.0) - - def test_frozen_cash_defaults_to_zero_not_missing(self): - """A server that predates this field must not resurrect the - AttributeError, and callers do arithmetic on it.""" - asset = self._trader({"cash": 100.0, "total_asset": 1000.0}).query_stock_asset( - StockAccount("acct") - ) - - self.assertEqual(asset.frozen_cash, 0.0) - self.assertEqual(asset.cash + asset.frozen_cash, 100.0) - - def test_derived_market_value_excludes_frozen_cash(self): - asset = self._trader( - {"cash": 100.0, "total_asset": 1000.0, "frozen_cash": 50.0} - ).query_stock_asset(StockAccount("acct")) - - self.assertEqual(asset.market_value, 850.0) - - def test_server_market_value_wins_over_derivation(self): - asset = self._trader( - {"cash": 100.0, "total_asset": 1000.0, "frozen_cash": 50.0, "market_value": 111.0} - ).query_stock_asset(StockAccount("acct")) - - self.assertEqual(asset.market_value, 111.0) - - def test_components_reconstruct_total_asset(self): - asset = self._trader( - {"cash": 100.0, "total_asset": 1000.0, "frozen_cash": 50.0, "market_value": 850.0} - ).query_stock_asset(StockAccount("acct")) - - self.assertAlmostEqual( - asset.cash + asset.frozen_cash + asset.market_value, asset.total_asset - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_adapters.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_adapters.py deleted file mode 100644 index cc1c9c8..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_adapters.py +++ /dev/null @@ -1,275 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapter_factory import build_app -from bigqmt_signal_trader.adapters.market_bigqmt import BigQmtMarketDataProvider -from bigqmt_signal_trader.adapters.order_bigqmt import BigQmtOrderGateway -from bigqmt_signal_trader.adapters.position_bigqmt import BigQmtPositionProvider -from bigqmt_signal_trader.models import OrderRef, OrderRequest - - -class Obj: - def __init__(self, **kwargs): - self.__dict__.update(kwargs) - - -class FakeContext: - def __init__(self): - self.tick_codes = [] - self.instrument_codes = [] - - def get_full_tick(self, codes): - self.tick_codes.append(list(codes)) - return {codes[0]: {"lastPrice": 10.0}} - - def get_instrumentdetail(self, code): - self.instrument_codes.append(code) - return {"InstrumentStatus": 0} - - -class FakeMarketDataContext(FakeContext): - def __init__(self): - super().__init__() - self.market_calls = [] - - def get_market_data_ex( - self, - fields=None, - stock_code=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - ): - self.market_calls.append( - { - "method": "get_market_data_ex", - "fields": fields, - "stock_code": stock_code, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - } - ) - return {"600000.SH": {"close": [10.0]}} - - -class FakeMarketDataFallbackContext(FakeContext): - def get_market_data(self, fields=None, stock_code=None, period="1d", **kwargs): - return { - "method": "get_market_data", - "fields": fields, - "stock_code": stock_code, - "period": period, - "kwargs": kwargs, - } - - -class BigQmtAdaptersTest(unittest.TestCase): - def test_market_provider_normalizes_codes_before_context_call(self): - context = FakeContext() - provider = BigQmtMarketDataProvider(context) - - ticks = provider.get_ticks(["600000"]) - instrument = provider.get_instrument("sz000001") - - self.assertIn("600000.SH", ticks) - self.assertEqual(context.tick_codes, [["600000.SH"]]) - self.assertEqual(context.instrument_codes, ["000001.SZ"]) - self.assertEqual(instrument["InstrumentStatus"], 0) - - def test_market_provider_passes_market_codes_to_full_tick(self): - context = FakeContext() - provider = BigQmtMarketDataProvider(context) - - provider.get_ticks(["SH", "sz"]) - - self.assertEqual(context.tick_codes, [["SH", "SZ"]]) - - def test_market_provider_supports_bigqmt_market_data_ex_signature(self): - context = FakeMarketDataContext() - provider = BigQmtMarketDataProvider(context) - - data = provider.get_market_data_ex(field_list=["close"], stock_list=["600000.SH"], count=1) - - self.assertEqual(data["600000.SH"]["close"], [10.0]) - self.assertEqual(context.market_calls[0]["fields"], ["close"]) - self.assertEqual(context.market_calls[0]["stock_code"], ["600000.SH"]) - self.assertEqual(context.market_calls[0]["count"], 1) - - def test_market_provider_falls_back_to_market_data_when_ex_is_missing(self): - context = FakeMarketDataFallbackContext() - provider = BigQmtMarketDataProvider(context) - - data = provider.get_market_data_ex(field_list=["close"], stock_list=["600000.SH"], period="1m") - - self.assertEqual(data["method"], "get_market_data") - self.assertEqual(data["fields"], ["close"]) - self.assertEqual(data["stock_code"], ["600000.SH"]) - self.assertEqual(data["period"], "1m") - - def test_position_provider_maps_qmt_position_objects(self): - calls = [] - - def fake_query(account, account_type, detail_type, *args): - calls.append((account, account_type, detail_type, args)) - if detail_type == "POSITION": - return [ - Obj( - m_strInstrumentID="510300", - m_strExchangeID="SH", - m_nVolume=1000, - m_nCanUseVolume=800, - m_dOpenPrice=3.456, - m_dLastPrice=3.789, - m_dMarketValue=3789.0, - m_nFrozenVolume=200, - m_nOnRoadVolume=10, - m_nYesterdayVolume=900, - m_strInstrumentName="ETF", - ) - ] - return [] - - provider = BigQmtPositionProvider(fake_query) - positions = provider.get_positions("acct") - - self.assertEqual(calls[0], ("acct", "STOCK", "POSITION", ())) - self.assertEqual(positions["510300.SH"].volume, 1000) - self.assertEqual(positions["510300.SH"].available, 800) - self.assertEqual(positions["510300.SH"].cost, 3.456) - self.assertEqual(positions["510300.SH"].price, 3.789) - self.assertEqual(positions["510300.SH"].market_value, 3789.0) - self.assertEqual(positions["510300.SH"].frozen_volume, 200) - self.assertEqual(positions["510300.SH"].on_road_volume, 10) - self.assertEqual(positions["510300.SH"].yesterday_volume, 900) - - def test_order_gateway_submit_uses_qmt_jq_trade_passorder_shape(self): - calls = [] - - def fake_passorder(*args): - calls.append(args) - - context = object() - gateway = BigQmtOrderGateway(context_info=context, passorder_func=fake_passorder) - request = OrderRequest( - signal_id="sig-001", - account_id="acct", - action="BUY", - stock_code="600000", - volume=300, - price=10.12, - price_type=44, - strategy_name="bigqmt_signal_trader", - remark="manual", - ) - - result = gateway.submit(request) - - self.assertEqual(result.status, "SUBMITTED") - self.assertEqual(result.user_order_id, "manual") - self.assertEqual(calls[0][0:9], (23, 1101, "acct", "600000.SH", 44, 10.12, 300, "bigqmt_signal_trader", 2)) - self.assertEqual(calls[0][9], result.user_order_id) - self.assertIs(calls[0][10], context) - - def test_order_gateway_cancel_and_query_orders(self): - cancel_calls = [] - - def fake_cancel(*args): - cancel_calls.append(args) - return True - - def fake_query(account, account_type, detail_type, strategy_name): - self.assertEqual((account, account_type, detail_type, strategy_name), ("acct", "STOCK", "ORDER", "s")) - return [ - Obj( - m_strOrderSysID="ord1", - m_strRemark="remark1", - m_strInstrumentID="000001", - m_strExchangeID="SZ", - m_nOffsetFlag=49, - m_nVolumeTotalOriginal=1000, - m_nVolumeTraded=200, - m_nOrderStatus=50, - ) - ] - - context = object() - gateway = BigQmtOrderGateway( - context_info=context, - account_id="acct", - cancel_func=fake_cancel, - get_trade_detail_data_func=fake_query, - ) - - cancel_result = gateway.cancel(OrderRef("ord1")) - orders = gateway.query_orders("acct", "s") - - self.assertTrue(cancel_result.success) - self.assertEqual(cancel_calls, [("ord1", "acct", "STOCK", context)]) - self.assertEqual(orders[0].stock_code, "000001.SZ") - self.assertEqual(orders[0].action, "SELL") - self.assertEqual(orders[0].traded_volume, 200) - - def test_query_trades_without_strategy_omits_strategy_filter(self): - calls = [] - - def fake_query(*args): - calls.append(args) - return [ - Obj( - m_strTradeID="manual-trade-1", - m_strOrderSysID="manual-order-1", - m_strInstrumentID="600276", - m_strExchangeID="SH", - m_nOffsetFlag=48, - m_nVolume=100, - m_dPrice=54.76, - m_strTradeTime="130524", - m_strRemark="", - ) - ] - - gateway = BigQmtOrderGateway( - context_info=object(), - get_trade_detail_data_func=fake_query, - ) - - trades = gateway.query_trades_strict("acct", "") - - self.assertEqual(calls, [("acct", "STOCK", "DEAL")]) - self.assertEqual(trades[0].trade_id, "manual-trade-1") - self.assertEqual(trades[0].stock_code, "600276.SH") - self.assertEqual(trades[0].action, "BUY") - self.assertEqual(trades[0].volume, 100) - self.assertEqual(trades[0].price, 54.76) - - def test_factory_bigqmt_mode_wires_real_adapters(self): - app = build_app( - FakeContext(), - { - "mode": "bigqmt", - "account_id": "acct", - "qmt_api": { - "passorder": lambda *args: None, - "cancel": lambda *args: True, - "get_trade_detail_data": lambda *args: [], - }, - }, - ) - - self.assertIsInstance(app.market_data, BigQmtMarketDataProvider) - self.assertIsInstance(app.position_provider, BigQmtPositionProvider) - self.assertIsInstance(app.order_gateway, BigQmtOrderGateway) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_market_raw_bridge.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_market_raw_bridge.py deleted file mode 100644 index 4104044..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_bigqmt_market_raw_bridge.py +++ /dev/null @@ -1,73 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapters.market_bigqmt import BigQmtMarketDataProvider - - -class RawMarketContext: - def __init__(self, payload=None): - self.payload = payload or {} - self.calls = [] - - def get_market_data_ex_ori( - self, - fields=None, - stock_code=None, - period="1d", - start_time="", - end_time="", - count=-1, - dividend_type="none", - ): - self.calls.append( - { - "fields": fields, - "stock_code": stock_code, - "period": period, - "start_time": start_time, - "end_time": end_time, - "count": count, - "dividend_type": dividend_type, - } - ) - return self.payload - - def get_market_data_ex(self, *args, **kwargs): - raise AssertionError("DataFrame-producing QMT API must not be called") - - -class BigQmtRawMarketBridgeTest(unittest.TestCase): - def test_market_data_ex_uses_raw_context_api(self): - rows = [[1784014200000, 55.1], [1784014260000, 55.2]] - context = RawMarketContext({"600276.SH": rows}) - provider = BigQmtMarketDataProvider(context) - - data = provider.get_market_data_ex( - field_list=["close"], stock_list=["600276.SH"], period="1m", count=2 - ) - - self.assertEqual("DataFrame", data["600276.SH"]["__bigqmt_type__"]) - self.assertEqual(["stime", "close"], data["600276.SH"]["columns"]) - self.assertEqual(rows, data["600276.SH"]["records"]) - self.assertEqual(["close"], context.calls[0]["fields"]) - self.assertEqual(["600276.SH"], context.calls[0]["stock_code"]) - - def test_market_data_ex_returns_empty_frame_for_requested_symbol(self): - context = RawMarketContext({}) - provider = BigQmtMarketDataProvider(context) - - data = provider.get_market_data_ex( - field_list=["close"], stock_list=["600276.SH"], period="1m", count=2 - ) - - self.assertEqual([], data["600276.SH"]["records"]) - self.assertEqual(["stime", "close"], data["600276.SH"]["columns"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_code_utils.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_code_utils.py deleted file mode 100644 index 9f270d9..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_code_utils.py +++ /dev/null @@ -1,38 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.code_utils import ( - normalize_stock_code, - round_buy_volume, - round_sell_volume, -) - - -class CodeUtilsTest(unittest.TestCase): - def test_normalize_stock_code_accepts_common_formats(self): - self.assertEqual(normalize_stock_code("600000"), "600000.SH") - self.assertEqual(normalize_stock_code("000001"), "000001.SZ") - self.assertEqual(normalize_stock_code("SZ000001"), "000001.SZ") - self.assertEqual(normalize_stock_code("sh600000"), "600000.SH") - self.assertEqual(normalize_stock_code("600000.SH"), "600000.SH") - - def test_normalize_stock_code_keeps_etf_tradable(self): - self.assertEqual(normalize_stock_code("510300"), "510300.SH") - self.assertEqual(normalize_stock_code("159915"), "159915.SZ") - - def test_round_buy_volume_by_lot(self): - self.assertEqual(round_buy_volume("000001.SZ", 1234), 1200) - self.assertEqual(round_buy_volume("688001.SH", 234), 200) - - def test_round_sell_volume_keeps_all_when_sell_all(self): - self.assertEqual(round_sell_volume("000001.SZ", 1234, sell_all=False), 1200) - self.assertEqual(round_sell_volume("000001.SZ", 1234, sell_all=True), 1234) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_download_jobs.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_download_jobs.py deleted file mode 100644 index 8e4e5fd..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_download_jobs.py +++ /dev/null @@ -1,181 +0,0 @@ -import json -import os -import sys -import time -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.download_jobs import ( - _enc, - current_key, - job_key, - pump_download_jobs, - queue_key, - read_download_status, - submit_download_job, - wait_download_job, -) - - -class FakeRedis: - def __init__(self): - self.kv = {} - self.lists = {} - self.expired = [] - - def setex(self, key, ttl, value): - self.kv[key] = value - self.expired.append((key, ttl)) - return True - - def set(self, key, value): - self.kv[key] = value - return True - - def get(self, key): - return self.kv.get(key) - - def delete(self, key): - self.kv.pop(key, None) - return 1 - - def rpush(self, key, value): - self.lists.setdefault(key, []).append(value) - return len(self.lists[key]) - - def lpop(self, key): - lst = self.lists.get(key) or [] - if not lst: - return None - return lst.pop(0) - - def expire(self, key, ttl): - self.expired.append((key, ttl)) - return True - - -class FakeMarketData: - def __init__(self, fail_on=None, sleep=0.0): - self.data2_calls = [] - self.data_calls = [] - self.fail_on = fail_on - self.sleep = sleep - - def download_history_data2(self, stock_list, period, start_time, end_time, incrementally): - if self.sleep: - time.sleep(self.sleep) - if self.fail_on and self.fail_on in stock_list: - raise RuntimeError("boom") - self.data2_calls.append(list(stock_list)) - - def download_history_data(self, code, period, start_time, end_time, incrementally): - if self.sleep: - time.sleep(self.sleep) - if self.fail_on and code == self.fail_on: - raise RuntimeError("boom") - self.data_calls.append(code) - - -class DownloadJobsTest(unittest.TestCase): - def test_submit_queues_pending_job(self): - r = FakeRedis() - job = submit_download_job(r, "acct", ["600000.SH", "000001.SZ"], "1d", chunk_size=1) - - self.assertEqual(job["state"], "pending") - self.assertEqual(job["total"], 2) - self.assertIn(_enc(job["job_id"]), r.lists[queue_key("acct")]) - self.assertEqual(read_download_status(r, "acct", job["job_id"])["state"], "pending") - - def test_pump_completes_and_chunks_the_symbol_list(self): - r = FakeRedis() - md = FakeMarketData() - submit_download_job(r, "acct", ["a", "b", "c", "d", "e"], "1d", chunk_size=2) - - # max_wall_seconds=0 disables the budget, so one tick drains the whole job. - res = pump_download_jobs(r, md, "acct", chunk_size=2, max_wall_seconds=0) - - self.assertEqual(res["state"], "done") - self.assertEqual(res["done"], 5) - self.assertEqual(md.data2_calls, [["a", "b"], ["c", "d"], ["e"]]) - # current pointer cleared when the job finishes. - self.assertIsNone(r.get(current_key("acct"))) - - def test_pump_spreads_across_ticks_under_wall_budget(self): - r = FakeRedis() - md = FakeMarketData(sleep=0.02) - submit_download_job(r, "acct", ["a", "b", "c"], "1d", chunk_size=1) - - res1 = pump_download_jobs(r, md, "acct", max_wall_seconds=0.005) - res2 = pump_download_jobs(r, md, "acct", max_wall_seconds=0.005) - res3 = pump_download_jobs(r, md, "acct", max_wall_seconds=0.005) - - # One chunk per tick (each chunk exceeds the tiny budget), progress resumes. - self.assertEqual((res1["state"], res1["done"]), ("running", 1)) - self.assertEqual((res2["state"], res2["done"]), ("running", 2)) - self.assertEqual((res3["state"], res3["done"]), ("done", 3)) - self.assertEqual(md.data_calls if md.data_calls else md.data2_calls, [["a"], ["b"], ["c"]]) - - def test_pump_marks_failed_and_clears_current(self): - r = FakeRedis() - md = FakeMarketData(fail_on="b") - job = submit_download_job( - r, "acct", ["a", "b", "c"], "1d", method="download_history_data", chunk_size=1 - ) - - res = pump_download_jobs(r, md, "acct", max_wall_seconds=0) - - self.assertEqual(res["state"], "failed") - self.assertEqual(md.data_calls, ["a"]) - status = read_download_status(r, "acct", job["job_id"]) - self.assertEqual(status["state"], "failed") - self.assertTrue(status["error"]) - self.assertIsNone(r.get(current_key("acct"))) - - def test_pump_with_no_job_returns_none(self): - self.assertIsNone(pump_download_jobs(FakeRedis(), FakeMarketData(), "acct")) - - def test_wait_returns_terminal_status(self): - r = FakeRedis() - job = submit_download_job(r, "acct", ["a"], "1d", chunk_size=1) - status = read_download_status(r, "acct", job["job_id"]) - status["state"] = "done" - status["done"] = 1 - r.set(job_key("acct", job["job_id"]), _enc(json.dumps(status))) - - res = wait_download_job(r, "acct", job["job_id"], wait_seconds=1, poll_interval_seconds=0.01) - - self.assertEqual(res["state"], "done") - - def test_stored_values_are_digit_free_and_compliance_safe(self): - import re - - from bigqmt_signal_trader.download_jobs import _dec - - # The QMT redis compliance filter blocks a response only when it contains a - # stock-code pattern (which requires digits). Encoded tokens are all letters. - stock_re = re.compile( - "(^|[^\\d])+([36]0[\\d]{4}|00(000[1-9]|[1-9][\\d]{3}|[\\d][1-9][\\d]{2}|[\\d]{2}[1-9][\\d]))([^\\d]|$)+" - ) - blob = json.dumps({"stock_list": ["600000.SH", "300750.SZ", "000001.SZ"], "chunk_size": 1}) - self.assertTrue(stock_re.search(blob)) # plaintext WOULD trip the filter - token = _enc(blob) - self.assertTrue(all(not c.isdigit() for c in token), "encoded token must be digit-free") - self.assertIsNone(stock_re.search(token), "encoded token must not match the stock-code filter") - self.assertEqual(_dec(token), blob) # round-trips - self.assertIsNone(_dec(None)) - self.assertIsNone(_dec("")) - - # what actually lands in Redis on submit must also be digit-free - r = FakeRedis() - job = submit_download_job(r, "acct", ["600000.SH"], "1d", chunk_size=1) - stored_blob = r.kv[job_key("acct", job["job_id"])] - queued = r.lists[queue_key("acct")][0] - self.assertTrue(all(not c.isdigit() for c in stored_blob)) - self.assertTrue(all(not c.isdigit() for c in queued)) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_entry_encoding.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_entry_encoding.py deleted file mode 100644 index a550696..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_entry_encoding.py +++ /dev/null @@ -1,50 +0,0 @@ -import glob -import os -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -SRC = os.path.join(ROOT, "src") - - -class EntryEncodingTest(unittest.TestCase): - """Guard against the QMT load crash: a file declaring ``#coding:gbk`` but - containing non-GBK (e.g. UTF-8 Chinese) bytes fails to load under QMT's - GBK-based Python with 'gbk codec can't decode ...'. Entry files loaded by the - QMT editor must stay GBK-decodable (ASCII is the safe subset).""" - - def test_gbk_declared_files_are_gbk_decodable(self): - bad = [] - for path in glob.glob(os.path.join(SRC, "**", "*.py"), recursive=True): - if "__pycache__" in path: - continue - data = open(path, "rb").read() - first_line = data.split(b"\n", 1)[0].lower().replace(b" ", b"") - if b"coding:gbk" not in first_line and b"coding=gbk" not in first_line: - continue - try: - data.decode("gbk") - except UnicodeDecodeError as exc: - bad.append("%s (byte %d)" % (os.path.relpath(path, ROOT), exc.start)) - self.assertEqual( - bad, - [], - "files declare #coding:gbk but are not GBK-decodable; QMT will fail to load them: %s" % bad, - ) - - def test_qmt_loader_stops_previous_service_before_clearing_modules(self): - """A QMT strategy restart must release the old ZMQ port first.""" - path = os.path.join(SRC, "BIGQMT_REDIS_DRYRUN.py") - with open(path, "r", encoding="gbk") as source_file: - source = source_file.read() - self.assertIn("def _stop_previous_rpc_service():", source) - stop_call = source.index("\n_stop_previous_rpc_service()\n") - clear_call = source.index("\n_clear_local_modules()\n") - self.assertLess( - stop_call, - clear_call, - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_exec_events.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_exec_events.py deleted file mode 100644 index b8c4ea2..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_exec_events.py +++ /dev/null @@ -1,633 +0,0 @@ -import json -import time -import threading -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.exec_events import ( - enrich_order_identity, - format_raw_snapshot, - normalize_cancel_error_event, - normalize_order_error_event, - normalize_order_event, - remember_order_identity, - normalize_trade_event, - order_channel, - order_error_channel, - cancel_error_channel, - publish_order_event, - publish_trade_event, - raw_field_snapshot, - trade_channel, -) -from bigqmt_signal_trader.xtquant_compat import BigQmtXtTrader, XtQuantTraderCallback - - -class FakeDeal: - m_strAccountID = "acct" - m_strInstrumentID = "600000.SH" - m_dPrice = 10.5 - m_nVolume = 100 - m_strTradeID = "T1" - m_strOrderSysID = "O1" - m_strTradeTime = "2026-07-02 10:00:00" - m_nDirection = 48 - m_dTradeAmount = 1050.0 - m_dComssion = 0.5 - - -class FakeOrder: - m_strAccountID = "acct" - m_strInstrumentID = "000001.SZ" - m_nOrderStatus = 50 - m_nVolumeTotal = 200 - m_nVolumeTraded = 50 - m_dLimitPrice = 9.9 - m_strOrderSysID = "O2" - m_nDirection = 49 - strategyName = "s1" - m_strRemark = "remark-1" - m_strOptName = "限价买入" - - -class FakeRedis: - def __init__(self): - self.xadds = [] - self.pubs = [] - self.kv = {} - - def xadd(self, key, fields, maxlen=None, approximate=None): - self.xadds.append((key, fields)) - return b"1-0" - - def publish(self, key, value): - self.pubs.append((key, value)) - return 1 - - def setex(self, key, _ttl, value): - self.kv[key] = value - return True - - def get(self, key): - return self.kv.get(key) - - -class RecordingCallback(XtQuantTraderCallback): - def __init__(self): - self.orders = [] - self.trades = [] - self.order_errors = [] - self.cancel_errors = [] - self.async_responses = [] - self.cancel_async_responses = [] - self.account_statuses = [] - - def on_stock_order(self, order): - self.orders.append(order) - - def on_stock_trade(self, trade): - self.trades.append(trade) - - def on_order_error(self, order_error): - self.order_errors.append(order_error) - - def on_cancel_error(self, cancel_error): - self.cancel_errors.append(cancel_error) - - def on_order_stock_async_response(self, response): - self.async_responses.append(response) - - def on_cancel_order_stock_async_response(self, response): - self.cancel_async_responses.append(response) - - def on_account_status(self, status): - self.account_statuses.append(status) - - -class ExecEventsServerTest(unittest.TestCase): - def test_normalize_trade_event_maps_thinktrader_fields(self): - ev = normalize_trade_event(FakeDeal(), "acct") - - self.assertEqual(ev["event_type"], "trade") - self.assertEqual(ev["stock_code"], "600000.SH") - self.assertEqual(ev["trade_id"], "T1") - self.assertEqual(ev["order_sys_id"], "O1") - self.assertEqual(ev["volume"], 100) - self.assertEqual(ev["price"], 10.5) - self.assertEqual(ev["action"], "BUY") # m_nDirection 48 -> buy - self.assertEqual(ev["traded_at"], "2026-07-02 10:00:00") - self.assertEqual(ev["commission"], 0.5) - - def test_normalize_order_event_maps_thinktrader_fields(self): - ev = normalize_order_event(FakeOrder(), "acct") - - self.assertEqual(ev["event_type"], "order") - self.assertEqual(ev["stock_code"], "000001.SZ") - self.assertEqual(ev["order_sys_id"], "O2") - self.assertEqual(ev["order_volume"], 200) - self.assertEqual(ev["traded_volume"], 50) - self.assertEqual(ev["status"], 50) - self.assertEqual(ev["action"], "SELL") # m_nDirection 49 -> sell - self.assertEqual(ev["strategy_name"], "s1") - self.assertEqual(ev["remark"], "remark-1") - self.assertEqual(ev["user_order_id"], "remark-1") - self.assertEqual(ev["opt_name"], "限价买入") - - def test_order_event_fills_strategy_from_remark_identity(self): - class CallbackOrder: - m_strAccountID = "acct" - m_strInstrumentID = "159518" - m_strRemark = "涨停价买入1手" - m_strOptName = "限价买入" - - redis_client = FakeRedis() - remember_order_identity(redis_client, "acct", "涨停价买入1手", "rpc_test", "159518") - ev = enrich_order_identity(redis_client, "acct", normalize_order_event(CallbackOrder(), "acct")) - - self.assertEqual(ev["strategy_name"], "rpc_test") - self.assertEqual(ev["remark"], "涨停价买入1手") - - def test_publish_writes_stream_and_channel(self): - r = FakeRedis() - publish_trade_event(r, "acct", {"event_type": "trade", "trade_id": "T1"}) - - self.assertEqual(r.pubs[0][0], trade_channel("acct")) - self.assertEqual(r.xadds[0][0], trade_channel("acct")) - self.assertIn("T1", r.pubs[0][1]) - - publish_order_event(r, "acct", {"event_type": "order"}) - self.assertEqual(r.pubs[1][0], order_channel("acct")) - - def test_arbitration_resolves_direction_offset_conflict_via_op_type(self): - """When m_nDirection and m_nOffsetFlag disagree (futures: sell+open), - arbitration via m_nOpType picks the semantically correct field.""" - class Deal: - m_strInstrumentID = "600000.SH" - m_nDirection = 49 # EEntrustBS sell - m_nOffsetFlag = 48 # offset 48 = 开仓 (open) - m_nOpType = 24 # STOCK_SELL — arbiter confirms sell - m_nVolume = 10 - m_dPrice = 1.0 - m_strTradeID = "X" - - ev = normalize_trade_event(Deal(), "acct") - - self.assertEqual(ev["action"], "SELL") # from direction via arbitration - self.assertEqual(ev["direction"], 49) # direction field = m_nDirection - self.assertEqual(ev["offset_flag"], 48) # raw offset preserved, not conflated - - def test_arbitration_stock_sell_wrong_direction_fixed_by_op_type(self): - """Stock sell: m_nDirection=48 (bug: always 48), m_nOffsetFlag=49, - m_nOpType=24 → arbitration picks offset (49→SELL).""" - class SellOrder: - m_strInstrumentID = "601398.SH" - m_nDirection = 48 # QMT bug — always 48 in live callbacks - m_nOffsetFlag = 49 # 平仓 = sell (correct) - m_nOpType = 24 # STOCK_SELL (correct) - m_nVolumeTotal = 100 - m_nVolumeTraded = 0 - m_dLimitPrice = 6.34 - m_strOrderSysID = "S123" - - ev = normalize_order_event(SellOrder(), "acct") - self.assertEqual(ev["action"], "SELL") - self.assertEqual(ev["direction"], 49) # offset_flag wins via arbitration - - def test_arbitration_stock_buy_agree(self): - """Stock buy: m_nDirection=48, m_nOffsetFlag=48 → agree → BUY.""" - class BuyOrder: - m_strInstrumentID = "601398.SH" - m_nDirection = 48 - m_nOffsetFlag = 48 - m_nOpType = 23 - m_nVolumeTotal = 100 - m_nVolumeTraded = 0 - m_dLimitPrice = 5.0 - m_strOrderSysID = "B456" - - ev = normalize_order_event(BuyOrder(), "acct") - self.assertEqual(ev["action"], "BUY") - self.assertEqual(ev["direction"], 48) - - def test_direction_zero_falls_back_to_offset(self): - """m_nDirection=0 is treated as absent; offset determines direction.""" - class SellOrder: - m_strInstrumentID = "601398.SH" - m_nDirection = 0 - m_nOffsetFlag = 49 - m_nVolumeTotal = 100 - m_nVolumeTraded = 0 - m_dLimitPrice = 6.34 - m_strOrderSysID = "S123" - - ev = normalize_order_event(SellOrder(), "acct") - self.assertEqual(ev["action"], "SELL") - self.assertEqual(ev["direction"], 49) - - def test_direction_none_falls_back_to_offset(self): - """m_nDirection=None → offset determines direction.""" - class BuyOrder: - m_strInstrumentID = "601398.SH" - m_nDirection = None - m_nOffsetFlag = 48 - m_nVolumeTotal = 100 - m_nVolumeTraded = 0 - m_dLimitPrice = 5.0 - m_strOrderSysID = "B456" - - ev = normalize_order_event(BuyOrder(), "acct") - self.assertEqual(ev["action"], "BUY") - self.assertEqual(ev["direction"], 48) - - def test_pledge_direction_has_no_buy_sell_action(self): - class Deal: - m_strInstrumentID = "600000.SH" - m_nDirection = 81 # 质押入库 - m_nVolume = 10 - m_dPrice = 1.0 - - ev = normalize_trade_event(Deal(), "acct") - - self.assertEqual(ev["action"], "") # pledge is neither buy nor sell - self.assertEqual(ev["direction"], 81) # raw direction preserved - - def test_normalize_order_error_event_maps_fields(self): - class OrderError: - m_strAccountID = "acct" - m_strInstrumentID = "600654.SH" - m_strOrderSysID = "sys-err-1" - m_nErrorID = 2147483647 - m_strErrorMsg = "废单" - - ev = normalize_order_error_event(OrderError(), "acct") - - self.assertEqual(ev["event_type"], "order_error") - self.assertEqual(ev["account_id"], "acct") - self.assertEqual(ev["stock_code"], "600654.SH") - self.assertEqual(ev["order_sys_id"], "sys-err-1") - self.assertEqual(ev["error_id"], 2147483647) - self.assertEqual(ev["error_msg"], "废单") - - def test_normalize_cancel_error_event_maps_fields(self): - class CancelError: - m_strAccountID = "acct" - m_strInstrumentID = "600654.SH" - m_strOrderSysID = "sys-cancel-1" - m_nErrorID = 99 - m_strErrorMsg = "撤单失败" - - ev = normalize_cancel_error_event(CancelError(), "acct") - - self.assertEqual(ev["event_type"], "cancel_error") - self.assertEqual(ev["account_id"], "acct") - self.assertEqual(ev["order_sys_id"], "sys-cancel-1") - self.assertEqual(ev["error_id"], 99) - self.assertEqual(ev["error_msg"], "撤单失败") - - def test_error_channels_are_account_scoped(self): - self.assertTrue(order_error_channel("acct").endswith(":acct")) - self.assertTrue(cancel_error_channel("acct").endswith(":acct")) - - -class RawFieldSnapshotTest(unittest.TestCase): - """The snapshot exists to settle what live callbacks actually carry, so it - must capture m_* and MiniQMT fields alike and never raise.""" - - def test_captures_thinktrader_and_miniqmt_fields(self): - snap = raw_field_snapshot(FakeOrder()) - - self.assertIn("m_nDirection", snap) - self.assertIn("49", snap["m_nDirection"]) - self.assertIn("int", snap["m_nDirection"]) - self.assertIn("m_strInstrumentID", snap) - - def test_captures_miniqmt_style_object(self): - class XtOrderLike: - stock_code = "601398.SH" - order_type = 24 - order_volume = 100 - - snap = raw_field_snapshot(XtOrderLike()) - - self.assertIn("24", snap["order_type"]) - self.assertIn("601398.SH", snap["stock_code"]) - - def test_captures_dict_payload(self): - snap = raw_field_snapshot({"m_nOffsetFlag": 48, "order_type": 24}) - - self.assertIn("48", snap["m_nOffsetFlag"]) - self.assertIn("24", snap["order_type"]) - - def test_skips_callables_and_dunders(self): - class WithMethod: - m_nDirection = 49 - - def m_method(self): - return 1 - - snap = raw_field_snapshot(WithMethod()) - - self.assertIn("m_nDirection", snap) - self.assertNotIn("m_method", snap) - - def test_unreadable_attribute_does_not_raise(self): - class Exploding: - m_nDirection = 49 - - @property - def m_nOffsetFlag(self): - raise RuntimeError("boom") - - snap = raw_field_snapshot(Exploding()) - - self.assertIn("m_nDirection", snap) - self.assertIn("unreadable", snap["m_nOffsetFlag"]) - - def test_format_is_a_single_ascii_safe_line(self): - line = format_raw_snapshot("order", FakeOrder()) - - self.assertNotIn("\n", line) - self.assertTrue(line.startswith("[bigqmt_exec_raw] order")) - self.assertIn("m_nDirection", line) - - -class ExecEventsClientDispatchTest(unittest.TestCase): - def _trader(self): - trader = BigQmtXtTrader(account_id="acct") - cb = RecordingCallback() - trader.register_callback(cb) - return trader, cb - - def test_dispatch_trade_invokes_on_stock_trade(self): - trader, cb = self._trader() - event = { - "event_type": "trade", - "account_id": "acct", - "stock_code": "600000.SH", - "order_sys_id": "sys-1", - "trade_id": "t-1", - "volume": 100, - "price": 10.5, - "action": "BUY", - "traded_at": "2026-07-02 10:00:00", - } - trader._dispatch_event(json.dumps(event).encode("utf-8")) - - self.assertEqual(len(cb.trades), 1) - trade = cb.trades[0] - self.assertEqual(trade.stock_code, "600000.SH") - self.assertEqual(trade.trade_id, "t-1") - self.assertEqual(trade.traded_volume, 100) - self.assertEqual(trade.traded_price, 10.5) - self.assertEqual(trade.order_type, 23) # BUY -> STOCK_BUY - - def test_dispatch_order_invokes_on_stock_order(self): - trader, cb = self._trader() - event = { - "event_type": "order", - "account_id": "acct", - "stock_code": "000001.SZ", - "order_sys_id": "sys-2", - "order_volume": 200, - "traded_volume": 50, - "price": 9.9, - "status": 50, - "action": "SELL", - } - trader._dispatch_event(json.dumps(event).encode("utf-8")) - - self.assertEqual(len(cb.orders), 1) - order = cb.orders[0] - self.assertEqual(order.stock_code, "000001.SZ") - self.assertEqual(order.order_volume, 200) - self.assertEqual(order.traded_volume, 50) - self.assertEqual(order.order_status, 50) - self.assertEqual(order.order_type, 24) # SELL -> STOCK_SELL - - def test_dispatch_without_callback_is_noop(self): - trader = BigQmtXtTrader(account_id="acct") - # No callback registered; must not raise. - trader._dispatch_event(json.dumps({"event_type": "trade"}).encode("utf-8")) - - def test_dispatch_order_error_invokes_on_order_error(self): - trader, cb = self._trader() - event = { - "event_type": "order_error", - "account_id": "acct", - "stock_code": "600654.SH", - "order_sys_id": "sys-err-1", - "error_id": 2147483647, - "error_msg": "废单", - } - trader._dispatch_event(json.dumps(event).encode("utf-8")) - - self.assertEqual(len(cb.order_errors), 1) - err = cb.order_errors[0] - self.assertEqual(err.order_id, "sys-err-1") - self.assertEqual(err.error_id, 2147483647) - self.assertEqual(err.error_msg, "废单") - self.assertEqual(err.stock_code, "600654.SH") - - def test_dispatch_cancel_error_invokes_on_cancel_error(self): - trader, cb = self._trader() - event = { - "event_type": "cancel_error", - "account_id": "acct", - "stock_code": "600654.SH", - "order_sys_id": "sys-cancel-1", - "error_id": 99, - "error_msg": "撤单失败", - } - trader._dispatch_event(json.dumps(event).encode("utf-8")) - - self.assertEqual(len(cb.cancel_errors), 1) - err = cb.cancel_errors[0] - self.assertEqual(err.order_id, "sys-cancel-1") - self.assertEqual(err.error_id, 99) - self.assertEqual(err.error_msg, "撤单失败") - - def _run_async(self, trader, result=None, raises=None): - """Submit one async order with order_stock_result stubbed, and wait. - - order_stock_async is fire-and-forget since issue #50: it returns the seq - without touching the network and the submit happens on a worker thread, - so the callback assertions need the queue drained first. The stub is - restored only after the worker is done with it. - """ - original = trader.order_stock_result - - def fake(*args, **kwargs): - if raises is not None: - raise raises - return result - - trader.order_stock_result = fake - try: - seq = trader.order_stock_async("acct", "600654.SH", 23, 100, 11, 10.0, "s", "r") - self.assertTrue(trader.wait_async_orders(timeout=5.0), "async order did not finish") - finally: - trader.order_stock_result = original - return seq - - def test_order_stock_async_returns_seq_without_submitting(self): - """issue #50: the seq must come back before any RPC happens.""" - trader, _cb = self._trader() - started = threading.Event() - release = threading.Event() - - def blocking(*args, **kwargs): - started.set() - release.wait(5.0) - return {"order_sys_id": "sys-slow"} - - trader.order_stock_result = blocking - try: - t0 = time.time() - seq = trader.order_stock_async("acct", "600654.SH", 23, 100, 11, 10.0, "s", "r") - elapsed = time.time() - t0 - - self.assertGreater(seq, 0) - self.assertLess(elapsed, 0.2, "order_stock_async blocked for %.3fs" % elapsed) - self.assertTrue(started.wait(5.0), "submit never ran on the worker") - finally: - release.set() - trader.wait_async_orders(timeout=5.0) - - def test_order_stock_async_fires_response_when_submitted(self): - trader, cb = self._trader() - seq = self._run_async(trader, result={"order_sys_id": "sys-ok-1", "user_order_id": "u-1"}) - - self.assertGreater(seq, 0) - self.assertEqual(len(cb.async_responses), 1) - resp = cb.async_responses[0] - self.assertEqual(resp.order_id, "sys-ok-1") - self.assertEqual(resp.account_id, "acct") - self.assertEqual(resp.seq, seq) - - def test_order_stock_async_requests_no_settlement_wait(self): - """The server must not hold the reply for the order id on this path.""" - trader, _cb = self._trader() - seen = {} - original = trader.order_stock_result - - def fake(*args, **kwargs): - seen.update(kwargs) - return {"order_sys_id": "sys-1"} - - trader.order_stock_result = fake - try: - trader.order_stock_async("acct", "600654.SH", 23, 100, 11, 10.0, "s", "r") - trader.wait_async_orders(timeout=5.0) - finally: - trader.order_stock_result = original - - self.assertIs(seen.get("wait_settlement"), False) - - def test_order_stock_async_minus_one_fires_order_error(self): - trader, cb = self._trader() - seq = self._run_async(trader, result=-1) # MiniQMT: submit failed - - self.assertGreater(seq, 0) - self.assertEqual(len(cb.order_errors), 1) - err = cb.order_errors[0] - self.assertEqual(err.error_id, -1) - self.assertEqual(err.stock_code, "600654.SH") - self.assertEqual(err.seq, seq) # correlate the failure to the seq - # No success response for a failed submit. - self.assertEqual(len(cb.async_responses), 0) - - def test_order_stock_async_submitted_without_sysid_fires_response_not_error(self): - # issue #38: passorder 已提交但委托号还没分配到(order_sys_id 为空)时, - # 必须回调成功响应而不是误报 on_order_error。issue #50 之后这是常态: - # 异步路径不再等待委托号,它由 order_callback 推送。 - trader, cb = self._trader() - seq = self._run_async( - trader, result={"status": "SUBMITTED", "user_order_id": "u-1", "order_sys_id": ""}) - - self.assertGreater(seq, 0) - self.assertEqual(len(cb.order_errors), 0) - self.assertEqual(len(cb.async_responses), 1) - resp = cb.async_responses[0] - self.assertEqual(resp.order_id, "u-1") # 委托号未知时回退到 user_order_id - self.assertEqual(resp.order_sys_id, "") - - def test_order_stock_async_server_error_fires_order_error_with_reason(self): - # server_error(委托没进系统)由 call() 转成异常后,async 必须把真实 - # 原因回调给 on_order_error(issue #38)。 - trader, cb = self._trader() - seq = self._run_async(trader, raises=RuntimeError( - "Big QMT order_stock server_error: passorder submitted but " - "order not found in system (stock=600654.SH action=BUY price=10.00 " - "volume=100). QMT may have silently rejected it.")) - - self.assertGreater(seq, 0) - self.assertEqual(len(cb.async_responses), 0) - self.assertEqual(len(cb.order_errors), 1) - err = cb.order_errors[0] - self.assertIn("not found in system", err.error_msg) - self.assertEqual(err.stock_code, "600654.SH") - - def test_async_orders_keep_submission_order(self): - """One worker, so responses arrive in the order the calls were made.""" - trader, cb = self._trader() - original = trader.order_stock_result - - def fake(*args, **kwargs): - return {"order_sys_id": "sys-%s" % args[1]} - - trader.order_stock_result = fake - try: - for code in ("A.SH", "B.SH", "C.SH"): - trader.order_stock_async("acct", code, 23, 100, 11, 10.0, "s", "r") - self.assertTrue(trader.wait_async_orders(timeout=5.0)) - finally: - trader.order_stock_result = original - - self.assertEqual([r.order_id for r in cb.async_responses], - ["sys-A.SH", "sys-B.SH", "sys-C.SH"]) - self.assertEqual([r.seq for r in cb.async_responses], - sorted(r.seq for r in cb.async_responses)) - - def test_cancel_order_stock_async_fires_response(self): - trader, cb = self._trader() - original = trader.cancel_order_stock_sysid - - def fake_cancel(account, market, sysid): - return True - - trader.cancel_order_stock_sysid = fake_cancel - try: - seq = trader.cancel_order_stock_sysid_async("acct", "SH", "sys-1") - finally: - trader.cancel_order_stock_sysid = original - - self.assertGreater(seq, 0) - self.assertEqual(len(cb.cancel_async_responses), 1) - resp = cb.cancel_async_responses[0] - self.assertTrue(resp.success) - self.assertEqual(resp.order_sys_id, "sys-1") - self.assertEqual(resp.account_id, "acct") - self.assertEqual(resp.seq, seq) - - def test_connect_and_subscribe_fire_account_status(self): - trader, cb = self._trader() - trader.client.account_id = "acct" - # connect() calls ping via RPC — stub it. - trader.client.call = lambda *a, **k: {"ok": True} - trader.connect() - trader.subscribe("acct") - - self.assertEqual(len(cb.account_statuses), 2) - status = cb.account_statuses[0] - self.assertEqual(status.account_id, "acct") - self.assertEqual(status.account_type, "STOCK") - self.assertEqual(status.status, 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_formula_server.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_formula_server.py deleted file mode 100644 index 3ea1bd9..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_formula_server.py +++ /dev/null @@ -1,340 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader import formula_server as fs - - -class BsonCodecTest(unittest.TestCase): - """The built-in codec is the no-dependency path; it must round-trip every - type this wire carries and stay byte-compatible with pymongo's bson.""" - - def _round_trip(self, document): - return fs._decode_document(fs._encode_document(document.items()), 0)[0] - - def test_round_trips_scalars(self): - doc = {"s": "平安银行", "i": 42, "big": 2 ** 40, "f": 10.29, "t": True, "f2": False, "n": None} - - self.assertEqual(self._round_trip(doc), doc) - - def test_round_trips_nested_containers(self): - doc = {"func": "getMarketData", "params": {"fields": ["close", "volume"], "count": -1}} - - self.assertEqual(self._round_trip(doc), doc) - - def test_round_trips_the_actual_request_envelope(self): - doc = { - "func": "getMarketData", - "params": { - "fields": ["close"], - "stockCodes": ["000001.SZ"], - "startTime": "", - "endTime": "", - "period": "1d", - "dividendType": "none", - "count": 3, - }, - } - - self.assertEqual(self._round_trip(doc), doc) - - def test_array_order_is_preserved(self): - doc = {"codes": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]} - - self.assertEqual(self._round_trip(doc)["codes"], doc["codes"]) - - def test_matches_pymongo_bson_when_available(self): - try: - import bson - except ImportError: - self.skipTest("pymongo bson not installed") - doc = {"func": "getLastVolume", "params": {"stockCode": "000001.SZ", "n": 1.5}} - - self.assertEqual(fs._encode_document(doc.items()), bson.BSON.encode(doc)) - self.assertEqual(fs._decode_document(bson.BSON.encode(doc), 0)[0], doc) - - def test_unsupported_type_is_rejected(self): - with self.assertRaises(TypeError): - fs._encode_document({"bad": object()}.items()) - - -class AddressResolutionTest(unittest.TestCase): - def test_reads_port_from_formulaserver_ini(self): - import tempfile - - root = tempfile.mkdtemp() - ini_dir = os.path.join(root, "config", "formulaserver") - os.makedirs(ini_dir) - with open(os.path.join(ini_dir, "formulaserver.ini"), "w") as handle: - handle.write("[server_formula]\naddress = 0.0.0.0:58600\n") - - self.assertEqual(fs.read_formulaserver_port(root), 58600) - self.assertEqual(fs.resolve_address({"qmt_root": root}), ("127.0.0.1", 58600)) - - def test_missing_ini_falls_back_to_default_port(self): - self.assertIsNone(fs.read_formulaserver_port(os.path.join(ROOT, "no-such-dir"))) - host, port = fs.resolve_address({"qmt_root": os.path.join(ROOT, "no-such-dir")}) - - self.assertEqual((host, port), ("127.0.0.1", fs.DEFAULT_PORT)) - - def test_explicit_config_wins(self): - self.assertEqual( - fs.resolve_address({"host": "10.0.0.5", "port": 59999}), ("10.0.0.5", 59999) - ) - - -class FakeClient(object): - host = "127.0.0.1" - port = 58600 - - def __init__(self, responses=None, error=None): - self.responses = responses or {} - self.error = error - self.calls = [] - - def request(self, func, params=None): - self.calls.append((func, dict(params or {}))) - if self.error is not None: - raise self.error - return self.responses.get(func, {"result": None}) - - def close(self): - pass - - -class ParamTranslationTest(unittest.TestCase): - def _router(self, responses=None, error=None): - client = FakeClient(responses=responses, error=error) - return fs.FormulaServerRouter(client=client), client - - def test_instrument_aliases_the_misspelled_volume_fields(self): - """FormulaServer ships FloatVolumn/TotalVolumn; the xtdata SDK spells - them FloatVolume/TotalVolume. Downstream reads the SDK spelling.""" - router, _ = self._router( - {"getInstrumentDetail": {"result": {"FloatVolumn": 1.0, "TotalVolumn": 2.0}}} - ) - - out = router.call("get_instrument", {"code": "000001.SZ"}) - - self.assertEqual(out["FloatVolume"], 1.0) - self.assertEqual(out["TotalVolume"], 2.0) - self.assertEqual(out["FloatVolumn"], 1.0) # raw key still present - - def test_sector_normalizes_the_minus_one_sentinel(self): - router, client = self._router({"getStockListInSector": {"result": ["600000.SH"]}}) - - router.call("get_stock_list_in_sector", {"sector_name": "沪深300", "real_timetag": -1}) - - self.assertEqual(client.calls[0][1], {"sectorName": "沪深300", "realtime": 0}) - - def test_market_data_refuses_adjusted_bars(self): - """dividendType is not honoured by the server; serving an adjusted - request from here would hand back unadjusted prices silently.""" - router, client = self._router({"getMarketData": {"result": []}}) - - for dividend_type in ("front", "back", "front_ratio"): - with self.assertRaises(fs.Unroutable): - router.call( - "get_market_data_ex", - { - "field_list": ["close"], - "stock_list": ["000001.SZ"], - "dividend_type": dividend_type, - }, - ) - self.assertEqual(client.calls, []) - - def test_market_data_allows_unadjusted(self): - router, client = self._router({"getMarketData": {"result": []}}) - - router.call( - "get_market_data_ex", - {"field_list": ["close"], "stock_list": ["000001.SZ"], "dividend_type": "none"}, - ) - - self.assertEqual(client.calls[0][1]["dividendType"], "none") - - def test_market_data_translates_flat_wire_shape(self): - router, _ = self._router( - { - "getMarketData": { - "result": [ - "000001.SZ", - ["20260703", ["close", 10.29, "volume", 863327.0]], - "600000.SH", - ["20260703", ["close", 8.69, "volume", 695133.0]], - ] - } - } - ) - - out = router.call( - "get_market_data_ex", - {"field_list": ["close", "volume"], "stock_list": ["000001.SZ", "600000.SH"]}, - ) - - self.assertEqual(out["000001.SZ"]["columns"], ["stime", "close", "volume"]) - self.assertEqual( - out["000001.SZ"]["records"], - [{"stime": "20260703", "close": 10.29, "volume": 863327.0}], - ) - self.assertEqual(out["600000.SH"]["records"][0]["close"], 8.69) - - def test_market_data_keeps_requested_codes_with_no_bars(self): - router, _ = self._router({"getMarketData": {"result": []}}) - - out = router.call( - "get_market_data_ex", - {"field_list": ["close"], "stock_list": ["000001.SZ", "600000.SH"]}, - ) - - self.assertEqual(sorted(out), ["000001.SZ", "600000.SH"]) - self.assertEqual(out["000001.SZ"]["records"], []) - - def test_missing_required_params_is_unroutable_not_a_crash(self): - router, client = self._router() - - with self.assertRaises(fs.Unroutable): - router.call("get_instrument", {}) - self.assertEqual(client.calls, []) - - -class FallbackBehaviourTest(unittest.TestCase): - def test_unmapped_method_is_not_supported(self): - router = fs.FormulaServerRouter(client=FakeClient()) - - self.assertFalse(router.supports("get_asset")) - self.assertFalse(router.supports("submit_order")) - self.assertFalse(router.supports("get_full_tick")) - - def test_trading_dates_and_dividends_stay_on_rpc(self): - """Their FormulaServer params mean something different from ours.""" - router = fs.FormulaServerRouter(client=FakeClient()) - - self.assertFalse(router.supports("get_trading_dates")) - self.assertFalse(router.supports("get_divid_factors")) - self.assertFalse(router.supports("get_risk_free_rate")) - - def test_transport_failure_trips_the_cooldown(self): - router = fs.FormulaServerRouter( - client=FakeClient(error=fs.FormulaServerUnavailable("down")), - failure_cooldown_seconds=60, - ) - - with self.assertRaises(fs.Unroutable): - router.call("get_last_volume", {"stock": "000001.SZ"}) - # Breaker is open: no further attempts until the cooldown expires. - self.assertFalse(router.supports("get_last_volume")) - - def test_method_not_found_disables_only_that_method(self): - router = fs.FormulaServerRouter( - client=FakeClient( - error=fs.FormulaServerError("nope", error_id=fs.ERROR_METHOD_NOT_FOUND) - ) - ) - - with self.assertRaises(fs.Unroutable): - router.call("get_main_contract", {"code_market": "IF00.IF"}) - - self.assertFalse(router.supports("get_main_contract")) - self.assertTrue(router.supports("get_last_volume")) # breaker not tripped - - def test_disabled_router_supports_nothing(self): - router = fs.build_router({"enabled": False}) - - for method in fs.SUPPORTED_METHODS: - self.assertFalse(router.supports(method)) - - def test_enabled_accepts_string_flags(self): - self.assertFalse(fs.build_router({"enabled": "false"}).enabled) - self.assertFalse(fs.build_router({"enabled": "0"}).enabled) - self.assertTrue(fs.build_router({"enabled": "true", "port": 1}).enabled) - - -class ClientCallIntegrationTest(unittest.TestCase): - """BigQmtRpcClient.call must prefer the router and fall back cleanly.""" - - def _client(self, router): - from bigqmt_signal_trader.xtquant_compat import BigQmtRpcClient - - client = BigQmtRpcClient(account_id="acct") - client._formula_router_instance = router - return client - - def test_routed_method_never_touches_rpc(self): - router = fs.FormulaServerRouter( - client=FakeClient({"getLastVolume": {"result": 123.0}}) - ) - client = self._client(router) - - def explode(*args, **kwargs): - raise AssertionError("RPC must not be used for a routed method") - - client._transport = explode - - self.assertEqual(client.call("get_last_volume", {"stock": "000001.SZ"}), 123.0) - - def test_unroutable_falls_back_to_rpc(self): - router = fs.FormulaServerRouter( - client=FakeClient(error=fs.FormulaServerUnavailable("down")) - ) - client = self._client(router) - calls = [] - - class FakeTransport: - def send_request(self, request, timeout): - calls.append(request["method"]) - return {"ok": True, "data": "from-rpc"} - - client._transport = lambda: FakeTransport() - - self.assertEqual(client.call("get_last_volume", {"stock": "000001.SZ"}), "from-rpc") - self.assertEqual(calls, ["get_last_volume"]) - - def test_unmapped_method_goes_straight_to_rpc(self): - router = fs.FormulaServerRouter(client=FakeClient()) - client = self._client(router) - calls = [] - - class FakeTransport: - def send_request(self, request, timeout): - calls.append(request["method"]) - return {"ok": True, "data": {"cash": 1.0}} - - client._transport = lambda: FakeTransport() - - self.assertEqual(client.call("get_asset", {}), {"cash": 1.0}) - self.assertEqual(calls, ["get_asset"]) - - def test_routed_dataframe_payload_is_restored_like_rpc(self): - router = fs.FormulaServerRouter( - client=FakeClient( - { - "getMarketData": { - "result": ["000001.SZ", ["20260703", ["close", 10.29]]] - } - } - ) - ) - client = self._client(router) - - out = client.call( - "get_market_data_ex", {"field_list": ["close"], "stock_list": ["000001.SZ"]} - ) - - frame = out["000001.SZ"] - # _restore_jsonable rebuilds a DataFrame when pandas is present, and - # degrades to the record list otherwise — same as the RPC path. - if hasattr(frame, "columns"): - self.assertEqual(list(frame.columns), ["stime", "close"]) - self.assertEqual(frame.iloc[0]["close"], 10.29) - else: - self.assertEqual(frame, [{"stime": "20260703", "close": 10.29}]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_full_tick_cache.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_full_tick_cache.py deleted file mode 100644 index a71d87c..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_full_tick_cache.py +++ /dev/null @@ -1,114 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.full_tick_cache import ( - full_tick_demand_key, - full_tick_request_id, - read_full_tick_cache, - refresh_full_tick_cache, - request_full_tick_cache, -) - - -class FakeRedis: - def __init__(self): - self.hashes = {} - self.kv = {} - self.deleted = [] - self.expired = [] - - def hset(self, key, field, value): - self.hashes.setdefault(key, {})[field] = value - return 1 - - def hgetall(self, key): - return self.hashes.get(key, {}) - - def hdel(self, key, field): - self.deleted.append((key, field)) - self.hashes.setdefault(key, {}).pop(field, None) - return 1 - - def expire(self, key, seconds): - self.expired.append((key, seconds)) - return True - - def setex(self, key, seconds, value): - self.kv[key] = value - self.expired.append((key, seconds)) - return True - - def get(self, key): - return self.kv.get(key) - - -class FakeContext: - def __init__(self): - self.calls = [] - - def get_full_tick(self, codes): - self.calls.append(list(codes)) - return {codes[0]: {"lastPrice": 10.0, "bidPrice": [9.9], "askPrice": [10.1]}} - - -class FullTickCacheTest(unittest.TestCase): - def test_request_then_refresh_writes_fresh_snapshot(self): - redis_client = FakeRedis() - context = FakeContext() - - demand = request_full_tick_cache(redis_client, "acct", ["600000"], demand_ttl_seconds=10) - refreshed = refresh_full_tick_cache(redis_client, context, "acct", cache_ttl_seconds=10) - ticks = read_full_tick_cache(redis_client, "acct", ["600000.SH"], max_age_seconds=10) - - self.assertEqual(demand["codes"], ["600000.SH"]) - self.assertEqual(refreshed, 1) - self.assertEqual(context.calls, [["600000.SH"]]) - self.assertEqual(ticks["600000.SH"]["lastPrice"], 10.0) - - def test_expired_demand_is_removed_without_refreshing(self): - redis_client = FakeRedis() - context = FakeContext() - key = full_tick_demand_key("acct") - request_id = full_tick_request_id(["600000.SH"]) - redis_client.hset( - key, - request_id, - '{"request_id":"%s","codes":["600000.SH"],"requested_at_ts":1,"expires_at_ts":1}' % request_id, - ) - - refreshed = refresh_full_tick_cache(redis_client, context, "acct", cache_ttl_seconds=10) - - self.assertEqual(refreshed, 0) - self.assertEqual(context.calls, []) - self.assertIn((key, request_id), redis_client.deleted) - - def test_refresh_kind_symbol_skips_market_demands(self): - redis_client = FakeRedis() - context = FakeContext() - request_full_tick_cache(redis_client, "acct", ["600000"], demand_ttl_seconds=10) - request_full_tick_cache(redis_client, "acct", ["SH", "SZ"], demand_ttl_seconds=10) - - refreshed = refresh_full_tick_cache(redis_client, context, "acct", cache_ttl_seconds=10, kind="symbol") - - self.assertEqual(refreshed, 1) - self.assertEqual(context.calls, [["600000.SH"]]) - - def test_refresh_kind_market_skips_symbol_demands(self): - redis_client = FakeRedis() - context = FakeContext() - request_full_tick_cache(redis_client, "acct", ["600000"], demand_ttl_seconds=10) - request_full_tick_cache(redis_client, "acct", ["SH", "SZ"], demand_ttl_seconds=10) - - refreshed = refresh_full_tick_cache(redis_client, context, "acct", cache_ttl_seconds=10, kind="market") - - self.assertEqual(refreshed, 1) - self.assertEqual(context.calls, [["SH", "SZ"]]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_local_cache.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_local_cache.py deleted file mode 100644 index f17f474..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_local_cache.py +++ /dev/null @@ -1,348 +0,0 @@ -import os -import shutil -import sys -import tempfile -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.local_cache import LocalMarketCache - - -def _has_pyarrow(): - try: - import pyarrow # noqa: F401 - - return True - except Exception: - return False - - -class LocalMarketCacheTest(unittest.TestCase): - def setUp(self): - self.dir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.dir, ignore_errors=True) - - def test_write_read_merge_dedupe(self): - import pandas as pd - - c = LocalMarketCache(self.dir) - c.write("600000.SH", "1d", pd.DataFrame({"stime": ["20260101", "20260102"], "close": [1.0, 2.0]})) - # overlapping second write: 20260102 should be replaced (keep last), 20260103 appended - c.write("600000.SH", "1d", pd.DataFrame({"stime": ["20260102", "20260103"], "close": [2.5, 3.0]})) - - df = c.read("600000.SH", "1d") - self.assertEqual(list(df["stime"]), ["20260101", "20260102", "20260103"]) - self.assertEqual(df[df["stime"] == "20260102"]["close"].iloc[0], 2.5) - - def test_range_and_count_filters(self): - import pandas as pd - - c = LocalMarketCache(self.dir) - c.write("X", "1d", pd.DataFrame({"stime": ["20260101", "20260102", "20260103"], "close": [1, 2, 3]})) - - self.assertEqual(list(c.read("X", "1d", start_time="20260102")["stime"]), ["20260102", "20260103"]) - self.assertEqual(list(c.read("X", "1d", end_time="20260102")["stime"]), ["20260101", "20260102"]) - self.assertEqual(list(c.read("X", "1d", count=1)["stime"]), ["20260103"]) - self.assertIsNone(c.read("MISSING", "1d")) - self.assertEqual(c.covered("X", "1d"), ("20260101", "20260103", 3)) - - def test_drops_zero_fill_placeholder_rows(self): - import pandas as pd - - c = LocalMarketCache(self.dir) - df = pd.DataFrame( - {"stime": ["20200101", "20200102", "20260701"], "close": [0.0, 0.0, 8.65], "open": [0.0, 0.0, 8.58]} - ) - c.write("X", "1d", df) - self.assertEqual(list(c.read("X", "1d")["stime"]), ["20260701"]) # 0-fill dropped - - # an all-placeholder write must not create/overwrite a cache file - self.assertEqual(c.write("Y", "1d", pd.DataFrame({"stime": ["20200101"], "close": [0.0]})), 0) - self.assertIsNone(c.read("Y", "1d")) - - def test_dividend_type_keeps_separate_caches(self): - import pandas as pd - - c = LocalMarketCache(self.dir) - c.write("X", "1d", pd.DataFrame({"stime": ["20260101"], "close": [10.0]}), dividend_type="none") - c.write("X", "1d", pd.DataFrame({"stime": ["20260101"], "close": [9.0]}), dividend_type="front") - - self.assertEqual(c.read("X", "1d", dividend_type="none")["close"].iloc[0], 10.0) - self.assertEqual(c.read("X", "1d", dividend_type="front")["close"].iloc[0], 9.0) - self.assertIsNone(c.read("X", "1d", dividend_type="back")) - - def test_pickle_format_roundtrip(self): - import pandas as pd - - c = LocalMarketCache(self.dir, fmt="pkl") - c.write("X", "1d", pd.DataFrame({"stime": ["20260101", "20260102"], "close": [1.0, 2.0]})) - self.assertTrue(c.path("X", "1d").endswith(".pkl")) - self.assertEqual(list(c.read("X", "1d")["close"]), [1.0, 2.0]) - - @unittest.skipUnless(_has_pyarrow(), "pyarrow not installed") - def test_parquet_format_roundtrip(self): - import pandas as pd - - c = LocalMarketCache(self.dir, fmt="parquet") - c.write("X", "1d", pd.DataFrame({"stime": ["20260101", "20260102"], "close": [1.0, 2.0]})) - self.assertTrue(c.path("X", "1d").endswith(".parquet")) - self.assertEqual(list(c.read("X", "1d")["close"]), [1.0, 2.0]) - - @unittest.skipUnless(_has_pyarrow(), "pyarrow not installed") - def test_migrates_pickle_to_parquet(self): - import pandas as pd - - LocalMarketCache(self.dir, fmt="pkl").write("X", "1d", pd.DataFrame({"stime": ["20260101"], "close": [1.0]})) - pq = LocalMarketCache(self.dir, fmt="parquet") - self.assertEqual(list(pq.read("X", "1d")["close"]), [1.0]) # reads the old pkl - pq.write("X", "1d", pd.DataFrame({"stime": ["20260102"], "close": [2.0]})) - self.assertTrue(os.path.isfile(pq.path("X", "1d"))) # parquet now exists - self.assertFalse(os.path.isfile(pq.path("X", "1d")[:-8] + ".pkl")) # old pkl removed - self.assertEqual(list(pq.read("X", "1d")["close"]), [1.0, 2.0]) # merged across formats - - -class FakeClient: - def __init__(self, cache_dir, fallback_rpc=False): - self.account_id = "acct" - self.calls = [] - self.call_params = [] - self.local_cache_config = {"enabled": True, "dir": cache_dir, "fallback_rpc": fallback_rpc} - - def _redis(self): - return None - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - self.calls.append(method) - self.call_params.append((method, params)) - if method == "get_market_data_ex": - import pandas as pd - - codes = (params or {}).get("stock_list") or [] - return {c: pd.DataFrame({"stime": ["20260626", "20260629"], "close": [8.76, 8.73]}) for c in codes} - if method == "download_history_data2": - # Server-side raw download (raw bars + dividend factors). - return True - raise AssertionError("unexpected rpc: %s" % method) - - -class LocalCacheClientTest(unittest.TestCase): - def setUp(self): - self.dir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.dir, ignore_errors=True) - - def _xt(self, fallback_rpc=False): - from bigqmt_signal_trader.xtquant_compat import BigQmtXtData - - return BigQmtXtData(FakeClient(self.dir, fallback_rpc=fallback_rpc)) - - def test_download_caches_then_get_local_reads_without_rpc(self): - xt = self._xt() - progress = [] - res = xt.download_history_data2(["600000.SH", "000001.SZ"], "1d", callback=lambda d: progress.append(d)) - - self.assertEqual(res, {"finished": 2, "total": 2}) - self.assertEqual(len(progress), 2) - self.assertEqual(progress[-1]["stockcode"], "000001.SZ") - self.assertEqual(progress[-1]["finished"], 2) - calls_after_download = list(xt.client.calls) - - data = xt.get_local_data(stock_list=["600000.SH", "000001.SZ"], period="1d") - - self.assertIn("600000.SH", data) - self.assertIn("000001.SZ", data) - self.assertEqual(list(data["600000.SH"]["close"]), [8.76, 8.73]) - # get_local_data must NOT issue any further RPC — pure local read. - self.assertEqual(xt.client.calls, calls_after_download) - - def test_get_market_data_ex_caches_through(self): - xt = self._xt() - # a plain live read must also populate the cache (cache-through) - xt.get_market_data_ex(field_list=["close"], stock_list=["600000.SH"], period="1d") - n = len(xt.client.calls) - - data = xt.get_local_data(stock_list=["600000.SH"], period="1d") - self.assertIn("600000.SH", data) - self.assertEqual(len(xt.client.calls), n) # served from cache, no extra RPC - - def test_get_local_miss_returns_empty_and_no_rpc(self): - xt = self._xt() - data = xt.get_local_data(stock_list=["600000.SH"], period="1d") - self.assertEqual(data, {}) - self.assertEqual(xt.client.calls, []) - - def test_get_local_fallback_rpc_fetches_and_caches(self): - xt = self._xt(fallback_rpc=True) - data = xt.get_local_data(stock_list=["600000.SH"], period="1d") - self.assertIn("600000.SH", data) - self.assertIn("get_market_data_ex", xt.client.calls) # fetched on miss - # second read is served from cache — no new RPC - n = len(xt.client.calls) - xt.get_local_data(stock_list=["600000.SH"], period="1d") - self.assertEqual(len(xt.client.calls), n) - - -class AdjustedDownloadTest(unittest.TestCase): - """Adjusted (front/back) downloads must trigger the server-side raw - download FIRST: Big QMT computes adjusted bars from raw bars + dividend - factors, and without the server-side download the adjusted result is - all zeros (verified live with 600654.SH).""" - - def setUp(self): - self.dir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.dir, ignore_errors=True) - - def _xt(self): - from bigqmt_signal_trader.xtquant_compat import BigQmtXtData - - return BigQmtXtData(FakeClient(self.dir)) - - def test_front_download_triggers_server_side_raw_download_first(self): - xt = self._xt() - xt.download_history_data2(["600000.SH"], "1d", start_time="20200101", dividend_type="front") - - # The server-side raw download must run BEFORE the adjusted pull. - method_calls = [m for m, _ in xt.client.call_params] - self.assertIn("download_history_data2", method_calls) - self.assertIn("get_market_data_ex", method_calls) - self.assertLess( - method_calls.index("download_history_data2"), - method_calls.index("get_market_data_ex"), - "server-side raw download must precede the adjusted pull", - ) - # The raw download carries the same codes/period/window. - raw_call = next(p for m, p in xt.client.call_params if m == "download_history_data2") - self.assertEqual(raw_call["stock_list"], ["600000.SH"]) - self.assertEqual(raw_call["period"], "1d") - self.assertEqual(raw_call["start_time"], "20200101") - - def test_none_download_still_triggers_server_side_download(self): - """issue #47: an unadjusted download used to skip the server RPC and - only read what Big QMT already had -- a no-op that still reported - {finished: N}. xtdata semantics are "populate the local QMT store", and - callers (FormulaServer, get_local_data) depend on that actually happening.""" - xt = self._xt() - xt.download_history_data2(["600000.SH"], "1d", dividend_type="none") - - method_calls = [m for m, _ in xt.client.call_params] - self.assertIn("download_history_data2", method_calls) - self.assertIn("get_market_data_ex", method_calls) - self.assertLess( - method_calls.index("download_history_data2"), - method_calls.index("get_market_data_ex"), - "the download must precede the pull, or the pull reads stale data", - ) - raw_call = next(p for m, p in xt.client.call_params if m == "download_history_data2") - self.assertEqual(raw_call["stock_list"], ["600000.SH"]) - self.assertEqual(raw_call["period"], "1d") - - def test_none_download_survives_server_download_failure(self): - """Same best-effort contract the adjusted path already had: a deployment - without the QMT global must still get its bars.""" - xt = self._xt() - original_call = xt.client.call - - def failing_download(method, params=None, account_id=None, timeout_seconds=None): - if method == "download_history_data2": - raise RuntimeError("global not available") - return original_call(method, params, account_id=account_id, timeout_seconds=timeout_seconds) - - xt.client.call = failing_download - result = xt.download_history_data2(["600000.SH"], "1d", dividend_type="none") - - self.assertEqual(result["finished"], 1) - self.assertIn("get_market_data_ex", [m for m, _ in xt.client.call_params]) - - def test_front_download_survives_server_download_failure(self): - # Deployments without the QMT global must still get the adjusted pull - # (best-effort raw download, never fatal). - xt = self._xt() - original_call = xt.client.call - - def failing_download(method, params=None, account_id=None, timeout_seconds=None): - if method == "download_history_data2": - raise RuntimeError("global not available") - return original_call(method, params, account_id=account_id, timeout_seconds=timeout_seconds) - - xt.client.call = failing_download - result = xt.download_history_data2(["600000.SH"], "1d", dividend_type="front") - self.assertEqual(result, {"finished": 1, "total": 1}) # adjusted pull still ran - - -class _AllZeroThenRealClient(FakeClient): - """First adjusted get_market_data_ex returns all-zero bars (server lacks - raw data); after a server-side raw download, subsequent pulls are real.""" - - def __init__(self, cache_dir): - super(_AllZeroThenRealClient, self).__init__(cache_dir) - self._downloaded = False - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - self.calls.append(method) - self.call_params.append((method, params)) - import pandas as pd - - if method == "download_history_data2": - self._downloaded = True - return True - if method == "get_market_data_ex": - codes = (params or {}).get("stock_list") or [] - if self._downloaded: - return {c: pd.DataFrame({"stime": ["20260626", "20260629"], "close": [8.76, 8.73]}) for c in codes} - # all-zero symptom: head zeros, last bar live - return {c: pd.DataFrame({"stime": ["20260626", "20260629"], "close": [0.0, 8.73]}) for c in codes} - raise AssertionError("unexpected rpc: %s" % method) - - -class AdjustedReadSelfHealTest(unittest.TestCase): - """Reading adjusted bars that come back all-zero must self-heal: - trigger a server-side raw download, wait, and retry once.""" - - def setUp(self): - self.dir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.dir, ignore_errors=True) - - def _xt(self): - from bigqmt_signal_trader.xtquant_compat import BigQmtXtData - - return BigQmtXtData(_AllZeroThenRealClient(self.dir)) - - def test_front_read_self_heals_all_zero_to_real(self): - xt = self._xt() - data = xt.get_market_data_ex( - field_list=["close"], stock_list=["600000.SH"], period="1d", - dividend_type="front", - ) - # After self-heal the retry returns real (non-zero) bars. - self.assertEqual(list(data["600000.SH"]["close"]), [8.76, 8.73]) - # The heal path must have triggered a server-side raw download. - method_calls = [m for m, _ in xt.client.call_params] - self.assertIn("download_history_data2", method_calls) - # get_market_data_ex called twice: initial all-zero pull + retry. - self.assertEqual(method_calls.count("get_market_data_ex"), 2) - - def test_none_read_does_not_self_heal(self): - xt = self._xt() - data = xt.get_market_data_ex( - field_list=["close"], stock_list=["600000.SH"], period="1d", - dividend_type="none", - ) - # none returns whatever the server sent (no heal, single pull). - self.assertEqual(list(data["600000.SH"]["close"]), [0.0, 8.73]) - method_calls = [m for m, _ in xt.client.call_params] - self.assertNotIn("download_history_data2", method_calls) - self.assertEqual(method_calls.count("get_market_data_ex"), 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_models.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_models.py deleted file mode 100644 index 1cc2542..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_models.py +++ /dev/null @@ -1,59 +0,0 @@ -import datetime -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.models import SignalAction, SignalStatus, TradeSignal - - -def _base_payload(**kwargs): - payload = { - "signal_id": "sig-001", - "account_id": "test", - "action": "BUY", - "stock_code": "000001.SZ", - "amount": 100, - "price_type": "AUTO_LIMIT", - "created_at": "2026-06-30 09:31:00", - "expire_at": "2026-06-30 09:36:00", - "schema_version": 1, - } - payload.update(kwargs) - return payload - - -class TradeSignalModelTest(unittest.TestCase): - def test_trade_signal_requires_signal_id(self): - payload = _base_payload() - payload.pop("signal_id") - - with self.assertRaisesRegex(ValueError, "signal_id"): - TradeSignal.from_dict(payload) - - def test_trade_signal_parses_buy_payload(self): - signal = TradeSignal.from_dict(_base_payload()) - - self.assertEqual(signal.signal_id, "sig-001") - self.assertEqual(signal.action, SignalAction.BUY) - self.assertEqual(signal.status, SignalStatus.PENDING) - self.assertEqual(signal.amount, 100) - - def test_sell_signal_requires_amount_or_percentage(self): - payload = _base_payload(action="SELL", amount=None, percentage=None) - - with self.assertRaisesRegex(ValueError, "amount or percentage"): - TradeSignal.from_dict(payload) - - def test_expired_signal_is_detected(self): - signal = TradeSignal.from_dict(_base_payload(expire_at="2026-06-30 09:32:00")) - now = datetime.datetime(2026, 6, 30, 9, 32, 1) - - self.assertTrue(signal.is_expired(now)) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_dryrun.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_dryrun.py deleted file mode 100644 index c1f2677..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_dryrun.py +++ /dev/null @@ -1,36 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapters.order_dryrun import DryRunOrderGateway -from bigqmt_signal_trader.models import OrderRequest - - -class DryRunOrderGatewayTest(unittest.TestCase): - def test_submit_records_request_without_real_order(self): - gateway = DryRunOrderGateway() - request = OrderRequest( - signal_id="sig-001", - account_id="test", - action="BUY", - stock_code="000001.SZ", - volume=100, - price=10.02, - price_type="LIMIT", - strategy_name="bigqmt_signal_trader", - remark="web_buy_command", - ) - - result = gateway.submit(request) - - self.assertEqual(result.status, "DRY_RUN") - self.assertEqual(gateway.submitted[0], request) - self.assertIn("sig-001", result.user_order_id) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_time_and_chunking.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_time_and_chunking.py deleted file mode 100644 index e369a28..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_order_time_and_chunking.py +++ /dev/null @@ -1,250 +0,0 @@ -"""issue #48 (order_time missing) and issue #47 (get_market_data_ex chunking). - -Both are gaps between what Big QMT hands us and what the MiniQMT-shaped API -promises: the ORDER row carries a submit time we never read, and a wide -stock_list shares one RPC timeout so it either fits or loses everything. -""" - -import os -import sys -import time -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapters import order_bigqmt -from bigqmt_signal_trader.adapters.order_bigqmt import BigQmtOrderGateway, _order_time_seconds -from bigqmt_signal_trader.models import OrderSnapshot -from bigqmt_signal_trader.xtquant_compat import BigQmtXtData, BigQmtXtTrader, StockAccount - - -class Row(object): - def __init__(self, **kwargs): - self.__dict__.update(kwargs) - - -def _order_row(**overrides): - base = dict( - m_strOrderSysID="sys-1", - m_strRemark="tag-1", - m_strInstrumentID="601398", - m_strExchangeID="SH", - m_nOffsetFlag=48, - m_nVolumeTotalOriginal=100, - m_nVolumeTraded=0, - m_nOrderStatus=50, - m_dLimitPrice=7.66, - m_strStrategyName="s1", - ) - base.update(overrides) - return Row(**base) - - -def _gateway(rows): - return BigQmtOrderGateway( - context_info=None, - passorder_func=None, - cancel_func=None, - get_trade_detail_data_func=lambda acct, atype, dtype, sname="": ( - list(rows) if dtype == "ORDER" else [] - ), - ) - - -class OrderTimeParsingTest(unittest.TestCase): - def setUp(self): - del order_bigqmt._missing_order_time_reported[:] - - def _expected(self, text): - return int(time.mktime(time.strptime(text, "%Y%m%d%H%M%S"))) - - def test_parses_split_date_and_time(self): - row = _order_row(m_strInsertDate="20260819", m_strInsertTime="093015") - self.assertEqual(_order_time_seconds(row), self._expected("20260819093015")) - - def test_tolerates_separators(self): - row = _order_row(m_strInsertDate="2026-08-19", m_strInsertTime="09:30:15") - self.assertEqual(_order_time_seconds(row), self._expected("20260819093015")) - - def test_drops_sub_second_precision(self): - row = _order_row(m_strInsertDate="20260819", m_strInsertTime="09:30:15.123") - self.assertEqual(_order_time_seconds(row), self._expected("20260819093015")) - - def test_accepts_alternate_field_spellings(self): - for date_field, time_field in (("m_strInsertDate", "m_strInsertTime"), - ("m_strOrderDate", "m_strOrderTime"), - ("insert_date", "insert_time")): - row = _order_row(**{date_field: "20260819", time_field: "093015"}) - self.assertEqual(_order_time_seconds(row), self._expected("20260819093015"), - "%s/%s" % (date_field, time_field)) - - def test_passes_through_an_epoch_value(self): - stamp = self._expected("20260819093015") - self.assertEqual(_order_time_seconds(_order_row(m_strInsertTime=stamp)), stamp) - - def test_normalizes_millisecond_epoch(self): - stamp = self._expected("20260819093015") - self.assertEqual(_order_time_seconds(_order_row(m_strInsertTime=stamp * 1000)), stamp) - - def test_missing_fields_yield_zero_not_epoch_start(self): - """0 means "not reported"; 1970-01-01 would look like a real timestamp.""" - self.assertEqual(_order_time_seconds(_order_row()), 0) - - def test_missing_fields_report_what_the_row_actually_has(self): - import contextlib - import io as _io - - buffer = _io.StringIO() - with contextlib.redirect_stdout(buffer): - _order_time_seconds(_order_row(m_strSomethingElse="x")) - output = buffer.getvalue() - - self.assertIn("order_time not found", output) - self.assertIn("m_strSomethingElse", output) - - def test_missing_field_reported_once_not_per_row(self): - import contextlib - import io as _io - - buffer = _io.StringIO() - with contextlib.redirect_stdout(buffer): - for _ in range(5): - _order_time_seconds(_order_row()) - - self.assertEqual(buffer.getvalue().count("order_time not found"), 1) - - def test_unparseable_date_yields_zero(self): - self.assertEqual( - _order_time_seconds(_order_row(m_strInsertDate="oops", m_strInsertTime="093015")), 0) - - -class OrderSnapshotTest(unittest.TestCase): - def setUp(self): - del order_bigqmt._missing_order_time_reported[:] - - def test_defaults_keep_existing_positional_callers_working(self): - snapshot = OrderSnapshot("sys", "tag", "601398.SH", "BUY", 100, 0, "50") - self.assertEqual(snapshot.order_time, 0) - - def test_gateway_fills_order_time(self): - rows = [_order_row(m_strInsertDate="20260819", m_strInsertTime="093015")] - order = _gateway(rows).query_orders("acct", "")[0] - - self.assertEqual(order.order_time, - int(time.mktime(time.strptime("20260819093015", "%Y%m%d%H%M%S")))) - self.assertEqual(order.stock_code, "601398.SH") - - -class ClientOrderTimeTest(unittest.TestCase): - """The reported symptom: XtOrder.order_time simply is not there.""" - - def _trader(self, payload): - trader = BigQmtXtTrader(account_id="acct") - trader.client.call = lambda method, params=None, account_id=None, **kw: payload - return trader - - def test_order_time_is_exposed(self): - stamp = int(time.mktime(time.strptime("20260819093015", "%Y%m%d%H%M%S"))) - orders = self._trader([{ - "stock_code": "601398.SH", "action": "BUY", "order_sys_id": "sys-1", - "volume": 100, "order_time": stamp, - }]).query_stock_orders(StockAccount("acct")) - - self.assertEqual(orders[0].order_time, stamp) - - def test_missing_order_time_defaults_to_zero(self): - """A server predating this field must not raise AttributeError.""" - orders = self._trader([{ - "stock_code": "601398.SH", "action": "BUY", "order_sys_id": "sys-1", "volume": 100, - }]).query_stock_orders(StockAccount("acct")) - - self.assertEqual(orders[0].order_time, 0) - - -class ChunkingClient(object): - """Records the code count of each request, and can fail chosen codes.""" - - def __init__(self, fail_codes=(), max_codes_per_call=None): - self.account_id = "acct" - self.batches = [] - self.local_cache_config = {"enabled": False} - self.fail_codes = set(fail_codes) - self.max_codes_per_call = max_codes_per_call - - def _redis(self): - return None - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - codes = list((params or {}).get("stock_list") or []) - self.batches.append(codes) - if self.max_codes_per_call is not None and len(codes) > self.max_codes_per_call: - raise TimeoutError("rpc timeout: %d codes" % len(codes)) - if self.fail_codes & set(codes): - raise TimeoutError("rpc timeout") - import pandas as pd - - return {c: pd.DataFrame({"stime": ["20260819"], "close": [7.66]}) for c in codes} - - -class MarketDataChunkingTest(unittest.TestCase): - """issue #47 comment: one request carries every code and one timeout, so a - wide stock_list times out and loses the whole pull.""" - - def _xt(self, **kwargs): - return BigQmtXtData(ChunkingClient(**kwargs)) - - def test_wide_list_is_split(self): - xt = self._xt() - codes = ["C%03d.SH" % i for i in range(250)] - - data = xt.get_market_data_ex(stock_list=codes, period="1d") - - self.assertEqual([len(b) for b in xt.client.batches], [100, 100, 50]) - self.assertEqual(len(data), 250) - - def test_small_list_stays_a_single_request(self): - xt = self._xt() - xt.get_market_data_ex(stock_list=["600000.SH", "601398.SH"], period="1d") - - self.assertEqual(len(xt.client.batches), 1) - - def test_chunk_size_zero_restores_single_request(self): - xt = self._xt() - codes = ["C%03d.SH" % i for i in range(250)] - - xt.get_market_data_ex(stock_list=codes, period="1d", chunk_size=0) - - self.assertEqual(len(xt.client.batches), 1) - - def test_a_wide_pull_that_would_time_out_now_succeeds(self): - """The actual report: the server cannot answer 250 codes at once.""" - xt = self._xt(max_codes_per_call=120) - codes = ["C%03d.SH" % i for i in range(250)] - - data = xt.get_market_data_ex(stock_list=codes, period="1d") - - self.assertEqual(len(data), 250) - - def test_one_failed_batch_does_not_lose_the_others(self): - xt = self._xt(fail_codes=["C150.SH"]) - codes = ["C%03d.SH" % i for i in range(250)] - - data = xt.get_market_data_ex(stock_list=codes, period="1d") - - # The 100-code batch holding C150 is lost; the other 150 survive. - self.assertEqual(len(data), 150) - self.assertNotIn("C150.SH", data) - self.assertIn("C000.SH", data) - - def test_total_failure_raises_rather_than_returning_empty(self): - xt = self._xt(max_codes_per_call=0) - codes = ["C%03d.SH" % i for i in range(250)] - - with self.assertRaises(TimeoutError): - xt.get_market_data_ex(stock_list=codes, period="1d") - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_price_engine.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_price_engine.py deleted file mode 100644 index cef1928..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_price_engine.py +++ /dev/null @@ -1,41 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.price_engine import build_order_price - - -class FakeMarketDataProvider: - def get_ticks(self, codes): - return { - "000001.SZ": { - "lastPrice": 10.0, - "askPrice": [10.01, 10.02], - "bidPrice": [9.99, 9.98], - } - } - - def get_instrument(self, code): - return { - "InstrumentStatus": 0, - "UpStopPrice": 11.0, - "DownStopPrice": 9.0, - } - - -class PriceEngineTest(unittest.TestCase): - def test_auto_buy_price_uses_ask2_when_better_than_markup(self): - price = build_order_price(FakeMarketDataProvider(), "000001.SZ", "BUY") - self.assertEqual(price, 10.02) - - def test_auto_sell_price_uses_bid2_when_better_than_discount(self): - price = build_order_price(FakeMarketDataProvider(), "000001.SZ", "SELL") - self.assertEqual(price, 9.98) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_production_failures.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_production_failures.py deleted file mode 100644 index 8c5d3c0..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_production_failures.py +++ /dev/null @@ -1,148 +0,0 @@ -# coding: utf-8 -"""Unit tests that simulate real QMT failure modes (production edge cases). - -The existing unit tests use happy-path mocks (FakeMarketData always returns -{"close": [10.0]}). These tests instead simulate the failure modes users hit -in production — so they are caught by the unit suite, not in production: - - - get_positions / get_asset return EMPTY when QMT context is unbound - - query_orders returns [] because strategy_name filtering mismatched - - get_market_data_ex(dividend_type='front') returns ALL-ZERO bars when the - server has no raw data downloaded - - order_stock returns -1 (submit failed) -> must surface on_order_error - - get_trade_detail_data ORDER/DEAL returns empty in some contexts - -Each test asserts the CODE behaves correctly for that failure (degrades / -self-heals / reports) — not merely that the call returns *something*. -""" -import os -import sys -import unittest - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.models import PositionSnapshot, AssetSnapshot -from bigqmt_signal_trader.redis_rpc import BigQmtRpcHandlers -from bigqmt_signal_trader.adapters.order_dryrun import DryRunOrderGateway - - -# --------------------------------------------------------------------------- -# Fakes that simulate QMT failure modes -# --------------------------------------------------------------------------- - -class _EmptyPositionProvider: - """QMT context unbound -> POSITION/ACCOUNT queries return empty.""" - def get_positions(self, account_id): - return {} - - def get_asset(self, account_id): - return AssetSnapshot(account_id=account_id, cash=None, total_asset=None) - - -class _EmptyMarketData: - """QMT returns all-zero bars for adjusted reads with no raw data.""" - def get_market_data_ex(self, **kwargs): - import pandas as pd - return {"600654.SH": pd.DataFrame({"stime": ["20240101", "20240102"], "close": [0.0, 0.0]})} - - -class _EmptyOrderGateway(DryRunOrderGateway): - """get_trade_detail_data(ORDER/DEAL) returns empty in some QMT contexts.""" - def query_orders(self, account_id, strategy_name): - return [] - - def query_trades(self, account_id, strategy_name): - return [] - - -# --------------------------------------------------------------------------- -# Tests -# --------------------------------------------------------------------------- - -class ProductionFailureTest(unittest.TestCase): - def _handlers(self, market_data=None, position_provider=None, order_gateway=None, allow_order_methods=False): - return BigQmtRpcHandlers( - account_id="acct", - market_data=market_data or _EmptyMarketData(), - position_provider=position_provider or _EmptyPositionProvider(), - order_gateway=order_gateway or _EmptyOrderGateway(), - allow_order_methods=allow_order_methods, - ) - - # 1. 持仓查询返回空(上下文未绑定)—— 不应崩溃,返回空 dict - def test_get_positions_empty_is_graceful(self): - handlers = self._handlers() - result = handlers.handle("get_positions", {}) - # Empty dict is a valid graceful result, not an exception. - self.assertEqual(result, {}) - - def test_get_asset_empty_fields_are_none(self): - handlers = self._handlers() - result = handlers.handle("get_asset", {}) - # AssetSnapshot with None fields is valid (QMT context unbound). - self.assertIsInstance(result, AssetSnapshot) - self.assertIsNone(result.cash) - - # 2. 委托查询返回空(strategy_name 不匹配 / ORDER 上下文无数据) - def test_query_orders_empty_is_graceful(self): - handlers = self._handlers() - result = handlers.handle("query_orders", {}) - self.assertEqual(result, []) - - def test_query_trades_empty_is_graceful(self): - handlers = self._handlers() - result = handlers.handle("query_trades", {}) - self.assertEqual(result, []) - - # 3. 复权全 0 —— 服务端返回全 0 时,结果应能被识别(客户端自愈逻辑在 compat 层) - def test_front_market_data_all_zero_detectable(self): - handlers = self._handlers() - result = handlers.handle("get_market_data_ex", { - "field_list": ["close"], "stock_list": ["600654.SH"], - "period": "1d", "count": 2, "dividend_type": "front", - }) - df = result["600654.SH"] - closes = list(df["close"]) if hasattr(df, "columns") else df - # The handler must NOT silently fix this — it returns the zeros so the - # client self-heal logic (in xtquant_compat) can detect and retry. - self.assertTrue(all(c == 0.0 for c in closes), "expected all-zero bars from unready server") - - # 4. 下单失败(order_stock 返回 -1)—— 必须能感知,不是静默成功 - def test_submit_order_negative_one_means_failed(self): - class FailingGateway(DryRunOrderGateway): - def submit(self, request): - from bigqmt_signal_trader.models import OrderSubmitResult - return OrderSubmitResult(status="REJECTED", user_order_id="-1", order_sys_id=None, message="submit failed") - - handlers = self._handlers(order_gateway=FailingGateway(), allow_order_methods=True) - result = handlers.handle("submit_order", { - "stock_code": "600654.SH", "action": "BUY", "volume": 100, - "price": 3.0, "price_type": "LIMIT", "strategy_name": "test", - }) - # The result must expose the failure, not look like a success. - self.assertEqual(result.user_order_id, "-1") - self.assertEqual(result.status, "REJECTED") - - # 5. 委托/成交查询空 + strategy_name 为空字符串时应返回全部(Issue 修复验证) - def test_query_orders_empty_strategy_returns_all(self): - class TwoStrategyGateway(DryRunOrderGateway): - def query_orders(self, account_id, strategy_name): - # Simulate: only "" (empty) returns all; specific name returns subset - if strategy_name == "": - return ["order-A", "order-B", "order-C"] - if strategy_name == "rpc_test": - return ["order-A"] - return [] - - handlers = self._handlers(order_gateway=TwoStrategyGateway()) - # Default (no strategy_name) should pass "" and get all 3 - all_orders = handlers.handle("query_orders", {}) - self.assertEqual(len(all_orders), 3) - # Explicit filter - filtered = handlers.handle("query_orders", {"strategy_name": "rpc_test"}) - self.assertEqual(len(filtered), 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_qmt_launcher.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_qmt_launcher.py deleted file mode 100644 index 08bd837..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_qmt_launcher.py +++ /dev/null @@ -1,231 +0,0 @@ -# coding: utf-8 -"""qmt_launcher tests (issue #45). - -The property that matters most is directory scoping: this machine runs several -QMT installs side by side, so a name-only match would stop the wrong account's -terminal. Process enumeration is stubbed so the tests never touch real ones. -""" - -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader import qmt_launcher -from bigqmt_signal_trader.qmt_launcher import ( - QmtLauncherError, - close_qmt, - find_qmt_processes, - is_qmt_running, - resolve_install_dir, -) - - -LEMO = os.path.normpath("D:/qmt_lemo/bin.x64") -OTHER = os.path.normpath("D:/qmt_other/bin.x64") - -FAKE_PROCESSES = [ - (100, "XtItClient.exe", os.path.join(LEMO, "XtItClient.exe")), - (101, "miniquote.exe", os.path.join(LEMO, "miniquote.exe")), - (200, "XtItClient.exe", os.path.join(OTHER, "XtItClient.exe")), # another account - (300, "notepad.exe", os.path.join(LEMO, "notepad.exe")), # not ours - (400, "XtItClient.exe", ""), # unreadable path -] - - -class ProcessStub(object): - """Patch process enumeration and record what gets terminated.""" - - def __init__(self, processes=None): - self.processes = list(FAKE_PROCESSES if processes is None else processes) - self.terminated = [] - self._orig_iter = None - self._orig_term = None - self._orig_isdir = None - - def __enter__(self): - self._orig_iter = qmt_launcher._iter_processes - self._orig_term = qmt_launcher._terminate - self._orig_isdir = os.path.isdir - - def _terminate(pid, force=False): - self.terminated.append((pid, force)) - self.processes = [p for p in self.processes if p[0] != pid] - return True - - qmt_launcher._iter_processes = lambda: list(self.processes) - qmt_launcher._terminate = _terminate - os.path.isdir = lambda p: True - return self - - def __exit__(self, *exc): - qmt_launcher._iter_processes = self._orig_iter - qmt_launcher._terminate = self._orig_term - os.path.isdir = self._orig_isdir - return False - - -class ResolveInstallDirTest(unittest.TestCase): - def test_accepts_root_bin_and_exe_paths(self): - expected = os.path.normpath("D:/qmt_lemo/bin.x64") - orig = os.path.isdir - os.path.isdir = lambda p: os.path.basename(os.path.normpath(p)).lower() == "bin.x64" - try: - for given in ("D:/qmt_lemo", "D:/qmt_lemo/bin.x64", - "D:/qmt_lemo/bin.x64/XtItClient.exe"): - self.assertEqual(os.path.normpath(resolve_install_dir(given)), expected, given) - finally: - os.path.isdir = orig - - def test_strips_surrounding_quotes(self): - self.assertTrue(resolve_install_dir('"D:/qmt_lemo/bin.x64"').endswith("bin.x64")) - - def test_empty_is_rejected(self): - for value in ("", None, " "): - with self.assertRaises(QmtLauncherError): - resolve_install_dir(value) - - -class FindProcessesTest(unittest.TestCase): - def test_only_matches_the_requested_install(self): - with ProcessStub(): - pids = sorted(p[0] for p in find_qmt_processes("D:/qmt_lemo")) - self.assertEqual(pids, [100, 101]) # not 200 (other install) - - def test_other_install_is_found_independently(self): - with ProcessStub(): - pids = sorted(p[0] for p in find_qmt_processes("D:/qmt_other")) - self.assertEqual(pids, [200]) - - def test_ignores_unrelated_process_names(self): - with ProcessStub(): - names = [p[1] for p in find_qmt_processes("D:/qmt_lemo")] - self.assertNotIn("notepad.exe", names) - - def test_skips_processes_with_no_readable_path(self): - """Matching a QMT name with an unknown path would be a coin flip on - which install it belongs to -- skip rather than guess.""" - with ProcessStub(): - pids = [p[0] for p in find_qmt_processes("D:/qmt_lemo")] - self.assertNotIn(400, pids) - - def test_is_qmt_running_reflects_scope(self): - with ProcessStub(): - self.assertTrue(is_qmt_running("D:/qmt_lemo")) - self.assertFalse(is_qmt_running("D:/qmt_unused")) - - -class CloseQmtTest(unittest.TestCase): - def test_closes_only_the_requested_install(self): - with ProcessStub() as stub: - closed = close_qmt("D:/qmt_lemo") - self.assertEqual(closed, 2) - self.assertEqual(sorted(pid for pid, _ in stub.terminated), [100, 101]) - self.assertNotIn(200, [pid for pid, _ in stub.terminated]) - - def test_terminates_gracefully_before_forcing(self): - """A hard kill skips the terminal's data flush and truncates the - local K-line store.""" - with ProcessStub() as stub: - close_qmt("D:/qmt_lemo") - self.assertTrue(all(force is False for _, force in stub.terminated)) - - def test_escalates_to_force_when_terminate_is_ignored(self): - with ProcessStub() as stub: - def _stubborn(pid, force=False): - stub.terminated.append((pid, force)) - if force: - stub.processes = [p for p in stub.processes if p[0] != pid] - return True - - qmt_launcher._terminate = _stubborn - close_qmt("D:/qmt_lemo", timeout_seconds=6.0, force_after_seconds=1.0) - - self.assertTrue(any(force for _, force in stub.terminated)) - - def test_no_processes_is_not_an_error(self): - with ProcessStub(processes=[]): - self.assertEqual(close_qmt("D:/qmt_lemo"), 0) - - -class ReadinessTest(unittest.TestCase): - def test_wait_until_ready_raises_on_timeout(self): - """A scheduled restart must fail loudly, not hand the next step a - terminal that never came up.""" - orig = qmt_launcher.port_is_listening - qmt_launcher.port_is_listening = lambda *a, **k: False - try: - with self.assertRaises(QmtLauncherError): - qmt_launcher.wait_until_ready(port=1, timeout_seconds=0.3, poll_interval=0.1) - finally: - qmt_launcher.port_is_listening = orig - - def test_wait_until_ready_returns_elapsed_seconds(self): - orig = qmt_launcher.port_is_listening - qmt_launcher.port_is_listening = lambda *a, **k: True - try: - self.assertIsInstance( - qmt_launcher.wait_until_ready(port=1, timeout_seconds=5.0), float) - finally: - qmt_launcher.port_is_listening = orig - - -class OpenQmtTest(unittest.TestCase): - def _stub_spawn(self): - calls = [] - orig_spawn = qmt_launcher._spawn - orig_ready = qmt_launcher.wait_until_ready - qmt_launcher._spawn = lambda cmd, cwd=None, shell=False: calls.append((cmd, cwd, shell)) - qmt_launcher.wait_until_ready = lambda *a, **k: 1.0 - return calls, orig_spawn, orig_ready - - def test_linkmini_mode_passes_the_passwordless_flag(self): - calls, orig_spawn, orig_ready = self._stub_spawn() - orig_isdir, orig_isfile = os.path.isdir, os.path.isfile - os.path.isdir = lambda p: True - os.path.isfile = lambda p: p.lower().endswith("xtminiqmt.exe") - try: - qmt_launcher.open_qmt("D:/qmt_lemo", mode="linkmini") - finally: - qmt_launcher._spawn, qmt_launcher.wait_until_ready = orig_spawn, orig_ready - os.path.isdir, os.path.isfile = orig_isdir, orig_isfile - - self.assertEqual(len(calls), 1) - self.assertIn("linkMini", calls[0][0]) - - def test_login_mode_without_credentials_is_rejected(self): - calls, orig_spawn, orig_ready = self._stub_spawn() - orig_isdir, orig_isfile = os.path.isdir, os.path.isfile - os.path.isdir = lambda p: True - os.path.isfile = lambda p: True - try: - with self.assertRaises(QmtLauncherError): - qmt_launcher.open_qmt("D:/qmt_lemo", mode="login", credentials={}) - finally: - qmt_launcher._spawn, qmt_launcher.wait_until_ready = orig_spawn, orig_ready - os.path.isdir, os.path.isfile = orig_isdir, orig_isfile - - def test_unknown_mode_is_rejected(self): - orig = os.path.isdir - os.path.isdir = lambda p: True - try: - with self.assertRaises(QmtLauncherError): - qmt_launcher.open_qmt("D:/qmt_lemo", mode="teleport") - finally: - os.path.isdir = orig - - def test_bat_mode_requires_an_existing_bat(self): - orig = os.path.isdir - os.path.isdir = lambda p: True - try: - with self.assertRaises(QmtLauncherError): - qmt_launcher.open_qmt("D:/qmt_lemo", mode="bat", bat_path="D:/nope.bat") - finally: - os.path.isdir = orig - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_on_push_wiring.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_on_push_wiring.py deleted file mode 100644 index 51105b0..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_on_push_wiring.py +++ /dev/null @@ -1,172 +0,0 @@ -import os -import sys -import threading -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_push_channel import RedisQuotePushChannel -from bigqmt_signal_trader.quote_subscription_manager import ( - QuoteSourceAdapter, - QuoteSubscriptionManager, -) - - -class FakeQuoteSource(QuoteSourceAdapter): - """Captures the on_push callback so tests can fire big-QMT quote events.""" - - def __init__(self): - self.subscriptions = {} - self.unsubscribed = [] - self._next_handle = 0 - - def subscribe(self, codes, on_push): - self._next_handle += 1 - handle = self._next_handle - self.subscriptions[handle] = {"codes": list(codes), "on_push": on_push} - return handle - - def unsubscribe(self, handle): - self.unsubscribed.append(handle) - self.subscriptions.pop(handle, None) - - def fire(self, handle, data): - self.subscriptions[handle]["on_push"](data) - - -class RecordingPublisher: - def __init__(self): - self.calls = [] - self._lock = threading.Lock() - - def __call__(self, topic, data): - with self._lock: - self.calls.append((topic, data)) - - -class OnPushWiringTest(unittest.TestCase): - def test_on_push_forwards_to_publisher_with_combo_topic(self): - source = FakeQuoteSource() - publisher = RecordingPublisher() - manager = QuoteSubscriptionManager(source, on_push_publisher=publisher) - - result = manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - handle = next(iter(source.subscriptions)) - source.fire(handle, {"000001.SZ": {"lastPrice": 10.5}}) - - self.assertEqual(len(publisher.calls), 1) - topic, data = publisher.calls[0] - self.assertEqual(topic, result["topic"]) - self.assertEqual(data["000001.SZ"]["lastPrice"], 10.5) - - def test_on_push_ignored_without_publisher(self): - source = FakeQuoteSource() - manager = QuoteSubscriptionManager(source) # no publisher wired - manager.subscribe("clientA", "sub1", ["SH"]) - handle = next(iter(source.subscriptions)) - # Must not raise even though nothing consumes the push. - source.fire(handle, {"000001.SZ": {"lastPrice": 10.5}}) - - def test_on_push_concurrent_callbacks_are_serialized(self): - source = FakeQuoteSource() - publisher = RecordingPublisher() - manager = QuoteSubscriptionManager(source, on_push_publisher=publisher) - manager.subscribe("clientA", "sub1", ["SH"]) - handle = next(iter(source.subscriptions)) - - threads = [ - threading.Thread(target=source.fire, args=(handle, {"000001.SZ": {"n": i}})) - for i in range(20) - ] - for thread in threads: - thread.start() - for thread in threads: - thread.join() - - self.assertEqual(len(publisher.calls), 20) - self.assertTrue(all(topic == "SH" for topic, _ in publisher.calls)) - - def test_push_stops_after_unsubscribe(self): - source = FakeQuoteSource() - publisher = RecordingPublisher() - manager = QuoteSubscriptionManager(source, on_push_publisher=publisher) - manager.subscribe("clientA", "sub1", ["SH"]) - handle = next(iter(source.subscriptions)) - manager.unsubscribe("clientA", "sub1") - # Subscription torn down at the source; nothing left to fire. - self.assertNotIn(handle, source.subscriptions) - - def test_concurrent_subscribe_unsubscribe_reap_and_push(self): - # Hammer the manager from the RPC thread (subscribe/unsubscribe), the - # scheduler thread (reap) and the quote thread (on_push) at once. The - # shared combo/sub-index state must stay consistent: no KeyError, no - # publish to a torn-down combo, and the source ends fully unsubscribed. - source = FakeQuoteSource() - publisher = RecordingPublisher() - clock = [0.0] - manager = QuoteSubscriptionManager( - source, heartbeat_timeout_seconds=30.0, time_func=lambda: clock[0], - on_push_publisher=publisher, - ) - - errors = [] - - def churn(i): - try: - for n in range(30): - sub = "sub-%d-%d" % (i, n) - manager.subscribe("client-%d" % i, sub, ["SH"]) - handle = next(iter(source.subscriptions), None) - if handle is not None: - source.fire(handle, {"x": n}) - manager.unsubscribe("client-%d" % i, sub) - except Exception as exc: # noqa: BLE001 - surface any race as a failure - errors.append(exc) - - def reap(): - try: - for _ in range(30): - clock[0] += 1.0 - manager.reap_expired() - except Exception as exc: # noqa: BLE001 - errors.append(exc) - - workers = [threading.Thread(target=churn, args=(i,)) for i in range(4)] - workers.append(threading.Thread(target=reap)) - for w in workers: - w.start() - for w in workers: - w.join() - - self.assertEqual(errors, []) - self.assertEqual(len(source.subscriptions), 0) - - -class OnPushToRedisChannelTest(unittest.TestCase): - def test_manager_wired_to_real_channel_publishes(self): - class FakeRedis: - def __init__(self): - self.messages = {} - - def publish(self, channel, value): - self.messages.setdefault(channel, []).append(value) - return 1 - - redis_client = FakeRedis() - channel = RedisQuotePushChannel(redis_client, account_id="acct") - channel.start_publisher() - - source = FakeQuoteSource() - manager = QuoteSubscriptionManager(source, on_push_publisher=channel.publish) - manager.subscribe("clientA", "sub1", ["SH"]) - handle = next(iter(source.subscriptions)) - source.fire(handle, {"000001.SZ": {"lastPrice": 10.5}}) - - self.assertIn("bigqmt:quote_push:acct:SH", redis_client.messages) - channel.stop() - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_push_channel.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_push_channel.py deleted file mode 100644 index 7067a30..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_push_channel.py +++ /dev/null @@ -1,190 +0,0 @@ -import json -import os -import sys -import threading -import time -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_push_channel import ( - _HAS_MSGPACK, - RedisQuotePushChannel, - ZmqQuotePushChannel, - decode_push_payload, - encode_push_payload, -) - - -def _msgpack_packb(payload): - import msgpack - - return msgpack.packb(payload, use_bin_type=True) - - -class FakePubSub: - def __init__(self, redis_client): - self._redis = redis_client - self._channels = [] - self._closed = False - - def subscribe(self, *channels): - self._channels.extend(channels) - - def get_message(self, timeout=0.1): - # Pull one queued message for a subscribed channel, if any. - for _ in range(50): - for channel in self._channels: - queue = self._redis.messages.setdefault(channel, []) - if queue: - return {"type": "message", "channel": channel, "data": queue.pop(0)} - time.sleep(0.01) - return None - - def close(self): - self._closed = True - - -class FakeRedis: - def __init__(self): - self.messages = {} - - def publish(self, channel, value): - self.messages.setdefault(channel, []).append(value) - return 1 - - def pubsub(self, ignore_subscribe_messages=True): - return FakePubSub(self) - - -class PayloadCodecTest(unittest.TestCase): - def test_roundtrip(self): - payload = {"combo_key": "SH,SZ", "data": {"000001.SZ": {"lastPrice": 10.5}}, "ts": 1.5} - blob = encode_push_payload(payload) - self.assertEqual(decode_push_payload(blob), payload) - - def test_binary_blob(self): - # Wire encoding must be bytes (msgpack or utf-8 json), not str. - blob = encode_push_payload({"a": 1}) - self.assertIsInstance(blob, (bytes, bytearray)) - - def test_decode_json_wire_with_msgpack_available(self): - # 服务端未装 msgpack 时用 json 兜底编码;若客户端装了 msgpack, - # msgpack.unpackb 会把 json 文本首字节 '{'(0x7B) 当整数解析并抛 - # ExtraData("unpack(b) received extra data")——真实联调中触发。 - # decode 必须识别并回退到 json。 - payload = {"combo_key": "000001.SZ", "data": {"000001.SZ": {"lastPrice": 11.19}}} - wire = json.dumps(payload, ensure_ascii=False, default=str).encode("utf-8") - self.assertIsInstance(wire, bytes) - self.assertEqual(decode_push_payload(wire), payload) - - def test_decode_msgpack_wire_without_msgpack_available(self): - # 反向:服务端装了 msgpack、客户端未装(仅 json 可用)。msgpack 字节流 - # 通常不是合法 utf-8,json 解码会失败——此时应显式尝试 msgpack 解码, - # 而不是吞掉异常返回 None。 - payload = {"combo_key": "SH,SZ", "data": {"600000.SH": {"lastPrice": 9.9}}} - if not _HAS_MSGPACK: - self.skipTest("msgpack not installed on this client") - wire = _msgpack_packb(payload) - self.assertEqual(decode_push_payload(wire), payload) - - -class ZmqPushChannelTest(unittest.TestCase): - def test_pub_sub_roundtrip_and_topic_filter(self): - zmq = __import__("zmq") - ctx = zmq.Context.instance() - pub_addr = "inproc://quote-push-test-%d" % id(self) - - server = ZmqQuotePushChannel(bind_address=pub_addr, context=ctx) - server.start_publisher() - - received = [] - done = threading.Event() - - client = ZmqQuotePushChannel(connect_address=pub_addr, context=ctx) - - def on_msg(topic, data): - received.append((topic, data)) - done.set() - - client.start_subscriber(["SH,SZ"], on_msg) - try: - # Give the SUB a moment to connect + apply the subscription filter. - time.sleep(0.2) - server.publish("SH,SZ", {"000001.SZ": {"lastPrice": 10.5}}) - server.publish("SH", {"600000.SH": {"lastPrice": 9.9}}) # filtered out - self.assertTrue(done.wait(2.0), "subscriber did not receive the SH,SZ push") - finally: - client.stop() - server.stop() - - self.assertEqual(len(received), 1) - topic, data = received[0] - self.assertEqual(topic, "SH,SZ") - self.assertEqual(data["000001.SZ"]["lastPrice"], 10.5) - - def test_stop_from_foreign_thread_while_sub_active_does_not_crash(self): - """Regression: stop() used to close the SUB socket from the calling - thread while the sub thread was still polling it -> Windows ZMQ - signaler abort -> QMT process crash ("auto-exit"). The sub thread must - close its own socket; repeated stop/start (topic changes) must be safe.""" - zmq = __import__("zmq") - ctx = zmq.Context.instance() - pub_addr = "inproc://quote-push-stop-%d" % id(self) - - server = ZmqQuotePushChannel(bind_address=pub_addr, context=ctx) - server.start_publisher() - client = ZmqQuotePushChannel(connect_address=pub_addr, context=ctx) - try: - for i in range(5): - # Simulate _sync_subscriber_locked topic changes: start a - # subscriber, immediately stop it from this (foreign) thread. - client.start_subscriber(["T%d" % i], lambda t, d: None) - time.sleep(0.05) - client.stop() # must not raise / abort - finally: - client.stop() - server.stop() - # Reaching here without an abort/crash is the assertion. - self.assertTrue(True) - - -class RedisPushChannelTest(unittest.TestCase): - def test_pub_sub_roundtrip(self): - redis_client = FakeRedis() - server = RedisQuotePushChannel(redis_client, account_id="acct") - server.start_publisher() - - received = [] - done = threading.Event() - client = RedisQuotePushChannel(redis_client, account_id="acct") - - def on_msg(topic, data): - received.append((topic, data)) - done.set() - - client.start_subscriber(["SH,SZ"], on_msg) - try: - time.sleep(0.1) - server.publish("SH,SZ", {"000001.SZ": {"lastPrice": 10.5}}) - self.assertTrue(done.wait(2.0), "redis subscriber did not receive the push") - finally: - client.stop() - server.stop() - - self.assertEqual(received[0][0], "SH,SZ") - self.assertEqual(received[0][1]["000001.SZ"]["lastPrice"], 10.5) - - def test_channel_name_scoped_by_account(self): - redis_client = FakeRedis() - server = RedisQuotePushChannel(redis_client, account_id="acct") - server.start_publisher() - server.publish("SH", {"x": 1}) - server.stop() - self.assertIn("bigqmt:quote_push:acct:SH", redis_client.messages) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_manager.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_manager.py deleted file mode 100644 index 2834a26..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_manager.py +++ /dev/null @@ -1,222 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_subscription_manager import ( - ContextInfoQuoteSource, - QuoteSourceAdapter, - QuoteSubscriptionManager, - combo_key, -) - - -class FakeContextInfo: - """Mirrors big-QMT ContextInfo subscribe_whole_quote/unsubscribe_quote.""" - - def __init__(self, fail=False): - self.calls = [] - self.fail = fail - self._next = 0 - - def subscribe_whole_quote(self, code_list, callback=None): - self.calls.append(("subscribe", list(code_list))) - if self.fail: - return -1 - self._next += 1 - return self._next - - def unsubscribe_quote(self, sub_id): - self.calls.append(("unsubscribe", sub_id)) - return 0 - - -class ContextInfoQuoteSourceTest(unittest.TestCase): - def test_subscribe_returns_handle_and_forwards_callback(self): - context = FakeContextInfo() - source = ContextInfoQuoteSource(context) - received = [] - handle = source.subscribe(["SH", "SZ"], received.append) - self.assertEqual(handle, 1) - self.assertEqual(context.calls, [("subscribe", ["SH", "SZ"])]) - - def test_subscribe_failure_raises(self): - context = FakeContextInfo(fail=True) - source = ContextInfoQuoteSource(context) - with self.assertRaises(RuntimeError): - source.subscribe(["SH"], lambda d: None) - - def test_unsubscribe_forwards_sub_id(self): - context = FakeContextInfo() - source = ContextInfoQuoteSource(context) - handle = source.subscribe(["SH"], lambda d: None) - source.unsubscribe(handle) - self.assertEqual(context.calls[-1], ("unsubscribe", handle)) - - - -class FakeQuoteSource(QuoteSourceAdapter): - """Records subscribe/unsubscribe calls against a fake big-QMT quote source.""" - - def __init__(self): - self.subscriptions = {} - self.unsubscribed = [] - self._next_handle = 0 - - def subscribe(self, codes, on_push): - self._next_handle += 1 - handle = self._next_handle - self.subscriptions[handle] = {"codes": list(codes), "on_push": on_push} - return handle - - def unsubscribe(self, handle): - self.unsubscribed.append(handle) - self.subscriptions.pop(handle, None) - - -class ComboKeyTest(unittest.TestCase): - def test_order_independent(self): - self.assertEqual(combo_key(["SH", "SZ"]), combo_key(["SZ", "SH"])) - - def test_case_and_whitespace_normalized(self): - self.assertEqual(combo_key([" sh ", "sz"]), combo_key(["SH", "SZ"])) - - def test_duplicates_collapse(self): - self.assertEqual(combo_key(["SH", "SH", "SZ"]), combo_key(["SH", "SZ"])) - - def test_symbol_list(self): - self.assertEqual( - combo_key(["600000.SH", "000001.SZ"]), - combo_key(["000001.SZ", "600000.SH"]), - ) - - def test_empty_entries_dropped(self): - self.assertEqual(combo_key(["SH", "", None, " "]), "SH") - - def test_empty_list(self): - self.assertEqual(combo_key([]), "") - - -class QuoteSubscriptionManagerTest(unittest.TestCase): - def setUp(self): - self.source = FakeQuoteSource() - self.manager = QuoteSubscriptionManager( - self.source, - heartbeat_timeout_seconds=30.0, - ) - - # -- first client creates the big-QMT subscription ----------------------- - def test_first_subscribe_creates_qmt_subscription(self): - result = self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.assertEqual(len(self.source.subscriptions), 1) - handle = next(iter(self.source.subscriptions)) - self.assertEqual(self.source.subscriptions[handle]["codes"], ["SH", "SZ"]) - self.assertEqual(result["combo_key"], "SH,SZ") - self.assertIn("topic", result) - - def test_same_combo_different_order_shares_subscription(self): - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.manager.subscribe("clientB", "sub2", ["sz", "sh"]) - # Same normalized combo -> only one big-QMT subscription. - self.assertEqual(len(self.source.subscriptions), 1) - - def test_different_combos_create_separate_subscriptions(self): - self.manager.subscribe("clientA", "sub1", ["SH"]) - self.manager.subscribe("clientB", "sub2", ["SZ"]) - self.assertEqual(len(self.source.subscriptions), 2) - - def test_idempotent_replay_same_client_and_sub(self): - # Replayed subscribe (recovery) must not create a second big-QMT subscription. - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.assertEqual(len(self.source.subscriptions), 1) - - def test_subscribe_response_carries_push_endpoint_when_configured(self): - manager = QuoteSubscriptionManager( - self.source, push_endpoint="tcp://127.0.0.1:15561" - ) - result = manager.subscribe("clientA", "sub1", ["SH"]) - self.assertEqual(result["push_endpoint"], "tcp://127.0.0.1:15561") - - def test_subscribe_response_push_endpoint_defaults_empty(self): - result = self.manager.subscribe("clientA", "sub1", ["SH"]) - self.assertEqual(result["push_endpoint"], "") - - # -- reference counting / unsubscribe ------------------------------------ - def test_unsubscribe_one_of_two_keeps_qmt_subscription(self): - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.manager.subscribe("clientB", "sub2", ["SH", "SZ"]) - self.manager.unsubscribe("clientA", "sub1") - self.assertEqual(len(self.source.subscriptions), 1) - self.assertEqual(self.source.unsubscribed, []) - - def test_unsubscribe_one_sub_of_same_client_keeps_combo_alive(self): - """同一 client 两个 sub_id 订阅同一组合,退订一个后另一个仍在: - 服务端引用按 (client_id, sub_id) 粒度,不按 client 粒度。""" - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.manager.subscribe("clientA", "sub2", ["SH", "SZ"]) - self.manager.unsubscribe("clientA", "sub1") - self.assertEqual(len(self.source.subscriptions), 1, "组合不应被拆掉") - self.assertEqual(self.source.unsubscribed, [], "不应退订大 QMT 订阅") - # 另一个 sub 还在:keepalive 应仍有效(不产生新订阅/退订) - self.manager.keepalive("clientA", "sub2") - self.assertEqual(len(self.source.subscriptions), 1) - # 最后一个 sub 退订 -> 组合拆掉 - self.manager.unsubscribe("clientA", "sub2") - self.assertEqual(len(self.source.subscriptions), 0) - self.assertEqual(len(self.source.unsubscribed), 1) - - def test_unsubscribe_last_client_unsubscribes_qmt(self): - self.manager.subscribe("clientA", "sub1", ["SH", "SZ"]) - self.manager.subscribe("clientB", "sub2", ["SH", "SZ"]) - self.manager.unsubscribe("clientA", "sub1") - self.manager.unsubscribe("clientB", "sub2") - self.assertEqual(len(self.source.subscriptions), 0) - self.assertEqual(len(self.source.unsubscribed), 1) - - def test_unsubscribe_unknown_sub_is_noop(self): - self.manager.subscribe("clientA", "sub1", ["SH"]) - # Should not raise, should not affect the live subscription. - self.manager.unsubscribe("clientA", "no-such-sub") - self.assertEqual(len(self.source.subscriptions), 1) - - # -- keepalive / reaper ---------------------------------------------------- - def test_keepalive_refreshes_last_seen(self): - self.manager.subscribe("clientA", "sub1", ["SH"]) - self.manager.keepalive("clientA", "sub1") - # Still alive -> reaping at a fresh timestamp removes nothing. - self.assertEqual(self.manager.reap_expired(now=self.manager._now()), 0) - self.assertEqual(len(self.source.subscriptions), 1) - - def test_reap_removes_timed_out_client_and_unsubscribes(self): - self.manager.subscribe("clientA", "sub1", ["SH"]) - now = self.manager._now() - # Advance past the 30s timeout with no keepalive. - self.assertEqual(self.manager.reap_expired(now=now + 31.0), 1) - self.assertEqual(len(self.source.subscriptions), 0) - - def test_reap_keeps_combo_alive_while_one_client_alive(self): - clock = [1000.0] - manager = QuoteSubscriptionManager( - self.source, heartbeat_timeout_seconds=30.0, time_func=lambda: clock[0] - ) - manager.subscribe("clientA", "sub1", ["SH"]) - manager.subscribe("clientB", "sub2", ["SH"]) - # clientB keepalives at +31 (fresh); clientA has been silent since subscribe. - clock[0] = 1031.0 - manager.keepalive("clientB", "sub2") - removed = manager.reap_expired() - # clientA reaped, but combo still has clientB -> big-QMT subscription stays. - self.assertEqual(removed, 1) - self.assertEqual(len(self.source.subscriptions), 1) - - def test_keepalive_unknown_sub_is_noop(self): - # Unknown sub should not raise. - self.manager.keepalive("clientA", "ghost") - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_service.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_service.py deleted file mode 100644 index 712f589..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_quote_subscription_service.py +++ /dev/null @@ -1,84 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_push_channel import ( - RedisQuotePushChannel, - ZmqQuotePushChannel, -) -from bigqmt_signal_trader.quote_subscription_manager import ( - QuoteSubscriptionManager, - build_quote_subscription_service, -) - - -class FakeContextInfo: - def __init__(self): - self._next = 0 - - def subscribe_whole_quote(self, code_list, callback=None): - self._next += 1 - return self._next - - def unsubscribe_quote(self, sub_id): - return 0 - - -class FakeRedis: - def publish(self, channel, value): - return 1 - - def pubsub(self, ignore_subscribe_messages=True): - raise AssertionError("not used here") - - -class BuildQuoteSubscriptionServiceTest(unittest.TestCase): - def test_disabled_returns_none(self): - result = build_quote_subscription_service( - FakeContextInfo(), transport_name="redis", account_id="acct", - redis_client=FakeRedis(), enabled=False, - ) - self.assertIsNone(result) - - def test_redis_transport_builds_redis_channel(self): - service = build_quote_subscription_service( - FakeContextInfo(), transport_name="redis", account_id="acct", - redis_client=FakeRedis(), enabled=True, - ) - manager, channel = service - self.assertIsInstance(manager, QuoteSubscriptionManager) - self.assertIsInstance(channel, RedisQuotePushChannel) - - def test_zmq_transport_builds_zmq_channel_with_bind_address(self): - service = build_quote_subscription_service( - FakeContextInfo(), transport_name="zmq", account_id="acct", - zmq_bind_address="tcp://127.0.0.1:15561", enabled=True, - ) - manager, channel = service - self.assertIsInstance(channel, ZmqQuotePushChannel) - self.assertEqual(channel.bind_address, "tcp://127.0.0.1:15561") - - def test_manager_on_push_publisher_is_channel_publish(self): - service = build_quote_subscription_service( - FakeContextInfo(), transport_name="redis", account_id="acct", - redis_client=FakeRedis(), enabled=True, - ) - manager, channel = service - # The assembled manager must publish through the assembled channel. - self.assertEqual(manager._on_push_publisher, channel.publish) - - def test_heartbeat_timeout_configurable(self): - service = build_quote_subscription_service( - FakeContextInfo(), transport_name="redis", account_id="acct", - redis_client=FakeRedis(), enabled=True, heartbeat_timeout_seconds=30.0, - ) - manager, _channel = service - self.assertEqual(manager._heartbeat_timeout, 30.0) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_adapters.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_adapters.py deleted file mode 100644 index ef29645..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_adapters.py +++ /dev/null @@ -1,179 +0,0 @@ -import json -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapter_factory import build_app -from bigqmt_signal_trader.adapters.position_sync_redis import RedisPositionSyncSink -from bigqmt_signal_trader.adapters.signal_redis import RedisStreamSignalSource, push_trade_signal -from bigqmt_signal_trader.adapters.state_redis import RedisStateStore -from bigqmt_signal_trader.models import AccountSnapshot, AssetSnapshot, PositionSnapshot - - -class FakeRedis: - def __init__(self): - self.streams = {} - self.groups = set() - self.acked = [] - self.kv = {} - self.hashes = {} - self.expired = [] - self.next_id = 1 - - def xgroup_create(self, stream_key, group_name, id="0-0", mkstream=True): - key = (stream_key, group_name) - if key in self.groups: - raise Exception("BUSYGROUP Consumer Group name already exists") - self.groups.add(key) - self.streams.setdefault(stream_key, []) - return True - - def xreadgroup(self, groupname, consumername, streams, count=None, block=None): - result = [] - for stream_key in streams: - entries = self.streams.get(stream_key, [])[: count or None] - if entries: - result.append((stream_key, entries)) - return result - - def xack(self, stream_key, group_name, stream_id): - self.acked.append((stream_key, group_name, stream_id)) - return 1 - - def xadd(self, stream_key, fields, maxlen=None, approximate=False): - stream_id = "%d-0" % self.next_id - self.next_id += 1 - self.streams.setdefault(stream_key, []).append((stream_id, fields)) - return stream_id - - def set(self, key, value, nx=False, ex=None): - if nx and key in self.kv: - return False - self.kv[key] = value - return True - - def hset(self, key, mapping): - self.hashes.setdefault(key, {}).update(mapping) - return len(mapping) - - def expire(self, key, seconds): - self.expired.append((key, seconds)) - return True - - def setex(self, key, seconds, value): - self.kv[key] = value - self.expired.append((key, seconds)) - return True - - -def _payload(**kwargs): - payload = { - "signal_id": "sig-redis-001", - "account_id": "acct", - "action": "BUY", - "stock_code": "600000", - "amount": 300, - "created_at": "2026-07-01 09:31:00", - "expire_at": "2026-07-01 09:36:00", - "schema_version": 1, - "force": "false", - } - payload.update(kwargs) - return payload - - -class RedisAdaptersTest(unittest.TestCase): - def test_stream_source_reads_json_payload_and_acks(self): - r = FakeRedis() - stream_key = "bigqmt:signals:acct" - r.xadd(stream_key, {"payload": json.dumps(_payload())}) - source = RedisStreamSignalSource(r, consumer_name="c1") - - signals = source.fetch("acct", 10) - source.ack(signals[0]) - - self.assertEqual(signals[0].signal_id, "sig-redis-001") - self.assertFalse(signals[0].force) - self.assertEqual(r.acked, [(stream_key, "bigqmt-signal-trader", "1-0")]) - - def test_stream_source_reads_flat_payload_with_bytes_keys(self): - r = FakeRedis() - fields = {key.encode("utf-8"): str(value).encode("utf-8") for key, value in _payload().items()} - r.streams["bigqmt:signals:acct"] = [("9-0", fields)] - source = RedisStreamSignalSource(r) - - signals = source.fetch("acct", 10) - - self.assertEqual(signals[0].stock_code, "600000") - self.assertEqual(signals[0].amount, 300) - - def test_state_store_claim_is_idempotent_and_writes_status(self): - r = FakeRedis() - source = RedisStreamSignalSource(r) - r.xadd("bigqmt:signals:acct", {"payload": json.dumps(_payload())}) - signal = source.fetch("acct", 1)[0] - store = RedisStateStore(r, account_id="acct") - - self.assertTrue(store.claim(signal, "consumer-a")) - self.assertFalse(store.claim(signal, "consumer-b")) - store.mark_finished(signal.signal_id, "SKIPPED", "test") - - status_key = "bigqmt:signal_status:acct:sig-redis-001" - self.assertEqual(r.hashes[status_key]["status"], "SKIPPED") - self.assertEqual(r.hashes[status_key]["message"], "test") - - def test_position_sync_sink_writes_snapshot_and_event(self): - r = FakeRedis() - sink = RedisPositionSyncSink(r) - snapshot = AccountSnapshot( - account_id="acct", - asset=AssetSnapshot(account_id="acct", cash=100.0, total_asset=1000.0), - positions={ - "600000.SH": PositionSnapshot( - stock_code="600000.SH", - volume=100, - available=100, - cost=10.0, - stock_name="PF Bank", - ) - }, - reason="test", - updated_at=__import__("datetime").datetime(2026, 7, 1, 9, 31), - ) - - sink.publish(snapshot) - - self.assertIn("bigqmt:positions:acct", r.kv) - self.assertIn("bigqmt:position_events:acct", r.streams) - - def test_push_trade_signal_uses_account_stream(self): - r = FakeRedis() - - stream_id = push_trade_signal(r, _payload()) - - self.assertEqual(stream_id, "1-0") - self.assertIn("bigqmt:signals:acct", r.streams) - - def test_factory_wires_redis_adapters_without_real_redis(self): - r = FakeRedis() - app = build_app( - config={ - "account_id": "acct", - "signal_source_type": "redis", - "state_store_type": "redis", - "position_sync_type": "redis", - "redis_client": r, - } - ) - - self.assertIsInstance(app.signal_source, RedisStreamSignalSource) - self.assertIsInstance(app.state_store, RedisStateStore) - self.assertIsInstance(app.position_sync_sink, RedisPositionSyncSink) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_rpc.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_rpc.py deleted file mode 100644 index 1f352c1..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_redis_rpc.py +++ /dev/null @@ -1,1146 +0,0 @@ -import json -import time -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.adapters.order_dryrun import DryRunOrderGateway -from bigqmt_signal_trader.models import AssetSnapshot, OrderSnapshot, PositionSnapshot, TradeSnapshot -from bigqmt_signal_trader.redis_rpc import ( - RPC_REVISION, - BigQmtRpcHandlers, - RedisPubSubRpcService, - decode_rpc_request_payload, - encode_rpc_request_payload, -) - - -class FakeRedis: - def __init__(self): - self.kv = {} - self.expired = [] - self.published = [] - - def setex(self, key, seconds, value): - self.kv[key] = value - self.expired.append((key, seconds)) - return True - - def set(self, key, value): - self.kv[key] = value - return True - - def publish(self, channel, value): - self.published.append((channel, value)) - return 1 - - -class FakeMarketData: - def get_ticks(self, codes): - return {codes[0]: {"lastPrice": 10.5}} - - def get_instrument(self, code): - return {"code": code, "InstrumentStatus": 0} - - def get_market_data_ex(self, **kwargs): - return {"params": kwargs, "data": {"600000.SH": {"close": [10.0]}}} - - -class FakePositionProvider: - def get_positions(self, account_id): - return { - "600000.SH": PositionSnapshot( - stock_code="600000.SH", - volume=1000, - available=800, - cost=10.0, - stock_name="PF Bank", - ) - } - - def get_asset(self, account_id): - return AssetSnapshot(account_id=account_id, cash=100.0, total_asset=1000.0) - - -def _service(allow_order_methods=False, process_in_listener=False): - return _service_with_listener_methods( - allow_order_methods=allow_order_methods, - process_in_listener=process_in_listener, - listener_methods=None, - ) - - -def _service_with_listener_methods(allow_order_methods=False, process_in_listener=False, listener_methods=None): - redis_client = FakeRedis() - order_gateway = DryRunOrderGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - order_gateway=order_gateway, - allow_order_methods=allow_order_methods, - # DryRunOrderGateway never registers an order, so retrying until the - # deadline would just stall every test by the full timeout. - order_settle_timeout_seconds=0.0, - ) - return redis_client, RedisPubSubRpcService( - redis_client, - handlers, - account_id="acct", - process_in_listener=process_in_listener, - listener_methods=listener_methods, - ) - - -class FakeOrderGateway(DryRunOrderGateway): - def query_orders(self, account_id, strategy_name): - return [ - OrderSnapshot( - order_sys_id="open-1", - user_order_id="remark-1", - stock_code="600000.SH", - action="BUY", - volume=100, - traded_volume=0, - status="50", - ), - OrderSnapshot( - order_sys_id="done-1", - user_order_id="remark-2", - stock_code="600000.SH", - action="BUY", - volume=100, - traded_volume=100, - status="56", - ), - ] - - -class CountingOrderGateway(DryRunOrderGateway): - def __init__(self, existing=None, query_error=None): - super().__init__() - self.existing = list(existing or []) - self.query_error = query_error - self.submit_count = 0 - self.query_count = 0 - - def query_orders_strict(self, _account_id, _strategy_name): - self.query_count += 1 - if self.query_error: - raise self.query_error - return list(self.existing) - - def submit(self, request): - self.submit_count += 1 - return super().submit(request) - - -class CapturingTradeGateway(DryRunOrderGateway): - def __init__(self): - super().__init__() - self.strategy_names = [] - - def query_trades(self, account_id, strategy_name): - self.strategy_names.append((account_id, strategy_name)) - return [] - - -class LandingOrderGateway(DryRunOrderGateway): - """模拟 QMT:passorder 异步落地,委托号稍后出现在查询结果里。""" - - def __init__(self, landed=True): - super().__init__() - self.landed = landed - self.orders = [] - - def submit(self, request): - result = super().submit(request) - if self.landed: - self.orders.append( - OrderSnapshot( - order_sys_id="sysid-1", - user_order_id=str(request.remark or ""), - stock_code=request.stock_code, - action=request.action, - volume=request.volume, - traded_volume=0, - status="50", - ) - ) - return result - - def query_orders(self, account_id, strategy_name): - return list(self.orders) - - -class CapturingExecutionGateway(CapturingTradeGateway): - def __init__(self): - super().__init__() - self.order_strategy_names = [] - - def query_orders(self, account_id, strategy_name): - self.order_strategy_names.append((account_id, strategy_name)) - return [ - OrderSnapshot( - order_sys_id="order-1", user_order_id="tag-1", stock_code="600276.SH", - action="SELL", volume=100, traded_volume=0, status="50", price=55.0, - ) - ] - - def query_trades(self, account_id, strategy_name): - self.strategy_names.append((account_id, strategy_name)) - return [ - TradeSnapshot( - trade_id="trade-1", order_sys_id="order-1", stock_code="600276.SH", - action="SELL", volume=100, price=55.0, - ) - ] - - -def _service_with_order_gateway(order_gateway, allow_order_methods=False): - redis_client = FakeRedis() - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - order_gateway=order_gateway, - allow_order_methods=allow_order_methods, - order_settle_timeout_seconds=0.0, - ) - return redis_client, RedisPubSubRpcService(redis_client, handlers, account_id="acct") - - -class LateLandingOrderGateway(DryRunOrderGateway): - """QMT assigns the order id asynchronously: the order only becomes visible - after ``appear_after`` lookups.""" - - def __init__(self, appear_after=2, never=False): - super().__init__() - self.appear_after = appear_after - self.never = never - self.lookups = 0 - self._request = None - - def submit(self, request): - self._request = request - return super().submit(request) - - def query_orders(self, account_id, strategy_name): - self.lookups += 1 - if self.never or self.lookups < self.appear_after or self._request is None: - return [] - return [ - OrderSnapshot( - order_sys_id="sysid-late", - user_order_id=str(self._request.remark or ""), - stock_code=self._request.stock_code, - action=self._request.action, - volume=self._request.volume, - traded_volume=0, - status="50", - ) - ] - - -class AsyncOrderSettlementTest(unittest.TestCase): - """issue #44: passorder must not hold the QMT adjust thread. - - The old path slept 0.5s inline, serializing every other request behind each - order and capping throughput at ~2 orders/sec. - """ - - def _service(self, gateway, timeout=5.0): - redis_client = FakeRedis() - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, order_settle_timeout_seconds=timeout, - ) - return redis_client, RedisPubSubRpcService(redis_client, handlers, account_id="acct") - - def _submit(self, service, request_id="ord-1"): - service.enqueue_payload({ - "request_id": request_id, "account_id": "acct", "method": "order_stock", - "params": {"stock_code": "600000.SH", "order_type": 23, "order_volume": 100, - "price_type": 11, "price": 10.1, "order_remark": request_id}, - }) - - def test_drain_does_not_sleep_waiting_for_the_order_id(self): - """The whole point: no 0.5s block on the adjust thread.""" - gateway = LateLandingOrderGateway(appear_after=99) - redis_client, service = self._service(gateway) - self._submit(service) - - started = time.monotonic() - service.drain_pending() - elapsed = time.monotonic() - started - - self.assertLess(elapsed, 0.2, "drain blocked for %.3fs" % elapsed) - - def test_unresolved_order_is_parked_not_answered(self): - gateway = LateLandingOrderGateway(appear_after=99) - redis_client, service = self._service(gateway) - self._submit(service) - service.drain_pending() - - self.assertNotIn("bigqmt:rpc:resp:acct:ord-1", redis_client.kv) - self.assertEqual(service.pending_settlement_count(), 1) - - def test_later_tick_settles_and_backfills_the_sysid(self): - gateway = LateLandingOrderGateway(appear_after=3) - redis_client, service = self._service(gateway) - self._submit(service) - - for _ in range(5): - service.drain_pending() - if "bigqmt:rpc:resp:acct:ord-1" in redis_client.kv: - break - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:ord-1"]) - self.assertTrue(response["ok"], response["error"]) - self.assertEqual(response["data"]["order_sys_id"], "sysid-late") - self.assertEqual(response["server_error"], "") - self.assertEqual(service.pending_settlement_count(), 0) - - def test_order_that_never_lands_reports_after_the_deadline(self): - gateway = LateLandingOrderGateway(never=True) - redis_client, service = self._service(gateway, timeout=0.0) - self._submit(service) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:ord-1"]) - self.assertTrue(response["ok"]) - self.assertIn("not found in system", response["server_error"]) - - def test_settlement_error_does_not_leak_into_other_responses(self): - """issue #43 again, by another route: settling happens long after the - order left the handler, so its diagnostic must ride on the settlement - rather than handlers._last_server_error.""" - gateway = LateLandingOrderGateway(never=True) - redis_client, service = self._service(gateway, timeout=0.0) - self._submit(service) - service.drain_pending() - self.assertIn("not found in system", - json.loads(redis_client.kv["bigqmt:rpc:resp:acct:ord-1"])["server_error"]) - - service.enqueue_payload({"request_id": "later-ping", "account_id": "acct", - "method": "ping", "params": {}}) - service.drain_pending() - - self.assertEqual( - json.loads(redis_client.kv["bigqmt:rpc:resp:acct:later-ping"])["server_error"], "") - - def test_many_orders_drain_without_accumulating_delay(self): - """Throughput was the reported symptom: 0.5s per order serialized.""" - gateway = LateLandingOrderGateway(appear_after=99) - redis_client, service = self._service(gateway) - for i in range(20): - self._submit(service, request_id="ord-%d" % i) - - started = time.monotonic() - service.drain_pending(max_items=50) - elapsed = time.monotonic() - started - - self.assertLess(elapsed, 1.0, "20 orders took %.2fs" % elapsed) - self.assertEqual(service.pending_settlement_count(), 20) - - -class RedisRpcTest(unittest.TestCase): - def test_execution_snapshot_queries_orders_and_all_trades_once(self): - gateway = CapturingExecutionGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - order_gateway=gateway, - ) - - snapshot = handlers.handle( - "query_execution_snapshot", - { - "account_id": "acct", - "order_strategy_name": "icestone_grid_600276", - "trade_strategy_name": "", - }, - ) - - self.assertEqual(gateway.order_strategy_names, [("acct", "icestone_grid_600276")]) - self.assertEqual(gateway.strategy_names, [("acct", "")]) - self.assertEqual(snapshot["orders"][0].order_sys_id, "order-1") - self.assertEqual(snapshot["trades"][0].trade_id, "trade-1") - self.assertEqual(snapshot["account_id"], "acct") - - def test_query_trades_preserves_explicit_empty_strategy_name(self): - gateway = CapturingTradeGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - order_gateway=gateway, - ) - - handlers.handle( - "query_stock_trades", - {"account_id": "acct", "strategy_name": ""}, - ) - - self.assertEqual(gateway.strategy_names, [("acct", "")]) - - def test_submit_orders_batch_returns_one_result_per_order(self): - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - order_gateway=DryRunOrderGateway(), - allow_order_methods=True, - ) - - results = handlers.handle( - "order_stock_batch", - { - "orders": [ - {"account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price": 10.0, "signal_id": "batch-1"}, - {"account_id": "acct", "stock_code": "600000.SH", "order_type": 24, - "order_volume": 100, "price": 10.5, "signal_id": "batch-2"}, - ] - }, - ) - - self.assertEqual(len(results), 2) - self.assertTrue(all(item["success"] for item in results)) - self.assertTrue(all(item["accepted"] for item in results)) - self.assertTrue(all(item["user_order_id"] for item in results)) - self.assertTrue(all(not item["order_sys_id"] for item in results)) - - def test_submit_order_enriches_order_sys_id_by_remark(self): - # issue #38: passorder 提交成功但委托号异步分配。服务端必须按唯一 - # user_order_id(remark) 匹配并回填 order_sys_id,客户端才不会把 - # 「已提交」误判成 -1 失败。 - gateway = LandingOrderGateway(landed=True) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - result = handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-38", - }) - self.assertEqual(result.order_sys_id, "sysid-1") - self.assertEqual(handlers._last_server_error, "") - - def test_submit_order_silent_rejection_sets_server_error(self): - # 委托没进系统(静默拒绝)时记录 server_error,客户端据此收到真实原因。 - gateway = LandingOrderGateway(landed=False) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - result = handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-38", - }) - self.assertIsNone(result.order_sys_id) - self.assertIn("not found in system", handlers._last_server_error) - - def test_server_error_does_not_leak_into_later_requests(self): - """issue #43: _last_server_error is instance state read by every response, - so a failed order used to stamp its error onto every later read.""" - gateway = LandingOrderGateway(landed=False) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-43", - }) - self.assertIn("not found in system", handlers._last_server_error) - - # Any later request must start from a clean slot. - handlers.handle("ping", {}) - self.assertEqual(handlers._last_server_error, "") - handlers.handle("get_positions", {"account_id": "acct"}) - self.assertEqual(handlers._last_server_error, "") - - def test_server_error_cleared_even_when_method_is_rejected(self): - """A rejected request must not carry the previous diagnostic either.""" - gateway = LandingOrderGateway(landed=False) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-43b", - }) - self.assertNotEqual(handlers._last_server_error, "") - with self.assertRaises(ValueError): - handlers.handle("no_such_method", {}) - self.assertEqual(handlers._last_server_error, "") - - def test_unrelated_same_stock_order_does_not_mask_a_silent_rejection(self): - """issue #41: an unrelated order on the same stock+side used to suppress - the warning, leaving order_sys_id unfilled with no signal at all.""" - gateway = LandingOrderGateway(landed=False) - # Pre-existing order: same stock and side, different (unrelated) remark. - gateway.orders.append( - OrderSnapshot( - order_sys_id="sysid-unrelated", - user_order_id="SOMEONE-ELSE", - stock_code="600000.SH", - action="BUY", - volume=200, - traded_volume=0, - status="50", - ) - ) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - result = handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-41", - }) - - self.assertIsNone(result.order_sys_id) - self.assertIn("not found in system", handlers._last_server_error) - - def test_remark_match_still_backfills_sysid_with_other_orders_present(self): - """The strict match must not regress the issue #38 backfill.""" - gateway = LandingOrderGateway(landed=True) - gateway.orders.append( - OrderSnapshot( - order_sys_id="sysid-unrelated", - user_order_id="SOMEONE-ELSE", - stock_code="600000.SH", - action="BUY", - volume=200, - traded_volume=0, - status="50", - ) - ) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - result = handlers.handle("order_stock", { - "account_id": "acct", "stock_code": "600000.SH", "order_type": 23, - "order_volume": 100, "price_type": 11, "price": 10.0, - "order_remark": "REMARK-38b", - }) - - self.assertEqual(result.order_sys_id, "sysid-1") - self.assertEqual(handlers._last_server_error, "") - - def test_submit_orders_batch_reuses_order_tag_without_resubmitting(self): - gateway = CountingOrderGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - params = { - "batch_id": "BATCH-1", - "orders": [{"account_id": "acct", "stock_code": "600000.SH", - "order_type": 23, "order_volume": 100, "price": 10.0, - "order_remark": "GRID-TAG-1"}], - } - - first = handlers.handle("order_stock_batch", params) - second = handlers.handle("order_stock_batch", params) - - self.assertEqual(gateway.submit_count, 1) - self.assertEqual(gateway.query_count, 0) - self.assertFalse(first[0]["idempotent"]) - self.assertTrue(second[0]["idempotent"]) - - def test_submit_orders_batch_rejects_missing_order_tag(self): - gateway = CountingOrderGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - - result = handlers.handle("order_stock_batch", { - "orders": [{"account_id": "acct", "stock_code": "600000.SH", - "order_type": 23, "order_volume": 100, "price": 10.0}], - })[0] - - self.assertEqual(gateway.submit_count, 0) - self.assertTrue(result["explicit_failure"]) - self.assertEqual(result["error"], "ORDER_TAG_REQUIRED") - - def test_submit_orders_batch_recognizes_existing_qmt_order(self): - existing = OrderSnapshot( - order_sys_id="SYS-EXISTING", user_order_id="GRID-TAG-2", - stock_code="600000.SH", action="BUY", volume=100, - traded_volume=0, status="50", - ) - gateway = CountingOrderGateway(existing=[existing]) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - - result = handlers.handle("order_stock_batch", { - "batch_id": "BATCH-2", - "orders": [{"account_id": "acct", "stock_code": "600000.SH", - "order_type": 23, "order_volume": 100, "price": 10.0, - "order_remark": "GRID-TAG-2", - "require_idempotency_check": True}], - })[0] - - self.assertEqual(gateway.submit_count, 0) - self.assertTrue(result["confirmed"]) - self.assertTrue(result["idempotent"]) - self.assertEqual(result["order_sys_id"], "SYS-EXISTING") - - def test_submit_orders_batch_recognizes_already_filled_trade(self): - class FilledGateway(CountingOrderGateway): - def query_submission_identities_strict(self, _account_id, _strategy_name): - trade = type("Trade", (), { - "user_order_id": "GRID-TAG-FILLED", - "order_sys_id": "SYS-FILLED", - })() - return [], [trade] - - gateway = FilledGateway() - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - - result = handlers.handle("order_stock_batch", { - "orders": [{"account_id": "acct", "stock_code": "600000.SH", - "order_type": 23, "order_volume": 100, "price": 10.0, - "order_remark": "GRID-TAG-FILLED", - "require_idempotency_check": True}], - })[0] - - self.assertEqual(gateway.submit_count, 0) - self.assertTrue(result["confirmed"]) - self.assertEqual(result["order_sys_id"], "SYS-FILLED") - - def test_submit_orders_batch_refuses_retry_when_lookup_is_unavailable(self): - gateway = CountingOrderGateway(query_error=RuntimeError("qmt offline")) - handlers = BigQmtRpcHandlers( - account_id="acct", market_data=FakeMarketData(), - position_provider=FakePositionProvider(), order_gateway=gateway, - allow_order_methods=True, - # Drive the lookup synchronously so these assertions stay about the - # matching logic, not about when the settle pass runs. - settle_orders_inline=True, - order_settle_timeout_seconds=0.0, - ) - - result = handlers.handle("order_stock_batch", { - "orders": [{"account_id": "acct", "stock_code": "600000.SH", - "order_type": 23, "order_volume": 100, "price": 10.0, - "order_remark": "GRID-TAG-3", - "require_idempotency_check": True}], - })[0] - - self.assertEqual(gateway.submit_count, 0) - self.assertFalse(result["explicit_failure"]) - self.assertEqual(result["error"], "IDEMPOTENCY_CHECK_UNAVAILABLE") - - def test_encoded_request_payload_hides_stock_codes_from_qmt_redis_guard(self): - request = { - "request_id": "encoded", - "account_id": "acct", - "method": "get_full_tick", - "params": {"codes": ["000001.SZ", "600000.SH"]}, - } - - encoded = encode_rpc_request_payload(request) - - self.assertNotIn("000001", encoded) - self.assertNotIn("600000", encoded) - self.assertEqual(json.loads(decode_rpc_request_payload(encoded)), request) - - def test_readonly_rpc_writes_position_response_to_key_and_channel(self): - redis_client, service = _service() - - processed = service.drain_pending() - self.assertEqual(processed, 0) - - service.enqueue_payload( - { - "request_id": "req-1", - "account_id": "acct", - "method": "get_positions", - "params": {}, - } - ) - self.assertEqual(service.drain_pending(), 1) - - response_key = "bigqmt:rpc:resp:acct:req-1" - response = json.loads(redis_client.kv[response_key]) - self.assertTrue(response["ok"]) - self.assertEqual(response["data"]["600000.SH"]["available"], 800) - self.assertEqual(redis_client.published[0][0], "bigqmt:rpc:resp:acct:req-1") - - def test_process_in_listener_handles_request_without_waiting_for_drain(self): - redis_client, service = _service(process_in_listener=True) - - service.enqueue_payload( - { - "request_id": "listener-req", - "account_id": "acct", - "method": "ping", - "params": {}, - } - ) - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:listener-req"]) - self.assertTrue(response["ok"], response["error"]) - self.assertTrue(response["data"]["pong"]) - self.assertFalse(response["data"]["allow_order_methods"]) - self.assertEqual(response["data"]["rpc_revision"], RPC_REVISION) - self.assertEqual(service.drain_pending(), 0) - - def test_process_in_listener_leaves_non_listener_methods_queued(self): - redis_client, service = _service(process_in_listener=True) - - service.enqueue_payload( - { - "request_id": "queued-tick", - "account_id": "acct", - "method": "get_full_tick", - "params": {"codes": ["600000.SH"]}, - } - ) - - self.assertNotIn("bigqmt:rpc:resp:acct:queued-tick", redis_client.kv) - self.assertEqual(service.drain_pending(), 1) - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:queued-tick"]) - self.assertTrue(response["ok"], response["error"]) - - def test_process_in_listener_wildcard_only_handles_ping_inline(self): - # get_full_tick is a market-data read (thread-safe in embedded terminal), - # so it stays inline for low latency and responds immediately. - redis_client, service = _service_with_listener_methods( - allow_order_methods=True, - process_in_listener=True, - listener_methods=("*",), - ) - - service.enqueue_payload( - { - "request_id": "direct-tick", - "account_id": "acct", - "method": "get_full_tick", - "params": {"codes": ["600000.SH"]}, - } - ) - - # Inline: response written immediately, no pending drain needed. - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:direct-tick"]) - self.assertTrue(response["ok"], response["error"]) - self.assertEqual(response["data"]["600000.SH"]["lastPrice"], 10.5) - - service.enqueue_payload( - { - "request_id": "queued-sync", - "account_id": "acct", - "method": "sync_positions", - "params": {}, - } - ) - - self.assertNotIn("bigqmt:rpc:resp:acct:queued-sync", redis_client.kv) - self.assertEqual(service.drain_pending(), 1) - sync_response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:queued-sync"]) - self.assertTrue(sync_response["ok"], sync_response["error"]) - self.assertEqual(sync_response["data"]["positions"]["600000.SH"]["available"], 800) - - service.enqueue_payload( - { - "request_id": "queued-order", - "account_id": "acct", - "method": "order_stock", - "params": { - "stock_code": "600000.SH", - "order_type": 23, - "order_volume": 100, - "price_type": 11, - "price": 10.1, - }, - } - ) - - self.assertNotIn("bigqmt:rpc:resp:acct:queued-order", redis_client.kv) - self.assertEqual(service.drain_pending(), 1) - order_response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:queued-order"]) - self.assertTrue(order_response["ok"], order_response["error"]) - - def test_listener_wildcard_defers_asset_query_to_strategy_thread(self): - redis_client, service = _service_with_listener_methods( - process_in_listener=True, - listener_methods=("*",), - ) - - service.enqueue_payload( - { - "request_id": "queued-asset", - "account_id": "acct", - "method": "query_stock_asset", - "params": {}, - } - ) - - self.assertNotIn("bigqmt:rpc:resp:acct:queued-asset", redis_client.kv) - self.assertEqual(service.drain_pending(), 1) - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:queued-asset"]) - self.assertTrue(response["ok"], response["error"]) - - def test_account_mismatch_is_rejected(self): - redis_client, service = _service() - - service.enqueue_payload( - { - "request_id": "req-2", - "account_id": "other", - "method": "get_asset", - "params": {}, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:other:req-2"]) - self.assertFalse(response["ok"]) - self.assertIn("account_id mismatch", response["error"]) - - def test_order_rpc_is_disabled_by_default(self): - redis_client, service = _service() - - service.enqueue_payload( - { - "request_id": "req-3", - "account_id": "acct", - "method": "submit_order", - "params": { - "action": "BUY", - "stock_code": "600000", - "volume": 100, - "price": 10.1, - }, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:req-3"]) - self.assertFalse(response["ok"]) - self.assertIn("not allowed", response["error"]) - - def test_order_rpc_can_be_enabled_for_dryrun_gateway(self): - redis_client, service = _service(allow_order_methods=True) - - service.enqueue_payload( - { - "request_id": "req-4", - "account_id": "acct", - "method": "submit_order", - "params": { - "action": "BUY", - "stock_code": "600000", - "volume": 100, - "price": 10.1, - }, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:req-4"]) - self.assertTrue(response["ok"]) - self.assertEqual(response["data"]["status"], "DRY_RUN") - - def test_miniqmt_read_aliases_are_accepted(self): - redis_client, service = _service() - - for request_id, method in ( - ("alias-pos", "query_stock_positions"), - ("alias-asset", "query_stock_asset"), - ("alias-tick", "get_full_tick"), - ): - params = {"codes": ["600000.SH"]} if method == "get_full_tick" else {} - service.enqueue_payload( - { - "request_id": request_id, - "account_id": "acct", - "method": method, - "params": params, - } - ) - service.drain_pending() - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:%s" % request_id]) - self.assertTrue(response["ok"], response["error"]) - - self.assertEqual( - json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-pos"])["data"]["600000.SH"]["volume"], - 1000, - ) - self.assertEqual( - json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-asset"])["data"]["cash"], - 100.0, - ) - self.assertEqual( - json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-tick"])["data"]["600000.SH"]["lastPrice"], - 10.5, - ) - - def test_miniqmt_single_position_alias_filters_by_stock_code(self): - redis_client, service = _service() - - service.enqueue_payload( - { - "request_id": "alias-single-position", - "account_id": "acct", - "method": "query_stock_position", - "params": {"stock_code": "600000"}, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-single-position"]) - self.assertTrue(response["ok"], response["error"]) - self.assertEqual(response["data"]["stock_code"], "600000.SH") - - def test_miniqmt_query_orders_alias_supports_cancelable_filter(self): - redis_client, service = _service_with_order_gateway(FakeOrderGateway()) - - service.enqueue_payload( - { - "request_id": "alias-orders", - "account_id": "acct", - "method": "query_stock_orders", - "params": {"cancelable_only": True}, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-orders"]) - self.assertTrue(response["ok"], response["error"]) - self.assertEqual(len(response["data"]), 1) - self.assertEqual(response["data"][0]["order_sys_id"], "open-1") - - def test_miniqmt_order_alias_is_disabled_by_default(self): - redis_client, service = _service() - - service.enqueue_payload( - { - "request_id": "alias-order-disabled", - "account_id": "acct", - "method": "order_stock", - "params": { - "stock_code": "600000.SH", - "order_type": 23, - "order_volume": 100, - "price_type": 11, - "price": 10.1, - "order_remark": "mini", - }, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-order-disabled"]) - self.assertFalse(response["ok"]) - self.assertTrue( - "not allowed" in response["error"] or "disabled" in response["error"], - response["error"], - ) - - def test_miniqmt_order_and_cancel_aliases_work_when_enabled(self): - order_gateway = DryRunOrderGateway() - redis_client, service = _service_with_order_gateway(order_gateway, allow_order_methods=True) - - service.enqueue_payload( - { - "request_id": "alias-order", - "account_id": "acct", - "method": "order_stock", - "params": { - "stock_code": "600000.SH", - "order_type": 24, - "order_volume": 100, - "price_type": 11, - "price": 10.1, - "order_remark": "mini", - }, - } - ) - service.enqueue_payload( - { - "request_id": "alias-cancel", - "account_id": "acct", - "method": "cancel_order_stock_sysid", - "params": {"account": {"account_id": "acct"}, "order_sysid": "sys-1"}, - } - ) - self.assertEqual(service.drain_pending(max_items=2), 2) - - order_response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-order"]) - cancel_response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:alias-cancel"]) - self.assertTrue(order_response["ok"], order_response["error"]) - self.assertTrue(cancel_response["ok"], cancel_response["error"]) - self.assertEqual(order_gateway.submitted[0].action, "SELL") - self.assertEqual(order_gateway.submitted[0].volume, 100) - self.assertEqual(order_gateway.submitted[0].remark, "mini") - self.assertEqual(order_gateway.cancelled[0].order_sys_id, "sys-1") - - def test_market_data_method_is_whitelisted_and_dispatched(self): - redis_client, service = _service() - - service.enqueue_payload( - { - "request_id": "market-data-ex", - "account_id": "acct", - "method": "get_market_data_ex", - "params": { - "field_list": ["close"], - "stock_list": ["600000.SH"], - "period": "1d", - "count": 1, - }, - } - ) - service.drain_pending() - - response = json.loads(redis_client.kv["bigqmt:rpc:resp:acct:market-data-ex"]) - self.assertTrue(response["ok"], response["error"]) - self.assertEqual(response["data"]["params"]["field_list"], ["close"]) - self.assertEqual(response["data"]["data"]["600000.SH"]["close"], [10.0]) - - -class DownloadHistoryDataTest(unittest.TestCase): - """Issue #32: download_history_data was routing to ContextInfo (which has - no such method) instead of the QMT-injected global function.""" - - def _handlers_with_qmt_global(self, func_name, func): - return BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - qmt_api={func_name: func}, - ) - - def test_download_history_data_calls_qmt_global(self): - calls = [] - - def fake_download(stock_code, period, start_time, end_time): - calls.append((stock_code, period, start_time, end_time)) - return True - - handlers = self._handlers_with_qmt_global("download_history_data", fake_download) - result = handlers.handle("download_history_data", { - "stock_code": "000001.SZ", - "period": "1d", - "start_time": "20230101", - "end_time": "", - }) - - self.assertEqual(calls, [("000001.SZ", "1d", "20230101", "")]) - self.assertTrue(result) - - def test_download_history_data2_calls_qmt_global(self): - calls = [] - - def fake_download2(stock_list, period, start_time, end_time): - calls.append((stock_list, period, start_time, end_time)) - return True - - handlers = self._handlers_with_qmt_global("download_history_data2", fake_download2) - result = handlers.handle("download_history_data2", { - "stock_list": ["000001.SZ", "600000.SH"], - "period": "1d", - "start_time": "20230101", - "end_time": "", - }) - - self.assertEqual(calls[0][0], ["000001.SZ", "600000.SH"]) - self.assertEqual(calls[0][1], "1d") - self.assertTrue(result) - - def test_download_history_data_fallback_to_adapter_when_no_global(self): - """When qmt_api has no download_history_data (e.g. outside QMT), - the handler falls back to the adapter path. With a FakeMarketData - that lacks the method, the handler returns False (graceful, not crash).""" - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - qmt_api={}, - ) - result = handlers.handle("download_history_data", { - "stock_code": "000001.SZ", - "period": "1d", - "start_time": "20230101", - "end_time": "", - }) - # No global func and adapter lacks the method → returns False, not crash. - self.assertFalse(result) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_runner.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_runner.py deleted file mode 100644 index 0bfecc0..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_runner.py +++ /dev/null @@ -1,179 +0,0 @@ -import datetime -import os -import sys -import unittest -from types import SimpleNamespace -from unittest import mock - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -import bigqmt_signal_trader_strategy as strategy_module - - -class FakeApp: - def __init__(self): - self.inited = 0 - self.ticks = [] - self.orders = [] - self.trades = [] - self.sync_reasons = [] - - def on_init(self, runtime): - self.inited += 1 - - def tick(self, now=None): - self.ticks.append(now) - - def on_order_event(self, event): - self.orders.append(event) - - def on_trade_event(self, event): - self.trades.append(event) - - def sync_positions(self, reason): - self.sync_reasons.append(reason) - - -class FakeContext: - def __init__(self): - self.accounts = [] - - def set_account(self, account_id): - self.accounts.append(account_id) - - -class FakeHistoryContext(FakeContext): - def is_last_bar(self): - return False - - -class FakeRpcService: - def __init__(self): - self.drained = [] - - def drain_pending(self, max_items=20): - self.drained.append(max_items) - return 0 - - def stop(self): - pass - - -class BigQmtStrategyRunnerTest(unittest.TestCase): - def setUp(self): - self.app = FakeApp() - strategy_module.reset_app() - strategy_module.set_app_factory(lambda context: self.app) - - def tearDown(self): - strategy_module.reset_app() - strategy_module.set_app_factory(None) - strategy_module.set_account_id("") - - def test_init_builds_app_and_calls_on_init(self): - strategy_module.init(FakeContext()) - - self.assertEqual(self.app.inited, 1) - - def test_init_sets_bigqmt_account_when_configured(self): - context = FakeContext() - strategy_module.set_account_id("test-account") - - strategy_module.init(context) - - self.assertEqual(context.accounts, ["test-account"]) - - def test_init_detects_bigqmt_account_from_runtime_global(self): - context = FakeContext() - strategy_module.account = "runtime-account" - try: - strategy_module.init(context) - finally: - delattr(strategy_module, "account") - - self.assertEqual(context.accounts, ["runtime-account"]) - - def test_adjust_forwards_to_app_tick(self): - strategy_module.init(FakeContext()) - strategy_module.adjust(FakeContext()) - - self.assertEqual(len(self.app.ticks), 1) - self.assertIsInstance(self.app.ticks[0], datetime.datetime) - - def test_handlebar_forwards_to_app_tick(self): - strategy_module.init(FakeContext()) - strategy_module.handlebar(FakeContext()) - - self.assertEqual(len(self.app.ticks), 1) - self.assertIsInstance(self.app.ticks[0], datetime.datetime) - - def test_adjust_skips_history_bars_when_bigqmt_exposes_is_last_bar(self): - strategy_module.init(FakeContext()) - - strategy_module.adjust(FakeHistoryContext()) - - self.assertEqual(self.app.ticks, []) - - def test_adjust_drains_rpc_even_when_not_last_bar(self): - rpc_service = FakeRpcService() - strategy_module._rpc_service = rpc_service - - strategy_module.adjust(FakeHistoryContext()) - - self.assertEqual(rpc_service.drained, [20]) - self.assertEqual(self.app.ticks, []) - - def test_zmq_rpc_build_does_not_create_redis_clients(self): - config = { - "account_id": "acct", - "enable_rpc": True, - "rpc": { - "enabled": True, - "account_id": "acct", - "transport": "zmq", - "zmq": {"connect_address": "tcp://127.0.0.1:20146"}, - "background_threads": True, - }, - "qmt_api": {}, - } - app = SimpleNamespace(order_gateway=None, position_sync_sink=None) - - with mock.patch( - "bigqmt_signal_trader.adapters.redis_common.build_redis_client", - side_effect=AssertionError("ZMQ mode must not build Redis clients"), - ): - service = strategy_module._build_rpc_service(FakeContext(), app, config) - - self.assertIsNone(service.listen_redis) - self.assertIsNone(service.redis) - self.assertEqual(service._transport.name, "zmq") - - def test_bigqmt_named_callbacks_forward_to_app(self): - strategy_module.init(FakeContext()) - order = object() - trade = object() - - strategy_module.order_callback(FakeContext(), order) - strategy_module.deal_callback(FakeContext(), trade) - - self.assertEqual(self.app.orders, [order]) - self.assertEqual(self.app.trades, [trade]) - - def test_only_bigqmt_named_callbacks_are_exposed(self): - self.assertFalse(hasattr(strategy_module, "on_order")) - self.assertFalse(hasattr(strategy_module, "on_trade")) - self.assertTrue(hasattr(strategy_module, "order_callback")) - self.assertTrue(hasattr(strategy_module, "deal_callback")) - - def test_manual_sync_forwards_to_app(self): - strategy_module.init(FakeContext()) - - strategy_module.sync_positions(FakeContext()) - - self.assertEqual(self.app.sync_reasons, ["manual"]) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transport_selection.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transport_selection.py deleted file mode 100644 index c1e279b..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transport_selection.py +++ /dev/null @@ -1,42 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader_strategy import _is_redis_transport, _resolve_background_threads - - -class BackgroundThreadResolutionTest(unittest.TestCase): - def test_redis_transport_keeps_configured_value(self): - # Redis (default) honors the configured flag — adjust-drain path when off. - self.assertFalse(_resolve_background_threads("redis", False)) - self.assertTrue(_resolve_background_threads("redis", True)) - self.assertFalse(_resolve_background_threads("", False)) - self.assertFalse(_resolve_background_threads("default", False)) - self.assertFalse(_resolve_background_threads(None, False)) - - def test_zmq_transport_forces_background_threads_on(self): - # ZMQ must run its background router thread regardless of config — - # without it, requests are never received (start_receiving(False) - # only binds the socket, never polls it). - self.assertTrue(_resolve_background_threads("zmq", False)) - self.assertTrue(_resolve_background_threads("ZMQ", False)) - self.assertTrue(_resolve_background_threads("zmq", True)) - - def test_other_non_redis_transports_force_background_threads_on(self): - for name in ("mysql", "shm"): - self.assertTrue(_resolve_background_threads(name, False), name) - self.assertTrue(_resolve_background_threads(name, True), name) - - def test_is_redis_transport(self): - for name in ("redis", "", "default", None, "REDIS", "Default"): - self.assertTrue(_is_redis_transport(name), name) - for name in ("zmq", "mysql", "shm", "ZMQ"): - self.assertFalse(_is_redis_transport(name), name) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transports.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transports.py deleted file mode 100644 index ae53964..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_transports.py +++ /dev/null @@ -1,352 +0,0 @@ -# coding: utf-8 -"""Tests for the pluggable transport layer. - -Covers: -* ``FakeTransport`` round-trip (validates the abstract contract). -* ZMQ transport round-trip on tcp loopback (the main low-latency path). -* MySQL transport round-trip against an in-memory sqlite3 DB (driver-agnostic). -* Factory dispatch: name → transport class, unknown-name rejection, shm raising. -""" - -import os -import sys -import threading -import time -import unittest - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.transports import ( # noqa: E402 - RpcTransport, - TransportError, - TransportTimeout, - build_transport, -) -from bigqmt_signal_trader.transports.base import RpcTransport as Base # noqa: E402 - - -class FakeTransport(RpcTransport): - """In-process transport: a thread-safe queue pair. Validates the contract.""" - - name = "fake" - - def __init__(self, account_id="", print_prefix="[fake]"): - super(FakeTransport, self).__init__(account_id=account_id, print_prefix=print_prefix) - self._req_q = [] # inbound requests waiting for the server - self._resp_q = {} # request_id -> response - self._lock = threading.Lock() - - def send_request(self, request, timeout_seconds): - rid = request["request_id"] - # Hand the request to the server (call on_request), then collect reply. - with self._lock: - self._req_q.append(request) - # Server processing: the registered callback handles it immediately. - callback = self._on_request - if callback is None: - raise TransportError("no server registered") - response = callback(request) or {} - with self._lock: - self._resp_q[rid] = response - return response - - def send_response(self, request, response): - rid = response.get("request_id") or request.get("request_id") - with self._lock: - self._resp_q[rid] = response - - def start_receiving(self, on_request, background_threads=True): - super(FakeTransport, self).start_receiving(on_request) - - -def _build_request(method="ping", params=None, account_id="acct"): - import uuid - - return { - "schema_version": 1, - "request_id": uuid.uuid4().hex, - "account_id": account_id, - "method": method, - "params": params or {}, - "reply_channel": "", - "reply_list": "", - "reply_key": "", - "ttl_seconds": 5, - } - - -class FakeTransportTest(unittest.TestCase): - def test_round_trip(self): - server = FakeTransport(account_id="acct") - server.start_receiving(lambda req: {"ok": True, "request_id": req["request_id"], "data": {"echo": req["params"]}}) - client = FakeTransport(account_id="acct") - client._on_request = server._on_request # share the handler - req = _build_request(params={"x": 1}) - resp = client.send_request(req, 2.0) - self.assertTrue(resp["ok"]) - self.assertEqual(resp["request_id"], req["request_id"]) - self.assertEqual(resp["data"], {"echo": {"x": 1}}) - - def test_deliver_auto_sends_response(self): - server = FakeTransport(account_id="acct") - sent = [] - server.send_response = lambda req, resp: sent.append(resp) # capture - server.start_receiving(lambda req: {"ok": True, "request_id": req["request_id"]}) - server.deliver(_build_request()) - self.assertEqual(len(sent), 1) - self.assertTrue(sent[0]["ok"]) - - def test_deliver_turns_handler_exception_into_error_envelope(self): - server = FakeTransport(account_id="acct") - sent = [] - server.send_response = lambda req, resp: sent.append(resp) - def boom(req): - raise ValueError("handler exploded") - server.start_receiving(boom) - server.deliver(_build_request()) - self.assertEqual(len(sent), 1) - self.assertFalse(sent[0]["ok"]) - self.assertIn("handler exploded", sent[0]["error"]) - - -class FactoryTest(unittest.TestCase): - def test_unknown_transport_rejected(self): - with self.assertRaises(ValueError): - build_transport("nonsense", {}, account_id="x") - - def test_shm_raises_on_use(self): - shm = build_transport("shm", {}, account_id="x") - with self.assertRaises(TransportError): - shm.send_request(_build_request(), 1.0) - - def test_redis_factory_with_injected_clients(self): - sentinel_listen = object() - sentinel_resp = object() - t = build_transport( - "redis", - {"redis_client": sentinel_listen, "response_redis_client": sentinel_resp}, - account_id="acct", - ) - self.assertIs(t.listen_redis, sentinel_listen) - self.assertIs(t.redis, sentinel_resp) - self.assertEqual(t.account_id, "acct") - - -class ZmqTransportTest(unittest.TestCase): - """Round-trip over tcp loopback. Skipped if pyzmq isn't installed.""" - - def setUp(self): - try: - import zmq # noqa: F401 - except ImportError: - self.skipTest("pyzmq not installed") - # Find a free port to avoid collisions between test runs. - import socket - - s = socket.socket() - s.bind(("127.0.0.1", 0)) - self.port = s.getsockname()[1] - s.close() - self.address = "tcp://127.0.0.1:%d" % self.port - - def test_round_trip(self): - from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - server = ZmqTransport( - bind_address=self.address, account_id="acct", recv_timeout_seconds=0.3 - ) - - def on_req(req): - return { - "schema_version": 1, - "request_id": req["request_id"], - "account_id": "acct", - "method": req["method"], - "ok": True, - "data": {"pong": True}, - "error": "", - "handled_at": "now", - } - - server.start_receiving(on_req, background_threads=True) - try: - time.sleep(0.4) - client = ZmqTransport(connect_address=self.address, account_id="acct") - time.sleep(0.3) - req = _build_request() - resp = client.send_request(req, timeout_seconds=3.0) - self.assertTrue(resp["ok"]) - self.assertEqual(resp["request_id"], req["request_id"]) - self.assertTrue(resp["data"]["pong"]) - client.stop() - finally: - server.stop() - - def test_main_thread_drain_round_trip(self): - from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - server = ZmqTransport( - bind_address=self.address, account_id="acct", recv_timeout_seconds=0.01 - ) - server.start_receiving( - lambda request: { - "request_id": request["request_id"], - "account_id": "acct", - "method": request["method"], - "ok": True, - "data": {"main_thread": True}, - "error": "", - }, - background_threads=False, - ) - client = ZmqTransport(connect_address=self.address, account_id="acct") - result = {} - - def call_client(): - result["response"] = client.send_request(_build_request(), timeout_seconds=2.0) - - try: - thread = threading.Thread(target=call_client) - thread.start() - deadline = time.time() + 1.0 - while thread.is_alive() and time.time() < deadline: - server.drain_request_queue(max_items=10) - time.sleep(0.01) - thread.join(1.0) - self.assertFalse(thread.is_alive()) - self.assertEqual(result["response"]["data"], {"main_thread": True}) - finally: - client.stop() - server.stop() - - def test_duplicate_server_address_fails_without_port_fallback(self): - from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - first = ZmqTransport(bind_address=self.address, account_id="acct") - duplicate = ZmqTransport( - bind_address=self.address, account_id="acct", port_scan_range=50 - ) - first.start_receiving(lambda _request: {}, background_threads=False) - try: - with self.assertRaisesRegex(TransportError, "ZMQ_BIND_CONFLICT"): - duplicate.start_receiving( - lambda _request: {}, background_threads=False - ) - self.assertIsNone(duplicate._actual_bind_address) - finally: - duplicate.stop() - first.stop() - - def test_deferred_response_returns_through_router_thread(self): - from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - pending = [] - server = ZmqTransport( - bind_address=self.address, account_id="acct", recv_timeout_seconds=0.05 - ) - server.start_receiving(lambda request: pending.append(request), background_threads=True) - client = ZmqTransport(connect_address=self.address, account_id="acct") - result = {} - - def call_client(): - result["response"] = client.send_request(_build_request(), timeout_seconds=2.0) - - try: - thread = threading.Thread(target=call_client) - thread.start() - deadline = time.time() + 1.0 - while not pending and time.time() < deadline: - time.sleep(0.01) - self.assertTrue(pending) - request = pending[0] - server.send_response( - request, - { - "request_id": request["request_id"], - "ok": True, - "data": {"deferred": True}, - "error": "", - }, - ) - thread.join(2.0) - self.assertFalse(thread.is_alive()) - self.assertEqual(result["response"]["data"], {"deferred": True}) - finally: - client.stop() - server.stop() - - def test_timeout_raises(self): - from bigqmt_signal_trader.transports.zmq_transport import ZmqTransport - - # Client connects to a port with no server → times out. - client = ZmqTransport(connect_address=self.address, account_id="acct") - time.sleep(0.2) - with self.assertRaises(TransportTimeout): - client.send_request(_build_request(), timeout_seconds=0.5) - client.stop() - - -class MysqlTransportTest(unittest.TestCase): - """Round-trip over sqlite3 (the transport is driver-agnostic).""" - - def setUp(self): - try: - import sqlite3 # noqa: F401 - except ImportError: - self.skipTest("sqlite3 not available") - self.db_path = os.path.join( - os.path.dirname(__file__), "..", "..", "_test_rpc_%s.sqlite" % os.getpid() - ) - if os.path.exists(self.db_path): - os.unlink(self.db_path) - - def tearDown(self): - if os.path.exists(self.db_path): - try: - os.unlink(self.db_path) - except Exception: - pass - - def test_round_trip(self): - from bigqmt_signal_trader.transports.mysql_transport import MysqlTransport - - cfg = { - "driver": "sqlite3", - "connect_kwargs": {"database": self.db_path, "check_same_thread": False}, - "account_id": "acct", - "poll_interval_seconds": 0.01, - # sqlite connections are thread-bound; keep them single-threaded in - # the pool. Real MySQL doesn't need this. - "pool_config": {"mincached": 1, "maxcached": 2, "maxshared": 0, "maxconnections": 2}, - } - server = MysqlTransport.from_config(cfg, account_id="acct") - server._ensure_schema() - - def on_req(req): - return { - "schema_version": 1, - "request_id": req["request_id"], - "account_id": "acct", - "method": req["method"], - "ok": True, - "data": {"pong": True}, - "error": "", - "handled_at": "now", - } - - server.start_receiving(on_req, background_threads=True) - try: - client = MysqlTransport.from_config(cfg, account_id="acct") - req = _build_request() - resp = client.send_request(req, timeout_seconds=3.0) - self.assertTrue(resp["ok"]) - self.assertEqual(resp["request_id"], req["request_id"]) - self.assertTrue(resp["data"]["pong"]) - client.stop() - finally: - server.stop() - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_client.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_client.py deleted file mode 100644 index e0340e6..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_client.py +++ /dev/null @@ -1,284 +0,0 @@ -import os -import sys -import threading -import time -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.whole_quote_session import WholeQuoteClientSession - - -class FakeRpc: - """Records control RPCs and returns canned subscribe responses.""" - - def __init__(self): - self.calls = [] - self._lock = threading.Lock() - - def __call__(self, method, params): - with self._lock: - self.calls.append((method, dict(params))) - if method == "subscribe_whole_quote": - codes = sorted(str(c).upper() for c in params.get("codes") or []) - return {"combo_key": ",".join(codes), "topic": ",".join(codes)} - return {} - - def methods(self): - with self._lock: - return [m for m, _ in self.calls] - - -class FakeRpcWithRestart(FakeRpc): - """Simulates a server restart: keepalive fails for a window of calls, - then the server is back (subscribe succeeds again).""" - - def __init__(self, fail_start=0, fail_count=3): - super().__init__() - self.fail_start = fail_start - self.fail_count = fail_count - - def __call__(self, method, params): - with self._lock: - self.calls.append((method, dict(params))) - if method == "quote_keepalive": - n = sum(1 for m, _ in self.calls if m == "quote_keepalive") - 1 # 本次 - if method == "quote_keepalive" and self.fail_start <= n < self.fail_start + self.fail_count: - raise RuntimeError("server restarting") - if method == "subscribe_whole_quote": - codes = sorted(str(c).upper() for c in params.get("codes") or []) - return {"combo_key": ",".join(codes), "topic": ",".join(codes)} - return {} - - -class FakePushChannel: - """Client-side push channel stand-in: lets tests inject server pushes.""" - - def __init__(self): - self.subscriptions = [] # list of (topics_tuple, on_msg) - self.started = False - self.stopped = False - self._on_msg = None - - def start_subscriber(self, topics, on_msg): - self.started = True - self._on_msg = on_msg - self.subscriptions.append(tuple(topics)) - - def inject(self, topic, data): - if self._on_msg is not None: - self._on_msg(topic, data) - - def stop(self): - self.stopped = True - - -class FakePushChannelWithTopics(FakePushChannel): - """Like FakePushChannel but tracks the currently subscribed topic set, so - the session can diff and reuse an existing subscriber.""" - - def __init__(self): - super().__init__() - self.active_topics = frozenset() - - def start_subscriber(self, topics, on_msg): - super().start_subscriber(topics, on_msg) - self.active_topics = frozenset(topics) - - def stop(self): - super().stop() - self.active_topics = frozenset() - - -class WholeQuoteSessionTest(unittest.TestCase): - def _session(self, **kwargs): - rpc = FakeRpc() - channel = FakePushChannel() - session = WholeQuoteClientSession( - rpc_call=rpc, - push_channel=channel, - client_id="client-test", - heartbeat_interval_seconds=kwargs.pop("heartbeat_interval_seconds", 0.05), - **kwargs, - ) - return session, rpc, channel - - def test_subscribe_sends_rpc_and_returns_sub_id(self): - session, rpc, _channel = self._session() - sub_id = session.subscribe_whole_quote(["SH", "SZ"], callback=lambda d: None) - self.assertIsNotNone(sub_id) - self.assertIn("subscribe_whole_quote", rpc.methods()) - - def test_subscribe_starts_push_channel_with_topic(self): - session, _rpc, channel = self._session() - session.subscribe_whole_quote(["SH", "SZ"], callback=lambda d: None) - self.assertTrue(channel.started) - self.assertIn(("SH,SZ",), channel.subscriptions) - - def test_incoming_push_invokes_callback(self): - session, _rpc, channel = self._session() - received = [] - session.subscribe_whole_quote(["SH"], callback=received.append) - channel.inject("SH", {"000001.SZ": {"lastPrice": 10.5}}) - self.assertEqual(received, [{"000001.SZ": {"lastPrice": 10.5}}]) - - def test_two_subscriptions_same_combo_share_one_push(self): - session, _rpc, channel = self._session() - got_a, got_b = [], [] - session.subscribe_whole_quote(["SH", "SZ"], callback=got_a.append) - session.subscribe_whole_quote(["sz", "sh"], callback=got_b.append) - channel.inject("SH,SZ", {"000001.SZ": {"lastPrice": 1.0}}) - self.assertEqual(len(got_a), 1) - self.assertEqual(len(got_b), 1) - - def test_unsubscribe_stops_callback_and_sends_rpc(self): - session, rpc, channel = self._session() - received = [] - sub_id = session.subscribe_whole_quote(["SH"], callback=received.append) - session.unsubscribe_quote(sub_id) - self.assertIn("unsubscribe_whole_quote", rpc.methods()) - channel.inject("SH", {"x": 1}) - self.assertEqual(received, []) - - def test_keepalive_sent_for_active_subscriptions(self): - session, rpc, _channel = self._session(heartbeat_interval_seconds=0.05) - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.start() - try: - deadline = time.time() + 1.5 - while time.time() < deadline and rpc.methods().count("quote_keepalive") < 2: - time.sleep(0.02) - finally: - session.stop() - self.assertGreaterEqual(rpc.methods().count("quote_keepalive"), 2) - - def test_keepalive_stops_after_unsubscribe(self): - session, rpc, _channel = self._session(heartbeat_interval_seconds=0.05) - sub_id = session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.start() - time.sleep(0.15) - session.unsubscribe_quote(sub_id) - count_at_unsub = rpc.methods().count("quote_keepalive") - time.sleep(0.2) - session.stop() - self.assertEqual(rpc.methods().count("quote_keepalive"), count_at_unsub) - - def test_replay_resubscribes_all_active(self): - session, rpc, _channel = self._session() - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.subscribe_whole_quote(["SZ"], callback=lambda d: None) - subscribes_before = rpc.methods().count("subscribe_whole_quote") - session.replay_subscriptions() - self.assertEqual(rpc.methods().count("subscribe_whole_quote"), subscribes_before + 2) - - def test_client_id_used_in_rpc(self): - session, rpc, _channel = self._session() - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - sub_params = [p for m, p in rpc.calls if m == "subscribe_whole_quote"][0] - self.assertEqual(sub_params["client_id"], "client-test") - - def test_auto_replay_after_server_restart(self): - """服务端重启后, 心跳线程应检测到 keepalive 失败并在服务端恢复后 - 自动重放订阅(否则推送永久中断)。""" - rpc = FakeRpcWithRestart(fail_start=1, fail_count=3) # 第1-3次keepalive失败 - channel = FakePushChannelWithTopics() - session = WholeQuoteClientSession( - rpc_call=rpc, push_channel=channel, client_id="client-test", - heartbeat_interval_seconds=0.05, - ) - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.start() - try: - deadline = time.time() + 2.0 - # 等待: keepalive 失败触发重放, 重放后又有新的 keepalive - while time.time() < deadline: - if rpc.methods().count("subscribe_whole_quote") >= 2 and \ - rpc.methods().count("quote_keepalive") >= 5: - break - time.sleep(0.02) - subs = rpc.methods().count("subscribe_whole_quote") - kps = rpc.methods().count("quote_keepalive") - print("auto-replay: subscribe=%d keepalive=%d" % (subs, kps)) - self.assertGreaterEqual(subs, 2, "服务端恢复后应重放订阅") - self.assertGreaterEqual(kps, 5) - finally: - session.stop() - - def test_no_replay_when_server_healthy(self): - """服务端健康时不应反复重放订阅(只保留初始 subscribe)。""" - session, rpc, _channel = self._session(heartbeat_interval_seconds=0.05) - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.start() - try: - time.sleep(0.4) - self.assertEqual(rpc.methods().count("subscribe_whole_quote"), 1) - finally: - session.stop() - - def test_replay_when_push_silent_after_restart(self): - """服务端重启后 keepalive 可能不失败(redis 队列兜住),但推送会静默。 - 客户端应在推送静默超过阈值后自动重放订阅。""" - rpc = FakeRpc() # keepalive 从不失败 - channel = FakePushChannelWithTopics() - session = WholeQuoteClientSession( - rpc_call=rpc, push_channel=channel, client_id="client-test", - heartbeat_interval_seconds=0.05, - push_silence_replay_heartbeats=2, # 2 个心跳周期无推送即重放 - ) - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.start() - try: - deadline = time.time() + 1.5 - while time.time() < deadline: - if rpc.methods().count("subscribe_whole_quote") >= 2: - break - time.sleep(0.02) - self.assertGreaterEqual( - rpc.methods().count("subscribe_whole_quote"), 2, - "推送静默超阈值应自动重放订阅", - ) - finally: - session.stop() - - def test_subscriber_reused_when_topic_set_unchanged(self): - """订阅/退订不应为同一 topic 集合反复重建订阅线程(线程泄漏)。""" - rpc = FakeRpc() - channel = FakePushChannelWithTopics() - session = WholeQuoteClientSession( - rpc_call=rpc, push_channel=channel, client_id="client-test", - heartbeat_interval_seconds=0.05, - ) - sub1 = session.subscribe_whole_quote(["SH"], callback=lambda d: None) - sub2 = session.subscribe_whole_quote(["SH"], callback=lambda d: None) - # 两次同 topic 订阅:共享订阅线程,只 start 一次 - self.assertEqual(len(channel.subscriptions), 1) - # 退订一个 sub_id:topic 集合不变,不应重启线程 - session.unsubscribe_quote(sub1) - self.assertEqual(len(channel.subscriptions), 1) - self.assertFalse(channel.stopped) - # 退订最后一个 sub_id:topic 集合变空,应停掉线程 - session.unsubscribe_quote(sub2) - self.assertTrue(channel.stopped) - self.assertEqual(len(channel.subscriptions), 1) - - def test_subscriber_restarts_when_topic_set_changes(self): - """topic 集合变化时重建订阅线程,但旧线程先 stop。""" - rpc = FakeRpc() - channel = FakePushChannelWithTopics() - session = WholeQuoteClientSession( - rpc_call=rpc, push_channel=channel, client_id="client-test", - heartbeat_interval_seconds=0.05, - ) - session.subscribe_whole_quote(["SH"], callback=lambda d: None) - session.subscribe_whole_quote(["SZ"], callback=lambda d: None) - # topic 集合从 {SH} 变成 {SH,SZ} -> 重建(但旧 stop) - self.assertEqual(len(channel.subscriptions), 2) - self.assertTrue(channel.stopped) - # 新线程订阅 {SH,SZ} - self.assertEqual(channel.active_topics, frozenset(["SH", "SZ"])) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_e2e.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_e2e.py deleted file mode 100644 index 9391fd4..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_e2e.py +++ /dev/null @@ -1,203 +0,0 @@ -import os -import sys -import threading -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_subscription_manager import ( - QuoteSourceAdapter, - QuoteSubscriptionManager, -) -from bigqmt_signal_trader.redis_rpc import BigQmtRpcHandlers -from bigqmt_signal_trader.whole_quote_session import WholeQuoteClientSession - - -class FakeQuoteSource(QuoteSourceAdapter): - def __init__(self): - self.subscriptions = {} - self.unsubscribed = [] - self._next = 0 - - def subscribe(self, codes, on_push): - self._next += 1 - handle = self._next - self.subscriptions[handle] = {"codes": list(codes), "on_push": on_push} - return handle - - def unsubscribe(self, handle): - self.unsubscribed.append(handle) - self.subscriptions.pop(handle, None) - - def fire(self, data_by_combo_codes): - # Fire every live subscription's on_push with the given data. - for handle, sub in list(self.subscriptions.items()): - sub["on_push"](data_by_combo_codes) - - -class FakeMarketData: - def get_ticks(self, codes): - return {} - - -class FakePositionProvider: - def get_positions(self, account_id): - return {} - - def get_asset(self, account_id): - return None - - -class InProcPushBus: - """Stands in for the QuotePushChannel: server publishes, every client channel - subscribed to the topic receives (topic, data).""" - - def __init__(self): - self._subscribers = [] # list of _BusSubscriber - - def register(self, subscriber): - self._subscribers.append(subscriber) - - def publish(self, topic, data): - for sub in list(self._subscribers): - sub._deliver(topic, data) - - -class ClientPushChannel: - """Client-side push channel wired to the bus. Mirrors QuotePushChannel's - subscriber surface used by WholeQuoteClientSession.""" - - def __init__(self, bus): - self.bus = bus - self.topics = [] - self._on_msg = None - bus.register(self) - - def start_subscriber(self, topics, on_msg): - self.topics = list(topics) - self._on_msg = on_msg - - def _deliver(self, topic, data): - if self._on_msg is not None and topic in self.topics: - self._on_msg(topic, data) - - def stop(self): - pass - - -class WholeQuoteE2ETest(unittest.TestCase): - def _server(self, heartbeat_timeout=30.0, clock=None): - source = FakeQuoteSource() - bus = InProcPushBus() - manager = QuoteSubscriptionManager( - source, - heartbeat_timeout_seconds=heartbeat_timeout, - time_func=clock, - on_push_publisher=bus.publish, - ) - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - quote_subscription_manager=manager, - ) - return source, bus, manager, handlers - - def _client(self, client_id, bus, handlers): - def rpc_call(method, params): - return handlers.handle(method, params) - - return WholeQuoteClientSession( - rpc_call=rpc_call, - push_channel=ClientPushChannel(bus), - client_id=client_id, - heartbeat_interval_seconds=0.05, - ) - - def test_two_clients_share_one_qmt_subscription_and_both_receive(self): - source, bus, manager, handlers = self._server() - client_a = self._client("clientA", bus, handlers) - client_b = self._client("clientB", bus, handlers) - got_a, got_b = [], [] - client_a.subscribe_whole_quote(["SH", "SZ"], callback=got_a.append) - client_b.subscribe_whole_quote(["sz", "sh"], callback=got_b.append) - - # One shared big-QMT subscription for the same normalized combo. - self.assertEqual(len(source.subscriptions), 1) - source.fire({"000001.SZ": {"lastPrice": 10.5}}) - self.assertEqual(len(got_a), 1) - self.assertEqual(len(got_b), 1) - - def test_one_unsubscribe_keeps_other_receiving(self): - source, bus, manager, handlers = self._server() - client_a = self._client("clientA", bus, handlers) - client_b = self._client("clientB", bus, handlers) - got_a, got_b = [], [] - sub_a = client_a.subscribe_whole_quote(["SH"], callback=got_a.append) - client_b.subscribe_whole_quote(["SH"], callback=got_b.append) - - client_a.unsubscribe_quote(sub_a) - self.assertEqual(len(source.subscriptions), 1) # still alive for clientB - source.fire({"x": 1}) - self.assertEqual(got_a, []) - self.assertEqual(len(got_b), 1) - - def test_all_unsubscribe_tears_down_qmt_subscription(self): - source, bus, manager, handlers = self._server() - client_a = self._client("clientA", bus, handlers) - client_b = self._client("clientB", bus, handlers) - sub_a = client_a.subscribe_whole_quote(["SH"], callback=lambda d: None) - sub_b = client_b.subscribe_whole_quote(["SH"], callback=lambda d: None) - client_a.unsubscribe_quote(sub_a) - client_b.unsubscribe_quote(sub_b) - self.assertEqual(len(source.subscriptions), 0) - self.assertEqual(len(source.unsubscribed), 1) - - def test_server_restart_client_replay_restores_push(self): - source, bus, manager, handlers = self._server() - client = self._client("clientA", bus, handlers) - received = [] - client.subscribe_whole_quote(["SH"], callback=received.append) - self.assertEqual(len(source.subscriptions), 1) - - # Server "restarts": a brand-new manager/source/handlers on the same bus. - source2 = FakeQuoteSource() - manager2 = QuoteSubscriptionManager(source2, on_push_publisher=bus.publish) - handlers2 = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - quote_subscription_manager=manager2, - ) - self.assertEqual(len(source2.subscriptions), 0) - - # Client detects the restart and replays its subscriptions. - def rpc_call2(method, params): - return handlers2.handle(method, params) - - client._rpc = rpc_call2 - client.replay_subscriptions() - - self.assertEqual(len(source2.subscriptions), 1) - source2.fire({"000001.SZ": {"lastPrice": 11.0}}) - self.assertEqual(len(received), 1) - self.assertEqual(received[0]["000001.SZ"]["lastPrice"], 11.0) - - def test_silent_client_reaped_and_qmt_subscription_torn_down(self): - clock = [1000.0] - source, bus, manager, handlers = self._server(clock=lambda: clock[0]) - client = self._client("clientA", bus, handlers) - client.subscribe_whole_quote(["SH"], callback=lambda d: None) - self.assertEqual(len(source.subscriptions), 1) - - # Client goes silent (no keepalive); clock advances past the timeout. - clock[0] += 31.0 - manager.reap_expired() - self.assertEqual(len(source.subscriptions), 0) - self.assertEqual(len(source.unsubscribed), 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_rpc_handlers.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_rpc_handlers.py deleted file mode 100644 index 8e41c0d..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_whole_quote_rpc_handlers.py +++ /dev/null @@ -1,117 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.quote_subscription_manager import ( - QuoteSourceAdapter, - QuoteSubscriptionManager, -) -from bigqmt_signal_trader.redis_rpc import BigQmtRpcHandlers - - -class FakeQuoteSource(QuoteSourceAdapter): - def __init__(self): - self.subscriptions = {} - self.unsubscribed = [] - self._next_handle = 0 - - def subscribe(self, codes, on_push): - self._next_handle += 1 - handle = self._next_handle - self.subscriptions[handle] = {"codes": list(codes), "on_push": on_push} - return handle - - def unsubscribe(self, handle): - self.unsubscribed.append(handle) - self.subscriptions.pop(handle, None) - - -class FakeMarketData: - def get_ticks(self, codes): - return {} - - -class FakePositionProvider: - def get_positions(self, account_id): - return {} - - def get_asset(self, account_id): - return None - - -def _handlers(with_manager=True): - source = FakeQuoteSource() - manager = QuoteSubscriptionManager(source) if with_manager else None - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - quote_subscription_manager=manager, - ) - return handlers, source - - -class WholeQuoteRpcHandlersTest(unittest.TestCase): - def test_subscribe_whole_quote_allowed_and_creates_subscription(self): - handlers, source = _handlers() - result = handlers.handle( - "subscribe_whole_quote", - {"client_id": "c1", "sub_id": "s1", "codes": ["SH", "SZ"]}, - ) - self.assertEqual(len(source.subscriptions), 1) - self.assertEqual(result["combo_key"], "SH,SZ") - self.assertIn("topic", result) - - def test_subscribe_whole_quote_idempotent_replay(self): - handlers, source = _handlers() - params = {"client_id": "c1", "sub_id": "s1", "codes": ["SH", "SZ"]} - handlers.handle("subscribe_whole_quote", params) - handlers.handle("subscribe_whole_quote", params) # replay after recovery - self.assertEqual(len(source.subscriptions), 1) - - def test_subscribe_whole_quote_requires_client_and_codes(self): - handlers, _source = _handlers() - with self.assertRaises(ValueError): - handlers.handle("subscribe_whole_quote", {"sub_id": "s1", "codes": ["SH"]}) - with self.assertRaises(ValueError): - handlers.handle("subscribe_whole_quote", {"client_id": "c1", "sub_id": "s1", "codes": []}) - - def test_unsubscribe_whole_quote_tears_down_last_client(self): - handlers, source = _handlers() - handlers.handle("subscribe_whole_quote", {"client_id": "c1", "sub_id": "s1", "codes": ["SH"]}) - handlers.handle("unsubscribe_whole_quote", {"client_id": "c1", "sub_id": "s1"}) - self.assertEqual(len(source.subscriptions), 0) - self.assertEqual(len(source.unsubscribed), 1) - - def test_quote_keepalive_ok(self): - handlers, _source = _handlers() - handlers.handle("subscribe_whole_quote", {"client_id": "c1", "sub_id": "s1", "codes": ["SH"]}) - result = handlers.handle("quote_keepalive", {"client_id": "c1", "sub_id": "s1"}) - self.assertEqual(result, {}) - - def test_quote_methods_rejected_without_manager(self): - handlers, _source = _handlers(with_manager=False) - with self.assertRaises(RuntimeError): - handlers.handle("subscribe_whole_quote", {"client_id": "c1", "sub_id": "s1", "codes": ["SH"]}) - with self.assertRaises(RuntimeError): - handlers.handle("quote_keepalive", {"client_id": "c1", "sub_id": "s1"}) - - def test_quote_methods_in_default_allowed_set(self): - # The three methods must be reachable through the default whitelist (no - # explicit allowed_methods passed), same as every other read method. - handlers = BigQmtRpcHandlers( - account_id="acct", - market_data=FakeMarketData(), - position_provider=FakePositionProvider(), - quote_subscription_manager=QuoteSubscriptionManager(FakeQuoteSource()), - ) - for method in ("subscribe_whole_quote", "unsubscribe_whole_quote", "quote_keepalive"): - self.assertIn(method, handlers.allowed_methods) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtdata_whole_quote.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtdata_whole_quote.py deleted file mode 100644 index 7bfd24c..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtdata_whole_quote.py +++ /dev/null @@ -1,101 +0,0 @@ -import os -import sys -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -import bigqmt_signal_trader.xtquant_compat as compat - - -class FakeSession: - """Captures BigQmtXtData -> WholeQuoteClientSession delegation.""" - - def __init__(self): - self.subscribed = [] - self.unsubscribed = [] - self.started = False - self._next = 0 - self._active = set() - - def subscribe_whole_quote(self, code_list, callback=None): - self._next += 1 - self._active.add(self._next) - self.subscribed.append((list(code_list), callback)) - return self._next - - def unsubscribe_quote(self, sub_id): - self.unsubscribed.append(sub_id) - self._active.discard(sub_id) - return 0 - - def has_subscription(self, sub_id): - return sub_id in self._active - - def start(self): - self.started = True - - def stop(self): - pass - - -class FakeClient: - def __init__(self): - self.account_id = "acct" - self.local_cache_config = {} - self.full_tick_cache_config = {} - self.transport_name = "redis" - self.calls = [] - - def call(self, method, params=None, **kwargs): - self.calls.append((method, params)) - if method == "get_full_tick": - return {c: {"lastPrice": 1.0} for c in (params or {}).get("codes") or []} - return {} - - def _redis(self): - raise AssertionError("redis not expected in this test") - - -class XtDataWholeQuoteDelegationTest(unittest.TestCase): - def _xtdata(self, session): - client = FakeClient() - data = compat.BigQmtXtData(client) - data._quote_session_factory = lambda: session - return data, client - - def test_subscribe_delegates_to_session_and_primes_full_tick(self): - session = FakeSession() - data, _client = self._xtdata(session) - received = [] - sub_id = data.subscribe_whole_quote(["000001.SZ"], callback=received.append) - # Delegated to the session. - self.assertEqual(session.subscribed[0][0], ["000001.SZ"]) - self.assertEqual(sub_id, 1) - # Primed via get_full_tick so the callback fires once with the snapshot. - self.assertEqual(received, [{"000001.SZ": {"lastPrice": 1.0}}]) - - def test_subscribe_starts_session_once(self): - session = FakeSession() - data, _client = self._xtdata(session) - data.subscribe_whole_quote(["SH"], callback=lambda d: None) - self.assertTrue(session.started) - - def test_unsubscribe_delegates_to_session(self): - session = FakeSession() - data, _client = self._xtdata(session) - sub_id = data.subscribe_whole_quote(["SH"], callback=lambda d: None) - data.unsubscribe_quote(sub_id) - self.assertEqual(session.unsubscribed, [sub_id]) - - def test_session_reused_across_subscriptions(self): - session = FakeSession() - data, _client = self._xtdata(session) - data.subscribe_whole_quote(["SH"], callback=lambda d: None) - data.subscribe_whole_quote(["SZ"], callback=lambda d: None) - self.assertEqual(len(session.subscribed), 2) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtquant_compat.py b/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtquant_compat.py deleted file mode 100644 index bccab6d..0000000 --- a/reference/xtquant_big_convert/tests/bigqmt_signal_trader/test_xtquant_compat.py +++ /dev/null @@ -1,648 +0,0 @@ -import os -import sys -import types -import unittest - - -ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -sys.path.insert(0, os.path.join(ROOT, "src")) - -from bigqmt_signal_trader.xtquant_compat import ( - BigQmtRpcClient, - FIX_PRICE, - MARKET_PEER_PRICE_FIRST, - SH_MARKET, - STOCK_BUY, - STOCK_SELL, - SZ_MARKET, - BigQmtXtData, - BigQmtXtTrader, - StockAccount, - configure, - load_client_config, - xt_trader, -) -from bigqmt_signal_trader.full_tick_cache import full_tick_demand_key, full_tick_request_id, write_full_tick_cache - - -class FakeRpcClient: - def __init__(self): - self.account_id = "acct" - self.calls = [] - self.redis = FakeRedisEvents() - self.full_tick_cache_config = { - "enabled": True, - "demand_ttl_seconds": 10, - "cache_ttl_seconds": 10, - "wait_seconds": 0.1, - "poll_interval_seconds": 0.01, - } - - def _redis(self): - return self.redis - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - self.calls.append((method, params or {}, account_id, timeout_seconds)) - if method == "query_stock_asset": - return {"account_id": "acct", "cash": 100.5, "total_asset": 1000.5} - if method == "query_stock_positions": - return { - "600000.SH": { - "stock_code": "600000.SH", - "volume": 1000, - "available": 800, - "cost": 10.2, - "price": 10.8, - "market_value": 10800.0, - "frozen_volume": 200, - "on_road_volume": 5, - "yesterday_volume": 900, - "direction": 48, - "stock_name": "PF Bank", - } - } - if method == "query_stock_position": - return { - "stock_code": "600000.SH", - "volume": 1000, - "available": 800, - "cost": 10.2, - } - if method == "query_stock_orders": - return [ - { - "order_sys_id": "sys-1", - "user_order_id": "remark-1", - "stock_code": "600000.SH", - "action": "SELL", - "volume": 300, - "traded_volume": 100, - "status": "50", - "price": 10.1, - } - ] - if method == "query_stock_trades": - return [ - { - "trade_id": "trade-1", - "order_sys_id": "sys-1", - "user_order_id": "remark-1", - "stock_code": "600000.SH", - "action": "BUY", - "volume": 100, - "price": 10.0, - } - ] - if method == "query_execution_snapshot": - return { - "account_id": "acct", - "server_time": "2026-07-15 10:00:00", - "orders": [ - { - "order_sys_id": "sys-1", "user_order_id": "remark-1", - "stock_code": "600000.SH", "action": "SELL", "volume": 300, - "traded_volume": 100, "status": "50", "price": 10.1, - } - ], - "trades": [ - { - "trade_id": "trade-1", "order_sys_id": "sys-1", - "user_order_id": "remark-1", "stock_code": "600000.SH", - "action": "BUY", "volume": 100, "price": 10.0, - } - ], - } - if method == "order_stock": - return {"status": "SUBMITTED", "user_order_id": "bq:1", "order_sys_id": "sys-2"} - if method == "order_stock_batch": - return [{"success": True, "accepted": True, "user_order_id": "batch-tag"}] - if method == "cancel_order_stock_sysid": - return {"success": True} - if method == "get_full_tick": - codes = params.get("codes") or [] - if codes == ["SH", "SZ"]: - return { - "000001.SH": {"lastPrice": 3000}, - "000001.SZ": {"lastPrice": 10}, - "600000.SH": {"lastPrice": 10}, - "510300.SH": {"lastPrice": 4}, - "300001.SZ": {"lastPrice": 20}, - "113001.SH": {"lastPrice": 100}, - } - return {codes[0]: {"lastPrice": 10, "bidPrice": [9.9], "askPrice": [10.1]}} - if method == "get_instrument_detail": - return {"InstrumentStatus": 0, "code": params.get("code")} - if method == "get_market_data_ex": - if params.get("stock_list") == ["159518.SZ"]: - try: - import pandas as pd - - return { - "159518.SZ": pd.DataFrame( - { - "stime": ["20250813 10:27:00", "20250813 10:28:00"], - "time": [None, None], - "open": [0.872, 0.873], - "high": [0.873, 0.873], - "low": [0.872, 0.872], - "close": [0.872, 0.872], - "volume": [2791.0, 1659.0], - } - ) - } - except Exception: - return {"159518.SZ": []} - return {"600000.SH": {"close": [10.0]}} - if method == "ping": - return {"pong": True} - raise AssertionError("unexpected method: %s" % method) - - def publish_event(self, event_type, payload, stream_template="bigqmt:quote_events:{account_id}"): - return self.redis.publish_event(event_type, payload) - - def save_quote_subscription(self, seq, payload, active=True): - if active: - self.redis.hset("bigqmt:quote_subscriptions:%s" % self.account_id, str(seq), payload) - else: - self.redis.hdel("bigqmt:quote_subscriptions:%s" % self.account_id, str(seq)) - - -class FakeRedisEvents: - def __init__(self): - self.kv = {} - self.hashes = {} - self.deleted = [] - self.events = [] - self.expired = [] - - def hset(self, key, field, value): - self.hashes.setdefault(key, {})[field] = value - return 1 - - def hdel(self, key, field): - self.deleted.append((key, field)) - self.hashes.setdefault(key, {}).pop(field, None) - return 1 - - def hgetall(self, key): - return self.hashes.get(key, {}) - - def expire(self, key, seconds): - self.expired.append((key, seconds)) - return True - - def setex(self, key, seconds, value): - self.kv[key] = value - self.expired.append((key, seconds)) - return True - - def publish_event(self, event_type, payload): - self.events.append((event_type, payload)) - return {"event_type": event_type, "payload": payload} - - def get(self, key): - if key in self.kv: - return self.kv[key] - value = self.hashes.get(key) - if value is None: - return None - import json - - return json.dumps(value).encode("utf-8") - - -class XtquantCompatTest(unittest.TestCase): - def _with_fake_config(self, module_name="test_bigqmt_client_cfg"): - module = types.ModuleType(module_name) - module.BIGQMT_ACCOUNT_ID = "cfg-account" - module.BIGQMT_RPC_TIMEOUT_SECONDS = 9 - module.BIGQMT_REDIS_CONFIG = { - "host": "cfg-host", - "port": 6380, - "db": 6, - "username": "cfg-user", - "password": "cfg-pass", - } - old_env = os.environ.get("BIGQMT_CLIENT_CONFIG_MODULE") - os.environ["BIGQMT_CLIENT_CONFIG_MODULE"] = module_name - sys.modules[module_name] = module - return module_name, old_env - - def _cleanup_fake_config(self, module_name, old_env): - sys.modules.pop(module_name, None) - if old_env is None: - os.environ.pop("BIGQMT_CLIENT_CONFIG_MODULE", None) - else: - os.environ["BIGQMT_CLIENT_CONFIG_MODULE"] = old_env - - def _trader(self): - trader = BigQmtXtTrader(account_id="acct") - trader.client = FakeRpcClient() - return trader - - def _xtdata(self): - return BigQmtXtData(FakeRpcClient()) - - def test_trader_read_methods_return_miniqmt_style_objects(self): - trader = self._trader() - acc = StockAccount("acct") - - asset = trader.query_stock_asset(acc) - positions = trader.query_stock_positions(acc) - single = trader.query_stock_position(acc, "600000") - - self.assertEqual(asset.cash, 100.5) - self.assertEqual(asset.market_value, 900.0) - self.assertEqual(positions[0].stock_code, "600000.SH") - self.assertEqual(positions[0].can_use_volume, 800) - self.assertEqual(positions[0].avg_price, 10.2) - self.assertEqual(positions[0].price, 10.8) - self.assertEqual(positions[0].market_value, 10800.0) - self.assertEqual(positions[0].frozen_volume, 200) - self.assertEqual(positions[0].on_road_volume, 5) - self.assertEqual(positions[0].yesterday_volume, 900) - self.assertEqual(positions[0].direction, 48) - self.assertEqual(single.stock_code, "600000.SH") - - def test_orders_trades_order_and_cancel_are_miniqmt_shaped(self): - trader = self._trader() - acc = StockAccount("acct") - - orders = trader.query_stock_orders(acc, cancelable_only=False) - trades = trader.query_stock_trades(acc) - order_id = trader.order_stock( - acc, - "600000.SH", - STOCK_BUY, - 100, - MARKET_PEER_PRICE_FIRST, - 0, - "strategy", - "remark", - ) - cancelled = trader.cancel_order_stock_sysid(acc, SH_MARKET, "sys-2") - - self.assertEqual(orders[0].order_type, STOCK_SELL) - self.assertEqual(orders[0].order_status, 50) - self.assertEqual(orders[0].order_volume, 300) - self.assertEqual(trades[0].order_type, STOCK_BUY) - self.assertEqual(trades[0].traded_price, 10.0) - self.assertEqual(trades[0].order_remark, "remark-1") - self.assertEqual(order_id, "sys-2") - self.assertTrue(cancelled) - self.assertEqual(trader.client.calls[-2][1]["price_type"], MARKET_PEER_PRICE_FIRST) - # strategy_name 默认 ""(返回全部委托),与服务端一致(strategy_name 陷阱)。 - self.assertEqual(trader.client.calls[-4][1]["strategy_name"], "") - - def test_execution_snapshot_maps_orders_and_trades_with_one_rpc(self): - trader = self._trader() - acc = StockAccount("acct") - - snapshot = trader.query_execution_snapshot( - acc, order_strategy_name="icestone_grid_600276", trade_strategy_name="" - ) - - self.assertEqual(snapshot["orders"][0].order_sysid, "sys-1") - self.assertEqual(snapshot["trades"][0].trade_id, "trade-1") - self.assertEqual(snapshot["server_time"], "2026-07-15 10:00:00") - self.assertEqual(trader.client.calls[-1][0], "query_execution_snapshot") - self.assertEqual(trader.client.calls[-1][1]["trade_strategy_name"], "") - - def test_order_stock_never_returns_user_tag_as_real_order_id(self): - trader = self._trader() - acc = StockAccount("acct") - trader.client.call = lambda *_args, **_kwargs: { - "status": "SUBMITTED", "user_order_id": "bq:request-only", - "order_sys_id": None, - } - - order_id = trader.order_stock( - acc, "600000.SH", STOCK_BUY, 100, FIX_PRICE, 10.0, - "strategy", "remark", - ) - result = trader.order_stock_result( - acc, "600000.SH", STOCK_BUY, 100, FIX_PRICE, 10.0, - "strategy", "remark", - ) - - self.assertEqual(order_id, -1) - self.assertEqual(result["user_order_id"], "bq:request-only") - self.assertIsNone(result["order_sys_id"]) - - def test_order_stock_batch_forwards_batch_identity(self): - trader = self._trader() - acc = StockAccount("acct") - - result = trader.order_stock_batch( - acc, - [{"stock_code": "600000.SH", "order_type": STOCK_BUY, - "order_volume": 100, "price": 10.0, - "order_remark": "batch-tag"}], - batch_id="BATCH-IDENTITY-1", - ) - - method, params, account_id, _timeout = trader.client.calls[-1] - self.assertEqual(method, "order_stock_batch") - self.assertEqual(account_id, "acct") - self.assertEqual(params["batch_id"], "BATCH-IDENTITY-1") - self.assertEqual(params["orders"][0]["order_remark"], "batch-tag") - self.assertTrue(result[0]["accepted"]) - - def test_xtdata_read_methods_and_sector_filter(self): - xtdata = self._xtdata() - write_full_tick_cache( - xtdata.client.redis, - xtdata.client.account_id, - ["600000.SH"], - {"600000.SH": {"lastPrice": 10, "bidPrice": [9.9], "askPrice": [10.1]}}, - ) - write_full_tick_cache( - xtdata.client.redis, - xtdata.client.account_id, - ["SH", "SZ"], - { - "000001.SH": {"lastPrice": 3000}, - "000001.SZ": {"lastPrice": 10}, - "600000.SH": {"lastPrice": 10}, - "510300.SH": {"lastPrice": 4}, - "300001.SZ": {"lastPrice": 20}, - "113001.SH": {"lastPrice": 100}, - }, - ) - - ticks = xtdata.get_full_tick(["600000.SH"]) - detail = xtdata.get_instrument_detail("600000.SH") - sector_codes = xtdata.get_stock_list_in_sector("沪深A股") - market_data = xtdata.get_market_data_ex(["close"], ["600000.SH"], count=1) - - self.assertEqual(ticks["600000.SH"]["bidPrice"], [9.9]) - self.assertEqual(detail["InstrumentStatus"], 0) - self.assertEqual(sector_codes, ["000001.SZ", "300001.SZ", "600000.SH"]) - self.assertEqual(market_data["600000.SH"]["close"], [10.0]) - - def test_market_data_ex_normalizes_bigqmt_stime_to_miniqmt_shape(self): - try: - import pandas # noqa: F401 - except Exception: - self.skipTest("pandas not installed") - - xtdata = self._xtdata() - - data = xtdata.get_market_data_ex( - ["time", "open", "high", "low", "close", "volume"], - ["159518.SZ"], - period="1m", - start_time="20250601000000", - end_time="", - count=-1, - ) - df = data["159518.SZ"] - - self.assertEqual(list(df.index), ["20250813102700", "20250813102800"]) - self.assertEqual( - list(df.columns), - ["time", "open", "high", "low", "close", "volume"], - ) - self.assertEqual(int(df.iloc[0]["time"]), 1755052020000) - self.assertNotIn("stime", df.columns) - - def test_xtdata_full_tick_reads_redis_cache_and_renews_demand(self): - xtdata = self._xtdata() - write_full_tick_cache( - xtdata.client.redis, - xtdata.client.account_id, - ["SZ", "SH"], - {"600000.SH": {"lastPrice": 10, "bidPrice": [9.9], "askPrice": [10.1]}}, - ) - - ticks = xtdata.get_full_tick(["SH", "SZ"]) - - self.assertIn("600000.SH", ticks) - self.assertFalse([call for call in xtdata.client.calls if call[0] == "get_full_tick"]) - demand_key = full_tick_demand_key(xtdata.client.account_id) - self.assertIn(full_tick_request_id(["SH", "SZ"]), xtdata.client.redis.hashes[demand_key]) - - def test_xtdata_full_tick_symbol_miss_falls_back_to_rpc(self): - xtdata = self._xtdata() - xtdata.client.full_tick_cache_config["wait_seconds"] = 0 - - ticks = xtdata.get_full_tick(["600000.SH"]) - - # A cold cache miss on a symbol list now falls back to a live RPC instead - # of a hard wait_seconds stall, so the first call returns in ~ms. - self.assertEqual(ticks["600000.SH"]["bidPrice"], [9.9]) - self.assertEqual([call[0] for call in xtdata.client.calls if call[0] == "get_full_tick"], ["get_full_tick"]) - demand_key = full_tick_demand_key(xtdata.client.account_id) - self.assertIn(full_tick_request_id(["600000.SH"]), xtdata.client.redis.hashes[demand_key]) - - def test_xtdata_full_market_tick_miss_raises_without_rpc(self): - xtdata = self._xtdata() - xtdata.client.full_tick_cache_config["wait_seconds"] = 0 - - # Whole-market snapshots must stay on the demand cache; a miss must never - # live-pull ~50k rows over RPC. - with self.assertRaises(TimeoutError): - xtdata.get_full_tick(["SH", "SZ"]) - - self.assertFalse([call for call in xtdata.client.calls if call[0] == "get_full_tick"]) - demand_key = full_tick_demand_key(xtdata.client.account_id) - self.assertIn(full_tick_request_id(["SH", "SZ"]), xtdata.client.redis.hashes[demand_key]) - - def test_xtdata_full_market_tick_can_fall_back_to_rpc_when_cache_disabled(self): - xtdata = self._xtdata() - xtdata.client.full_tick_cache_config["enabled"] = False - - xtdata.get_full_tick(["SH", "SZ"]) - - self.assertEqual(xtdata.client.calls[-1][0], "get_full_tick") - self.assertEqual(xtdata.client.calls[-1][3], 30) - - def test_quote_subscribe_and_unsubscribe_write_redis_events(self): - xtdata = self._xtdata() - - seq = xtdata.subscribe_quote("600000.SH", period="tick") - result = xtdata.unsubscribe_quote(seq) - - key = "bigqmt:quote_subscriptions:acct" - self.assertEqual(result, 0) - self.assertNotIn(str(seq), xtdata.client.redis.hashes.get(key, {})) - self.assertIn((key, str(seq)), xtdata.client.redis.deleted) - self.assertEqual(xtdata.client.redis.events[0][0], "subscribe_quote") - self.assertEqual(xtdata.client.redis.events[1][0], "unsubscribe_quote") - - def test_optional_xtquant_shim_imports_constants_and_classes(self): - from xtquant import xtconstant - from xtquant.xttrader import XtQuantTrader - from xtquant.xttype import StockAccount as ShimStockAccount - - self.assertEqual(xtconstant.STOCK_BUY, STOCK_BUY) - self.assertEqual(xtconstant.FIX_PRICE, FIX_PRICE) - self.assertEqual(xtconstant.SZ_MARKET, SZ_MARKET) - self.assertIs(XtQuantTrader, BigQmtXtTrader) - self.assertEqual(ShimStockAccount("acct").account_id, "acct") - - def test_configure_updates_imported_xt_trader_object_in_place(self): - original = xt_trader - configure(account_id="acct-new", redis_client=FakeRpcClient()) - - self.assertIs(xt_trader, original) - self.assertEqual(xt_trader.client.account_id, "acct-new") - - def test_client_reads_account_and_redis_from_private_config(self): - module_name, old_env = self._with_fake_config() - try: - config = load_client_config() - client = BigQmtRpcClient() - finally: - self._cleanup_fake_config(module_name, old_env) - - self.assertEqual(config["account_id"], "cfg-account") - self.assertEqual(client.account_id, "cfg-account") - self.assertEqual(client.redis_config["host"], "cfg-host") - self.assertEqual(client.redis_config["port"], 6380) - self.assertEqual(client.redis_config["db"], 6) - self.assertEqual(client.redis_config["username"], "cfg-user") - self.assertEqual(client.redis_config["password"], "cfg-pass") - self.assertEqual(client.timeout_seconds, 9) - - def test_explicit_client_params_override_private_config(self): - module_name, old_env = self._with_fake_config() - try: - client = BigQmtRpcClient( - account_id="explicit-account", - redis_config={"host": "explicit-host", "password": ""}, - timeout_seconds=3, - ) - finally: - self._cleanup_fake_config(module_name, old_env) - - self.assertEqual(client.account_id, "explicit-account") - self.assertEqual(client.redis_config["host"], "explicit-host") - self.assertEqual(client.redis_config["port"], 6380) - self.assertEqual(client.redis_config["password"], "") - self.assertEqual(client.timeout_seconds, 3) - - def test_trader_falls_back_to_cached_positions_when_rpc_fails(self): - class FailingRpcClient(FakeRpcClient): - def call(self, method, params=None, account_id=None, timeout_seconds=None): - if method in ("query_stock_asset", "query_stock_positions", "query_stock_position"): - raise RuntimeError("rpc down") - return super().call(method, params, account_id, timeout_seconds) - - client = FailingRpcClient() - client.redis.hashes["bigqmt:positions:acct"] = { - "account_id": "acct", - "asset": {"cash": 123.0, "total_asset": 456.0}, - "positions": { - "600000.SH": { - "stock_code": "600000.SH", - "volume": 100, - "available": 80, - "cost": 10.5, - "stock_name": "cached", - } - }, - } - trader = BigQmtXtTrader(account_id="acct") - trader.client = client - acc = StockAccount("acct") - - asset = trader.query_stock_asset(acc) - positions = trader.query_stock_positions(acc) - single = trader.query_stock_position(acc, "600000") - - self.assertEqual(asset.cash, 123.0) - self.assertEqual(asset.total_asset, 456.0) - self.assertEqual(positions[0].stock_code, "600000.SH") - self.assertEqual(positions[0].can_use_volume, 80) - self.assertEqual(single.stock_name, "cached") - - def test_zmq_query_failure_never_falls_back_to_redis_cache(self): - class FailingZmqClient(FakeRpcClient): - transport_name = "zmq" - - def call(self, method, params=None, account_id=None, timeout_seconds=None): - raise RuntimeError("zmq timeout") - - def _redis(self): - raise AssertionError("ZMQ query must not access Redis") - - trader = BigQmtXtTrader(account_id="acct") - trader.client = FailingZmqClient() - acc = StockAccount("acct") - - with self.assertRaisesRegex(RuntimeError, "zmq timeout"): - trader.query_stock_asset(acc) - with self.assertRaisesRegex(RuntimeError, "zmq timeout"): - trader.query_stock_positions(acc) - with self.assertRaisesRegex(RuntimeError, "zmq timeout"): - trader.query_stock_position(acc, "600000.SH") - - def test_client_call_via_transport_builds_valid_request(self): - # Regression: the swappable-transport path in BigQmtRpcClient.call() built - # request_id with __import__("uuid").uuid.uuid4() (AttributeError), crashing - # every non-redis transport on first call. This path had no coverage. - captured = {} - - class _FakeTransport: - def send_request(self, request, timeout_seconds): - captured["request"] = request - captured["timeout"] = timeout_seconds - return {"ok": True, "data": {"pong": True}} - - client = BigQmtRpcClient(account_id="acct", redis_config={"host": "127.0.0.1"}) - client.transport_name = "zmq" - client._transport_instance = _FakeTransport() - - result = client.call("ping", {"x": 1}) - - self.assertEqual(result, {"pong": True}) - request = captured["request"] - self.assertEqual(request["method"], "ping") - self.assertEqual(request["account_id"], "acct") - self.assertEqual(request["params"], {"x": 1}) - # request_id must be a real 32-char uuid hex, not a crash. - self.assertEqual(len(request["request_id"]), 32) - int(request["request_id"], 16) - - def test_client_call_raises_on_server_error(self): - # issue #38: server_error(QMT 端诊断,如 passorder 提交但委托没进系统) - # 之前被 call() 静默丢弃,导致下单流程看不到真实原因。现在必须转成异常。 - class _FakeTransport: - def send_request(self, request, timeout_seconds): - return { - "ok": True, - "data": {"status": "SUBMITTED", "order_sys_id": ""}, - "server_error": "passorder submitted but order not found in system", - } - - client = BigQmtRpcClient(account_id="acct", redis_config={"host": "127.0.0.1"}) - client.transport_name = "zmq" - client._transport_instance = _FakeTransport() - - with self.assertRaises(RuntimeError) as ctx: - client.call("order_stock", {"stock_code": "600000.SH"}) - self.assertIn("not found in system", str(ctx.exception)) - - def test_zmq_with_explicit_address_never_builds_redis_discovery(self): - client = BigQmtRpcClient( - account_id="acct", - redis_config={ - "transport": "zmq", - "zmq": {"connect_address": "tcp://127.0.0.1:20146"}, - }, - ) - - def forbidden_redis(): - raise AssertionError("ZMQ explicit-address mode must not touch Redis") - - client._redis = forbidden_redis - transport = client._transport() - - self.assertEqual(transport.name, "zmq") - self.assertEqual(transport.connect_address, "tcp://127.0.0.1:20146") - self.assertIsNone(transport.discovery_redis_client) - - -if __name__ == "__main__": - unittest.main() diff --git a/reference/xtquant_big_convert/uv.lock b/reference/xtquant_big_convert/uv.lock deleted file mode 100644 index 8a79b39..0000000 --- a/reference/xtquant_big_convert/uv.lock +++ /dev/null @@ -1,503 +0,0 @@ -version = 1 -revision = 3 -requires-python = ">=3.8" -resolution-markers = [ - "python_full_version >= '3.10'", - "python_full_version == '3.9.*'", - "python_full_version < '3.9'", -] - -[[package]] -name = "async-timeout" -version = "5.0.1" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/a5/ae/136395dfbfe00dfc94da3f3e136d0b13f394cba8f4841120e34226265780/async_timeout-5.0.1.tar.gz", hash = "sha256:d9321a7a3d5a6a5e187e824d2fa0793ce379a202935782d555d6e9d2735677d3", size = 9274, upload-time = "2024-11-06T16:41:39.6Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/fe/ba/e2081de779ca30d473f21f5b30e0e737c438205440784c7dfc81efc2b029/async_timeout-5.0.1-py3-none-any.whl", hash = "sha256:39e3809566ff85354557ec2398b55e096c8364bacac9405a7a1fa429e77fe76c", size = 6233, upload-time = "2024-11-06T16:41:37.9Z" }, -] - -[[package]] -name = "cffi" -version = "1.17.1" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version < '3.9'", -] -dependencies = [ - { name = "pycparser", version = "2.23", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/fc/97/c783634659c2920c3fc70419e3af40972dbaf758daa229a7d6ea6135c90d/cffi-1.17.1.tar.gz", hash = "sha256:1c39c6016c32bc48dd54561950ebd6836e1670f2ae46128f67cf49e789c52824", size = 516621, upload-time = "2024-09-04T20:45:21.852Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/90/07/f44ca684db4e4f08a3fdc6eeb9a0d15dc6883efc7b8c90357fdbf74e186c/cffi-1.17.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:df8b1c11f177bc2313ec4b2d46baec87a5f3e71fc8b45dab2ee7cae86d9aba14", size = 182191, upload-time = "2024-09-04T20:43:30.027Z" }, - { url = "https://files.pythonhosted.org/packages/08/fd/cc2fedbd887223f9f5d170c96e57cbf655df9831a6546c1727ae13fa977a/cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8f2cdc858323644ab277e9bb925ad72ae0e67f69e804f4898c070998d50b1a67", size = 178592, upload-time = "2024-09-04T20:43:32.108Z" }, - { url = "https://files.pythonhosted.org/packages/de/cc/4635c320081c78d6ffc2cab0a76025b691a91204f4aa317d568ff9280a2d/cffi-1.17.1-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:edae79245293e15384b51f88b00613ba9f7198016a5948b5dddf4917d4d26382", size = 426024, upload-time = "2024-09-04T20:43:34.186Z" }, - { url = "https://files.pythonhosted.org/packages/b6/7b/3b2b250f3aab91abe5f8a51ada1b717935fdaec53f790ad4100fe2ec64d1/cffi-1.17.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:45398b671ac6d70e67da8e4224a065cec6a93541bb7aebe1b198a61b58c7b702", size = 448188, upload-time = "2024-09-04T20:43:36.286Z" }, - { url = "https://files.pythonhosted.org/packages/d3/48/1b9283ebbf0ec065148d8de05d647a986c5f22586b18120020452fff8f5d/cffi-1.17.1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ad9413ccdeda48c5afdae7e4fa2192157e991ff761e7ab8fdd8926f40b160cc3", size = 455571, upload-time = "2024-09-04T20:43:38.586Z" }, - { url = "https://files.pythonhosted.org/packages/40/87/3b8452525437b40f39ca7ff70276679772ee7e8b394934ff60e63b7b090c/cffi-1.17.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:5da5719280082ac6bd9aa7becb3938dc9f9cbd57fac7d2871717b1feb0902ab6", size = 436687, upload-time = "2024-09-04T20:43:40.084Z" }, - { url = "https://files.pythonhosted.org/packages/8d/fb/4da72871d177d63649ac449aec2e8a29efe0274035880c7af59101ca2232/cffi-1.17.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2bb1a08b8008b281856e5971307cc386a8e9c5b625ac297e853d36da6efe9c17", size = 446211, upload-time = "2024-09-04T20:43:41.526Z" }, - { url = "https://files.pythonhosted.org/packages/ab/a0/62f00bcb411332106c02b663b26f3545a9ef136f80d5df746c05878f8c4b/cffi-1.17.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:045d61c734659cc045141be4bae381a41d89b741f795af1dd018bfb532fd0df8", size = 461325, upload-time = "2024-09-04T20:43:43.117Z" }, - { url = "https://files.pythonhosted.org/packages/36/83/76127035ed2e7e27b0787604d99da630ac3123bfb02d8e80c633f218a11d/cffi-1.17.1-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:6883e737d7d9e4899a8a695e00ec36bd4e5e4f18fabe0aca0efe0a4b44cdb13e", size = 438784, upload-time = "2024-09-04T20:43:45.256Z" }, - { url = "https://files.pythonhosted.org/packages/21/81/a6cd025db2f08ac88b901b745c163d884641909641f9b826e8cb87645942/cffi-1.17.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:6b8b4a92e1c65048ff98cfe1f735ef8f1ceb72e3d5f0c25fdb12087a23da22be", size = 461564, upload-time = "2024-09-04T20:43:46.779Z" }, - { url = "https://files.pythonhosted.org/packages/f8/fe/4d41c2f200c4a457933dbd98d3cf4e911870877bd94d9656cc0fcb390681/cffi-1.17.1-cp310-cp310-win32.whl", hash = "sha256:c9c3d058ebabb74db66e431095118094d06abf53284d9c81f27300d0e0d8bc7c", size = 171804, upload-time = "2024-09-04T20:43:48.186Z" }, - { url = "https://files.pythonhosted.org/packages/d1/b6/0b0f5ab93b0df4acc49cae758c81fe4e5ef26c3ae2e10cc69249dfd8b3ab/cffi-1.17.1-cp310-cp310-win_amd64.whl", hash = "sha256:0f048dcf80db46f0098ccac01132761580d28e28bc0f78ae0d58048063317e15", size = 181299, upload-time = "2024-09-04T20:43:49.812Z" }, - { url = "https://files.pythonhosted.org/packages/6b/f4/927e3a8899e52a27fa57a48607ff7dc91a9ebe97399b357b85a0c7892e00/cffi-1.17.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:a45e3c6913c5b87b3ff120dcdc03f6131fa0065027d0ed7ee6190736a74cd401", size = 182264, upload-time = "2024-09-04T20:43:51.124Z" }, - { url = "https://files.pythonhosted.org/packages/6c/f5/6c3a8efe5f503175aaddcbea6ad0d2c96dad6f5abb205750d1b3df44ef29/cffi-1.17.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:30c5e0cb5ae493c04c8b42916e52ca38079f1b235c2f8ae5f4527b963c401caf", size = 178651, upload-time = "2024-09-04T20:43:52.872Z" }, - { url = "https://files.pythonhosted.org/packages/94/dd/a3f0118e688d1b1a57553da23b16bdade96d2f9bcda4d32e7d2838047ff7/cffi-1.17.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f75c7ab1f9e4aca5414ed4d8e5c0e303a34f4421f8a0d47a4d019ceff0ab6af4", size = 445259, upload-time = "2024-09-04T20:43:56.123Z" }, - { url = "https://files.pythonhosted.org/packages/2e/ea/70ce63780f096e16ce8588efe039d3c4f91deb1dc01e9c73a287939c79a6/cffi-1.17.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a1ed2dd2972641495a3ec98445e09766f077aee98a1c896dcb4ad0d303628e41", size = 469200, upload-time = "2024-09-04T20:43:57.891Z" }, - { url = "https://files.pythonhosted.org/packages/1c/a0/a4fa9f4f781bda074c3ddd57a572b060fa0df7655d2a4247bbe277200146/cffi-1.17.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:46bf43160c1a35f7ec506d254e5c890f3c03648a4dbac12d624e4490a7046cd1", size = 477235, upload-time = "2024-09-04T20:44:00.18Z" }, - { url = "https://files.pythonhosted.org/packages/62/12/ce8710b5b8affbcdd5c6e367217c242524ad17a02fe5beec3ee339f69f85/cffi-1.17.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a24ed04c8ffd54b0729c07cee15a81d964e6fee0e3d4d342a27b020d22959dc6", size = 459721, upload-time = "2024-09-04T20:44:01.585Z" }, - { url = "https://files.pythonhosted.org/packages/ff/6b/d45873c5e0242196f042d555526f92aa9e0c32355a1be1ff8c27f077fd37/cffi-1.17.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:610faea79c43e44c71e1ec53a554553fa22321b65fae24889706c0a84d4ad86d", size = 467242, upload-time = "2024-09-04T20:44:03.467Z" }, - { url = "https://files.pythonhosted.org/packages/1a/52/d9a0e523a572fbccf2955f5abe883cfa8bcc570d7faeee06336fbd50c9fc/cffi-1.17.1-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:a9b15d491f3ad5d692e11f6b71f7857e7835eb677955c00cc0aefcd0669adaf6", size = 477999, upload-time = "2024-09-04T20:44:05.023Z" }, - { url = "https://files.pythonhosted.org/packages/44/74/f2a2460684a1a2d00ca799ad880d54652841a780c4c97b87754f660c7603/cffi-1.17.1-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:de2ea4b5833625383e464549fec1bc395c1bdeeb5f25c4a3a82b5a8c756ec22f", size = 454242, upload-time = "2024-09-04T20:44:06.444Z" }, - { url = "https://files.pythonhosted.org/packages/f8/4a/34599cac7dfcd888ff54e801afe06a19c17787dfd94495ab0c8d35fe99fb/cffi-1.17.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:fc48c783f9c87e60831201f2cce7f3b2e4846bf4d8728eabe54d60700b318a0b", size = 478604, upload-time = "2024-09-04T20:44:08.206Z" }, - { url = "https://files.pythonhosted.org/packages/34/33/e1b8a1ba29025adbdcda5fb3a36f94c03d771c1b7b12f726ff7fef2ebe36/cffi-1.17.1-cp311-cp311-win32.whl", hash = "sha256:85a950a4ac9c359340d5963966e3e0a94a676bd6245a4b55bc43949eee26a655", size = 171727, upload-time = "2024-09-04T20:44:09.481Z" }, - { url = "https://files.pythonhosted.org/packages/3d/97/50228be003bb2802627d28ec0627837ac0bf35c90cf769812056f235b2d1/cffi-1.17.1-cp311-cp311-win_amd64.whl", hash = "sha256:caaf0640ef5f5517f49bc275eca1406b0ffa6aa184892812030f04c2abf589a0", size = 181400, upload-time = "2024-09-04T20:44:10.873Z" }, - { url = "https://files.pythonhosted.org/packages/5a/84/e94227139ee5fb4d600a7a4927f322e1d4aea6fdc50bd3fca8493caba23f/cffi-1.17.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:805b4371bf7197c329fcb3ead37e710d1bca9da5d583f5073b799d5c5bd1eee4", size = 183178, upload-time = "2024-09-04T20:44:12.232Z" }, - { url = "https://files.pythonhosted.org/packages/da/ee/fb72c2b48656111c4ef27f0f91da355e130a923473bf5ee75c5643d00cca/cffi-1.17.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:733e99bc2df47476e3848417c5a4540522f234dfd4ef3ab7fafdf555b082ec0c", size = 178840, upload-time = "2024-09-04T20:44:13.739Z" }, - { url = "https://files.pythonhosted.org/packages/cc/b6/db007700f67d151abadf508cbfd6a1884f57eab90b1bb985c4c8c02b0f28/cffi-1.17.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1257bdabf294dceb59f5e70c64a3e2f462c30c7ad68092d01bbbfb1c16b1ba36", size = 454803, upload-time = "2024-09-04T20:44:15.231Z" }, - { url = "https://files.pythonhosted.org/packages/1a/df/f8d151540d8c200eb1c6fba8cd0dfd40904f1b0682ea705c36e6c2e97ab3/cffi-1.17.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:da95af8214998d77a98cc14e3a3bd00aa191526343078b530ceb0bd710fb48a5", size = 478850, upload-time = "2024-09-04T20:44:17.188Z" }, - { url = "https://files.pythonhosted.org/packages/28/c0/b31116332a547fd2677ae5b78a2ef662dfc8023d67f41b2a83f7c2aa78b1/cffi-1.17.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d63afe322132c194cf832bfec0dc69a99fb9bb6bbd550f161a49e9e855cc78ff", size = 485729, upload-time = "2024-09-04T20:44:18.688Z" }, - { url = "https://files.pythonhosted.org/packages/91/2b/9a1ddfa5c7f13cab007a2c9cc295b70fbbda7cb10a286aa6810338e60ea1/cffi-1.17.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:f79fc4fc25f1c8698ff97788206bb3c2598949bfe0fef03d299eb1b5356ada99", size = 471256, upload-time = "2024-09-04T20:44:20.248Z" }, - { url = "https://files.pythonhosted.org/packages/b2/d5/da47df7004cb17e4955df6a43d14b3b4ae77737dff8bf7f8f333196717bf/cffi-1.17.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b62ce867176a75d03a665bad002af8e6d54644fad99a3c70905c543130e39d93", size = 479424, upload-time = "2024-09-04T20:44:21.673Z" }, - { url = "https://files.pythonhosted.org/packages/0b/ac/2a28bcf513e93a219c8a4e8e125534f4f6db03e3179ba1c45e949b76212c/cffi-1.17.1-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:386c8bf53c502fff58903061338ce4f4950cbdcb23e2902d86c0f722b786bbe3", size = 484568, upload-time = "2024-09-04T20:44:23.245Z" }, - { url = "https://files.pythonhosted.org/packages/d4/38/ca8a4f639065f14ae0f1d9751e70447a261f1a30fa7547a828ae08142465/cffi-1.17.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:4ceb10419a9adf4460ea14cfd6bc43d08701f0835e979bf821052f1805850fe8", size = 488736, upload-time = "2024-09-04T20:44:24.757Z" }, - { url = "https://files.pythonhosted.org/packages/86/c5/28b2d6f799ec0bdecf44dced2ec5ed43e0eb63097b0f58c293583b406582/cffi-1.17.1-cp312-cp312-win32.whl", hash = "sha256:a08d7e755f8ed21095a310a693525137cfe756ce62d066e53f502a83dc550f65", size = 172448, upload-time = "2024-09-04T20:44:26.208Z" }, - { url = "https://files.pythonhosted.org/packages/50/b9/db34c4755a7bd1cb2d1603ac3863f22bcecbd1ba29e5ee841a4bc510b294/cffi-1.17.1-cp312-cp312-win_amd64.whl", hash = "sha256:51392eae71afec0d0c8fb1a53b204dbb3bcabcb3c9b807eedf3e1e6ccf2de903", size = 181976, upload-time = "2024-09-04T20:44:27.578Z" }, - { url = "https://files.pythonhosted.org/packages/8d/f8/dd6c246b148639254dad4d6803eb6a54e8c85c6e11ec9df2cffa87571dbe/cffi-1.17.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f3a2b4222ce6b60e2e8b337bb9596923045681d71e5a082783484d845390938e", size = 182989, upload-time = "2024-09-04T20:44:28.956Z" }, - { url = "https://files.pythonhosted.org/packages/8b/f1/672d303ddf17c24fc83afd712316fda78dc6fce1cd53011b839483e1ecc8/cffi-1.17.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:0984a4925a435b1da406122d4d7968dd861c1385afe3b45ba82b750f229811e2", size = 178802, upload-time = "2024-09-04T20:44:30.289Z" }, - { url = "https://files.pythonhosted.org/packages/0e/2d/eab2e858a91fdff70533cab61dcff4a1f55ec60425832ddfdc9cd36bc8af/cffi-1.17.1-cp313-cp313-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d01b12eeeb4427d3110de311e1774046ad344f5b1a7403101878976ecd7a10f3", size = 454792, upload-time = "2024-09-04T20:44:32.01Z" }, - { url = "https://files.pythonhosted.org/packages/75/b2/fbaec7c4455c604e29388d55599b99ebcc250a60050610fadde58932b7ee/cffi-1.17.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:706510fe141c86a69c8ddc029c7910003a17353970cff3b904ff0686a5927683", size = 478893, upload-time = "2024-09-04T20:44:33.606Z" }, - { url = "https://files.pythonhosted.org/packages/4f/b7/6e4a2162178bf1935c336d4da8a9352cccab4d3a5d7914065490f08c0690/cffi-1.17.1-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:de55b766c7aa2e2a3092c51e0483d700341182f08e67c63630d5b6f200bb28e5", size = 485810, upload-time = "2024-09-04T20:44:35.191Z" }, - { url = "https://files.pythonhosted.org/packages/c7/8a/1d0e4a9c26e54746dc08c2c6c037889124d4f59dffd853a659fa545f1b40/cffi-1.17.1-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c59d6e989d07460165cc5ad3c61f9fd8f1b4796eacbd81cee78957842b834af4", size = 471200, upload-time = "2024-09-04T20:44:36.743Z" }, - { url = "https://files.pythonhosted.org/packages/26/9f/1aab65a6c0db35f43c4d1b4f580e8df53914310afc10ae0397d29d697af4/cffi-1.17.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dd398dbc6773384a17fe0d3e7eeb8d1a21c2200473ee6806bb5e6a8e62bb73dd", size = 479447, upload-time = "2024-09-04T20:44:38.492Z" }, - { url = "https://files.pythonhosted.org/packages/5f/e4/fb8b3dd8dc0e98edf1135ff067ae070bb32ef9d509d6cb0f538cd6f7483f/cffi-1.17.1-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:3edc8d958eb099c634dace3c7e16560ae474aa3803a5df240542b305d14e14ed", size = 484358, upload-time = "2024-09-04T20:44:40.046Z" }, - { url = "https://files.pythonhosted.org/packages/f1/47/d7145bf2dc04684935d57d67dff9d6d795b2ba2796806bb109864be3a151/cffi-1.17.1-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:72e72408cad3d5419375fc87d289076ee319835bdfa2caad331e377589aebba9", size = 488469, upload-time = "2024-09-04T20:44:41.616Z" }, - { url = "https://files.pythonhosted.org/packages/bf/ee/f94057fa6426481d663b88637a9a10e859e492c73d0384514a17d78ee205/cffi-1.17.1-cp313-cp313-win32.whl", hash = "sha256:e03eab0a8677fa80d646b5ddece1cbeaf556c313dcfac435ba11f107ba117b5d", size = 172475, upload-time = "2024-09-04T20:44:43.733Z" }, - { url = "https://files.pythonhosted.org/packages/7c/fc/6a8cb64e5f0324877d503c854da15d76c1e50eb722e320b15345c4d0c6de/cffi-1.17.1-cp313-cp313-win_amd64.whl", hash = "sha256:f6a16c31041f09ead72d69f583767292f750d24913dadacf5756b966aacb3f1a", size = 182009, upload-time = "2024-09-04T20:44:45.309Z" }, - { url = "https://files.pythonhosted.org/packages/48/08/15bf6b43ae9bd06f6b00ad8a91f5a8fe1069d4c9fab550a866755402724e/cffi-1.17.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:636062ea65bd0195bc012fea9321aca499c0504409f413dc88af450b57ffd03b", size = 182457, upload-time = "2024-09-04T20:44:47.892Z" }, - { url = "https://files.pythonhosted.org/packages/c2/5b/f1523dd545f92f7df468e5f653ffa4df30ac222f3c884e51e139878f1cb5/cffi-1.17.1-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c7eac2ef9b63c79431bc4b25f1cd649d7f061a28808cbc6c47b534bd789ef964", size = 425932, upload-time = "2024-09-04T20:44:49.491Z" }, - { url = "https://files.pythonhosted.org/packages/53/93/7e547ab4105969cc8c93b38a667b82a835dd2cc78f3a7dad6130cfd41e1d/cffi-1.17.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e221cf152cff04059d011ee126477f0d9588303eb57e88923578ace7baad17f9", size = 448585, upload-time = "2024-09-04T20:44:51.671Z" }, - { url = "https://files.pythonhosted.org/packages/56/c4/a308f2c332006206bb511de219efeff090e9d63529ba0a77aae72e82248b/cffi-1.17.1-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:31000ec67d4221a71bd3f67df918b1f88f676f1c3b535a7eb473255fdc0b83fc", size = 456268, upload-time = "2024-09-04T20:44:53.51Z" }, - { url = "https://files.pythonhosted.org/packages/ca/5b/b63681518265f2f4060d2b60755c1c77ec89e5e045fc3773b72735ddaad5/cffi-1.17.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6f17be4345073b0a7b8ea599688f692ac3ef23ce28e5df79c04de519dbc4912c", size = 436592, upload-time = "2024-09-04T20:44:55.085Z" }, - { url = "https://files.pythonhosted.org/packages/bb/19/b51af9f4a4faa4a8ac5a0e5d5c2522dcd9703d07fac69da34a36c4d960d3/cffi-1.17.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0e2b1fac190ae3ebfe37b979cc1ce69c81f4e4fe5746bb401dca63a9062cdaf1", size = 446512, upload-time = "2024-09-04T20:44:57.135Z" }, - { url = "https://files.pythonhosted.org/packages/e2/63/2bed8323890cb613bbecda807688a31ed11a7fe7afe31f8faaae0206a9a3/cffi-1.17.1-cp38-cp38-win32.whl", hash = "sha256:7596d6620d3fa590f677e9ee430df2958d2d6d6de2feeae5b20e82c00b76fbf8", size = 171576, upload-time = "2024-09-04T20:44:58.535Z" }, - { url = "https://files.pythonhosted.org/packages/2f/70/80c33b044ebc79527447fd4fbc5455d514c3bb840dede4455de97da39b4d/cffi-1.17.1-cp38-cp38-win_amd64.whl", hash = "sha256:78122be759c3f8a014ce010908ae03364d00a1f81ab5c7f4a7a5120607ea56e1", size = 181229, upload-time = "2024-09-04T20:44:59.963Z" }, - { url = "https://files.pythonhosted.org/packages/b9/ea/8bb50596b8ffbc49ddd7a1ad305035daa770202a6b782fc164647c2673ad/cffi-1.17.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:b2ab587605f4ba0bf81dc0cb08a41bd1c0a5906bd59243d56bad7668a6fc6c16", size = 182220, upload-time = "2024-09-04T20:45:01.577Z" }, - { url = "https://files.pythonhosted.org/packages/ae/11/e77c8cd24f58285a82c23af484cf5b124a376b32644e445960d1a4654c3a/cffi-1.17.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:28b16024becceed8c6dfbc75629e27788d8a3f9030691a1dbf9821a128b22c36", size = 178605, upload-time = "2024-09-04T20:45:03.837Z" }, - { url = "https://files.pythonhosted.org/packages/ed/65/25a8dc32c53bf5b7b6c2686b42ae2ad58743f7ff644844af7cdb29b49361/cffi-1.17.1-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1d599671f396c4723d016dbddb72fe8e0397082b0a77a4fab8028923bec050e8", size = 424910, upload-time = "2024-09-04T20:45:05.315Z" }, - { url = "https://files.pythonhosted.org/packages/42/7a/9d086fab7c66bd7c4d0f27c57a1b6b068ced810afc498cc8c49e0088661c/cffi-1.17.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ca74b8dbe6e8e8263c0ffd60277de77dcee6c837a3d0881d8c1ead7268c9e576", size = 447200, upload-time = "2024-09-04T20:45:06.903Z" }, - { url = "https://files.pythonhosted.org/packages/da/63/1785ced118ce92a993b0ec9e0d0ac8dc3e5dbfbcaa81135be56c69cabbb6/cffi-1.17.1-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f7f5baafcc48261359e14bcd6d9bff6d4b28d9103847c9e136694cb0501aef87", size = 454565, upload-time = "2024-09-04T20:45:08.975Z" }, - { url = "https://files.pythonhosted.org/packages/74/06/90b8a44abf3556599cdec107f7290277ae8901a58f75e6fe8f970cd72418/cffi-1.17.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:98e3969bcff97cae1b2def8ba499ea3d6f31ddfdb7635374834cf89a1a08ecf0", size = 435635, upload-time = "2024-09-04T20:45:10.64Z" }, - { url = "https://files.pythonhosted.org/packages/bd/62/a1f468e5708a70b1d86ead5bab5520861d9c7eacce4a885ded9faa7729c3/cffi-1.17.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cdf5ce3acdfd1661132f2a9c19cac174758dc2352bfe37d98aa7512c6b7178b3", size = 445218, upload-time = "2024-09-04T20:45:12.366Z" }, - { url = "https://files.pythonhosted.org/packages/5b/95/b34462f3ccb09c2594aa782d90a90b045de4ff1f70148ee79c69d37a0a5a/cffi-1.17.1-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:9755e4345d1ec879e3849e62222a18c7174d65a6a92d5b346b1863912168b595", size = 460486, upload-time = "2024-09-04T20:45:13.935Z" }, - { url = "https://files.pythonhosted.org/packages/fc/fc/a1e4bebd8d680febd29cf6c8a40067182b64f00c7d105f8f26b5bc54317b/cffi-1.17.1-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:f1e22e8c4419538cb197e4dd60acc919d7696e5ef98ee4da4e01d3f8cfa4cc5a", size = 437911, upload-time = "2024-09-04T20:45:15.696Z" }, - { url = "https://files.pythonhosted.org/packages/e6/c3/21cab7a6154b6a5ea330ae80de386e7665254835b9e98ecc1340b3a7de9a/cffi-1.17.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:c03e868a0b3bc35839ba98e74211ed2b05d2119be4e8a0f224fba9384f1fe02e", size = 460632, upload-time = "2024-09-04T20:45:17.284Z" }, - { url = "https://files.pythonhosted.org/packages/cb/b5/fd9f8b5a84010ca169ee49f4e4ad6f8c05f4e3545b72ee041dbbcb159882/cffi-1.17.1-cp39-cp39-win32.whl", hash = "sha256:e31ae45bc2e29f6b2abd0de1cc3b9d5205aa847cafaecb8af1476a609a2f6eb7", size = 171820, upload-time = "2024-09-04T20:45:18.762Z" }, - { url = "https://files.pythonhosted.org/packages/8c/52/b08750ce0bce45c143e1b5d7357ee8c55341b52bdef4b0f081af1eb248c2/cffi-1.17.1-cp39-cp39-win_amd64.whl", hash = "sha256:d016c76bdd850f3c626af19b0542c9677ba156e4ee4fccfdd7848803533ef662", size = 181290, upload-time = "2024-09-04T20:45:20.226Z" }, -] - -[[package]] -name = "cffi" -version = "2.0.0" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version == '3.9.*'", -] -dependencies = [ - { name = "pycparser", version = "2.23", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version == '3.9.*' and implementation_name != 'PyPy'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/eb/56/b1ba7935a17738ae8453301356628e8147c79dbb825bcbc73dc7401f9846/cffi-2.0.0.tar.gz", hash = "sha256:44d1b5909021139fe36001ae048dbdde8214afa20200eda0f64c068cac5d5529", size = 523588, upload-time = "2025-09-08T23:24:04.541Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/93/d7/516d984057745a6cd96575eea814fe1edd6646ee6efd552fb7b0921dec83/cffi-2.0.0-cp310-cp310-macosx_10_13_x86_64.whl", hash = "sha256:0cf2d91ecc3fcc0625c2c530fe004f82c110405f101548512cce44322fa8ac44", size = 184283, upload-time = "2025-09-08T23:22:08.01Z" }, - { url = "https://files.pythonhosted.org/packages/9e/84/ad6a0b408daa859246f57c03efd28e5dd1b33c21737c2db84cae8c237aa5/cffi-2.0.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:f73b96c41e3b2adedc34a7356e64c8eb96e03a3782b535e043a986276ce12a49", size = 180504, upload-time = "2025-09-08T23:22:10.637Z" }, - { url = "https://files.pythonhosted.org/packages/50/bd/b1a6362b80628111e6653c961f987faa55262b4002fcec42308cad1db680/cffi-2.0.0-cp310-cp310-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:53f77cbe57044e88bbd5ed26ac1d0514d2acf0591dd6bb02a3ae37f76811b80c", size = 208811, upload-time = "2025-09-08T23:22:12.267Z" }, - { url = "https://files.pythonhosted.org/packages/4f/27/6933a8b2562d7bd1fb595074cf99cc81fc3789f6a6c05cdabb46284a3188/cffi-2.0.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3e837e369566884707ddaf85fc1744b47575005c0a229de3327f8f9a20f4efeb", size = 216402, upload-time = "2025-09-08T23:22:13.455Z" }, - { url = "https://files.pythonhosted.org/packages/05/eb/b86f2a2645b62adcfff53b0dd97e8dfafb5c8aa864bd0d9a2c2049a0d551/cffi-2.0.0-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:5eda85d6d1879e692d546a078b44251cdd08dd1cfb98dfb77b670c97cee49ea0", size = 203217, upload-time = "2025-09-08T23:22:14.596Z" }, - { url = "https://files.pythonhosted.org/packages/9f/e0/6cbe77a53acf5acc7c08cc186c9928864bd7c005f9efd0d126884858a5fe/cffi-2.0.0-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:9332088d75dc3241c702d852d4671613136d90fa6881da7d770a483fd05248b4", size = 203079, upload-time = "2025-09-08T23:22:15.769Z" }, - { url = "https://files.pythonhosted.org/packages/98/29/9b366e70e243eb3d14a5cb488dfd3a0b6b2f1fb001a203f653b93ccfac88/cffi-2.0.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:fc7de24befaeae77ba923797c7c87834c73648a05a4bde34b3b7e5588973a453", size = 216475, upload-time = "2025-09-08T23:22:17.427Z" }, - { url = "https://files.pythonhosted.org/packages/21/7a/13b24e70d2f90a322f2900c5d8e1f14fa7e2a6b3332b7309ba7b2ba51a5a/cffi-2.0.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:cf364028c016c03078a23b503f02058f1814320a56ad535686f90565636a9495", size = 218829, upload-time = "2025-09-08T23:22:19.069Z" }, - { url = "https://files.pythonhosted.org/packages/60/99/c9dc110974c59cc981b1f5b66e1d8af8af764e00f0293266824d9c4254bc/cffi-2.0.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:e11e82b744887154b182fd3e7e8512418446501191994dbf9c9fc1f32cc8efd5", size = 211211, upload-time = "2025-09-08T23:22:20.588Z" }, - { url = "https://files.pythonhosted.org/packages/49/72/ff2d12dbf21aca1b32a40ed792ee6b40f6dc3a9cf1644bd7ef6e95e0ac5e/cffi-2.0.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:8ea985900c5c95ce9db1745f7933eeef5d314f0565b27625d9a10ec9881e1bfb", size = 218036, upload-time = "2025-09-08T23:22:22.143Z" }, - { url = "https://files.pythonhosted.org/packages/e2/cc/027d7fb82e58c48ea717149b03bcadcbdc293553edb283af792bd4bcbb3f/cffi-2.0.0-cp310-cp310-win32.whl", hash = "sha256:1f72fb8906754ac8a2cc3f9f5aaa298070652a0ffae577e0ea9bd480dc3c931a", size = 172184, upload-time = "2025-09-08T23:22:23.328Z" }, - { url = "https://files.pythonhosted.org/packages/33/fa/072dd15ae27fbb4e06b437eb6e944e75b068deb09e2a2826039e49ee2045/cffi-2.0.0-cp310-cp310-win_amd64.whl", hash = "sha256:b18a3ed7d5b3bd8d9ef7a8cb226502c6bf8308df1525e1cc676c3680e7176739", size = 182790, upload-time = "2025-09-08T23:22:24.752Z" }, - { url = "https://files.pythonhosted.org/packages/12/4a/3dfd5f7850cbf0d06dc84ba9aa00db766b52ca38d8b86e3a38314d52498c/cffi-2.0.0-cp311-cp311-macosx_10_13_x86_64.whl", hash = "sha256:b4c854ef3adc177950a8dfc81a86f5115d2abd545751a304c5bcf2c2c7283cfe", size = 184344, upload-time = "2025-09-08T23:22:26.456Z" }, - { url = "https://files.pythonhosted.org/packages/4f/8b/f0e4c441227ba756aafbe78f117485b25bb26b1c059d01f137fa6d14896b/cffi-2.0.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2de9a304e27f7596cd03d16f1b7c72219bd944e99cc52b84d0145aefb07cbd3c", size = 180560, upload-time = "2025-09-08T23:22:28.197Z" }, - { url = "https://files.pythonhosted.org/packages/b1/b7/1200d354378ef52ec227395d95c2576330fd22a869f7a70e88e1447eb234/cffi-2.0.0-cp311-cp311-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:baf5215e0ab74c16e2dd324e8ec067ef59e41125d3eade2b863d294fd5035c92", size = 209613, upload-time = "2025-09-08T23:22:29.475Z" }, - { url = "https://files.pythonhosted.org/packages/b8/56/6033f5e86e8cc9bb629f0077ba71679508bdf54a9a5e112a3c0b91870332/cffi-2.0.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:730cacb21e1bdff3ce90babf007d0a0917cc3e6492f336c2f0134101e0944f93", size = 216476, upload-time = "2025-09-08T23:22:31.063Z" }, - { url = "https://files.pythonhosted.org/packages/dc/7f/55fecd70f7ece178db2f26128ec41430d8720f2d12ca97bf8f0a628207d5/cffi-2.0.0-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:6824f87845e3396029f3820c206e459ccc91760e8fa24422f8b0c3d1731cbec5", size = 203374, upload-time = "2025-09-08T23:22:32.507Z" }, - { url = "https://files.pythonhosted.org/packages/84/ef/a7b77c8bdc0f77adc3b46888f1ad54be8f3b7821697a7b89126e829e676a/cffi-2.0.0-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:9de40a7b0323d889cf8d23d1ef214f565ab154443c42737dfe52ff82cf857664", size = 202597, upload-time = "2025-09-08T23:22:34.132Z" }, - { url = "https://files.pythonhosted.org/packages/d7/91/500d892b2bf36529a75b77958edfcd5ad8e2ce4064ce2ecfeab2125d72d1/cffi-2.0.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:8941aaadaf67246224cee8c3803777eed332a19d909b47e29c9842ef1e79ac26", size = 215574, upload-time = "2025-09-08T23:22:35.443Z" }, - { url = "https://files.pythonhosted.org/packages/44/64/58f6255b62b101093d5df22dcb752596066c7e89dd725e0afaed242a61be/cffi-2.0.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:a05d0c237b3349096d3981b727493e22147f934b20f6f125a3eba8f994bec4a9", size = 218971, upload-time = "2025-09-08T23:22:36.805Z" }, - { url = "https://files.pythonhosted.org/packages/ab/49/fa72cebe2fd8a55fbe14956f9970fe8eb1ac59e5df042f603ef7c8ba0adc/cffi-2.0.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:94698a9c5f91f9d138526b48fe26a199609544591f859c870d477351dc7b2414", size = 211972, upload-time = "2025-09-08T23:22:38.436Z" }, - { url = "https://files.pythonhosted.org/packages/0b/28/dd0967a76aab36731b6ebfe64dec4e981aff7e0608f60c2d46b46982607d/cffi-2.0.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:5fed36fccc0612a53f1d4d9a816b50a36702c28a2aa880cb8a122b3466638743", size = 217078, upload-time = "2025-09-08T23:22:39.776Z" }, - { url = "https://files.pythonhosted.org/packages/2b/c0/015b25184413d7ab0a410775fdb4a50fca20f5589b5dab1dbbfa3baad8ce/cffi-2.0.0-cp311-cp311-win32.whl", hash = "sha256:c649e3a33450ec82378822b3dad03cc228b8f5963c0c12fc3b1e0ab940f768a5", size = 172076, upload-time = "2025-09-08T23:22:40.95Z" }, - { url = "https://files.pythonhosted.org/packages/ae/8f/dc5531155e7070361eb1b7e4c1a9d896d0cb21c49f807a6c03fd63fc877e/cffi-2.0.0-cp311-cp311-win_amd64.whl", hash = "sha256:66f011380d0e49ed280c789fbd08ff0d40968ee7b665575489afa95c98196ab5", size = 182820, upload-time = "2025-09-08T23:22:42.463Z" }, - { url = "https://files.pythonhosted.org/packages/95/5c/1b493356429f9aecfd56bc171285a4c4ac8697f76e9bbbbb105e537853a1/cffi-2.0.0-cp311-cp311-win_arm64.whl", hash = "sha256:c6638687455baf640e37344fe26d37c404db8b80d037c3d29f58fe8d1c3b194d", size = 177635, upload-time = "2025-09-08T23:22:43.623Z" }, - { url = "https://files.pythonhosted.org/packages/ea/47/4f61023ea636104d4f16ab488e268b93008c3d0bb76893b1b31db1f96802/cffi-2.0.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:6d02d6655b0e54f54c4ef0b94eb6be0607b70853c45ce98bd278dc7de718be5d", size = 185271, upload-time = "2025-09-08T23:22:44.795Z" }, - { url = "https://files.pythonhosted.org/packages/df/a2/781b623f57358e360d62cdd7a8c681f074a71d445418a776eef0aadb4ab4/cffi-2.0.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:8eca2a813c1cb7ad4fb74d368c2ffbbb4789d377ee5bb8df98373c2cc0dee76c", size = 181048, upload-time = "2025-09-08T23:22:45.938Z" }, - { url = "https://files.pythonhosted.org/packages/ff/df/a4f0fbd47331ceeba3d37c2e51e9dfc9722498becbeec2bd8bc856c9538a/cffi-2.0.0-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:21d1152871b019407d8ac3985f6775c079416c282e431a4da6afe7aefd2bccbe", size = 212529, upload-time = "2025-09-08T23:22:47.349Z" }, - { url = "https://files.pythonhosted.org/packages/d5/72/12b5f8d3865bf0f87cf1404d8c374e7487dcf097a1c91c436e72e6badd83/cffi-2.0.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:b21e08af67b8a103c71a250401c78d5e0893beff75e28c53c98f4de42f774062", size = 220097, upload-time = "2025-09-08T23:22:48.677Z" }, - { url = "https://files.pythonhosted.org/packages/c2/95/7a135d52a50dfa7c882ab0ac17e8dc11cec9d55d2c18dda414c051c5e69e/cffi-2.0.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:1e3a615586f05fc4065a8b22b8152f0c1b00cdbc60596d187c2a74f9e3036e4e", size = 207983, upload-time = "2025-09-08T23:22:50.06Z" }, - { url = "https://files.pythonhosted.org/packages/3a/c8/15cb9ada8895957ea171c62dc78ff3e99159ee7adb13c0123c001a2546c1/cffi-2.0.0-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:81afed14892743bbe14dacb9e36d9e0e504cd204e0b165062c488942b9718037", size = 206519, upload-time = "2025-09-08T23:22:51.364Z" }, - { url = "https://files.pythonhosted.org/packages/78/2d/7fa73dfa841b5ac06c7b8855cfc18622132e365f5b81d02230333ff26e9e/cffi-2.0.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:3e17ed538242334bf70832644a32a7aae3d83b57567f9fd60a26257e992b79ba", size = 219572, upload-time = "2025-09-08T23:22:52.902Z" }, - { url = "https://files.pythonhosted.org/packages/07/e0/267e57e387b4ca276b90f0434ff88b2c2241ad72b16d31836adddfd6031b/cffi-2.0.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:3925dd22fa2b7699ed2617149842d2e6adde22b262fcbfada50e3d195e4b3a94", size = 222963, upload-time = "2025-09-08T23:22:54.518Z" }, - { url = "https://files.pythonhosted.org/packages/b6/75/1f2747525e06f53efbd878f4d03bac5b859cbc11c633d0fb81432d98a795/cffi-2.0.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:2c8f814d84194c9ea681642fd164267891702542f028a15fc97d4674b6206187", size = 221361, upload-time = "2025-09-08T23:22:55.867Z" }, - { url = "https://files.pythonhosted.org/packages/7b/2b/2b6435f76bfeb6bbf055596976da087377ede68df465419d192acf00c437/cffi-2.0.0-cp312-cp312-win32.whl", hash = "sha256:da902562c3e9c550df360bfa53c035b2f241fed6d9aef119048073680ace4a18", size = 172932, upload-time = "2025-09-08T23:22:57.188Z" }, - { url = "https://files.pythonhosted.org/packages/f8/ed/13bd4418627013bec4ed6e54283b1959cf6db888048c7cf4b4c3b5b36002/cffi-2.0.0-cp312-cp312-win_amd64.whl", hash = "sha256:da68248800ad6320861f129cd9c1bf96ca849a2771a59e0344e88681905916f5", size = 183557, upload-time = "2025-09-08T23:22:58.351Z" }, - { url = "https://files.pythonhosted.org/packages/95/31/9f7f93ad2f8eff1dbc1c3656d7ca5bfd8fb52c9d786b4dcf19b2d02217fa/cffi-2.0.0-cp312-cp312-win_arm64.whl", hash = "sha256:4671d9dd5ec934cb9a73e7ee9676f9362aba54f7f34910956b84d727b0d73fb6", size = 177762, upload-time = "2025-09-08T23:22:59.668Z" }, - { url = "https://files.pythonhosted.org/packages/4b/8d/a0a47a0c9e413a658623d014e91e74a50cdd2c423f7ccfd44086ef767f90/cffi-2.0.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:00bdf7acc5f795150faa6957054fbbca2439db2f775ce831222b66f192f03beb", size = 185230, upload-time = "2025-09-08T23:23:00.879Z" }, - { url = "https://files.pythonhosted.org/packages/4a/d2/a6c0296814556c68ee32009d9c2ad4f85f2707cdecfd7727951ec228005d/cffi-2.0.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:45d5e886156860dc35862657e1494b9bae8dfa63bf56796f2fb56e1679fc0bca", size = 181043, upload-time = "2025-09-08T23:23:02.231Z" }, - { url = "https://files.pythonhosted.org/packages/b0/1e/d22cc63332bd59b06481ceaac49d6c507598642e2230f201649058a7e704/cffi-2.0.0-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:07b271772c100085dd28b74fa0cd81c8fb1a3ba18b21e03d7c27f3436a10606b", size = 212446, upload-time = "2025-09-08T23:23:03.472Z" }, - { url = "https://files.pythonhosted.org/packages/a9/f5/a2c23eb03b61a0b8747f211eb716446c826ad66818ddc7810cc2cc19b3f2/cffi-2.0.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:d48a880098c96020b02d5a1f7d9251308510ce8858940e6fa99ece33f610838b", size = 220101, upload-time = "2025-09-08T23:23:04.792Z" }, - { url = "https://files.pythonhosted.org/packages/f2/7f/e6647792fc5850d634695bc0e6ab4111ae88e89981d35ac269956605feba/cffi-2.0.0-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:f93fd8e5c8c0a4aa1f424d6173f14a892044054871c771f8566e4008eaa359d2", size = 207948, upload-time = "2025-09-08T23:23:06.127Z" }, - { url = "https://files.pythonhosted.org/packages/cb/1e/a5a1bd6f1fb30f22573f76533de12a00bf274abcdc55c8edab639078abb6/cffi-2.0.0-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:dd4f05f54a52fb558f1ba9f528228066954fee3ebe629fc1660d874d040ae5a3", size = 206422, upload-time = "2025-09-08T23:23:07.753Z" }, - { url = "https://files.pythonhosted.org/packages/98/df/0a1755e750013a2081e863e7cd37e0cdd02664372c754e5560099eb7aa44/cffi-2.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:c8d3b5532fc71b7a77c09192b4a5a200ea992702734a2e9279a37f2478236f26", size = 219499, upload-time = "2025-09-08T23:23:09.648Z" }, - { url = "https://files.pythonhosted.org/packages/50/e1/a969e687fcf9ea58e6e2a928ad5e2dd88cc12f6f0ab477e9971f2309b57c/cffi-2.0.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:d9b29c1f0ae438d5ee9acb31cadee00a58c46cc9c0b2f9038c6b0b3470877a8c", size = 222928, upload-time = "2025-09-08T23:23:10.928Z" }, - { url = "https://files.pythonhosted.org/packages/36/54/0362578dd2c9e557a28ac77698ed67323ed5b9775ca9d3fe73fe191bb5d8/cffi-2.0.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:6d50360be4546678fc1b79ffe7a66265e28667840010348dd69a314145807a1b", size = 221302, upload-time = "2025-09-08T23:23:12.42Z" }, - { url = "https://files.pythonhosted.org/packages/eb/6d/bf9bda840d5f1dfdbf0feca87fbdb64a918a69bca42cfa0ba7b137c48cb8/cffi-2.0.0-cp313-cp313-win32.whl", hash = "sha256:74a03b9698e198d47562765773b4a8309919089150a0bb17d829ad7b44b60d27", size = 172909, upload-time = "2025-09-08T23:23:14.32Z" }, - { url = "https://files.pythonhosted.org/packages/37/18/6519e1ee6f5a1e579e04b9ddb6f1676c17368a7aba48299c3759bbc3c8b3/cffi-2.0.0-cp313-cp313-win_amd64.whl", hash = "sha256:19f705ada2530c1167abacb171925dd886168931e0a7b78f5bffcae5c6b5be75", size = 183402, upload-time = "2025-09-08T23:23:15.535Z" }, - { url = "https://files.pythonhosted.org/packages/cb/0e/02ceeec9a7d6ee63bb596121c2c8e9b3a9e150936f4fbef6ca1943e6137c/cffi-2.0.0-cp313-cp313-win_arm64.whl", hash = "sha256:256f80b80ca3853f90c21b23ee78cd008713787b1b1e93eae9f3d6a7134abd91", size = 177780, upload-time = "2025-09-08T23:23:16.761Z" }, - { url = "https://files.pythonhosted.org/packages/92/c4/3ce07396253a83250ee98564f8d7e9789fab8e58858f35d07a9a2c78de9f/cffi-2.0.0-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:fc33c5141b55ed366cfaad382df24fe7dcbc686de5be719b207bb248e3053dc5", size = 185320, upload-time = "2025-09-08T23:23:18.087Z" }, - { url = "https://files.pythonhosted.org/packages/59/dd/27e9fa567a23931c838c6b02d0764611c62290062a6d4e8ff7863daf9730/cffi-2.0.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:c654de545946e0db659b3400168c9ad31b5d29593291482c43e3564effbcee13", size = 181487, upload-time = "2025-09-08T23:23:19.622Z" }, - { url = "https://files.pythonhosted.org/packages/d6/43/0e822876f87ea8a4ef95442c3d766a06a51fc5298823f884ef87aaad168c/cffi-2.0.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:24b6f81f1983e6df8db3adc38562c83f7d4a0c36162885ec7f7b77c7dcbec97b", size = 220049, upload-time = "2025-09-08T23:23:20.853Z" }, - { url = "https://files.pythonhosted.org/packages/b4/89/76799151d9c2d2d1ead63c2429da9ea9d7aac304603de0c6e8764e6e8e70/cffi-2.0.0-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:12873ca6cb9b0f0d3a0da705d6086fe911591737a59f28b7936bdfed27c0d47c", size = 207793, upload-time = "2025-09-08T23:23:22.08Z" }, - { url = "https://files.pythonhosted.org/packages/bb/dd/3465b14bb9e24ee24cb88c9e3730f6de63111fffe513492bf8c808a3547e/cffi-2.0.0-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:d9b97165e8aed9272a6bb17c01e3cc5871a594a446ebedc996e2397a1c1ea8ef", size = 206300, upload-time = "2025-09-08T23:23:23.314Z" }, - { url = "https://files.pythonhosted.org/packages/47/d9/d83e293854571c877a92da46fdec39158f8d7e68da75bf73581225d28e90/cffi-2.0.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:afb8db5439b81cf9c9d0c80404b60c3cc9c3add93e114dcae767f1477cb53775", size = 219244, upload-time = "2025-09-08T23:23:24.541Z" }, - { url = "https://files.pythonhosted.org/packages/2b/0f/1f177e3683aead2bb00f7679a16451d302c436b5cbf2505f0ea8146ef59e/cffi-2.0.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:737fe7d37e1a1bffe70bd5754ea763a62a066dc5913ca57e957824b72a85e205", size = 222828, upload-time = "2025-09-08T23:23:26.143Z" }, - { url = "https://files.pythonhosted.org/packages/c6/0f/cafacebd4b040e3119dcb32fed8bdef8dfe94da653155f9d0b9dc660166e/cffi-2.0.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:38100abb9d1b1435bc4cc340bb4489635dc2f0da7456590877030c9b3d40b0c1", size = 220926, upload-time = "2025-09-08T23:23:27.873Z" }, - { url = "https://files.pythonhosted.org/packages/3e/aa/df335faa45b395396fcbc03de2dfcab242cd61a9900e914fe682a59170b1/cffi-2.0.0-cp314-cp314-win32.whl", hash = "sha256:087067fa8953339c723661eda6b54bc98c5625757ea62e95eb4898ad5e776e9f", size = 175328, upload-time = "2025-09-08T23:23:44.61Z" }, - { url = "https://files.pythonhosted.org/packages/bb/92/882c2d30831744296ce713f0feb4c1cd30f346ef747b530b5318715cc367/cffi-2.0.0-cp314-cp314-win_amd64.whl", hash = "sha256:203a48d1fb583fc7d78a4c6655692963b860a417c0528492a6bc21f1aaefab25", size = 185650, upload-time = "2025-09-08T23:23:45.848Z" }, - { url = "https://files.pythonhosted.org/packages/9f/2c/98ece204b9d35a7366b5b2c6539c350313ca13932143e79dc133ba757104/cffi-2.0.0-cp314-cp314-win_arm64.whl", hash = "sha256:dbd5c7a25a7cb98f5ca55d258b103a2054f859a46ae11aaf23134f9cc0d356ad", size = 180687, upload-time = "2025-09-08T23:23:47.105Z" }, - { url = "https://files.pythonhosted.org/packages/3e/61/c768e4d548bfa607abcda77423448df8c471f25dbe64fb2ef6d555eae006/cffi-2.0.0-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:9a67fc9e8eb39039280526379fb3a70023d77caec1852002b4da7e8b270c4dd9", size = 188773, upload-time = "2025-09-08T23:23:29.347Z" }, - { url = "https://files.pythonhosted.org/packages/2c/ea/5f76bce7cf6fcd0ab1a1058b5af899bfbef198bea4d5686da88471ea0336/cffi-2.0.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:7a66c7204d8869299919db4d5069a82f1561581af12b11b3c9f48c584eb8743d", size = 185013, upload-time = "2025-09-08T23:23:30.63Z" }, - { url = "https://files.pythonhosted.org/packages/be/b4/c56878d0d1755cf9caa54ba71e5d049479c52f9e4afc230f06822162ab2f/cffi-2.0.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:7cc09976e8b56f8cebd752f7113ad07752461f48a58cbba644139015ac24954c", size = 221593, upload-time = "2025-09-08T23:23:31.91Z" }, - { url = "https://files.pythonhosted.org/packages/e0/0d/eb704606dfe8033e7128df5e90fee946bbcb64a04fcdaa97321309004000/cffi-2.0.0-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:92b68146a71df78564e4ef48af17551a5ddd142e5190cdf2c5624d0c3ff5b2e8", size = 209354, upload-time = "2025-09-08T23:23:33.214Z" }, - { url = "https://files.pythonhosted.org/packages/d8/19/3c435d727b368ca475fb8742ab97c9cb13a0de600ce86f62eab7fa3eea60/cffi-2.0.0-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:b1e74d11748e7e98e2f426ab176d4ed720a64412b6a15054378afdb71e0f37dc", size = 208480, upload-time = "2025-09-08T23:23:34.495Z" }, - { url = "https://files.pythonhosted.org/packages/d0/44/681604464ed9541673e486521497406fadcc15b5217c3e326b061696899a/cffi-2.0.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:28a3a209b96630bca57cce802da70c266eb08c6e97e5afd61a75611ee6c64592", size = 221584, upload-time = "2025-09-08T23:23:36.096Z" }, - { url = "https://files.pythonhosted.org/packages/25/8e/342a504ff018a2825d395d44d63a767dd8ebc927ebda557fecdaca3ac33a/cffi-2.0.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:7553fb2090d71822f02c629afe6042c299edf91ba1bf94951165613553984512", size = 224443, upload-time = "2025-09-08T23:23:37.328Z" }, - { url = "https://files.pythonhosted.org/packages/e1/5e/b666bacbbc60fbf415ba9988324a132c9a7a0448a9a8f125074671c0f2c3/cffi-2.0.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:6c6c373cfc5c83a975506110d17457138c8c63016b563cc9ed6e056a82f13ce4", size = 223437, upload-time = "2025-09-08T23:23:38.945Z" }, - { url = "https://files.pythonhosted.org/packages/a0/1d/ec1a60bd1a10daa292d3cd6bb0b359a81607154fb8165f3ec95fe003b85c/cffi-2.0.0-cp314-cp314t-win32.whl", hash = "sha256:1fc9ea04857caf665289b7a75923f2c6ed559b8298a1b8c49e59f7dd95c8481e", size = 180487, upload-time = "2025-09-08T23:23:40.423Z" }, - { url = "https://files.pythonhosted.org/packages/bf/41/4c1168c74fac325c0c8156f04b6749c8b6a8f405bbf91413ba088359f60d/cffi-2.0.0-cp314-cp314t-win_amd64.whl", hash = "sha256:d68b6cef7827e8641e8ef16f4494edda8b36104d79773a334beaa1e3521430f6", size = 191726, upload-time = "2025-09-08T23:23:41.742Z" }, - { url = "https://files.pythonhosted.org/packages/ae/3a/dbeec9d1ee0844c679f6bb5d6ad4e9f198b1224f4e7a32825f47f6192b0c/cffi-2.0.0-cp314-cp314t-win_arm64.whl", hash = "sha256:0a1527a803f0a659de1af2e1fd700213caba79377e27e4693648c2923da066f9", size = 184195, upload-time = "2025-09-08T23:23:43.004Z" }, - { url = "https://files.pythonhosted.org/packages/c0/cc/08ed5a43f2996a16b462f64a7055c6e962803534924b9b2f1371d8c00b7b/cffi-2.0.0-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:fe562eb1a64e67dd297ccc4f5addea2501664954f2692b69a76449ec7913ecbf", size = 184288, upload-time = "2025-09-08T23:23:48.404Z" }, - { url = "https://files.pythonhosted.org/packages/3d/de/38d9726324e127f727b4ecc376bc85e505bfe61ef130eaf3f290c6847dd4/cffi-2.0.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:de8dad4425a6ca6e4e5e297b27b5c824ecc7581910bf9aee86cb6835e6812aa7", size = 180509, upload-time = "2025-09-08T23:23:49.73Z" }, - { url = "https://files.pythonhosted.org/packages/9b/13/c92e36358fbcc39cf0962e83223c9522154ee8630e1df7c0b3a39a8124e2/cffi-2.0.0-cp39-cp39-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:4647afc2f90d1ddd33441e5b0e85b16b12ddec4fca55f0d9671fef036ecca27c", size = 208813, upload-time = "2025-09-08T23:23:51.263Z" }, - { url = "https://files.pythonhosted.org/packages/15/12/a7a79bd0df4c3bff744b2d7e52cc1b68d5e7e427b384252c42366dc1ecbc/cffi-2.0.0-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3f4d46d8b35698056ec29bca21546e1551a205058ae1a181d871e278b0b28165", size = 216498, upload-time = "2025-09-08T23:23:52.494Z" }, - { url = "https://files.pythonhosted.org/packages/a3/ad/5c51c1c7600bdd7ed9a24a203ec255dccdd0ebf4527f7b922a0bde2fb6ed/cffi-2.0.0-cp39-cp39-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e6e73b9e02893c764e7e8d5bb5ce277f1a009cd5243f8228f75f842bf937c534", size = 203243, upload-time = "2025-09-08T23:23:53.836Z" }, - { url = "https://files.pythonhosted.org/packages/32/f2/81b63e288295928739d715d00952c8c6034cb6c6a516b17d37e0c8be5600/cffi-2.0.0-cp39-cp39-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:cb527a79772e5ef98fb1d700678fe031e353e765d1ca2d409c92263c6d43e09f", size = 203158, upload-time = "2025-09-08T23:23:55.169Z" }, - { url = "https://files.pythonhosted.org/packages/1f/74/cc4096ce66f5939042ae094e2e96f53426a979864aa1f96a621ad128be27/cffi-2.0.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:61d028e90346df14fedc3d1e5441df818d095f3b87d286825dfcbd6459b7ef63", size = 216548, upload-time = "2025-09-08T23:23:56.506Z" }, - { url = "https://files.pythonhosted.org/packages/e8/be/f6424d1dc46b1091ffcc8964fa7c0ab0cd36839dd2761b49c90481a6ba1b/cffi-2.0.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:0f6084a0ea23d05d20c3edcda20c3d006f9b6f3fefeac38f59262e10cef47ee2", size = 218897, upload-time = "2025-09-08T23:23:57.825Z" }, - { url = "https://files.pythonhosted.org/packages/f7/e0/dda537c2309817edf60109e39265f24f24aa7f050767e22c98c53fe7f48b/cffi-2.0.0-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:1cd13c99ce269b3ed80b417dcd591415d3372bcac067009b6e0f59c7d4015e65", size = 211249, upload-time = "2025-09-08T23:23:59.139Z" }, - { url = "https://files.pythonhosted.org/packages/2b/e7/7c769804eb75e4c4b35e658dba01de1640a351a9653c3d49ca89d16ccc91/cffi-2.0.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:89472c9762729b5ae1ad974b777416bfda4ac5642423fa93bd57a09204712322", size = 218041, upload-time = "2025-09-08T23:24:00.496Z" }, - { url = "https://files.pythonhosted.org/packages/aa/d9/6218d78f920dcd7507fc16a766b5ef8f3b913cc7aa938e7fc80b9978d089/cffi-2.0.0-cp39-cp39-win32.whl", hash = "sha256:2081580ebb843f759b9f617314a24ed5738c51d2aee65d31e02f6f7a2b97707a", size = 172138, upload-time = "2025-09-08T23:24:01.7Z" }, - { url = "https://files.pythonhosted.org/packages/54/8f/a1e836f82d8e32a97e6b29cc8f641779181ac7363734f12df27db803ebda/cffi-2.0.0-cp39-cp39-win_amd64.whl", hash = "sha256:b882b3df248017dba09d6b16defe9b5c407fe32fc7c65a9c69798e6175601be9", size = 182794, upload-time = "2025-09-08T23:24:02.943Z" }, -] - -[[package]] -name = "cffi" -version = "2.1.0" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version >= '3.10'", -] -dependencies = [ - { name = "pycparser", version = "3.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.10' and implementation_name != 'PyPy'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/57/5f/ff100cae70ebe9d8df1c01a00e510e45d9adb5c1fdda84791b199141de97/cffi-2.1.0.tar.gz", hash = "sha256:efc1cdd798b1aaf39b4610bba7aad28c9bea9b910f25c784ccf9ec1fa719d1f9", size = 531036, upload-time = "2026-07-06T21:34:30.382Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/c0/e9/6d7724983b3d5a0908dbf74f64038ade77c18646ff6636ec7894fd392ce1/cffi-2.1.0-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:b65f590ef2a44640f9a05dbb548a429b4ade77913ce683ac8b1480777658a6c0", size = 183837, upload-time = "2026-07-06T21:32:09.655Z" }, - { url = "https://files.pythonhosted.org/packages/69/aa/24580a278de21fd7322635556334d9b535f1cbc00b0a3919447cdf464c65/cffi-2.1.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:164bff1657b2a74f0b6d54e11c9b375bc97b931f2ca9c43fcf875838da1570dd", size = 184226, upload-time = "2026-07-06T21:32:11.196Z" }, - { url = "https://files.pythonhosted.org/packages/88/a9/02cae418ec4beb282ace11958d9d4737793439d561fadc7e6d56f2e2b354/cffi-2.1.0-cp310-cp310-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:c941bb58d5a6e1c3892d86e42927ed6c180302f07e6d395d08c416e594b98b46", size = 211107, upload-time = "2026-07-06T21:32:12.328Z" }, - { url = "https://files.pythonhosted.org/packages/3b/30/c806937ed5e4c2c7ac30d9d6b76b5dc57ff8b75d83800d9bb11a8253cf2a/cffi-2.1.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:a016194dbe13d14ee9556e734b772d8d67b947092b268d757fd4290e3ba2dfc2", size = 218733, upload-time = "2026-07-06T21:32:13.67Z" }, - { url = "https://files.pythonhosted.org/packages/f9/cf/398272b8bbfd58aa314fda5a7f1cdbb26d1d78ae324a11211521315dd1f0/cffi-2.1.0-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:03e9810d18c646077e501f661b682fbf5dee4676048527ca3cffe66faa9960dd", size = 205543, upload-time = "2026-07-06T21:32:15.148Z" }, - { url = "https://files.pythonhosted.org/packages/45/ca/f91641185cdd90c36d317a9dc7f85e88ef8682d8b300977baff5e23c35d8/cffi-2.1.0-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:19c54ac121cad98450b4896fa9a43ee0180d57bc4bc911a33db6cab1efab6cd3", size = 205460, upload-time = "2026-07-06T21:32:16.479Z" }, - { url = "https://files.pythonhosted.org/packages/38/66/04781a77b411f0bb5b234d62c1814754ab75ebe455ccff1b08e8d7aae98f/cffi-2.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:4d433a51f1870e43a13b6732f92aaf540ff77c2015097c78556f75a2d6c030e0", size = 218760, upload-time = "2026-07-06T21:32:17.98Z" }, - { url = "https://files.pythonhosted.org/packages/d0/9a/bb1d5ed9c3fcae158e9f6391bf309c95d98c2ac37ed56573228471d0af5e/cffi-2.1.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:3d7f118b5adbfdfead90c25822690b02bc8074fba949bb7858bec4ebd55adb43", size = 221230, upload-time = "2026-07-06T21:32:19.407Z" }, - { url = "https://files.pythonhosted.org/packages/41/aa/3c1409cdd26094efacd1c36c66e0a6eb9d4296e4fd4f9901b8b2042f4323/cffi-2.1.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:c5f5df567f6eb216de69be06ce55c8b714090fae02b18a3b40da8163b8c5fa9c", size = 213524, upload-time = "2026-07-06T21:32:20.828Z" }, - { url = "https://files.pythonhosted.org/packages/fa/75/74dfb7c3fc6ebbd408038476bd4c1d7e925c62614e7b9c534ecc34218288/cffi-2.1.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:11b3fb55f4f8ad92274ed26705f65d8f91457de71f5380061eb6d125a768fecd", size = 220341, upload-time = "2026-07-06T21:32:21.9Z" }, - { url = "https://files.pythonhosted.org/packages/70/b6/9003c33a3e7d2c1306f5962e646457dcfe5a8cd8fce6bbe02d7af25db783/cffi-2.1.0-cp310-cp310-win32.whl", hash = "sha256:9d72af0cf10a76a600a9690078fe31c63b9588c8e86bf9fd353f713c84b5db0f", size = 174578, upload-time = "2026-07-06T21:32:23.073Z" }, - { url = "https://files.pythonhosted.org/packages/8a/26/710688310447531c7a22f857c7f79d9855ec18b03e04494ced723fb37e2f/cffi-2.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:fb62edb5bb52cca65fab91a63afa7561607120d26090a7e8fda6fb9f064726da", size = 185071, upload-time = "2026-07-06T21:32:24.671Z" }, - { url = "https://files.pythonhosted.org/packages/d3/67/85c89a59ba36a671e79638f44d466749f08179266a57e4f2ffdf92174072/cffi-2.1.0-cp311-cp311-macosx_10_15_x86_64.whl", hash = "sha256:02cb7ff33ded4f1532476731f89ede53e2e488a8e6205515a82144246ffa7dcc", size = 183845, upload-time = "2026-07-06T21:32:26.32Z" }, - { url = "https://files.pythonhosted.org/packages/ea/dd/e3b0baa2d3d6a857ac72b7efbf18e32e487c9cdafcc13049ad765495b15e/cffi-2.1.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f5bce581e6b8c235e566a14768a943b172ada3ed73537bb0c0be1edee312d4e7", size = 184186, upload-time = "2026-07-06T21:32:28.025Z" }, - { url = "https://files.pythonhosted.org/packages/65/68/9f3ef890cf3c6ab97bd531c5677f67613d302165d16f8142b2811782a614/cffi-2.1.0-cp311-cp311-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:30b65779d598c370374fefabf138d456fd6f3216bfa7bedfab1ba82025b0cd93", size = 211892, upload-time = "2026-07-06T21:32:29.565Z" }, - { url = "https://files.pythonhosted.org/packages/22/d7/1a74539db16d8bfd839ff1515948948efbb162e574650fd3d846896eea95/cffi-2.1.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:88023dfe18799507b73f1dbb0d14326a17465de1bc9c9c7655c22845e9ddc3a2", size = 218793, upload-time = "2026-07-06T21:32:30.951Z" }, - { url = "https://files.pythonhosted.org/packages/ec/d1/9a5b7169499e8e8d8e636de70b97ac7c9447104d2ff1a2cd94790cea5162/cffi-2.1.0-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:0a96b74cda968eebbad56d973efe5098974f0a9fb323865bf99ea1fd24e3e64c", size = 205737, upload-time = "2026-07-06T21:32:32.216Z" }, - { url = "https://files.pythonhosted.org/packages/ba/b0/e131a9c41f10607926278453d9596163594fe1c4ebc46efe3b5e5b34eb84/cffi-2.1.0-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a5781494d4d400a3f47f8f1da94b324f6e6b440a53387774002890a2a2f4b50f", size = 204909, upload-time = "2026-07-06T21:32:33.655Z" }, - { url = "https://files.pythonhosted.org/packages/fb/d2/4398416cd699b35167947c6e22aca52c47e69ad5695073c9f1f2c52e04aa/cffi-2.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:aa7a1b53a2a4452ada2d1b5dade9960b2522f1e61293a811a077439e39029565", size = 217883, upload-time = "2026-07-06T21:32:35.173Z" }, - { url = "https://files.pythonhosted.org/packages/a2/a5/d4fe77b589e5e82d43ebc809bf2e6474afe8e48e32ea050b9357645b6471/cffi-2.1.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:9d8272c0e483b024e1b9ad029821470ed8ec65631dbd90217469da0e7cd89f1c", size = 221251, upload-time = "2026-07-06T21:32:36.527Z" }, - { url = "https://files.pythonhosted.org/packages/22/f0/a2fc43084c0433caf7f461bccc013e28f848d04ee1c5ed7fce71423cf4d9/cffi-2.1.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:7762faa47e8ff7eb80bd261d9a7d8eea2d8baa69de5e95b70c1f338bbe712f02", size = 214250, upload-time = "2026-07-06T21:32:37.852Z" }, - { url = "https://files.pythonhosted.org/packages/04/8c/b925975448cf20634a9fbd5efceb807219db452653648d2897c0989cab2d/cffi-2.1.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:89095c1968b4ba8285840e131bf2891b09ae137fe2146905acae0354fbce1b5e", size = 219441, upload-time = "2026-07-06T21:32:39.146Z" }, - { url = "https://files.pythonhosted.org/packages/eb/da/5c4918a2d61d86fa927d716cb3d8e4626ef8dc8f605a599d32f33897f59a/cffi-2.1.0-cp311-cp311-win32.whl", hash = "sha256:64c753a0f87a256020004f37a1c8c02c480e725f910f0b2a0f3f07debd1b2479", size = 174496, upload-time = "2026-07-06T21:32:40.467Z" }, - { url = "https://files.pythonhosted.org/packages/f9/c8/6c2de1d55cf35ef8b92885d5ef280790f0fb9634d87ea1cc315176aecd61/cffi-2.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:4f26194e3d95e06501b942642855aed4f953d55e95d7d01b7c4483db3ecff458", size = 185113, upload-time = "2026-07-06T21:32:41.761Z" }, - { url = "https://files.pythonhosted.org/packages/9e/4e/e8d7cb5783f1841a3c8fb3a7735838d7484d08ec08c9f984b14cac1ac0e9/cffi-2.1.0-cp311-cp311-win_arm64.whl", hash = "sha256:35aaea0c7ee0e58a5cd8c2fd1a48fdf7ece0d2699b7ecdda08194e9ce5dd9b3d", size = 179927, upload-time = "2026-07-06T21:32:42.961Z" }, - { url = "https://files.pythonhosted.org/packages/1e/85/990925db5df586ec90beb97529c853497e7f85ba0234830447faf41c3057/cffi-2.1.0-cp312-cp312-macosx_10_15_x86_64.whl", hash = "sha256:df2b82571a1b30f58a87bf4e5a9e78d2b1eff6c6ce8fd3aa3757221f93f0863f", size = 184829, upload-time = "2026-07-06T21:32:44.324Z" }, - { url = "https://files.pythonhosted.org/packages/4b/92/e7bb136ad6b5352603732cf907ef862ca103f20f2031c1735a46300c20c9/cffi-2.1.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:78474632761faa0fb96f30b1c928c84ebcf68713cbb80d15bab09dfe61640fde", size = 184728, upload-time = "2026-07-06T21:32:45.683Z" }, - { url = "https://files.pythonhosted.org/packages/c3/c0/d1ec30ffb370f748f2fb54425972bfef9871e0132e82fb589c46b6676049/cffi-2.1.0-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:5972433ad71a9e46516584ef60a0fda12d9dc459938d1539c3ddecf9bdc1368d", size = 214815, upload-time = "2026-07-06T21:32:48.557Z" }, - { url = "https://files.pythonhosted.org/packages/1b/dc/5620cf930688be01f2d673804291de757a934c90b946dbdc3d84130c2ea4/cffi-2.1.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:b6422532152adf4e59b110cb2808cee7a033800952f5c036b4af047ee43199e7", size = 222429, upload-time = "2026-07-06T21:32:49.848Z" }, - { url = "https://files.pythonhosted.org/packages/4b/a4/77b53abbf7a1e0beb9637edbef2a94d15f9c822f591e85d439ffd91519a6/cffi-2.1.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:46b1c8db8f6122420f32d02fffb924c2fe9bc772d228c7c711748fff56aabb2b", size = 210315, upload-time = "2026-07-06T21:32:51.221Z" }, - { url = "https://files.pythonhosted.org/packages/58/0c/f528df19cc94b675087324d4760d9e6d5bfae97d6217aa4fac43de4f5fcc/cffi-2.1.0-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:d9fafc5aa2e2a39aaf7f8cc0c1f044a9b07fca12e558dca53a3cc5c654ad67a7", size = 208859, upload-time = "2026-07-06T21:32:52.512Z" }, - { url = "https://files.pythonhosted.org/packages/62/f2/c9522a81c32132799a1972c39f5c5f8b4c8b9f00488a23feaa6c06f07741/cffi-2.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:1e9f50d192a3e525b15a75ab5114e442d83d657b7ec29182a991bc9a88fd3a66", size = 221844, upload-time = "2026-07-06T21:32:53.704Z" }, - { url = "https://files.pythonhosted.org/packages/6e/28/bd53988b9833e8f8ad539d26f4c07a6b3f6bcb1e9e02e7ca038250b3428d/cffi-2.1.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:98fff996e983a36d3aa2eca83af40c5821202e7e6f32d13ae94e3d2286f10cfe", size = 225287, upload-time = "2026-07-06T21:32:54.907Z" }, - { url = "https://files.pythonhosted.org/packages/79/99/0d0fd37f055224085f42bbb2c022d002e17dde4a97972822327b07d84101/cffi-2.1.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:379de10ce1ba048b1448599d1b37b24caee16309d1ac98d3982fc997f768700b", size = 223681, upload-time = "2026-07-06T21:32:56.329Z" }, - { url = "https://files.pythonhosted.org/packages/b0/80/c138990aa2a70b1a269f6e06348729836d733d6f970867943f61d367f8cc/cffi-2.1.0-cp312-cp312-win32.whl", hash = "sha256:9b8f0f26ca4e7513c534d351eca551947d053fac438f2a04ac96d882909b0d3a", size = 175269, upload-time = "2026-07-06T21:32:57.777Z" }, - { url = "https://files.pythonhosted.org/packages/a8/eb/f636456ff21a83fc13c032b58cc5dde061691546ac79efa284b2989b7982/cffi-2.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:c97f080ea627e2863524c5af3836e2270b5f5dfff1f104392b959f8df0c5d384", size = 185881, upload-time = "2026-07-06T21:32:59.253Z" }, - { url = "https://files.pythonhosted.org/packages/dd/2c/400ea43e721727dca8a65c4521390e9196757caba4a45643acb2b63271b8/cffi-2.1.0-cp312-cp312-win_arm64.whl", hash = "sha256:6d194185eabd279f1c05ebe3504265ddfc5ad2b58d0714f7db9f01da592e9eb6", size = 180088, upload-time = "2026-07-06T21:33:02.278Z" }, - { url = "https://files.pythonhosted.org/packages/96/88/a996879e2eeccb815f6e3a5967b12a308257412acec882039d386bd2aa7b/cffi-2.1.0-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:10537b1df4967ca26d21e5072d7d54188354483b91dc75058968d3f0cf13fbda", size = 194331, upload-time = "2026-07-06T21:33:03.697Z" }, - { url = "https://files.pythonhosted.org/packages/58/85/7ae00d5c8dd6266f4e944c3db630f3c5c9a98b61d469c714d848b1d8138a/cffi-2.1.0-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:a95b05f9baf29b91171b3a8bd2020b028835243e7b0ff6bb23e2a3c228518b1b", size = 196966, upload-time = "2026-07-06T21:33:05.353Z" }, - { url = "https://files.pythonhosted.org/packages/8c/e9/45c3a76ad8d43ad9261f4c95436da61128d3ca545d72b9612c0ab5be0b1c/cffi-2.1.0-cp313-cp313-macosx_10_15_x86_64.whl", hash = "sha256:15faec4adfff450819f3aee0e2e02c812de6edb88203aa58807955db2003472a", size = 184795, upload-time = "2026-07-06T21:33:06.699Z" }, - { url = "https://files.pythonhosted.org/packages/84/4c/82f132cb4418ee6d953d982b19191e87e2a6372c8a4ce36e50b69d6ade4a/cffi-2.1.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:716ff8ec22f20b4d988b12884086bcef0fc99737043e503f7a3935a6be99b1ea", size = 184746, upload-time = "2026-07-06T21:33:08.071Z" }, - { url = "https://files.pythonhosted.org/packages/a0/1c/4ed5a0e5bdca6cbc275556de3328dd1b76fd0c11cc13c88fe66d1d8715f2/cffi-2.1.0-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:63960549e4f8dc41e31accb97b975abaecfc44c03e396c093a6436763c2ea7db", size = 214747, upload-time = "2026-07-06T21:33:09.671Z" }, - { url = "https://files.pythonhosted.org/packages/3a/a6/e879bb68cc23a2bc9ba8f4b7d8019f0c2694bad2ab6c4a3701d429439f58/cffi-2.1.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:ff067a8d8d880e7809e4ac88eb009bb848870115317b306666502ccad30b147f", size = 222392, upload-time = "2026-07-06T21:33:10.896Z" }, - { url = "https://files.pythonhosted.org/packages/88/f6/01890cfd63c08f8eb96a8319b0443690197d240a8bd6346048cf7bde9190/cffi-2.1.0-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:3b926723c13eba9f81d2ef3820d63aeceec3b2d4639906047bf675cb8a7a500d", size = 210285, upload-time = "2026-07-06T21:33:12.251Z" }, - { url = "https://files.pythonhosted.org/packages/a6/cf/2b684132056f438567b61e19d690dd31cd0921ace051e0a458be6074369e/cffi-2.1.0-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:47ff3a8bfd8cb9da1af7524b965127095055654c177fcfc7578debcb015eecd0", size = 208801, upload-time = "2026-07-06T21:33:13.617Z" }, - { url = "https://files.pythonhosted.org/packages/6f/08/f2e7d62c460faae0926f2d6e423694aa409ced3bc1fe2927a0a6e5f05416/cffi-2.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:799416bae98336e400981ff6e532d67d5c709cfb30afb79865a1315f94b0e224", size = 221808, upload-time = "2026-07-06T21:33:15.466Z" }, - { url = "https://files.pythonhosted.org/packages/38/37/04f54b8e63a02f3d908332c9effbf8c366167c6f733ed8a3d4f79b7e2a1e/cffi-2.1.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:961be50688f7fba2fa65f63712d3b9b341a22311f5253460ce933f52f0de1c8c", size = 225241, upload-time = "2026-07-06T21:33:16.869Z" }, - { url = "https://files.pythonhosted.org/packages/a9/d6/c72eecca433cd3e681c65ed313ab4835d9d4a379704d0f628a6a05f51c2e/cffi-2.1.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:bf5c6cf48238b0eb4c086978c492ad1cbc22373fc5b2d7353b3a598ce6db887a", size = 223588, upload-time = "2026-07-06T21:33:18.239Z" }, - { url = "https://files.pythonhosted.org/packages/c6/4b/e706f67279140f92939da3475ad610df18bfd52d50f14953a8e5fede71d5/cffi-2.1.0-cp313-cp313-win32.whl", hash = "sha256:db3eb7d46527159a878ec3460e9d40615bc25ba337d477db681aea6e4f05c5d2", size = 175248, upload-time = "2026-07-06T21:33:19.799Z" }, - { url = "https://files.pythonhosted.org/packages/5a/47/59eb7975cb0e4ef0afa764ea945b29a5bb4537a9f771cb7d6c8a5dd74c95/cffi-2.1.0-cp313-cp313-win_amd64.whl", hash = "sha256:8e74a6135550c4748af665b1b1118b6aab33b1fc6a16f9aff630af107c3b4512", size = 185717, upload-time = "2026-07-06T21:33:21.47Z" }, - { url = "https://files.pythonhosted.org/packages/5a/af/34fee85c48f8d94efc8597bc09470c9dd274c145f1c12e0fbc6ab6d38d74/cffi-2.1.0-cp313-cp313-win_arm64.whl", hash = "sha256:2282cd5e38aa8accd03e99d1256af8411c84cdbee6a89d841b563fdbd1f3e50f", size = 180114, upload-time = "2026-07-06T21:33:22.515Z" }, - { url = "https://files.pythonhosted.org/packages/d8/f0/81478e482afa03f6d18dc8f2afb5edc45b3080853b634b5ed91961be0998/cffi-2.1.0-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:d2117334c3af3bdcb9a88522b844a2bdb5efdc4f71c6c822df55486ae1c3347a", size = 194142, upload-time = "2026-07-06T21:33:23.657Z" }, - { url = "https://files.pythonhosted.org/packages/7d/95/8de304305cd9204974b0ca051b86d307cafca13aa575a0ef1b44d92c0d8c/cffi-2.1.0-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:702c436735fbe99d59ada02a1f65cfc0d31c0ee8b7290912f8fbc5cd1e4b16c3", size = 196819, upload-time = "2026-07-06T21:33:25.007Z" }, - { url = "https://files.pythonhosted.org/packages/20/71/7c8372d30e42415602ed9f268f7cfd66f1b855fed881ecd168bcb45dbc0b/cffi-2.1.0-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:1ff3456eab0d889592d1936d6125bbfbc7ae4d3354a700f8bd80450a66445d4d", size = 184965, upload-time = "2026-07-06T21:33:26.605Z" }, - { url = "https://files.pythonhosted.org/packages/d6/5c/584e626835f0375c928176c04137c96927165cb8733cdb3150ec04e5ee5e/cffi-2.1.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:c4165821e131d6d4ca444347c2b694e2311bcfa3fe5a861cc72968f28867beac", size = 184952, upload-time = "2026-07-06T21:33:27.823Z" }, - { url = "https://files.pythonhosted.org/packages/2e/d2/065fcae1c73979fac8e054462478d0ff8a29c40cdc2ed7ea5676a061df53/cffi-2.1.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:276f20fffd7b396e12516ba8edf9509210ac248cbbc5acbc39cd512f9f59ebe6", size = 222353, upload-time = "2026-07-06T21:33:29.178Z" }, - { url = "https://files.pythonhosted.org/packages/ed/a5/e8bbb1ce5b3ac2f53ad6a10bde44318a5a8d99d4f4a000d44a6e39aeb3e4/cffi-2.1.0-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:7d5980a3433d4b71a5e120f9dd551403d7824e31e2e67124fe2769c404c06913", size = 210051, upload-time = "2026-07-06T21:33:30.534Z" }, - { url = "https://files.pythonhosted.org/packages/28/ed/c127d3ac36e899c965e3361357c3befacd6578c03f40125183e41c3b219e/cffi-2.1.0-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:6ca4919c6e4f89aa99c42510b42cf54596892c00b3f9077f6bdd1505e24b9c8d", size = 208630, upload-time = "2026-07-06T21:33:31.753Z" }, - { url = "https://files.pythonhosted.org/packages/cc/d7/97d3136f81db489ec8d1d67748c110d6c994268fd7528014aa9f2b085e4e/cffi-2.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:d53d10f7da99ae46f7373b9150393e9c5eab9b224909982b43832668de4779f5", size = 221593, upload-time = "2026-07-06T21:33:33.044Z" }, - { url = "https://files.pythonhosted.org/packages/d3/27/93195977168ee63aed233a1a0993a2178798654d1f4bddcdd321d6fd3b21/cffi-2.1.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:c351efb95e832a853a29361675f33a7ce53de1a109cd73fd47af0712213aa4ce", size = 225146, upload-time = "2026-07-06T21:33:34.224Z" }, - { url = "https://files.pythonhosted.org/packages/b3/c1/6dbd291ee2ae5a50a034aa057207081f545923bbf15dad4511e985aafff5/cffi-2.1.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:dbf7c7a88e2bac086f06d14577332760bdeecc42bdec8ac4077f6260557d9326", size = 223240, upload-time = "2026-07-06T21:33:35.57Z" }, - { url = "https://files.pythonhosted.org/packages/0f/6f/ade5ce9863a57992a6ea3d0d10d7e29b8749fc127204b3d493d667b2815f/cffi-2.1.0-cp314-cp314-win32.whl", hash = "sha256:1854b724d00f6654c742097d5387569021be12d3a0f770eae1df8f8acfcc6acd", size = 177723, upload-time = "2026-07-06T21:33:51.626Z" }, - { url = "https://files.pythonhosted.org/packages/41/de/92b9eeed4ae4a21d6fd9b2a2c8505cbed573299902ea73981cc13f7ff62c/cffi-2.1.0-cp314-cp314-win_amd64.whl", hash = "sha256:1b96bfe2c4bd825681b7d311ad6d9b7280a091f43e8f63da5729638083cd3bfb", size = 187937, upload-time = "2026-07-06T21:33:53.403Z" }, - { url = "https://files.pythonhosted.org/packages/2e/1a/cc6ae6c2913a03aab8898eee57963cf1035b8df5872ed8b9115fcc7e2be8/cffi-2.1.0-cp314-cp314-win_arm64.whl", hash = "sha256:7d28dff1db6764108bc30788d85d61c876beff416d9a49cb9dd7c5a9f34f5804", size = 183001, upload-time = "2026-07-06T21:33:54.74Z" }, - { url = "https://files.pythonhosted.org/packages/14/f0/134c00ce0779ec86dea2aa1aac69339c2741a8045072676763512363a2ea/cffi-2.1.0-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7ea6b3e2c4250ff1de21c630fe72d0f63eb95c2c32ffbf64a358cf4a8836d714", size = 188538, upload-time = "2026-07-06T21:33:36.792Z" }, - { url = "https://files.pythonhosted.org/packages/50/d8/3b86aba791cb610d24e8a3e1b2cd529e71fa15096b04e4d4e360049d4a4c/cffi-2.1.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:6af371f3767faeffc6ac1ef57cdfd25844403e9d3f476c5537caee499de96376", size = 188230, upload-time = "2026-07-06T21:33:38.011Z" }, - { url = "https://files.pythonhosted.org/packages/14/d0/117dcd9209255ad8571fbc8c92ef32593a1d294dcec91ddc4e4db50606f2/cffi-2.1.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:eb4e8997a49aa2c08a3e43c9045d224448b8941d88e7ac163c7d383e560cbf98", size = 223899, upload-time = "2026-07-06T21:33:39.514Z" }, - { url = "https://files.pythonhosted.org/packages/b6/3d/f20f8b886b254e3ad10e15cd4186d3aed49f3e6a35ab37aab9f8f25f7c03/cffi-2.1.0-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:bf01d8c84cbea96b944c73b22182e6c7c432b3475632b8111dbfdc95ddad6e13", size = 211652, upload-time = "2026-07-06T21:33:40.851Z" }, - { url = "https://files.pythonhosted.org/packages/28/3b/fad54de07260b93ddeef4b96d0131d57ea900675df1d410ae1deee52d7a6/cffi-2.1.0-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:33eb1ad83ebe8f313e0df035c406227d55a79456704a863fad9842136af5ad7d", size = 210755, upload-time = "2026-07-06T21:33:42.183Z" }, - { url = "https://files.pythonhosted.org/packages/cc/82/3d5c705acb7abbba9bbd7d79b8e62e0f25b6120eb7ae6ac49f1b721722fe/cffi-2.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ac0f1a2d0cfa7eea3f2aaf006ab6e70e8feeb16b75d65b7e5939982ca2f11056", size = 223933, upload-time = "2026-07-06T21:33:43.603Z" }, - { url = "https://files.pythonhosted.org/packages/6c/d0/47e338384ab6b1004241002fa616301020cea4fc95f283506565d252f276/cffi-2.1.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:c16914df9fb7f500e440e6875fa23ff5e0b31db01fa9c06af98d59a91f0dc2e4", size = 226749, upload-time = "2026-07-06T21:33:45.046Z" }, - { url = "https://files.pythonhosted.org/packages/70/25/65bd5b58ea4bfdfc15cde02cb5365f89ef8ab8b2adfb8fe5c4bd4233382f/cffi-2.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:5ecbd0499275d57506d397eebe1981cee87b47fcd9ef5c22cab7ed7644a39a94", size = 225703, upload-time = "2026-07-06T21:33:46.374Z" }, - { url = "https://files.pythonhosted.org/packages/dc/78/aa01ac599a8a4322533d45a1f9bc93b338276d2d59dabbe7c6d92a775c81/cffi-2.1.0-cp314-cp314t-win32.whl", hash = "sha256:7d034dcffa09e9a46c93fa3a3be402096cb5354ac6e41ab8e5cc9cd8b642ad76", size = 182857, upload-time = "2026-07-06T21:33:47.696Z" }, - { url = "https://files.pythonhosted.org/packages/b9/26/d00496b22de4d4228f32dde94ad996f350c8aad676d63bcca0743c8dea4d/cffi-2.1.0-cp314-cp314t-win_amd64.whl", hash = "sha256:0582a58f3051372229ca8e7f5f589f9e5632678208d8636fea3676711fdf7fe5", size = 194065, upload-time = "2026-07-06T21:33:48.953Z" }, - { url = "https://files.pythonhosted.org/packages/d5/dd/0c7dbf815a579ff005008a2d815a55d6bb047c349eef536d9dc53d3f0a8d/cffi-2.1.0-cp314-cp314t-win_arm64.whl", hash = "sha256:510aeeeac94811b138077451da1fb18b308a5feab47dd2b603af55804155e1c8", size = 186404, upload-time = "2026-07-06T21:33:50.309Z" }, - { url = "https://files.pythonhosted.org/packages/55/c7/8c8c50cb11c6750051daf12164098a9a6f027ac4356967fd4d800a07f242/cffi-2.1.0-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:2e9dabb9abcb7ad15938c7196ad5c1718a4e6d33cc79b4c0209bdb64c4a54a5c", size = 194121, upload-time = "2026-07-06T21:33:56.109Z" }, - { url = "https://files.pythonhosted.org/packages/99/e2/67680bf19a6b60d2bb7ff83baefa2a4c3d2d7dc0f3277034b802e1fc504c/cffi-2.1.0-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:37f525a7e7e50c017fdebe58b787be310ad59357ae43a053943a6e1a6c526001", size = 196820, upload-time = "2026-07-06T21:33:57.288Z" }, - { url = "https://files.pythonhosted.org/packages/ed/da/4bbe583a3b3a5c8c60892124fe17f3fa3656523faf0d3484eae90f091853/cffi-2.1.0-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:95f2954c2c9473d892eca6e0409f3568b37ab62a8eedb122461f73cc273476e3", size = 184936, upload-time = "2026-07-06T21:33:58.765Z" }, - { url = "https://files.pythonhosted.org/packages/e5/4b/1f4c36ab273980d7aa75bb126ea4f8971f24a96108acad3a0a084028c57b/cffi-2.1.0-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:cdf2448aab5f661c9315308ec8b93f4e8a1a67a3c733f8631067a2b67d5913dc", size = 185045, upload-time = "2026-07-06T21:34:00.085Z" }, - { url = "https://files.pythonhosted.org/packages/ef/c3/ad299dc38f3583f8d916b299f028af418a9ec98bc695fcbebeae7420691c/cffi-2.1.0-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:90bec57cf82089383bd06a605b3eb8daebf7e5a668520beaf6e327a83a947699", size = 222342, upload-time = "2026-07-06T21:34:01.814Z" }, - { url = "https://files.pythonhosted.org/packages/eb/d8/df4543cc087245044ed02ef3ad8e0a26619d0075ac7a77a12dc81177851b/cffi-2.1.0-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:6274dcb2d15cef48daa73ed1be5a40d501d74dccd0cd6db364776d12cb6ba022", size = 210073, upload-time = "2026-07-06T21:34:03.255Z" }, - { url = "https://files.pythonhosted.org/packages/2c/0e/fac738d73728c6cea2a88a2883dca54892496cbba88a1dc1f2909cb8a6f5/cffi-2.1.0-cp315-cp315-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:2b71d409cccee78310ab5dec549aed052aaea483346e282c7b02362596e01bb0", size = 208551, upload-time = "2026-07-06T21:34:04.433Z" }, - { url = "https://files.pythonhosted.org/packages/e6/3f/0b04a700dd64f465c93020253a793a82c9b4dff9961f48facd0df945d9b8/cffi-2.1.0-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:7d3538f9c0e50670f4deb93dbb696576e60590369cae2faf7de681e597a8a1f1", size = 221649, upload-time = "2026-07-06T21:34:06.157Z" }, - { url = "https://files.pythonhosted.org/packages/5d/7c/b7379a5704c79eda57ce075869ba70a0368d1c850f803b3c0d078d39dcaf/cffi-2.1.0-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:8f9ec95b8a043d3dfbc74d9abc6f7baf524dd27a8dc160b0a32ff9cdab650c28", size = 225203, upload-time = "2026-07-06T21:34:07.489Z" }, - { url = "https://files.pythonhosted.org/packages/5a/02/d5e6c43ea85c41bda2a184a3418f195fe7cf602967a8d2b94e085b83deef/cffi-2.1.0-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:af5e2915d41fe6c961694d7bfdc8562942638200f3ce2765dfb8b745cf997629", size = 223263, upload-time = "2026-07-06T21:34:08.712Z" }, - { url = "https://files.pythonhosted.org/packages/2c/d8/772b8259bf75749adffb1c546828978381fb516f60cf701f6c83daf60c85/cffi-2.1.0-cp315-cp315-win32.whl", hash = "sha256:0a42c688d19fca6e095a53c6a6e2295a5b050a8b289f109adab02a9e61a25de6", size = 177696, upload-time = "2026-07-06T21:34:26.355Z" }, - { url = "https://files.pythonhosted.org/packages/2f/dd/afa2191fc6d57fedd26e5844a2fe2fcc0bbfa00961bbaa5a41e4921e7cca/cffi-2.1.0-cp315-cp315-win_amd64.whl", hash = "sha256:bccbbb5ee76a61f9d99b5bf3846a51d7fca4b6a732fe46f89295610edaf41853", size = 187914, upload-time = "2026-07-06T21:34:27.58Z" }, - { url = "https://files.pythonhosted.org/packages/05/ef/6cd4f8c671517162379dc79cfae5aea9106bc38abb89628d5c16adf6a838/cffi-2.1.0-cp315-cp315-win_arm64.whl", hash = "sha256:8d35c139744adb3e727cd51b1a18324bbe44b8bd41bf8322bca4d41289f48eda", size = 183004, upload-time = "2026-07-06T21:34:28.905Z" }, - { url = "https://files.pythonhosted.org/packages/11/b6/12fc55092817a5faa26fb8c40c7f9d662e11a46ee248c137aafc42517d92/cffi-2.1.0-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:f9912624a0c0b834b7520d7769b3644453aabc0a7e1c839da7359f050750e9bc", size = 188378, upload-time = "2026-07-06T21:34:09.926Z" }, - { url = "https://files.pythonhosted.org/packages/8d/2e/cdac88979f295fde5daa69622c7d2111e56e7ceb94f211357fbe452339e4/cffi-2.1.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:df92f2aba50eb4d96718b68ef76f2e57a57b54f2fa62333496d16c6d585a85ca", size = 188319, upload-time = "2026-07-06T21:34:11.101Z" }, - { url = "https://files.pythonhosted.org/packages/e0/27/1d0b408497e41a74795af122d7b603c418c5fed0171450f899afd04e594f/cffi-2.1.0-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:0520e1f4c35f44e209cbbb421b67eec42e6a157f59444dfb6058874ff3610e5d", size = 223904, upload-time = "2026-07-06T21:34:12.606Z" }, - { url = "https://files.pythonhosted.org/packages/8b/31/e115c985105dd7ffb32444505f18ceb874bb42d992af05d5dced7ecf1980/cffi-2.1.0-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:3681e031db29958a7502f5c0c9d6bbc4c36cb20f7b104086fa642d1799631ff8", size = 211554, upload-time = "2026-07-06T21:34:13.987Z" }, - { url = "https://files.pythonhosted.org/packages/5a/67/9e6e09409336d9e515c58367e7cfcf4f89df06ad25252675595a58eb59d5/cffi-2.1.0-cp315-cp315t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:762f99479dcb369f60ab9017ad4ab97a36a1dd7c1ee5a3b15db0f4b8659120cd", size = 210795, upload-time = "2026-07-06T21:34:15.972Z" }, - { url = "https://files.pythonhosted.org/packages/19/e5/d3cc82a4a0be7902af279c04181ad038449c096734464a5ae1de3e1401bd/cffi-2.1.0-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:0611e7ebf90573a535ebdc33ae9da222d037853983e13359f580fab781ca017f", size = 223843, upload-time = "2026-07-06T21:34:17.509Z" }, - { url = "https://files.pythonhosted.org/packages/b9/65/b434abc97ce7cecc2c640fde160507c0ecc7e21544b483ba3325d2e2ea17/cffi-2.1.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:86cf8755a791f72c85dc287128cc62d4f24d392e3f1e15837245623f4a33cccc", size = 226773, upload-time = "2026-07-06T21:34:19.05Z" }, - { url = "https://files.pythonhosted.org/packages/b5/9f/d4dc66ca651eb1145a133314cda721abf13cfac3d28c4a0402263ae6ad75/cffi-2.1.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:ba00f661f8ba35d075c937174e27c2c421cec3942fd2e0ea3e66996757c0fdd9", size = 225719, upload-time = "2026-07-06T21:34:20.576Z" }, - { url = "https://files.pythonhosted.org/packages/68/5a/e536c528bc8057496c360c0978559a2dc45653f89dd6151078aa7d8fca1a/cffi-2.1.0-cp315-cp315t-win32.whl", hash = "sha256:cb96698e3c7413d906ce83f8ffd245ec1bd94707541f299d0ce4d6b0193e982b", size = 182760, upload-time = "2026-07-06T21:34:22.059Z" }, - { url = "https://files.pythonhosted.org/packages/d3/0b/0ffe8b82d3875bced5fa1e7986a7a46b748262a40ab7f60b475eb9fb1bb3/cffi-2.1.0-cp315-cp315t-win_amd64.whl", hash = "sha256:f146d154428a2523f9cc7936c02353c2459b8f6cf07d3cd1ee1c0a611109c5d5", size = 193769, upload-time = "2026-07-06T21:34:23.589Z" }, - { url = "https://files.pythonhosted.org/packages/a0/17/1073b53b68c9b5ca6914adf5f8bf55aacc2d3be102418c90700160ea8605/cffi-2.1.0-cp315-cp315t-win_arm64.whl", hash = "sha256:cbb7640ce37159548d2147b5b8c241f962143d4c71231431820783f4dc78f210", size = 186405, upload-time = "2026-07-06T21:34:24.857Z" }, -] - -[[package]] -name = "pycparser" -version = "2.23" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version == '3.9.*'", - "python_full_version < '3.9'", -] -sdist = { url = "https://files.pythonhosted.org/packages/fe/cf/d2d3b9f5699fb1e4615c8e32ff220203e43b248e1dfcc6736ad9057731ca/pycparser-2.23.tar.gz", hash = "sha256:78816d4f24add8f10a06d6f05b4d424ad9e96cfebf68a4ddc99c65c0720d00c2", size = 173734, upload-time = "2025-09-09T13:23:47.91Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/a0/e3/59cd50310fc9b59512193629e1984c1f95e5c8ae6e5d8c69532ccc65a7fe/pycparser-2.23-py3-none-any.whl", hash = "sha256:e5c6e8d3fbad53479cab09ac03729e0a9faf2bee3db8208a550daf5af81a5934", size = 118140, upload-time = "2025-09-09T13:23:46.651Z" }, -] - -[[package]] -name = "pycparser" -version = "3.0" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version >= '3.10'", -] -sdist = { url = "https://files.pythonhosted.org/packages/1b/7d/92392ff7815c21062bea51aa7b87d45576f649f16458d78b7cf94b9ab2e6/pycparser-3.0.tar.gz", hash = "sha256:600f49d217304a5902ac3c37e1281c9fe94e4d0489de643a9504c5cdfdfc6b29", size = 103492, upload-time = "2026-01-21T14:26:51.89Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/0c/c3/44f3fbbfa403ea2a7c779186dc20772604442dde72947e7d01069cbe98e3/pycparser-3.0-py3-none-any.whl", hash = "sha256:b727414169a36b7d524c1c3e31839a521725078d7b2ff038656844266160a992", size = 48172, upload-time = "2026-01-21T14:26:50.693Z" }, -] - -[[package]] -name = "pyzmq" -version = "27.1.0" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "cffi", version = "1.17.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9' and implementation_name == 'pypy'" }, - { name = "cffi", version = "2.0.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version == '3.9.*' and implementation_name == 'pypy'" }, - { name = "cffi", version = "2.1.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.10' and implementation_name == 'pypy'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/04/0b/3c9baedbdf613ecaa7aa07027780b8867f57b6293b6ee50de316c9f3222b/pyzmq-27.1.0.tar.gz", hash = "sha256:ac0765e3d44455adb6ddbf4417dcce460fc40a05978c08efdf2948072f6db540", size = 281750, upload-time = "2025-09-08T23:10:18.157Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/67/b9/52aa9ec2867528b54f1e60846728d8b4d84726630874fee3a91e66c7df81/pyzmq-27.1.0-cp310-cp310-macosx_10_15_universal2.whl", hash = "sha256:508e23ec9bc44c0005c4946ea013d9317ae00ac67778bd47519fdf5a0e930ff4", size = 1329850, upload-time = "2025-09-08T23:07:26.274Z" }, - { url = "https://files.pythonhosted.org/packages/99/64/5653e7b7425b169f994835a2b2abf9486264401fdef18df91ddae47ce2cc/pyzmq-27.1.0-cp310-cp310-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:507b6f430bdcf0ee48c0d30e734ea89ce5567fd7b8a0f0044a369c176aa44556", size = 906380, upload-time = "2025-09-08T23:07:29.78Z" }, - { url = "https://files.pythonhosted.org/packages/73/78/7d713284dbe022f6440e391bd1f3c48d9185673878034cfb3939cdf333b2/pyzmq-27.1.0-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bf7b38f9fd7b81cb6d9391b2946382c8237fd814075c6aa9c3b746d53076023b", size = 666421, upload-time = "2025-09-08T23:07:31.263Z" }, - { url = "https://files.pythonhosted.org/packages/30/76/8f099f9d6482450428b17c4d6b241281af7ce6a9de8149ca8c1c649f6792/pyzmq-27.1.0-cp310-cp310-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:03ff0b279b40d687691a6217c12242ee71f0fba28bf8626ff50e3ef0f4410e1e", size = 854149, upload-time = "2025-09-08T23:07:33.17Z" }, - { url = "https://files.pythonhosted.org/packages/59/f0/37fbfff06c68016019043897e4c969ceab18bde46cd2aca89821fcf4fb2e/pyzmq-27.1.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:677e744fee605753eac48198b15a2124016c009a11056f93807000ab11ce6526", size = 1655070, upload-time = "2025-09-08T23:07:35.205Z" }, - { url = "https://files.pythonhosted.org/packages/47/14/7254be73f7a8edc3587609554fcaa7bfd30649bf89cd260e4487ca70fdaa/pyzmq-27.1.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:dd2fec2b13137416a1c5648b7009499bcc8fea78154cd888855fa32514f3dad1", size = 2033441, upload-time = "2025-09-08T23:07:37.432Z" }, - { url = "https://files.pythonhosted.org/packages/22/dc/49f2be26c6f86f347e796a4d99b19167fc94503f0af3fd010ad262158822/pyzmq-27.1.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:08e90bb4b57603b84eab1d0ca05b3bbb10f60c1839dc471fc1c9e1507bef3386", size = 1891529, upload-time = "2025-09-08T23:07:39.047Z" }, - { url = "https://files.pythonhosted.org/packages/a3/3e/154fb963ae25be70c0064ce97776c937ecc7d8b0259f22858154a9999769/pyzmq-27.1.0-cp310-cp310-win32.whl", hash = "sha256:a5b42d7a0658b515319148875fcb782bbf118dd41c671b62dae33666c2213bda", size = 567276, upload-time = "2025-09-08T23:07:40.695Z" }, - { url = "https://files.pythonhosted.org/packages/62/b2/f4ab56c8c595abcb26b2be5fd9fa9e6899c1e5ad54964e93ae8bb35482be/pyzmq-27.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:c0bb87227430ee3aefcc0ade2088100e528d5d3298a0a715a64f3d04c60ba02f", size = 632208, upload-time = "2025-09-08T23:07:42.298Z" }, - { url = "https://files.pythonhosted.org/packages/3b/e3/be2cc7ab8332bdac0522fdb64c17b1b6241a795bee02e0196636ec5beb79/pyzmq-27.1.0-cp310-cp310-win_arm64.whl", hash = "sha256:9a916f76c2ab8d045b19f2286851a38e9ac94ea91faf65bd64735924522a8b32", size = 559766, upload-time = "2025-09-08T23:07:43.869Z" }, - { url = "https://files.pythonhosted.org/packages/06/5d/305323ba86b284e6fcb0d842d6adaa2999035f70f8c38a9b6d21ad28c3d4/pyzmq-27.1.0-cp311-cp311-macosx_10_15_universal2.whl", hash = "sha256:226b091818d461a3bef763805e75685e478ac17e9008f49fce2d3e52b3d58b86", size = 1333328, upload-time = "2025-09-08T23:07:45.946Z" }, - { url = "https://files.pythonhosted.org/packages/bd/a0/fc7e78a23748ad5443ac3275943457e8452da67fda347e05260261108cbc/pyzmq-27.1.0-cp311-cp311-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:0790a0161c281ca9723f804871b4027f2e8b5a528d357c8952d08cd1a9c15581", size = 908803, upload-time = "2025-09-08T23:07:47.551Z" }, - { url = "https://files.pythonhosted.org/packages/7e/22/37d15eb05f3bdfa4abea6f6d96eb3bb58585fbd3e4e0ded4e743bc650c97/pyzmq-27.1.0-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c895a6f35476b0c3a54e3eb6ccf41bf3018de937016e6e18748317f25d4e925f", size = 668836, upload-time = "2025-09-08T23:07:49.436Z" }, - { url = "https://files.pythonhosted.org/packages/b1/c4/2a6fe5111a01005fc7af3878259ce17684fabb8852815eda6225620f3c59/pyzmq-27.1.0-cp311-cp311-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5bbf8d3630bf96550b3be8e1fc0fea5cbdc8d5466c1192887bd94869da17a63e", size = 857038, upload-time = "2025-09-08T23:07:51.234Z" }, - { url = "https://files.pythonhosted.org/packages/cb/eb/bfdcb41d0db9cd233d6fb22dc131583774135505ada800ebf14dfb0a7c40/pyzmq-27.1.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:15c8bd0fe0dabf808e2d7a681398c4e5ded70a551ab47482067a572c054c8e2e", size = 1657531, upload-time = "2025-09-08T23:07:52.795Z" }, - { url = "https://files.pythonhosted.org/packages/ab/21/e3180ca269ed4a0de5c34417dfe71a8ae80421198be83ee619a8a485b0c7/pyzmq-27.1.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:bafcb3dd171b4ae9f19ee6380dfc71ce0390fefaf26b504c0e5f628d7c8c54f2", size = 2034786, upload-time = "2025-09-08T23:07:55.047Z" }, - { url = "https://files.pythonhosted.org/packages/3b/b1/5e21d0b517434b7f33588ff76c177c5a167858cc38ef740608898cd329f2/pyzmq-27.1.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:e829529fcaa09937189178115c49c504e69289abd39967cd8a4c215761373394", size = 1894220, upload-time = "2025-09-08T23:07:57.172Z" }, - { url = "https://files.pythonhosted.org/packages/03/f2/44913a6ff6941905efc24a1acf3d3cb6146b636c546c7406c38c49c403d4/pyzmq-27.1.0-cp311-cp311-win32.whl", hash = "sha256:6df079c47d5902af6db298ec92151db82ecb557af663098b92f2508c398bb54f", size = 567155, upload-time = "2025-09-08T23:07:59.05Z" }, - { url = "https://files.pythonhosted.org/packages/23/6d/d8d92a0eb270a925c9b4dd039c0b4dc10abc2fcbc48331788824ef113935/pyzmq-27.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:190cbf120fbc0fc4957b56866830def56628934a9d112aec0e2507aa6a032b97", size = 633428, upload-time = "2025-09-08T23:08:00.663Z" }, - { url = "https://files.pythonhosted.org/packages/ae/14/01afebc96c5abbbd713ecfc7469cfb1bc801c819a74ed5c9fad9a48801cb/pyzmq-27.1.0-cp311-cp311-win_arm64.whl", hash = "sha256:eca6b47df11a132d1745eb3b5b5e557a7dae2c303277aa0e69c6ba91b8736e07", size = 559497, upload-time = "2025-09-08T23:08:02.15Z" }, - { url = "https://files.pythonhosted.org/packages/92/e7/038aab64a946d535901103da16b953c8c9cc9c961dadcbf3609ed6428d23/pyzmq-27.1.0-cp312-abi3-macosx_10_15_universal2.whl", hash = "sha256:452631b640340c928fa343801b0d07eb0c3789a5ffa843f6e1a9cee0ba4eb4fc", size = 1306279, upload-time = "2025-09-08T23:08:03.807Z" }, - { url = "https://files.pythonhosted.org/packages/e8/5e/c3c49fdd0f535ef45eefcc16934648e9e59dace4a37ee88fc53f6cd8e641/pyzmq-27.1.0-cp312-abi3-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:1c179799b118e554b66da67d88ed66cd37a169f1f23b5d9f0a231b4e8d44a113", size = 895645, upload-time = "2025-09-08T23:08:05.301Z" }, - { url = "https://files.pythonhosted.org/packages/f8/e5/b0b2504cb4e903a74dcf1ebae157f9e20ebb6ea76095f6cfffea28c42ecd/pyzmq-27.1.0-cp312-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3837439b7f99e60312f0c926a6ad437b067356dc2bc2ec96eb395fd0fe804233", size = 652574, upload-time = "2025-09-08T23:08:06.828Z" }, - { url = "https://files.pythonhosted.org/packages/f8/9b/c108cdb55560eaf253f0cbdb61b29971e9fb34d9c3499b0e96e4e60ed8a5/pyzmq-27.1.0-cp312-abi3-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:43ad9a73e3da1fab5b0e7e13402f0b2fb934ae1c876c51d0afff0e7c052eca31", size = 840995, upload-time = "2025-09-08T23:08:08.396Z" }, - { url = "https://files.pythonhosted.org/packages/c2/bb/b79798ca177b9eb0825b4c9998c6af8cd2a7f15a6a1a4272c1d1a21d382f/pyzmq-27.1.0-cp312-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:0de3028d69d4cdc475bfe47a6128eb38d8bc0e8f4d69646adfbcd840facbac28", size = 1642070, upload-time = "2025-09-08T23:08:09.989Z" }, - { url = "https://files.pythonhosted.org/packages/9c/80/2df2e7977c4ede24c79ae39dcef3899bfc5f34d1ca7a5b24f182c9b7a9ca/pyzmq-27.1.0-cp312-abi3-musllinux_1_2_i686.whl", hash = "sha256:cf44a7763aea9298c0aa7dbf859f87ed7012de8bda0f3977b6fb1d96745df856", size = 2021121, upload-time = "2025-09-08T23:08:11.907Z" }, - { url = "https://files.pythonhosted.org/packages/46/bd/2d45ad24f5f5ae7e8d01525eb76786fa7557136555cac7d929880519e33a/pyzmq-27.1.0-cp312-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:f30f395a9e6fbca195400ce833c731e7b64c3919aa481af4d88c3759e0cb7496", size = 1878550, upload-time = "2025-09-08T23:08:13.513Z" }, - { url = "https://files.pythonhosted.org/packages/e6/2f/104c0a3c778d7c2ab8190e9db4f62f0b6957b53c9d87db77c284b69f33ea/pyzmq-27.1.0-cp312-abi3-win32.whl", hash = "sha256:250e5436a4ba13885494412b3da5d518cd0d3a278a1ae640e113c073a5f88edd", size = 559184, upload-time = "2025-09-08T23:08:15.163Z" }, - { url = "https://files.pythonhosted.org/packages/fc/7f/a21b20d577e4100c6a41795842028235998a643b1ad406a6d4163ea8f53e/pyzmq-27.1.0-cp312-abi3-win_amd64.whl", hash = "sha256:9ce490cf1d2ca2ad84733aa1d69ce6855372cb5ce9223802450c9b2a7cba0ccf", size = 619480, upload-time = "2025-09-08T23:08:17.192Z" }, - { url = "https://files.pythonhosted.org/packages/78/c2/c012beae5f76b72f007a9e91ee9401cb88c51d0f83c6257a03e785c81cc2/pyzmq-27.1.0-cp312-abi3-win_arm64.whl", hash = "sha256:75a2f36223f0d535a0c919e23615fc85a1e23b71f40c7eb43d7b1dedb4d8f15f", size = 552993, upload-time = "2025-09-08T23:08:18.926Z" }, - { url = "https://files.pythonhosted.org/packages/60/cb/84a13459c51da6cec1b7b1dc1a47e6db6da50b77ad7fd9c145842750a011/pyzmq-27.1.0-cp313-cp313-android_24_arm64_v8a.whl", hash = "sha256:93ad4b0855a664229559e45c8d23797ceac03183c7b6f5b4428152a6b06684a5", size = 1122436, upload-time = "2025-09-08T23:08:20.801Z" }, - { url = "https://files.pythonhosted.org/packages/dc/b6/94414759a69a26c3dd674570a81813c46a078767d931a6c70ad29fc585cb/pyzmq-27.1.0-cp313-cp313-android_24_x86_64.whl", hash = "sha256:fbb4f2400bfda24f12f009cba62ad5734148569ff4949b1b6ec3b519444342e6", size = 1156301, upload-time = "2025-09-08T23:08:22.47Z" }, - { url = "https://files.pythonhosted.org/packages/a5/ad/15906493fd40c316377fd8a8f6b1f93104f97a752667763c9b9c1b71d42d/pyzmq-27.1.0-cp313-cp313t-macosx_10_15_universal2.whl", hash = "sha256:e343d067f7b151cfe4eb3bb796a7752c9d369eed007b91231e817071d2c2fec7", size = 1341197, upload-time = "2025-09-08T23:08:24.286Z" }, - { url = "https://files.pythonhosted.org/packages/14/1d/d343f3ce13db53a54cb8946594e567410b2125394dafcc0268d8dda027e0/pyzmq-27.1.0-cp313-cp313t-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:08363b2011dec81c354d694bdecaef4770e0ae96b9afea70b3f47b973655cc05", size = 897275, upload-time = "2025-09-08T23:08:26.063Z" }, - { url = "https://files.pythonhosted.org/packages/69/2d/d83dd6d7ca929a2fc67d2c3005415cdf322af7751d773524809f9e585129/pyzmq-27.1.0-cp313-cp313t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:d54530c8c8b5b8ddb3318f481297441af102517602b569146185fa10b63f4fa9", size = 660469, upload-time = "2025-09-08T23:08:27.623Z" }, - { url = "https://files.pythonhosted.org/packages/3e/cd/9822a7af117f4bc0f1952dbe9ef8358eb50a24928efd5edf54210b850259/pyzmq-27.1.0-cp313-cp313t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6f3afa12c392f0a44a2414056d730eebc33ec0926aae92b5ad5cf26ebb6cc128", size = 847961, upload-time = "2025-09-08T23:08:29.672Z" }, - { url = "https://files.pythonhosted.org/packages/9a/12/f003e824a19ed73be15542f172fd0ec4ad0b60cf37436652c93b9df7c585/pyzmq-27.1.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:c65047adafe573ff023b3187bb93faa583151627bc9c51fc4fb2c561ed689d39", size = 1650282, upload-time = "2025-09-08T23:08:31.349Z" }, - { url = "https://files.pythonhosted.org/packages/d5/4a/e82d788ed58e9a23995cee70dbc20c9aded3d13a92d30d57ec2291f1e8a3/pyzmq-27.1.0-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:90e6e9441c946a8b0a667356f7078d96411391a3b8f80980315455574177ec97", size = 2024468, upload-time = "2025-09-08T23:08:33.543Z" }, - { url = "https://files.pythonhosted.org/packages/d9/94/2da0a60841f757481e402b34bf4c8bf57fa54a5466b965de791b1e6f747d/pyzmq-27.1.0-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:add071b2d25f84e8189aaf0882d39a285b42fa3853016ebab234a5e78c7a43db", size = 1885394, upload-time = "2025-09-08T23:08:35.51Z" }, - { url = "https://files.pythonhosted.org/packages/4f/6f/55c10e2e49ad52d080dc24e37adb215e5b0d64990b57598abc2e3f01725b/pyzmq-27.1.0-cp313-cp313t-win32.whl", hash = "sha256:7ccc0700cfdf7bd487bea8d850ec38f204478681ea02a582a8da8171b7f90a1c", size = 574964, upload-time = "2025-09-08T23:08:37.178Z" }, - { url = "https://files.pythonhosted.org/packages/87/4d/2534970ba63dd7c522d8ca80fb92777f362c0f321900667c615e2067cb29/pyzmq-27.1.0-cp313-cp313t-win_amd64.whl", hash = "sha256:8085a9fba668216b9b4323be338ee5437a235fe275b9d1610e422ccc279733e2", size = 641029, upload-time = "2025-09-08T23:08:40.595Z" }, - { url = "https://files.pythonhosted.org/packages/f6/fa/f8aea7a28b0641f31d40dea42d7ef003fded31e184ef47db696bc74cd610/pyzmq-27.1.0-cp313-cp313t-win_arm64.whl", hash = "sha256:6bb54ca21bcfe361e445256c15eedf083f153811c37be87e0514934d6913061e", size = 561541, upload-time = "2025-09-08T23:08:42.668Z" }, - { url = "https://files.pythonhosted.org/packages/87/45/19efbb3000956e82d0331bafca5d9ac19ea2857722fa2caacefb6042f39d/pyzmq-27.1.0-cp314-cp314t-macosx_10_15_universal2.whl", hash = "sha256:ce980af330231615756acd5154f29813d553ea555485ae712c491cd483df6b7a", size = 1341197, upload-time = "2025-09-08T23:08:44.973Z" }, - { url = "https://files.pythonhosted.org/packages/48/43/d72ccdbf0d73d1343936296665826350cb1e825f92f2db9db3e61c2162a2/pyzmq-27.1.0-cp314-cp314t-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:1779be8c549e54a1c38f805e56d2a2e5c009d26de10921d7d51cfd1c8d4632ea", size = 897175, upload-time = "2025-09-08T23:08:46.601Z" }, - { url = "https://files.pythonhosted.org/packages/2f/2e/a483f73a10b65a9ef0161e817321d39a770b2acf8bcf3004a28d90d14a94/pyzmq-27.1.0-cp314-cp314t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7200bb0f03345515df50d99d3db206a0a6bee1955fbb8c453c76f5bf0e08fb96", size = 660427, upload-time = "2025-09-08T23:08:48.187Z" }, - { url = "https://files.pythonhosted.org/packages/f5/d2/5f36552c2d3e5685abe60dfa56f91169f7a2d99bbaf67c5271022ab40863/pyzmq-27.1.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:01c0e07d558b06a60773744ea6251f769cd79a41a97d11b8bf4ab8f034b0424d", size = 847929, upload-time = "2025-09-08T23:08:49.76Z" }, - { url = "https://files.pythonhosted.org/packages/c4/2a/404b331f2b7bf3198e9945f75c4c521f0c6a3a23b51f7a4a401b94a13833/pyzmq-27.1.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:80d834abee71f65253c91540445d37c4c561e293ba6e741b992f20a105d69146", size = 1650193, upload-time = "2025-09-08T23:08:51.7Z" }, - { url = "https://files.pythonhosted.org/packages/1c/0b/f4107e33f62a5acf60e3ded67ed33d79b4ce18de432625ce2fc5093d6388/pyzmq-27.1.0-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:544b4e3b7198dde4a62b8ff6685e9802a9a1ebf47e77478a5eb88eca2a82f2fd", size = 2024388, upload-time = "2025-09-08T23:08:53.393Z" }, - { url = "https://files.pythonhosted.org/packages/0d/01/add31fe76512642fd6e40e3a3bd21f4b47e242c8ba33efb6809e37076d9b/pyzmq-27.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:cedc4c68178e59a4046f97eca31b148ddcf51e88677de1ef4e78cf06c5376c9a", size = 1885316, upload-time = "2025-09-08T23:08:55.702Z" }, - { url = "https://files.pythonhosted.org/packages/c4/59/a5f38970f9bf07cee96128de79590bb354917914a9be11272cfc7ff26af0/pyzmq-27.1.0-cp314-cp314t-win32.whl", hash = "sha256:1f0b2a577fd770aa6f053211a55d1c47901f4d537389a034c690291485e5fe92", size = 587472, upload-time = "2025-09-08T23:08:58.18Z" }, - { url = "https://files.pythonhosted.org/packages/70/d8/78b1bad170f93fcf5e3536e70e8fadac55030002275c9a29e8f5719185de/pyzmq-27.1.0-cp314-cp314t-win_amd64.whl", hash = "sha256:19c9468ae0437f8074af379e986c5d3d7d7bfe033506af442e8c879732bedbe0", size = 661401, upload-time = "2025-09-08T23:08:59.802Z" }, - { url = "https://files.pythonhosted.org/packages/81/d6/4bfbb40c9a0b42fc53c7cf442f6385db70b40f74a783130c5d0a5aa62228/pyzmq-27.1.0-cp314-cp314t-win_arm64.whl", hash = "sha256:dc5dbf68a7857b59473f7df42650c621d7e8923fb03fa74a526890f4d33cc4d7", size = 575170, upload-time = "2025-09-08T23:09:01.418Z" }, - { url = "https://files.pythonhosted.org/packages/38/f8/946ecde123eaffe933ecf287186495d5f22a8bf444bcb774d9c83dcb2fa5/pyzmq-27.1.0-cp38-cp38-macosx_10_15_universal2.whl", hash = "sha256:18339186c0ed0ce5835f2656cdfb32203125917711af64da64dbaa3d949e5a1b", size = 1332188, upload-time = "2025-09-08T23:09:03.639Z" }, - { url = "https://files.pythonhosted.org/packages/56/08/5960fd162bf1e0e22f251c2f7744101241bc419fbc52abab4108260eb3e0/pyzmq-27.1.0-cp38-cp38-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:753d56fba8f70962cd8295fb3edb40b9b16deaa882dd2b5a3a2039f9ff7625aa", size = 907319, upload-time = "2025-09-08T23:09:06.079Z" }, - { url = "https://files.pythonhosted.org/packages/7f/62/2d8712aafbd7fcf0e303d67c1d923f64a41aa872f1348e3d5dcec147c909/pyzmq-27.1.0-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b721c05d932e5ad9ff9344f708c96b9e1a485418c6618d765fca95d4daacfbef", size = 864213, upload-time = "2025-09-08T23:09:07.985Z" }, - { url = "https://files.pythonhosted.org/packages/e1/04/e9a1550d2dcb29cd662d88c89e9fe975393dd577e2c8b2c528d0a0bacfac/pyzmq-27.1.0-cp38-cp38-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7be883ff3d722e6085ee3f4afc057a50f7f2e0c72d289fd54df5706b4e3d3a50", size = 668520, upload-time = "2025-09-08T23:09:10.317Z" }, - { url = "https://files.pythonhosted.org/packages/48/ad/1638518b7554686d17b5fdd0c0381c13656fe4899dc13af0ba10850d56f0/pyzmq-27.1.0-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:b2e592db3a93128daf567de9650a2f3859017b3f7a66bc4ed6e4779d6034976f", size = 1657582, upload-time = "2025-09-08T23:09:12.256Z" }, - { url = "https://files.pythonhosted.org/packages/cc/b7/6cb8123ee217c1efa8e917feabe86425185a7b55504af32bffa057dcd91d/pyzmq-27.1.0-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:ad68808a61cbfbbae7ba26d6233f2a4aa3b221de379ce9ee468aa7a83b9c36b0", size = 2035054, upload-time = "2025-09-08T23:09:14.175Z" }, - { url = "https://files.pythonhosted.org/packages/cb/95/8d6ec87b43e1d8608be461165180fec4744da9edceea4ce48c7bd8c60402/pyzmq-27.1.0-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:e2687c2d230e8d8584fbea433c24382edfeda0c60627aca3446aa5e58d5d1831", size = 1894186, upload-time = "2025-09-08T23:09:15.797Z" }, - { url = "https://files.pythonhosted.org/packages/a7/2a/7806479dd1f1b964d0aa07f1d961fcaa8673ed543c911847fc45e91f103a/pyzmq-27.1.0-cp38-cp38-win32.whl", hash = "sha256:a1aa0ee920fb3825d6c825ae3f6c508403b905b698b6460408ebd5bb04bbb312", size = 567508, upload-time = "2025-09-08T23:09:17.514Z" }, - { url = "https://files.pythonhosted.org/packages/9f/24/70e83d3ff64ef7e3d6666bd30a241be695dad0ef30d5519bf9c5ff174786/pyzmq-27.1.0-cp38-cp38-win_amd64.whl", hash = "sha256:df7cd397ece96cf20a76fae705d40efbab217d217897a5053267cd88a700c266", size = 632740, upload-time = "2025-09-08T23:09:19.352Z" }, - { url = "https://files.pythonhosted.org/packages/ac/4e/782eb6df91b6a9d9afa96c2dcfc5cac62562a68eb62a02210101f886014d/pyzmq-27.1.0-cp39-cp39-macosx_10_15_universal2.whl", hash = "sha256:96c71c32fff75957db6ae33cd961439f386505c6e6b377370af9b24a1ef9eafb", size = 1330426, upload-time = "2025-09-08T23:09:21.03Z" }, - { url = "https://files.pythonhosted.org/packages/8d/ca/2b8693d06b1db4e0c084871e4c9d7842b561d0a6ff9d780640f5e3e9eb55/pyzmq-27.1.0-cp39-cp39-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:49d3980544447f6bd2968b6ac913ab963a49dcaa2d4a2990041f16057b04c429", size = 906559, upload-time = "2025-09-08T23:09:22.983Z" }, - { url = "https://files.pythonhosted.org/packages/6a/b3/b99b39e2cfdcebd512959780e4d299447fd7f46010b1d88d63324e2481ec/pyzmq-27.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:849ca054d81aa1c175c49484afaaa5db0622092b5eccb2055f9f3bb8f703782d", size = 863816, upload-time = "2025-09-08T23:09:24.556Z" }, - { url = "https://files.pythonhosted.org/packages/61/b2/018fa8e8eefb34a625b1a45e2effcbc9885645b22cdd0a68283f758351e7/pyzmq-27.1.0-cp39-cp39-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3970778e74cb7f85934d2b926b9900e92bfe597e62267d7499acc39c9c28e345", size = 666735, upload-time = "2025-09-08T23:09:26.297Z" }, - { url = "https://files.pythonhosted.org/packages/01/05/8ae778f7cd7c94030731ae2305e6a38f3a333b6825f56c0c03f2134ccf1b/pyzmq-27.1.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:da96ecdcf7d3919c3be2de91a8c513c186f6762aa6cf7c01087ed74fad7f0968", size = 1655425, upload-time = "2025-09-08T23:09:28.172Z" }, - { url = "https://files.pythonhosted.org/packages/ad/ad/d69478a97a3f3142f9dbbbd9daa4fcf42541913a85567c36d4cfc19b2218/pyzmq-27.1.0-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:9541c444cfe1b1c0156c5c86ece2bb926c7079a18e7b47b0b1b3b1b875e5d098", size = 2033729, upload-time = "2025-09-08T23:09:30.097Z" }, - { url = "https://files.pythonhosted.org/packages/9a/6d/e3c6ad05bc1cddd25094e66cc15ae8924e15c67e231e93ed2955c401007e/pyzmq-27.1.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:e30a74a39b93e2e1591b58eb1acef4902be27c957a8720b0e368f579b82dc22f", size = 1891803, upload-time = "2025-09-08T23:09:31.875Z" }, - { url = "https://files.pythonhosted.org/packages/7f/a7/97e8be0daaca157511563160b67a13d4fe76b195e3fa6873cb554ad46be3/pyzmq-27.1.0-cp39-cp39-win32.whl", hash = "sha256:b1267823d72d1e40701dcba7edc45fd17f71be1285557b7fe668887150a14b78", size = 567627, upload-time = "2025-09-08T23:09:33.98Z" }, - { url = "https://files.pythonhosted.org/packages/5c/91/70bbf3a7c5b04c904261ef5ba224d8a76315f6c23454251bf5f55573a8a1/pyzmq-27.1.0-cp39-cp39-win_amd64.whl", hash = "sha256:0c996ded912812a2fcd7ab6574f4ad3edc27cb6510349431e4930d4196ade7db", size = 632315, upload-time = "2025-09-08T23:09:36.097Z" }, - { url = "https://files.pythonhosted.org/packages/cc/b5/a4173a83c7fd37f6bdb5a800ea338bc25603284e9ef8681377cec006ede4/pyzmq-27.1.0-cp39-cp39-win_arm64.whl", hash = "sha256:346e9ba4198177a07e7706050f35d733e08c1c1f8ceacd5eb6389d653579ffbc", size = 559833, upload-time = "2025-09-08T23:09:38.183Z" }, - { url = "https://files.pythonhosted.org/packages/f3/81/a65e71c1552f74dec9dff91d95bafb6e0d33338a8dfefbc88aa562a20c92/pyzmq-27.1.0-pp310-pypy310_pp73-macosx_10_15_x86_64.whl", hash = "sha256:c17e03cbc9312bee223864f1a2b13a99522e0dc9f7c5df0177cd45210ac286e6", size = 836266, upload-time = "2025-09-08T23:09:40.048Z" }, - { url = "https://files.pythonhosted.org/packages/58/ed/0202ca350f4f2b69faa95c6d931e3c05c3a397c184cacb84cb4f8f42f287/pyzmq-27.1.0-pp310-pypy310_pp73-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:f328d01128373cb6763823b2b4e7f73bdf767834268c565151eacb3b7a392f90", size = 800206, upload-time = "2025-09-08T23:09:41.902Z" }, - { url = "https://files.pythonhosted.org/packages/47/42/1ff831fa87fe8f0a840ddb399054ca0009605d820e2b44ea43114f5459f4/pyzmq-27.1.0-pp310-pypy310_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9c1790386614232e1b3a40a958454bdd42c6d1811837b15ddbb052a032a43f62", size = 567747, upload-time = "2025-09-08T23:09:43.741Z" }, - { url = "https://files.pythonhosted.org/packages/d1/db/5c4d6807434751e3f21231bee98109aa57b9b9b55e058e450d0aef59b70f/pyzmq-27.1.0-pp310-pypy310_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:448f9cb54eb0cee4732b46584f2710c8bc178b0e5371d9e4fc8125201e413a74", size = 747371, upload-time = "2025-09-08T23:09:45.575Z" }, - { url = "https://files.pythonhosted.org/packages/26/af/78ce193dbf03567eb8c0dc30e3df2b9e56f12a670bf7eb20f9fb532c7e8a/pyzmq-27.1.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:05b12f2d32112bf8c95ef2e74ec4f1d4beb01f8b5e703b38537f8849f92cb9ba", size = 544862, upload-time = "2025-09-08T23:09:47.448Z" }, - { url = "https://files.pythonhosted.org/packages/4c/c6/c4dcdecdbaa70969ee1fdced6d7b8f60cfabe64d25361f27ac4665a70620/pyzmq-27.1.0-pp311-pypy311_pp73-macosx_10_15_x86_64.whl", hash = "sha256:18770c8d3563715387139060d37859c02ce40718d1faf299abddcdcc6a649066", size = 836265, upload-time = "2025-09-08T23:09:49.376Z" }, - { url = "https://files.pythonhosted.org/packages/3e/79/f38c92eeaeb03a2ccc2ba9866f0439593bb08c5e3b714ac1d553e5c96e25/pyzmq-27.1.0-pp311-pypy311_pp73-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:ac25465d42f92e990f8d8b0546b01c391ad431c3bf447683fdc40565941d0604", size = 800208, upload-time = "2025-09-08T23:09:51.073Z" }, - { url = "https://files.pythonhosted.org/packages/49/0e/3f0d0d335c6b3abb9b7b723776d0b21fa7f3a6c819a0db6097059aada160/pyzmq-27.1.0-pp311-pypy311_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:53b40f8ae006f2734ee7608d59ed661419f087521edbfc2149c3932e9c14808c", size = 567747, upload-time = "2025-09-08T23:09:52.698Z" }, - { url = "https://files.pythonhosted.org/packages/a1/cf/f2b3784d536250ffd4be70e049f3b60981235d70c6e8ce7e3ef21e1adb25/pyzmq-27.1.0-pp311-pypy311_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f605d884e7c8be8fe1aa94e0a783bf3f591b84c24e4bc4f3e7564c82ac25e271", size = 747371, upload-time = "2025-09-08T23:09:54.563Z" }, - { url = "https://files.pythonhosted.org/packages/01/1b/5dbe84eefc86f48473947e2f41711aded97eecef1231f4558f1f02713c12/pyzmq-27.1.0-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:c9f7f6e13dff2e44a6afeaf2cf54cee5929ad64afaf4d40b50f93c58fc687355", size = 544862, upload-time = "2025-09-08T23:09:56.509Z" }, - { url = "https://files.pythonhosted.org/packages/eb/d8/2cf36ee6d037b52640997bde488d046db55bdea05e34229cf9cd3154fd7d/pyzmq-27.1.0-pp38-pypy38_pp73-macosx_10_15_x86_64.whl", hash = "sha256:50081a4e98472ba9f5a02850014b4c9b629da6710f8f14f3b15897c666a28f1b", size = 836250, upload-time = "2025-09-08T23:09:58.313Z" }, - { url = "https://files.pythonhosted.org/packages/e5/40/5ff9acff898558fb54731d4b897d5bf16b3725e0c1778166ac9a234b5297/pyzmq-27.1.0-pp38-pypy38_pp73-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:510869f9df36ab97f89f4cff9d002a89ac554c7ac9cadd87d444aa4cf66abd27", size = 800201, upload-time = "2025-09-08T23:10:00.131Z" }, - { url = "https://files.pythonhosted.org/packages/2f/58/f941950f64c5e7919c64d36e52991ade7ac8ea4805e9d2cdba47337d9edc/pyzmq-27.1.0-pp38-pypy38_pp73-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:1f8426a01b1c4098a750973c37131cf585f61c7911d735f729935a0c701b68d3", size = 758755, upload-time = "2025-09-08T23:10:01.896Z" }, - { url = "https://files.pythonhosted.org/packages/7b/26/ddd3502658bf85d41ab6d75dcab78a7af5bb32fb5f7ac38bd7cf1bce321d/pyzmq-27.1.0-pp38-pypy38_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:726b6a502f2e34c6d2ada5e702929586d3ac948a4dbbb7fed9854ec8c0466027", size = 567742, upload-time = "2025-09-08T23:10:03.732Z" }, - { url = "https://files.pythonhosted.org/packages/36/ad/50515db14fb3c19d48a2a05716c7f4d658da51ea2b145c67f003b3f443d2/pyzmq-27.1.0-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:bd67e7c8f4654bef471c0b1ca6614af0b5202a790723a58b79d9584dc8022a78", size = 544859, upload-time = "2025-09-08T23:10:05.491Z" }, - { url = "https://files.pythonhosted.org/packages/57/f4/c2e978cf6b833708bad7d6396c3a20c19750585a1775af3ff13c435e1912/pyzmq-27.1.0-pp39-pypy39_pp73-macosx_10_15_x86_64.whl", hash = "sha256:722ea791aa233ac0a819fc2c475e1292c76930b31f1d828cb61073e2fe5e208f", size = 836257, upload-time = "2025-09-08T23:10:07.635Z" }, - { url = "https://files.pythonhosted.org/packages/5f/5f/4e10c7f57a4c92ab0fbb2396297aa8d618e6f5b9b8f8e9756d56f3e6fc52/pyzmq-27.1.0-pp39-pypy39_pp73-manylinux2014_i686.manylinux_2_17_i686.whl", hash = "sha256:01f9437501886d3a1dd4b02ef59fb8cc384fa718ce066d52f175ee49dd5b7ed8", size = 800203, upload-time = "2025-09-08T23:10:09.436Z" }, - { url = "https://files.pythonhosted.org/packages/19/72/a74a007cd636f903448c6ab66628104b1fc5f2ba018733d5eabb94a0a6fb/pyzmq-27.1.0-pp39-pypy39_pp73-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:4a19387a3dddcc762bfd2f570d14e2395b2c9701329b266f83dd87a2b3cbd381", size = 758756, upload-time = "2025-09-08T23:10:11.733Z" }, - { url = "https://files.pythonhosted.org/packages/a9/d4/30c25b91f2b4786026372f5ef454134d7f576fcf4ac58539ad7dd5de4762/pyzmq-27.1.0-pp39-pypy39_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4c618fbcd069e3a29dcd221739cacde52edcc681f041907867e0f5cc7e85f172", size = 567742, upload-time = "2025-09-08T23:10:14.732Z" }, - { url = "https://files.pythonhosted.org/packages/92/aa/ee86edad943438cd0316964020c4b6d09854414f9f945f8e289ea6fcc019/pyzmq-27.1.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:ff8d114d14ac671d88c89b9224c63d6c4e5a613fe8acd5594ce53d752a3aafe9", size = 544857, upload-time = "2025-09-08T23:10:16.431Z" }, -] - -[[package]] -name = "redis" -version = "6.1.1" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version < '3.9'", -] -dependencies = [ - { name = "async-timeout", marker = "python_full_version < '3.9'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/07/8b/14ef373ffe71c0d2fde93c204eab78472ea13c021d9aee63b0e11bd65896/redis-6.1.1.tar.gz", hash = "sha256:88c689325b5b41cedcbdbdfd4d937ea86cf6dab2222a83e86d8a466e4b3d2600", size = 4629515, upload-time = "2025-06-02T11:44:04.137Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/c2/cd/29503c609186104c363ef1f38d6e752e7d91ef387fc90aa165e96d69f446/redis-6.1.1-py3-none-any.whl", hash = "sha256:ed44d53d065bbe04ac6d76864e331cfe5c5353f86f6deccc095f8794fd15bb2e", size = 273930, upload-time = "2025-06-02T11:44:02.705Z" }, -] - -[[package]] -name = "redis" -version = "7.0.1" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version == '3.9.*'", -] -dependencies = [ - { name = "async-timeout", marker = "python_full_version == '3.9.*'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/57/8f/f125feec0b958e8d22c8f0b492b30b1991d9499a4315dfde466cf4289edc/redis-7.0.1.tar.gz", hash = "sha256:c949df947dca995dc68fdf5a7863950bf6df24f8d6022394585acc98e81624f1", size = 4755322, upload-time = "2025-10-27T14:34:00.33Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/e9/97/9f22a33c475cda519f20aba6babb340fb2f2254a02fb947816960d1e669a/redis-7.0.1-py3-none-any.whl", hash = "sha256:4977af3c7d67f8f0eb8b6fec0dafc9605db9343142f634041fb0235f67c0588a", size = 339938, upload-time = "2025-10-27T14:33:58.553Z" }, -] - -[[package]] -name = "redis" -version = "8.0.1" -source = { registry = "https://pypi.org/simple" } -resolution-markers = [ - "python_full_version >= '3.10'", -] -dependencies = [ - { name = "async-timeout", marker = "python_full_version >= '3.10' and python_full_version < '3.11.3'" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/cc/c3/928b290c2c0ca99ab96eea5b4ff8f30be8112b075301a7d3ba214a3c8c12/redis-8.0.1.tar.gz", hash = "sha256:afc5a7a2f5a084f5b1880dec548dd45be17db7e43c82a30d84f952aefb05cfb0", size = 5114170, upload-time = "2026-06-23T14:52:37.728Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/fd/0a/c2345ebf1ebe70840ce3f6c6ee612f8fa749cfbd1b03069c53bf0c62aaad/redis-8.0.1-py3-none-any.whl", hash = "sha256:47daa35a058c23468d6437f17a8c76882cb316b838ef763036af99b96cedd743", size = 502406, upload-time = "2026-06-23T14:52:36.137Z" }, -] - -[[package]] -name = "xtquant-big-convert" -version = "0.1.0" -source = { editable = "." } -dependencies = [ - { name = "pyzmq" }, -] - -[package.optional-dependencies] -redis = [ - { name = "redis", version = "6.1.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9'" }, - { name = "redis", version = "7.0.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version == '3.9.*'" }, - { name = "redis", version = "8.0.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.10'" }, -] - -[package.metadata] -requires-dist = [ - { name = "pyzmq", specifier = ">=25.0.0" }, - { name = "redis", marker = "extra == 'redis'", specifier = ">=5.0.0" }, -] -provides-extras = ["redis", "dev"]