Spaces:
Paused
Paused
zt p commited on
Commit ·
6ffa41d
1
Parent(s): 49efd8b
优化买卖墙告警(绝对金额门槛+方向主导,过滤无方向双墙); 新增阈值模式(定时自动校准/自定义可选)
Browse files- coinpush.py +116 -19
coinpush.py
CHANGED
|
@@ -97,6 +97,9 @@ def _btc_defaults():
|
|
| 97 |
"spread_pct": 0.03, "spread_severe_pct": 0.08,
|
| 98 |
"imbalance_depth_pct": 0.5, "imbalance_ratio": 2.5,
|
| 99 |
"wall_depth_pct": 1.0, "wall_mult": 3.0, "wall_vanish_pct": 60.0,
|
|
|
|
|
|
|
|
|
|
| 100 |
# 技术指标
|
| 101 |
"ma_deviation_pct": 2.5,
|
| 102 |
"consecutive_klines": 5,
|
|
@@ -130,6 +133,7 @@ def _ton_defaults():
|
|
| 130 |
"spread_pct": 0.12, "spread_severe_pct": 0.35,
|
| 131 |
"imbalance_depth_pct": 1.0, "imbalance_ratio": 3.0,
|
| 132 |
"wall_depth_pct": 2.0, "wall_mult": 4.0, "wall_vanish_pct": 70.0,
|
|
|
|
| 133 |
"ma_deviation_pct": 5.0,
|
| 134 |
"consecutive_klines": 4,
|
| 135 |
"rsi_high": 80.0, "rsi_low": 20.0,
|
|
@@ -150,6 +154,10 @@ DEFAULT_CONFIG = {
|
|
| 150 |
"daily_report_hour": 9,
|
| 151 |
"down_priority": True,
|
| 152 |
"alert_sensitivity": 1.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
},
|
| 154 |
}
|
| 155 |
|
|
@@ -565,6 +573,8 @@ def calibrate_thresholds(api, coin_cfg, logger=print, sensitivity=1.0):
|
|
| 565 |
# 单笔大额:一笔成交达到约 3 分钟的平均成交额才算“巨单”
|
| 566 |
avg1m_qvol = avg5_qvol / 5.0
|
| 567 |
out["big_trade_usdt"] = round(max(avg1m_qvol * 3.0 / s, 5000))
|
|
|
|
|
|
|
| 568 |
|
| 569 |
# 4) 偏离 MA20:1h 历史偏离
|
| 570 |
if hist_1h and len(hist_1h) > 40:
|
|
@@ -853,38 +863,73 @@ class CoinEvaluator:
|
|
| 853 |
add("imbalance", "imbalance_sell", "跌", "卖盘失衡(卖强)",
|
| 854 |
f"±{cfg['imbalance_depth_pct']}%深度内 卖/买 = {ask_usdt / bid_usdt:.1f} 倍")
|
| 855 |
|
| 856 |
-
# 买卖墙 + 墙消失
|
| 857 |
-
|
| 858 |
-
self._wall_side(cfg, "bid", "买墙", bids, [p for p, q in bids if p >= mid * (1 - wp)],
|
| 859 |
-
bids, mid, wp, head, add, "涨")
|
| 860 |
-
self._wall_side(cfg, "ask", "卖墙", asks, [p for p, q in asks if p <= mid * (1 + wp)],
|
| 861 |
-
asks, mid, wp, head, add, "跌")
|
| 862 |
|
| 863 |
-
def
|
|
|
|
| 864 |
if side == "bid":
|
| 865 |
near = [(p, q) for p, q in orders if p >= mid * (1 - wp)]
|
| 866 |
else:
|
| 867 |
near = [(p, q) for p, q in orders if p <= mid * (1 + wp)]
|
| 868 |
if len(near) < 3:
|
| 869 |
-
|
| 870 |
-
return
|
| 871 |
notionals = [p * q for p, q in near]
|
| 872 |
max_n = max(notionals)
|
| 873 |
avg_n = sum(notionals) / len(notionals)
|
| 874 |
wall_price = near[notionals.index(max_n)][0]
|
| 875 |
-
|
| 876 |
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
add("wall_vanish", f"wall_vanish_{side}",
|
| 885 |
"跌" if side == "bid" else "涨", f"{label}快速撤离",
|
| 886 |
f"原 {label} {fmt_usdt(prev)} 已减少超 {cfg['wall_vanish_pct']}%")
|
| 887 |
-
|
|
|
|
|
|
|
| 888 |
|
| 889 |
def _check_ma(self, cfg, sym, price, head, add):
|
| 890 |
kh = self.api.klines(sym, "1h", 21)
|
|
@@ -1045,7 +1090,7 @@ class CryptoMonitor:
|
|
| 1045 |
self.log("⚙️ 配置已更新并保存")
|
| 1046 |
|
| 1047 |
def calibrate_all(self):
|
| 1048 |
-
"""对所有币种执行历史校准并保存(
|
| 1049 |
with self.lock:
|
| 1050 |
cfg = copy.deepcopy(self.config)
|
| 1051 |
scale = float(cfg["global"].get("alert_sensitivity", 1.0))
|
|
@@ -1055,11 +1100,40 @@ class CryptoMonitor:
|
|
| 1055 |
cfg["coins"][name] = _deep_merge(coin, overrides)
|
| 1056 |
except Exception as e:
|
| 1057 |
self.log(f"⚠️ {name} 校准失败: {e}")
|
|
|
|
| 1058 |
with self.lock:
|
| 1059 |
self.config = cfg
|
| 1060 |
save_config(cfg)
|
| 1061 |
self._rebuild_evaluators()
|
| 1062 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1063 |
def _maybe_daily_report(self):
|
| 1064 |
now = get_beijing_now()
|
| 1065 |
today = now.strftime("%Y-%m-%d")
|
|
@@ -1108,6 +1182,7 @@ class CryptoMonitor:
|
|
| 1108 |
interval = int(cfg["global"].get("poll_interval_sec", 30))
|
| 1109 |
|
| 1110 |
self._maybe_daily_report()
|
|
|
|
| 1111 |
|
| 1112 |
self.status_text = "🔥 监控中"
|
| 1113 |
total_alerts = []
|
|
@@ -1176,6 +1251,8 @@ FIELD_GROUPS = [
|
|
| 1176 |
("imbalance_ratio", "买卖失衡倍数"),
|
| 1177 |
("wall_depth_pct", "买卖墙深度 %"),
|
| 1178 |
("wall_mult", "墙厚度倍数"),
|
|
|
|
|
|
|
| 1179 |
("wall_vanish_pct", "墙消失减少 %"),
|
| 1180 |
]),
|
| 1181 |
("技术指标", [
|
|
@@ -1216,6 +1293,26 @@ def render_config_editor(monitor):
|
|
| 1216 |
value=float(g.get("alert_sensitivity", 1.0)), step=0.1, format="%.1f",
|
| 1217 |
help="校准时按“理想每日触发次数 × 灵敏度”反推阈值;调整后点币种页的“历史自动校准”生效")
|
| 1218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1219 |
coin_names = list(cfg["coins"].keys())
|
| 1220 |
tabs = st.tabs(coin_names + ["➕ 新增币种"])
|
| 1221 |
|
|
|
|
| 97 |
"spread_pct": 0.03, "spread_severe_pct": 0.08,
|
| 98 |
"imbalance_depth_pct": 0.5, "imbalance_ratio": 2.5,
|
| 99 |
"wall_depth_pct": 1.0, "wall_mult": 3.0, "wall_vanish_pct": 60.0,
|
| 100 |
+
# 买卖墙绝对门槛与方向主导:墙金额需 >= wall_min_usdt,且一侧需明显
|
| 101 |
+
# 大于对侧(>= wall_dominance 倍)才推送,避免买卖双墙同时出现却无方向
|
| 102 |
+
"wall_min_usdt": 3_000_000, "wall_dominance": 1.8,
|
| 103 |
# 技术指标
|
| 104 |
"ma_deviation_pct": 2.5,
|
| 105 |
"consecutive_klines": 5,
|
|
|
|
| 133 |
"spread_pct": 0.12, "spread_severe_pct": 0.35,
|
| 134 |
"imbalance_depth_pct": 1.0, "imbalance_ratio": 3.0,
|
| 135 |
"wall_depth_pct": 2.0, "wall_mult": 4.0, "wall_vanish_pct": 70.0,
|
| 136 |
+
"wall_min_usdt": 300_000, "wall_dominance": 1.8,
|
| 137 |
"ma_deviation_pct": 5.0,
|
| 138 |
"consecutive_klines": 4,
|
| 139 |
"rsi_high": 80.0, "rsi_low": 20.0,
|
|
|
|
| 154 |
"daily_report_hour": 9,
|
| 155 |
"down_priority": True,
|
| 156 |
"alert_sensitivity": 1.0,
|
| 157 |
+
# 阈值模式:auto=定时历史自动校准;manual=使用自定义阈值不自动覆盖
|
| 158 |
+
"calibration_mode": "auto",
|
| 159 |
+
"auto_calibrate_hours": 24,
|
| 160 |
+
"last_calibrate_ts": 0,
|
| 161 |
},
|
| 162 |
}
|
| 163 |
|
|
|
|
| 573 |
# 单笔大额:一笔成交达到约 3 分钟的平均成交额才算“巨单”
|
| 574 |
avg1m_qvol = avg5_qvol / 5.0
|
| 575 |
out["big_trade_usdt"] = round(max(avg1m_qvol * 3.0 / s, 5000))
|
| 576 |
+
# 买卖墙绝对门槛:与单笔巨单同量级,过滤掉无意义的小墙
|
| 577 |
+
out["wall_min_usdt"] = round(max(out["big_trade_usdt"], avg1m_qvol * 3.0))
|
| 578 |
|
| 579 |
# 4) 偏离 MA20:1h 历史偏离
|
| 580 |
if hist_1h and len(hist_1h) > 40:
|
|
|
|
| 863 |
add("imbalance", "imbalance_sell", "跌", "卖盘失衡(卖强)",
|
| 864 |
f"±{cfg['imbalance_depth_pct']}%深度内 卖/买 = {ask_usdt / bid_usdt:.1f} 倍")
|
| 865 |
|
| 866 |
+
# 买卖墙 + 墙消失(合并评估,只推送方向明确的主导墙)
|
| 867 |
+
self._check_walls(cfg, bids, asks, mid, head, add)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
|
| 869 |
+
def _side_wall(self, orders, mid, wp, side):
|
| 870 |
+
"""返回 (最大挂单额, 该价位, 近端均值);不足样本返回 (0,0,0)"""
|
| 871 |
if side == "bid":
|
| 872 |
near = [(p, q) for p, q in orders if p >= mid * (1 - wp)]
|
| 873 |
else:
|
| 874 |
near = [(p, q) for p, q in orders if p <= mid * (1 + wp)]
|
| 875 |
if len(near) < 3:
|
| 876 |
+
return 0.0, 0.0, 0.0
|
|
|
|
| 877 |
notionals = [p * q for p, q in near]
|
| 878 |
max_n = max(notionals)
|
| 879 |
avg_n = sum(notionals) / len(notionals)
|
| 880 |
wall_price = near[notionals.index(max_n)][0]
|
| 881 |
+
return max_n, wall_price, avg_n
|
| 882 |
|
| 883 |
+
def _check_walls(self, cfg, bids, asks, mid, head, add):
|
| 884 |
+
wp = cfg["wall_depth_pct"] / 100
|
| 885 |
+
min_usdt = cfg.get("wall_min_usdt", 0)
|
| 886 |
+
dominance = cfg.get("wall_dominance", 1.8)
|
| 887 |
+
mult = cfg["wall_mult"]
|
| 888 |
+
|
| 889 |
+
bid_n, bid_price, bid_avg = self._side_wall(bids, mid, wp, "bid")
|
| 890 |
+
ask_n, ask_price, ask_avg = self._side_wall(asks, mid, wp, "ask")
|
| 891 |
+
|
| 892 |
+
def qualifies(max_n, avg_n):
|
| 893 |
+
# 同时满足:相对附近均值够厚 + 绝对金额够大
|
| 894 |
+
return avg_n > 0 and max_n >= avg_n * mult and max_n >= min_usdt
|
| 895 |
+
|
| 896 |
+
bid_ok = qualifies(bid_n, bid_avg)
|
| 897 |
+
ask_ok = qualifies(ask_n, ask_avg)
|
| 898 |
+
|
| 899 |
+
# 方向判定:只在一侧明显主导对侧时推送,双墙均衡视为无方向不推送
|
| 900 |
+
emit_side = None
|
| 901 |
+
if bid_ok and ask_ok:
|
| 902 |
+
if bid_n >= ask_n * dominance:
|
| 903 |
+
emit_side = "bid"
|
| 904 |
+
elif ask_n >= bid_n * dominance:
|
| 905 |
+
emit_side = "ask"
|
| 906 |
+
# 否则买卖墙势均力敌,无明确方向,跳过
|
| 907 |
+
elif bid_ok:
|
| 908 |
+
emit_side = "bid"
|
| 909 |
+
elif ask_ok:
|
| 910 |
+
emit_side = "ask"
|
| 911 |
+
|
| 912 |
+
if emit_side == "bid":
|
| 913 |
+
ratio = ask_n / bid_n if ask_n > 0 else float("inf")
|
| 914 |
+
add("wall", "wall_bid", "涨", f"出现买墙 {fmt_usdt(bid_n)}",
|
| 915 |
+
f"{fmt_price(bid_price)} 附近买单 {fmt_usdt(bid_n)},"
|
| 916 |
+
f"为附近均值的 {bid_n / bid_avg:.1f} 倍,且明显强于卖墙({fmt_usdt(ask_n)})")
|
| 917 |
+
elif emit_side == "ask":
|
| 918 |
+
add("wall", "wall_ask", "跌", f"出现卖墙 {fmt_usdt(ask_n)}",
|
| 919 |
+
f"{fmt_price(ask_price)} 附近卖单 {fmt_usdt(ask_n)},"
|
| 920 |
+
f"为附近均值的 {ask_n / ask_avg:.1f} 倍,且明显强于买墙({fmt_usdt(bid_n)})")
|
| 921 |
+
|
| 922 |
+
# 墙消失:之前推送过的主导墙快速撤离
|
| 923 |
+
for side, cur_n in (("bid", bid_n), ("ask", ask_n)):
|
| 924 |
+
prev = self.prev_walls.get(side, 0.0)
|
| 925 |
+
if prev > 0 and cur_n <= prev * (1 - cfg["wall_vanish_pct"] / 100):
|
| 926 |
+
label = "买墙" if side == "bid" else "卖墙"
|
| 927 |
add("wall_vanish", f"wall_vanish_{side}",
|
| 928 |
"跌" if side == "bid" else "涨", f"{label}快速撤离",
|
| 929 |
f"原 {label} {fmt_usdt(prev)} 已减少超 {cfg['wall_vanish_pct']}%")
|
| 930 |
+
# 记录当前主导墙用于下次消失检测(仅记录达标的墙)
|
| 931 |
+
self.prev_walls["bid"] = bid_n if bid_ok else 0.0
|
| 932 |
+
self.prev_walls["ask"] = ask_n if ask_ok else 0.0
|
| 933 |
|
| 934 |
def _check_ma(self, cfg, sym, price, head, add):
|
| 935 |
kh = self.api.klines(sym, "1h", 21)
|
|
|
|
| 1090 |
self.log("⚙️ 配置已更新并保存")
|
| 1091 |
|
| 1092 |
def calibrate_all(self):
|
| 1093 |
+
"""对所有币种执行历史校准并保存(首次启动 / 定时自动校准调用)"""
|
| 1094 |
with self.lock:
|
| 1095 |
cfg = copy.deepcopy(self.config)
|
| 1096 |
scale = float(cfg["global"].get("alert_sensitivity", 1.0))
|
|
|
|
| 1100 |
cfg["coins"][name] = _deep_merge(coin, overrides)
|
| 1101 |
except Exception as e:
|
| 1102 |
self.log(f"⚠️ {name} 校准失败: {e}")
|
| 1103 |
+
cfg["global"]["last_calibrate_ts"] = time.time()
|
| 1104 |
with self.lock:
|
| 1105 |
self.config = cfg
|
| 1106 |
save_config(cfg)
|
| 1107 |
self._rebuild_evaluators()
|
| 1108 |
|
| 1109 |
+
def _maybe_auto_calibrate(self):
|
| 1110 |
+
"""自动模式下,按设定间隔定时重新历史校准(后台线程执行,不阻塞轮询)"""
|
| 1111 |
+
g = self.config["global"]
|
| 1112 |
+
if g.get("calibration_mode", "auto") != "auto":
|
| 1113 |
+
return
|
| 1114 |
+
if getattr(self, "_calibrating", False):
|
| 1115 |
+
return
|
| 1116 |
+
hours = float(g.get("auto_calibrate_hours", 24))
|
| 1117 |
+
if hours <= 0:
|
| 1118 |
+
return
|
| 1119 |
+
last = float(g.get("last_calibrate_ts", 0) or 0)
|
| 1120 |
+
if time.time() - last < hours * 3600:
|
| 1121 |
+
return
|
| 1122 |
+
|
| 1123 |
+
self._calibrating = True
|
| 1124 |
+
|
| 1125 |
+
def _job():
|
| 1126 |
+
try:
|
| 1127 |
+
self.log(f"🧪 定时自动校准(每 {hours:g} 小时)...")
|
| 1128 |
+
self.calibrate_all()
|
| 1129 |
+
self.log("✅ 定时自动校准完成并已保存")
|
| 1130 |
+
except Exception as e:
|
| 1131 |
+
self.log(f"⚠️ 定时校准异常: {e}")
|
| 1132 |
+
finally:
|
| 1133 |
+
self._calibrating = False
|
| 1134 |
+
|
| 1135 |
+
threading.Thread(target=_job, daemon=True).start()
|
| 1136 |
+
|
| 1137 |
def _maybe_daily_report(self):
|
| 1138 |
now = get_beijing_now()
|
| 1139 |
today = now.strftime("%Y-%m-%d")
|
|
|
|
| 1182 |
interval = int(cfg["global"].get("poll_interval_sec", 30))
|
| 1183 |
|
| 1184 |
self._maybe_daily_report()
|
| 1185 |
+
self._maybe_auto_calibrate()
|
| 1186 |
|
| 1187 |
self.status_text = "🔥 监控中"
|
| 1188 |
total_alerts = []
|
|
|
|
| 1251 |
("imbalance_ratio", "买卖失衡倍数"),
|
| 1252 |
("wall_depth_pct", "买卖墙深度 %"),
|
| 1253 |
("wall_mult", "墙厚度倍数"),
|
| 1254 |
+
("wall_min_usdt", "墙最小金额 USDT"),
|
| 1255 |
+
("wall_dominance", "墙方向主导倍数"),
|
| 1256 |
("wall_vanish_pct", "墙消失减少 %"),
|
| 1257 |
]),
|
| 1258 |
("技术指标", [
|
|
|
|
| 1293 |
value=float(g.get("alert_sensitivity", 1.0)), step=0.1, format="%.1f",
|
| 1294 |
help="校准时按“理想每日触发次数 × 灵敏度”反推阈值;调整后点币种页的“历史自动校准”生效")
|
| 1295 |
|
| 1296 |
+
st.markdown("---")
|
| 1297 |
+
mode_label = st.radio(
|
| 1298 |
+
"阈值模式",
|
| 1299 |
+
["自动校准", "自定义"],
|
| 1300 |
+
index=0 if g.get("calibration_mode", "auto") == "auto" else 1,
|
| 1301 |
+
horizontal=True,
|
| 1302 |
+
help="自动校准:按设定间隔定时拉取历史行情重算阈值;自定义:仅使用你手动设置的阈值,不自动覆盖")
|
| 1303 |
+
g["calibration_mode"] = "auto" if mode_label == "自动校准" else "manual"
|
| 1304 |
+
if g["calibration_mode"] == "auto":
|
| 1305 |
+
g["auto_calibrate_hours"] = st.number_input(
|
| 1306 |
+
"自动校准间隔(小时)", min_value=1, max_value=168,
|
| 1307 |
+
value=int(g.get("auto_calibrate_hours", 24)), step=1,
|
| 1308 |
+
help="到期后后台自动对所有币种重新历史校准并保存")
|
| 1309 |
+
last = float(g.get("last_calibrate_ts", 0) or 0)
|
| 1310 |
+
if last > 0:
|
| 1311 |
+
last_str = datetime.fromtimestamp(last, timezone(timedelta(hours=8))).strftime("%m-%d %H:%M")
|
| 1312 |
+
st.caption(f"上次校准:{last_str}(北京时间)")
|
| 1313 |
+
else:
|
| 1314 |
+
st.caption("自定义模式:阈值以下方各币种设置为准,不会被自动覆盖。")
|
| 1315 |
+
|
| 1316 |
coin_names = list(cfg["coins"].keys())
|
| 1317 |
tabs = st.tabs(coin_names + ["➕ 新增币种"])
|
| 1318 |
|