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

PTrade 盘口策略:利用 get_gear_price 分析档位买卖量与打板扫单

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

问题描述

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

Title: PTrade 盘口策略:利用 get_gear_price 分析档位买卖量与打板扫单

Question: 如何使用 ptrade 的 get_gear_price 接口获取买卖五档/十档挂单数据并判断盘口封单强度?

解决方案

PTrade 盘口策略:利用 get_gear_price 分析档位买卖量与打板扫单

在高频交易与涨停板打板策略中,对盘口买卖档位(Bid/Offer)数据的实时分析至关重要。PTrade 平台提供了 get_gear_price 接口,用于精准获取标的的实时档位行情(包含价格、委托量及委托笔数),帮助开发者及时捕捉盘口挂单变化与封单强度。


1. get_gear_price 接口详解

接口说明

get_gear_price 仅在交易模块(盘中运行)可用,用于实时查询指定股票或可转债的委买、委卖档位数据。

语法与参数

get_gear_price(sids)
  • sids: 股票代码或代码列表,支持 strlist[str](如 '600570.SS'['600570.SS', '000001.SZ'])。

返回数据结构

返回嵌套字典格式:

{
    'bid_grp': {1: [买一价, 买一量, 买一笔数], 2: [...], ...},
    'offer_grp': {1: [卖一价, 卖一量, 卖一笔数], 2: [...], ...}
}

注:在存在 Level 2 行情时,将返回五档或十档数据及精准委托笔数;若无 L2 行情,委托笔数默认返回 0。


2. 判断涨停板封单强度的逻辑

当股票触及涨停时:

  1. 卖一档(offer_grp[1]):价格与挂单量均为 0([0.0, 0, 0]),表示上方无卖单挂出。
  2. 买一档(bid_grp[1]):挂单价格等于涨停价(up_px),且买一量(total_bid_qty)极高。

封单强度指标通常通过以下公式衡量:
$$\text{封单额} = \text{买一挂单量} \times \text{买一价格}$$
$$\text{封单占比} = \frac{\text{买一挂单量}}{\text{流通股本 (或成交量)}}$$


3. 实战策略示例:涨停打板与盘口监控

以下策略展示了如何在 PTrade 中利用 get_gear_price 结合 order_tickorder 接口进行涨停板盘口监控与自动扫单。

def initialize(context):
    g.security = '600570.SS'
    set_universe(g.security)
    # 订阅 tick 级或秒级定时器运行
    run_interval(context, check_gear_and_trade, seconds=3)

def check_gear_and_trade(context):
    security = g.security
    
    # 获取实时行情快照(获取涨停价)
    snapshot = get_snapshot(security)
    if not snapshot or security not in snapshot:
        return
    
    up_px = snapshot[security].get('up_px', 0)
    last_px = snapshot[security].get('last_px', 0)
    
    # 获取档位行情
    gear = get_gear_price(security)
    if not gear or 'bid_grp' not in gear:
        return
    
    bid_1 = gear['bid_grp'].get(1, [0, 0, 0])
    offer_1 = gear['offer_grp'].get(1, [0, 0, 0])
    
    bid_1_price, bid_1_volume, bid_1_orders = bid_1[0], bid_1[1], bid_1[2]
    offer_1_price, offer_1_volume = offer_1[0], offer_1[1]
    
    # 条件1:买一价等于涨停价,且卖一无挂单(进入涨停状态)
    if bid_1_price == up_px and offer_1_price == 0:
        # 计算封单金额(假设单位:元)
        seal_balance = bid_1_price * bid_1_volume
        log.info(f"{security} 已封涨停!买一封单量: {bid_1_volume} 股, 封单额: {seal_balance:.2f} 元")
        
        # 条件2:如果当前未持仓,且封单金额突破 1000 万元,跟风打板
        position = get_position(security)
        if position.amount == 0 and seal_balance > 10000000:
            log.info("封单强度达标,执行打板买入!")
            order(security, 1000, limit_price=up_px)

def handle_data(context, data):
    pass

4. 开发与注意事项

  1. 限价单精度:股票限价需保留 2 位小数,可转债/ETF保留 3 位小数,避免因精度问题导致报单被拒。
  2. 行情缓存get_gear_price 在盘中频繁调用时,需注意执行效率,建议在 run_intervaltick_data 中使用。
  3. L2 权限:若要分析更深档位(如 6~10 档)及委托笔数,需确保交易账户已开通 Level 2 行情权限。