tradingSystem/app/services/dispatcher.py

367 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
指令下发通道 (设计 §9 权限移交的落点)
======================================
两个适配器, 由参数 `PMS_DISPATCH_MODE` 切换, **默认 shadow**:
shadow 影子运行 —— 只记账不下发。指令照常过规则闸、照常置 DISPATCHED,
等用户人工在 QMT 侧执行, 成交由回放按 FIFO 认领回来。
这是设计 §9 的一期口径:「通道未通前由用户人工执行、PMS 记账跟踪」。
ws WebSocket 长连接直连 QMT 执行服务, 协议见 QMT_WS_PROTOCOL.md V1.0。
ws 模式的进程边界 (这一段是理解本模块的关键)
--------------------------------------------
ws 是**一条常驻长连接**, 而 executor 跑在 celery worker 这种短命任务进程里, 且协议 §1
明确「PMS 不开第二条连接」(避免指令乱序)。所以连接由独立的 `pms-ws` 进程持有
(app/ws/runner.py), 本模块**不碰 socket** —— `_ws()` 只做一件事:
把这张委托写进 pms_qmt_order 置 QUEUED, 然后就返回。
出口队列落在业务表上而不是内存或 Redis, 是因为「先记账后动作」这条铁律在这里可以字面
成立: **落表就是记账**。ws 进程崩了重启队列还在; 页面查 pms_qmt_order 就能看到在途委托;
不引入任何新中间件。代价是亚秒级的轮询延迟 —— 择时本来就是分钟级节奏, 无感。
放不放行的判断 (对应协议 §6.3「故障即守成」)
-------------------------------------------
ws 进程心跳陈旧 (进程没了) → 一律拒发。队列里的单子谁也发不出去, 排进去只是假装干活。
进程在、连接断 (非 ONLINE) → **买入拒发, 卖出照常入队**, 重连后立即发出。
这是既有口径「冻结与刹车只挡增持不挡减持」的延续。
进程在、连接通 → 放行。
无论哪种模式, 本模块只负责「往下游递一手」, 不改父指令状态 —— 状态由 executor 统一推进。
历史包袱清理 (2026-07-28)
-------------------------
原有 plan_x / channel_y 两个适配器已删除, 原因:
plan_x 买入写 trading_buy_plan(is_active=6)。这个 6 本身就是错的
(下游语义 0待审/1已激活/2择时监测/5盘中观察, 没有 6); 且该表唯一约束
是 (stock_code, trading_time)、buy_amount 是金额不是股数, 单股分批
必撞约束。架构定案后该路径整体作废。
channel_y 写 pms_order_request 表由下游轮询。双方已改定 WebSocket 直连,
表通道作废 (见 QMT_INTERFACE_REQUIREMENTS.md V2 的 B 部分)。
`pms_order_request` 表保留未用, 不必删表。
留着作废路径比删掉更危险 —— 后来者会以为它可用。
"""
from __future__ import annotations
import logging
from datetime import datetime
from config.settings import settings
from app.core import ws_codec as wsc
from app.repo import qmt_repo
from app.services import param_store
logger = logging.getLogger("pms.dispatch")
MODE_SHADOW, MODE_WS = "shadow", "ws"
MODES = (MODE_SHADOW, MODE_WS)
# ws 进程心跳超过这个时长没更新, 就认为进程已经没了 (它每 2 秒写一次)
DEFAULT_STALE_SEC = 15
def mode() -> str:
raw = (param_store.get("PMS_DISPATCH_MODE", MODE_SHADOW) or MODE_SHADOW).strip()
m = raw.lower() # 大小写归一: 手改参数写成 "WS"/"Ws" 不该静默退回影子模式
if m in MODES:
return m
# 非法值退安全侧 (shadow), 但必须吭声 —— "以为在实盘, 实际一单没发"比报错糟得多
logger.error("[下发] PMS_DISPATCH_MODE=%r 不是合法模式 %s, 已按 shadow (影子) 处理 —— "
"指令只记账不下发, 请到参数页改回合法值", raw, sorted(MODES))
return MODE_SHADOW
# ================================================================ 通道健康
def channel_status() -> dict:
"""ws 通道当前能不能用。页面运维抽屉与下发前置校验共用同一份判断。"""
stale = param_store.get_int("PMS_QMT_HEARTBEAT_STALE_SEC", DEFAULT_STALE_SEC)
out = {"process_alive": False, "conn_state": "UNKNOWN", "online": False,
"heartbeat_age_sec": None, "last_seq": 0, "acked_seq": 0,
"resync_required": False, "queue": {}, "inbox_pending": 0,
"orphan_held": 0, "error": None}
try:
st = qmt_repo.get_state()
except Exception as e:
out["error"] = f"通道状态读取失败: {type(e).__name__}: {e}"
return out
hb = st.get("heartbeat_at")
if hb:
age = (datetime.now() - hb).total_seconds()
out["heartbeat_age_sec"] = round(age, 1)
out["process_alive"] = age <= stale
out.update({"conn_state": st.get("conn_state") or "INIT",
"last_seq": int(st.get("last_seq") or 0),
"acked_seq": int(st.get("acked_seq") or 0),
"resync_required": bool(st.get("resync_flag")),
"connected_at": str(st.get("connected_at") or ""),
"last_error": st.get("last_error"), "stat": st.get("stat") or {}})
out["online"] = out["process_alive"] and out["conn_state"] == "ONLINE"
try:
out["queue"] = qmt_repo.queue_depth()
out["inbox_pending"] = qmt_repo.inbox_pending_count()
# 挂起待人工的成交 (processed=3)。它不在 inbox_pending 里、调度也不会再碰,
# 不在这里报个数就等于一笔无人知晓的漏账。
out["orphan_held"] = qmt_repo.inbox_orphan_count()
except Exception as e: # 统计失败不影响放行判断
out["error"] = f"队列统计失败: {type(e).__name__}: {e}"
return out
def describe() -> dict:
m = mode()
out = {"mode": m, "shadow": m == MODE_SHADOW, "modes": list(MODES)}
if m == MODE_SHADOW:
out["hint"] = ("影子运行: 指令只记账不下发, 请在 QMT 侧人工执行, "
"成交由回放自动认领回账本")
return out
ch = channel_status()
out["channel"] = ch
if not ch["process_alive"]:
out["hint"] = (f"WebSocket 直连: pms-ws 常驻进程未在线 (心跳 "
f"{ch['heartbeat_age_sec']}s 前), 指令一律拒发。"
f"启动: docker compose --profile ws up -d pms-ws")
elif not ch["online"]:
out["hint"] = (f"WebSocket 直连: 进程在线但连接 {ch['conn_state']} —— "
f"买入拒发, 卖出仍可入队待重连后发出 (协议 §6.3)")
else:
out["hint"] = (f"WebSocket 直连 QMT: 在线, 已确认水位 seq={ch['acked_seq']}, "
f"出口队列 {ch['queue'].get('QUEUED', 0)} 张待发")
if ch.get("resync_required"):
out["hint"] += " · 注意: 对端补发不全, 需走全量对账 (协议 §6.2)"
return out
# ================================================================ 下发
def dispatch(*, instruction_id: str, ts_code: str, side: str, qty: int, limit_price=None,
valid_until=None, stock_name=None, intent=None, parent_id=None,
note=None) -> dict:
"""递一手给下游。返回 {ok, ref, mode, note, error}; ok=False 时 executor 不改指令状态。"""
m = mode()
try:
from app.repo import pms_repo
from app.services import execution_orders, execution_basis
pid = _parent_of(instruction_id, parent_id)
parent = pms_repo.get_instruction(pid)
correction = execution_orders.correction_blocker(parent, ts_code=ts_code, side=str(side).lower())
if correction:
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': correction}
if parent and (parent.get('progress') or {}).get('cancel_requested'):
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': '父指令撤销处理中,禁止继续下发'}
if settings.INFRA_STRATEGY_BOOK_SCHEMA and str(side).lower() == 'buy':
from app.core import strategy_book
from app.services.rotation_service import strategy_buy_blocker
if strategy_book.CURRENT.get() == strategy_book.ORIGINAL:
from app.repo import pms_repo
parent = pms_repo.get_instruction(_parent_of(instruction_id, parent_id))
blocked = strategy_buy_blocker(strategy_book.ORIGINAL, parent)
if blocked:
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': blocked}
if str(side).lower() == 'buy':
from app.services.execution_conflicts import buy_blocker
from app.repo import pms_repo
pid = _parent_of(instruction_id, parent_id)
parent = pms_repo.get_instruction(pid)
stopped = pms_repo.instruction_buy_stop(pid)
from app.services import daily_buy_review
review_block = daily_buy_review.blocker(parent) if parent else None
blocked = review_block or ('清仓已终止原买入安排' if stopped or ((parent or {}).get('progress') or {}).get('stopped_by_exit') else buy_blocker(ts_code))
if parent and parent.get('status') not in ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED'):
blocked = '来源买入指令已结束,不再发单'
if blocked:
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': blocked}
if m == MODE_WS:
from app.services import executor
now = datetime.now()
multi_day = executor.ordinary_days_left(parent, now)
guard = pms_repo.account_decision_guard if multi_day else execution_orders.account_guard
with guard():
parent = pms_repo.get_instruction(pid)
blocked = execution_orders.correction_blocker(parent, ts_code=ts_code, side=str(side).lower())
if blocked:
raise ValueError(blocked)
reprice = execution_orders.enabled()
new_order = (multi_day or reprice) and not qmt_repo.get_order(instruction_id)
if new_order:
if not parent or parent.get('status') not in ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED'):
raise ValueError('父指令未获准或已结束')
progress = parent.get('progress') or {}
if progress.get('cancel_requested'):
raise ValueError('父指令正在撤销')
if multi_day and new_order:
days = executor.ordinary_days_left(parent, now)
if days:
available = executor.dispatch_allowance(parent, now, days)
if qty > available:
reason = f'本轮原计划{qty}股,按最新成交和在途委托当日还可投{available}股,下一轮重新计算'
return {'ok': False, 'deferred': True, 'ref': None, 'mode': m,
'note': '', 'error': reason}
if reprice and new_order:
from app.core.execution_policy import supported_code
if not supported_code(ts_code):
raise ValueError('执行范围仅限沪深合资格证券')
state = execution_orders.replacement_state(pid)
if not state['ready'] or qty > state['remaining']:
raise ValueError('旧子单未确认结束、成交未入账或数量超过剩余目标')
retry_wait = execution_orders.retry_reason(pid)
if retry_wait:
raise ValueError(retry_wait)
if execution_basis.uses_basis(parent):
reason = execution_basis.cache_reason(progress)
if reason:
raise ValueError(reason)
reason = execution_orders.code_settlement_blocker(ts_code)
if reason:
raise ValueError(reason)
boundary = progress.get('user_limit_price', parent.get('limit_price'))
if boundary is not None and ((side == 'buy' and float(limit_price) > float(boundary)) or
(side == 'sell' and float(limit_price) < float(boundary))):
raise ValueError('报价突破用户指定价格边界')
if side == 'buy' and not settings.INFRA_STRATEGY_BOOK_SCHEMA and parent:
from app.services.buy_budget import check_order
check_order(parent, {'qty': qty, 'limit_price': limit_price})
return _ws(instruction_id=instruction_id, ts_code=ts_code, side=side, qty=qty,
limit_price=limit_price, valid_until=valid_until,
intent=intent, parent_id=parent_id, note=note)
if side == 'buy' and parent:
from app.services.buy_budget import check_order
check_order(parent, {'qty': qty, 'limit_price': limit_price})
return _shadow(instruction_id, side)
except Exception as e:
logger.exception("下发失败 %s", instruction_id)
return {"ok": False, "ref": None, "mode": m, "note": "",
"error": f"{type(e).__name__}: {e}"}
def _shadow(instruction_id: str, side: str) -> dict:
return {"ok": True, "ref": f"manual:{instruction_id}", "mode": MODE_SHADOW,
"note": f"影子运行: 请在 QMT 侧人工{'买入' if side == 'buy' else '卖出'}, "
f"成交由回放认领", "error": None}
def _to_epoch_ms(valid_until) -> int:
"""valid_until 统一成 epoch 毫秒 (协议 §1: 时间字段一律整数毫秒)。
executor 传的是 datetime, 页面或脚本可能直接传毫秒 —— 两种都收。
给不出有效期时兜底为「当日 14:57」而不是无限期: 挂单不设终点等于把撤单责任又留回
自己身上, 而 valid_until 存在的全部意义就是把它交给 QMT (协议 §4.2)。
"""
if isinstance(valid_until, datetime):
return int(valid_until.timestamp() * 1000)
if isinstance(valid_until, (int, float)) and valid_until > 0:
v = int(valid_until)
return v if v > 10 ** 12 else v * 1000 # 传秒的也认
fallback = datetime.now().replace(hour=14, minute=57, second=0, microsecond=0)
return int(fallback.timestamp() * 1000)
def _parent_of(instruction_id: str, parent_id=None) -> str:
"""子单 id 形如 {父指令}_D01 (见 executor._child_id)。显式传入的父 id 优先。"""
if parent_id:
return parent_id
iid = str(instruction_id or "")
tail = iid.rsplit("_D", 1)
return tail[0] if len(tail) == 2 and tail[1].isdigit() else iid
def order_strategy_type(parent_id: str) -> str:
"""下单报文按持久父指令的账本归类,不用备注、当前阶段或调用上下文猜测。"""
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
return "subjective_quant" # 未启用分策略账本的既有实例只有主观量化。
from app.core import strategy_book as books
from app.repo import pms_repo
with books.scope('all'):
parent = pms_repo.get_instruction(parent_id)
types = {books.ORIGINAL: "subjective_quant", books.MICRO: "micro_rotation"}
book = (parent or {}).get('book_id')
if book not in types:
raise RuntimeError('委托的策略来源无法核实')
return types[book]
def _ws(*, instruction_id, ts_code, side, qty, limit_price, valid_until, intent=None,
parent_id=None, note=None) -> dict:
"""ws 直连: 落出口队列即返回, 实际发送由 pms-ws 常驻进程完成。
这里**不等** QMT 的 ack —— executor 一跳是分钟级节奏, 不该被一次网络往返卡住;
受理结果由 ws 进程回写 pms_qmt_order.status, 页面与对账都看那里。
"""
side = str(side or "").lower()
ch = channel_status()
if not ch["process_alive"]:
return {"ok": False, "ref": None, "mode": MODE_WS, "note": "",
"error": f"pms-ws 常驻进程未在线 (心跳 {ch['heartbeat_age_sec']}s 前), "
f"拒绝下发 —— 排进队列也发不出去"}
if not ch["online"] and side != "sell":
# 协议 §6.3: QMT 断连 → 停发一切增持指令, 仅保留减持路径
return {"ok": False, "ref": None, "mode": MODE_WS, "note": "",
"error": f"QMT 连接 {ch['conn_state']}, 按协议 §6.3 暂停买入下发 "
f"(卖出不受此限)"}
try:
payload = wsc.place_order_payload(
instruction_id=instruction_id, ts_code=ts_code, side=side, qty=qty,
limit_price=limit_price, valid_until=_to_epoch_ms(valid_until),
intent=(intent or "OPEN"), note=(note or ""))
except wsc.CodecError as e:
# 参数不合规就在本地拦下 —— 换 QMT 一个 BAD_PARAM 是白跑一趟, 还污染对方日志
return {"ok": False, "ref": None, "mode": MODE_WS, "note": "",
"error": f"指令不合协议要求 ({e.code}): {e.message}"}
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
from app.services.rotation_execution import enqueue_with_reservation
enqueue_with_reservation(payload, _parent_of(instruction_id, parent_id))
else:
qmt_repo.enqueue_order(
instruction_id=payload["instruction_id"],
parent_id=_parent_of(instruction_id, parent_id),
ts_code=payload["ts_code"], side=payload["side"], qty=payload["qty"],
limit_price=payload["limit_price"], valid_until=payload["valid_until"],
intent=payload["intent"], note=payload["note"])
logger.info("[ws] 入队 %s %s %s %s股 @%.2f 有效至 %s", payload["instruction_id"],
payload["ts_code"], payload["side"], payload["qty"],
payload["limit_price"],
datetime.fromtimestamp(payload["valid_until"] / 1000).strftime("%H:%M:%S"))
hint = "" if ch["online"] else " (连接未就绪, 重连后立即发出)"
return {"ok": True, "ref": payload["instruction_id"], "mode": MODE_WS,
"note": f"已入 ws 出口队列{hint}", "error": None}
# ================================================================ 撤单
def cancel(*, instruction_id: str, dispatch_ref=None) -> dict:
"""请求撤单。shadow 无下游撤单语义, 只回执由 executor 置本地状态。
ws 模式下入参 instruction_id 是**父指令**, 名下可能有多张已发出的子单 —— 全部标记
待撤, 由 ws 进程逐张发 cancel_order。已终态的子单不动 (协议 §4.3: 撤单前已全成会回
reject{ALREADY_FINAL})。
"""
m = mode()
if m != MODE_WS:
return {"ok": True, "mode": m, "note": "本模式无下游撤单动作, 仅本地置撤销"}
try:
cancel_id = wsc.new_cancel_id(int(datetime.now().strftime("%Y%m%d")))
n = qmt_repo.request_cancel(parent_id=instruction_id, cancel_id=cancel_id)
except Exception as e:
logger.exception("撤单请求落表失败 %s", instruction_id)
return {"ok": False, "mode": m, "error": f"{type(e).__name__}: {e}"}
pending = bool(n)
if not pending:
try:
pending = any(o.get('status') in qmt_repo.LIVE
for o in qmt_repo.orders_for_parent(instruction_id))
except Exception:
# 撤销意图已写入。展示核验失败不能使父任务继续新增或恢复续单。
pending = True
logger.warning('撤单请求已登记,子委托状态暂不可核实 %s', instruction_id)
if not n:
return {"ok": True, "mode": m, "cancelled": 0, "cancel_pending": pending,
"note": "撤单已在处理中,等待QMT终态" if pending else "该指令名下没有在途子单, 仅本地置撤销"}
note = f"已标记 {n} 张在途委托待撤 (cancel_id={cancel_id})"
ch = channel_status()
if not ch["online"]:
note += f"; 连接当前 {ch['conn_state']}, 重连后发出"
return {"ok": True, "mode": m, "cancelled": n, "cancel_pending": pending,
"cancel_id": cancel_id, "note": note}