diff --git a/plan_review.py b/plan_review.py index 652b1c1..430e88b 100644 --- a/plan_review.py +++ b/plan_review.py @@ -117,16 +117,35 @@ def cap_bucket(day: str) -> dict[str, str]: return dict(zip(df["k"], df["cap"].astype(str))) +def _roster_pick(rows, tiers, mode, n) -> list[str]: + """从名册行还原 PMS 当天交付的生产名单(纯函数,可离线测)。 + + 档位白名单是一道独立的门、与排序模式无关(PMS 任何模式下都先按它过滤主榜行再截断),所以 + 两种模式都先过滤、再定序:upstream 按上游名次 r 升序,否则按分数 s 降序,取前 n。tiers 为空 + 表示不按档过滤。返回前缀式代码列表。""" + main = [r for r in rows if str(r.get("b") or "main") == "main"] + tset = {str(t).strip() for t in (tiers or []) if str(t).strip()} + if tset: + main = [r for r in main if str(r.get("t") or "") in tset] + if mode == "upstream": + main.sort(key=lambda r: r.get("r") if r.get("r") is not None else 1 << 60) + else: + main.sort(key=lambda r: -(r.get("s") or 0)) + return [common.to_prefix(str(r["c"]).strip()) for r in main[:n] if r.get("c")] + + def pms_roster(day: str) -> tuple[list[str], str]: - """PMS 当日拿到的生产名单。返回 (代码, 注记)。序按名册元数据 rank_mode 还原当天真正交付的那份 - (2026-09-17 工作包乙):upstream(上游质地序)按上游名次升序取前 N;否则按旧口径(档位强传导、 - 分数降序)取前 N。""" + """PMS 当日拿到的生产名单。返回 (代码, 注记)。档位白名单(PMS_PLAN_TIERS,与排序模式无关的独立门) + 两种模式下都先过滤,再按名册元数据 rank_mode 定序(2026-09-17 工作包乙):upstream 按上游名次升序, + 否则按分数降序。白名单从参数表读,缺行退回"强传导"(与持仓管理系统 settings 初值一致),空串不过滤。""" try: df = db.read_mysql( "pms", "SELECT roster_json, meta_json, fetched_at FROM pms_plan_snapshot " "WHERE plan_date = %s ORDER BY id DESC LIMIT 1", (day,)) n_df = db.read_mysql( "pms", "SELECT param_value FROM pms_runtime_param WHERE param_key='PMS_PLAN_TOP_N'") + t_df = db.read_mysql( + "pms", "SELECT param_value FROM pms_runtime_param WHERE param_key='PMS_PLAN_TIERS'") except Exception as e: # noqa: BLE001 # 异常原文会被印进周报的逐日注记。只进日志,不进报告。 print(f" (PMS 计划快照读取失败: {e!r})") @@ -134,21 +153,18 @@ def pms_roster(day: str) -> tuple[list[str], str]: if df.empty: return [], "PMS 无当日快照,生产名单退为桥重算" n = int(n_df.iloc[0, 0]) if not n_df.empty else 30 + tiers_raw = str(t_df.iloc[0, 0]) if not t_df.empty else "强传导" # 参数表无此行时退回 PMS settings 初值 + tiers = [x.strip() for x in tiers_raw.split(",") if x.strip()] roster = json.loads(df.iloc[0]["roster_json"] or "[]") try: meta = json.loads(df.iloc[0]["meta_json"] or "{}") except (TypeError, ValueError): meta = {} - main_rows = [r for r in roster if str(r.get("b") or "main") == "main"] - if meta.get("rank_mode") == "upstream": - # 上游质地序:PMS 按上游名次升序取前 N(不再按强传导过滤,与 PMS 的 select_candidates 同口径) - main_rows.sort(key=lambda r: r.get("r") if r.get("r") is not None else 1 << 60) - note = f"PMS 快照名册(序按名册元数据 rank_mode=upstream 还原,上游名次升序,N={n})" - else: - main_rows = [r for r in main_rows if str(r.get("t") or "") == "强传导"] - main_rows.sort(key=lambda r: -(r.get("s") or 0)) - note = f"PMS 快照名册(N={n} 为现值,历史 N 不可还原)" - return [common.to_prefix(str(r["c"]).strip()) for r in main_rows[:n] if r.get("c")], note + mode = meta.get("rank_mode") + codes = _roster_pick(roster, tiers, mode, n) + seq = "上游名次升序" if mode == "upstream" else "分数降序" + white = "、".join(tiers) if tiers else "不过滤" + return codes, f"PMS 快照名册(档位白名单 {white},序按 rank_mode={mode or '(无)'}→{seq},N={n} 为现值)" def _uniq_codes(values) -> list[str]: diff --git a/test_plan_review_source.py b/test_plan_review_source.py index d5a07fb..1888e06 100644 --- a/test_plan_review_source.py +++ b/test_plan_review_source.py @@ -20,6 +20,7 @@ import pandas as pd # noqa: E402,F401 —— 只为触发真 pandas 导入,pl sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import common # noqa: E402 import config # noqa: E402 import plan # noqa: E402 import plan_review # noqa: E402 @@ -76,11 +77,29 @@ def main(): finally: config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect = old_dir, old_src, old_collect + # 复审退回修补 (2026-09-17): pms_roster 的档位白名单是独立门, 两种模式都先过滤再定序; 空白名单不过滤。 + roster = [ + {"c": "600000.SH", "b": "main", "t": "强传导", "s": 90, "r": 3}, + {"c": "000001.SZ", "b": "main", "t": "弱传导", "s": 100, "r": 1}, + {"c": "600519.SH", "b": "main", "t": "强传导", "s": 80, "r": 2}, + {"c": "000002.SZ", "b": "observe", "t": "强传导", "s": 70, "r": 1}, + ] + up = plan_review._roster_pick(roster, ["强传导"], "upstream", 10) + t("upstream: 先过强传导再按名次升序 (600519 r=2 在 600000 r=3 前; 弱传导与观察档不入)", + up == [common.to_prefix("600519.SH"), common.to_prefix("600000.SH")], up) + old = plan_review._roster_pick(roster, ["强传导"], "score", 10) + t("旧模式: 先过强传导再按分数降序 (600000 s=90 在 600519 s=80 前)", + old == [common.to_prefix("600000.SH"), common.to_prefix("600519.SH")], old) + nofilter = plan_review._roster_pick(roster, [], "upstream", 10) + t("白名单为空: 不按档过滤 (弱传导 000001 r=1 也入且在最前, 主榜三只全在)", + nofilter[0] == common.to_prefix("000001.SZ") and len(nofilter) == 3, nofilter) + print("-" * 70) if FAILED: print("FAILED %d: %s" % (len(FAILED), "; ".join(FAILED))) return 1 - print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧") + print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧 / " + "pms_roster 档位白名单先过滤再按 rank_mode 定序") return 0