3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
您好!关于您反馈的 QMT 平台 VBA(公式语言)下 strategyName 失效导致误撤单的问题,这通常是因为早期公式引擎在处理底层订单过滤时,对 strategyName 的上下文隔离支持不够完善,导致 ORDERINGS 查出了该账号下的所有委托单,从而引发了全局撤单。
虽然公式语言(VBA)对于传统股票软件用户有其便捷性,但正如 QMT 官方文档所述,QMT 平台目前的核心优势和官方重点维护方向是 Python API。在 Python API 中,strategyName 的隔离和撤单逻辑是非常完善且经过大量实盘验证的。
为了彻底解决您的精准撤单需求,强烈建议您使用 QMT 的 Python API 来实现。以下是基于 Python API 的完美替代方案:
在 Python 中,您可以通过 get_trade_detail_data 函数的第四个参数传入 strategyName,从而精准获取特定策略的委托单,并结合 can_cancel_order 和 cancel 函数进行安全撤单。
#coding:gbk
def init(ContextInfo):
# 初始化时设定交易账号
ContextInfo.accountID = '您的资金账号'
ContextInfo.set_account(ContextInfo.accountID)
def handlebar(ContextInfo):
# 1. 使用 strategyName1 下单
# passorder(opType, orderType, accountid, orderCode, prType, modelprice, volume, strategyName, quickTrade, userOrderId, ContextInfo)
passorder(23, 1101, ContextInfo.accountID, '000001.SZ', 5, -1, 100, 'strategyName1', 1, 'order1', ContextInfo)
# 2. 精准查询 strategyName2 的委托信息并撤单
# get_trade_detail_data(accountID, strAccountType, strDatatype, strategyName)
order_list = get_trade_detail_data(ContextInfo.accountID, 'stock', 'order', 'strategyName2')
for order_obj in order_list:
order_id = order_obj.m_strOrderSysID
# 检查该委托单是否可撤销
if can_cancel_order(order_id, ContextInfo.accountID, 'stock'):
# 执行撤单
cancel(order_id, ContextInfo.accountID, 'stock', ContextInfo)
print(f"成功发送撤单指令,撤销 strategyName2 的委托单: {order_id}")
do_order 等高级机制。NumPy, Pandas, TA_Lib, scikit-learn 等大量第三方库,远超 VBA 的表达能力。strategyName 过滤失效的底层 Bug 在 Python 框架中已被彻底解决。如果您有大量的 VBA 策略,建议逐步将其核心逻辑迁移至 Python 的 handlebar 机制中,这不仅能解决当前的撤单 Bug,还能大幅提升策略的运行效率和扩展性。