3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT(迅投量化交易终端)中,利用 Level-2 高频行情数据 可以捕获市场买卖双方的真实意图和微观结构变化。通过 get_market_data_ex 或 subscribe_quote 接口订阅 l2quoteaux 周期,开发者能够实时获取委买均价、委卖均价以及撤单量等深度指标。
在 QMT Python API 中,获取 Level-2 行情快照指标需将周期参数设置为 'l2quoteaux'。该周期返回的数据包含以下关键字段:
avgBidPrice: 委买均价totalBidQuantity: 委买总量avgOffPrice: 委卖均价totalOffQuantity: 委卖总量withdrawBidQuantity: 买入撤单总量withdrawBidAmount: 买入撤单总额withdrawOffQuantity: 卖出撤单总量withdrawOffAmount: 卖出撤单总额注:使用 Level-2 高频接口前,需确保账号已开通国信 QMT Level-2 增强版权限并补充了对应的历史行情数据。
以下示例展示了如何在策略的 handlebar 函数中,获取并解析当前主图标的的 Level-2 行情快照数据:
#encoding:gbk
import pandas as pd
def init(ContextInfo):
# 订阅并设置标的
ContextInfo.symbol = '600000.SH'
ContextInfo.set_universe([ContextInfo.symbol])
print("Level-2 行情策略初始化完成")
def handlebar(ContextInfo):
# 仅在最后一根 Bar 上提取最新高频数据
if not ContextInfo.is_last_bar():
return
# 获取 Level-2 盘口扩展指标数据
data_dict = ContextInfo.get_market_data_ex(
fields=[], # 空列表表示获取所有可用字段
stock_code=[ContextInfo.symbol],
period='l2quoteaux', # 设定周期为 Level-2 快照指标
count=1, # 获取最新的 1 条记录
subscribe=True # 自动订阅数据
)
if ContextInfo.symbol in data_dict:
df = data_dict[ContextInfo.symbol]
if not df.empty:
latest = df.iloc[-1]
print(f"=== {ContextInfo.symbol} Level-2 盘口数据 ===")
print(f"时间: {latest.get('stime', 'N/A')}")
print(f"委买均价: {latest.get('avgBidPrice', 0.0):.4f} | 委买总量: {latest.get('totalBidQuantity', 0)}")
print(f"委卖均价: {latest.get('avgOffPrice', 0.0):.4f} | 委卖总量: {latest.get('totalOffQuantity', 0)}")
print(f"买入撤单总量: {latest.get('withdrawBidQuantity', 0)} | 买入撤单金额: {latest.get('withdrawBidAmount', 0.0):.2f}")
print(f"卖出撤单总量: {latest.get('withdrawOffQuantity', 0)} | 卖出撤单金额: {latest.get('withdrawOffAmount', 0.0):.2f}")
l2quoteaux 数据需要券商交易端开通 Level-2 增强版行情权限,否则接口返回空数据或报错。ContextInfo.subscribe_quote 接口使用回调函数(callback)来处理实时推送的高频 Level-2 数据。