Spaces:
Paused
Paused
zt p commited on
Commit ·
2a66b0b
1
Parent(s): 5c306b6
Add alert strictness control
Browse files- README.md +2 -1
- coinpush.py +108 -2
README.md
CHANGED
|
@@ -22,7 +22,8 @@ Binance Alpha 标的按普通币种管理,和 BTC / STRC 一样拥有独立页
|
|
| 22 |
|
| 23 |
## 阈值校准
|
| 24 |
基于约 30 天历史行情,按“理想每日触发次数”反推阈值,突出关键转折点;
|
| 25 |
-
可在页面用「通知灵敏度」
|
|
|
|
| 26 |
|
| 27 |
## 环境变量(在 Space 的 Settings → Variables and secrets 配置)
|
| 28 |
- `WEWORK_BOT_WEBHOOK`(必填):企业微信机器人 Webhook
|
|
|
|
| 22 |
|
| 23 |
## 阈值校准
|
| 24 |
基于约 30 天历史行情,按“理想每日触发次数”反推阈值,突出关键转折点;
|
| 25 |
+
可在页面用「通知灵敏度」影响历史校准目标次数,用「阈值严格度」在校准阈值上进一步加严,
|
| 26 |
+
新增币种自动校准。
|
| 27 |
|
| 28 |
## 环境变量(在 Space 的 Settings → Variables and secrets 配置)
|
| 29 |
- `WEWORK_BOT_WEBHOOK`(必填):企业微信机器人 Webhook
|
coinpush.py
CHANGED
|
@@ -245,6 +245,7 @@ def _alpha_defaults():
|
|
| 245 |
return d
|
| 246 |
|
| 247 |
DEFAULT_SPREAD_THRESHOLD_PCT = 3.0
|
|
|
|
| 248 |
|
| 249 |
|
| 250 |
DEFAULT_CONFIG = {
|
|
@@ -258,6 +259,7 @@ DEFAULT_CONFIG = {
|
|
| 258 |
"daily_report_hour": 8,
|
| 259 |
"down_priority": True,
|
| 260 |
"alert_sensitivity": 1.0,
|
|
|
|
| 261 |
# 阈值模式:auto=定时历史自动校准;manual=使用自定义阈值不自动覆盖
|
| 262 |
"calibration_mode": "auto",
|
| 263 |
"auto_calibrate_hours": 1,
|
|
@@ -1540,6 +1542,102 @@ def calibrate_thresholds(api, coin_cfg, logger=print, sensitivity=1.0):
|
|
| 1540 |
return out
|
| 1541 |
|
| 1542 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1543 |
# --- 9. 单币种评估器 ---
|
| 1544 |
class CoinEvaluator:
|
| 1545 |
WINDOW_BACK = {"1m": 2, "5m": 6, "15m": 16, "1h": 61}
|
|
@@ -2329,6 +2427,7 @@ class CryptoMonitor:
|
|
| 2329 |
with self.lock:
|
| 2330 |
cfg = copy.deepcopy(self.config)
|
| 2331 |
interval = int(cfg["global"].get("poll_interval_sec", 30))
|
|
|
|
| 2332 |
|
| 2333 |
self._maybe_daily_report()
|
| 2334 |
self._maybe_auto_calibrate()
|
|
@@ -2340,7 +2439,8 @@ class CryptoMonitor:
|
|
| 2340 |
if not evaluator:
|
| 2341 |
continue
|
| 2342 |
try:
|
| 2343 |
-
|
|
|
|
| 2344 |
if snap:
|
| 2345 |
self.snapshots[name] = snap
|
| 2346 |
else:
|
|
@@ -2366,7 +2466,8 @@ class CryptoMonitor:
|
|
| 2366 |
mon = OKXContractMonitor(self.okx, self.log)
|
| 2367 |
self.okx_contract_mons[inst] = mon
|
| 2368 |
try:
|
| 2369 |
-
|
|
|
|
| 2370 |
except Exception as e:
|
| 2371 |
self.log(f"❌ OKX {inst} 评估异常: {e}")
|
| 2372 |
|
|
@@ -2462,6 +2563,11 @@ def render_config_editor(monitor):
|
|
| 2462 |
"通知灵敏度(越大通知越多,建议 0.5~2.0)", min_value=0.2, max_value=5.0,
|
| 2463 |
value=float(g.get("alert_sensitivity", 1.0)), step=0.1, format="%.1f",
|
| 2464 |
help="校准时按“理想每日触发次数 × 灵敏度”反推阈值;调整后点币种页的“历史自动校准”生效")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2465 |
|
| 2466 |
st.markdown("---")
|
| 2467 |
mode_label = st.radio(
|
|
|
|
| 245 |
return d
|
| 246 |
|
| 247 |
DEFAULT_SPREAD_THRESHOLD_PCT = 3.0
|
| 248 |
+
DEFAULT_ALERT_STRICTNESS = 1.5
|
| 249 |
|
| 250 |
|
| 251 |
DEFAULT_CONFIG = {
|
|
|
|
| 259 |
"daily_report_hour": 8,
|
| 260 |
"down_priority": True,
|
| 261 |
"alert_sensitivity": 1.0,
|
| 262 |
+
"alert_strictness": DEFAULT_ALERT_STRICTNESS,
|
| 263 |
# 阈值模式:auto=定时历史自动校准;manual=使用自定义阈值不自动覆盖
|
| 264 |
"calibration_mode": "auto",
|
| 265 |
"auto_calibrate_hours": 1,
|
|
|
|
| 1542 |
return out
|
| 1543 |
|
| 1544 |
|
| 1545 |
+
STRICTNESS_MULTIPLIED_FIELDS = (
|
| 1546 |
+
"break_24h_pct", "break_7d_pct",
|
| 1547 |
+
"vol_anomaly_mult", "vol_severe_mult",
|
| 1548 |
+
"amp_5m", "amp_15m", "amp_1h",
|
| 1549 |
+
"big_trade_usdt", "cum_trade_5m_usdt",
|
| 1550 |
+
"spread_pct", "spread_severe_pct",
|
| 1551 |
+
"imbalance_ratio", "wall_mult", "wall_min_usdt", "wall_dominance",
|
| 1552 |
+
"ma_deviation_pct",
|
| 1553 |
+
"funding_high", "funding_hot", "funding_neg",
|
| 1554 |
+
"oi_15m", "oi_1h", "oi_4h",
|
| 1555 |
+
"liq_5m_usdt", "liq_severe_usdt",
|
| 1556 |
+
)
|
| 1557 |
+
|
| 1558 |
+
|
| 1559 |
+
def _alert_strictness(global_cfg):
|
| 1560 |
+
try:
|
| 1561 |
+
return max(0.5, float((global_cfg or {}).get("alert_strictness", DEFAULT_ALERT_STRICTNESS)))
|
| 1562 |
+
except (TypeError, ValueError):
|
| 1563 |
+
return DEFAULT_ALERT_STRICTNESS
|
| 1564 |
+
|
| 1565 |
+
|
| 1566 |
+
def _scale_number(value, scale):
|
| 1567 |
+
try:
|
| 1568 |
+
return float(value) * scale
|
| 1569 |
+
except (TypeError, ValueError):
|
| 1570 |
+
return value
|
| 1571 |
+
|
| 1572 |
+
|
| 1573 |
+
def _apply_alert_strictness(coin_cfg, strictness):
|
| 1574 |
+
"""在保存的校准阈值上临时加严,不改写配置文件里的基础阈值。"""
|
| 1575 |
+
try:
|
| 1576 |
+
scale = max(0.5, float(strictness or DEFAULT_ALERT_STRICTNESS))
|
| 1577 |
+
except (TypeError, ValueError):
|
| 1578 |
+
scale = DEFAULT_ALERT_STRICTNESS
|
| 1579 |
+
if abs(scale - 1.0) < 1e-9:
|
| 1580 |
+
return coin_cfg
|
| 1581 |
+
|
| 1582 |
+
cfg = copy.deepcopy(coin_cfg)
|
| 1583 |
+
|
| 1584 |
+
pc = cfg.get("price_change")
|
| 1585 |
+
if isinstance(pc, dict):
|
| 1586 |
+
cfg["price_change"] = {}
|
| 1587 |
+
for win, val in pc.items():
|
| 1588 |
+
if isinstance(val, list):
|
| 1589 |
+
cfg["price_change"][win] = [_scale_number(x, scale) for x in val]
|
| 1590 |
+
else:
|
| 1591 |
+
cfg["price_change"][win] = _scale_number(val, scale)
|
| 1592 |
+
|
| 1593 |
+
for key in STRICTNESS_MULTIPLIED_FIELDS:
|
| 1594 |
+
if key in cfg:
|
| 1595 |
+
cfg[key] = _scale_number(cfg[key], scale)
|
| 1596 |
+
|
| 1597 |
+
if "wall_vanish_pct" in cfg:
|
| 1598 |
+
scaled = _scale_number(cfg["wall_vanish_pct"], scale)
|
| 1599 |
+
if isinstance(scaled, (int, float)):
|
| 1600 |
+
cfg["wall_vanish_pct"] = min(95.0, scaled)
|
| 1601 |
+
|
| 1602 |
+
if "rsi_high" in cfg:
|
| 1603 |
+
try:
|
| 1604 |
+
high = float(cfg["rsi_high"])
|
| 1605 |
+
cfg["rsi_high"] = min(95.0, 50.0 + max(0.0, high - 50.0) * scale)
|
| 1606 |
+
except (TypeError, ValueError):
|
| 1607 |
+
pass
|
| 1608 |
+
if "rsi_low" in cfg:
|
| 1609 |
+
try:
|
| 1610 |
+
low = float(cfg["rsi_low"])
|
| 1611 |
+
cfg["rsi_low"] = max(5.0, 50.0 - max(0.0, 50.0 - low) * scale)
|
| 1612 |
+
except (TypeError, ValueError):
|
| 1613 |
+
pass
|
| 1614 |
+
|
| 1615 |
+
if "consecutive_klines" in cfg:
|
| 1616 |
+
try:
|
| 1617 |
+
base = int(cfg["consecutive_klines"])
|
| 1618 |
+
delta = int(max(0.0, scale - 1.0) * 2 + 0.999)
|
| 1619 |
+
cfg["consecutive_klines"] = max(2, base + delta)
|
| 1620 |
+
except (TypeError, ValueError):
|
| 1621 |
+
pass
|
| 1622 |
+
|
| 1623 |
+
return cfg
|
| 1624 |
+
|
| 1625 |
+
|
| 1626 |
+
def _apply_okx_contract_strictness(contract_cfg, strictness):
|
| 1627 |
+
try:
|
| 1628 |
+
scale = max(0.5, float(strictness or DEFAULT_ALERT_STRICTNESS))
|
| 1629 |
+
except (TypeError, ValueError):
|
| 1630 |
+
scale = DEFAULT_ALERT_STRICTNESS
|
| 1631 |
+
if abs(scale - 1.0) < 1e-9:
|
| 1632 |
+
return contract_cfg
|
| 1633 |
+
|
| 1634 |
+
cfg = copy.deepcopy(contract_cfg)
|
| 1635 |
+
for key in ("price_change_pct", "oi_change_pct", "funding_high", "funding_neg", "amp_24h_pct"):
|
| 1636 |
+
if key in cfg:
|
| 1637 |
+
cfg[key] = _scale_number(cfg[key], scale)
|
| 1638 |
+
return cfg
|
| 1639 |
+
|
| 1640 |
+
|
| 1641 |
# --- 9. 单币种评估器 ---
|
| 1642 |
class CoinEvaluator:
|
| 1643 |
WINDOW_BACK = {"1m": 2, "5m": 6, "15m": 16, "1h": 61}
|
|
|
|
| 2427 |
with self.lock:
|
| 2428 |
cfg = copy.deepcopy(self.config)
|
| 2429 |
interval = int(cfg["global"].get("poll_interval_sec", 30))
|
| 2430 |
+
strictness = _alert_strictness(cfg.get("global", {}))
|
| 2431 |
|
| 2432 |
self._maybe_daily_report()
|
| 2433 |
self._maybe_auto_calibrate()
|
|
|
|
| 2439 |
if not evaluator:
|
| 2440 |
continue
|
| 2441 |
try:
|
| 2442 |
+
eval_cfg = _apply_alert_strictness(coin_cfg, strictness)
|
| 2443 |
+
alerts, snap = evaluator.evaluate(eval_cfg)
|
| 2444 |
if snap:
|
| 2445 |
self.snapshots[name] = snap
|
| 2446 |
else:
|
|
|
|
| 2466 |
mon = OKXContractMonitor(self.okx, self.log)
|
| 2467 |
self.okx_contract_mons[inst] = mon
|
| 2468 |
try:
|
| 2469 |
+
eval_cinfo = _apply_okx_contract_strictness(cinfo, strictness)
|
| 2470 |
+
total_alerts.extend(mon.evaluate(eval_cinfo))
|
| 2471 |
except Exception as e:
|
| 2472 |
self.log(f"❌ OKX {inst} 评估异常: {e}")
|
| 2473 |
|
|
|
|
| 2563 |
"通知灵敏度(越大通知越多,建议 0.5~2.0)", min_value=0.2, max_value=5.0,
|
| 2564 |
value=float(g.get("alert_sensitivity", 1.0)), step=0.1, format="%.1f",
|
| 2565 |
help="校准时按“理想每日触发次数 × 灵敏度”反推阈值;调整后点币种页的“历史自动校准”生效")
|
| 2566 |
+
g["alert_strictness"] = st.number_input(
|
| 2567 |
+
"阈值严格度(越大通知越少,1.0=原样)", min_value=0.5, max_value=5.0,
|
| 2568 |
+
value=float(g.get("alert_strictness", DEFAULT_ALERT_STRICTNESS)),
|
| 2569 |
+
step=0.1, format="%.1f",
|
| 2570 |
+
help="在历史校准/手动阈值基础上临时放大触发门槛,保存后立刻生效;建议 1.5~2.5")
|
| 2571 |
|
| 2572 |
st.markdown("---")
|
| 2573 |
mode_label = st.radio(
|