diff --git a/DEVLOG.md b/DEVLOG.md index e580484..d2c4fbe 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -2895,3 +2895,8 @@ PMS最终代码56db0fd已推送,155 web/WS/beat为execution-2b1c447,worker 按最新反馈,顶部在当日历史分位前增加带正负号的净额,红色表示流入、绿色表示流出。悬浮框固定60个交易日,取消周期切换、近5日汇总及分位,删除基金数量、参考样本、规模比例、连续天数和说明文案。保留日期、当日净申购估算、当日分位、趋势与原详情入口。 开发机验证1440/390/320布局、60根柱和无周期按钮、指定文案移除、待补及详情交互,无横向溢出或页面错误。仅更新独立原型及设计文档,应用与线上服务未变。最新图片与边界见[当前设计](docs/ETF每日资金与行业对比原型_2026-11-06.md)。 + + +## 2026-11-06 宽基每日资金与信号覆盖 + +按用户确认原型接入每日资金,删除运营日报页面入口,补齐当天信号读取与浏览。10:15实际源5/99/105/0条,对应旧快照5/38/40/0条,定位全局及分类上限和前端过滤。仅拟发布155页面与独立观测,交易执行进程保持。实现及验收状态见[发布接续](docs/宽基每日资金与信号覆盖发布_2026-11-06.md)。 diff --git a/app/core/etf_contract/DAILY_PROVENANCE.json b/app/core/etf_contract/DAILY_PROVENANCE.json new file mode 100644 index 0000000..00af52c --- /dev/null +++ b/app/core/etf_contract/DAILY_PROVENANCE.json @@ -0,0 +1,12 @@ +{ + "repository": "main-trend-factor-system", + "path": "src/etf", + "sha256": { + "daily_flow.py": "9ce80d4f31227414eea84c328bf37cc3a095d322f37a32562d25bfdd4801cb7f", + "daily_transport.py": "91096a090721205041812d8618e88efc3ff0dc8510afab653c7055941d9ef2c1", + "publisher.py": "ec62ad7347bbe6c5278c13e5d7e3f413704203d820fb4c4bf28f49407a571437" + }, + "dependencies": "Existing pinned consumer, signals, timing; Python standard library only", + "copied_at": "2026-11-06", + "runtime_verification": "2026-11-06 10:34 CST: exact hashes matched mtf-etf-155-worker-batch-1 /app/src/etf; existing consumer/signals/timing also matched" +} diff --git a/app/core/etf_contract/daily_flow.py b/app/core/etf_contract/daily_flow.py new file mode 100644 index 0000000..e033046 --- /dev/null +++ b/app/core/etf_contract/daily_flow.py @@ -0,0 +1,269 @@ +"""Daily observations: ETF net subscription estimates and THS industry flows. + +The two accounting bases never share a total. All rolling windows use actual +trading dates and a fixed cohort; missing observations never become zero. +""" +from __future__ import annotations + +from decimal import Decimal, InvalidOperation +import hashlib +import json +from statistics import median + + +CONTRACT = "daily-flow-1.0" +ETF_BASIS = "ETF_SHARE_CHANGE_TIMES_NAV_ESTIMATE" +INDUSTRY_BASIS = "THS_INDUSTRY_NET_FLOW" +THRESHOLD = "daily-flow-reference-v0" + + +def decimal(value): + if value is None or isinstance(value, bool): + raise ValueError("MISSING_OR_INVALID_NUMBER") + try: + result = Decimal(str(value)) + except InvalidOperation: + raise ValueError("INVALID_NUMBER") from None + if not result.is_finite(): + raise ValueError("NONFINITE_NUMBER") + return result + + +def canonical(value): + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False) + + +def digest(value): + return hashlib.sha256(canonical(value).encode()).hexdigest() + + +def iso_day(value): + from datetime import date + text = str(value)[:10] + if len(text) == 8 and text.isdigit(): + text = f"{text[:4]}-{text[4:6]}-{text[6:]}" + return date.fromisoformat(text).isoformat() + + +def subscription(current, previous, day, previous_day): + """Source total_share=10,000 units, total_size=10,000 CNY, NAV=CNY/unit. + + This estimates net creation/redemption asset value, including in-kind + baskets. It is NOT actual cash transferred, secondary-market turnover, + gross subscriptions/redemptions, or cash entering the stock market. + """ + result = dict(trade_date=day, net_flow_yuan=None, previous_size_yuan=None, + flow_rate=None, status="MISSING_SOURCE", corporate_action_status="UNVERIFIED") + if current is None or previous is None: + return result + if (current.get("ts_code") != previous.get("ts_code") + or iso_day(current["trade_date"]) != day + or iso_day(previous["trade_date"]) != previous_day): + raise ValueError("SHARE_DATE_OR_IDENTITY_MISMATCH") + try: + shares, old_shares = decimal(current["total_share"]), decimal(previous["total_share"]) + nav, old_nav = decimal(current["nav"]), decimal(previous["nav"]) + size, old_size = decimal(current["total_size"]), decimal(previous["total_size"]) + if min(shares, old_shares, nav, old_nav, size, old_size) <= 0: + return dict(result, status="INVALID_SOURCE") + except (ValueError, KeyError): + return dict(result, status="INVALID_SOURCE") + # NAV is rounded by the provider. This is an identity/units sanity bound, + # not an assertion that the reported size is exactly NAV * rounded shares. + if any(abs(s * n - a) / a > Decimal("0.001") + for s, n, a in ((shares, nav, size), (old_shares, old_nav, old_size))): + return dict(result, status="NAV_SIZE_INCONSISTENT") + share_ratio, nav_ratio = shares / old_shares, nav / old_nav + if (abs(share_ratio - 1) > Decimal("0.2") and abs(nav_ratio - 1) > Decimal("0.2") + and abs(share_ratio * nav_ratio - 1) < Decimal("0.15")): + return dict(result, status="POSSIBLE_SHARE_UNIT_EVENT", corporate_action_status="REVIEW_REQUIRED") + flow = (shares - old_shares) * Decimal(10000) * nav + previous_size = old_size * Decimal(10000) + return dict(result, status="ESTIMATED", net_flow_yuan=float(flow), + previous_size_yuan=float(previous_size), flow_rate=float(flow / previous_size), + share_change_units=float((shares - old_shares) * Decimal(10000)), + nav_yuan=float(nav), assumption="NO_UNIDENTIFIED_SHARE_UNIT_EVENT") + + +def aggregate(records, expected_codes, day): + expected = tuple(sorted(set(expected_codes))) + selected = [records.get(code) for code in expected] + valid = [r for r in selected if r and r.get("net_flow_yuan") is not None] + total = sum((decimal(r["net_flow_yuan"]) for r in valid), Decimal(0)) + base = sum((decimal(r["previous_size_yuan"]) for r in valid), Decimal(0)) + complete = bool(expected) and len(valid) == len(expected) + return dict(trade_date=day, status="ESTIMATED" if complete else "INCOMPLETE_COHORT", + net_flow_yuan=float(total) if complete else None, + observed_partial_net_flow_yuan=float(total) if valid else None, + previous_size_yuan=float(base) if complete else None, + flow_rate=float(total / base) if complete and base else None, + member_count=len(expected), observed_member_count=len(valid), + missing_codes=[c for c, r in zip(expected, selected) if not r or r.get("net_flow_yuan") is None]) + + +def reference(value, history): + values = [decimal(v) for v in history if v is not None][-252:] + result = dict(n=len(values), percentile=None, percentile252=None, + tail="UNAVAILABLE" if value is None else "INSUFFICIENT_REFERENCE", + threshold_version=THRESHOLD, calibrated=False, + percentile_semantics="MIDRANK_DESCRIPTIVE_NOT_PROBABILITY") + if value is None or not values: + return result + x = decimal(value) + less, equal = sum(v < x for v in values), sum(v == x for v in values) + rank = 100 * (less + equal / 2) / len(values) + result.update(percentile=rank, percentile252=rank if len(values) == 252 else None) + if len(values) < 60: + return result + # Use both ends of a tie's rank interval so a constant history cannot be + # labelled extreme merely because weak ECDF maps ties to 100%. + lower, upper = 100 * less / len(values), 100 * (less + equal) / len(values) + result["tail"] = "NONE" + if x > 0 and lower >= 95: + result["tail"] = "INFLOW_TOP_1PCT" if len(values) == 252 and lower >= 99 else "INFLOW_TOP_5PCT" + elif x < 0 and upper <= 5: + result["tail"] = "OUTFLOW_BOTTOM_1PCT" if len(values) == 252 and upper <= 1 else "OUTFLOW_BOTTOM_5PCT" + return result + + +def _window(series, end, length, basis): + if end + 1 < length: + return None, None + rows = series[end + 1 - length:end + 1] + if any(r.get("net_flow_yuan") is None for r in rows): + return None, None + value = sum((decimal(r["net_flow_yuan"]) for r in rows), Decimal(0)) + base = rows[0].get("previous_size_yuan") + rate = float(value / decimal(base)) if basis == ETF_BASIS and base is not None and decimal(base) > 0 else None + return float(value), rate + + +def summarize(series, basis): + if not series: + raise ValueError("EMPTY_DAILY_SERIES") + dates = [iso_day(r["trade_date"]) for r in series] + if dates != sorted(set(dates)): + raise ValueError("DATES_MUST_BE_UNIQUE_AND_ORDERED") + last = series[-1] + value = last.get("net_flow_yuan") + direction = "UNKNOWN" if value is None else "INFLOW" if value > 0 else "OUTFLOW" if value < 0 else "FLAT" + field = "flow_rate" if basis == ETF_BASIS else "net_flow_yuan" + daily_ref = reference(last.get(field), [r.get(field) for r in series[:-1]]) + result = dict(latest=last, direction=direction, reference=daily_ref, periods={}, + reference_basis="NET_FLOW_DIVIDED_BY_PREVIOUS_SIZE" if basis == ETF_BASIS else "OWN_SECTOR_NET_FLOW_AMOUNT", + daily_series=series[-60:], model_status="uncalibrated", calibration_status="PROVISIONAL") + for length in (5, 20, 60): + amount, rate = _window(series, len(series)-1, length, basis) + history = [_window(series, i, length, basis)[1 if basis == ETF_BASIS else 0] + for i in range(length-1, len(series)-1)] + valid_days = sum(r.get("net_flow_yuan") is not None for r in series[-length:]) + result["periods"][str(length)] = dict(net_flow_yuan=amount, flow_rate=rate, + expected_days=length, valid_days=valid_days, + complete=amount is not None, reference=reference(rate if basis == ETF_BASIS else amount, history)) + current5, _ = _window(series, len(series)-1, 5, basis) + previous5, _ = _window(series, len(series)-6, 5, basis) if len(series) >= 10 else (None, None) + result["change_in_5d_average_yuan_per_day"] = ((current5-previous5)/5 + if current5 is not None and previous5 is not None else None) + streak = 0 + if direction in ("INFLOW", "OUTFLOW"): + for row in reversed(series): + flow = row.get("net_flow_yuan") + if flow is None or not ((flow > 0 and direction == "INFLOW") or (flow < 0 and direction == "OUTFLOW")): + break + streak += 1 + result["same_direction_trading_days"] = streak + result["streak_is_lower_bound"] = streak == len(series) and bool(streak) + label = "ETF净申购估算" if basis == ETF_BASIS else "行业资金净额(同花顺口径)" + if value is None: + headline = f"{dates[-1]}:{label}待补数据;不判定流入、流出或极端程度。" + else: + headline = f"{dates[-1]}:{label}{value/100000000:+.2f}亿元" + if basis == ETF_BASIS and last.get("flow_rate") is not None: + headline += f",占前日规模{last['flow_rate']*100:+.2f}%" + if daily_ref["percentile"] is not None: + headline += f",历史分位{daily_ref['percentile']:.1f}%(n={daily_ref['n']})" + tail_labels = {"INFLOW_TOP_5PCT":"流入前5%档", "OUTFLOW_BOTTOM_5PCT":"流出后5%档", + "INFLOW_TOP_1PCT":"流入前1%档", "OUTFLOW_BOTTOM_1PCT":"流出后1%档", + "INSUFFICIENT_REFERENCE":"样本不足,暂不判极端", "NONE":"未达到尾部档位"} + headline += ";"+tail_labels.get(daily_ref["tail"],"历史参照不足")+";未校准。" + result["conclusion"] = {"text":headline,"direction":direction,"extreme_code":daily_ref["tail"], + "is_calibrated":False} + return result + + +def build_report(*, dates, share_days, industry_rows, cohort, group_names=None): + """dates is a verified trading calendar, including previous boundary day. + + cohort maps ETF to tracking index. Every historical aggregate is recomputed + using exactly this cohort; archived market totals from other pools are not + concatenated. Industry values are already normalized to CNY by the adapter. + """ + dates = [iso_day(day) for day in dates] + if len(dates) < 2 or dates != sorted(set(dates)) or not cohort: + raise ValueError("VERIFIED_DATES_AND_NONEMPTY_COHORT_REQUIRED") + share_index = {} + for day in dates: + indexed = {} + for row in share_days.get(day, []): + code = row["ts_code"] + if iso_day(row["trade_date"]) != day or code in indexed: + raise ValueError("DUPLICATE_OR_WRONG_DATE_SHARE_ROW") + indexed[code] = row + share_index[day] = indexed + series = {code: [] for code in cohort} + groups = {key: sorted(code for code, group in cohort.items() if group == key) for key in sorted(set(cohort.values()))} + group_series = {key: [] for key in groups} + market = [] + for previous, day in zip(dates, dates[1:]): + rows = {code: subscription(share_index[day].get(code), share_index[previous].get(code), day, previous) for code in cohort} + for code, row in rows.items(): + series[code].append(row) + market.append(aggregate(rows, cohort, day)) + for group, codes in groups.items(): + group_series[group].append(aggregate(rows, codes, day)) + etfs = [{"subject_type":"ETF", "subject_id":code, "tracking_index":cohort[code], + **summarize(rows, ETF_BASIS)} for code, rows in sorted(series.items())] + # Detailed ETF daily history is kept in durable source facts. The public + # snapshot contains their current/rolling metrics; charts use group series. + for item in etfs: + item.pop("daily_series") + indexes = [{"subject_type":"INDEX", "subject_id":key, "name":(group_names or {}).get(key, key), + **summarize(rows, ETF_BASIS)} for key, rows in sorted(group_series.items())] + ind_index, names = {}, {} + for row in industry_rows: + day, code = iso_day(row["trade_date"]), str(row["industry_code"]) + if day not in dates[1:]: + continue + if (code, day) in ind_index: + raise ValueError("DUPLICATE_INDUSTRY_ROW") + value = row.get("net_flow_yuan") + value = float(decimal(value)) if value is not None else None + ind_index[code, day] = dict(trade_date=day, net_flow_yuan=value, + status="REPORTED" if value is not None else "MISSING_SOURCE") + names[code] = row.get("industry_name", code) + industries = [{"subject_type":"INDUSTRY", "subject_id":code, "name":names[code], + **summarize([ind_index.get((code, day), dict(trade_date=day, net_flow_yuan=None, + status="MISSING_SOURCE")) for day in dates[1:]], INDUSTRY_BASIS)} for code in sorted(names)] + for subjects in (indexes, industries): + valid = [r for r in subjects if r["latest"]["net_flow_yuan"] is not None] + for rank, row in enumerate(sorted(valid, key=lambda r:-r["latest"]["net_flow_yuan"]), 1): + row["rank_by_net_flow_desc"] = rank + row["rank_population"] = len(valid) + return dict(schema_version=1, contract_version=CONTRACT, trade_date=dates[-1], + publication_mode="observation", purpose="MARKET_OBSERVATION", trade_instruction=False, + model_status="uncalibrated", calibration_status="PROVISIONAL", threshold_version=THRESHOLD, + accounting_bases_combined=False, cohort_basis="AS_OF_REPORT_FIXED_COHORT", + historical_membership_reconstructed=False, cohort_sha256=digest(cohort), + historical_knowledge_basis="FINALIZED_SOURCE_AS_LOADED_NOT_INTRADAY_REPLAY", + expected_trading_dates=dates[1:], + etf={"flow_basis":ETF_BASIS, "amount_unit":"CNY", "cohort":cohort, + "corporate_action_status":"UNVERIFIED_EXCEPT_SUSPECT_EVENTS_EXCLUDED", + "interpretation":"ESTIMATED_NET_CREATION_REDEMPTION_ASSET_VALUE_NOT_CASH_OR_STOCK_MARKET_NET_INFLOW", + "market":{"subject_type":"MARKET", "subject_id":"BROAD_ETF", **summarize(market, ETF_BASIS)}, + "indexes":indexes, "etfs":etfs}, + industry={"flow_basis":INDUSTRY_BASIS, "amount_unit":"CNY", "taxonomy":"THS_INDUSTRY", + "interpretation":"PROVIDER_REPORTED_ORDER_FLOW_NOT_ETF_SUBSCRIPTION", + "total":None, "sectors":industries}, + limitations=["Reference tails are provisional descriptive observations, not calibrated probabilities.", + "ETF creation/redemption can be in kind; estimated flows are not literal cash movement.", + "Sector differences do not establish that the same investors moved money between sectors."]) diff --git a/app/core/etf_contract/daily_transport.py b/app/core/etf_contract/daily_transport.py new file mode 100644 index 0000000..395b0df --- /dev/null +++ b/app/core/etf_contract/daily_transport.py @@ -0,0 +1,165 @@ +"""Daily immutable facts use the existing two-Redis commit mechanism. + +DAILY_UPSERT revisions are deliberately distinct from intraday signal leases. +The consumer must supply the date it intends to display. +""" +from datetime import datetime, time, timedelta +import hashlib +import json + +from .daily_flow import CONTRACT, ETF_BASIS, INDUSTRY_BASIS, THRESHOLD, canonical, iso_day +from .signals import SHANGHAI, aware_time +from .publisher import PREPARE_LUA, COMMIT_LUA, LATEST_LUA, _text +from .consumer import _json + + +NAMESPACE = "mtf:funds:daily:observation:v1" +MAX_REPORT_BYTES = 8 * 1024 * 1024 +REVOKE_COMPLETE_LUA = """-- DAILY_REVOKE_COMPLETE_V1 +local old = redis.call('GET', KEYS[1]) +if not old then return 0 end +local v = cjson.decode(old) +if v.trade_date == ARGV[1] and tonumber(v.revision) < tonumber(ARGV[2]) then + return redis.call('DEL', KEYS[1]) +end +return 0 +""" + + +def validate_report(report): + day = iso_day(report["trade_date"]) + if (report.get("schema_version") != 1 or report.get("contract_version") != CONTRACT + or report.get("model_status") != "uncalibrated" or report.get("calibration_status") != "PROVISIONAL" + or report.get("purpose") != "MARKET_OBSERVATION" or report.get("publication_mode") != "observation" + or report.get("trade_instruction") is not False or report.get("accounting_bases_combined") is not False + or report.get("threshold_version") != THRESHOLD + or report.get("etf", {}).get("flow_basis") != ETF_BASIS + or report.get("industry", {}).get("flow_basis") != INDUSTRY_BASIS + or report.get("industry", {}).get("total") is not None + or report.get("valid_for_trade_date") != day): + raise ValueError("DAILY_CONTRACT_MISMATCH") + revision = report.get("revision") + if type(revision) is not int or revision < 0 or report.get("supersedes_revision") != (revision-1 if revision else None): + raise ValueError("DAILY_REVISION_INVALID") + if aware_time(report["published_at"]).date().isoformat() < day: + raise ValueError("DAILY_PUBLICATION_BEFORE_FACT_DATE") + market = report["etf"]["market"]["latest"] + sectors = report["industry"]["sectors"] + covered = sum(s["latest"]["net_flow_yuan"] is not None for s in sectors) + expected = {"etf":"AVAILABLE_ESTIMATE" if market["net_flow_yuan"] is not None else "AWAITING_COMPLETE_SHARES_NAV", + "industry":"AVAILABLE_REPORTED" if sectors and covered == len(sectors) + else "PARTIAL_REPORTED" if covered else "AWAITING_SOURCE"} + if report.get("component_status") != expected or market.get("trade_date") != day: + raise ValueError("DAILY_COMPONENT_QUALITY_MISMATCH") + if market["net_flow_yuan"] is not None and (market.get("member_count",0) <= 0 + or market.get("observed_member_count") != market.get("member_count")): + raise ValueError("DAILY_INCOMPLETE_MARKET_MISLABELLED") + return day, revision + + +class DailyPublisher: + def __init__(self, snapshots, events): + self.snapshots, self.events = snapshots, events + + def publish(self, report, delivery_at): + day, revision = validate_report(report) + now = aware_time(delivery_at) + if aware_time(report["published_at"]) > now: + raise ValueError("DAILY_DELIVERY_BEFORE_PUBLICATION") + expiry = int((datetime.combine(datetime.fromisoformat(day).date(), time(23, 59), SHANGHAI) + + timedelta(days=90)).timestamp()) + if expiry <= now.timestamp(): + return {"status":"HISTORY_ONLY", "trade_date":day, "revision":revision} + raw = canonical(report) + if len(raw.encode()) > MAX_REPORT_BYTES: + raise ValueError("DAILY_REPORT_TOO_LARGE") + suffix = f"{day}:r{revision}" + snapshot_key = NAMESPACE+":snapshot:"+suffix + manifest_key = NAMESPACE+":manifest:"+suffix + manifest = dict(schema_version=1, contract_version=CONTRACT, namespace=NAMESPACE, status="READY", + trade_date=day, revision=revision, snapshot_key=snapshot_key, manifest_key=manifest_key, + snapshot_sha256=hashlib.sha256(raw.encode()).hexdigest(), published_at=report["published_at"], + purpose="MARKET_OBSERVATION", model_status="uncalibrated", calibration_status="PROVISIONAL", + trade_instruction=False, event_type="DAILY_UPSERT", window_epoch=int(datetime.fromisoformat(day).replace(tzinfo=SHANGHAI).timestamp())) + event = dict(manifest, event_id=f"daily-flow:{day}:r{revision}", supersedes_revision=report["supersedes_revision"], + component_status=report["component_status"]) + # Stream/set expiry uses delivery month to prevent an older-date retry + # shortening the retention of newer entries sharing the same stream. + stream_expiry = int((now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)+timedelta(days=123)).timestamp()) + self.snapshots.eval(PREPARE_LUA, 1, snapshot_key, raw, expiry) + committed = self.events.eval(COMMIT_LUA, 4, NAMESPACE+":unused_state", NAMESPACE+":events:"+day[:7], + NAMESPACE+":event_ids:"+day[:7], manifest_key, "0", 0, "{}", canonical(manifest), + canonical([{"key":event["event_id"], "data":canonical(event)}]), expiry, stream_expiry, 100000) + actual = _json(_text(committed)) + if actual != manifest: + raise ValueError("DAILY_MANIFEST_CONFLICT") + # A per-date pointer exposes revisions even when correcting an older + # date. Global latest never moves back to that older date. + for key in (NAMESPACE+":latest:"+day, NAMESPACE+":latest"): + self.snapshots.eval(LATEST_LUA, 1, key, manifest["window_epoch"], revision, canonical(manifest), expiry) + if (report["component_status"].get("etf") == "AVAILABLE_ESTIMATE" + and report["component_status"].get("industry") == "AVAILABLE_REPORTED"): + self.snapshots.eval(LATEST_LUA, 1, NAMESPACE+":latest_complete", manifest["window_epoch"], + revision, canonical(manifest), expiry) + else: + self.snapshots.eval(REVOKE_COMPLETE_LUA, 1, NAMESPACE+":latest_complete", day, revision) + self.snapshots.publish(NAMESPACE+":daily_ready", canonical(manifest)) + return manifest + + +def _read(client, key, limit): + length = client.strlen(key) + if type(length) is not int or not 0 < length <= limit: + raise ValueError("DAILY_MISSING_OR_OVERSIZED_RECORD") + # Fetch the bounded, complete UTF-8 value in one GETRANGE. Arbitrary byte + # chunks can split a Chinese character before a decode_responses client + # returns it. Both binary and decoded Redis clients are supported. + value = client.getrange(key, 0, length-1) + if not isinstance(value, (str, bytes)): + raise ValueError("DAILY_JSON_TEXT_REQUIRED") + raw = value.encode() if isinstance(value, str) else value + if len(raw) != length or client.strlen(key) != length: + raise ValueError("DAILY_INCOMPLETE_READ") + return raw + + +def read_daily_report(snapshot_client, event_client, manifest_key, *, expected_trade_date, last_revision=-1): + expected = iso_day(expected_trade_date) + if not isinstance(manifest_key, str) or not manifest_key.startswith(NAMESPACE+":manifest:"+expected+":r"): + raise ValueError("DAILY_EXPECTED_DATE_OR_NAMESPACE_MISMATCH") + manifest_raw = _read(event_client, manifest_key, 32768) + manifest = _json(manifest_raw) + revision = manifest.get("revision") + suffix = f"{expected}:r{revision}" + if (manifest.get("namespace") != NAMESPACE or manifest.get("status") != "READY" + or manifest.get("trade_date") != expected or manifest.get("manifest_key") != manifest_key + or manifest_key != NAMESPACE+":manifest:"+suffix + or manifest.get("snapshot_key") != NAMESPACE+":snapshot:"+suffix): + raise ValueError("DAILY_COMMIT_IDENTITY_MISMATCH") + raw = _read(snapshot_client, manifest["snapshot_key"], MAX_REPORT_BYTES) + if hashlib.sha256(raw).hexdigest() != manifest.get("snapshot_sha256"): + raise ValueError("DAILY_SNAPSHOT_HASH_MISMATCH") + if _read(event_client, manifest_key, 32768) != manifest_raw: + raise ValueError("DAILY_MANIFEST_CHANGED") + report = _json(raw) + day, revision = validate_report(report) + if day != expected or revision != manifest["revision"]: + raise ValueError("DAILY_SNAPSHOT_IDENTITY_MISMATCH") + for field in ("contract_version", "published_at", "purpose", "model_status", "calibration_status", "trade_instruction"): + if report.get(field) != manifest.get(field): + raise ValueError("DAILY_METADATA_MISMATCH") + latest_key = NAMESPACE+":latest:"+day + superseded = False + if snapshot_client.strlen(latest_key): + latest = _json(_read(snapshot_client, latest_key, 32768)) + if latest.get("namespace") != NAMESPACE or latest.get("trade_date") != day: + raise ValueError("DAILY_LATEST_IDENTITY_MISMATCH") + latest_manifest = _json(_read(event_client, latest["manifest_key"], 32768)) + if latest_manifest != latest or latest.get("status") != "READY": + raise ValueError("DAILY_LATEST_NOT_COMMITTED") + superseded = latest["revision"] > revision + return dict(report=report, manifest=manifest, apply=revision > last_revision and not superseded, + superseded=superseded, + revision_gap=revision > last_revision+1, + read_completed_at=datetime.now(SHANGHAI).isoformat(), + valid_for_trade_date=day, is_trading_signal=False) diff --git a/app/core/etf_contract/publisher.py b/app/core/etf_contract/publisher.py new file mode 100644 index 0000000..a59c2ec --- /dev/null +++ b/app/core/etf_contract/publisher.py @@ -0,0 +1,245 @@ +"""Redis transport for ETF snapshots and lifecycle events. + +The two Redis instances cannot share a transaction. An immutable snapshot is +prepared first; one Lua operation on the event instance commits events, +CAS-protected state and the READY manifest. Only then can a monotonic latest +pointer move and a notification be published. Consumers verify the manifest. +""" +from __future__ import annotations + +from datetime import datetime, time, timedelta +import hashlib +import json + +from .signals import SHANGHAI, aware_time, lease_until, validate_snapshot +from .timing import LEGACY_POLICY, admission_until, policy_version +from .consumer import OBSERVATION_NAMESPACE, validate_publication_contract, validate_observation_event_metadata + + +class PublishConflict(RuntimeError): + pass + + +def _json(value): + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), + allow_nan=False, + default=lambda x: aware_time(x).isoformat() if isinstance(x, datetime) else _unsupported(x)) + + +def _unsupported(value): + raise TypeError("not JSON serializable: " + type(value).__name__) + + +def _text(value): + return value.decode() if isinstance(value, bytes) else value + + +PREPARE_LUA = """-- ETF_PREPARE_V1 +local old = redis.call('GET', KEYS[1]) +if old and old ~= ARGV[1] then return redis.error_reply('IMMUTABLE_ROUND_CONFLICT') end +if not old then redis.call('SET', KEYS[1], ARGV[1]) end +redis.call('EXPIREAT', KEYS[1], ARGV[2]) +return 1 +""" + + +COMMIT_LUA = """-- ETF_COMMIT_V1 +local kinds = {'string', 'stream', 'set', 'string'} +for i, key in ipairs(KEYS) do + local kind = redis.call('TYPE', key).ok + if kind ~= 'none' and kind ~= kinds[i] then return redis.error_reply('WRONGTYPE_ETF_KEY') end +end +local done = redis.call('GET', KEYS[4]) +if done then return done end +local current = redis.call('GET', KEYS[1]) +local actual = 0 +if current then actual = tonumber(cjson.decode(current).state_version or 0) end +if ARGV[1] == '1' and actual ~= tonumber(ARGV[2]) then + return redis.error_reply('STATE_CAS_CONFLICT') +end +local events = cjson.decode(ARGV[5]) +for _, e in ipairs(events) do + if redis.call('SISMEMBER', KEYS[3], e.key) == 0 then + redis.call('XADD', KEYS[2], 'MAXLEN', '~', ARGV[8], '*', 'data', e.data) + redis.call('SADD', KEYS[3], e.key) + end +end +if ARGV[1] == '1' then + redis.call('SET', KEYS[1], ARGV[3]) + redis.call('EXPIREAT', KEYS[1], ARGV[6]) +end +if #events > 0 then + redis.call('EXPIREAT', KEYS[2], ARGV[7]) + redis.call('EXPIREAT', KEYS[3], ARGV[7]) +end +redis.call('SET', KEYS[4], ARGV[4]) +redis.call('EXPIREAT', KEYS[4], ARGV[7]) +return ARGV[4] +""" + + +LATEST_LUA = """-- ETF_LATEST_V1 +local old = redis.call('GET', KEYS[1]) +if old then + local v = cjson.decode(old) + if tonumber(v.window_epoch) > tonumber(ARGV[1]) then return 0 end + if tonumber(v.window_epoch) == tonumber(ARGV[1]) and tonumber(v.revision) > tonumber(ARGV[2]) then return 0 end +end +redis.call('SET', KEYS[1], ARGV[3]) +redis.call('EXPIREAT', KEYS[1], ARGV[4]) +return 1 +""" + + +FAILURE_LUA = """-- ETF_FAILURE_V1 +redis.call('SET', KEYS[1], ARGV[1]) +redis.call('EXPIREAT', KEYS[1], ARGV[2]) +return 1 +""" + + +class RedisPublisher: + def __init__(self, snapshot_client, event_client, namespace="mtf:etf:shadow:v1", maxlen=100000): + if not namespace or namespace.endswith(":" ): + raise ValueError("namespace must be nonempty without a trailing colon") + if maxlen <= 0: + raise ValueError("maxlen must be positive") + self.snapshots = snapshot_client + self.events = event_client + self.namespace = namespace + self.maxlen = int(maxlen) + self.channel = namespace + ":round_ready" + + def state_key(self, trade_date): + return f"{self.namespace}:state:{trade_date}" + + def load_state(self, trade_date): + raw = self.events.get(self.state_key(trade_date)) + return json.loads(_text(raw)) if raw else {} + + def _expiry(self, trade_date, days): + close = datetime.combine(datetime.fromisoformat(trade_date).date(), time(15), SHANGHAI) + return int((close + timedelta(days=days)).timestamp()) + + def publish(self, snapshot, events, new_state, published_at, delivery_at=None): + """Commit a frozen publication; delivery_at is the real retry clock. + + Late retries retain original event timestamps for audit but never move + the live pointer. The runner must pass its current clock as delivery_at. + Omitting it preserves deterministic replay/test behavior. + """ + end = validate_snapshot(snapshot) + publication_metadata = validate_publication_contract(snapshot, self.namespace) + timing_policy = policy_version(snapshot) + now = aware_time(published_at) + if self.namespace == OBSERVATION_NAMESPACE: + if aware_time(snapshot.get("published_at")) != now: + raise ValueError("observation snapshot must retain the frozen publication timestamp") + if type(snapshot.get("session_closed")) is not bool or snapshot["session_closed"] != (end.hour == 15): + raise ValueError("observation session_closed must match the fixed closing window") + delivery = aware_time(delivery_at) if delivery_at is not None else now + if delivery < now: + raise ValueError("delivery_at cannot precede the frozen publication timestamp") + if end > now: + raise ValueError("cannot publish an unfinished future window") + date = snapshot["trade_date"] + revision = snapshot.get("revision", 0) + round_id = snapshot["round_id"] + suffix = f"{date}:{round_id}:r{revision}" + snapshot_key = f"{self.namespace}:snapshot:{suffix}" + manifest_key = f"{self.namespace}:manifest:{suffix}" + snapshot_expiry, stream_expiry = self._expiry(date, 2), self._expiry(date, 7) + canonical = _json(snapshot) + lease = lease_until(end, timing_policy) + admission = admission_until(end, timing_policy) + original_eligible = (snapshot.get("mode", "LIVE") == "LIVE" + and snapshot.get("data_status") == "OK" + and end <= now < admission and not snapshot.get("session_closed", False)) + current = original_eligible and end <= delivery < lease + manifest = { + "schema_version": 1, "status": "READY", "round_id": round_id, + "revision": revision, "trade_date": date, "window_end": end.isoformat(), + "window_epoch": int(end.timestamp()), "published_at": now.isoformat(), + "valid_until": lease.isoformat(), + "timing_policy_version": timing_policy, "admission_until": admission.isoformat(), + "session_closed": bool(snapshot.get("session_closed")) or end.hour == 15, + "live_eligible": current, + "mode": snapshot.get("mode", "LIVE"), "snapshot_key": snapshot_key, + "manifest_key": manifest_key, "event_count": len(events), + "snapshot_sha256": hashlib.sha256(canonical.encode()).hexdigest(), + } + if self.namespace == OBSERVATION_NAMESPACE: + manifest.update(publication_mode="observation", **publication_metadata) + # Historical backfills belong in the durable results store. Never + # resurrect Redis data beyond its fixed retention boundary. + if snapshot_expiry <= int(delivery.timestamp()): + return dict(manifest, status="HISTORY_ONLY", cached=False) + if events and snapshot.get("mode", "LIVE") == "REPLAY": + raise ValueError("REPLAY must not publish live lifecycle events") + encoded_events = [] + for event in events: + if self.namespace == OBSERVATION_NAMESPACE: + validate_observation_event_metadata(event, snapshot) + event_policy = policy_version(event) + if event.get("round_id") != round_id or event.get("revision") != revision: + raise ValueError("event round/revision does not match snapshot") + if event.get("trade_date") != date or event.get("window_end") != end.isoformat(): + raise ValueError("event time partition does not match snapshot") + if aware_time(event["published_at"]) != now: + raise ValueError("retry the exact frozen publication timestamp") + if event.get("event_state") in ("OPEN", "CONTINUE"): + if not original_eligible: + raise ValueError("stale, replay or closed rounds cannot publish active events") + if event_policy != timing_policy: + raise ValueError("active event timing policy must match its snapshot") + if aware_time(event["valid_until"]) > lease_until(end, event_policy): + raise ValueError("event lease cannot extend beyond its window lease or market close") + key = f"{event['event_id']}:{event['event_seq']}:{event['revision']}" + encoded_events.append({"key": key, "data": _json(event)}) + write_state = bool(new_state.get("_write_state", False)) + if events and not write_state: + raise ValueError("lifecycle events require an atomic state update") + if write_state and (snapshot.get("mode", "LIVE") == "REPLAY" or new_state.get("trade_date") != date): + raise ValueError("state update must belong to this LIVE trading date") + if write_state and policy_version(new_state) != timing_policy: + raise ValueError("state timing policy must match its current snapshot") + expected = int(new_state.get("_expected_version", new_state.get("state_version", 0))) + persisted_state = {k: v for k, v in new_state.items() if not k.startswith("_")} + if write_state and persisted_state.get("state_version") != expected + 1: + raise ValueError("state_version must increment exactly once") + try: + self.snapshots.eval(PREPARE_LUA, 1, snapshot_key, canonical, snapshot_expiry) + committed = self.events.eval( + COMMIT_LUA, 4, self.state_key(date), f"{self.namespace}:events:{date}", + f"{self.namespace}:event_ids:{date}", manifest_key, + "1" if write_state else "0", expected, _json(persisted_state), _json(manifest), + _json(encoded_events), snapshot_expiry, stream_expiry, self.maxlen, + ) + manifest = json.loads(_text(committed)) + if current: + switched = self.snapshots.eval( + LATEST_LUA, 1, self.namespace + ":latest", int(end.timestamp()), revision, + _json(manifest), snapshot_expiry, + ) + if switched: + self.snapshots.publish(self.channel, _json(manifest)) + return manifest + except Exception as exc: + if "CONFLICT" in str(exc): + raise PublishConflict(str(exc)) from exc + raise + + def publish_failure(self, round_id, trade_date, window_end, error_code, published_at, revision=0, + timing_policy_version=LEGACY_POLICY): + """Diagnostic status only; never changes a READY manifest or latest.""" + end, now = aware_time(window_end), aware_time(published_at) + payload = { + "schema_version": 1, "status": "FAILED", "round_id": round_id, + "trade_date": trade_date, "window_end": end.isoformat(), "revision": revision, + "published_at": now.isoformat(), "error_code": str(error_code), + "timing_policy_version": policy_version({"timing_policy_version": timing_policy_version}), + } + key = f"{self.namespace}:failure:{trade_date}:{round_id}:r{revision}" + self.events.eval(FAILURE_LUA, 1, key, _json(payload), self._expiry(trade_date, 2)) + self.snapshots.publish(self.channel, _json(payload)) + return payload diff --git a/app/observation_worker.py b/app/observation_worker.py index 57a26e4..86b06de 100644 --- a/app/observation_worker.py +++ b/app/observation_worker.py @@ -6,6 +6,7 @@ from threading import Event def main(): from app.services.etf_observation import consume_once + from app.services.broad_funds import consume_once as consume_daily logging.basicConfig(level=logging.INFO) stopping = Event() signal.signal(signal.SIGTERM, lambda *_: stopping.set()) @@ -17,6 +18,12 @@ def main(): logging.warning('ETF观测暂不可用:%s', result.get('error')) except Exception as exc: logging.warning('ETF观测保存失败:%s', type(exc).__name__) + try: + result = consume_daily() + if result.get('status') == 'unavailable': + logging.warning('每日资金暂不可用:%s', result.get('error')) + except Exception as exc: + logging.warning('每日资金保存失败:%s', type(exc).__name__) stopping.wait(15) diff --git a/app/services/broad_funds.py b/app/services/broad_funds.py new file mode 100644 index 0000000..c361ef9 --- /dev/null +++ b/app/services/broad_funds.py @@ -0,0 +1,150 @@ +"""宽基每日资金只读接入;原文留存后确认,仅供页面观测。""" +from __future__ import annotations + +import hashlib +import json +import re +from datetime import datetime, timedelta + +from app.core.etf_contract import daily_transport as contract +from app.repo import rotation_repo as store +from app.services import etf_observation as minute, param_store + +NS = contract.NAMESPACE +GROUP = 'pms-broad-daily-v1' +STATE = 'broad-funds:state' +INDEX_NAMES = {'000016.SH': '上证50', '000300.SH': '沪深300', '000510.SH': '中证A500', + '000688.SH': '科创50', '000852.SH': '中证1000', '000905.SH': '中证500', + '399006.SZ': '创业板指', '932000.CSI': '中证2000'} + + +def initial(): + return {'days': {}, 'latest': None, 'last_success_at': None, 'error': None} + + +def validate_scope(report, at): + if report['trade_date'] > at.date().isoformat() or datetime.fromisoformat(report['published_at']) > at: + raise ValueError('DAILY_FUTURE_FACT') + for code in report['etf'].get('cohort', []): + if not re.fullmatch(r'\d{6}\.(SH|SZ)', str(code)): + raise ValueError('DAILY_OUT_OF_SCOPE') + for row in report['etf'].get('etfs', []): + if not minute.subject_allowed('ETF', row['subject_id']): + raise ValueError('DAILY_OUT_OF_SCOPE') + for row in report['etf'].get('indexes', []): + if not minute.subject_allowed('INDEX', row['subject_id']): + raise ValueError('DAILY_OUT_OF_SCOPE') + + +def ingest(snapshots, events, manifest_key, day, at, delivery=None): + """契约核验、同日修订防倒退、事实与回执在同一事务落库。""" + audit_key = 'broad-funds:event:' + delivery[0] + ':' + delivery[1] if delivery else None + if audit_key and store.get(audit_key)[0] is not None: + return + result = contract.read_daily_report(snapshots, events, manifest_key, expected_trade_date=day) + report, manifest = result['report'], result['manifest'] + validate_scope(report, at) + if manifest.get('event_type') != 'DAILY_UPSERT': + raise ValueError('DAILY_EVENT_TYPE_MISMATCH') + raw = contract._read(snapshots, manifest['snapshot_key'], contract.MAX_REPORT_BYTES) + if hashlib.sha256(raw).hexdigest() != manifest['snapshot_sha256']: + raise ValueError('DAILY_ARCHIVE_HASH_MISMATCH') + if delivery: + fields = delivery[2] + event = contract._json(fields.get('data')) + expected = dict(manifest, event_id=f"daily-flow:{day}:r{report['revision']}", + supersedes_revision=report['supersedes_revision'], component_status=report['component_status']) + if event != expected: + raise ValueError('DAILY_EVENT_COMMIT_MISMATCH') + key = f"broad-funds:report:{day}:r{report['revision']}" + with store.locked(STATE, 'broad_funds_state', initial()) as state: + if audit_key and store.get(audit_key)[0] is not None: + return + store.put_once(key, 'broad_funds_report', {'manifest': manifest, 'snapshot_text': raw.decode('utf-8')}) + old = state['days'].get(day, {}) + apply = not result['superseded'] and report['revision'] > old.get('revision', -1) + if apply: + state['days'][day] = {'revision': report['revision'], 'key': key, + 'component_status': report['component_status'], 'published_at': report['published_at']} + if not state['latest'] or day >= state['latest']: + state['latest'] = day + if audit_key: + store.put_once(audit_key, 'broad_funds_event', {'stream': delivery[0], 'stream_id': delivery[1], + 'raw': delivery[2], 'manifest_key': manifest_key, 'applied': apply, + 'superseded': result['superseded'], 'received_at': at.isoformat()}) + # 只限制状态索引大小,原始快照与事件审计均保留。 + state['days'] = {k: state['days'][k] for k in sorted(state['days'])[-100:]} + + +def consume_once(snapshots=None, events=None, clock=minute.now): + if not param_store.get_bool('PMS_BROAD_DAILY_ENABLED', False): + return {'status': 'disabled'} + snapshots, events = (snapshots, events) if snapshots is not None else minute.clients() + at = clock().astimezone(minute.TZ) + failures, processed = [], 0 + # 正式保留期内的月流,固定边界,不扫描整个Redis,也不触碰交易消费组。 + months = sorted({(at.date() - timedelta(days=n)).isoformat()[:7] for n in range(91)}) + try: + for month in months: + stream = NS + ':events:' + month + if not events.exists(stream): + continue + try: + events.xgroup_create(stream, GROUP, id='0-0') + except Exception as exc: + if 'BUSYGROUP' not in str(exc): + raise + for cursor in ('0-0', '>'): + for _stream, rows in events.xreadgroup(GROUP, minute.CONSUMER, {stream: cursor}, count=100): + for sid, fields in rows: + try: + event = contract._json(fields.get('data')) + day = event['trade_date'] + if day[:7] != month: + raise ValueError('DAILY_STREAM_DATE_MISMATCH') + ingest(snapshots, events, event['manifest_key'], day, clock(), (stream, sid, fields)) + events.xack(stream, GROUP, sid) + processed += 1 + except Exception as exc: + failures.append(type(exc).__name__) + # 即使月流暂缺或某条待补,也核验最新正式提交;没有事实时不沿用旧日冒充今天。 + if snapshots.strlen(NS + ':latest'): + pointer = contract._json(contract._read(snapshots, NS + ':latest', 32768)) + manifest = contract._json(contract._read(events, pointer['manifest_key'], 32768)) + if pointer != manifest: + raise ValueError('DAILY_POINTER_NOT_COMMITTED') + ingest(snapshots, events, pointer['manifest_key'], pointer['trade_date'], clock()) + except Exception as exc: + failures.append(type(exc).__name__) + with store.locked(STATE, 'broad_funds_state', initial()) as state: + if failures: + state.update(error=failures[0], last_failure_at=clock().isoformat()) + else: + state.update(error=None, last_success_at=clock().isoformat()) + return {'status': 'unavailable' if failures else 'ok', 'processed': processed, + 'error': failures[0] if failures else None} + + +def status(clock=minute.now): + if not param_store.get_bool('PMS_BROAD_DAILY_ENABLED', False): + return {'enabled': False, 'trade_instruction': False} + state = store.get(STATE)[0] or initial() + entry = state['days'].get(state['latest'], {}) + saved = store.get(entry['key'])[0] if entry.get('key') else None + at = clock() + healthy = bool(state['last_success_at']) and not state['error'] and ( + at - datetime.fromisoformat(state['last_success_at'])).total_seconds() <= 180 + result = {'enabled': True, 'status': 'ok' if healthy and saved else 'waiting' if healthy else 'unavailable', + 'trade_instruction': False, 'last_success_at': state['last_success_at'], 'error': state['error'], + 'server_at': at.isoformat(), 'report': None} + if saved: + report = json.loads(saved['snapshot_text']) + # 页面只需市场、指数、行业;完整逐基金事实保存在不可变档案中。 + for key in ('etfs', 'cohort'): + report['etf'].pop(key, None) + report.pop('source_observations', None) + for row in report['etf'].get('indexes', []): + row['name'] = INDEX_NAMES.get(row['subject_id']) or row.get('name') or row['subject_id'] + result['report'] = report + result['manifest'] = saved['manifest'] + return result diff --git a/app/services/etf_observation.py b/app/services/etf_observation.py index bb93fe5..081a7d2 100644 --- a/app/services/etf_observation.py +++ b/app/services/etf_observation.py @@ -72,6 +72,8 @@ def bundle(snapshots, events, key): def subject_allowed(kind, code): if kind == 'MARKET': return code == 'BROAD_ETF' + if kind == 'INDEX' and code == '932000.CSI': + return True return kind in ('ETF', 'INDEX') and bool(re.fullmatch(r'\d{6}\.(SH|SZ)', str(code))) diff --git a/app/services/param_store.py b/app/services/param_store.py index 092ec24..847b1b8 100644 --- a/app/services/param_store.py +++ b/app/services/param_store.py @@ -47,6 +47,8 @@ def _valid_limit_flag(value): # 运行态开关: key -> (默认值, 类型, 说明) RUNTIME_EXTRA = { "PMS_SIGNAL_FOCUS_ENABLED": (False, bool, "持仓与有效关注共用完整信号读取,不扩大交易资格"), + "PMS_BROAD_DAILY_ENABLED": (False, bool, "接入正式宽基与行业每日资金,仅供观测"), + "PMS_UPSTREAM_DAY_SCAN": (20000, int, "信号流当天每路读取上限,达到上限明确提示"), "PMS_ETF_OBSERVATION_ENABLED": (False, bool, "接入正式ETF观测,仅展示,不生成交易指令"), 'PMS_RISK_EXIT_ENABLED': (False, bool, '原策略退出纪律:第一次清仓级信号停买,第二次独立信号清仓;买方整体评级下降直接清仓'), 'PMS_RISK_EXIT_START_AT': ('', str, '退出纪律启用时刻,北京时间;只接收此后有效新信号,既有真实停买最多承接第一次'), @@ -616,6 +618,7 @@ _RANGES = { # 任务会先被 celery 打死 (而且是在已经落了一部分表之后)。 "PMS_UPSTREAM_ALERT_SCAN": (1, 20000), "PMS_UPSTREAM_ALERT_PER_CAT": (1, 1000), "PMS_UPSTREAM_ALERT_MAX_AGE_MIN": (0, 1440), # 0=不限龄, 上限 24 小时 + "PMS_UPSTREAM_DAY_SCAN": (500, 50000), "PMS_UPSTREAM_BYCODE_SCAN": (100, 50000), # 下限 100 保证至少扫得到一页 # 解锁重问 (2026-09-09) "PMS_OPEN_REASK_MIN_GAP_MIN": (5, 240), "PMS_OPEN_REASK_MAX_PER_DAY": (0, 5), diff --git a/app/services/upstream_signals.py b/app/services/upstream_signals.py index 3531f18..733742c 100644 --- a/app/services/upstream_signals.py +++ b/app/services/upstream_signals.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """上游信号只读快照 (2026-08-12: 只展示、不进任何下单/决策逻辑)。 -把三系统盘中信号取最近若干条给页面看: +把三系统当天正式发布的信号分页读全,供页面按原分类浏览: · 决策系统(bionic) 买卖广播 + 风控卖出动作 —— 208 db2 intraday_signals / db3 llm_sell_actions · 盘中择时层(intraday_timing) BUY 入场 + 双向风控告警 —— 208 db2 intraday_signals / intraday_alerts · mtf 资金异动 + 实况分关注/回避榜 —— 208 db2 stream:metrics / 214 db0 mr:board @@ -80,25 +80,16 @@ def _focus_codes(): def _entries(c, key, count): """每路统一扫描一次并缓存,不逐股轮询完整上游流。""" - codes = _focus_codes() - if not codes: - return [(sid, row) for sid, row in c.xrevrange(key, count=count) if _entry_code(row)] - cache_key = (key, tuple(sorted(codes))) + _focus_codes() + cache_key = (key, _today_start_ms()) cached = _focused_streams.get(cache_key) if cached and time.time() - cached['at'] < 30: return cached['rows'] - rows, truncated = _scan_stream(c, key, cap=_bycode_scan(), stop_before_ms=_today_start_ms()) - result, per_code = [], {} - for i, (sid, fields) in enumerate(rows): - code = _entry_code(fields) - if not code: - continue - selected = code in codes and per_code.get(code, 0) < 60 - if i < count or selected: - result.append((sid, fields)) - if selected: - per_code[code] = per_code.get(code, 0) + 1 - _focused_streams[cache_key] = {'at': time.time(), 'rows': result, 'truncated': truncated} + cap = max(500, min(50000, param_store.get_int('PMS_UPSTREAM_DAY_SCAN', 20000))) + rows, truncated = _scan_stream(c, key, cap=cap, stop_before_ms=_today_start_ms()) + result = [(sid, fields) for sid, fields in rows if _entry_code(fields)] + _focused_streams[cache_key] = {'at': time.time(), 'rows': result, 'truncated': truncated, + 'scanned': len(rows), 'eligible': len(result)} for old in list(_focused_streams): if old != cache_key and time.time() - _focused_streams[old]['at'] > 60: del _focused_streams[old] @@ -228,12 +219,9 @@ _ALERT_META = { def _read_alerts(c, ymd): key = "intraday_alerts:%s" % ymd - per_cat = _alert_per_cat() max_age = _alert_max_age_sec() # 秒; 0=不限龄。超龄不删, 只标 stale 供页面置灰。 now = time.time() - counts = {} # cat -> 已收数量; 每类只留最新 per_cat 条(仍按 newest-first) out = [] - focused = _focus_codes() for _id, f in _entries(c, key, _alert_scan()): src = str(f.get("source") or "") meta = f.get("metadata") @@ -243,8 +231,6 @@ def _read_alerts(c, ymd): except Exception: meta = {} dir_fixed, cat, cat_label = _ALERT_META.get(src, (None, "other", "其他")) - if counts.get(cat, 0) >= per_cat and _norm_code(f.get('ts_code')) not in focused: # 该类已满 → 跳过, 高频源不挤掉别的类 - continue if dir_fixed is None: # multi_source 或未登记源: 方向以 metadata.direction 为准, 拿不到就 — mdir = str((meta or {}).get("direction") or "").lower() @@ -259,8 +245,6 @@ def _read_alerts(c, ymd): except (TypeError, ValueError): age_sec, age_min = None, None stale = bool(max_age) and age_sec is not None and age_sec > max_age - if _norm_code(f.get('ts_code')) not in focused: - counts[cat] = counts.get(cat, 0) + 1 out.append({"id": _id, "ts_code": f.get("ts_code"), "source": src, "direction": d, "cat": cat, "cat_label": cat_label, "level": f.get("level"), "value": _num(f.get("value")), @@ -337,7 +321,7 @@ def _read_mr(c): key = "mtf:mr:intraday:board" top = c.zrevrange(key, 0, 9, withscores=True) bottom = c.zrange(key, 0, 9, withscores=True) - fmt = lambda pairs: [{"ts_code": k, "score": round(float(v), 3)} for k, v in pairs] + fmt = lambda pairs: [{"ts_code": k, "score": round(float(v), 3)} for k, v in pairs if _entry_code({"ts_code": k})] return {"key": key, "watch": fmt(top), "avoid": fmt(bottom)} @@ -364,6 +348,8 @@ def snapshot(force: bool = False) -> dict: _try("sell_actions", lambda: _read_sell_actions(_c208(dba))) _try("mr", lambda: _read_mr(_c214())) + out['coverage'] = {key[0]: {k: v[k] for k in ('truncated', 'scanned', 'eligible')} + for key, v in _focused_streams.items() if key[1] == _today_start_ms()} if param_store.get_bool('PMS_SIGNAL_FOCUS_ENABLED', False): out['focus'] = {'count': len(_focus_cache['codes']), 'error': _focus_cache['error'], 'truncated': any(v['truncated'] for v in _focused_streams.values())} @@ -620,7 +606,7 @@ def _bycode_metrics(c, code, cap): def by_code(ts_code: str, force: bool = False) -> dict: """一只票今天的全部上游信号。点击触发, 不进任何轮询。""" code = _norm_code(ts_code) - if not code: + if not _entry_code({"ts_code": code}): return {"ok": False, "error": "认不出的股票代码: %s" % ts_code, "timeline": [], "snapshot": {"metrics": [], "mr": {"watch": None, "avoid": None}}} six = _code6(code) diff --git a/app/web/main.py b/app/web/main.py index 6f5c382..780a229 100644 --- a/app/web/main.py +++ b/app/web/main.py @@ -894,6 +894,12 @@ def api_upstream_signals(): return ok(upstream_signals.snapshot) +@app.get("/api/broad/funds") +def api_broad_funds(): + from app.services import broad_funds + return ok(broad_funds.status) + + @app.get("/api/etf/observation") def api_etf_observation(): """读取本实例已验证的ETF观测,不发任务或交易命令。""" diff --git a/app/web/static/index.html b/app/web/static/index.html index 89a5a62..5a6eb5a 100644 --- a/app/web/static/index.html +++ b/app/web/static/index.html @@ -593,6 +593,10 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;} @media(max-width:640px){.plan-add-panel{padding:14px}.plan-user-top{gap:6px}.plan-user-item{padding:10px}.plan-add-actions .el-button{flex:1}} .etf-total{display:flex;align-items:baseline;gap:5px;margin:12px 0}.etf-total b{font-size:25px;font-weight:650}.etf-stats{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin:16px 0}.etf-stats b{font-size:22px;font-weight:650}.etf-stats small{display:block;color:var(--muted);font-size:11px}.etf-events{margin-top:12px;border-top:1px solid var(--hair)}.etf-event{padding:12px 0;display:flex;align-items:center;gap:8px;border-bottom:1px solid var(--hair);font-size:12px}.etf-event .etf-name{flex:1;min-width:0;border:0;background:none;text-align:left;cursor:pointer;font:inherit}.etf-event small{display:block;font-size:11px;color:var(--muted)}.etf-note{font-size:11.5px;color:var(--muted);line-height:1.8;margin:12px 0}.etf-selected{border:1px solid var(--hair);border-radius:8px;padding:12px;margin:12px 0}.etf-selected dt{color:var(--muted)}.etf-selected dd{margin:0 0 9px}.etf-bars{display:flex;gap:8px;align-items:flex-end;height:85px;margin:8px 0 20px;border-bottom:1px solid var(--hair)}.etf-bar{flex:1;position:relative;background:#80a6dc;min-width:0}.etf-bar span{position:absolute;bottom:-18px;font-size:9px;left:50%;transform:translateX(-50%);color:var(--muted)}.etf-bar i{position:absolute;bottom:100%;font:10px inherit;left:50%;transform:translateX(-50%);color:var(--muted)}.etf-drawer{width:min(580px,100vw)}.hd .hd-etf{cursor:pointer;border:0;font-family:inherit}.etf-bar.missing{background:none;border-bottom:1px dashed var(--muted)}.etf-missing{border:1px dashed var(--muted)!important;background:none!important;color:var(--muted)!important}.signal-flow .disp.etf-state{font-size:10px;padding:1px 4px}.signal-flow .nm.etf-name{font-size:11.5px}.sigrow2.etf-flow{gap:2.5px;font-size:12px}.sigrow2.etf-flow .cat{font-size:11px}.sigrow2.etf-flow .t{font-size:10.5px}.sig-observe{color:var(--accent);background:var(--accent-bg,#ecf3fd)} @media(max-width:600px){.hd-tags>.hst.hd-etf{display:inline-flex}.etf-stats{gap:6px}.etf-stats b{font-size:19px}} +/* 宽基每日观测沿用原顶部提示、右侧抽屉与红绿资金方向。 */ +.broad-chip{white-space:nowrap;gap:3px}.broad-chip b{margin:0 4px;font-variant-numeric:tabular-nums}.broad-chip-date{font-weight:400;opacity:.8}.broad-popover{width:370px;max-width:calc(100vw - 46px)}.broad-amount{display:flex;align-items:baseline;gap:6px;margin:16px 0 20px}.broad-amount strong{font-size:29px;font-weight:650;line-height:1.3;font-variant-numeric:tabular-nums}.broad-pop-footer{text-align:right;border-top:1px solid var(--hair);padding-top:10px;margin-top:10px}.broad-pop-footer button{border:0;background:none;font-size:12px}.broad-chart svg{display:block;width:100%;height:auto}.broad-chart text{font-size:10px;fill:var(--muted);font-family:inherit}.broad-badge{display:inline-block;padding:2px 7px;border-radius:7px;background:#f2f3f1;color:var(--muted);font-size:11px;white-space:nowrap}.broad-gold{background:#fbf4e2!important;color:#9a792c!important}.broad-neutral{background:#edf3fb;color:#4e7daf}.broad-chip.broad-neutral{background:#edf3fb;color:#4e7daf}.broad-drawer{max-width:100vw}.broad-drawer .el-drawer__header{margin-bottom:0;padding:18px 22px;border-bottom:1px solid var(--hair);color:var(--ink)}.broad-drawer .el-drawer__title{font-size:16px;font-weight:650}.broad-drawer .el-drawer__body{padding:18px 22px;font-size:12px}.broad-tabs{display:flex;gap:22px;border-bottom:1px solid var(--hair);margin-bottom:19px}.broad-tabs button{border:0;background:none;padding:0 0 10px;color:var(--muted);font-size:14px;cursor:pointer}.broad-tabs .on{color:var(--ink);border-bottom:2px solid var(--ink);font-weight:650}.broad-heading{display:flex;justify-content:space-between;align-items:center;gap:8px;font-size:13px;margin:18px 0 8px}.broad-heading:first-child{margin-top:0}.broad-heading .muted{font-size:11px;font-weight:400}.broad-sub{font-size:11px}.broad-periods{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px}.broad-periods button{border:1px solid var(--hair);background:#fafaf8;border-radius:8px;text-align:left;padding:12px 10px;cursor:pointer}.broad-periods button.on{border-color:#9eafc0;background:#f6f8fb}.broad-periods small{display:block;color:var(--muted);font-size:11px}.broad-periods strong{display:block;font-size:21px;margin:7px 0 8px;font-weight:600;font-variant-numeric:tabular-nums}.broad-seg{display:flex;gap:2px;background:#f3f3f0;border-radius:6px;padding:2px}.broad-seg button{padding:4px 8px;background:none;border:0;border-radius:4px;font-size:11px;color:var(--muted);cursor:pointer}.broad-seg button.on{background:white;color:var(--ink);box-shadow:0 1px 3px #0001}.broad-table{width:100%;border-collapse:collapse;font-size:12px;font-variant-numeric:tabular-nums}.broad-table th{font-weight:500;color:var(--muted);font-size:11px;background:#fafaf8}.broad-table th,.broad-table td{padding:10px 7px;text-align:right;border-bottom:1px solid var(--hair);white-space:nowrap}.broad-table th:first-child,.broad-table td:first-child{text-align:left}.broad-table .selected{background:#f3f6fa}.broad-table .lnk,.broad-rank .lnk{border:0;background:none;color:inherit;font:inherit;padding:0;cursor:pointer}.broad-table .lnk{text-decoration:underline dotted;text-underline-offset:3px}.broad-foot{font-size:11px;color:var(--muted);line-height:1.8;margin-top:18px;border-top:1px solid var(--hair);padding-top:12px}.broad-notice,.broad-basis{padding:10px 12px;border-radius:7px;background:#f6f6f2;color:#78776e;line-height:1.6;margin-bottom:12px}.broad-counts{display:flex;gap:8px;flex-wrap:wrap;margin:13px 0 8px}.broad-in{background:#fcf0ed;color:var(--up)}.broad-out{background:#eef5f0;color:var(--down)}.broad-countbar{height:6px;display:flex;border-radius:5px;overflow:hidden;background:#eee;margin-bottom:14px}.broad-countbar i:first-child{background:#df9b90}.broad-countbar i:last-child{background:#9bbca5}.broad-ranks{display:grid;grid-template-columns:1fr 1fr;gap:22px;margin-bottom:20px}.broad-ranks h4{font-size:12px;margin:8px 0;color:var(--muted);font-weight:500}.broad-rank{display:flex;gap:7px;align-items:center;margin:12px 0;font-size:11px}.broad-rank button{min-width:60px;text-align:left;white-space:nowrap}.broad-track{height:8px;flex:1;background:#f4f4f0;border-radius:3px;overflow:hidden}.broad-track i{height:100%;display:block}.broad-track .positive{background:#e5a098}.broad-track .negative{background:#94b8a0}.broad-rank>span{min-width:48px;text-align:right}.broad-sector-detail{padding:13px 14px;background:#fafaf8;border:1px solid var(--hair);border-radius:9px}.broad-sector-detail .broad-heading{margin-top:0}.broad-search{width:145px;border:1px solid var(--hair);padding:6px 9px;border-radius:6px;font-size:12px;background:white}.broad-allrows{max-height:400px;overflow:auto}.broad-empty{padding:25px 0;text-align:center;color:var(--muted)}.sig-more{display:block;margin:8px auto;border:0;background:none;color:var(--muted);font-size:11px;cursor:pointer} +@media(max-width:600px){.hd-tags{flex-wrap:wrap;overflow:visible;row-gap:5px}.broad-drawer .el-drawer__header{padding:15px}.broad-drawer .el-drawer__body{padding:15px}.broad-periods{gap:6px}.broad-periods button{padding:10px 7px}.broad-periods strong{font-size:18px}.broad-ranks{grid-template-columns:1fr;gap:3px}.broad-table th,.broad-table td{padding:9px 4px;font-size:11px}.broad-table .broad-badge{font-size:10px;padding:2px 4px}.broad-heading{flex-wrap:wrap}.broad-search{max-width:130px}.broad-chip{font-size:11px!important}} + @@ -672,20 +676,18 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;} 微盘 {{ microChipText }} - -
-
ETF观测 · {{ etfActiveCount }}项查看明细 ›
+
宽基观测 · {{ etfActiveCount }}项查看明细 ›