diff --git a/BIONIC_PMS_INTERFACE.md b/BIONIC_PMS_INTERFACE.md index 286ab59..364cc34 100644 --- a/BIONIC_PMS_INTERFACE.md +++ b/BIONIC_PMS_INTERFACE.md @@ -182,6 +182,8 @@ PMS 侧对建议价有一道保护:偏离现价超过 `PMS_EXEC_LIMIT_BAND`( ### 3.4 下一个迭代(本轮明确不做) +2026-10-28的155模拟接入修订:启用`PMS_EXEC_QMT_PRICE_MONITOR`时,PMS仍使用本接口给出的执行区间,但不采用“现价尚未进入区间”的等待判断。PMS把买入上沿或卖出下沿作为限价直接交QMT。`observed.today_exit`、昨夜回避结论与参考位漂移仍有效,不能把它们误当价格等待。取数失败或区间缺失时等待补齐,不退回现价追单。旧缓存缺少离场字段时先重新咨询;旧模式行为保持。 + 凌晨管线加一步大模型推理,对每只票明示写出买入区间与卖出区间,盘中改为直接读它——届时 只换本模块第一步的取数来源和 `advisor` 版本标记,应答结构与 PMS 侧都不动。动凌晨管线 属于重大迭代,做之前会先出细方案确认(覆盖范围、存放位置、算不出来怎么办)。 diff --git a/QMT_WS_PROTOCOL.md b/QMT_WS_PROTOCOL.md index 0db5870..a1586ec 100644 --- a/QMT_WS_PROTOCOL.md +++ b/QMT_WS_PROTOCOL.md @@ -191,6 +191,8 @@ def sign(seed_hex, canon): `valid_until` 是这套设计里替代「10 秒轮询撤单」的关键:PMS 不必盯着撤,到点 QMT 自己收。 +2026-10-28补充:155模拟实例可启用`PMS_EXEC_QMT_PRICE_MONITOR`。已生成的计划由PMS给出限价、数量和有效期,即可提交QMT等待成交,不再等待现价进入区间。买单限价为执行区间上沿,卖单为下沿;买入可在更低价格成交,卖出可在更高价格成交。这不是上下界条件单,协议字段没有增加。用户暂停、资金占用、配额、可卖量和回报入账仍由PMS负责。原执行模式默认保留,不改变其他实例。 + ### 4.3 `cancel_order` —— 撤单 ```json diff --git a/app/core/rule_gate.py b/app/core/rule_gate.py index 515eeeb..527c40a 100644 --- a/app/core/rule_gate.py +++ b/app/core/rule_gate.py @@ -192,33 +192,35 @@ def _check_buy(failed, warns, qty, price, ctx, pos, day, prm, flg, is_cmd, hard, else: failed.append("BRAKE_ACTIVE: 组合刹车中, 自主增持暂停") - if day.get("limit_up"): - failed.append("LIMIT_UP: 涨停封板, 不追买") - elif day.get("limit_down"): - # 一字跌停不接 (2026-10-08): 方向修对之前它被误判成涨停而拦在上一行, 现在单独拦。 - # 用 elif: 方向不明时两个标记都亮, 只记上一行那一条, 与修之前逐字相同。 - failed.append("LIMIT_DOWN: 跌停封板, 不接") + hard["qmt_price_monitor"] = bool(flg.get("qmt_price_monitor")) + if not flg.get("qmt_price_monitor"): + if day.get("limit_up"): + failed.append("LIMIT_UP: 涨停封板, 不追买") + elif day.get("limit_down"): + # 一字跌停不接 (2026-10-08): 方向修对之前它被误判成涨停而拦在上一行, 现在单独拦。 + # 用 elif: 方向不明时两个标记都亮, 只记上一行那一条, 与修之前逐字相同。 + failed.append("LIMIT_DOWN: 跌停封板, 不接") - # 不追高: 当日涨幅 与 距 MA5 两道 - dayup = day.get("day_chg_from_open") - cap_dayup = _num(prm.get("buy_halt_dayup"), 0.05) - if dayup is None: - # 缺输入**必须留痕**, 与下面 MA5_MISSING 同口径。原来只有 `is not None` 一个条件, - # 缺了就整道闸无声跳过 —— 而自主提议那条路一直硬编码 None, 于是动作引擎产出的每一笔 - # 买单都没过过这道闸, 返回值还是 `passed=True, failed=[], warnings=[]`, 与 - # 「涨幅 2%、检查通过」一字不差, 账本里也查不出来。 - warns.append("DAYUP_MISSING: 取不到当日涨幅, 不追高(涨幅)一项未校验") - elif _num(dayup) > cap_dayup: - failed.append(f"NO_CHASE_DAYUP: 当日涨幅 {_num(dayup):.2%} > 上限 {cap_dayup:.0%}") - ma5 = _num(day.get("ma5")) - cap_ma5 = _num(prm.get("no_chase_ma5"), 0.06) - if ma5 > 0: - gap = price / ma5 - 1 - hard["ma5_gap"] = round(gap, 4) - if gap > cap_ma5: - failed.append(f"NO_CHASE_MA5: 距 MA5 {gap:.2%} > 上限 {cap_ma5:.0%}") - else: - warns.append("MA5_MISSING: 取不到 MA5, 距均线一项未校验") + # 不追高: 当日涨幅 与 距 MA5 两道 + dayup = day.get("day_chg_from_open") + cap_dayup = _num(prm.get("buy_halt_dayup"), 0.05) + if dayup is None: + # 缺输入**必须留痕**, 与下面 MA5_MISSING 同口径。原来只有 `is not None` 一个条件, + # 缺了就整道闸无声跳过 —— 而自主提议那条路一直硬编码 None, 于是动作引擎产出的每一笔 + # 买单都没过过这道闸, 返回值还是 `passed=True, failed=[], warnings=[]`, 与 + # 「涨幅 2%、检查通过」一字不差, 账本里也查不出来。 + warns.append("DAYUP_MISSING: 取不到当日涨幅, 不追高(涨幅)一项未校验") + elif _num(dayup) > cap_dayup: + failed.append(f"NO_CHASE_DAYUP: 当日涨幅 {_num(dayup):.2%} > 上限 {cap_dayup:.0%}") + ma5 = _num(day.get("ma5")) + cap_ma5 = _num(prm.get("no_chase_ma5"), 0.06) + if ma5 > 0: + gap = price / ma5 - 1 + hard["ma5_gap"] = round(gap, 4) + if gap > cap_ma5: + failed.append(f"NO_CHASE_MA5: 距 MA5 {gap:.2%} > 上限 {cap_ma5:.0%}") + else: + warns.append("MA5_MISSING: 取不到 MA5, 距均线一项未校验") caps = ctx.get("caps") if caps: diff --git a/app/services/exec_advisor.py b/app/services/exec_advisor.py index 8c2bc00..ce9f7e4 100644 --- a/app/services/exec_advisor.py +++ b/app/services/exec_advisor.py @@ -23,6 +23,7 @@ from __future__ import annotations import logging +import math from app.core import exec_timing as et from app.core import tradedays as td @@ -52,6 +53,9 @@ def available() -> bool: def status() -> dict: """页面/排查用的一句话状态。""" + if param_store.get_bool("PMS_EXEC_QMT_PRICE_MONITOR", False): + return {"impl": impl(), "available": available(), "price_monitor": "qmt", + "note": "PMS确定限价和数量,QMT等待成交;执行区间缺失时等待补齐"} if impl() != IMPL_A: return {"impl": IMPL_B, "available": False, "note": "内置保守择时 (PMS_EXEC_IMPL=B)。切实现A: 页面改 PMS_EXEC_IMPL=A"} @@ -107,7 +111,7 @@ def _strategy_immediate(*, side, now, day, quota, fired_today, def decide(*, side: str, action: str, ts_code: str, now, day: dict, params: dict, is_last_day: bool, is_command: bool = True, urgent: bool = False, fired_today: int = 0, quota: int = 0, - pos: dict = None, tdays_left=None, prog: dict = None) -> dict: + pos: dict = None, tdays_left=None, prog: dict = None, instruction_limit=None) -> dict: """择时判定统一入口 (executor.run_tick 的唯一调用点)。 返回结构与 exec_timing.decide 相同, 另带 source 字段标明这条决定是谁做的: @@ -118,6 +122,12 @@ def decide(*, side: str, action: str, ts_code: str, now, day: dict, params: dict prog 由调用方传入指令的 progress dict, 咨询结果/失败冷却会写进 prog["exec_advice"], 随调用方既有的落表动作持久化; 传 None 则本轮结论不缓存 (dry_run 语义)。 """ + if param_store.get_bool("PMS_EXEC_QMT_PRICE_MONITOR", False): + return _qmt_limit(side=side, action=action, ts_code=ts_code, now=now, day=day, + params=params, is_last_day=is_last_day, urgent=urgent, + fired_today=fired_today, quota=quota, pos=pos or {}, + tdays_left=tdays_left, prog=prog, instruction_limit=instruction_limit) + # 网格/跟踪止盈: 策略层已按档位触发, 这里不做择时博弈, 到价即时成交、收盘不强制。 # 只识别策略来源且类型为 GRID/TRAIL 的指令; 做T(T0)与其余来源一律走原有逻辑, 不受影响。 if (prog or {}).get("origin") == "strategy" and str((prog or {}).get("stype")) in ("GRID", "TRAIL"): @@ -164,6 +174,63 @@ def decide(*, side: str, action: str, ts_code: str, now, day: dict, params: dict return d +def _qmt_limit(*, side, action, ts_code, now, day, params, is_last_day, urgent, + fired_today, quota, pos, tdays_left, prog, instruction_limit): + """计划已确定后只定限价,不等现价进入区间;数据故障不能转成追价买入。""" + left = max(0, int(quota) - int(fired_today)) + + def result(action, reason, limit=None, **extra): + return {"action": action, "qty_hint": left, "limit_price": limit, + "reason": reason, "forced": False, "source": "QMT限价", + "qmt_price_monitor": True, **extra} + + price = _f(day.get("price")) + if side not in ("buy", "sell"): + return result(et.ACT_SKIP, "委托方向无效") + if not price or day.get("halted"): + return result(et.ACT_WAIT, "无有效行情或已停牌,等待核实") + if left <= 0: + return result(et.ACT_WAIT, et.quota_wait_reason(urgent, is_last_day)) + if not et.in_session(et.hm_to_min(now)): + return result(et.ACT_WAIT, "非交易时段") + + strategy = (prog or {}).get("origin") == "strategy" and (prog or {}).get("stype") in ("GRID", "TRAIL") + if (urgent and side == "sell") or strategy: + # 已触发的网格、跟踪止盈和紧急退出沿用自己的定价,不再附加价格到位检查。 + limit = _f(instruction_limit) + if not limit: + ratio = (params.get("urgent_sell_discount", 0.995) if urgent and side == "sell" + else 1.002 if side == "buy" else 0.998) + limit = round(price * float(ratio), 2) + return result(et.ACT_FIRE, "按策略限价提交QMT等待成交", round(limit, 2)) + + if not available(): + return result(et.ACT_WAIT, "未配置有效执行区间来源,暂不提交限价") + advice, note = _advice(ts_code=ts_code, side=side, action=action, now=now, day=day, + pos=pos, left=left, is_last_day=is_last_day, + tdays_left=tdays_left, prog=prog) + if advice is None: + return result(et.ACT_WAIT, "执行价格暂不可用:" + note) + if advice.get("ref_drift"): + return result(et.ACT_WAIT, advice["ref_drift"], ref_drift=advice["ref_drift"]) + if side == "buy" and (advice.get("today_exit") or + str(advice.get("y_signal") or "").upper() in ("SELL", "AVOID", "DROPPED")): + return result(et.ACT_WAIT, "已有离场或回避结论,暂停买入", y_signal=advice.get("y_signal")) + band = advice.get("buy_band" if side == "buy" else "sell_band") + if not isinstance(band, (list, tuple)) or len(band) != 2: + return result(et.ACT_WAIT, "执行区间缺失,暂不提交限价") + lo, hi = (_f(x) for x in band) + if not lo or not hi or lo > hi: + return result(et.ACT_WAIT, "执行区间无效,暂不提交限价") + limit = round(hi if side == "buy" else lo, 2) + if limit <= 0: + return result(et.ACT_WAIT, "执行限价无效,暂不提交") + # WAIT只表示当前价格未进区间。限价买单允许更低成交,卖单允许更高成交。 + # 不因距离现价较远而改价,也不在尾盘把锁定买价改成追价。 + return result(et.ACT_FIRE, f"限价{limit:.2f}提交QMT等待成交", limit, + y_signal=advice.get("y_signal")) + + def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_left, prog): """取一份有效研判: 缓存命中 → 直接用; 冷却中 → (None, 原因); 否则咨询一次。 返回 (advice|None, 不可用原因)。advice 带 _source 标明 A / A缓存。""" @@ -174,6 +241,8 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le if int(cached.get("ymd") or 0) == today: if (cached.get("verdict") in (FIRE, WAIT) + and (not param_store.get_bool("PMS_EXEC_QMT_PRICE_MONITOR", False) + or cached.get("price_facts_version") == 1) and now_min <= int(cached.get("valid_until_min") or -1)): c = dict(cached) c["_source"] = "A缓存" @@ -195,6 +264,8 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le } to = max(1, param_store.get_int("PMS_EXEC_TIMEOUT_SEC", 8)) url = base_url() + (param_store.get("PMS_EXEC_PATH", "/api/intraday/pms_exec") or "") + fallback_note = ("等待有效执行价格" if param_store.get_bool("PMS_EXEC_QMT_PRICE_MONITOR", False) + else "本轮退实现B") def _cool(err: str): cool = max(1, param_store.get_int("PMS_EXEC_FAIL_COOLDOWN_MIN", 5)) @@ -207,7 +278,7 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le data = _post(url, payload, to) except Exception as e: err = f"{type(e).__name__}: {e}" - logger.error("[择时A] 咨询失败, 本轮退实现B (%s %s): %s", ts_code, side, err) + logger.error("[择时A] 咨询失败, %s (%s %s): %s", fallback_note, ts_code, side, err) _cool(err) return None, err @@ -215,8 +286,8 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le if verdict not in (FIRE, WAIT): # 对端明说给不出结论 (UNAVAILABLE), 或答复不认识 —— 都按拿不到处理, 退实现B reason = str(data.get("reason") or f"verdict={verdict or '空'}")[:200] - logger.warning("[择时A] 决策系统给不出结论 (%s %s): %s —— 本轮退实现B", - ts_code, side, reason) + logger.warning("[择时A] 决策系统给不出结论 (%s %s): %s —— %s", + ts_code, side, reason, fallback_note) _cool(f"UNAVAILABLE: {reason}") return None, f"UNAVAILABLE: {reason}" @@ -235,6 +306,8 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le # 支撑/压力/买入区间也一并存下来 —— 新建仓的漂移比对要用 (见 _check_ref_drift) "support": obs.get("support"), "pressure": obs.get("pressure"), "buy_band": obs.get("buy_band"), + "sell_band": obs.get("sell_band"), "today_exit": obs.get("today_exit"), + "price_facts_version": 1, "valid_until_min": et.add_trade_minutes(now_min, valid_min), "consulted_at": et._fmt(now_min)} drift = _check_ref_drift(action=action, prog=prog, adv=adv, today=today) @@ -253,7 +326,7 @@ def _advice(*, ts_code, side, action, now, day, pos, left, is_last_day, tdays_le def _f(v): try: x = float(v) - return x if x > 0 else 0.0 + return x if math.isfinite(x) and x > 0 else 0.0 except (TypeError, ValueError): return 0.0 diff --git a/app/services/executor.py b/app/services/executor.py index fad17e4..50dc84d 100644 --- a/app/services/executor.py +++ b/app/services/executor.py @@ -16,6 +16,7 @@ progress_json.children[]; exec_qty 由回放认领回填 (账本以下游成交 from __future__ import annotations import logging +from copy import deepcopy from datetime import datetime import hashlib @@ -251,7 +252,7 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: # 择时判定统一走 exec_advisor: PMS_EXEC_IMPL=B (默认) 时它就是 et.decide 原样; # =A 时先过硬闸 (配额/兜底等 PMS 自留地), 再委托决策系统, 不可用退实现B。 # 咨询结论会写进 prog["exec_advice"], 随下面既有的 update_instruction 落表; - # dry_run 传 None —— 只算不落库, 试算不该占用/刷新研判缓存。 + # dry_run 传进度副本,保留参考位锁定,但不落库或刷新真实缓存。 # is_command 必须一路传到 hard_gate: 窗口末日的强制完成只对命令驱动生效, # 自主买入到期作废 (与 window_verdict 的口径对齐, 见 exec_timing 里那段说明)。 d = exec_advisor.decide(side=side, action=ins.get("action"), ts_code=code, @@ -261,7 +262,8 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: urgent=bool(prog.get("urgent")), fired_today=fired_today, quota=quota, pos=pos, tdays_left=tdays_left, - prog=(None if dry_run else prog)) + prog=(deepcopy(prog) if dry_run else prog), + instruction_limit=ins.get("limit_price")) if d["action"] != et.ACT_FIRE: prog["last_decision"] = {"at": now.strftime("%H:%M"), **d} @@ -302,7 +304,7 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: "action": et.ACT_WAIT, "reason": "可卖量不足, 顺延"}) continue - ma5 = market.get_ma5(code) if side == "buy" else None + ma5 = market.get_ma5(code) if side == "buy" and not d.get("qmt_price_monitor") else None # 新建仓 (从没持仓过的票) 的组合上下文必须显式带 is_new_name 与行业名: # caps_ctx 在 view 里找不到该票时带不出行业, 行业集中度终检会静默跳过 # (与 proposal_service._route_one 已修过的同一个洞, 2026-08-28 补齐执行终检侧) @@ -318,7 +320,9 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: caps = portfolio.caps_ctx(view, ts_code=code, is_new_name=is_new, sector=sec) \ if is_new else portfolio.caps_ctx(view, ts_code=code) gate = rule_gate.check( - side=side, action=ins.get("action"), qty=qty, price=day_ctx.get("price"), + side=side, action=ins.get("action"), qty=qty, + price=(max(float(day_ctx.get("price") or 0), float(d["limit_price"])) + if side == "buy" and d.get("qmt_price_monitor") else day_ctx.get("price")), ctx={"ts_code": code, "position": pos, "day": {**day_ctx, "ma5": ma5}, "params": {**exec_prm, "sector_source_ready": view["sector_ready"]}, "caps": caps, @@ -329,6 +333,7 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: # 只告警不拦截, 告警随 hard_numbers 落进评审账本。 "stop_price": stock_params.get(code, {}).get("stop_price"), "is_command": bool(prog.get("is_command")), + "qmt_price_monitor": bool(d.get("qmt_price_monitor")), # 决策系统昨夜对该股的定性, 由择时应答带回 (退实现B 时为 None) "y_signal": d.get("y_signal")}}) diff --git a/app/services/param_store.py b/app/services/param_store.py index 70fac40..bff53b8 100644 --- a/app/services/param_store.py +++ b/app/services/param_store.py @@ -264,6 +264,7 @@ DESC = { "PMS_SECTOR_MAX_RATIO": "同行业最大占总仓比例 (硬拦截)", "PMS_EXEC_WINDOW_TDAYS": "任务命令默认执行窗口 (交易日)", "PMS_EXEC_IMPL": "择时实现: B=内置保守择时 / A=委托决策系统按凌晨结论给执行区间 (不可用自动退B)", + "PMS_EXEC_QMT_PRICE_MONITOR": "限价直接提交QMT等待成交,PMS保留数量、资金及用户命令检查", "PMS_EXEC_API_BASE": "择时实现A接口根地址; 留空=沿用 PMS_JUDGE_API_BASE", "PMS_EXEC_PATH": "择时接口路径 (bionic 侧 /api/intraday/pms_exec)", "PMS_EXEC_TIMEOUT_SEC": "择时咨询超时 (秒), 超时本轮退实现B", diff --git a/app/web/static/index.html b/app/web/static/index.html index 2e76767..7da68e1 100644 --- a/app/web/static/index.html +++ b/app/web/static/index.html @@ -612,7 +612,7 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}