3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 PTrade 交易模块中,get_gear_price(sids) 函数用于获取指定标的的买卖档位行情数据(包含委买、委卖的价格、委托量及委托笔数)。
get_gear_price(sids)
sids 支持单只或多只股票代码(如 '600570.SS' 或 ['600570.SS', '000001.SZ'])。dict。
bid_grp: 委买档位 (档位1~5或1~10,包含 [价格, 委托量, 委托笔数])。offer_grp: 委卖档位 (档位1~5或1~10,包含 [价格, 委托量, 委托笔数])。当股票触及涨停时:
offer_grp 1档):价格与委托量通常为 0,即无卖单挂出。bid_grp 1档):挂单价格为涨停价,委托量即为当前的涨停封单量。以下示例展示了如何在盘中通过 get_gear_price 实时监控标的盘口深度,并判断买一封单量是否达到设定门槛:
def initialize(context):
g.security = '600570.SS'
set_universe(g.security)
# 设置定时任务,每10秒检测一次盘口
run_interval(context, check_gear_depth, seconds=10)
def check_gear_depth(context):
security = g.security
# 获取五档盘口行情
gear_data = get_gear_price(security)
if not gear_data or 'bid_grp' not in gear_data:
log.warning(f"获取 {security} 盘口行情失败")
return
bid_1 = gear_data['bid_grp'].get(1, [0, 0, 0])
offer_1 = gear_data['offer_grp'].get(1, [0, 0, 0])
bid_price, bid_vol, bid_count = bid_1[0], bid_1[1], bid_1[2]
offer_price, offer_vol, offer_count = offer_1[0], offer_1[1], offer_1[2]
# 判断是否涨停(卖一价格为0且买一有大额封单)
if offer_price == 0 and bid_vol > 0:
log.info(f"{security} 当前处于涨停状态!涨停封单价: {bid_price}, 封单量: {bid_vol} 股, 封单笔数: {bid_count}")
# 示例:若封单量大于100万股,可触发后续逻辑
if bid_vol >= 1000000:
log.info("涨停封单较为稳固")
else:
log.info(f"{security} 未涨停。买一价: {bid_price} (量:{bid_vol}), 卖一价: {offer_price} (量:{offer_vol})")
def handle_data(context, data):
pass
run_interval 或 tick_data 中频繁调用时,注意控制策略计算开销,避免阻塞逻辑。order()),需确保股票价格格式保留两位小数,避免因精度问题导致报单失败。