3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在高频交易与涨停板打板策略中,对盘口买卖档位(Bid/Offer)数据的实时分析至关重要。PTrade 平台提供了 get_gear_price 接口,用于精准获取标的的实时档位行情(包含价格、委托量及委托笔数),帮助开发者及时捕捉盘口挂单变化与封单强度。
get_gear_price 接口详解get_gear_price 仅在交易模块(盘中运行)可用,用于实时查询指定股票或可转债的委买、委卖档位数据。
get_gear_price(sids)
str 或 list[str](如 '600570.SS' 或 ['600570.SS', '000001.SZ'])。返回嵌套字典格式:
{
'bid_grp': {1: [买一价, 买一量, 买一笔数], 2: [...], ...},
'offer_grp': {1: [卖一价, 卖一量, 卖一笔数], 2: [...], ...}
}
注:在存在 Level 2 行情时,将返回五档或十档数据及精准委托笔数;若无 L2 行情,委托笔数默认返回 0。
当股票触及涨停时:
[0.0, 0, 0]),表示上方无卖单挂出。up_px),且买一量(total_bid_qty)极高。封单强度指标通常通过以下公式衡量:
$$\text{封单额} = \text{买一挂单量} \times \text{买一价格}$$
$$\text{封单占比} = \frac{\text{买一挂单量}}{\text{流通股本 (或成交量)}}$$
以下策略展示了如何在 PTrade 中利用 get_gear_price 结合 order_tick 或 order 接口进行涨停板盘口监控与自动扫单。
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
get_gear_price 在盘中频繁调用时,需注意执行效率,建议在 run_interval 或 tick_data 中使用。