diff --git a/DEVLOG.md b/DEVLOG.md index 6ac77a5..5ef13d7 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -2862,3 +2862,7 @@ PMS最终代码56db0fd已推送,155 web/WS/beat为execution-2b1c447,worker 14:17至14:19只读核验155和bionic:八只关注股票买方评析及每日研究完成,六份最新盘中结论有效、两份模型结果无效;八只均未纳入常规盘中白名单。人工关注分支只存回执,不发布原信号流。原始资金指标当日有四条关注记录,页面全市场最新40条截取及速览未纳入metrics共同造成不可见。常规决策与清仓消费组pending均为0,不据此声称全链路覆盖正常。 按现有风格制作ETF顶部概览、原单行信号流及详情抽屉原型,使用正式13:30封存观测并标注非实时。开发机浏览器验证桌面、390与320宽度、筛选、抽屉、到期与读取失败;未改变线上代码、服务或开关。详见[巡检与设计](docs/自选股信号巡检与ETF观测原型_2026-11-05.md)。实现及真实消费验收待设计确认,不新增长期观察。 + +## 2026-11-05 自选股补显与ETF观测 + +按用户确认原型实现已有真实信号补留、资金观测速览及独立ETF消费与页面。研究结果不伪造信号,ETF不进入交易动作。开发机隔离全量和浏览器验收通过,发布与自然判收见[实施记录](docs/自选股补显与ETF观测实施_2026-11-05.md)。 diff --git a/README.md b/README.md index 725dc7f..5bddd1d 100644 --- a/README.md +++ b/README.md @@ -512,3 +512,7 @@ docker compose --profile ws up -d pms-ws && docker compose logs -f pms-ws 新增可独立开启的异步研判和限价撤换流程。bionic负责机会判断,PMS按新鲜行情报价。撤单确认、累计成交入账之前,旧单继续占用数量和资金。微盘v3将首选与有序备选一并冻结;单股受阻不会中止其他成员。旧v2快照与规则保持兼容。 新开关默认关闭,运行值通过ParamStore读取。接口、开启条件和验证记录见[实施规格](docs/执行报价与异步研判实施规格_2026-11-03.md)。没有实时盘口时明确使用分钟行情降级,不承诺即时成交。 + +### ETF观测与关注信号 + +`pms-observation`独立消费正式ETF观测,持久化后确认。页面经`GET /api/etf/observation`读取,两个独立Redis连接仅由私有环境注入。`PMS_ETF_OBSERVATION_ENABLED`控制观测消费与显示,`PMS_SIGNAL_FOCUS_ENABLED`控制持仓及关注真实信号补留,均经ParamStore读取。ETF不参与交易执行。详见[实施及发布状态](docs/自选股补显与ETF观测实施_2026-11-05.md)。 diff --git a/app/core/etf_contract/PROVENANCE.json b/app/core/etf_contract/PROVENANCE.json new file mode 100644 index 0000000..8c882de --- /dev/null +++ b/app/core/etf_contract/PROVENANCE.json @@ -0,0 +1,14 @@ +{ + "repository": "main-trend-factor-system", + "path": "src/etf", + "sha256": { + "consumer.py": "8b7e0baee4b607546fe2b73ff96eb9f2dcd7c97fa91786046a43f911db49a6e9", + "signals.py": "0c2535a77c8613b21551190e7a96f8623edac0f393e8e11f37f0946fe8138844", + "timing.py": "250bb8e912c7a8f639a8e74f992b71ce185d70ac3a34b70bc0a75ab632f478c6" + }, + "dependencies": "Python standard library only; source files copied without modifications", + "workspace_head_for_context": "feba149eda7b2090c01d4c536d53eb460fedafef", + "source_tracking": "upstream files untracked; pinned by exact SHA256 and deployed image, not workspace commit", + "verified_runtime_image": "main-trend-factor-app:etf-v5-observation", + "verified_on": "155 at 2026-11-05 15:03 CST" +} diff --git a/app/core/etf_contract/__init__.py b/app/core/etf_contract/__init__.py new file mode 100644 index 0000000..dcfdabb --- /dev/null +++ b/app/core/etf_contract/__init__.py @@ -0,0 +1 @@ +"""固定版本的上游ETF观测契约,只含纯校验逻辑。""" diff --git a/app/core/etf_contract/consumer.py b/app/core/etf_contract/consumer.py new file mode 100644 index 0000000..0b7e344 --- /dev/null +++ b/app/core/etf_contract/consumer.py @@ -0,0 +1,457 @@ +"""Dependency-free downstream contract validation; performs no business actions. + +Use the exact immutable snapshot text fetched from the snapshot instance. +Persist sequence/revision watermarks with your business update before XACK. +""" +from __future__ import annotations + +from datetime import datetime, timedelta +import hashlib +import json +import math +import re +from typing import Mapping + +from .signals import DIRECTIONS, KNOWN_DIRECTIONS, aware_time, lease_until, validate_snapshot +from .timing import LEGACY_POLICY, admission_until, policy_version + + +ACTIVE_STATES = frozenset({"OPEN", "CONTINUE"}) +TERMINAL_STATES = frozenset({"RESOLVED", "REVERSED", "EXPIRED", "CLOSED", "RETRACTED"}) +OBSERVATION_NAMESPACE = "mtf:etf:observation:v1" +OBSERVATION_CONTRACT_VERSION = "etf-observation-1.0" +OBSERVATION_FIELDS = ("observation_contract_version", "purpose", "model_status", + "calibration_status", "threshold_version", "review_status", "calibration_review", + "release_id", "effective_from", "trade_instruction") +OBSERVATION_EVENT_FIELDS = ("publication_mode", "observation_contract_version", "purpose", "model_status", + "calibration_status", "threshold_version", "release_id", "trade_instruction") + + +class ConsumerContractError(ValueError): + """Invalid or incomplete transport data; do not acknowledge as a valid signal.""" + + +def _pairs(values): + result = {} + for key, value in values: + if key in result: + raise ConsumerContractError("DUPLICATE_JSON_FIELD") + result[key] = value + return result + + +def _json(value): + if not isinstance(value, (str, bytes)): + raise ConsumerContractError("JSON_TEXT_REQUIRED") + def invalid_constant(value): + raise ConsumerContractError("NONFINITE_JSON_NUMBER") + try: + result = json.loads(value, object_pairs_hook=_pairs, parse_constant=invalid_constant) + except (ValueError, UnicodeError) as exc: + if isinstance(exc, ConsumerContractError): + raise + raise ConsumerContractError("INVALID_JSON") from None + if not isinstance(result, dict): + raise ConsumerContractError("JSON_OBJECT_REQUIRED") + try: + json.dumps(result, allow_nan=False) + except (ValueError, TypeError): + raise ConsumerContractError("NONFINITE_JSON_NUMBER") from None + return result + + +def decode_stream_entry(fields: Mapping) -> dict: + """Decode the producer's single `data` field, with str or bytes clients. + + Subject identity must be inside the JSON. There is no fallback to outer + ts_code or another field, avoiding ambiguous producer/consumer contracts. + """ + if not isinstance(fields, Mapping): + raise ConsumerContractError("STREAM_FIELDS_REQUIRED") + keys = list(fields) + if keys not in (["data"], [b"data"]): + raise ConsumerContractError("SINGLE_DATA_ENVELOPE_REQUIRED") + return _json(fields[keys[0]]) + + +def _require(condition, reason): + if not condition: + raise ConsumerContractError(reason) + + +def _time(value): + try: + return aware_time(value) + except (ValueError, TypeError): + raise ConsumerContractError("AWARE_TIMESTAMP_REQUIRED") from None + + +def validate_publication_contract(snapshot: Mapping, namespace: str, manifest: Mapping | None = None) -> dict: + """Validate the independent observation contract without approving a model. + + Legacy shadow/live publications retain their existing policy. Observation + metadata is committed in both snapshot and manifest, never inferred from a + consumer option or from the fact that a publication is current. + """ + observation = namespace == OBSERVATION_NAMESPACE + if not observation: + _require(snapshot.get("publication_mode") != "observation", "OBSERVATION_NAMESPACE_MISMATCH") + return {} + _require(snapshot.get("publication_mode") == "observation", "PUBLICATION_MODE_MISMATCH") + _require(snapshot.get("trade_instruction") is False, "OBSERVATION_IS_NOT_TRADE_INSTRUCTION") + _require(isinstance(snapshot.get("release_id"), str) and bool(snapshot["release_id"].strip()), + "INVALID_OBSERVATION_RELEASE_ID") + _require(_time(snapshot.get("window_end")) >= _time(snapshot.get("effective_from")), + "WINDOW_PREDATES_OBSERVATION_RELEASE") + for key, expected in (("observation_contract_version", OBSERVATION_CONTRACT_VERSION), + ("purpose", "MARKET_OBSERVATION"), ("model_status", "uncalibrated"), + ("calibration_status", "PROVISIONAL")): + _require(snapshot.get(key) == expected, "INVALID_OBSERVATION_" + key.upper()) + threshold = snapshot.get("threshold_version") + _require(isinstance(threshold, str) and bool(threshold.strip()), "INVALID_OBSERVATION_THRESHOLD_VERSION") + review = snapshot.get("calibration_review") + _require(isinstance(review, dict), "INVALID_CALIBRATION_REVIEW") + _require(type(review.get("sample_count")) is int and review["sample_count"] >= 0 + and type(review.get("target_days")) is int and review["target_days"] > 0 + and type(review.get("eligible_cohort_size")) is int and review["eligible_cohort_size"] >= 0 + and isinstance(review.get("by_subject"), dict), "INVALID_CALIBRATION_SAMPLE_COUNT") + _require(all(isinstance(k, str) and type(v) is int and v >= 0 for k, v in review["by_subject"].items()) + and review["sample_count"] == min(review["by_subject"].values(), default=0), + "INVALID_CALIBRATION_SAMPLE_COUNT") + expected_review = "ACCUMULATING" if review["sample_count"] < review["target_days"] else "REVIEW_DUE" + _require(snapshot.get("review_status") == expected_review, "INVALID_OBSERVATION_REVIEW_STATUS") + for subject in snapshot.get("subjects", []): + _require(isinstance(subject, Mapping), "INVALID_OBSERVATION_SUBJECT") + observed = subject.get("observation") + if observed is not None: + _require(isinstance(observed, Mapping) and observed.get("amount_unit") == "CNY" + and observed.get("flow_basis") == "TURNOVER_NOT_NET_FLOW", "INVALID_OBSERVED_AMOUNT_BASIS") + for value_key, status_key, missing in (("amount", "window_amount_status", "MISSING"), + ("cumulative_amount", "cumulative_amount_status", "INCOMPLETE_PREFIX")): + amount = observed.get(value_key) + _require(amount is None or (type(amount) in (int, float) and math.isfinite(amount) and amount >= 0), + "INVALID_OBSERVED_AMOUNT") + _require(observed.get(status_key) == ("OK" if amount is not None else missing), + "OBSERVED_AMOUNT_STATUS_MISMATCH") + if value_key + "_yuan" in observed: + _require(observed[value_key + "_yuan"] == amount, "OBSERVED_AMOUNT_ALIAS_MISMATCH") + for scope in ("WINDOW_15M", "SESSION_CUM", "EOD"): + block = subject.get(scope) + if block is None: + continue + _require(isinstance(block, Mapping) and isinstance(block.get("metrics"), dict), + "INVALID_OBSERVATION_SCOPE") + metrics = block["metrics"] + _require(metrics.get("threshold_version") == threshold, "OBSERVATION_THRESHOLD_MISMATCH") + direction = block.get("direction") + _require(isinstance(direction, str) and direction in DIRECTIONS, "UNSUPPORTED_DIRECTION") + _require(block.get("direction_status") == ("UNAVAILABLE" if direction == "UNKNOWN" else "OK"), + "INVALID_DIRECTION_STATUS") + if direction == "UNKNOWN": + balance_key = "direction_balance" if scope == "WINDOW_15M" else "cumulative_direction_balance" + _require(all(metrics.get(key) is None for key in + (balance_key, "pressure_score", "high_buy_pressure_score", + "high_sell_pressure_score", "low_buy_pressure_score", "low_sell_pressure_score")), + "UNKNOWN_DIRECTION_HAS_PRESSURE") + fields = {key: snapshot[key] for key in OBSERVATION_FIELDS} + if manifest is not None: + _require(manifest.get("publication_mode") == "observation", "PUBLICATION_MODE_MISMATCH") + _require(all(json.dumps(manifest.get(key), sort_keys=True, allow_nan=False) + == json.dumps(value, sort_keys=True, allow_nan=False) for key, value in fields.items()), + "OBSERVATION_MANIFEST_MISMATCH") + return fields + + +def validate_observation_snapshot(manifest: Mapping, snapshot: str | bytes, now: datetime, + *, last_revision: int = -1) -> dict: + """Read numerical observations even when no anomaly event was emitted. + + This validates a READY round and returns per-subject/per-scope permissions. + ``quality_eligible`` describes the recorded facts; ``observation_eligible`` + additionally requires an unexpired, timely LIVE publication. Neither grants + trading authority or claims the reference thresholds have been calibrated. + Keep revision watermarks per namespace and round, never globally per day. + """ + _require(isinstance(manifest, Mapping), "OBJECTS_REQUIRED") + _require(type(last_revision) is int and last_revision >= -1, "INVALID_CONSUMER_WATERMARK") + snap = _json(snapshot) + for item in (manifest, snap): + _require(type(item.get("schema_version")) is int and item["schema_version"] == 1, + "UNSUPPORTED_SCHEMA_VERSION") + _require(type(item.get("revision")) is int and item["revision"] >= 0, "INVALID_REVISION") + _require(manifest.get("status") == "READY", "ROUND_NOT_READY") + raw = snapshot.encode("utf-8") if isinstance(snapshot, str) else snapshot + _require(hashlib.sha256(raw).hexdigest() == manifest.get("snapshot_sha256"), "SNAPSHOT_HASH_MISMATCH") + try: + end = validate_snapshot(snap) + version = policy_version(snap) + manifest_version = policy_version(manifest) + except (ValueError, TypeError, KeyError): + raise ConsumerContractError("INVALID_SNAPSHOT") from None + _require(manifest_version == version, "ROUND_TIMING_POLICY_MISMATCH") + for key in ("round_id", "trade_date", "revision"): + _require(manifest.get(key) == snap[key], "ROUND_IDENTITY_MISMATCH") + suffix = f"{snap['trade_date']}:{snap['round_id']}:r{snap['revision']}" + _require(manifest.get("manifest_key") == f"{OBSERVATION_NAMESPACE}:manifest:{suffix}" + and manifest.get("snapshot_key") == f"{OBSERVATION_NAMESPACE}:snapshot:{suffix}", + "NAMESPACE_OR_KEY_MISMATCH") + metadata = validate_publication_contract(snap, OBSERVATION_NAMESPACE, manifest) + _require(_time(manifest.get("window_end")) == end, "WINDOW_END_MISMATCH") + published = _time(manifest.get("published_at")) + _require(_time(snap.get("published_at")) == published, "PUBLICATION_TIME_MISMATCH") + _require(published >= end, "PUBLICATION_PRECEDES_WINDOW") + expiry = lease_until(end, version) + _require(_time(manifest.get("valid_until")) == expiry, "INVALID_MANIFEST_LEASE") + _require(_time(manifest.get("admission_until")) == admission_until(end, version), "INVALID_MANIFEST_ADMISSION") + _require(manifest.get("mode") == snap.get("mode"), "EVALUATION_MODE_MISMATCH") + _require(type(manifest.get("live_eligible")) is bool, "INVALID_LIVE_ELIGIBILITY") + _require(type(manifest.get("session_closed")) is bool and type(snap.get("session_closed")) is bool + and manifest["session_closed"] == snap["session_closed"] == (end.hour == 15), + "INVALID_SESSION_CLOSED") + current = _time(now) + reason = ("OLD_ROUND_REVISION" if snap["revision"] < last_revision else + "FUTURE_EVENT" if published > current or end > current else + "REPLAY_NOT_ACTIONABLE" if snap.get("mode") != "LIVE" else + "SESSION_CLOSED" if snap["session_closed"] else + "ADMISSION_DEADLINE_PASSED" if published >= admission_until(end, version) else + "LEASE_EXPIRED" if current >= expiry else + "ROUND_NOT_LIVE_ELIGIBLE" if not manifest["live_eligible"] else + "INSUFFICIENT_DATA" if snap.get("data_status") != "OK" else None) + scopes, identities = [], set() + for subject in snap["subjects"]: + identity = (subject.get("subject_type"), subject.get("subject_id")) + _require(identity[0] in {"ETF", "INDEX", "MARKET"} + and isinstance(identity[1], str) and bool(identity[1]), "INVALID_OBSERVATION_SUBJECT") + _require(identity not in identities, "DUPLICATE_OBSERVATION_SUBJECT") + identities.add(identity) + for scope in ("WINDOW_15M", "SESSION_CUM", "EOD"): + block = subject.get(scope) + if block is None: + continue + amount_key = "amount" if scope == "WINDOW_15M" else "cumulative_amount" + facts = subject.get("observation") + value = (facts if isinstance(facts, Mapping) else block["metrics"]).get(amount_key) + amount_valid = type(value) in (int, float) and math.isfinite(value) and value >= 0 + model_quality = subject.get("data_status") == "OK" and block.get("data_status") == "OK" + raw_quality = (isinstance(facts, Mapping) and facts.get("amount_unit") == "CNY" + and facts.get("flow_basis") == "TURNOVER_NOT_NET_FLOW" + and facts.get("window_amount_status" if scope == "WINDOW_15M" else "cumulative_amount_status") == "OK" + and subject.get("data_status") in {"OK", "INSUFFICIENT_HISTORY"} + and block.get("data_status") in {"OK", "INSUFFICIENT_HISTORY"}) + quality = amount_valid and (model_quality or raw_quality) + direction_available = model_quality and amount_valid and block["direction_status"] == "OK" + eligible = reason is None and quality + scopes.append({"subject_type": identity[0], "subject_id": identity[1], "scope": scope, + "data_status": block.get("data_status"), "quality_eligible": quality, + "model_scoring_available": model_quality, + "observation_eligible": eligible, "amount_available": quality, + "direction_available": direction_available, + "usable_fields": (["turnover"] + (["price_volume_direction"] if direction_available else [])) + if eligible else []}) + available = reason is None and any(item["observation_eligible"] for item in scopes) + return dict(metadata, contract_valid=True, namespace=OBSERVATION_NAMESPACE, + round_id=snap["round_id"], revision=snap["revision"], + published_at=published.isoformat(), valid_until=expiry.isoformat(), + observation_eligible=available, trade_instruction=False, + reason_code=reason or ("CURRENT_OBSERVATION" if available else "NO_USABLE_SCOPE"), + scopes=scopes, snapshot=snap) + + +def validate_observation_event_metadata(event: Mapping, snapshot: Mapping): + _require(all(type(event.get(key)) is type(snapshot.get(key)) and event.get(key) == snapshot.get(key) + for key in OBSERVATION_EVENT_FIELDS), "OBSERVATION_EVENT_METADATA_MISMATCH") + + +def read_committed_observation(snapshot_client, event_client, manifest_key: str, *, clock=None, + last_revision: int = -1) -> dict: + """Bounded read from the two Redis instances; no writes, API or task calls. + + Fetch READY from event Redis, then the exact immutable snapshot from snapshot + Redis. The caller may supply a clock callable; it is evaluated after reads + so network delay cannot turn an expired snapshot into a current observation. + A notification or latest pointer alone is never proof of commitment. + """ + pattern = re.escape(OBSERVATION_NAMESPACE) + r":manifest:\d{4}-\d{2}-\d{2}:[A-Za-z0-9_-]+:r\d+" + _require(isinstance(manifest_key, str) and re.fullmatch(pattern, manifest_key) is not None, + "INVALID_OBSERVATION_MANIFEST_KEY") + + def bounded(client, key, maximum): + size = client.strlen(key) + _require(type(size) is int and 0 < size <= maximum, "MISSING_OR_OVERSIZED_COMMITTED_DATA") + raw = client.getrange(key, 0, maximum - 1) + _require(isinstance(raw, (str, bytes)), "JSON_TEXT_REQUIRED") + _require(len(raw.encode("utf-8") if isinstance(raw, str) else raw) == size, + "COMMITTED_DATA_CHANGED_DURING_READ") + return raw + + encoded = bounded(event_client, manifest_key, 262_144) + manifest = _json(encoded) + _require(manifest.get("status") == "READY", "ROUND_NOT_READY") + _require(manifest.get("manifest_key") == manifest_key, "NAMESPACE_OR_KEY_MISMATCH") + expected_key = manifest_key.replace(":manifest:", ":snapshot:", 1) + _require(manifest.get("snapshot_key") == expected_key, "NAMESPACE_OR_KEY_MISMATCH") + raw = bounded(snapshot_client, expected_key, 16_777_216) + _require(bounded(event_client, manifest_key, 262_144) == encoded, "COMMITTED_DATA_CHANGED_DURING_READ") + result = validate_observation_snapshot(manifest, raw, clock() if clock else datetime.now().astimezone(), + last_revision=last_revision) + return dict(result, manifest=manifest) + + +def validate_envelope(event: Mapping, manifest: Mapping, snapshot: str | bytes, + now: datetime, *, allow_shadow: bool = False, + last_event_seq: int = 0, last_revision: int = -1) -> dict: + """Validate a committed event and return an eligibility decision. + + last_event_seq is the durable watermark for this event_id; last_revision is + the durable watermark for this round_id. A sequence gap is exposed for + reconciliation; it does not make a later terminal state unsafe to apply. + Structural failures raise ConsumerContractError. Valid but stale/duplicate + or shadow events return apply=False with an explicit reason code. + """ + _require(isinstance(event, Mapping) and isinstance(manifest, Mapping), "OBJECTS_REQUIRED") + _require(type(last_event_seq) is int and last_event_seq >= 0 + and type(last_revision) is int and last_revision >= -1, "INVALID_CONSUMER_WATERMARK") + _require(type(allow_shadow) is bool, "INVALID_SHADOW_POLICY") + snap = _json(snapshot) + for item in (event, manifest, snap): + _require(type(item.get("schema_version")) is int and item["schema_version"] == 1, + "UNSUPPORTED_SCHEMA_VERSION") + _require(type(item.get("revision")) is int and item["revision"] >= 0, "INVALID_REVISION") + try: + json.dumps(dict(event), allow_nan=False) + except (ValueError, TypeError): + raise ConsumerContractError("EVENT_MUST_BE_FINITE_JSON") from None + _require(manifest.get("status") == "READY", "ROUND_NOT_READY") + raw = snapshot.encode("utf-8") if isinstance(snapshot, str) else snapshot + _require(hashlib.sha256(raw).hexdigest() == manifest.get("snapshot_sha256"), "SNAPSHOT_HASH_MISMATCH") + try: + end = validate_snapshot(dict(snap)) + except (ValueError, TypeError, KeyError): + raise ConsumerContractError("INVALID_SNAPSHOT") from None + try: + snapshot_policy = policy_version(snap) + manifest_policy = policy_version(manifest) + event_policy = policy_version(event) + except ValueError: + raise ConsumerContractError("UNSUPPORTED_TIMING_POLICY") from None + _require(manifest_policy == snapshot_policy, "ROUND_TIMING_POLICY_MISMATCH") + for name in ("event_id", "round_id", "subject_id", "model_version", "trade_date"): + _require(isinstance(event.get(name), str) and bool(event[name]), "MISSING_EVENT_IDENTITY") + _require(event.get("producer_id") == "mtf_etf", "UNKNOWN_PRODUCER") + _require(event.get("flow_basis") == "price_volume_proxy", "UNSUPPORTED_FLOW_BASIS") + _require(isinstance(event.get("subject_type"), str) + and event["subject_type"] in {"ETF", "INDEX", "MARKET"}, "UNSUPPORTED_SUBJECT_TYPE") + _require(isinstance(event.get("scope"), str) + and event["scope"] in {"WINDOW_15M", "SESSION_CUM"}, "UNSUPPORTED_EVENT_SCOPE") + _require(isinstance(event.get("direction"), str) + and event["direction"] in DIRECTIONS, "UNSUPPORTED_DIRECTION") + direction_status = "UNAVAILABLE" if event["direction"] == "UNKNOWN" else "OK" + # Previous known-direction v1 events did not carry this additive field. + # UNKNOWN is new and must state explicitly that direction is unavailable. + _require(event.get("direction_status", "OK") == direction_status, "INVALID_DIRECTION_STATUS") + last_known = event.get("last_known_direction", event["direction"] if event["direction"] in KNOWN_DIRECTIONS else None) + _require(last_known is None or (isinstance(last_known, str) and last_known in KNOWN_DIRECTIONS), + "INVALID_LAST_KNOWN_DIRECTION") + if event["direction"] in KNOWN_DIRECTIONS: + _require(last_known == event["direction"], "LAST_KNOWN_DIRECTION_MISMATCH") + _require(type(event.get("severity")) is int and 0 <= event["severity"] <= 3, "INVALID_SEVERITY") + _require(type(event.get("event_seq")) is int and event["event_seq"] >= 1, "INVALID_EVENT_SEQUENCE") + _require(type(event.get("revision")) is int and event["revision"] >= 0, "INVALID_REVISION") + _require(isinstance(event.get("metrics"), dict), "METRICS_OBJECT_REQUIRED") + _require(isinstance(event.get("reason_codes"), list) + and all(isinstance(value, str) for value in event["reason_codes"]), "INVALID_REASON_CODES") + state = event.get("event_state") + _require(isinstance(state, str) and state in ACTIVE_STATES | TERMINAL_STATES, "UNSUPPORTED_EVENT_STATE") + if state in ACTIVE_STATES: + _require(event_policy == snapshot_policy, "ACTIVE_TIMING_POLICY_MISMATCH") + for name in ("round_id", "revision", "trade_date"): + _require(event.get(name) == manifest.get(name) == snap.get(name), "ROUND_IDENTITY_MISMATCH") + _require(_time(event.get("window_end")) == _time(manifest.get("window_end")) == end, + "WINDOW_END_MISMATCH") + _require(_time(event.get("window_start")) == end - timedelta(minutes=15), "WINDOW_START_MISMATCH") + published = _time(event.get("published_at")) + _require(published == _time(manifest.get("published_at")), "PUBLICATION_TIME_MISMATCH") + _require(published >= end, "PUBLICATION_PRECEDES_WINDOW") + valid_until = _time(event.get("valid_until")) + opened = _time(event.get("opened_at")) + _require(opened.date() == end.date() and opened <= end and opened <= valid_until <= lease_until(end, event_policy), + "INVALID_EVENT_LEASE") + _require(_time(manifest.get("valid_until")) == lease_until(end, snapshot_policy), "INVALID_MANIFEST_LEASE") + if manifest_policy != LEGACY_POLICY or "admission_until" in manifest: + _require(_time(manifest.get("admission_until")) == admission_until(end, snapshot_policy), + "INVALID_MANIFEST_ADMISSION") + _require(type(manifest.get("live_eligible")) is bool, "INVALID_LIVE_ELIGIBILITY") + manifest_key = manifest.get("manifest_key") + _require(isinstance(manifest_key, str) and ":manifest:" in manifest_key, "INVALID_MANIFEST_KEY") + namespace = manifest_key.split(":manifest:", 1)[0] + _require(namespace in {"mtf:etf:shadow:v1", "mtf:etf:live:v1", OBSERVATION_NAMESPACE}, "UNSUPPORTED_NAMESPACE") + suffix = f"{event['trade_date']}:{event['round_id']}:r{event['revision']}" + _require(manifest_key == f"{namespace}:manifest:{suffix}" + and manifest.get("snapshot_key") == f"{namespace}:snapshot:{suffix}", "NAMESPACE_OR_KEY_MISMATCH") + deployment_mode = namespace.split(":")[2] + _require(snap.get("publication_mode", deployment_mode) == deployment_mode, "PUBLICATION_MODE_MISMATCH") + _require(manifest.get("mode") == snap.get("mode", "LIVE"), "EVALUATION_MODE_MISMATCH") + metadata = validate_publication_contract(snap, namespace, manifest) + if deployment_mode == "observation": + validate_observation_event_metadata(event, snap) + observation = (validate_observation_snapshot(manifest, snapshot, now, last_revision=last_revision) + if deployment_mode == "observation" else None) + + result = {**metadata, "apply": False, "active_eligible": False, "terminal": state in TERMINAL_STATES, + "reason_code": None, "namespace": namespace, "event_id": event["event_id"], + "event_seq": event["event_seq"], "round_id": event["round_id"], + "revision": event["revision"], "event_state": state, + "direction": event["direction"], "direction_status": direction_status, + "direction_available": direction_status == "OK", + "last_known_direction": last_known, + "timing_policy_version": event_policy, + "sequence_gap": event["event_seq"] > last_event_seq + 1} + reason = None + current = _time(now) + if deployment_mode == "shadow" and not allow_shadow: + reason = "SHADOW_NAMESPACE_REJECTED" + elif event["event_seq"] <= last_event_seq: + reason = "DUPLICATE_OR_OLD_SEQUENCE" + elif event["revision"] < last_revision: + reason = "OLD_ROUND_REVISION" + elif published > current or end > current: + reason = "FUTURE_EVENT" + elif manifest.get("mode") != "LIVE": + reason = "REPLAY_NOT_ACTIONABLE" + elif state in TERMINAL_STATES: + # Closing/retracting a previously applied event remains useful after + # its lease expires and when an EOD manifest has live_eligible=False. + result["apply"] = True + reason = "TERMINAL_EVENT" + elif published >= admission_until(end, event_policy): + reason = "ADMISSION_DEADLINE_PASSED" + elif current >= valid_until: + reason = "LEASE_EXPIRED" + elif not manifest["live_eligible"]: + reason = "ROUND_NOT_LIVE_ELIGIBLE" + elif deployment_mode == "live" and snap.get("model_status") != "calibrated": + reason = "MODEL_NOT_CALIBRATED" + elif observation is not None and not observation["observation_eligible"]: + reason = observation["reason_code"] + else: + _require(event["model_version"] == snap["model_version"], "ACTIVE_MODEL_MISMATCH") + subjects = [item for item in snap["subjects"] + if item.get("subject_type") == event["subject_type"] + and item.get("subject_id") == event["subject_id"]] + _require(len(subjects) == 1, "ACTIVE_SUBJECT_MISSING_OR_DUPLICATE") + evaluation = subjects[0].get(event["scope"], {}) + _require(subjects[0].get("data_status") == "OK" + and evaluation.get("data_status", "OK") == "OK" + and evaluation.get("anomaly") is True, "ACTIVE_EVIDENCE_MISSING") + _require(event["direction"] == evaluation.get("direction", "UNKNOWN") + and direction_status == evaluation.get("direction_status", direction_status) + and event["severity"] == evaluation.get("severity", 0) + and event["metrics"] == evaluation.get("metrics", {}), "ACTIVE_EVIDENCE_MISMATCH") + result.update(apply=True, active_eligible=True) + reason = "ACTIVE_EVENT" + result["reason_code"] = reason + if observation is not None: + result.update(observation_eligible=result["active_eligible"], trade_instruction=False, + usable_fields=(["turnover"] + (["price_volume_direction"] if direction_status == "OK" else [])) + if result["active_eligible"] else []) + return result diff --git a/app/core/etf_contract/signals.py b/app/core/etf_contract/signals.py new file mode 100644 index 0000000..6766188 --- /dev/null +++ b/app/core/etf_contract/signals.py @@ -0,0 +1,266 @@ +"""Pure, versioned event lifecycle for completed ETF 15-minute windows. + +BUY/SELL describe price-volume pressure, never an order to trade. Redis key +retention is deliberately independent of the business lease on each event. +""" +from __future__ import annotations + +from copy import deepcopy +from datetime import datetime, timedelta +import hashlib +import json +from zoneinfo import ZoneInfo + +from .timing import LEGACY_POLICY, admission_until, lease_until, policy_version + + +SHANGHAI = ZoneInfo("Asia/Shanghai") +SCOPES = ("WINDOW_15M", "SESSION_CUM") +LEASE = timedelta(minutes=20) +KNOWN_DIRECTIONS = frozenset({"BUY", "SELL", "MIXED"}) +DIRECTIONS = KNOWN_DIRECTIONS | {"UNKNOWN"} + + +def aware_time(value) -> datetime: + if isinstance(value, str): + value = datetime.fromisoformat(value.replace("Z", "+00:00")) + if not isinstance(value, datetime) or value.tzinfo is None or value.utcoffset() is None: + raise ValueError("timestamps must be timezone-aware") + return value.astimezone(SHANGHAI) + + +def iso(value) -> str: + return aware_time(value).isoformat() + + +def validate_snapshot(snapshot: dict) -> datetime: + for key in ("round_id", "trade_date", "model_version", "subjects"): + if key not in snapshot: + raise ValueError("snapshot missing " + key) + for key in ("round_id", "trade_date", "model_version"): + if not isinstance(snapshot[key], str) or not snapshot[key]: + raise ValueError(key + " must be a nonempty string") + if snapshot.get("schema_version", 1) != 1: + raise ValueError("unsupported schema_version") + end = aware_time(snapshot["window_end"]) + if end.date().isoformat() != snapshot["trade_date"]: + raise ValueError("trade_date must match window_end in Asia/Shanghai") + minute = end.hour * 60 + end.minute + if (end.second or end.microsecond or end.minute % 15 + or not (585 <= minute <= 690 or 795 <= minute <= 900)): + raise ValueError("window_end must be a completed regular 15-minute trading slot") + if snapshot.get("mode", "LIVE") not in ("LIVE", "REPLAY"): + raise ValueError("mode must be LIVE or REPLAY") + if type(snapshot.get("revision", 0)) is not int or snapshot.get("revision", 0) < 0: + raise ValueError("revision must be a nonnegative integer") + if not isinstance(snapshot["subjects"], list): + raise ValueError("subjects must be a list") + policy_version(snapshot) + return end + + +def _subject_key(subject, scope): + return json.dumps([subject["subject_type"], subject["subject_id"], scope], separators=(",", ":")) + + +def _event_id(snapshot, subject, scope, direction): + identity = [snapshot["model_version"], snapshot["trade_date"], + subject["subject_type"], subject["subject_id"], scope, + iso(snapshot["window_end"]), direction] + # Preserve historical identifiers, while a new timing contract cannot + # reuse an old event identity even in a revision of the same window. + if policy_version(snapshot) != LEGACY_POLICY: + identity.append(policy_version(snapshot)) + if snapshot.get("publication_mode") == "observation": + identity.extend(("observation", snapshot["release_id"])) + return "etf_" + hashlib.sha256(json.dumps(identity, separators=(",", ":")).encode()).hexdigest()[:24] + + +def _last_known_direction(active): + """Read new and previously persisted states without inventing a direction.""" + known = active.get("last_known_direction") + if isinstance(known, str) and known in KNOWN_DIRECTIONS: + return known + direction = active.get("direction") + return direction if isinstance(direction, str) and direction in KNOWN_DIRECTIONS else None + + +class EventEngine: + """Advance one immutable round. State and returned events are JSON-compatible. + + An evaluation block has anomaly(bool), direction(BUY/SELL/MIXED/UNKNOWN), + severity(0..3), metrics(dict), optionally reason_codes(list). + UNKNOWN preserves the last known direction for a later reversal check but + never asserts that direction for the current window. MIXED is observed + balance and resets that memory to MIXED. Missing direction defaults UNKNOWN. + Missing/invalid evaluations never resolve an active event. + Private state keys tell the publisher whether a CAS write is necessary; + they are not persisted. State must be loaded separately per trade_date. + """ + + def advance(self, snapshot: dict, state: dict, published_at: datetime): + end = validate_snapshot(snapshot) + timing_policy = policy_version(snapshot) + now = aware_time(published_at) + original = {k: deepcopy(v) for k, v in (state or {}).items() if not k.startswith("_")} + if original.get("trade_date") not in (None, snapshot["trade_date"]): + raise ValueError("state must be isolated by trade_date") + version = int(original.get("state_version", 0)) + + def unchanged(): + result = deepcopy(original) + result.update(_expected_version=version, _write_state=False) + return [], result + + revision = snapshot.get("revision", 0) + last_end = aware_time(original["last_attempt_window_end"]) if original.get("last_attempt_window_end") else None + if snapshot.get("mode", "LIVE") == "REPLAY" or end > now: + return unchanged() + if last_end == end and original.get("last_round_id") != snapshot["round_id"]: + raise ValueError("one fixed window must have one stable round_id") + if last_end and (end < last_end or (end == last_end and revision <= original.get("last_revision", 0))): + return unchanged() + + current = deepcopy(original) + current.setdefault("active", {}) + current.setdefault("sequences", {}) + events = [] + + def emit(active, event_state, evaluation=None, reason=None): + eid = active["event_id"] + seq = int(current["sequences"].get(eid, active.get("event_seq", 0))) + 1 + current["sequences"][eid] = seq + active["event_seq"] = seq + event = { + "schema_version": 1, + "model_version": active["model_version"], + "baseline_version": snapshot.get("baseline_version"), + "producer_id": "mtf_etf", + "subject_type": active["subject_type"], "subject_id": active["subject_id"], + "scope": active["scope"], "trade_date": snapshot["trade_date"], + "round_id": snapshot["round_id"], "revision": revision, + "event_id": eid, "event_seq": seq, "event_state": event_state, + "timing_policy_version": policy_version(active), + "direction": active["direction"], "severity": active["severity"], + "direction_status": "UNAVAILABLE" if active["direction"] == "UNKNOWN" else "OK", + "last_known_direction": _last_known_direction(active), + "opened_at": active["opened_at"], + "window_start": (end - timedelta(minutes=15)).isoformat(), + "window_end": end.isoformat(), "published_at": now.isoformat(), + "valid_until": active["valid_until"], + "metrics": deepcopy((evaluation or {}).get("metrics", active.get("metrics", {}))), + "reason_codes": list((evaluation or {}).get("reason_codes", [])), + "flow_basis": "price_volume_proxy", + } + if snapshot.get("publication_mode") == "observation": + for field in ("publication_mode", "observation_contract_version", "purpose", "model_status", + "calibration_status", "threshold_version", "release_id", "trade_instruction"): + event[field] = snapshot[field] + if reason: + event["reason_codes"].append(reason) + if (event_state in {"OPEN", "CONTINUE"} and active["direction"] == "UNKNOWN" + and "DIRECTION_UNAVAILABLE" not in event["reason_codes"]): + event["reason_codes"].append("DIRECTION_UNAVAILABLE") + events.append(event) + return event + + # A higher revision retracts only this round's previous emissions, + # then recalculates from its saved pre-round state. Sequence numbers + # remain monotonic even when an OPEN/CONTINUE is withdrawn. + is_revision = last_end == end + if is_revision: + for old in original.get("last_round_events", []): + emit(deepcopy(old), "RETRACTED", reason="SOURCE_REVISION") + current["active"] = deepcopy(original.get("last_round_base_active", {})) + base_active = deepcopy(current["active"]) + + # A late LIVE attempt still advances the watermark. It can expire a + # lease, but cannot manufacture a fresh live signal from stale data. + fresh = end <= now < admission_until(end, timing_policy) and snapshot.get("data_status") == "OK" + closing = bool(snapshot.get("session_closed")) or (end.hour == 15 and end.minute == 0) + for key, active in list(current["active"].items()): + if closing: + emit(active, "CLOSED", reason="SESSION_CLOSED") + del current["active"][key] + elif aware_time(active["valid_until"]) <= now: + emit(active, "EXPIRED", reason="LEASE_EXPIRED") + del current["active"][key] + + if fresh and not closing: + seen = set() + for subject in snapshot["subjects"]: + for name in ("subject_type", "subject_id"): + if not subject.get(name): + raise ValueError("subject missing " + name) + identity = (subject["subject_type"], subject["subject_id"]) + if identity in seen: + raise ValueError("duplicate subject") + seen.add(identity) + if subject.get("data_status") != "OK": + continue + for scope in SCOPES: + evaluation = subject.get(scope) + if not isinstance(evaluation, dict) or evaluation.get("data_status", "OK") != "OK": + continue + if not isinstance(evaluation.get("anomaly"), bool): + continue + direction = evaluation.get("direction", "UNKNOWN") + severity = evaluation.get("severity", 0) + if not isinstance(direction, str) or direction not in DIRECTIONS: + raise ValueError("unsupported pressure direction") + direction_status = "UNAVAILABLE" if direction == "UNKNOWN" else "OK" + if evaluation.get("direction_status", direction_status) != direction_status: + raise ValueError("direction_status contradicts pressure direction") + if type(severity) is not int or not 0 <= severity <= 3: + raise ValueError("severity must be an integer from 0 through 3") + key = _subject_key(subject, scope) + active = current["active"].get(key) + if active and active["model_version"] != snapshot["model_version"]: + emit(active, "CLOSED", reason="MODEL_CHANGED") + del current["active"][key] + active = None + if active and policy_version(active) != timing_policy: + emit(active, "CLOSED", reason="TIMING_POLICY_CHANGED") + del current["active"][key] + active = None + if not evaluation["anomaly"]: + if active: + emit(active, "RESOLVED", evaluation, "VALID_NORMAL_WINDOW") + del current["active"][key] + continue + last_known = _last_known_direction(active) if active else None + if active and {last_known, direction} == {"BUY", "SELL"}: + emit(active, "REVERSED", evaluation, "PRESSURE_DIRECTION_CHANGED") + del current["active"][key] + active = None + if active is None: + active = { + "event_id": _event_id(snapshot, subject, scope, direction), + "event_seq": 0, "model_version": snapshot["model_version"], + "timing_policy_version": timing_policy, + "subject_type": subject["subject_type"], "subject_id": subject["subject_id"], + "scope": scope, "direction": direction, "severity": severity, + "direction_status": direction_status, + "last_known_direction": direction if direction in KNOWN_DIRECTIONS else None, + "opened_at": end.isoformat(), "valid_until": lease_until(end, timing_policy).isoformat(), + "metrics": deepcopy(evaluation.get("metrics", {})), + } + current["active"][key] = active + emit(active, "OPEN", evaluation) + else: + active.update(direction=direction, severity=severity, valid_until=lease_until(end, timing_policy).isoformat(), + direction_status=direction_status, + last_known_direction=direction if direction in KNOWN_DIRECTIONS else last_known, + metrics=deepcopy(evaluation.get("metrics", {}))) + emit(active, "CONTINUE", evaluation) + + current.update( + schema_version=1, trade_date=snapshot["trade_date"], state_version=version + 1, + timing_policy_version=timing_policy, + last_attempt_window_end=end.isoformat(), last_seen_at=now.isoformat(), + last_round_id=snapshot["round_id"], last_revision=revision, + last_round_base_active=base_active, + last_round_events=deepcopy([e for e in events if e["event_state"] != "RETRACTED"]), + session_closed=closing, _expected_version=version, _write_state=True, + ) + return events, current diff --git a/app/core/etf_contract/timing.py b/app/core/etf_contract/timing.py new file mode 100644 index 0000000..957cfcf --- /dev/null +++ b/app/core/etf_contract/timing.py @@ -0,0 +1,56 @@ +"""Versioned admission deadlines and fixed event leases for ETF signals. + +Missing policy metadata always means the original twenty-minute contract. +The delayed policy admits a new evaluation strictly before window_end + 10m, +then retains a legally admitted event until window_end + 30m. Neither deadline +slides on delivery/retry, and both are capped by the 15:00 market close. +""" +from __future__ import annotations + +from datetime import datetime, timedelta +from typing import Mapping +from zoneinfo import ZoneInfo + + +SHANGHAI = ZoneInfo("Asia/Shanghai") +LEGACY_POLICY = "legacy-v1" +DELAYED_POLICY = "etf-delayed-v2" +SUPPORTED_POLICIES = frozenset({LEGACY_POLICY, DELAYED_POLICY}) + + +def _version(value): + if not isinstance(value, str) or value not in SUPPORTED_POLICIES: + raise ValueError("unsupported timing_policy_version") + return value + + +def policy_version(value: Mapping) -> str: + """Read explicit metadata; absence is legacy, never the deployment default.""" + if not isinstance(value, Mapping): + raise ValueError("timing policy metadata must be a mapping") + return _version(value.get("timing_policy_version", LEGACY_POLICY)) + + +def _end(value): + if isinstance(value, str): + value = datetime.fromisoformat(value.replace("Z", "+00:00")) + if not isinstance(value, datetime) or value.tzinfo is None or value.utcoffset() is None: + raise ValueError("window_end must be timezone-aware") + return value.astimezone(SHANGHAI) + + +def lease_until(window_end, policy_version: str = LEGACY_POLICY) -> datetime: + version = _version(policy_version) + end = _end(window_end) + minutes = 30 if version == DELAYED_POLICY else 20 + close = end.replace(hour=15, minute=0, second=0, microsecond=0) + return min(end + timedelta(minutes=minutes), close) + + +def admission_until(window_end, policy_version: str = LEGACY_POLICY) -> datetime: + version = _version(policy_version) + end = _end(window_end) + if version == LEGACY_POLICY: + return lease_until(end, version) + close = end.replace(hour=15, minute=0, second=0, microsecond=0) + return min(end + timedelta(minutes=10), close) diff --git a/app/observation_worker.py b/app/observation_worker.py new file mode 100644 index 0000000..57a26e4 --- /dev/null +++ b/app/observation_worker.py @@ -0,0 +1,24 @@ +"""ETF观测单进程入口,不启动交易worker或QMT连接。""" +import logging +import signal +from threading import Event + + +def main(): + from app.services.etf_observation import consume_once + logging.basicConfig(level=logging.INFO) + stopping = Event() + signal.signal(signal.SIGTERM, lambda *_: stopping.set()) + signal.signal(signal.SIGINT, lambda *_: stopping.set()) + while not stopping.is_set(): + try: + result = consume_once() + if result.get('status') == 'unavailable': + logging.warning('ETF观测暂不可用:%s', result.get('error')) + except Exception as exc: + logging.warning('ETF观测保存失败:%s', type(exc).__name__) + stopping.wait(15) + + +if __name__ == '__main__': + main() diff --git a/app/services/etf_observation.py b/app/services/etf_observation.py new file mode 100644 index 0000000..bb93fe5 --- /dev/null +++ b/app/services/etf_observation.py @@ -0,0 +1,264 @@ +"""正式ETF观测独立消费者。持久化后确认;不导入交易、信号执行或QMT服务。""" +from __future__ import annotations + +import copy +import hashlib +import json +import re +from datetime import datetime, timedelta +from zoneinfo import ZoneInfo + +from app.core.etf_contract import consumer as contract +from app.repo import rotation_repo as store +from app.services import param_store +from config.settings import settings + +TZ = ZoneInfo('Asia/Shanghai') +NS = contract.OBSERVATION_NAMESPACE +GROUP = 'pms-etf-observation-v1' +CONSUMER = 'pms-observation' +PREFIX = 'etf-observation:' +_clients = {} + + +def now(): + return datetime.now(TZ) + + +def day_key(day): + return PREFIX + 'day:' + day + + +def initial(day): + return {'day': day, 'namespace': NS, 'rounds': {}, 'sequences': {}, 'events': {}, + 'history': [], 'summary': None, 'last_success_at': None, 'error': None} + + +def clients(): + import redis + pair = (settings.PMS_ETF_SNAPSHOT_REDIS_URL, settings.PMS_ETF_EVENT_REDIS_URL) + if not all(pair): + raise ValueError('ETF观测连接未配置') + if pair not in _clients: + _clients[pair] = tuple(redis.Redis.from_url(url, protocol=2, decode_responses=True, + socket_connect_timeout=3, socket_timeout=5) for url in pair) + return _clients[pair] + + +def bounded(client, key, maximum): + size = client.strlen(key) + if type(size) is not int or not 0 < size <= maximum: + raise ValueError('观测提交数据缺失或超限') + raw = client.getrange(key, 0, maximum - 1) + if not isinstance(raw, str) or len(raw.encode()) != size: + raise ValueError('观测读取期间数据变化') + return raw + + +def bundle(snapshots, events, key): + if not re.fullmatch(re.escape(NS) + r':manifest:\d{4}-\d{2}-\d{2}:\d{8}T\d{4}:r\d+', key): + raise ValueError('非正式ETF提交键') + encoded = bounded(events, key, 262144) + manifest = contract._json(encoded) + expected = key.replace(':manifest:', ':snapshot:', 1) + if manifest.get('manifest_key') != key or manifest.get('snapshot_key') != expected: + raise ValueError('观测提交身份不匹配') + raw = bounded(snapshots, expected, 16777216) + if bounded(events, key, 262144) != encoded: + raise ValueError('观测提交版本变化') + return manifest, raw + + +def subject_allowed(kind, code): + if kind == 'MARKET': + return code == 'BROAD_ETF' + return kind in ('ETF', 'INDEX') and bool(re.fullmatch(r'\d{6}\.(SH|SZ)', str(code))) + + +def archive_round(manifest, raw): + key = PREFIX + 'round:' + manifest['trade_date'] + ':' + manifest['round_id'] + ':r' + str(manifest['revision']) + store.put_once(key, 'etf_observation_round', {'manifest': manifest, 'snapshot_text': raw}) + + +def summary_of(result): + snap = result['snapshot'] + market = next((s for s in snap['subjects'] if s['subject_type'] == 'MARKET' and s['subject_id'] == 'BROAD_ETF'), {}) + scopes = {p['scope']: p for p in result['scopes'] if p['subject_type'] == 'MARKET' and p['subject_id'] == 'BROAD_ETF'} + return {key: result.get(key) for key in ('round_id', 'revision', 'published_at', 'valid_until', 'reason_code', + 'observation_eligible', 'model_status', 'calibration_status', 'review_status', 'calibration_review')} | { + 'trade_date': snap['trade_date'], 'window_end': snap['window_end'], 'session_closed': snap['session_closed'], + 'market': market, 'scopes': scopes, 'member_count': market.get('member_count'), + 'trade_instruction': False} + + +def save_snapshot(manifest, raw, at): + day = manifest['trade_date'] + with store.locked(day_key(day), 'etf_observation_state', initial(day)) as state: + revision = state['rounds'].get(manifest['round_id'], -1) + result = contract.validate_observation_snapshot(manifest, raw, at, last_revision=revision) + if result['reason_code'] in ('OLD_ROUND_REVISION', 'FUTURE_EVENT', 'REPLAY_NOT_ACTIONABLE'): + return + archive_round(manifest, raw) + state['rounds'][manifest['round_id']] = max(revision, manifest['revision']) + summary = summary_of(result) + old = state.get('summary') + if old is None or (summary['window_end'], summary['revision']) >= (old['window_end'], old['revision']): + state['summary'] = summary + history = {s['round_id']: s for s in state.get('history', [])} + history[summary['round_id']] = summary + state['history'] = sorted(history.values(), key=lambda s: s['window_end'])[-16:] + + +def apply_event(stream_id, fields, manifest, raw, at): + event = contract.decode_stream_entry(fields) + if not subject_allowed(event.get('subject_type'), event.get('subject_id')): + raise ValueError('ETF观测主体不在沪深范围') + audit_key = PREFIX + 'event:' + event['trade_date'] + ':' + stream_id + # 已提交未ACK时重放,不改变第一次处理结论或实际接收时间。 + if store.get(audit_key)[0] is not None: + return + day = event['trade_date'] + with store.locked(day_key(day), 'etf_observation_state', initial(day)) as state: + if store.get(audit_key)[0] is not None: + return + last = state['sequences'].get(event['event_id'], 0) + decision = contract.validate_envelope(event, manifest, raw, at, last_event_seq=last, + last_revision=state['rounds'].get(event['round_id'], -1)) + archive_round(manifest, raw) + store.put_once(audit_key, 'etf_observation_event', {'stream_id': stream_id, 'namespace': NS, + 'raw': fields, 'decision': decision, 'received_at': at.isoformat()}) + if decision['reason_code'] not in ('FUTURE_EVENT', 'OLD_ROUND_REVISION', 'REPLAY_NOT_ACTIONABLE'): + state['sequences'][event['event_id']] = max(last, event['event_seq']) + state['rounds'][event['round_id']] = max(state['rounds'].get(event['round_id'], -1), event['revision']) + if decision['sequence_gap']: + state['sequence_gap'] = True + if decision['apply']: + previous = state['events'].get(event['event_id']) or {} + subjects = json.loads(raw)['subjects'] + subject = next((s for s in subjects if (s['subject_type'], s['subject_id']) == + (event['subject_type'], event['subject_id'])), {}) + state['events'][event['event_id']] = {'event': event, 'decision': decision, + 'received_at': at.isoformat(), 'first_received_at': previous.get('first_received_at', at.isoformat()), + 'observation': subject.get('observation', {}), 'member_count': subject.get('member_count')} + + +def consume_once(snapshots=None, events=None, clock=now): + if not param_store.get_bool('PMS_ETF_OBSERVATION_ENABLED', False): + return {'status': 'disabled'} + snapshots, events = (snapshots, events) if snapshots is not None else clients() + day = clock().astimezone(TZ).date().isoformat() + stream = NS + ':events:' + day + processed = 0 + failures = [] + try: + try: + events.xgroup_create(stream, GROUP, id='0-0', mkstream=True) + except Exception as exc: + if 'BUSYGROUP' not in str(exc): + raise + # 固定消费者名,重启先读取自己的未确认项;不触碰别的消费组。 + for start in ('0-0', '>'): + for _key, rows in events.xreadgroup(GROUP, CONSUMER, {stream: start}, count=100): + for stream_id, fields in rows: + try: + event = contract.decode_stream_entry(fields) + if event.get('trade_date') != day: + raise ValueError('事件日期与流日期不一致') + key = f"{NS}:manifest:{day}:{event['round_id']}:r{event['revision']}" + manifest, raw = bundle(snapshots, events, key) + apply_event(stream_id, fields, manifest, raw, clock()) + # put_once、状态和水位已经持久化;数据库故障时不能走到这里。 + events.xack(stream, GROUP, stream_id) + processed += 1 + except Exception as exc: + failures.append(type(exc).__name__) + pointer = snapshots.get(NS + ':latest') + keys = [] + if pointer: + manifest = contract._json(pointer) + if manifest.get('trade_date') == day: + keys.append(manifest['manifest_key']) + # 收盘快照不推进活动latest,显式检查当日固定收盘轮及其最高可见修订。 + if clock().astimezone(TZ).hour >= 15: + close_keys = list(events.scan_iter(match=f'{NS}:manifest:{day}:{day.replace("-", "")}T1500:r*', count=20)) + if close_keys: + keys.append(max(close_keys, key=lambda k: int(k.rsplit(':r', 1)[1]))) + for key in dict.fromkeys(keys): + manifest, raw = bundle(snapshots, events, key) + save_snapshot(manifest, raw, clock()) + with store.locked(day_key(day), 'etf_observation_state', initial(day)) as state: + if failures: + state.update(error=failures[0], last_failure_at=clock().isoformat()) + else: + state.update(last_success_at=clock().isoformat(), error=None) + return {'status': 'unavailable' if failures else 'ok', 'error': failures[0] if failures else None, 'processed': processed} + except Exception as exc: + # 不输出连接串、上游原文或密钥。坏包保留pending,下一轮补齐后恢复。 + with store.locked(day_key(day), 'etf_observation_state', initial(day)) as state: + state['error'] = type(exc).__name__ + state['last_failure_at'] = clock().isoformat() + return {'status': 'unavailable', 'error': type(exc).__name__, 'processed': processed} + + +def name_map(): + """只读ETF基础资料;缺名显示代码,不借用同代码股票名称。""" + from app.db import session as db + try: + rows = db.fetch_all('SELECT ts_code,csname,index_code,index_name FROM gp_etf_basic WHERE ts_code LIKE :sh OR ts_code LIKE :sz', {'sh': '%.SH', 'sz': '%.SZ'}) + result = {} + for row in rows: + code = str(row.get('ts_code') or '') + if subject_allowed('ETF', code): + result[code] = str(row.get('csname') or code) + index = str(row.get('index_code') or '') + if subject_allowed('INDEX', index): + result[index] = str(row.get('index_name') or index) + return result + except Exception: + return {} + + +def short_name(name, code): + text = str(name or code) + for old, new in [('工银瑞信', '工银'), ('华泰柏瑞', '华泰'), ('上证科创板50成份增强策略', '科创50'), + ('上证科创板50成份', '科创50'), ('中证A500', 'A500'), ('中证2000', '2000'), ('交易型开放式指数证券投资基金', ''), + ('交易型开放式指数', ''), ('证券投资基金', ''), ('ETF', '')]: + text = text.replace(old, new) + return text if 0 < sum(1 if ord(c) < 128 else 2 for c in text) <= 10 else code.split('.')[0] + + +def presentation(state, at, names=None): + names = names or {} + data = copy.deepcopy(state) + summary = data.get('summary') + healthy = bool(data.get('last_success_at')) and (at - datetime.fromisoformat(data['last_success_at'])).total_seconds() <= 90 and not data.get('error') + live = bool(summary and any(p.get('observation_eligible') for p in summary.get('scopes', {}).values()) and datetime.fromisoformat(summary['valid_until']) > at) + data['status'] = ('unavailable' if not healthy else 'waiting' if not summary else + 'closed' if summary.get('session_closed') else 'live' if live else 'expired') + rows = [] + for item in data.get('events', {}).values(): + e = item['event'] + full = names.get(e['subject_id']) or ('宽基ETF' if e['subject_type'] == 'MARKET' else e['subject_id']) + stale = e['event_state'] not in contract.ACTIVE_STATES or at >= datetime.fromisoformat(e['valid_until']) + rows.append({**e, 'name': full if e['subject_type'] == 'INDEX' and len(full) <= 9 else short_name(full, e['subject_id']), 'full_name': full, + 'observation': item['observation'], 'member_count': item.get('member_count'), + 'received_at': item['received_at'], 'stale': stale, + 'display_state': 'EXPIRED' if stale and e['event_state'] in contract.ACTIVE_STATES else e['event_state']}) + data.update(events=sorted(rows, key=lambda e: (e['published_at'], e['event_id']), reverse=True), + enabled=True, trade_instruction=False, server_at=at.isoformat(), current_eligible=live) + data.pop('sequences', None) + data.pop('rounds', None) + return data + + +_view_names = {'at': None, 'data': {}} + + +def status(): + if not param_store.get_bool('PMS_ETF_OBSERVATION_ENABLED', False): + return {'enabled': False, 'status': 'disabled', 'events': [], 'trade_instruction': False} + at = now() + state = store.get(day_key(at.date().isoformat()))[0] or initial(at.date().isoformat()) + if _view_names['at'] is None or (at - _view_names['at']).total_seconds() >= 3600: + _view_names.update(at=at, data=name_map()) + return presentation(state, at, _view_names['data']) diff --git a/app/services/param_store.py b/app/services/param_store.py index 298308a..092ec24 100644 --- a/app/services/param_store.py +++ b/app/services/param_store.py @@ -36,7 +36,7 @@ INFRA_PREFIX = ("PROXY_DB", "SOURCE_DB", "DB_MYSQL", "SIGNAL_REDIS", "PMS_REDIS" # 2026-09-03 补进会话票签名密钥 PMS_SESSION_SECRET: 它 2026-08 随登录一起加进 settings, # 当时漏了这份名单, 于是参数设置页把它当普通业务参数原文显示出来 (登录中间件读的是 # settings, 功能没受影响, 但密钥不该出现在页面上)。 -SECRET_KEYS = ("PMS_QMT_SIGN_SEED_HEX", "PMS_QMT_PEER_PUBKEY_B64", "PMS_SESSION_SECRET", "PMS_PLAN_WRITE_KEY", "PMS_TUSHARE_TOKEN") +SECRET_KEYS = ("PMS_QMT_SIGN_SEED_HEX", "PMS_QMT_PEER_PUBKEY_B64", "PMS_SESSION_SECRET", "PMS_PLAN_WRITE_KEY", "PMS_TUSHARE_TOKEN", "PMS_ETF_SNAPSHOT_REDIS_URL", "PMS_ETF_EVENT_REDIS_URL") LIMIT_FLAGS = ('PMS_LIMIT_CACHE_ENABLED', 'PMS_LIMIT_COLLECT_ENABLED') @@ -46,6 +46,8 @@ def _valid_limit_flag(value): # 运行态开关: key -> (默认值, 类型, 说明) RUNTIME_EXTRA = { + "PMS_SIGNAL_FOCUS_ENABLED": (False, bool, "持仓与有效关注共用完整信号读取,不扩大交易资格"), + "PMS_ETF_OBSERVATION_ENABLED": (False, bool, "接入正式ETF观测,仅展示,不生成交易指令"), 'PMS_RISK_EXIT_ENABLED': (False, bool, '原策略退出纪律:第一次清仓级信号停买,第二次独立信号清仓;买方整体评级下降直接清仓'), 'PMS_RISK_EXIT_START_AT': ('', str, '退出纪律启用时刻,北京时间;只接收此后有效新信号,既有真实停买最多承接第一次'), 'PMS_RISK_SIGNAL_MAX_AGE_SEC': (300, int, '清仓信号最大时龄,秒;消息期限更短时服从消息期限'), diff --git a/app/services/upstream_signals.py b/app/services/upstream_signals.py index 85aef17..3531f18 100644 --- a/app/services/upstream_signals.py +++ b/app/services/upstream_signals.py @@ -18,6 +18,7 @@ import json import logging import time from datetime import datetime +from zoneinfo import ZoneInfo from config.settings import settings from app.services import param_store @@ -26,6 +27,82 @@ logger = logging.getLogger("pms.upstream") _clients = {} _cache = {"at": 0.0, "data": None} +_focus_cache = {'at': 0., 'codes': set(), 'error': None} +_focused_streams = {} + + +def _entry_code(fields): + from app.core.command_spec import normalize_code, is_stock_code + payload = fields.get('data') + try: + payload = json.loads(payload) if isinstance(payload, str) else payload or {} + except (ValueError, TypeError): + payload = {} + payload = payload if isinstance(payload, dict) else {} + code = normalize_code(str(fields.get('ts_code') or fields.get('code') or payload.get('ts_code') or '')) + return code if code.endswith(('.SH', '.SZ')) and is_stock_code(code) else '' + + +def _focus_codes(): + """持仓及已完成买方评析的有效关注;交易白名单保持原样。""" + if not param_store.get_bool('PMS_SIGNAL_FOCUS_ENABLED', False): + return set() + if time.time() - _focus_cache['at'] < 30: + return _focus_cache['codes'] + from app.repo import pms_repo + from app.services import monitor_watch + codes, errors = set(), [] + try: + codes.update(_norm_code(r['ts_code']) for r in pms_repo.list_positions(only_open=True)) + except Exception: + errors.append('持仓名单读取失败') + try: + scope = monitor_watch.scope() + if scope: + raw = _c208(settings.SIGNAL_REDIS_DB_INTRADAY).get(monitor_watch.SOURCE_KEY + scope) + doc = json.loads(raw or 'null') + at = datetime.now(ZoneInfo('Asia/Shanghai')) + if (not isinstance(doc, dict) or doc.get('schema_version') != 1 or doc.get('source_scope') != scope + or not monitor_watch._time(doc.get('generated_at')) or not monitor_watch._time(doc.get('expires_at')) + or not monitor_watch._time(doc['generated_at']) <= at < monitor_watch._time(doc['expires_at']) + or (monitor_watch._time(doc['expires_at']) - monitor_watch._time(doc['generated_at'])).total_seconds() > 600): + raise ValueError('关注名单失效') + for row in doc.get('items', []): + if row.get('active') is True and row.get('review_status') == 'ready' and str(row.get('expires_on') or '') >= at.date().isoformat(): + codes.add(_norm_code(row.get('ts_code'))) + except Exception: + errors.append('关注覆盖名单暂不可用') + from app.core.command_spec import is_stock_code + codes = {c for c in codes if c.endswith(('.SH', '.SZ')) and is_stock_code(c)} + _focus_cache.update(at=time.time(), codes=codes, error=';'.join(errors) or None) + return 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))) + 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} + for old in list(_focused_streams): + if old != cache_key and time.time() - _focused_streams[old]['at'] > 60: + del _focused_streams[old] + return result def _to() -> int: @@ -116,10 +193,10 @@ def _read_intraday(c, ymd): 其余归「决策系统买卖」(bionic 广播 ENTRY/EXIT · BUY/SELL)。""" key = "intraday_signals:%s" % ymd decision, timing = [], [] - for _id, f in c.xrevrange(key, count=_limit()): + for _id, f in _entries(c, key, _limit()): producer = str(f.get("producer_id") or "") is_itd = ("intraday_timing" in producer) or (f.get("entry_score") is not None) - row = {"ts_code": f.get("ts_code"), "action": f.get("action"), + row = {"id": _id, "ts_code": f.get("ts_code"), "action": f.get("action"), "signal_type": f.get("signal_type"), "price": _num(f.get("suggested_price")), "target": _num(f.get("target_price")), "confidence": _num(f.get("confidence")), "entry_score": _num(f.get("entry_score")), "time": _hm(f.get("trigger_time"))} @@ -156,7 +233,8 @@ def _read_alerts(c, ymd): now = time.time() counts = {} # cat -> 已收数量; 每类只留最新 per_cat 条(仍按 newest-first) out = [] - for _id, f in c.xrevrange(key, count=_alert_scan()): + focused = _focus_codes() + for _id, f in _entries(c, key, _alert_scan()): src = str(f.get("source") or "") meta = f.get("metadata") if not isinstance(meta, dict): @@ -165,7 +243,7 @@ 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: # 该类已满 → 跳过, 高频源不挤掉别的类 + 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 为准, 拿不到就 — @@ -181,8 +259,9 @@ 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 - counts[cat] = counts.get(cat, 0) + 1 - out.append({"ts_code": f.get("ts_code"), "source": src, "direction": d, + 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")), "time": _hm(f.get("trigger_time")), "age_min": age_min, "stale": stale}) @@ -195,7 +274,7 @@ def _read_metrics(c): mtf 侧字段: direction(inflow/outflow), z_dd(横截面异动z), window_net(净额), window_ret(区间涨跌); 无原生 level。""" key = "mtf:intraday:stream:metrics" out = [] - for _id, f in c.xrevrange(key, count=_limit()): + for _id, f in _entries(c, key, _limit()): code = f.get("ts_code") or f.get("code") payload = f.get("data") if isinstance(payload, str): @@ -206,7 +285,12 @@ def _read_metrics(c): if not isinstance(payload, dict): payload = {} g = lambda k, d=None: payload.get(k, f.get(k, d)) - out.append({"ts_code": code, "direction": g("direction"), + ms = _id_ms(_id) + age = max(0, time.time() - ms / 1000) if ms else None + out.append({"id": _id, "ts_code": code or payload.get('ts_code'), "direction": g("direction"), + "time": _hm(ms), "ts": ms, "ymd": _ymd_of(ms), "today": _ymd_of(ms) == datetime.now().strftime('%Y-%m-%d'), + "ts_from": "stream_id", "age_min": int(age // 60) if age is not None else None, + "stale": bool(_alert_max_age_sec() and age is not None and age > _alert_max_age_sec()), "z_dd": _num(g("z_dd")), "window_net": _num(g("window_net")), "window_ret": _num(g("window_ret")), "level": g("level"), "value": _num(g("value") if g("value") is not None else g("net")), @@ -228,7 +312,7 @@ def _read_sell_actions(c): key = "bionic:signals:llm_sell_actions" today = time.strftime("%Y-%m-%d") out = [] - for _id, f in c.xrevrange(key, count=_limit()): + for _id, f in _entries(c, key, _limit()): raw = f.get("data") d = f if raw: @@ -238,7 +322,7 @@ def _read_sell_actions(c): d = {} ts = d.get("timestamp") or d.get("trigger_time") or f.get("ts") ymd = _ymd_of(ts) - out.append({"ts_code": d.get("ts_code") or f.get("ts_code"), + out.append({"id": _id, "ts_code": d.get("ts_code") or f.get("ts_code"), "action": d.get("action") or f.get("action"), "confidence": _num(d.get("confidence")), "dominant_signal": d.get("dominant_signal"), @@ -280,6 +364,9 @@ def snapshot(force: bool = False) -> dict: _try("sell_actions", lambda: _read_sell_actions(_c208(dba))) _try("mr", lambda: _read_mr(_c214())) + 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())} _cache["at"], _cache["data"] = now, out return out diff --git a/app/web/main.py b/app/web/main.py index b4615f2..6f5c382 100644 --- a/app/web/main.py +++ b/app/web/main.py @@ -894,6 +894,13 @@ def api_upstream_signals(): return ok(upstream_signals.snapshot) +@app.get("/api/etf/observation") +def api_etf_observation(): + """读取本实例已验证的ETF观测,不发任务或交易命令。""" + from app.services import etf_observation + return ok(etf_observation.status) + + @app.get("/api/upstream/signals/{ts_code}") def api_upstream_signals_by_code(ts_code: str): """一只票**今天的全部**上游信号 (2026-09-09, 台账 055)。 diff --git a/app/web/static/index.html b/app/web/static/index.html index ea7250a..fcd5dea 100644 --- a/app/web/static/index.html +++ b/app/web/static/index.html @@ -247,7 +247,7 @@ pre.json{background:var(--surface-2);border:1px solid var(--hair);border-radius: @media(max-width:1350px){ .hd .hd-exec,.hd .hd-tech{display:none;} } @media(max-width:1100px){ .hd .hd-plan{display:none;} } @media(max-width:1000px){ .hd{flex-wrap:wrap;} .hd-tags{order:3;flex:1 0 100%;overflow-x:auto;} .hd-tags::-webkit-scrollbar{display:none;} } -@media(max-width:600px){ .hd-tags > .hst:not(.hd-macro):not(.hd-micro){display:none;} .hd .hd-acts{gap:4px;} .hd .tbtn{padding:4px 7px;} } +@media(max-width:600px){ .hd-tags > .hst:not(.hd-macro):not(.hd-micro):not(.hd-etf){display:none;} .hd .hd-acts{gap:4px;} .hd .tbtn{padding:4px 7px;} } @media(max-width:1100px){ .hd .hd-exec{display:none;} .hd .hd-tech{display:none;} } @media(max-width:1000px){ .hd .hd-buy{display:none;} } @@ -591,6 +591,8 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;} .cs-research-note{font-size:12px;color:var(--muted);line-height:1.5;overflow-wrap:anywhere;margin-top:5px} .plan-add-actions{display:flex;justify-content:flex-end;gap:8px;margin-top:16px} @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}} @@ -669,6 +671,21 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;} 微盘 {{ microChipText }} + + + + 下发 {{ dm.mode || '-' }} @@ -827,36 +844,43 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;} {{ L.label }} {{ levelCount(L.key) }} + ? +
+
ETF观测 · {{ etfActiveCount }}项查看明细 ›
+
-