3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化交易中,当交易大额订单时,为了减小对市场的冲击成本(Market Impact),通常会采用智能算法交易。QMT 平台提供了 smart_algo_passorder 接口,支持 VWAP(成交量加权平均价)、TWAP(时间加权平均价) 等多种主流智能算法。 本文将为您详细解析该接口的参数配置方法,并提供开箱即用的 Python 代码示例。
smart_algo_passorder 专门用于股票、两融等业务的算法拆单交易。其基本调用格式如下:
smart_algo_passorder(opType, orderType, accountid, orderCode, prType, modelprice, volume, strageName, quickTrade, userid, smartAlgoType, limitOverRate, minAmountPerOrder, [targetPriceLevel], ContextInfo)
| 参数名 | 类型 | 说明 | 常用配置值 |
|---|---|---|---|
| opType | int | 操作类型 | 23:股票买入;24:股票卖出;33:信用账号买入;34:信用账号卖出 |
| orderType | int | 下单方式 | 1101:单股、单账号、普通、股/手方式下单 |
| accountid | string | 资金账号 | 您的实盘或模拟资金账号 |
| orderCode | string | 合约代码 | 格式如 '000001.SZ' 或 '600000.SH' |
| prType | int | 下单选价类型 | 5:最新价;11:指定价(模型价);12:涨跌停价 |
| modelprice | float | 下单价格 | 当 prType 为 11 时有效,其他情况可填 -1 |
| volume | int/float | 下单数量 | 对应 orderType 的单位(如股数) |
| strageName | string | 策略名 | 自定义策略名称,不可缺省 |
| quickTrade | int | 立即触发下单 | 0:否(随K线走完触发);1:是(立即触发) |
| userid | string | 投资备注 | 自定义备注,不可缺省 |
| smartAlgoType | string | 智能算法类型 | 'VWAP' 或 'TWAP' |
| limitOverRate | int | 量比限制 | 数据范围 0-100,控制算法参与市场成交量的比例上限 |
| minAmountPerOrder | int | 最小委托金额 | 数据范围 0-100000,单笔拆单的最小金额限制,默认为 0 |
| startTime | string | 算法开始时间 | (可选) 格式 'HH:MM:SS',如 '09:30:00' |
| endTime | string | 算法截止时间 | (可选) 格式 'HH:MM:SS',如 '14:30:00' |
TWAP 算法将订单均匀地分布在设定的时间段内执行,适合市场成交量分布较为均匀或波动较小的标的。
#coding:gbk
def init(ContextInfo):
# 设定交易账号
ContextInfo.accid = '6000000105'
ContextInfo.set_account(ContextInfo.accid)
def handlebar(ContextInfo):
if not ContextInfo.is_last_bar():
return
# 标的代码:平安银行
stock_code = '000001.SZ'
# 计划买入 50000 股
total_volume = 50000
# 调用 TWAP 智能算法下单
# 参数配置:最新价选价(5),快速交易(1),量比限制20%,最小委托金额0,执行时间 09:30:00 至 14:00:00
smart_algo_passorder(
23, # opType: 股票买入
1101, # orderType: 股/手方式
ContextInfo.accid, # 资金账号
stock_code, # 证券代码
5, # prType: 最新价
-1, # modelprice: 选最新价时填-1
total_volume, # volume: 50000股
"MyTwapStrategy", # strageName: 策略名
1, # quickTrade: 立即触发
"remark_twap", # userid: 投资备注
"TWAP", # smartAlgoType: TWAP算法
20, # limitOverRate: 量比20%
0, # minAmountPerOrder: 最小委托金额0
0, # targetPriceLevel: 冰山算法参数,此处填0
'09:30:00', # startTime: 开始时间
'14:00:00', # endTime: 截止时间
ContextInfo # ContextInfo对象
)
print("TWAP 智能算法下单任务已发送")
VWAP 算法根据历史成交量分布预测,在成交量大的时段多下单,成交量小时段少下单,力求使最终成交均价接近市场成交量加权均价。
#coding:gbk
def init(ContextInfo):
ContextInfo.accid = '6000000105'
ContextInfo.set_account(ContextInfo.accid)
def handlebar(ContextInfo):
if not ContextInfo.is_last_bar():
return
stock_code = '600000.SH' # 浦发银行
total_volume = 100000 # 计划买入 100000 股
# 调用 VWAP 智能算法下单
# 参数配置:最新价选价(5),快速交易(1),量比限制30%,最小委托金额10000元,执行时间 10:00:00 至 14:50:00
smart_algo_passorder(
23,
1101,
ContextInfo.accid,
stock_code,
5,
-1,
total_volume,
"MyVwapStrategy",
1,
"remark_vwap",
"VWAP", # 算法类型设为 VWAP
30, # 量比限制 30%
10000, # 最小委托金额 10000 元
0,
'10:00:00',
'14:50:00',
ContextInfo
)
print("VWAP 智能算法下单任务已发送")
startTime 和 endTime,系统默认的算法执行时间段通常为 '09:30:00' 至 '15:30:00'。get_trade_detail_data(..., 'TASK') 获取任务状态,并使用 pause_task() 或 cancel_task() 对算法任务进行暂停或撤销控制。