3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化交易中,大单直接申报容易对市场造成较大的冲击成本(Slippage)。QMT 平台提供了强大的智能算法交易接口,其中 PINLINE(跟价算法) 是一种通过紧密跟踪盘口价格变化、动态调整委托价格来争取高执行效率和低滑点的算法。本文将详细解析如何在 QMT 中配置 PINLINE 算法以达到最佳执行效果。
PINLINE(跟价算法)的核心目标是紧跟盘口价格。它会根据设定的基准价格和盘口动态,将大单拆分成小单,并在盘口价格对己方有利或处于特定档位时进行挂单。相比于 TWAP(时间加权)和 VWAP(成交量加权),PINLINE 更加注重价格敏感度,适合在趋势明显或波动剧烈的行情中争取更好的成交价格。
smart_algo_passorder在 QMT Python API 中,调用智能算法需要使用 ContextInfo.smart_algo_passorder() 函数。其基本格式如下:
ContextInfo.smart_algo_passorder(opType, orderType, accountid, orderCode, prType, modelprice, volume, smartAlgoType, limitOverRate, minAmountPerOrder, [targetPriceLevel], startTime, endTime, ContextInfo)
smartAlgoType (智能算法类型): 必须设置为 'PINLINE'。limitOverRate (量比限制):
0-100)。10 到 20。若设置过高(如 >30),拆单委托过大,容易暴露交易意图并引起价格反弹;若设置过低(如 <5),在成交量低迷时可能导致无法按时完成目标仓位。minAmountPerOrder (单笔最小委托金额):
0-100000 元)。10000 到 20000。避免拆单过碎产生过多的无效报撤单,同时符合交易所防范异常交易的监管要求。targetPriceLevel (目标价格档位):
1(己方盘口1)、2(己方盘口2)... 6(最新价)、7(对方盘口)。1(己方一档挂单等待成交)。6(最新价)或 7(对方一档,即买入时用卖一价,卖出时用买一价)。startTime 与 endTime (执行时间窗口):
'09:45:00' 至 '14:45:00'。以下是一个在 QMT 中使用 PINLINE 算法买入平安银行(000001.SZ)的完整策略示例:
#coding:gbk
import time
def init(ContextInfo):
# 设定交易账号
ContextInfo.accid = '6000000248'
ContextInfo.set_account(ContextInfo.accid)
ContextInfo.stock = '000001.SZ'
ContextInfo.set_universe([ContextInfo.stock])
def handlebar(ContextInfo):
# 仅在最后一根 K 线(盘中实时行情)触发算法交易
if not ContextInfo.is_last_bar():
return
# 设定交易参数
opType = 23 # 股票买入
orderType = 1101 # 单股、单账号、普通、股方式下单
volume = 50000 # 目标买入 50,000 股
prType = 5 # 最新价选价
modelprice = -1 # 非指定价,传入 -1
# 智能算法专属参数
smartAlgoType = 'PINLINE' # 跟价算法
limitOverRate = 15 # 量比控制在 15%
minAmountPerOrder = 10000 # 单笔子单不低于 10,000 元
targetPriceLevel = 6 # 跟踪最新价进行动态调整
startTime = '09:45:00'
endTime = '14:30:00'
# 发起智能算法交易任务
ContextInfo.smart_algo_passorder(
opType, orderType, ContextInfo.accid, ContextInfo.stock,
prType, modelprice, volume,
"Pinline_Strategy", 1, "remark_pinline",
smartAlgoType, limitOverRate, minAmountPerOrder, targetPriceLevel,
startTime, endTime, ContextInfo
)
print(f"已提交 PINLINE 算法交易任务:买入 {ContextInfo.stock} {volume} 股")
limitOverRate 严格限制子单在市场总成交中的占比,大单被化整为零,有效避免了“大单压盘”或“大单托盘”引发的跟风盘,从而锁定了更优的建仓均价。limitOverRate(如 5%-10%),拉长执行时间窗口,防止算法自身推高股价。limitOverRate(如 20%),并将 targetPriceLevel 设为对方盘口(7),以牺牲极小的点差为代价换取极高的成交确定性。