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

如何在 JoinQuant 策略中为科创板股票设置市价单保护价?

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

问题描述

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

Title: joinquant 科创板交易教程:MarketOrderStyle 保护价机制

Question: 如何在 joinquant 策略中为科创板股票设置市价单保护价?

解决方案

在 JoinQuant(聚宽)量化交易平台上,交易科创板(688开头的股票)市价单时,根据交易所规则,必须指定保护价(Limit Price),否则订单可能会创建失败或引发非预期的委托风险。


一、保护价(MarketOrderStyle)的运行逻辑

通过 MarketOrderStyle(limit_price) 可以设置科创板市价单的保护价:

  1. 买入时:成交最高价格不得高于指定的保护价。若市场最优一档的价格高于该保护价,剩余未成交部分将自动撤单。
  2. 卖出时:成交最低价格不得低于指定的保护价。若市场最优一档的价格低于该保护价,剩余未成交部分将自动撤单。

二、核心 API 与参数说明

下单函数定义

order(security, amount, style=MarketOrderStyle(limit_price))
  • security: 股票代码,如科创板股票 '688001.XSHG'
  • amount: 交易数量,正数为买入,负数为卖出。
  • style: 订单类型,使用 MarketOrderStyle(limit_price) 指定保护价。

三、完整 Python 策略示例代码

以下示例展示了如何在聚宽策略中初始化并对科创板股票使用含保护价的市价单进行买卖操作:

# 导入聚宽函数库
import jqdata

def initialize(context):
    # 设定基准
    set_benchmark('000300.XSHG')
    # 开启动态复权模式(真实价格)
    set_option('use_real_price', True)
    # 设置要交易的科创板股票
    g.security = '688001.XSHG'
    # 运行定时任务
    run_daily(market_open, time='every_bar')

def market_open(context):
    security = g.security
    # 获取当前最新价格
    current_data = get_current_data()
    price = current_data[security].last_price
    
    # 检查持仓情况
    position = context.portfolio.positions[security].total_amount
    cash = context.portfolio.available_cash
    
    # 买入逻辑:若无持仓且有现金,以当前价的 102% 作为买入保护价下市价单
    if position == 0 and cash > 0:
        buy_protection_price = round(price * 1.02, 2)
        # 科创板股票买入委托需满足 200 股起
        order_res = order(security, 200, MarketOrderStyle(buy_protection_price))
        if order_res:
            log.info("已发送买入委托:%s,数量:200,买入保护价:%s" % (security, buy_protection_price))
            
    # 卖出逻辑:若有持仓,以当前价的 98% 作为卖出保护价下市价单
    elif position >= 200:
        sell_protection_price = round(price * 0.98, 2)
        order_res = order(security, -200, MarketOrderStyle(sell_protection_price))
        if order_res:
            log.info("已发送卖出委托:%s,数量:200,卖出保护价:%s" % (security, sell_protection_price))

四、注意事项

  1. 交易单位:科创板股票单笔申报数量起点为 200 股,超过 200 股的部分可以以 1 股为单位递增。
  2. 限价单替代方案:如果不想使用带有保护价的市价单,也可以使用 LimitOrderStyle(limit_price) 下达标准的限价单。
  3. 失败排查:若下单返回 None,请检查是否未指定 protection_price 保护价,或者指定的保护价触发了涨跌停限制/资金不足等条件。