🚀 AI 一键生成 ptrade 策略代码

ptrade 策略中 on_trade_response 无法接收撤单成交主推怎么办?

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

问题描述

在on_trade_response 未收到撤单的成交主推。

/

解决方案

在 ptrade 量化交易平台中,如果在 on_trade_responseon_order_response 中未收到撤单产生的主推信息,通常是因为系统默认关闭了撤单主推的接收

原因分析

根据 ptrade API 文档,为了减少不必要的信息推送,策略默认是不接收撤单委托产生的主推的(缺省值为 0)。

解决方案

您需要在策略的初始化函数 initialize 中,使用 set_parameters 接口显式地开启接收撤单主推的功能。具体参数为 receive_cancel_response="1"

Python 代码示例

def initialize(context):
    # 初始化股票池
    g.security = '600570.SS'
    set_universe(g.security)
    
    # 关键设置:开启接收撤单委托产生的主推
    set_parameters(receive_cancel_response="1")
    
    # 可选:如果还需要接收非本策略产生的主推,可以同时开启
    # set_parameters(receive_cancel_response="1", receive_other_response="1")

def on_trade_response(context, trade_list):
    # 此时可以接收到撤单的成交主推
    log.info("收到成交主推:%s" % trade_list)
    for trade in trade_list:
        # 撤单成交的 trans_flag 通常为 1(具体视柜台返回而定),或者可以通过 status 判断
        log.info("委托状态: %s" % trade.get('status'))

def handle_data(context, data):
    pass

注意事项

  1. 参数格式set_parameters 的入参必须是字符串形式,即 "1" 表示开启,"0" 表示关闭。
  2. 非本交易主推:如果您撤销的是手动在客户端下的单(非策略产生的订单),您还需要同时开启 receive_other_response="1" 才能在策略中收到该主推。
  3. 废单与撤单的区别:如果是废单(例如价格超出涨跌停限制),在 on_trade_response 里面的 status 通常是 9(废单),这与主动撤单(status6 已撤 或 5 部撤)是不同的。