3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化交易中,可转债折溢价套利或折价抢筹是非常经典的策略。本文将详细介绍如何在 QMT (迅投量化终端) 中使用 Python API 实时监控可转债转股溢价率,并在满足条件时自动触发快速下单。
ContextInfo.get_full_tick(stock_code):获取转债与正股的最新分笔数据。ContextInfo.get_instrumentdetail(stock_code):获取合约详细信息(用于提取标的特征)。passorder(opType, orderType, accountid, orderCode, prType, price, volume, strategyName, quickTrade, ...):执行买卖下单操作。通过设置 quickTrade=1 实现盘中触发即刻下单。#encoding:gbk
import pandas as pd
def init(ContextInfo):
# 1. 策略基础配置
ContextInfo.accID = '6000000001' # 请替换为实际交易账号
ContextInfo.set_account(ContextInfo.accID)
# 2. 配置可转债与正股及转股价的映射关系字典
# 格式: '转债代码': {'stock': '正股代码', 'convert_price': 当前转股价}
ContextInfo.cb_map = {
'113050.SH': {'stock': '600519.SH', 'convert_price': 1800.00},
'128012.SZ': {'stock': '002415.SZ', 'convert_price': 25.50}
}
# 3. 策略监控阈值:转股溢价率低于 -1.5% 时触发抢筹下单
ContextInfo.threshold = -1.5
# 4. 下单标志位(防止重复下单)
ContextInfo.traded_list = []
def handlebar(ContextInfo):
# 仅在最新 Bar/Tick 驱动时运行
if not ContextInfo.is_last_bar():
return
for cb_code, info in ContextInfo.cb_map.items():
if cb_code in ContextInfo.traded_list:
continue
stock_code = info['stock']
convert_price = info['convert_price']
# 获取转债和正股的最新 Tick
tick_data = ContextInfo.get_full_tick([cb_code, stock_code])
if cb_code not in tick_data or stock_code not in tick_data:
continue
cb_price = tick_data[cb_code]['lastPrice']
stock_price = tick_data[stock_code]['lastPrice']
if cb_price <= 0 or stock_price <= 0 or convert_price <= 0:
continue
# 计算转股价值与转股溢价率
convert_value = (stock_price / convert_price) * 100
premium_rate = ((cb_price / convert_value) - 1) * 100
print(f"[监控] 转债: {cb_code}, 现价: {cb_price}, 正股价格: {stock_price}, 转股价值: {convert_value:.2f}, 溢价率: {premium_rate:.2f}%")
# 判断是否满足折价抢筹条件
if premium_rate <= ContextInfo.threshold:
print(f"[触发信号] 溢价率 {premium_rate:.2f}% 低于阈值 {ContextInfo.threshold}%,执行买入转债!")
# 执行普通股票买入模式(opType=23, orderType=1101, prType=5最新价, quickTrade=1快速交易)
passorder(23, 1101, ContextInfo.accID, cb_code, 5, 0, 10, 'CB_Arbitrage', 1, cb_code, ContextInfo)
# 记录已交易,避免重复下单
ContextInfo.traded_list.append(cb_code)
convert_price 为最新转股价。