3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化交易中,面对大额订单时,直接下单容易对市场造成冲击,增加交易成本。QMT 平台提供了 smart_algo_passorder 接口,支持 TWAP(时间加权平均价格) 和 VWAP(成交量加权平均价格) 等智能算法,通过科学的拆单策略实现无人值守的算法交易。
要配置 TWAP 或 VWAP 算法,需要正确填写 smart_algo_passorder 的参数。其完整调用格式如下:
smart_algo_passorder(opType, orderType, accountid, orderCode, prType, modelprice, volume, strageName, quickTrade, userid, smartAlgoType, limitOverRate, minAmountPerOrder, [targetPriceLevel], [startTime], [endTime], ContextInfo)
"TWAP",配置 VWAP 填 "VWAP"。0 - 100。用于限制算法执行时的成交量占比,防止因交易过激暴露意图。0 - 100000,默认设为 0。"HH:MM:SS"(如 "09:30:00")。"HH:MM:SS"(如 "14:30:00")。23,股票卖出填 24;信用账户买入填 33,卖出填 34。1101,金额下单填 1102。5(最新价)或 11(指定价/模型价)。TWAP 算法将订单均匀地分布在设定的时间段内执行,适合市场成交量分布较为均匀或波动较小的标的。
#coding:gbk
def init(ContextInfo):
ContextInfo.accid = '600000105' # 替换为您的资金账号
ContextInfo.set_account(ContextInfo.accid)
def handlebar(ContextInfo):
if not ContextInfo.is_last_bar():
return
# 示例:使用 TWAP 算法买入 50000 股平安银行
# 限制量比为 20%,最小委托金额为 0,执行时间段为 09:30:00 至 14:00:00
smart_algo_passorder(
23, # opType: 股票买入
1101, # orderType: 单股单账号股数下单
ContextInfo.accid, # accountid
'000001.SZ', # orderCode: 平安银行
5, # prType: 最新价
-1, # modelprice: 非指定价填 -1
50000, # volume: 50000股
"MyStrategy", # strageName: 策略名
1, # quickTrade: 1 表示立即触发
"remark_twap", # userid: 投资备注
"TWAP", # smartAlgoType: TWAP算法
20, # limitOverRate: 量比 20%
0, # minAmountPerOrder: 最小金额 0
0, # targetPriceLevel: 冰山算法参数,此处填 0 占位
'09:30:00', # startTime: 开始时间
'14:00:00', # endTime: 截止时间
ContextInfo
)
VWAP 算法根据历史成交量分布预测,在成交量大的时段多下单,成交量小时段少下单,力求使最终成交均价逼近市场成交量加权均价。
#coding:gbk
def init(ContextInfo):
ContextInfo.accid = '600000105'
ContextInfo.set_account(ContextInfo.accid)
def handlebar(ContextInfo):
if not ContextInfo.is_last_bar():
return
# 示例:使用 VWAP 算法卖出 30000 股平安银行
# 限制量比为 15%,最小委托金额为 1000,执行时间段为 10:00:00 至 14:30:00
smart_algo_passorder(
24, # opType: 股票卖出
1101, # orderType: 股数下单
ContextInfo.accid, # accountid
'000001.SZ', # orderCode
5, # prType: 最新价
-1, # modelprice
30000, # volume
"MyStrategy", # strageName
1, # quickTrade
"remark_vwap", # userid
"VWAP", # smartAlgoType: VWAP算法
15, # limitOverRate: 量比 15%
1000, # minAmountPerOrder: 最小金额 1000 元
0, # targetPriceLevel
'10:00:00', # startTime
'14:30:00', # endTime
ContextInfo
)
limitOverRate) 的艺术:
20% - 30% 之间,以加快执行速度。10% 以内,避免算法拆单本身对盘口造成二次冲击。startTime / endTime) 的选择:
09:30 - 09:45)和收盘前 15 分钟(14:45 - 15:00)的市场剧烈波动期,除非使用特定的 MOC(尾盘)算法。