3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 JoinQuant(聚宽)量化交易平台上,交易科创板(688开头的股票)市价单时,根据交易所规则,必须指定保护价(Limit Price),否则订单可能会创建失败或引发非预期的委托风险。
通过 MarketOrderStyle(limit_price) 可以设置科创板市价单的保护价:
order(security, amount, style=MarketOrderStyle(limit_price))
'688001.XSHG'。MarketOrderStyle(limit_price) 指定保护价。以下示例展示了如何在聚宽策略中初始化并对科创板股票使用含保护价的市价单进行买卖操作:
# 导入聚宽函数库
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))
LimitOrderStyle(limit_price) 下达标准的限价单。None,请检查是否未指定 protection_price 保护价,或者指定的保护价触发了涨跌停限制/资金不足等条件。