修复关注信号补显并接入ETF观测
This commit is contained in:
parent
2885da891c
commit
f6cf445586
|
|
@ -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)。
|
||||
|
|
|
|||
|
|
@ -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)。
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
"""固定版本的上游ETF观测契约,只含纯校验逻辑。"""
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
@ -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()
|
||||
|
|
@ -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'])
|
||||
|
|
@ -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, '清仓信号最大时龄,秒;消息期限更短时服从消息期限'),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)。
|
||||
|
|
|
|||
|
|
@ -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}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
@ -669,6 +671,21 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}
|
|||
</div>
|
||||
</template>
|
||||
<a class="hst hd-micro" :class="microChipClass" href="/rotation" target="_blank" rel="noopener noreferrer" style="text-decoration:none">微盘 {{ microChipText }}</a>
|
||||
</el-tooltip>
|
||||
<el-tooltip v-if="etf.enabled" placement="bottom" effect="light" :show-after="200" :enterable="true">
|
||||
<template #content><div class="market-popover etf-popover">
|
||||
<div class="trend-heading"><b>宽基ETF观测</b><span>仅观测</span></div>
|
||||
<div class="trend-meta"><span>{{ etfTime(etfSummary.window_end) }}窗口 · {{ etfTime(etfSummary.published_at) }}更新</span><span>{{ etfStatusText }}</span></div>
|
||||
<div class="etf-total"><b>{{ etfLive ? etfAmount(etfFacts.cumulative_amount) : '—' }}</b><span class="muted">亿元 · 今日累计</span></div>
|
||||
<div class="trend-meta"><span>相当于常日 <b>{{ etfLive ? etfRatio(etfFacts.cumulative_to_full_day_median20) : '—' }}</b></span><span>本窗 <b>{{ etfLive ? etfAmount(etfFacts.amount) : '—' }}</b> 亿元</span></div>
|
||||
<div class="trend-section"><div class="trend-heading"><b>十五分钟成交额</b><span>亿元</span></div>
|
||||
<div class="etf-bars"><div v-for="bar in etfBars" :key="bar.key" class="etf-bar" :class="{missing:bar.missing}" :style="{height:bar.height+'px'}" :title="bar.title"><i>{{ bar.value }}</i><span>{{ bar.time }}</span></div></div>
|
||||
</div>
|
||||
<div class="trend-foot">{{ etfOverall }} · 当前异动{{ etfActiveCount }}项</div>
|
||||
<div class="trend-foot">常日基准:前20个完整交易日全日中位数</div>
|
||||
<div class="trend-meta"><span>阈值待校准</span><button class="tbtn" @click="openEtf()">查看明细 ›</button></div>
|
||||
</div></template>
|
||||
<button class="hst hd-etf" :class="etfLive?'ok':'etf-missing'" @click="openEtf()">ETF {{ etfChipText }}</button>
|
||||
</el-tooltip>
|
||||
<span class="hst" :class="dm.shadow?'idle':'ok'" v-show="mode==='ops'" title="下发模式。实盘=真的发到 QMT 下单;影子=只记录,不真下单,用来观察。">下发 {{ dm.mode || '-' }}</span>
|
||||
</div>
|
||||
|
|
@ -827,36 +844,43 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}
|
|||
<span v-for="L in ALERT_LEVELS" :key="L.key" class="lv-chip"
|
||||
:class="levelShown(L.key)?('on lv-'+L.cls):'off'" @click="toggleLevel(L.key)">
|
||||
{{ L.label }} <span style="opacity:.7">{{ levelCount(L.key) }}</span></span>
|
||||
<button class="lv-chip sig-observe" :class="showObservations?'on':'off'" :aria-pressed="showObservations" @click="showObservations=!showObservations">观测 {{ observationCount }}</button>
|
||||
<span class="help" data-tip="点一下可以把这一档的信号收起来或放出来。级别说的是信号有多强,不是看涨看跌。">?</span>
|
||||
</div>
|
||||
|
||||
<!-- 速览: 持仓和用户关注合并置顶, 其余只收重要和警告 -->
|
||||
<template v-if="sigTab==='brief'">
|
||||
<div class="sig-fail" v-if="upstream.focus && (upstream.focus.error || upstream.focus.truncated)">{{ upstream.focus.error || '信号回扫达到上限,部分早盘记录可能不完整' }}</div>
|
||||
<div class="sig-fail" v-if="srcFail('metrics')">资金观测读取失败,不能当作没有信号。</div>
|
||||
<div class="sig-fail" v-if="srcFail('alerts') || srcFail('intraday')">
|
||||
上游信号读不到({{ (srcFail('alerts')||srcFail('intraday')).error }})。
|
||||
这不是「今天没有信号」,是这一路取不到数——不要当成一切正常。</div>
|
||||
<div class="muted" v-else-if="!sigDisplayToday.length">今天暂无信号。</div>
|
||||
<template v-if="sigDisplayToday.length">
|
||||
<div v-for="(g,i) in sigBriefHeld" :key="'h'+i" class="sigrow2 signal-flow" :class="{old:g.stale}">
|
||||
<div v-for="(g,i) in sigBriefHeld" :key="'h'+i" class="sigrow2 signal-flow" :class="{old:g.stale,'etf-flow':g.flowKind==='etf'}">
|
||||
<span class="t mono" :title="g.time">{{ flowTime(g) }}</span>
|
||||
<span v-if="flowLabel(g)" class="pin" :class="{plan:flowLabel(g)!=='持仓'}">{{ flowLabel(g) }}</span>
|
||||
<button class="nm" :title="g.ts_code+' · 查看信号与研究'" @click.stop="openCodeSignals(g.ts_code)">{{ nm(g.ts_code) }}</button>
|
||||
<button class="nm" :class="{'etf-name':g.flowKind==='etf'}" :title="g.full_name || g.ts_code+' · 查看信号与研究'" @click.stop="openFlowSignal(g)">{{ g.flowKind==='etf' ? g.name : nm(g.ts_code) }}</button>
|
||||
<span class="cat">{{ g.cat }}</span>
|
||||
<span v-if="g.direction" class="direction" :class="dirCls(g.direction)">{{ g.direction }}</span>
|
||||
<span class="sp"></span>
|
||||
<span v-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
<span v-if="g.flowKind==='etf'" class="disp etf-state" :class="g.stale?'wait':g.event_state==='OPEN'?'queue':'strong'">{{ etfStateText(g.display_state) }}</span>
|
||||
<span v-else-if="g.flowKind==='metrics'" class="disp wait">观测</span>
|
||||
<span v-else-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
</div>
|
||||
<div class="muted" v-if="sigBriefOther.length"
|
||||
style="margin:10px 0 5px;font-weight:600;font-size:11.5px;border-top:1px dashed var(--hair);padding-top:8px">
|
||||
其他股票</div>
|
||||
<div v-for="(g,i) in sigBriefOther" :key="'o'+i" class="sigrow2 signal-flow" :class="{old:g.stale}">
|
||||
其他信号</div>
|
||||
<div v-for="(g,i) in sigBriefOther" :key="'o'+i" class="sigrow2 signal-flow" :class="{old:g.stale,'etf-flow':g.flowKind==='etf'}">
|
||||
<span class="t mono" :title="g.time">{{ flowTime(g) }}</span>
|
||||
<span v-if="flowLabel(g)" class="pin" :class="{plan:flowLabel(g)!=='持仓'}">{{ flowLabel(g) }}</span>
|
||||
<button class="nm" :title="g.ts_code+' · 查看信号与研究'" @click.stop="openCodeSignals(g.ts_code)">{{ nm(g.ts_code) }}</button>
|
||||
<button class="nm" :class="{'etf-name':g.flowKind==='etf'}" :title="g.full_name || g.ts_code+' · 查看信号与研究'" @click.stop="openFlowSignal(g)">{{ g.flowKind==='etf' ? g.name : nm(g.ts_code) }}</button>
|
||||
<span class="cat">{{ g.cat }}</span>
|
||||
<span v-if="g.direction" class="direction" :class="dirCls(g.direction)">{{ g.direction }}</span>
|
||||
<span class="sp"></span>
|
||||
<span v-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
<span v-if="g.flowKind==='etf'" class="disp etf-state" :class="g.stale?'wait':g.event_state==='OPEN'?'queue':'strong'">{{ etfStateText(g.display_state) }}</span>
|
||||
<span v-else-if="g.flowKind==='metrics'" class="disp wait">观测</span>
|
||||
<span v-else-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
</div>
|
||||
<div v-if="sigToday.some(g=>g.stale)" class="muted" style="margin-top:9px;font-size:11.5px">
|
||||
淡色信号已过时,仅供回顾。</div>
|
||||
|
|
@ -868,17 +892,22 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}
|
|||
<div v-for="(g,i) in sigOld.slice(0,60)" :key="'d'+i" class="sigrow2 signal-flow history-row old">
|
||||
<span class="t mono" :title="g.time">{{ flowTime(g) }}</span>
|
||||
<span v-if="flowLabel(g)" class="pin" :class="{plan:flowLabel(g)!=='持仓'}">{{ flowLabel(g) }}</span>
|
||||
<button class="nm" :title="g.ts_code+' · 查看信号与研究'" @click.stop="openCodeSignals(g.ts_code)">{{ nm(g.ts_code) }}</button>
|
||||
<button class="nm" :class="{'etf-name':g.flowKind==='etf'}" :title="g.full_name || g.ts_code+' · 查看信号与研究'" @click.stop="openFlowSignal(g)">{{ g.flowKind==='etf' ? g.name : nm(g.ts_code) }}</button>
|
||||
<span class="cat">{{ g.cat }}</span>
|
||||
<span v-if="g.direction" class="direction" :class="dirCls(g.direction)">{{ g.direction }}</span>
|
||||
<span class="sp"></span>
|
||||
<span v-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
<span v-if="g.flowKind==='etf'" class="disp etf-state" :class="g.stale?'wait':g.event_state==='OPEN'?'queue':'strong'">{{ etfStateText(g.display_state) }}</span>
|
||||
<span v-else-if="g.flowKind==='metrics'" class="disp wait">观测</span>
|
||||
<span v-else-if="g.level" class="disp" :class="lvClass(g.level)">{{ lvLabel(g.level) }}</span>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<div v-if="sigTab!=='brief' && etf.enabled" class="srcgroup">
|
||||
<div class="srch" @click="openEtf()">ETF观测 <span class="n">· {{ etfActiveCount }}项</span><span class="sp"></span><span>查看明细 ›</span></div>
|
||||
</div>
|
||||
<!-- 按来源: 原来的「完整信号中心」搬到这里, 就地展开不跳转 -->
|
||||
<template v-else>
|
||||
<template v-if="sigTab!=='brief'">
|
||||
<div class="muted" style="margin-bottom:8px" v-if="upstream.at">
|
||||
最近取数 {{ (upstream.at||'').slice(11,19) }} · 来源是决策系统和盘中择时层,只读参考,不进下单逻辑。</div>
|
||||
|
||||
|
|
@ -2403,6 +2432,32 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}
|
|||
</div>
|
||||
</teleport>
|
||||
|
||||
<teleport to="body"><div v-if="etfOpen">
|
||||
<div class="cs-scrim" @click="etfOpen=false"></div>
|
||||
<aside class="cs-drawer etf-drawer" role="dialog" aria-label="ETF观测详情" aria-modal="true">
|
||||
<div class="cs-h"><span class="ttl">ETF观测</span><span class="sp" style="flex:1"></span><button class="iconbtn" @click="etfOpen=false" title="关闭ETF详情">✕</button></div>
|
||||
<div class="cs-b">
|
||||
<div class="trend-heading"><b>宽基ETF量能</b><span>仅观测 · 阈值待校准</span></div>
|
||||
<p class="etf-note">{{ etfSummary.trade_date || '—' }} · {{ etfTime(etfSummary.window_end) }}窗口 · {{ etfTime(etfSummary.published_at) }}发布<br>{{ etfStatusText }};最后成功读取 {{ etfTime(etf.last_success_at) }}</p>
|
||||
<div class="etf-note" v-if="etf.status==='unavailable'">当前读取失败,以下保留已验证历史。仍有效的缓存按原有效期使用。</div>
|
||||
<div class="etf-note" v-if="etf.sequence_gap">部分事件历史不连续,已保留缺口记录,不能视为完整连续观测。</div>
|
||||
<b>{{ etfOverall }}</b>
|
||||
<div class="etf-stats"><div><small>{{ etfLive?'今日累计':'归档累计' }}</small><b>{{ etfAmount(etfFacts.cumulative_amount) }}</b> 亿元</div><div><small>本窗成交额</small><b>{{ etfAmount(etfFacts.amount) }}</b> 亿元</div><div><small>相当于常日</small><b>{{ etfRatio(etfFacts.cumulative_to_full_day_median20) }}</b></div></div>
|
||||
<p class="etf-note">常日:前20个完整交易日全日中位数 {{ etfAmount(etfFacts.full_day_median20) }} 亿元。不是历史同一时刻比较。</p>
|
||||
<section class="etf-selected" v-if="etfSelected"><b>{{ etfSelected.full_name }}</b><p class="etf-note">{{ etfSelected.subject_id }} · {{ etfSelected.subject_type==='INDEX'?'指数组':etfSelected.subject_type==='MARKET'?'市场汇总':'ETF' }}</p>
|
||||
<dl><dt>观测结论</dt><dd :class="etfDirectionClass(etfSelected)">{{ etfCaption(etfSelected) }} · {{ etfStateText(etfSelected.display_state) }}</dd><dt>观察范围</dt><dd>{{ etfSelected.scope==='SESSION_CUM'?'日内累计量能':'十五分钟量能' }}</dd><dt>首次窗口 / 最近发布</dt><dd>{{ etfTime(etfSelected.opened_at) }} / {{ etfTime(etfSelected.published_at) }}</dd><dt>有效至</dt><dd>{{ etfTime(etfSelected.valid_until) }}</dd><dt>累计 / 本窗成交额</dt><dd>{{ etfAmount((etfSelected.observation||{}).cumulative_amount) }} / {{ etfAmount((etfSelected.observation||{}).amount) }} 亿元</dd></dl>
|
||||
</section>
|
||||
<div class="trend-heading"><b>个别异动</b><span>当前{{ etfActiveCount }}项 · 当日{{ etfRows.length }}项记录</span></div>
|
||||
<div class="etf-events"><div v-for="row in etfRows" :key="row.event_id" class="etf-event" :class="{muted:row.stale}">
|
||||
<span class="pin plan">{{ row.subject_type==='INDEX'?'指数':row.subject_type==='MARKET'?'市场':'ETF' }}</span>
|
||||
<button class="etf-name" @click="etfSelected=row"><b>{{ row.full_name }}</b><small>{{ row.subject_id }} · {{ row.scope==='SESSION_CUM'?'日内累计量能':'十五分钟量能' }}</small></button>
|
||||
<span :class="etfDirectionClass(row)">{{ etfCaption(row) }}</span><span class="disp" :class="row.stale?'wait':'strong'">{{ etfStateText(row.display_state) }}</span>
|
||||
</div></div>
|
||||
<p class="etf-note" v-if="!etfRows.length">本日尚无已接收的有效异动事件。整体量能单独读取。</p>
|
||||
<p class="etf-note">{{ etfSummary.member_count || '—' }}只合格ETF。个别异动不代表市场整体异常。偏多、偏空来自价量关系,不等于净申购、净赎回或买卖指令。到期自动失效,同一事件持续时更新原行。</p>
|
||||
</div>
|
||||
</aside>
|
||||
</div></teleport>
|
||||
<!-- 运维抽屉 -->
|
||||
<!-- ═══ 单票信号抽屉 (2026-09-09): 点一只票看它今天的全部信号 ═══
|
||||
不用 el-drawer 是因为弹层库的层级从 2000 起自增, 信号栏浮层写死 2010、遮罩 2005,
|
||||
|
|
@ -4254,8 +4309,8 @@ createApp({
|
|||
// 信号方向配色 (2026-08-28): A股口径 —— 买入/看涨/流入类一律红, 卖出/看跌/流出类一律绿。
|
||||
// 原来模板里全写 dirUp(x)?'clr-up':'clr-down', 而词表里没有「买入/卖出」: 买入落到
|
||||
// else 分支被涂成绿色, 方向色恰好用反。认不出的词给中性灰, 不硬猜方向。
|
||||
const DIR_UP_SET = new Set(['看涨', '涨', 'inflow', '流入', '买入', '建议买入', 'BUY', '转多']);
|
||||
const DIR_DOWN_SET = new Set(['看跌', '跌', 'outflow', '流出', '卖出', '建议卖出', 'SELL', '减仓', '清仓']);
|
||||
const DIR_UP_SET = new Set(['看涨', '涨', 'inflow', '流入', '买入', '建议买入', 'BUY', '转多', '放量偏多']);
|
||||
const DIR_DOWN_SET = new Set(['看跌', '跌', 'outflow', '流出', '卖出', '建议卖出', 'SELL', '减仓', '清仓', '放量偏空']);
|
||||
const dirCls = d => DIR_UP_SET.has(String(d || '')) ? 'clr-up'
|
||||
: (DIR_DOWN_SET.has(String(d || '')) ? 'clr-down' : 'muted');
|
||||
// 把握值统一显示成百分比: 上游有的发 0.72、有的发 72, 折成同一口径, 不再出现「72(满分 1)」。
|
||||
|
|
@ -4333,7 +4388,7 @@ createApp({
|
|||
const flowSort = rows => rows.sort((a,b)=>flowStamp(b)-flowStamp(a));
|
||||
const flowTime = row => row.ymd && row.ymd!==flowDay()
|
||||
? row.ymd.slice(5)+' '+(hm(row.time)||'—') : (hm(row.time)||'—');
|
||||
const flowLabel = row => (row.held ?? heldCodeSet.value.has(code6(row.ts_code))) ? '持仓' :
|
||||
const flowLabel = row => row.flowKind==='etf' ? (row.subject_type==='INDEX'?'指数':row.subject_type==='MARKET'?'市场':'ETF') : (row.held ?? heldCodeSet.value.has(code6(row.ts_code))) ? '持仓' :
|
||||
(row.watched ?? watchedCodeSet.value.has(code6(row.ts_code))) ? '关注' :
|
||||
(row.plan ?? planCodeSet.value.has(code6(row.ts_code))) ? '计划' : '';
|
||||
// 关注只扩大原信号的归类范围;研究观察、完成回执和等待状态不生成信号行。
|
||||
|
|
@ -4358,8 +4413,19 @@ createApp({
|
|||
for (const r of (s.timing || [])) out.push({
|
||||
...r, flowKind:'timing', ts_code: r.ts_code, time: r.time || '', cat: '买入时机',
|
||||
direction: '买入', level: 'INFO', held: held.has(code6(r.ts_code)) });
|
||||
if(showObservations.value){
|
||||
for(const r of s.metrics||[]){
|
||||
if(!held.has(code6(r.ts_code)) && !watchedCodeSet.value.has(code6(r.ts_code)))continue;
|
||||
if(!['inflow','outflow'].includes(r.direction) || !r.ts)continue;
|
||||
out.push({...r,flowKind:'metrics',cat:'资金异动',direction:flowText(r.direction),level:null,
|
||||
held:held.has(code6(r.ts_code)),sortAt:r.ts});
|
||||
}
|
||||
for(const r of etfRows.value)out.push({...r,flowKind:'etf',ts_code:r.subject_id,time:etfTime(r.published_at),
|
||||
ymd:r.trade_date,today:r.trade_date===flowDay(),sortAt:Date.parse(r.published_at),cat:'ETF观测',
|
||||
direction:etfCaption(r),level:null,held:false,watched:false,plan:false});
|
||||
}
|
||||
const inPlan = planCodeSet.value;
|
||||
for (const g of out) {g.plan = inPlan.has(code6(g.ts_code));g.watched=watchedCodeSet.value.has(code6(g.ts_code));}
|
||||
for (const g of out) {if(g.flowKind==='etf')continue;g.plan = inPlan.has(code6(g.ts_code));g.watched=watchedCodeSet.value.has(code6(g.ts_code));}
|
||||
out.sort((a, b) => (b.held - a.held) || String(b.time).localeCompare(String(a.time)));
|
||||
return out;
|
||||
});
|
||||
|
|
@ -4592,6 +4658,44 @@ createApp({
|
|||
}
|
||||
const alertRowClass = ({ row }) => (row && row.stale) ? 'alert-stale' : '';
|
||||
// —— 宏观择时 (MACRO_TIMING_PLAN.md): 面板取数 / 手动扫描 / 建议采纳 ——
|
||||
// ETF观测只读状态;即使关闭自动刷新,也按真实时间到期。
|
||||
const etf=ref({enabled:false,events:[]}),etfOpen=ref(false),etfSelectedId=ref(null),showObservations=ref(true);
|
||||
const etfClock=ref(Date.now()); let etfOffset=0;
|
||||
const etfTimer=setInterval(()=>{etfClock.value=Date.now()+etfOffset;},1000);
|
||||
onUnmounted(()=>clearInterval(etfTimer));
|
||||
const etfSummary=computed(()=>etf.value.summary||{});
|
||||
const etfFacts=computed(()=>{
|
||||
const s=etfSummary.value,f={...((s.market||{}).observation||{})},sc=s.scopes||{};
|
||||
if(!(sc.WINDOW_15M||{}).quality_eligible)f.amount=null;
|
||||
if(!(sc.SESSION_CUM||sc.EOD||{}).quality_eligible){f.cumulative_amount=null;f.cumulative_to_full_day_median20=null;f.full_day_median20=null;}
|
||||
return f;
|
||||
});
|
||||
const etfLive=computed(()=>Object.values(etfSummary.value.scopes||{}).some(s=>s.observation_eligible) && etfClock.value<Date.parse(etfSummary.value.valid_until));
|
||||
const etfTime=v=>v?new Intl.DateTimeFormat('zh-CN',{timeZone:'Asia/Shanghai',hour:'2-digit',minute:'2-digit',hourCycle:'h23'}).format(new Date(v)):'—';
|
||||
const etfAmount=v=>v==null||!Number.isFinite(Number(v))?'—':(Number(v)/1e8).toFixed(2);
|
||||
const etfRatio=v=>v==null||!Number.isFinite(Number(v))?'—':Number(v).toFixed(2)+'倍';
|
||||
const etfStateText=s=>({OPEN:'新发',CONTINUE:'持续',RESOLVED:'解除',REVERSED:'反转',EXPIRED:'过期',CLOSED:'收盘',RETRACTED:'撤回'}[s]||'未知');
|
||||
const etfCaption=r=>r.direction_status==='UNAVAILABLE'||r.direction==='UNKNOWN'?'方向未知':r.direction==='BUY'?'放量偏多':r.direction==='SELL'?'放量偏空':'量能放大';
|
||||
const etfDirectionClass=r=>r.direction==='BUY'?'clr-up':r.direction==='SELL'?'clr-down':'muted';
|
||||
const etfRows=computed(()=>(etf.value.events||[]).map(r=>{
|
||||
const expired=['OPEN','CONTINUE'].includes(r.event_state) && etfClock.value>=Date.parse(r.valid_until);
|
||||
return {...r,stale:r.stale||expired,display_state:expired?'EXPIRED':r.display_state};
|
||||
}));
|
||||
const etfSelected=computed({get:()=>etfRows.value.find(r=>r.event_id===etfSelectedId.value)||null,set:r=>{etfSelectedId.value=r?r.event_id:null;}});
|
||||
const etfActiveCount=computed(()=>etfRows.value.filter(r=>!r.stale).length);
|
||||
const observationCount=computed(()=>etfRows.value.length+(SIG.value.metrics||[]).filter(r=>heldCodeSet.value.has(code6(r.ts_code))||watchedCodeSet.value.has(code6(r.ts_code))).length);
|
||||
const etfStatusText=computed(()=>etf.value.status==='unavailable'?'读取失败':etfSummary.value.session_closed?'已收盘':etfLive.value?'有效至'+etfTime(etfSummary.value.valid_until):etfSummary.value.valid_until?'已过期':'等待数据');
|
||||
const etfChipText=computed(()=>etf.value.status==='unavailable'?'读取失败':etfLive.value?'累计'+etfRatio(etfFacts.value.cumulative_to_full_day_median20):etfStatusText.value);
|
||||
const etfOverall=computed(()=>!etfSummary.value.market?'暂无整体读数':!etfLive.value?'历史观测,仅供回看':
|
||||
['WINDOW_15M','SESSION_CUM'].some(k=>((etfSummary.value.scopes||{})[k]||{}).model_scoring_available && (etfSummary.value.market[k]||{}).anomaly===true)?'整体量能出现异常':
|
||||
['WINDOW_15M','SESSION_CUM'].every(k=>((etfSummary.value.scopes||{})[k]||{}).model_scoring_available)?'整体未触发异常':'整体异常判断暂缺');
|
||||
const etfBars=computed(()=>{
|
||||
const history=(etf.value.history||[]).slice(-4), vals=history.map(h=>((h.scopes||{}).WINDOW_15M||{}).quality_eligible?((h.market||{}).observation||{}).amount:null),max=Math.max(1,...vals.filter(v=>v!=null));
|
||||
return history.map((h,i)=>({key:h.round_id,time:etfTime(h.window_end),value:etfAmount(vals[i]),missing:vals[i]==null,height:vals[i]==null?1:Math.max(1,vals[i]/max*65),title:etfTime(h.window_end)+' · '+(vals[i]==null?'数据暂缺':etfAmount(vals[i])+'亿元')}));
|
||||
});
|
||||
function openEtf(row){etfSelected.value=row||null;etfOpen.value=true;}
|
||||
function openFlowSignal(row){if(row.flowKind==='etf')openEtf(row);else openCodeSignals(row.ts_code);}
|
||||
async function loadEtf(){const d=await call('get','/api/etf/observation');if(d && d.status){etf.value=d;if(d.server_at)etfOffset=Date.parse(d.server_at)-Date.now();etfClock.value=Date.now()+etfOffset;}else etf.value={...etf.value,status:'unavailable'};}
|
||||
const macro = ref({});
|
||||
const macroGateOn = computed(() => !!(((macro.value || {}).gate || {}).active));
|
||||
const macroUnavailable = computed(() => {
|
||||
|
|
@ -4772,7 +4876,7 @@ createApp({
|
|||
// (通道告警对交易员改由 dispatch-mode 里的 channel 兜底, 见 wsc 的注释)
|
||||
(isAdmin.value ? loadWs() : Promise.resolve()),
|
||||
loadStrategies(), loadOpLog(), loadOpenScan(), loadDividend(), loadUpstream(),
|
||||
loadMacro(), loadMicro(), loadTech()]);
|
||||
loadMacro(), loadEtf(), loadMicro(), loadTech()]);
|
||||
await loadNames();
|
||||
lastRefresh.value = new Date().toTimeString().slice(0, 8);
|
||||
loading.value = false;
|
||||
|
|
@ -5147,7 +5251,7 @@ createApp({
|
|||
async function refreshLive() {
|
||||
// allSettled: 单个 loader 失败/超时(axios 60s 兜底)不拖累其余与时间戳更新。
|
||||
await Promise.allSettled([loadOverview(), loadPositions(), loadInstructions(), loadLedger(),
|
||||
loadProposals(), loadStrategies(), loadOpLog(), loadOpenScan(), loadDividend(), loadUpstream(), loadMacro(), loadMicro(), loadPlanUsers()]);
|
||||
loadProposals(), loadStrategies(), loadOpLog(), loadOpenScan(), loadDividend(), loadUpstream(), loadMacro(), loadEtf(), loadMicro(), loadPlanUsers()]);
|
||||
try { await loadNames(); } catch (e) {}
|
||||
// 用客户端钟, 保证"自动·HH:MM:SS"稳定跳动, 不依赖 /health 的 now。
|
||||
lastRefresh.value = new Date().toTimeString().slice(0, 8);
|
||||
|
|
@ -5306,7 +5410,7 @@ createApp({
|
|||
// 默认只看今天 (2026-08-28): 风控卖出流不按日期分键, 清晨没有新信号时最近 N 条全是
|
||||
// 昨天以前的, 页面又只显示 HH:MM —— 昨天 14:49 的旧条目会挂在今天 13:20 的页面上冒充
|
||||
// 新消息。今天的照常按持仓分两段; 更早的收进底部折叠区, 带日期、置灰 —— 不丢信息也不吵人。
|
||||
const sigToday = computed(() => sigBrief.value.filter(x => x.today !== false));
|
||||
const sigToday = computed(() => sigBrief.value.filter(x => x.today !== false && passLevel(x.level)));
|
||||
const sigDisplayToday = computed(() => flowSort([...sigToday.value]));
|
||||
const sigDecisionRows = computed(() => flowSort(SIG.value.decision.slice(0,16).map(r=>({
|
||||
...r,cat:'决策系统',direction:r.action==='SELL'?'卖出':'买入'}))));
|
||||
|
|
@ -5400,7 +5504,7 @@ createApp({
|
|||
openRailSide(side);
|
||||
}
|
||||
function scrimTE(e) { e.preventDefault(); closeRail(); }
|
||||
function _onEsc(e) { if (e.key === 'Escape') closeRail(); }
|
||||
function _onEsc(e) { if (e.key === 'Escape') {if(etfOpen.value)etfOpen.value=false;else closeRail();} }
|
||||
onMounted(() => {
|
||||
_railClasses(); applyDock();
|
||||
window.addEventListener('resize', applyDock);
|
||||
|
|
@ -5466,6 +5570,9 @@ createApp({
|
|||
csDetail, csToggle, csVerdict, csFundRow, csTechRow, csTimingRow, csPlainReason, csYmd,
|
||||
openCodeSignals, closeCodeSignals, csLoadFull,
|
||||
techFeed, techFeedErr, techStanceCls,
|
||||
etf, etfOpen, etfSelected, showObservations, etfSummary, etfFacts, etfLive, etfTime, etfAmount, etfRatio,
|
||||
etfStateText, etfCaption, etfDirectionClass, etfRows, etfActiveCount, observationCount, etfStatusText, etfChipText,
|
||||
etfOverall, etfBars, openEtf, openFlowSignal, loadEtf,
|
||||
macro, macroGateOn, macroUnavailable, mzLabel, mzColor, mzBarStyle,
|
||||
loadMacro, macroScan, macroAdopt,
|
||||
macroChart, microCharts, microHistoryError, microParameters, microThreshold, microDataReady, microSnapshot, microSignal, microState, microError, microReady, microPhaseText, microModeText, microNumber, microPercent, microChipClass, microChipText,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||
|
||||
# ETF观测专用连接,不进入参数页面。只注入独立消费服务。
|
||||
PMS_ETF_SNAPSHOT_REDIS_URL: str = ""
|
||||
PMS_ETF_EVENT_REDIS_URL: str = ""
|
||||
|
||||
# 当日限制价缓存与独立采集。业务开关和预算统一从ParamStore读取。
|
||||
PMS_LIMIT_CACHE_ENABLED: bool = False
|
||||
PMS_LIMIT_COLLECT_ENABLED: bool = False
|
||||
|
|
|
|||
|
|
@ -102,3 +102,21 @@ services:
|
|||
command: python -m app.ws.runner
|
||||
stop_grace_period: 25s
|
||||
stop_signal: SIGTERM
|
||||
|
||||
pms-observation:
|
||||
<<: *pms-base
|
||||
container_name: ${PMS_STACK_PREFIX:-}pms-observation
|
||||
profiles: ["observation"]
|
||||
env_file:
|
||||
- .env
|
||||
- path: .env.observation
|
||||
required: false
|
||||
command: python -m app.observation_worker
|
||||
volumes: []
|
||||
cpus: 0.5
|
||||
mem_limit: 256m
|
||||
read_only: true
|
||||
tmpfs: ['/tmp:size=32m,mode=1777']
|
||||
cap_drop: [ALL]
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
stop_grace_period: 60s
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
| 原策略两次清仓信号与买方评级下降退出 | [退出纪律接续](两次清仓信号与评级下降退出_2026-11-05.md),含第一次停买、第二次清仓、任一级评级下降、持仓轮次、重复消息和T+1收尾 |
|
||||
| 人工关注显示已剔除、每日选股重复与研究覆盖 | [人工关注分析链修复规格](人工关注分析链修复规格_2026-11-04.md),说明独立研究、回执与交易边界;[只读巡检及当日更正](人工关注与选股覆盖巡检_2026-11-04.md)保留历史时点证据;首次部署见[发布接续](人工关注与吸筹参考发布接续_2026-11-04.md),11月4日进度见[后续跟进](人工关注后续跟进与信号融合_2026-11-04.md),最新格式要求及计划过期恢复见[11月5日接续](信号流格式与选股恢复_2026-11-05.md) |
|
||||
| 架构与接口 | [README模块地图](../README.md)、[总体设计](../POSITION_MGMT_DESIGN.md);改接口再读[QMT协议](../QMT_WS_PROTOCOL.md)、[bionic接口](../BIONIC_PMS_INTERFACE.md)或[计划接口](../UPSTREAM_PLAN_API.md) |
|
||||
| 自选股零信号巡检与ETF观测页面设计 | [11月5日巡检与交互原型](自选股信号巡检与ETF观测原型_2026-11-05.md),区分研究回执、常规信号覆盖、资金观测展示及消费;仅设计提案,未上线 |
|
||||
| 自选股零信号巡检与ETF观测页面设计 | [11月5日巡检与交互原型](自选股信号巡检与ETF观测原型_2026-11-05.md),区分研究回执、常规信号覆盖、资金观测展示及消费;确认后实施及实际发布状态见[补显与ETF实施](自选股补显与ETF观测实施_2026-11-05.md) |
|
||||
| 后端、前端、测试、研究 | [后端规则](../app/AGENTS.md)、[页面规则](../app/web/AGENTS.md)、[脚本规则](../scripts/AGENTS.md)、[研究规则](../research/AGENTS.md),仅选择所涉及目录 |
|
||||
| 部署与运行核验 | [pms-release](../.agents/skills/pms-release/SKILL.md);命令目录为`make help`,并非所有目标都是只读 |
|
||||
| QMT验签、序号、委托与对账问题 | [pms-qmt-diagnose](../.agents/skills/pms-qmt-diagnose/SKILL.md) |
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
# 自选股补显与ETF观测实施
|
||||
|
||||
## 当前进度
|
||||
|
||||
北京时间2026年11月5日15:25,开发完成,尚未发布。用户已确认[原型](自选股信号巡检与ETF观测原型_2026-11-05.md)并要求实施。发布状态将在本文末尾追加,以下隔离结果不冒充真机自然判收。
|
||||
|
||||
## 自选股修复
|
||||
|
||||
持仓与有效关注集合共用现有五类上游流读取。每个流按当天边界最多扫描既有配置上限,补留每只关注或持仓最多60条原消息;其他股票仍保留原全局条数。缓存30秒,关注集合变化立即失效,保留原消息标识、时间、来源和过期判断,扫描截断或关注源失败明确返回状态。读取入口排除北交所。
|
||||
|
||||
资金指标原本没有进入速览,现在仅将持仓及关注股票的真实流入、流出观测并入原行。保留“决策系统”“资金分布”等原来源,新增观测沿用原来源“资金异动”。没有真实信号不占行,不将研究观察、风险研判和每日完成转换成买卖信号。
|
||||
|
||||
常规白名单同时约束交易研判,不能将关注伪装成持仓或扩大交易池。本次修复已有观测的读取和展示,不放开此门槛。bionic配套修复仅对已收到的人工关注回复解析代码围栏或说明文字中的唯一合法JSON,保留四种枚举和非空理由要求;多对象、未知枚举仍不可用,不增加模型调用。
|
||||
|
||||
15:03在155单次只读进程加载候选读取逻辑,命中5只持仓和8只关注。取回中文在线13:21、捷成股份11:18及13:21、果麦文化13:21四条真实观测。四条当时均已过期,不重置时间。该进程阻断写操作,阻断计数为0,没有修改运行服务。
|
||||
|
||||
## ETF接入边界
|
||||
|
||||
独立进程pms-observation读取正式命名空间,使用自己的消费组。环境提供两个独立Redis连接,密钥不进入ParamStore。两个功能开关默认为关闭,155发布后经ParamStore启用。
|
||||
|
||||
逐条校验官方manifest、不可变快照、摘要、身份、时间、序列与修订。原始消息、处理结论及水位在同一事务内保存后才确认;重复回放保留第一次接收时间。坏包留待恢复,其他正常消息与最新快照仍处理。晚到活动事件不重新激活,合法终结可关闭已有事件。独立快照提供无异动时的量能;收盘单独读取15:00封存轮。数据库使用现有轮换文档表中的独立键,无表结构变更。
|
||||
|
||||
官方校验器及依赖逐字复制,来源清单见app/core/etf_contract/PROVENANCE.json。源项目这三个文件当时未纳入Git,因此不以其仓库HEAD冒充文件版本。已将三个文件摘要与155正式ETF镜像逐一核对一致。
|
||||
|
||||
页面增加顶部ETF入口、原风格悬停、右侧明细和原信号流。每个事件持续更新原行,原发布时刻保持。十五分钟和累计尺度分别判断可用性,缺数显示未知。前20日完整交易日中位数不解释为同一时刻历史比较。读取故障保留合法缓存,时间独立推进,到期自行置灰;ETF仅作价量观测,不进入命令、清仓计数、仓位、账本或QMT。
|
||||
|
||||
## 验证
|
||||
|
||||
开发机隔离全量79批通过,ETF专项23项,包含乱序、修订、迟到、主动过期、断线、重复、先保存后确认、坏包不阻塞和来源补显。页面三道护栏由全量入口覆盖。bionic离线16批通过。
|
||||
|
||||
真实只读响应与13:30正式ETF封存回放用于浏览器验收,明确标示非实时且拒绝全部写请求。1440、390、320宽度通过,无信号折行或横向溢出。观测筛选、详情、Escape关闭、有效、过期和读取失败验证通过,最终加载无控制台错误。截图仅是隔离复现。
|
||||
|
||||
## 发布计划及回退
|
||||
|
||||
155仅重建pms-web,新增独立pms-observation;原worker、唯一WS、beat及采集不为追平版本重启。188仅更新bionic worker-brain,先停止其队列新消费并等待在途清空,原目录源码及.env保持。真实PMS不操作。
|
||||
|
||||
发布前记录实际镜像、私有配置和其余容器基线。候选先断网全量回归。ETF独立私有环境文件仅在155保存,来源为既有上游观测服务的同一连接。回退先关闭两个新参数及停止新增观测服务,恢复web或brain旧镜像和挂载;保留已接收观测、队列及所有交易账本。
|
||||
|
||||
实际部署、自然消费与未判收状态待下面追加。
|
||||
|
|
@ -199,7 +199,7 @@ import sys
|
|||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.dirname(HERE)
|
||||
# 退出纪律专项:两次独立清仓信号、评级下降、事务、双策略隔离及撤单收尾。
|
||||
SUITES = ["test_risk_exit.py", "test_rotation_accounting_units.py", "test_cost_precision.py", "test_execution_basis_decimal.py",
|
||||
SUITES = ["test_etf_observation.py", "test_risk_exit.py", "test_rotation_accounting_units.py", "test_cost_precision.py", "test_execution_basis_decimal.py",
|
||||
"test_trade_correction_units.py", "test_trade_correction_transport.py",
|
||||
"test_trade_correction_service.py", "test_trade_lineage_units.py",
|
||||
"test_trade_correction_strategy.py",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,146 @@
|
|||
"""ETF观测契约、持久化/ACK次序和信号补显。全为合成数据及内存替身。"""
|
||||
import unit_test_guard
|
||||
import contextlib
|
||||
import copy
|
||||
from datetime import datetime,timedelta
|
||||
import hashlib
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest import TestCase, main
|
||||
from unittest.mock import patch
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
from app.services import etf_observation as svc, upstream_signals as upstream, param_store
|
||||
from app.core.etf_contract import consumer as contract
|
||||
from app.core.etf_contract.timing import DELAYED_POLICY, lease_until, admission_until
|
||||
from config.settings import settings
|
||||
|
||||
NOW=datetime.fromisoformat('2026-11-05T10:03:00+08:00')
|
||||
DAY=NOW.date().isoformat()
|
||||
|
||||
def fixture(hm='10:00',seq=1,state='OPEN',revision=0):
|
||||
end=datetime.fromisoformat(DAY+'T'+hm+':00+08:00'); published=end+timedelta(minutes=2)
|
||||
observed={'amount_unit':'CNY','flow_basis':'TURNOVER_NOT_NET_FLOW','amount':100000000.,'cumulative_amount':200000000.,'window_amount_status':'OK','cumulative_amount_status':'OK','full_day_median20':100000000.,'cumulative_to_full_day_median20':2.}
|
||||
metrics={'threshold_version':'test-threshold','amount':100000000.,'cumulative_amount':200000000.,'pressure_score':.1}
|
||||
block={'data_status':'OK','anomaly':True,'direction':'MIXED','direction_status':'OK','severity':1,'metrics':metrics}
|
||||
snap={'schema_version':1,'round_id':'20261105T'+hm.replace(':',''),'trade_date':DAY,'revision':revision,'window_end':end.isoformat(),'published_at':published.isoformat(),'mode':'LIVE','publication_mode':'observation','model_status':'uncalibrated','calibration_status':'PROVISIONAL','threshold_version':'test-threshold','model_version':'synthetic','data_status':'OK','session_closed':hm=='15:00','timing_policy_version':DELAYED_POLICY,'observation_contract_version':'etf-observation-1.0','purpose':'MARKET_OBSERVATION','trade_instruction':False,'release_id':'test-release','effective_from':DAY+'T09:45:00+08:00','review_status':'ACCUMULATING','calibration_review':{'sample_count':33,'target_days':60,'eligible_cohort_size':1,'by_subject':{'MARKET:BROAD_ETF':33}},'subjects':[{'subject_type':'MARKET','subject_id':'BROAD_ETF','data_status':'OK','member_count':1,'observation':observed,'WINDOW_15M':block,'SESSION_CUM':copy.deepcopy(block)}]}
|
||||
event={k:copy.deepcopy(snap[k]) for k in ('schema_version','round_id','trade_date','revision','window_end','published_at','model_version','timing_policy_version')+contract.OBSERVATION_EVENT_FIELDS}
|
||||
event.update(event_id='synthetic-event',event_seq=seq,event_state=state,subject_type='MARKET',subject_id='BROAD_ETF',scope='WINDOW_15M',producer_id='mtf_etf',flow_basis='price_volume_proxy',window_start=(end-timedelta(minutes=15)).isoformat(),valid_until=lease_until(end,DELAYED_POLICY).isoformat(),opened_at=DAY+'T10:00:00+08:00',direction='MIXED',direction_status='OK',last_known_direction='MIXED',severity=1,metrics=metrics,reason_codes=[])
|
||||
raw=json.dumps(snap,ensure_ascii=False)
|
||||
mf={k:copy.deepcopy(snap[k]) for k in ('schema_version','round_id','trade_date','revision','window_end','published_at','mode','publication_mode','session_closed','timing_policy_version')+contract.OBSERVATION_FIELDS}
|
||||
suffix=f"{DAY}:{snap['round_id']}:r{revision}"
|
||||
mf.update(status='READY',snapshot_sha256=hashlib.sha256(raw.encode()).hexdigest(),manifest_key=svc.NS+':manifest:'+suffix,snapshot_key=svc.NS+':snapshot:'+suffix,live_eligible=hm!='15:00',valid_until=lease_until(end,DELAYED_POLICY).isoformat(),admission_until=admission_until(end,DELAYED_POLICY).isoformat())
|
||||
return event,mf,raw
|
||||
|
||||
class MemoryStore:
|
||||
def __init__(self):self.docs={};self.fail=False
|
||||
def get(self,k):return copy.deepcopy(self.docs.get(k)),1
|
||||
def put_once(self,k,kind,v):
|
||||
if self.fail:raise RuntimeError('database unavailable')
|
||||
if k in self.docs and self.docs[k]!=v:raise ValueError('immutable')
|
||||
self.docs[k]=copy.deepcopy(v)
|
||||
@contextlib.contextmanager
|
||||
def locked(self,k,kind,initial):
|
||||
if self.fail:raise RuntimeError('database unavailable')
|
||||
before=copy.deepcopy(self.docs);v=copy.deepcopy(self.docs.get(k,initial))
|
||||
try:yield v
|
||||
except BaseException:self.docs=before;raise
|
||||
else:self.docs[k]=v
|
||||
|
||||
class RedisFake:
|
||||
def __init__(self,store=None):self.values={};self.rows=[];self.pending=[];self.acks=[];self.store=store
|
||||
def get(self,key):return self.values.get(key)
|
||||
def strlen(self,key):return len(self.values.get(key,'').encode())
|
||||
def getrange(self,key,a,b):return self.values.get(key,'').encode()[a:b+1].decode()
|
||||
def xgroup_create(self,*a,**kw):pass
|
||||
def scan_iter(self,**kw):return []
|
||||
def xreadgroup(self,group,consumer,streams,count):
|
||||
if next(iter(streams.values()))=='0-0': rows=list(self.pending)
|
||||
else:rows=self.rows[:count];self.rows=self.rows[count:];self.pending+=rows
|
||||
return [(next(iter(streams)),rows)] if rows else []
|
||||
def xack(self,key,group,sid):
|
||||
assert any(k.endswith(':'+sid) for k in self.store.docs),'ACK before durable record'
|
||||
self.acks.append(sid);self.pending=[r for r in self.pending if r[0]!=sid]
|
||||
|
||||
class ETFTests(TestCase):
|
||||
def setUp(self):
|
||||
self.stack=contextlib.ExitStack();self.addCleanup(self.stack.close)
|
||||
self.store=MemoryStore();self.stack.enter_context(patch.object(svc,'store',self.store))
|
||||
self.stack.enter_context(patch.object(param_store,'get_bool',return_value=True))
|
||||
self.snap,self.bus=RedisFake(),RedisFake(self.store)
|
||||
def load(self,hm='10:00',seq=1,state='OPEN',revision=0,sid=None):
|
||||
e,m,r=fixture(hm,seq,state,revision);sid=sid or str(seq)+'-0'
|
||||
self.bus.rows.append((sid,{'data':json.dumps(e)}));self.bus.values[m['manifest_key']]=json.dumps(m);self.snap.values[m['snapshot_key']]=r;self.snap.values[svc.NS+':latest']=json.dumps(m)
|
||||
return e,m,r
|
||||
def runat(self,at=NOW):return svc.consume_once(self.snap,self.bus,clock=lambda:at)
|
||||
def state(self):return self.store.docs[svc.day_key(DAY)]
|
||||
def test_original_valid_envelope_saved_before_ack(self):
|
||||
self.load();self.assertEqual(self.runat()['status'],'ok');self.assertEqual(self.bus.acks,['1-0']);self.assertEqual(len(self.state()['events']),1)
|
||||
def test_duplicate_delivery_and_restart_do_not_duplicate(self):
|
||||
self.load();self.runat();first=copy.deepcopy(self.state()['events']);self.load();self.runat();self.assertEqual(self.state()['events'],first)
|
||||
def test_database_failure_keeps_pending(self):
|
||||
self.load();self.store.fail=True
|
||||
with self.assertRaises(RuntimeError):self.runat()
|
||||
self.assertFalse(self.bus.acks);self.assertTrue(self.bus.pending)
|
||||
self.store.fail=False;self.runat();self.assertEqual(self.bus.acks,['1-0'])
|
||||
def test_current_continue_updates_one_row(self):
|
||||
self.load();self.runat();self.load('10:15',2,'CONTINUE');self.runat(NOW+timedelta(minutes=15));self.assertEqual(len(self.state()['events']),1);self.assertEqual(self.state()['events']['synthetic-event']['event']['event_seq'],2)
|
||||
def test_older_revision_does_not_replace(self):
|
||||
self.load(revision=1);self.runat();self.load(seq=2,revision=0);self.runat();self.assertEqual(self.state()['summary']['revision'],1);self.assertEqual(self.state()['events']['synthetic-event']['event']['revision'],1)
|
||||
def test_late_open_is_archived_not_current(self):
|
||||
self.load();self.runat(NOW+timedelta(minutes=31));self.assertFalse(self.state()['events']);self.assertEqual(len(self.bus.acks),1)
|
||||
def test_late_terminal_closes_previously_open(self):
|
||||
self.load();self.runat();self.load('10:15',2,'RESOLVED');self.runat(NOW+timedelta(hours=1));self.assertEqual(self.state()['events']['synthetic-event']['event']['event_state'],'RESOLVED')
|
||||
def test_sequence_gap_is_exposed(self):
|
||||
self.load(seq=3,state='CONTINUE');self.runat();self.assertTrue(self.state()['sequence_gap'])
|
||||
def test_corrupt_hash_not_acknowledged(self):
|
||||
e,m,r=self.load();self.snap.values[m['snapshot_key']]=r+' ';self.assertEqual(self.runat()['status'],'unavailable');self.assertFalse(self.bus.acks)
|
||||
def test_bad_pending_does_not_block_later_event_or_latest_snapshot(self):
|
||||
e,m,r=self.load();del self.snap.values[m['snapshot_key']]
|
||||
self.load('10:15',2,'CONTINUE')
|
||||
self.assertEqual(self.runat(NOW+timedelta(minutes=15))['status'],'unavailable')
|
||||
self.assertEqual(self.bus.acks,['2-0']);self.assertEqual(self.state()['summary']['round_id'],'20261105T1015')
|
||||
def test_other_subject_eligible_does_not_make_market_live(self):
|
||||
self.load();self.runat();state=copy.deepcopy(self.state())
|
||||
for permission in state['summary']['scopes'].values():permission['observation_eligible']=False
|
||||
self.assertFalse(svc.presentation(state,NOW)['current_eligible'])
|
||||
def test_source_recovery_reprocesses_pending(self):
|
||||
e,m,r=self.load();del self.snap.values[m['snapshot_key']];self.runat();self.snap.values[m['snapshot_key']]=r;self.runat();self.assertEqual(self.bus.acks,['1-0'])
|
||||
def test_no_events_still_publishes_summary(self):
|
||||
self.load();self.bus.rows=[];self.runat();self.assertEqual(self.state()['summary']['market']['observation']['amount'],100000000.)
|
||||
def test_read_clock_after_fetch_prevents_expired_activation(self):
|
||||
self.load();ticks=iter([NOW,NOW+timedelta(hours=1),NOW+timedelta(hours=1),NOW+timedelta(hours=1),NOW+timedelta(hours=1)])
|
||||
svc.consume_once(self.snap,self.bus,clock=lambda:next(ticks));self.assertFalse(self.state()['events'])
|
||||
def test_expiry_is_recomputed_without_new_messages(self):
|
||||
self.load();self.runat();view=svc.presentation(self.state(),NOW+timedelta(minutes=28));self.assertTrue(view['events'][0]['stale']);self.assertFalse(view['current_eligible'])
|
||||
def test_read_failure_preserves_valid_cached_facts(self):
|
||||
self.load();self.runat();state=self.state();state['error']='ConnectionError';view=svc.presentation(state,NOW);self.assertEqual(view['status'],'unavailable');self.assertTrue(view['current_eligible']);self.assertFalse(view['trade_instruction'])
|
||||
def test_secrets_never_enter_parameter_registry(self):
|
||||
for key in ('PMS_ETF_SNAPSHOT_REDIS_URL','PMS_ETF_EVENT_REDIS_URL'):
|
||||
self.assertIn(key,param_store.SECRET_KEYS);self.assertNotIn(key,param_store._editable_keys())
|
||||
def test_disallowed_subject_and_unknown_direction(self):
|
||||
self.assertFalse(svc.subject_allowed('ETF','920001.BJ'));self.assertFalse(svc.subject_allowed('INDEX','bad'))
|
||||
def test_vendor_bytes_match_pinned_source(self):
|
||||
path=Path(contract.__file__).parent;manifest=json.loads((path/'PROVENANCE.json').read_text())
|
||||
for name,digest in manifest['sha256'].items():self.assertEqual(hashlib.sha256((path/name).read_bytes()).hexdigest(),digest)
|
||||
|
||||
class FocusTests(TestCase):
|
||||
def setUp(self):
|
||||
upstream._focused_streams.clear()
|
||||
p=patch.object(param_store,'get_int',side_effect=lambda key,default=0:default);p.start();self.addCleanup(p.stop)
|
||||
def test_old_watched_and_held_rows_survive_global_limit_and_are_cached(self):
|
||||
rows=[(str(1000-i)+'-0',{'ts_code':'600009.SH'})for i in range(70)]+[('800-0',{'ts_code':'300001.SZ'}),('799-0',{'ts_code':'600001.SH'})]
|
||||
with patch.object(upstream,'_focus_codes',return_value={'300001.SZ','600001.SH'}),patch.object(upstream,'_scan_stream',return_value=(rows,False)) as scan:
|
||||
got=upstream._entries(None,'test',40);self.assertEqual(len(got),42);self.assertEqual(upstream._entries(None,'test',40),got);self.assertEqual(scan.call_count,1)
|
||||
def test_metrics_use_original_stream_time_not_refresh_time(self):
|
||||
ms=int(NOW.timestamp()*1000)
|
||||
with patch.object(upstream,'_entries',return_value=[(str(ms)+'-0',{'ts_code':'SZ300001','data':json.dumps({'direction':'inflow','z_dd':6})})]):
|
||||
row=upstream._read_metrics(None)['items'][0];self.assertEqual(row['ts'],ms);self.assertEqual(row['id'],str(ms)+'-0');self.assertEqual(row['ts_from'],'stream_id')
|
||||
def test_empty_source_creates_no_placeholder(self):
|
||||
with patch.object(upstream,'_entries',return_value=[]):self.assertEqual(upstream._read_metrics(None)['items'],[])
|
||||
def test_focus_removed_no_longer_reused(self):
|
||||
rows=[('1-0',{'ts_code':'300001.SZ'})]
|
||||
with patch.object(upstream,'_focus_codes',return_value={'300001.SZ'}),patch.object(upstream,'_scan_stream',return_value=(rows,False)):self.assertEqual(len(upstream._entries(None,'test',0)),1)
|
||||
with patch.object(upstream,'_focus_codes',return_value={'600001.SH'}),patch.object(upstream,'_scan_stream',return_value=(rows,False)):self.assertEqual(upstream._entries(None,'test',0),[])
|
||||
|
||||
if __name__=='__main__':main()
|
||||
Loading…
Reference in New Issue