3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT(迅投量化交易系统)中,通过开通 Level-2 增强版权限,投资者可以获取更深精度的盘口与高频快照指标。ContextInfo.get_market_data_ex 提供了对 Level-2 行情快照指标(l2quoteaux)的订阅与数据获取能力。
订阅周期参数设为 'l2quoteaux' 时,返回的数据集中包含以下重要快照字段:
avgBidPrice: 委买均价avgOffPrice: 委卖均价totalBidQuantity: 委买总量totalOffQuantity: 委卖总量withdrawBidQuantity: 买入撤单总量withdrawBidAmount: 买入撤单总额withdrawOffQuantity: 卖出撤单总量withdrawOffAmount: 卖出撤单总额time / stime: 时间戳 / 格式化时间字符串以下示例演示如何在 QMT 策略的 handlebar 驱动中,通过 get_market_data_ex 获取指定的 Level-2 快照指标:
#coding:gbk
def init(ContextInfo):
# 设定关注的股票池
ContextInfo.set_universe(['600000.SH'])
def handlebar(ContextInfo):
# 只在最新 K 线(或最新 Tick)时读取数据
if not ContextInfo.is_last_bar():
return
stock_list = ContextInfo.get_universe()
# 订阅并获取 Level-2 行情快照指标
l2_data = ContextInfo.get_market_data_ex(
fields=['avgBidPrice', 'avgOffPrice', 'withdrawBidQuantity', 'withdrawOffQuantity'],
stock_code=stock_list,
period='l2quoteaux',
count=10,
subscribe=True
)
# 解析数据
for code in stock_list:
if code in l2_data:
df = l2_data[code]
print(f"=== 股票代码: {code} Level-2 快照指标 ===")
print(df.tail())
l2quoteaux 数据需要券商端账号额外开通 Level-2 增强版 权限,否则无法接收或推送相关数据。subscribe=True): 在盘中实时运行时,确保 subscribe 设置为 True,以便实时接收最新的盘口变化推送。pandas.DataFrame,索引(Index)为时间序列,列(Columns)为请求的数据字段。