Skip to content

miniQMT 停了,本机怎么调 QMT 函数

部分通道不再提供 miniQMT,外面的脚本不能直接 import 客户端函数。可以在大 QMT 里起一个本机 HTTP,让另一份 Python 来查行情、对持仓。

这是工程记录,不是跟单,也不是把信号同步到别人机器。监听地址默认 127.0.0.1。令牌放文件里,不要写进策略正文。不构成投资建议,不代操盘。

手册:get_full_tick · get_trade_detail_data · passorder

和「把策略全塞回编辑器」差在哪

QMT 内置 Python 大约 3.6.8,自带 tornado,不必再装 FastAPI。外面继续用你熟悉的 3.10 / 3.13 写研究脚本,只通过 HTTP 要数据。

坑:有人在 init 里直接 IOLoop.current().start()。这一句会占住线程,handlebar 不再进。把 IOLoop 丢到后台线程,策略日志里还能看到 handlebar still alive

QMT 里:起桥

账号用交易界面注入的 account。首次运行在同目录写 qmt_bridge.token

python
#coding:gbk
import threading
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, HTTPError

LISTEN_HOST = "127.0.0.1"
LISTEN_PORT = 18765

def init(C):
    try:
        C.set_account(account)
    except Exception:
        pass
    t = threading.Thread(target=start_bridge, args=(C,))
    t.daemon = True
    t.start()

def handlebar(C):
    if C.is_last_bar():
        print("[bridge] handlebar still alive")

路由不要一次铺几十个。先三个就够对账:健康检查、分笔、资金持仓。

python
def make_app(ctx, account_id, token):
    app = Application([
        (r"/health", HealthHandler),
        (r"/v1/quote/tick", TickHandler),
        (r"/v1/account/book", BookHandler),
        (r"/v1/order/pass", PassorderHandler),
    ])
    app.qmt_ctx = ctx
    app.account_id = account_id
    app.bridge_token = token
    return app

取分笔时上交所用 .SH。空 dict 先看后缀和全推权限,见 拿不到实时行情

python
class TickHandler(BaseHandler):
    def post(self):
        data = self.read_json()
        codes = data.get("codes") or []
        if isinstance(codes, str):
            codes = [x.strip() for x in codes.split(",") if x.strip()]
        if not codes:
            raise HTTPError(400, "codes required")
        ok, ret, err = safe_call(self.ctx().get_full_tick, codes)
        if not ok:
            self.dump({"ok": False, "error": err}, 500)
            return
        self.dump({"ok": True, "data": ret or {}})

持仓字段仍是 m_dAvailablem_nVolume 那一套。打印出来对账,不当调仓指令。见 查持仓和资金

外面:本机再打一枪

外部脚本和 QMT 必须能读到同一份 token 文件。请求头用 X-Bridge-Token,不要用随手写的 123456

python
c = BridgeClient()          # 默认 http://127.0.0.1:18765
print(c.health())
print(c.tick(["600000.SH"]))
print(c.book())

600000.SH 只是代码格式示例,不是推荐标的。

委托接口为什么放最后

passorder 可以再包一层,但必须自己传确认参数,并且返回值只表示「函数调用了」,不表示成交。查成交仍用 get_trade_detail_data(..., 'DEAL')

不要做成买卖点,也不要映射到公网给别的电脑调。本机研究、仿真对账够用。

完整示例:qmt-local-http-bridge。克隆后先看 README。令牌文件不要提交。

相关:API 查询 · 热门问题 · 关注