mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 21:24:16 +08:00
* feat(screener): 选股引擎支持 ETF - 12 个内置策略打 asset_types 白名单 + strategy_supports_asset;涨停类 (连板/断板反包)仅股票,其余 10 个技术类对 ETF 开放 - ScreenerService(repo, asset_type) 分流取数,ETF 复用 kline_etf_enriched, 跳过股票专用历史缓存与涨停信号;进程级 _history_cache key 含 asset_type - API /run、/run_preset 透传 asset_type;/strategies 按资产过滤; 股票专有策略在 ETF 下返回空 - 新增 enriched_dirname(asset_type) 共享 helper;get_enriched_latest_asset 增 refresh 参数(供轮询线程避免冷缓存同步重算) - 前端「策略」页加 股票/ETF 切换,ETF 走实时单跑(空日期→用 ETF 自身最新日); QK.screenerStrategies 按 asset_type keyed - 测试:test_screener_etf.py Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(backtest): 回测支持 ETF(个股/因子/策略组合) - 三条回测路径 + 共用 BacktestEngine 面板加载按 asset_type 路由到 kline_etf_enriched(复用 enriched_dirname);PanelCache key 隔离资产; ETF 跳过股票专用 get_enriched_range 缓存 - 面板 compute_all/名称 JOIN 按 asset_type 取维表(get_instruments_asset), 修复 ETF 策略回测用错股票维表致名称为空/涨停信号算错 - BacktestConfig/FactorConfig/StrategyBacktestConfig 增 asset_type - 三个回测 API + SSE stream 透传 asset_type;_make_job_key 纳入 asset_type (修复 stream 与 cancel job_key 不对齐致取消失效的回归) - 前端策略组合页/因子页加 股票/ETF 切换,标的搜索与策略列表跟随资产; assetType 持久化 - 测试:test_backtest_etf.py(含 job_key 一致性回归);既有回测测试替身同步 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(monitor): 监控规则支持 ETF - engine.evaluate(df, asset_type) 按规则 asset_type 分轮评估;quote_service 增开 ETF 评估轮(用 ETF enriched 快照),股票轮不受影响、不重置其策略结果 - ETF 评估轮独立 try(异常不丢弃已算出的股票告警)+ refresh=False(不在轮询 线程触发 ETF 冷缓存同步重算) - ETF 版历史加载器(main.py 注入)+ 按规则 asset_type 选加载器 - _strategy_pools 按 (sid, asset_type) 键,避免同策略股票/ETF 规则互相覆盖 - name_map 仅在有 ETF 规则时补 ETF 维表, setdefault 保股票名优先 - RuleModel/normalize 增 asset_type(默认 stock,持久化往返) - 前端 RuleEditor 加 股票/ETF 选择,策略列表与标的搜索跟随资产 - 测试:test_monitor_etf.py Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(etf): 前端 API 绑定透传 asset_type + 文档 - api.ts: screener/backtest 绑定加 assetType 参数,MonitorRule 类型加 asset_type - docs/features.md: 标注选股/回测/监控的 ETF 支持范围与前提 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(reliability): 管道并发/原子写/能力探测/监控告警多处加固 后端可靠性专项修复(均带回归测试, backend 全套 64 passed): 并发与数据完整性: - 盘后管道单飞: JobStore.create() 去重纳入 pending∨running, 关闭"两次快速点击" 并发双跑窗口; 新增 _heavy_run_lock 执行槽挡住 reap 后僵尸线程并发写 parquet - adj_factor/minute 全部改走原子写(tmp+replace), 消除 kill/断电致 all.parquet 损坏 - 分块拉取失败聚合 WARNING 可见化(不再静默当成功); 复权失败标的会保持旧价已提示 能力探测: - 周期重探(60min)热更新 app.state.capabilities, 付费 Key 过期/续费无需重启即可见 - 瞬时探测失败(超时/连接/5xx, 按 _is_transient 判定)不降级、保留旧付费档; 真 401/无权限仍正常降级回落 free-api 监控告警: - 评估仅在连续竞价(9:30-11:30/13:00-15:00)+ 快照当日新鲜度下进行, 避开集合竞价/ 收盘后陈旧价与节假日误告警 - scope=sector fail-closed(validate 拒绝新建 + _apply_scope 返回空), 修复板块规则 对全市场刷屏 - 飞书 webhook 加退避重试并移到独立线程池 fire-and-forget, 不再阻塞行情轮询线程 单标的新鲜度: 新增 repo.symbols_lagging() 检测掉队标的并 WARNING + 计入 job 结果 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
265 lines
11 KiB
Python
265 lines
11 KiB
Python
"""监控规则 — 统一的 MonitorRule 模型,覆盖策略/个股信号/个股价格/市场异动四类。
|
|
|
|
职责:
|
|
- 从 data/user_data/monitor_rules/*.json 加载规则定义
|
|
- 校验规则字段合法性
|
|
- 提供 CRUD (load_all / save_one / delete_one)
|
|
|
|
不知道: 行情评估引擎、API、告警落盘。纯函数 + 文件存储。
|
|
|
|
设计 (镜像 custom_signals.py 的写法):
|
|
- 一对象一文件 + glob 全扫 + 全量重写
|
|
- 字段白名单复用 custom_signals.ALLOWED_FIELDS (阈值条件) + 信号列清单 (布尔条件)
|
|
- id 正则与 custom_signals 一致,保证可纳入同一索引体系
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import re
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
from app.strategy.custom_signals import ALLOWED_FIELDS
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# ── 常量 ────────────────────────────────────────────────
|
|
ID_RE = re.compile(r"^[a-z0-9_]{1,40}$")
|
|
RULE_TYPES = {"strategy", "signal", "price", "market", "ladder"}
|
|
SCOPES = {"symbols", "all", "sector"}
|
|
LOGICS = {"and", "or"}
|
|
DIRECTIONS = {"entry", "exit", "both"}
|
|
SEVERITIES = {"info", "warn", "critical"}
|
|
OPS = {">", ">=", "<", "<=", "==", "!="}
|
|
# ladder 规则: 封单监控的指标 (量=手, 额=元)
|
|
LADDER_METRICS = {"sealed_vol", "sealed_amount"}
|
|
# ladder 规则: 方向 (up=涨停炸板预警, down=跌停翘板预警)
|
|
LADDER_DIRECTIONS = {"up", "down"}
|
|
|
|
# 布尔信号列前缀 (op=truth 时 field 取这些)
|
|
_SIGNAL_PREFIXES = ("signal_", "csg_")
|
|
|
|
|
|
# ── 持久化 (镜像 custom_signals.py) ─────────────────────
|
|
def _dir(data_dir: Path) -> Path:
|
|
d = data_dir / "user_data" / "monitor_rules"
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
return d
|
|
|
|
|
|
def _path(data_dir: Path, rule_id: str) -> Path:
|
|
return _dir(data_dir) / f"{rule_id}.json"
|
|
|
|
|
|
def load_all(data_dir: Path) -> list[dict]:
|
|
"""读取全部监控规则。损坏的文件被跳过。"""
|
|
d = _dir(data_dir)
|
|
out: list[dict] = []
|
|
for f in sorted(d.glob("*.json")):
|
|
try:
|
|
out.append(json.loads(f.read_text(encoding="utf-8")))
|
|
except Exception as e:
|
|
logger.warning("monitor rule load failed %s: %s", f.name, e)
|
|
return out
|
|
|
|
|
|
def load_one(data_dir: Path, rule_id: str) -> dict | None:
|
|
p = _path(data_dir, rule_id)
|
|
if not p.exists():
|
|
return None
|
|
try:
|
|
return json.loads(p.read_text(encoding="utf-8"))
|
|
except Exception as e:
|
|
logger.warning("monitor rule load failed %s: %s", rule_id, e)
|
|
return None
|
|
|
|
|
|
def save_one(data_dir: Path, rule: dict) -> None:
|
|
p = _path(data_dir, rule["id"])
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
p.write_text(json.dumps(rule, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
|
|
def delete_one(data_dir: Path, rule_id: str) -> bool:
|
|
p = _path(data_dir, rule_id)
|
|
if p.exists():
|
|
p.unlink()
|
|
return True
|
|
return False
|
|
|
|
|
|
# ── 校验 ────────────────────────────────────────────────
|
|
def _is_signal_field(field: str) -> bool:
|
|
"""判断 field 是否为布尔信号列 (signal_ / csg_ 前缀)。"""
|
|
return any(field.startswith(p) for p in _SIGNAL_PREFIXES)
|
|
|
|
|
|
def validate(rule: dict) -> None:
|
|
"""校验一条监控规则,非法则抛 ValueError (含中文信息)。"""
|
|
rid = rule.get("id", "")
|
|
if not isinstance(rid, str) or not ID_RE.match(rid):
|
|
raise ValueError(f"规则 id 非法 (仅小写字母数字下划线, 1-40字符): {rid!r}")
|
|
if not isinstance(rule.get("name"), str) or not rule["name"].strip():
|
|
raise ValueError("规则 name 不能为空")
|
|
if rule.get("type") not in RULE_TYPES:
|
|
raise ValueError(f"type 必须是 {RULE_TYPES} 之一")
|
|
|
|
# 策略类型: 需要 strategy_id + direction,conditions 可空
|
|
if rule.get("type") == "strategy":
|
|
if not rule.get("strategy_id"):
|
|
raise ValueError("策略类型规则必须指定 strategy_id")
|
|
if rule.get("direction", "entry") not in DIRECTIONS:
|
|
raise ValueError(f"direction 必须是 {DIRECTIONS} 之一")
|
|
elif rule.get("type") == "ladder":
|
|
# 连板梯队封单监控: 需 metric + threshold + direction(up/down), 不用 conditions
|
|
if rule.get("metric", "sealed_vol") not in LADDER_METRICS:
|
|
raise ValueError(f"metric 必须是 {LADDER_METRICS} 之一")
|
|
if rule.get("direction", "up") not in LADDER_DIRECTIONS:
|
|
raise ValueError(f"direction 必须是 {LADDER_DIRECTIONS} 之一 (up=涨停炸板, down=跌停翘板)")
|
|
thr = rule.get("threshold")
|
|
if not isinstance(thr, (int, float)) or thr < 0:
|
|
raise ValueError("threshold 必须是非负数字 (封单 ≤ 此值时报警)")
|
|
else:
|
|
# 信号/价格/市场类型: 需要 conditions
|
|
conds = rule.get("conditions")
|
|
if not isinstance(conds, list) or len(conds) == 0:
|
|
raise ValueError("conditions 不能为空")
|
|
if len(conds) > 8:
|
|
raise ValueError("conditions 最多 8 条")
|
|
if rule.get("logic", "and") not in LOGICS:
|
|
raise ValueError(f"logic 必须是 {LOGICS} 之一")
|
|
for i, c in enumerate(conds):
|
|
if not isinstance(c, dict):
|
|
raise ValueError(f"第 {i+1} 个条件格式错误")
|
|
field = c.get("field", "")
|
|
op = c.get("op", "")
|
|
if op == "truth":
|
|
# 布尔信号: field 必须是 signal_/csg_ 前缀
|
|
if not _is_signal_field(field):
|
|
raise ValueError(f"第 {i+1} 个条件: op=truth 时 field 必须是信号列 (signal_/csg_ 前缀): {field!r}")
|
|
elif op in OPS:
|
|
# 阈值比较: field 必须在白名单, 需要 value
|
|
if field not in ALLOWED_FIELDS:
|
|
raise ValueError(f"第 {i+1} 个条件: 阈值字段 {field!r} 不在白名单")
|
|
if not isinstance(c.get("value"), (int, float)):
|
|
raise ValueError(f"第 {i+1} 个条件: value 必须是数字")
|
|
else:
|
|
raise ValueError(f"第 {i+1} 个条件: op {op!r} 非法 (应为 truth 或 {OPS})")
|
|
|
|
# scope 校验
|
|
if rule.get("scope", "symbols") not in SCOPES:
|
|
raise ValueError(f"scope 必须是 {SCOPES} 之一")
|
|
if rule.get("scope") == "symbols":
|
|
syms = rule.get("symbols")
|
|
if not isinstance(syms, list) or len(syms) == 0:
|
|
raise ValueError("scope=symbols 时 symbols 不能为空")
|
|
# sector 作用域的板块 JOIN 尚未实现: _apply_scope 目前会退化为「全市场」,
|
|
# 一条本意针对某板块的规则会对全市场每只命中都触发(告警风暴)。在板块 JOIN
|
|
# 落地前, 拒绝创建 sector 规则(fail-closed), 避免用户建出会刷屏的规则。
|
|
if rule.get("scope") == "sector":
|
|
raise ValueError("scope=sector 暂未支持(板块 JOIN 未实现),请改用 scope=symbols 指定标的或 scope=all")
|
|
|
|
# 其余枚举
|
|
if rule.get("severity", "info") not in SEVERITIES:
|
|
raise ValueError(f"severity 必须是 {SEVERITIES} 之一")
|
|
cd = rule.get("cooldown_seconds", 3600)
|
|
if not isinstance(cd, int) or cd < 0:
|
|
raise ValueError("cooldown_seconds 必须是非负整数")
|
|
|
|
|
|
def normalize(rule: dict) -> dict:
|
|
"""补全默认字段,返回规范化后的规则 (不校验)。"""
|
|
r = dict(rule)
|
|
r.setdefault("enabled", True)
|
|
r.setdefault("asset_type", "stock")
|
|
r.setdefault("scope", "symbols")
|
|
r.setdefault("symbols", [])
|
|
r.setdefault("sector", None)
|
|
r.setdefault("strategy_id", None)
|
|
# direction 默认值: ladder 用 "up", 其余用 "entry"
|
|
r.setdefault("direction", "up" if r.get("type") == "ladder" else "entry")
|
|
r.setdefault("conditions", [])
|
|
# ladder 专属默认字段
|
|
r.setdefault("metric", "sealed_vol")
|
|
r.setdefault("threshold", 0)
|
|
r.setdefault("logic", "and")
|
|
r.setdefault("cooldown_seconds", 3600)
|
|
r.setdefault("severity", "info")
|
|
r.setdefault("message", "")
|
|
r.setdefault("webhook_url", "")
|
|
r.setdefault("webhook_enabled", False)
|
|
r.setdefault("created_at", datetime.now(timezone.utc).isoformat())
|
|
return r
|
|
|
|
|
|
# 策略监控自动迁移的规则 id 前缀 (固定, 保证幂等)
|
|
STRATEGY_RULE_PREFIX = "mr_strategy_"
|
|
|
|
|
|
def strategy_rule_id(strategy_id: str) -> str:
|
|
"""策略监控规则 id = mr_strategy_{strategy_id}。"""
|
|
return f"{STRATEGY_RULE_PREFIX}{strategy_id}"
|
|
|
|
|
|
def migrate_strategy_monitors(data_dir: Path, strategy_ids: list[str], strategy_names: dict[str, str]) -> list[dict]:
|
|
"""把 preferences.strategy_monitor_ids 里的策略,同步生成/更新 type=strategy 规则。
|
|
|
|
幂等: 已存在的策略规则会被更新 (方向/名称),不会重复创建。
|
|
已从 strategy_ids 移除的策略, 其规则会被停用 (enabled=False) 而非删除 (保留历史触发记录的关联)。
|
|
|
|
Args:
|
|
data_dir: 数据目录
|
|
strategy_ids: 当前监控池中的策略 id 列表
|
|
strategy_names: {strategy_id: 策略名} 用于规则显示名
|
|
Returns:
|
|
本次生成/更新的规则列表
|
|
"""
|
|
desired = set(strategy_ids)
|
|
existing = load_all(data_dir)
|
|
# 已存在的策略规则 {strategy_id: rule}
|
|
existing_strategy_rules: dict[str, dict] = {}
|
|
for r in existing:
|
|
rid = r.get("id", "")
|
|
if rid.startswith(STRATEGY_RULE_PREFIX):
|
|
sid = rid[len(STRATEGY_RULE_PREFIX):]
|
|
if sid:
|
|
existing_strategy_rules[sid] = r
|
|
|
|
touched: list[dict] = []
|
|
# 1. 为当前监控池的策略 upsert 规则
|
|
for sid in desired:
|
|
rule_id = strategy_rule_id(sid)
|
|
name = strategy_names.get(sid, sid)
|
|
rule = existing_strategy_rules.get(sid)
|
|
if rule is None:
|
|
rule = normalize({
|
|
"id": rule_id,
|
|
"name": f"策略监控 · {name}",
|
|
"type": "strategy",
|
|
"scope": "all",
|
|
"strategy_id": sid,
|
|
"direction": "entry",
|
|
"conditions": [],
|
|
"cooldown_seconds": 3600,
|
|
"enabled": True,
|
|
})
|
|
else:
|
|
rule = dict(rule)
|
|
rule["enabled"] = True
|
|
rule["strategy_id"] = sid
|
|
rule["name"] = f"策略监控 · {name}"
|
|
rule.setdefault("scope", "all")
|
|
rule.setdefault("direction", "entry")
|
|
save_one(data_dir, rule)
|
|
touched.append(rule)
|
|
|
|
# 2. 不在监控池的策略 → 停用其规则 (不删除)
|
|
for sid, rule in existing_strategy_rules.items():
|
|
if sid not in desired and rule.get("enabled") is not False:
|
|
rule = dict(rule)
|
|
rule["enabled"] = False
|
|
save_one(data_dir, rule)
|
|
|
|
return touched
|