3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在沪深交易所中,科创板和创业板引入了盘后固定价格交易机制。投资者可以在交易日的盘后阶段,以当日收盘价进行股票买卖。PTrade 平台提供了 after_trading_order 接口,专门用于盘后固定价委托申报。
after_trading_orderafter_trading_order(security, amount, entrust_price)
| 参数名 | 类型 | 说明 |
|---|---|---|
security |
str | 股票代码(如 '300001.SZ' 或 '688001.SS') |
amount |
int | 交易数量,正数表示买入,负数表示卖出 |
entrust_price |
float | 委托价格(必填,通常填入当日收盘价/最新价) |
order_id(str),失败则返回 None。run_daily 设定在 15:00 之后触发定时执行盘后委托处理函数。after_trading_cancel_order(order_param)。以下示例展示了如何在 15:15 触发盘后固定价委托买入创业板股票,并配合挂单撤销的完整逻辑:
import time
def initialize(context):
# 设置目标交易标的(创业板股票)
g.security = "300001.SZ"
set_universe(g.security)
# 设定定时任务:在 15:15 执行盘后固定价下单逻辑
run_daily(context, after_trading_logic, time="15:15")
g.flag = False
def after_trading_logic(context):
if not g.flag:
# 获取最新的行情快照(获取收盘价格)
snapshot = get_snapshot(g.security)
if snapshot and g.security in snapshot:
last_px = snapshot[g.security].get("last_px", 0)
if last_px > 0:
# 发起盘后固定价买入委托(买入 200 股)
order_id = after_trading_order(g.security, 200, float(last_px))
log.info(f"盘后固定价委托已发送,订单ID: {order_id},委托价: {last_px}")
# 如需撤单,可配合 after_trading_cancel_order 进行撤单
# time.sleep(5)
# after_trading_cancel_order(order_id)
g.flag = True
def handle_data(context, data):
pass