【迅投 QMT】QMT如何下单和撤单?order_stock()买入卖出完整示例

发布时间:2026/8/18 5:54:53
【迅投 QMT】QMT如何下单和撤单?order_stock()买入卖出完整示例 本方案由EasyQuant AI量化助手提供。问题背景在 QMT 实盘策略中完成行情判断后还需要调用交易接口提交买入或卖出委托并在未成交时根据策略需要撤单。QMT 的XtQuantTrader提供order_stock()下单、cancel_order_stock()撤单以及订单和成交查询接口。迅投 XtTrader 交易模块文档实现思路基本流程如下连接 QMT 并订阅资金账户查询资金和持仓避免无效委托使用order_stock()提交买入或卖出委托保存返回的订单编号使用query_stock_order()查询订单状态未成交且不符合策略预期时调用cancel_order_stock()撤单。完整下单与撤单示例from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback from xtquant.xttype import StockAccount from xtquant import xtconstant QMT_PATH rD:\迅投极速交易终端 QMT\userdata_mini ACCOUNT_ID 你的资金账号 SESSION_ID 10001 class MyCallback(XtQuantTraderCallback): 接收委托、成交和报错推送。 def on_stock_order(self, order): print( 委托回报, order.stock_code, 订单号, order.order_id, 状态, order.order_status ) def on_stock_trade(self, trade): print( 成交回报, trade.stock_code, 成交量, trade.traded_volume, 成交价, trade.traded_price ) def on_order_error(self, order_error): print( 下单失败, 订单号, order_error.order_id, 错误码, order_error.error_id, 原因, order_error.error_msg ) def on_cancel_error(self, cancel_error): print( 撤单失败, 订单号, cancel_error.order_id, 错误码, cancel_error.error_id, 原因, cancel_error.error_msg ) def connect_qmt(): 创建交易连接。 callback MyCallback() trader XtQuantTrader(QMT_PATH, SESSION_ID) trader.register_callback(callback) trader.start() result trader.connect() if result ! 0: raise RuntimeError(QMT连接失败请确认客户端已启动) account StockAccount(ACCOUNT_ID, STOCK) subscribe_result trader.subscribe(account) if subscribe_result ! 0: raise RuntimeError(账户订阅失败请检查资金账号) return trader, account def buy_stock(trader, account, stock_code, price, volume): 限价买入股票。 asset trader.query_stock_asset(account) if asset is None: print(无法查询账户资金) return None required_cash price * volume * 1.002 if asset.cash required_cash: print(可用资金不足%.2f预计需要%.2f % ( asset.cash, required_cash )) return None order_id trader.order_stock( account, stock_code, xtconstant.STOCK_BUY, volume, xtconstant.FIX_PRICE, price, easyquant_strategy, 均线策略买入 ) print(买入委托结果订单号, order_id) return order_id def sell_stock(trader, account, stock_code, price, volume): 限价卖出股票。 position trader.query_stock_position(account, stock_code) if position is None: print(%s 当前没有持仓 % stock_code) return None if position.can_use_volume volume: print( 可卖数量不足可卖%s计划卖出%s % (position.can_use_volume, volume) ) return None order_id trader.order_stock( account, stock_code, xtconstant.STOCK_SELL, volume, xtconstant.FIX_PRICE, price, easyquant_strategy, 止损卖出 ) print(卖出委托结果订单号, order_id) return order_id def query_and_cancel(trader, account, order_id): 查询订单示例中按策略条件执行撤单。 if order_id is None or order_id 0: print(无有效订单号跳过查询和撤单) return order trader.query_stock_order(account, order_id) if order is None: print(未查询到订单信息) return print( 订单状态, 代码, order.stock_code, 委托量, order.order_volume, 已成交, order.traded_volume, 委托价, order.price, 状态, order.order_status ) # 示例如果策略决定不再等待该笔订单则撤单 cancel_result trader.cancel_order_stock(account, order_id) print(撤单请求结果, cancel_result) def main(): trader, account connect_qmt() stock_code 510300.SH # 示例以4.00元限价买入100股 order_id buy_stock( tradertrader, accountaccount, stock_codestock_code, price4.00, volume100 ) # 实盘中应根据订单状态、等待时间和策略逻辑决定是否撤单 # query_and_cancel(trader, account, order_id) trader.run_forever() if __name__ __main__: main()order_stock()关键参数说明trader.order_stock( account, # 资金账户对象 stock_code, # 证券代码例如 510300.SH xtconstant.STOCK_BUY, # 买入卖出使用 STOCK_SELL volume, # 委托数量 xtconstant.FIX_PRICE, # 指定价类型 price, # 委托价格 strategy_name, # 策略名称 remark # 委托备注 )股票买入使用xtconstant.STOCK_BUY卖出使用xtconstant.STOCK_SELL指定价委托可使用xtconstant.FIX_PRICE。下单成功时会返回订单编号可用于后续查询或撤单。注意事项下单前确认 QMT 客户端已经启动且交易账户登录正常。股票代码要使用 QMT 格式例如600000.SH、000001.SZ、510300.SH。A 股股票委托数量通常应符合整手规则卖出零股时以券商规则为准。买入前检查asset.cash卖出前检查position.can_use_volume。order_stock()返回订单编号不表示已成交应继续通过回调或查询接口确认最终结果。不要在每个行情回调中无条件重复下单应结合持仓、订单状态和信号去重处理。总结QMT 股票交易的核心接口是order_stock()与cancel_order_stock()。实盘策略应建立“资金校验—持仓校验—提交委托—订单查询—必要时撤单”的完整流程才能降低重复下单和无效委托风险。