🚀 AI 一键生成 qmt 策略代码
立即体验

QMT 平台如何通过 get_market_data_ex 订阅 Level-2 行情快照指标 (l2quoteaux)?

qmt | 发布时间: 2026/7/28
以下内容由 EasyQuant 生成。

问题描述

请根据以下 SEO 主题生成完整页面内容:

Title: QMT Level-2 高频数据:get_market_data_ex 订阅行情快照指标 (l2quoteaux)

Question: 如何通过 ContextInfo.get_market_data_ex 订阅 Level-2 的委买委卖均价与撤单总量?

解决方案

QMT Level-2 高频数据:订阅行情快照指标 (l2quoteaux)

在 QMT(迅投量化交易系统)中,通过开通 Level-2 增强版权限,投资者可以获取更深精度的盘口与高频快照指标。ContextInfo.get_market_data_ex 提供了对 Level-2 行情快照指标(l2quoteaux)的订阅与数据获取能力。


1. l2quoteaux 包含的核心指标

订阅周期参数设为 'l2quoteaux' 时,返回的数据集中包含以下重要快照字段:

  • avgBidPrice: 委买均价
  • avgOffPrice: 委卖均价
  • totalBidQuantity: 委买总量
  • totalOffQuantity: 委卖总量
  • withdrawBidQuantity: 买入撤单总量
  • withdrawBidAmount: 买入撤单总额
  • withdrawOffQuantity: 卖出撤单总量
  • withdrawOffAmount: 卖出撤单总额
  • time / stime: 时间戳 / 格式化时间字符串

2. 代码实现示例

以下示例演示如何在 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())

3. 注意事项

  1. 权限开通: l2quoteaux 数据需要券商端账号额外开通 Level-2 增强版 权限,否则无法接收或推送相关数据。
  2. 数据订阅(subscribe=True: 在盘中实时运行时,确保 subscribe 设置为 True,以便实时接收最新的盘口变化推送。
  3. 数据结构: 返回值为以股票代码为 Key 的字典,字典的值为 pandas.DataFrame,索引(Index)为时间序列,列(Columns)为请求的数据字段。