diff --git a/.env.example b/.env.example index 9d0e953..9b31b27 100644 --- a/.env.example +++ b/.env.example @@ -100,6 +100,10 @@ MONGO_DB=stock_predictions # CARD_SORT_BY_QUALITY=1 # 候选卡内序先质地带;0=回"先判决再涨幅" # REVIEW_TARGETS_AXIS=both # 评析名单=原始分前100 ∪ 新序前50;score=只按原始分前100(旧) +# --- 周报底本(2026-09-17 拍板,工作包丙):snapshot=每个计划日先读当天落盘快照(当天真出过的那份), +# 读不出才回落现算;live=回旧做法每天现算(会把晚到的评析回头算进当天名单)--- +# PLAN_REVIEW_SOURCE=snapshot + # --- 入池口径(默认与 PMS 候选一致:强传导主榜前 20;改动记得与 PMS 页面的 # PMS_PLAN_TOP_N / PMS_PLAN_TIERS 保持一致)--- # POOL_GROUP_CODE=AKG_PLAN diff --git a/config.py b/config.py index 8cb6b73..96d9e4d 100644 --- a/config.py +++ b/config.py @@ -249,6 +249,10 @@ POOL_RANK_KEEP_OLD = os.environ.get("POOL_RANK_KEEP_OLD", "0") == "1" # 评析目标名单排序轴(2026-09-17):both(默认)= 原始分前一百 ∪ 新序前五十的并集,评析覆盖与排序轴 # 解耦(新被指向的公司先有报告,好公司报告保持新鲜);score = 只按原始分前一百(逐字回旧)。 REVIEW_TARGETS_AXIS = os.environ.get("REVIEW_TARGETS_AXIS", "both").strip().lower() +# 周报底本(2026-09-17 拍板做,工作包丙):snapshot(默认)= 每个计划日先读当天落盘的 JSON 快照全量行, +# 快照不存在或读不出才回落现算;live = 回旧做法,每个计划日用 plan.collect 现算。现算用的是跑周报那一刻 +# 的评析报告(查询不按日期截断),质地成主轴后会把晚到的报告回头算进当天名单,四周复核要的是当天真出过的那份。 +PLAN_REVIEW_SOURCE = os.environ.get("PLAN_REVIEW_SOURCE", "snapshot").strip().lower() # 低优先入池切片:让"门槛全过但缺吸筹评分"的关注票入池、当晚获得评分,否则复盘缺数据是环状依赖。 # 0 = 不开(默认);受 POOL_MAX 约束,只改 Mongo 池成分,PMS 不读 Mongo 池。 POOL_WATCH_SLICE = int(os.environ.get("POOL_WATCH_SLICE", "0")) diff --git a/docs/交接_2026-09-10.md b/docs/交接_2026-09-10.md index 425387c..c8e5521 100644 --- a/docs/交接_2026-09-10.md +++ b/docs/交接_2026-09-10.md @@ -182,6 +182,16 @@ ssh -p 2280 factor@192.168.16.155 'docker exec akg_factor_bridge date "+%F %T %Z 每个仓库各有一份,记的是每一条影响下单或候选单的规则改动、依据、预期、复核日期。 -- 选股系统:`akg-factor-bridge/docs/复盘决定台账.md`,最新到第 057 条 +- 选股系统:`akg-factor-bridge/docs/复盘决定台账.md`,最新到第 063 条 - 持仓管理系统:`tradingSystem/docs/复盘决定台账.md`,第 001 到 005 条 - 择时决策系统:`bionic_trader/docs/复盘决定台账.md`,第 001 到 004 条 + +## 十一、2026-09-17 补记:排序主轴换成公司质地 + +**改了什么。** 选股计划的排序主轴从传导档位换成公司质地——三份复盘读数一致显示传导越强超额越负,传导分的公式奖励的正是被证明为负的"未动补涨"。传导降为候选卡门槛一(雷达门)与同一质地档内的次级键,不再决定谁排前面。方案与规格书见 `docs/选股计划调整方案_2026-09-17.md` 与 `docs/上线与协同阶段工作方案与规格书_2026-09-17.md`(四仓库同步),台账第 063 条。 + +**下游看到什么。** 主榜、观察档、候选单的名次先按公司质地(好、中、无评析、差四带)排,同带内被传导指向的在前。每行多四个键:`quality_band`(质地带,好 3、中 2、无评析 1、差 0)、`rank_axis`(quality 或 transmission)、`pointed`(是否被传导指向)、`rank_old`(旧序名次,供四周对照)。原有的 `score`、`score_adj`、`tier` 值与含义不变。逐票对账接口 `/plan/verdict` 的"主榜第 N 名"仍按原始分序(应答带 `rank_axis: transmission` 说明),与计划里按质地排的 `rank` 会不一致,属正常。持仓管理系统请注意前缀效应:质地差的强传导票可能落到它要的三百只前缀之外,与"质地差降关注"同向,不是故障。 + +**怎么回退。** 四个开关全设旧值即逐字回旧,不用回代码、不用重启:`PLAN_RANK_AXIS=transmission`、`CARD_SORT_BY_QUALITY=0`、`POOL_SOURCE=tier`、`REVIEW_TARGETS_AXIS=score`。持仓管理系统那边靠上游的 `rank_axis` 自门控,上游一回退它自动回旧。 + +**读数在哪。** 评析目标名单在 153 库的 `t_akg_review_targets` 表;周报第一节的"主榜新序前二十"与"主榜旧序前二十"两份对照名单,四周(10-16 前后)按超额与跑赢比例决定保留还是切回。上线次日的六处判收读数见《选股计划调整方案_2026-09-17》第六节。 diff --git a/plan.py b/plan.py index 1df296e..c706f84 100644 --- a/plan.py +++ b/plan.py @@ -170,6 +170,26 @@ def _rerank(ranked, cards: dict, has_tier: bool): return _rerank_by_quality(ranked, cards, config.PLAN_QUALITY_BONUS) +def take_by_theme(items, n, theme_cap, theme_of, uncapped_no_theme): + """按主题限额截取——装配路与快照路的唯一实现(2026-09-17 修复必改一)。 + items 可迭代;theme_of(item) 回主题字符串、无主题回 None。有主题的每主题最多 theme_cap 条 + (0 不设限);无主题的:uncapped_no_theme 为真时免限额、不计数,为假时归"(无传导)"一个桶计数 + (旧轴原样)。取够 n 条即停。两条路对"已启动成员"是否算进所在主题的分歧,靠 theme_of 同源消除。""" + out, cnt = [], {} + for it in items: + th = theme_of(it) + exempt = uncapped_no_theme and not th + if not exempt: + bucket = th or "(无传导)" + if theme_cap and cnt.get(bucket, 0) >= theme_cap: + continue + cnt[bucket] = cnt.get(bucket, 0) + 1 + out.append(it) + if len(out) >= n: + break + return out + + def _val(series: pd.Series, k: str): v = series.get(k) return None if v is None or pd.isna(v) else float(v) @@ -520,25 +540,23 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10, main = _rerank(main, cards, True) obs = _rerank(obs, cards, False) + def _theme_of(k): + """一条记录的传导主题(与 _row 写 evidence 同源):传导视图有证据行取它,否则已启动成员用 + 已动成员视图补的主题,都没有回 None。必改一:装配路与快照路的主题取法从此一处同源, + 已启动成员两条路都算进所在主题。""" + e = ev.get(k) + if e: + return e[0] + c = cards.get(k) or {} + if c.get("started_source") == "moved_view" and c.get("theme"): + return c["theme"] + return None + def _pick(ranked: pd.Series, n: int, uncapped_no_theme: bool = False): - """分数从高到低取 n 条;每个传导主题最多 theme_cap 条(0=不设限)—— - 传导目标是环节级、同环节成员共享同一条证据,不限额会被少数环节刷屏。 - uncapped_no_theme(质地轴下为真):没有传导主题的行不计数、不受主题限额。质地序前列有不少 - 质地好的无传导票,若仍受五只上限,第六只起就被质地中的被指向票顶掉,主轴被限额悄悄推翻; - 无传导本就不是板块,限额防的是单板块刷屏。有主题的行照旧每主题最多 theme_cap 条。""" - out, cnt = [], {} - for k, s in ranked.items(): - e = ev.get(k) - theme = e[0] if e else "(无传导)" - exempt = uncapped_no_theme and e is None - if not exempt: - if theme_cap and cnt.get(theme, 0) >= theme_cap: - continue - cnt[theme] = cnt.get(theme, 0) + 1 - out.append((k, s)) - if len(out) >= n: - break - return out + """主榜/观察档截取:调模块级 take_by_theme,主题取法用 _theme_of(含已动成员视图补充), + 与快照路 pick_rows 同一套规则。质地轴下无传导行免主题限额(限额防的是单板块刷屏,无传导不是板块)。""" + return take_by_theme(ranked.items(), n, theme_cap, + lambda kv: _theme_of(kv[0]), uncapped_no_theme) def _row(rank: int, k: str, s: float, with_tier: bool) -> dict: e = ev.get(k) @@ -711,7 +729,8 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10, "gate_on": bool(config.ENABLE_TRACK_GATE), "encoding": "主榜分=200+传导档位×20+组内分(还没热、还便宜);" "观察档分=100+0.6z(传导)+0.4z(−热度);" - "名次按质地带优先,传导只在带内作次级键(开关 PLAN_RANK_AXIS)", + "名次按开关 PLAN_RANK_AXIS:quality 时质地带优先、传导只在带内作次级键," + "transmission 时按 09-14 的质地调整分序", # ---- 候选卡(2026-09-02):分数与档位之外的另一份产物,不受 top / theme_cap 裁剪 ---- "card_params": {"start_pct": config.CARD_START_PCT, "require_started": config.CARD_REQUIRE_STARTED, @@ -1020,21 +1039,13 @@ def pick_rows(rows, n: int, theme_cap: int, uncapped_no_theme=None) -> list: uncapped_no_theme(2026-09-17):质地轴下没有传导主题的行免限额、不计数(与 _pick 同口径)。 缺省 None 时从行的 rank_axis 推断(质地轴即为真),周报重建旧序时显式传 False。 + 必改一:与 collect 里的 _pick 调同一个模块级 take_by_theme,主题取行上的 evidence.theme(含已动 + 成员视图补的),已启动成员两条路都算进所在主题,同一天 md 与接口给出同一份前二十。 """ if uncapped_no_theme is None: uncapped_no_theme = bool(rows) and (rows[0].get("rank_axis") == "quality") - out, cnt = [], {} - for r in rows: - theme = ((r.get("evidence") or {}).get("theme")) or "(无传导)" - exempt = uncapped_no_theme and theme == "(无传导)" - if not exempt: - if theme_cap and cnt.get(theme, 0) >= theme_cap: - continue - cnt[theme] = cnt.get(theme, 0) + 1 - out.append(r) - if len(out) >= n: - break - return out + return take_by_theme(rows, n, theme_cap, + lambda r: ((r.get("evidence") or {}).get("theme")) or None, uncapped_no_theme) def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10, diff --git a/plan_review.py b/plan_review.py index c7dc7b5..2cc68c6 100644 --- a/plan_review.py +++ b/plan_review.py @@ -358,6 +358,35 @@ def _pricing_spread_md(groups_tbl: pd.DataFrame) -> str: return "\n".join(lines) +def _read_review_snapshot(day: str): + """读当日计划快照,整理成与 plan.collect 返回同形的字典(_full 放全量 main/observe,顶层带 + candidates/watch)。快照不存在、坏 JSON、或 main 不是列表,回 None,由调用方回落到现算。""" + path = os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{day}.json") + try: + with open(path, encoding="utf-8") as f: + snap = json.load(f) + except (OSError, ValueError): + return None + main = snap.get("main") + if not isinstance(main, list): + return None + return {"date": snap.get("date") or day, + "_full": {"main": main, "observe": snap.get("observe") or []}, + "candidates": snap.get("candidates") or [], "watch": snap.get("watch") or []} + + +def _plan_for_review(day: str): + """周报某个计划日的底本(工作包丙,2026-09-17 拍板):默认先读当天落盘快照(当天真出过的那份), + 快照读不出才回落现算;开关 PLAN_REVIEW_SOURCE=live 直接现算。现算用跑周报那一刻的评析报告、 + 查询不按日期截断,质地成主轴后会把晚到的报告回头算进当天名单,四周复核要的是当天真出过的那份。 + 返回 (data, 底本名)。""" + if config.PLAN_REVIEW_SOURCE != "live": + snap = _read_review_snapshot(day) + if snap is not None: + return snap, "快照" + return plan.collect(day, top=5000, obs_top=5000, theme_cap=0), "现算" + + def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str, with_cards: bool = True) -> dict: days_plan = plan_dates(since, until) @@ -371,9 +400,9 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str rows, notes = [], [] for day in days_plan: try: - data = plan.collect(day, top=5000, obs_top=5000, theme_cap=0) + data, benben = _plan_for_review(day) except Exception as e: # noqa: BLE001 - notes.append(f"{day}: 计划重算失败 {e!r}") + notes.append(f"{day}: 计划取数失败 {e!r}") continue full = data.get("_full") or {} main_rows, obs_rows = full.get("main", []), full.get("observe", []) @@ -510,7 +539,7 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str if s: rows.append({"date": day, "h": h, "list": lst_name, "group": f"行业催化={label}", "regime_post": regime_post, "regime_pre": regime_pre, **s}) - notes.append(f"{day}: 主榜 {len(main_codes)} 观察 {len(obs_rows)} 候选 {len(cands)} " + notes.append(f"{day}: 底本:{benben};主榜 {len(main_codes)} 观察 {len(obs_rows)} 候选 {len(cands)} " f"关注 {len(watch)} 生产 {len(prod)}({prod_note});" f"账本 机器通过 {len(ledger['机器通过名单'])} 人批 {len(ledger['人批名单'])} " f"人拒 {len(ledger['人拒名单'])}({ledger_note});择时看多 {len(bullish)}" diff --git a/test_plan_rank_axis.py b/test_plan_rank_axis.py index e867a09..04e6152 100644 --- a/test_plan_rank_axis.py +++ b/test_plan_rank_axis.py @@ -186,11 +186,42 @@ def test_10_pool_keep_old(): config.POOL_MAX = old_max +def test_11_started_member_counts_theme(): + print("[case11 必改一:已启动成员两条路都算进所在主题、受限额]") + # 六条同主题"锂电",前五条走传导视图证据行,第六条无证据行、由候选卡 moved_view 补主题 + ev = {f"c{i}": ("锂电", 2, 0.3) for i in range(5)} + cards = {"c5": {"started_source": "moved_view", "theme": "锂电"}} + + def _theme_of(kv): # 与 collect 里 _theme_of 同源:先传导视图证据行,再已动成员视图补充 + k = kv[0] + e = ev.get(k) + if e: + return e[0] + c = cards.get(k) or {} + if c.get("started_source") == "moved_view" and c.get("theme"): + return c["theme"] + return None + + items = [(f"c{i}", 200.0 - i) for i in range(6)] + picked = plan.take_by_theme(items, 20, 5, _theme_of, uncapped_no_theme=True) + t("装配路:同主题只放五条(含 moved_view 补主题那条算进主题)", len(picked) == 5, len(picked)) + rows = [{"code": f"c{i}", "evidence": {"theme": "锂电"}, "rank_axis": "quality"} for i in range(6)] + t("快照路:同样六条只放五条", len(plan.pick_rows(rows, 20, 5)) == 5, len(plan.pick_rows(rows, 20, 5))) + + +def test_12_no_theme_by_axis(): + print("[case12 必改一:无主题行两轴各归各——质地轴免限额,旧轴归无传导桶计数]") + items = [(f"n{i}", 200.0 - i) for i in range(6)] + none_theme = lambda kv: None # noqa: E731 —— 六条都无主题 + t("质地轴(uncapped 真)放六条", len(plan.take_by_theme(items, 20, 5, none_theme, True)) == 6) + t("旧轴(uncapped 假)归无传导桶、限额五条", len(plan.take_by_theme(items, 20, 5, none_theme, False)) == 5) + + def main(): for fn in (test_1_replay_transmission, test_2_bands_order, test_3_within_band, test_4_deterministic, test_5_stale_demotes, test_6_pool_rank_source, test_7_review_targets_union, test_8_card_sort_toggle, test_9_theme_cap_exemption, - test_10_pool_keep_old): + test_10_pool_keep_old, test_11_started_member_counts_theme, test_12_no_theme_by_axis): fn() print("-" * 70) if FAILED: diff --git a/test_plan_review_source.py b/test_plan_review_source.py new file mode 100644 index 0000000..d5a07fb --- /dev/null +++ b/test_plan_review_source.py @@ -0,0 +1,88 @@ +"""周报底本改读当日快照(工作包丙,2026-09-17 拍板):先读快照、无快照回落现算、开关 live 回旧。 + +离线,不连库。跑法:python3 test_plan_review_source.py,预期最后一行是 ALL OK。 +""" +import json +import os +import sys +import tempfile +import types + +# 离线不连库:缺席的驱动打空壳,pandas 本机装得上用真的。 +for _n in ("psycopg", "pymysql", "dotenv"): + if _n not in sys.modules: + try: + __import__(_n) + except Exception: # noqa: BLE001 + sys.modules[_n] = types.ModuleType(_n) + +import pandas as pd # noqa: E402,F401 —— 只为触发真 pandas 导入,plan_review 要用 + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +import config # noqa: E402 +import plan # noqa: E402 +import plan_review # noqa: E402 + +FAILED = [] + + +def t(name, cond, extra=""): + print((" ok " if cond else " FAIL ") + name + ((" " + str(extra)) if not cond else "")) + if not cond: + FAILED.append(name) + + +def main(): + day = "2026-09-16" + snap = {"date": day, + "main": [{"code": "600000.SH", "rank": 1}, {"code": "000001.SZ", "rank": 2}], + "observe": [{"code": "600519.SH", "rank": 1}], + "candidates": [{"code": "600000.SH"}], "watch": [{"code": "000001.SZ"}]} + old_dir, old_src, old_collect = config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect + calls = {"n": 0} + + def fake_collect(d, **kw): + calls["n"] += 1 + return {"date": d, "_full": {"main": [{"code": "X", "rank": 1}], "observe": []}, + "candidates": [], "watch": []} + + try: + with tempfile.TemporaryDirectory() as tmp: + config.PLAN_SNAPSHOT_DIR = tmp + plan.collect = fake_collect + with open(os.path.join(tmp, f"plan_{day}.json"), "w", encoding="utf-8") as f: + json.dump(snap, f) + + # 1. 有快照:读快照,不现算 + config.PLAN_REVIEW_SOURCE = "snapshot" + data, benben = plan_review._plan_for_review(day) + t("有快照时底本为快照", benben == "快照", benben) + t("_full.main 来自快照", [r["code"] for r in data["_full"]["main"]] == ["600000.SH", "000001.SZ"]) + t("observe 来自快照", [r["code"] for r in data["_full"]["observe"]] == ["600519.SH"]) + t("candidates 来自快照", [r["code"] for r in data["candidates"]] == ["600000.SH"]) + t("读快照不触发现算", calls["n"] == 0, calls["n"]) + + # 2. 无快照:回落现算 + _d2, benben2 = plan_review._plan_for_review("2026-09-15") + t("无快照时底本为现算", benben2 == "现算", benben2) + t("回落走了 plan.collect 一次", calls["n"] == 1, calls["n"]) + + # 3. 开关 live:即使有快照也现算,逐字回旧 + config.PLAN_REVIEW_SOURCE = "live" + _d3, benben3 = plan_review._plan_for_review(day) + t("live 时底本为现算(有快照也不读)", benben3 == "现算", benben3) + t("live 又现算一次", calls["n"] == 2, calls["n"]) + finally: + config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect = old_dir, old_src, old_collect + + print("-" * 70) + if FAILED: + print("FAILED %d: %s" % (len(FAILED), "; ".join(FAILED))) + return 1 + print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧") + return 0 + + +if __name__ == "__main__": + sys.exit(main())