Spaces:
Paused
Paused
zt p commited on
Commit ·
46b44fb
1
Parent(s): 0f0aa8f
STRC 改为 OKX 合约处理(STRC-USDT-SWAP),统一 OKXContractMonitor 支持休市属性; Bitget STRC 仅用于跨所差价; 旧配置自动迁移
Browse files- README.md +5 -3
- coinpush.py +195 -152
README.md
CHANGED
|
@@ -33,8 +33,10 @@ pinned: false
|
|
| 33 |
- `BITGET_BASE` / `OKX_BASE`(可选,受限区域可指向可用代理)
|
| 34 |
|
| 35 |
## 多交易所扩展
|
| 36 |
-
- 跨所差价提醒:BTC、TON/GRAM 在 Binance / Bitget / OKX 之间的价差(Binance 仍叫 TON,Bitget/OKX 已改名 GRAM)。
|
| 37 |
-
-
|
| 38 |
-
-
|
|
|
|
|
|
|
| 39 |
- 以上均可在页面「多交易所设置」开关与调参。Bitget/OKX 对部分地区有访问限制,
|
| 40 |
若 Space 处于受限区域,需通过 `BITGET_BASE` / `OKX_BASE` 指向可用代理。
|
|
|
|
| 33 |
- `BITGET_BASE` / `OKX_BASE`(可选,受限区域可指向可用代理)
|
| 34 |
|
| 35 |
## 多交易所扩展
|
| 36 |
+
- 跨所差价提醒:BTC、TON/GRAM、STRC 在 Binance / Bitget / OKX 之间的价差(Binance 仍叫 TON,Bitget/OKX 已改名 GRAM)。
|
| 37 |
+
- OKX 永续合约关键信息(数据主源 OKX,交易量更大):合约价异动、24h 振幅、资金费率、持仓量变化。
|
| 38 |
+
- `GRAM-USDT-SWAP`:普通 7x24 合约。
|
| 39 |
+
- `STRC-USDT-SWAP`:股票类合约,等同普通合约但多一个「休市」属性——价格长时间不变即判定休市,期间不提醒,复市后首次异动标注「复市后」。
|
| 40 |
+
- Bitget 数据(含 STRC 现货 rToken `RSTRCUSDT`)用于跨所差价对比。
|
| 41 |
- 以上均可在页面「多交易所设置」开关与调参。Bitget/OKX 对部分地区有访问限制,
|
| 42 |
若 Space 处于受限区域,需通过 `BITGET_BASE` / `OKX_BASE` 指向可用代理。
|
coinpush.py
CHANGED
|
@@ -171,7 +171,7 @@ DEFAULT_CONFIG = {
|
|
| 171 |
"auto_calibrate_hours": 24,
|
| 172 |
"last_calibrate_ts": 0,
|
| 173 |
},
|
| 174 |
-
# 多交易所扩展:跨所差价、
|
| 175 |
"exchanges": {
|
| 176 |
"enable_bitget": True,
|
| 177 |
"enable_okx": True,
|
|
@@ -183,26 +183,33 @@ DEFAULT_CONFIG = {
|
|
| 183 |
# Binance 仍叫 TON,Bitget/OKX 已改名 GRAM
|
| 184 |
{"name": "BTC", "binance": "BTCUSDT", "bitget": "BTCUSDT", "okx": "BTC-USDT"},
|
| 185 |
{"name": "TON/GRAM", "binance": "TONUSDT", "bitget": "GRAMUSDT", "okx": "GRAM-USDT"},
|
|
|
|
|
|
|
| 186 |
],
|
| 187 |
},
|
| 188 |
-
#
|
| 189 |
-
"
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
},
|
| 207 |
}
|
| 208 |
|
|
@@ -231,6 +238,56 @@ def _deep_merge(base, override):
|
|
| 231 |
return result
|
| 232 |
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
def load_config():
|
| 235 |
"""读取配置文件,并以默认值补全缺失字段"""
|
| 236 |
cfg = copy.deepcopy(DEFAULT_CONFIG)
|
|
@@ -250,6 +307,7 @@ def load_config():
|
|
| 250 |
cfg["coins"] = merged_coins
|
| 251 |
# exchanges:跨所/STRC/OKX 合约配置,缺失字段以默认补全
|
| 252 |
cfg["exchanges"] = _deep_merge(cfg["exchanges"], saved.get("exchanges", {}))
|
|
|
|
| 253 |
except Exception as e:
|
| 254 |
print(f"⚠️ 配置读取失败,使用默认值: {e}")
|
| 255 |
return cfg
|
|
@@ -1237,7 +1295,7 @@ class CoinEvaluator:
|
|
| 1237 |
return None
|
| 1238 |
|
| 1239 |
|
| 1240 |
-
# --- 9b. 多交易所监控(跨所差价 /
|
| 1241 |
def make_alert(name, category, key, direction, headline, body):
|
| 1242 |
"""构造与 AlertManager 兼容的告警字典"""
|
| 1243 |
emoji = SIGNAL_EMOJI[direction]
|
|
@@ -1307,126 +1365,101 @@ class CrossExchangeMonitor:
|
|
| 1307 |
return alerts
|
| 1308 |
|
| 1309 |
|
| 1310 |
-
class
|
| 1311 |
-
"""
|
| 1312 |
-
|
| 1313 |
-
|
| 1314 |
-
|
| 1315 |
-
self.logger = logger
|
| 1316 |
-
self.last_price = None
|
| 1317 |
-
self.last_change_ts = 0.0
|
| 1318 |
-
|
| 1319 |
-
def evaluate(self, strc_cfg):
|
| 1320 |
-
alerts = []
|
| 1321 |
-
if not strc_cfg.get("enable", True):
|
| 1322 |
-
return alerts
|
| 1323 |
-
symbol = strc_cfg.get("symbol", "RSTRCUSDT")
|
| 1324 |
-
t = self.bitget.spot_ticker(symbol)
|
| 1325 |
-
if not t:
|
| 1326 |
-
return alerts
|
| 1327 |
-
price = t["price"]
|
| 1328 |
-
now = time.time()
|
| 1329 |
-
name = f"STRC({symbol})"
|
| 1330 |
-
|
| 1331 |
-
if self.last_price is None:
|
| 1332 |
-
self.last_price = price
|
| 1333 |
-
self.last_change_ts = now
|
| 1334 |
-
return alerts
|
| 1335 |
-
|
| 1336 |
-
static_secs = float(strc_cfg.get("static_secs", 600))
|
| 1337 |
-
skip_static = strc_cfg.get("skip_when_static", True)
|
| 1338 |
-
is_static = (now - self.last_change_ts) >= static_secs
|
| 1339 |
-
|
| 1340 |
-
if price != self.last_price:
|
| 1341 |
-
pct = (price - self.last_price) / self.last_price * 100
|
| 1342 |
-
paused = (now - self.last_change_ts) >= static_secs
|
| 1343 |
-
self.last_price = price
|
| 1344 |
-
self.last_change_ts = now
|
| 1345 |
-
if abs(pct) >= float(strc_cfg.get("change_pct", 1.0)):
|
| 1346 |
-
direction = "涨" if pct > 0 else "跌"
|
| 1347 |
-
arrow = "📈" if pct > 0 else "📉"
|
| 1348 |
-
prefix = "复市后" if paused else ""
|
| 1349 |
-
alerts.append(make_alert(
|
| 1350 |
-
name, "strc", "strc_change", direction,
|
| 1351 |
-
f"{prefix}股价异动 {arrow} {pct:+.2f}%",
|
| 1352 |
-
f"现价 {fmt_price(price)} USDT(较上次 {pct:+.2f}%)"))
|
| 1353 |
-
return alerts
|
| 1354 |
-
|
| 1355 |
-
# 价格未变:休市判定
|
| 1356 |
-
if skip_static and is_static:
|
| 1357 |
-
return alerts
|
| 1358 |
-
|
| 1359 |
-
# 交易中但价格暂稳:检查 24h 振幅
|
| 1360 |
-
if t["low24"] > 0:
|
| 1361 |
-
amp = (t["high24"] - t["low24"]) / t["low24"] * 100
|
| 1362 |
-
if amp >= float(strc_cfg.get("amp_24h_pct", 3.0)):
|
| 1363 |
-
alerts.append(make_alert(
|
| 1364 |
-
name, "strc", "strc_amp", "关注",
|
| 1365 |
-
f"24h 振幅 {amp:.2f}%",
|
| 1366 |
-
f"24h 区间 {fmt_price(t['low24'])} ~ {fmt_price(t['high24'])}"
|
| 1367 |
-
f",现价 {fmt_price(price)}"))
|
| 1368 |
-
return alerts
|
| 1369 |
-
|
| 1370 |
-
|
| 1371 |
-
class OKXGramSwapMonitor:
|
| 1372 |
-
"""OKX GRAM 永续合约关键信息:价格异动 / 资金费率 / 持仓量变化"""
|
| 1373 |
|
| 1374 |
def __init__(self, okx_api, logger):
|
| 1375 |
self.okx = okx_api
|
| 1376 |
self.logger = logger
|
| 1377 |
self.last_price = None
|
| 1378 |
self.last_oi = None
|
|
|
|
| 1379 |
|
| 1380 |
-
def evaluate(self,
|
| 1381 |
alerts = []
|
| 1382 |
-
if not
|
| 1383 |
return alerts
|
| 1384 |
-
inst =
|
| 1385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1386 |
|
| 1387 |
-
# 1) 合约价
|
|
|
|
|
|
|
| 1388 |
t = self.okx.ticker(inst)
|
| 1389 |
if t:
|
| 1390 |
price = t["price"]
|
| 1391 |
-
if self.last_price
|
| 1392 |
-
|
| 1393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1394 |
direction = "涨" if pct > 0 else "跌"
|
| 1395 |
arrow = "📈" if pct > 0 else "📉"
|
|
|
|
| 1396 |
alerts.append(make_alert(
|
| 1397 |
-
|
| 1398 |
-
f"合约价异动 {arrow} {pct:+.2f}%",
|
| 1399 |
f"现价 {fmt_price(price)} USDT(较上次 {pct:+.2f}%)"))
|
| 1400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1401 |
|
| 1402 |
-
#
|
| 1403 |
fr = self.okx.funding_rate(inst)
|
| 1404 |
if fr:
|
| 1405 |
rate = fr["rate_pct"]
|
| 1406 |
-
high = float(
|
| 1407 |
-
neg = float(
|
| 1408 |
if rate >= high:
|
| 1409 |
alerts.append(make_alert(
|
| 1410 |
-
|
| 1411 |
f"资金费率偏高 {rate:+.4f}%",
|
| 1412 |
f"多头需付费,资金费率 {rate:+.4f}%(阈值 {high}%),警惕多头拥挤"))
|
| 1413 |
elif rate <= neg:
|
| 1414 |
alerts.append(make_alert(
|
| 1415 |
-
|
| 1416 |
f"资金费率为负 {rate:+.4f}%",
|
| 1417 |
f"空头需付费,资金费率 {rate:+.4f}%(阈值 {neg}%),警惕空头拥挤"))
|
| 1418 |
|
| 1419 |
-
#
|
| 1420 |
oi = self.okx.open_interest(inst)
|
| 1421 |
if oi:
|
| 1422 |
cur = oi["oi"]
|
| 1423 |
if self.last_oi and self.last_oi > 0:
|
| 1424 |
pct = (cur - self.last_oi) / self.last_oi * 100
|
| 1425 |
-
if abs(pct) >= float(
|
| 1426 |
direction = "涨" if pct > 0 else "跌"
|
| 1427 |
word = "增仓" if pct > 0 else "减仓"
|
| 1428 |
alerts.append(make_alert(
|
| 1429 |
-
|
| 1430 |
f"持仓量{word} {pct:+.2f}%",
|
| 1431 |
f"持仓量 {fmt_usdt(oi['oi_usd'])}(较上次 {pct:+.2f}%)"))
|
| 1432 |
self.last_oi = cur
|
|
@@ -1457,8 +1490,8 @@ class CryptoMonitor:
|
|
| 1457 |
|
| 1458 |
# 多交易所监控器
|
| 1459 |
self.cross_ex = CrossExchangeMonitor(self.api, self.bitget, self.okx, self.log)
|
| 1460 |
-
|
| 1461 |
-
self.
|
| 1462 |
|
| 1463 |
self.evaluators = {}
|
| 1464 |
self._rebuild_evaluators()
|
|
@@ -1607,17 +1640,26 @@ class CryptoMonitor:
|
|
| 1607 |
except Exception as e:
|
| 1608 |
self.log(f"❌ {name} 评估异常: {e}")
|
| 1609 |
|
| 1610 |
-
# 多交易所监控(跨所差价 /
|
| 1611 |
ex_cfg = cfg.get("exchanges", {})
|
| 1612 |
-
|
| 1613 |
-
(
|
| 1614 |
-
|
| 1615 |
-
|
| 1616 |
-
|
| 1617 |
-
|
| 1618 |
-
|
| 1619 |
-
|
| 1620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1621 |
|
| 1622 |
if total_alerts:
|
| 1623 |
self.alert_mgr.emit(total_alerts)
|
|
@@ -1769,44 +1811,45 @@ def render_config_editor(monitor):
|
|
| 1769 |
if parsed:
|
| 1770 |
sp["pairs"] = parsed
|
| 1771 |
|
| 1772 |
-
st.markdown("**
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
|
| 1779 |
-
"
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
-
"
|
| 1784 |
-
|
| 1785 |
-
|
| 1786 |
-
|
| 1787 |
-
|
| 1788 |
-
|
| 1789 |
-
|
| 1790 |
-
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
|
|
|
| 1810 |
|
| 1811 |
coin_names = list(cfg["coins"].keys())
|
| 1812 |
tabs = st.tabs(coin_names + ["➕ 新增币种"])
|
|
|
|
| 171 |
"auto_calibrate_hours": 24,
|
| 172 |
"last_calibrate_ts": 0,
|
| 173 |
},
|
| 174 |
+
# 多交易所扩展:跨所差价、OKX 合约(GRAM / STRC)
|
| 175 |
"exchanges": {
|
| 176 |
"enable_bitget": True,
|
| 177 |
"enable_okx": True,
|
|
|
|
| 183 |
# Binance 仍叫 TON,Bitget/OKX 已改名 GRAM
|
| 184 |
{"name": "BTC", "binance": "BTCUSDT", "bitget": "BTCUSDT", "okx": "BTC-USDT"},
|
| 185 |
{"name": "TON/GRAM", "binance": "TONUSDT", "bitget": "GRAMUSDT", "okx": "GRAM-USDT"},
|
| 186 |
+
# STRC:Bitget 现货 rToken 与 OKX 永续做价差对比
|
| 187 |
+
{"name": "STRC", "binance": "", "bitget": "RSTRCUSDT", "okx": "STRC-USDT-SWAP"},
|
| 188 |
],
|
| 189 |
},
|
| 190 |
+
# OKX 永续合约关键信息(STRC 即普通合约 + 休市属性,数据主源走 OKX)
|
| 191 |
+
"okx_contracts": [
|
| 192 |
+
{
|
| 193 |
+
"name": "GRAM", "inst_id": "GRAM-USDT-SWAP", "enable": True,
|
| 194 |
+
"price_change_pct": 2.0, # 合约价短时异动 %
|
| 195 |
+
"funding_high": 0.05, # 资金费率绝对值高位 %
|
| 196 |
+
"funding_neg": -0.05,
|
| 197 |
+
"oi_change_pct": 5.0, # 持仓量较上次变动 %
|
| 198 |
+
"amp_24h_pct": 0.0, # 0=不监控 24h 振幅
|
| 199 |
+
"market_close_aware": False, # 普通合约 7x24 交易
|
| 200 |
+
"static_secs": 600,
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"name": "STRC", "inst_id": "STRC-USDT-SWAP", "enable": True,
|
| 204 |
+
"price_change_pct": 1.0,
|
| 205 |
+
"funding_high": 0.05,
|
| 206 |
+
"funding_neg": -0.05,
|
| 207 |
+
"oi_change_pct": 5.0,
|
| 208 |
+
"amp_24h_pct": 3.0,
|
| 209 |
+
"market_close_aware": True, # 股票类合约,休市价格不变时不提醒
|
| 210 |
+
"static_secs": 600, # 多久不变判定为休市
|
| 211 |
+
},
|
| 212 |
+
],
|
| 213 |
},
|
| 214 |
}
|
| 215 |
|
|
|
|
| 238 |
return result
|
| 239 |
|
| 240 |
|
| 241 |
+
def _normalize_exchanges(ex):
|
| 242 |
+
"""兼容旧配置:把旧的 strc / okx_gram_swap 结构迁移到 okx_contracts,
|
| 243 |
+
并确保 spread 含 STRC 对照、清理过时字段。"""
|
| 244 |
+
ex = copy.deepcopy(ex)
|
| 245 |
+
|
| 246 |
+
# 1) spread 对照表确保含 STRC(Bitget 现货 vs OKX 永续)
|
| 247 |
+
sp = ex.setdefault("spread", {})
|
| 248 |
+
pairs = sp.get("pairs") or []
|
| 249 |
+
if "STRC" not in {p.get("name") for p in pairs}:
|
| 250 |
+
pairs.append({"name": "STRC", "binance": "",
|
| 251 |
+
"bitget": "RSTRCUSDT", "okx": "STRC-USDT-SWAP"})
|
| 252 |
+
sp["pairs"] = pairs
|
| 253 |
+
|
| 254 |
+
# 2) okx_contracts 不存在则按默认创建
|
| 255 |
+
contracts = ex.get("okx_contracts")
|
| 256 |
+
if not isinstance(contracts, list) or not contracts:
|
| 257 |
+
contracts = [
|
| 258 |
+
{"name": "GRAM", "inst_id": "GRAM-USDT-SWAP", "enable": True,
|
| 259 |
+
"price_change_pct": 2.0, "funding_high": 0.05, "funding_neg": -0.05,
|
| 260 |
+
"oi_change_pct": 5.0, "amp_24h_pct": 0.0,
|
| 261 |
+
"market_close_aware": False, "static_secs": 600},
|
| 262 |
+
{"name": "STRC", "inst_id": "STRC-USDT-SWAP", "enable": True,
|
| 263 |
+
"price_change_pct": 1.0, "funding_high": 0.05, "funding_neg": -0.05,
|
| 264 |
+
"oi_change_pct": 5.0, "amp_24h_pct": 3.0,
|
| 265 |
+
"market_close_aware": True, "static_secs": 600},
|
| 266 |
+
]
|
| 267 |
+
|
| 268 |
+
by_name = {c.get("name"): c for c in contracts}
|
| 269 |
+
# 旧 okx_gram_swap -> GRAM 合约
|
| 270 |
+
old_gram = ex.get("okx_gram_swap")
|
| 271 |
+
if isinstance(old_gram, dict) and "GRAM" in by_name:
|
| 272 |
+
for k in ("inst_id", "enable", "price_change_pct",
|
| 273 |
+
"funding_high", "funding_neg", "oi_change_pct"):
|
| 274 |
+
if k in old_gram:
|
| 275 |
+
by_name["GRAM"][k] = old_gram[k]
|
| 276 |
+
# 旧 strc -> STRC 合约(change_pct 映射为 price_change_pct)
|
| 277 |
+
old_strc = ex.get("strc")
|
| 278 |
+
if isinstance(old_strc, dict) and "STRC" in by_name:
|
| 279 |
+
if "change_pct" in old_strc:
|
| 280 |
+
by_name["STRC"]["price_change_pct"] = old_strc["change_pct"]
|
| 281 |
+
for k in ("enable", "amp_24h_pct", "static_secs"):
|
| 282 |
+
if k in old_strc:
|
| 283 |
+
by_name["STRC"][k] = old_strc[k]
|
| 284 |
+
|
| 285 |
+
ex["okx_contracts"] = contracts
|
| 286 |
+
ex.pop("okx_gram_swap", None)
|
| 287 |
+
ex.pop("strc", None)
|
| 288 |
+
return ex
|
| 289 |
+
|
| 290 |
+
|
| 291 |
def load_config():
|
| 292 |
"""读取配置文件,并以默认值补全缺失字段"""
|
| 293 |
cfg = copy.deepcopy(DEFAULT_CONFIG)
|
|
|
|
| 307 |
cfg["coins"] = merged_coins
|
| 308 |
# exchanges:跨所/STRC/OKX 合约配置,缺失字段以默认补全
|
| 309 |
cfg["exchanges"] = _deep_merge(cfg["exchanges"], saved.get("exchanges", {}))
|
| 310 |
+
cfg["exchanges"] = _normalize_exchanges(cfg["exchanges"])
|
| 311 |
except Exception as e:
|
| 312 |
print(f"⚠️ 配置读取失败,使用默认值: {e}")
|
| 313 |
return cfg
|
|
|
|
| 1295 |
return None
|
| 1296 |
|
| 1297 |
|
| 1298 |
+
# --- 9b. 多交易所监控(跨所差价 / OKX 合约:GRAM、STRC)---
|
| 1299 |
def make_alert(name, category, key, direction, headline, body):
|
| 1300 |
"""构造与 AlertManager 兼容的告警字典"""
|
| 1301 |
emoji = SIGNAL_EMOJI[direction]
|
|
|
|
| 1365 |
return alerts
|
| 1366 |
|
| 1367 |
|
| 1368 |
+
class OKXContractMonitor:
|
| 1369 |
+
"""OKX 永续合约关键信息:价格异动 / 资金费率 / 持仓量变化。
|
| 1370 |
+
market_close_aware=True 时(如 STRC 股票类合约),价格长时间不变判定为休市,
|
| 1371 |
+
休市期间不推送任何提醒;复市后的首次异动会标注“复市后”。
|
| 1372 |
+
每个 inst 用独立实例维护状态。"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1373 |
|
| 1374 |
def __init__(self, okx_api, logger):
|
| 1375 |
self.okx = okx_api
|
| 1376 |
self.logger = logger
|
| 1377 |
self.last_price = None
|
| 1378 |
self.last_oi = None
|
| 1379 |
+
self.last_change_ts = 0.0
|
| 1380 |
|
| 1381 |
+
def evaluate(self, cfg):
|
| 1382 |
alerts = []
|
| 1383 |
+
if not cfg.get("enable", True):
|
| 1384 |
return alerts
|
| 1385 |
+
inst = cfg.get("inst_id")
|
| 1386 |
+
if not inst:
|
| 1387 |
+
return alerts
|
| 1388 |
+
name = cfg.get("name") or inst
|
| 1389 |
+
label = f"{name}({inst})"
|
| 1390 |
+
now = time.time()
|
| 1391 |
+
market_close = bool(cfg.get("market_close_aware", False))
|
| 1392 |
+
static_secs = float(cfg.get("static_secs", 600))
|
| 1393 |
|
| 1394 |
+
# 1) 合约价 + 休市判定
|
| 1395 |
+
paused_before = False
|
| 1396 |
+
in_recess = False
|
| 1397 |
t = self.okx.ticker(inst)
|
| 1398 |
if t:
|
| 1399 |
price = t["price"]
|
| 1400 |
+
if self.last_price is None:
|
| 1401 |
+
self.last_price = price
|
| 1402 |
+
self.last_change_ts = now
|
| 1403 |
+
elif price != self.last_price:
|
| 1404 |
+
pct = (price - self.last_price) / self.last_price * 100 if self.last_price > 0 else 0.0
|
| 1405 |
+
paused_before = market_close and (now - self.last_change_ts) >= static_secs
|
| 1406 |
+
self.last_price = price
|
| 1407 |
+
self.last_change_ts = now
|
| 1408 |
+
if abs(pct) >= float(cfg.get("price_change_pct", 2.0)):
|
| 1409 |
direction = "涨" if pct > 0 else "跌"
|
| 1410 |
arrow = "📈" if pct > 0 else "📉"
|
| 1411 |
+
prefix = "复市后" if paused_before else ""
|
| 1412 |
alerts.append(make_alert(
|
| 1413 |
+
label, "okx_price", f"okx_{inst}_price", direction,
|
| 1414 |
+
f"{prefix}合约价异动 {arrow} {pct:+.2f}%",
|
| 1415 |
f"现价 {fmt_price(price)} USDT(较上次 {pct:+.2f}%)"))
|
| 1416 |
+
else:
|
| 1417 |
+
# 价格未变:达到静止时长则视为休市
|
| 1418 |
+
in_recess = market_close and (now - self.last_change_ts) >= static_secs
|
| 1419 |
+
|
| 1420 |
+
# 休市期间不再推送振幅/资金/持仓,直接返回
|
| 1421 |
+
if in_recess:
|
| 1422 |
+
return alerts
|
| 1423 |
+
|
| 1424 |
+
# 2) 24h 振幅(amp_24h_pct>0 时启用,主要服务 STRC)
|
| 1425 |
+
amp_thr = float(cfg.get("amp_24h_pct", 0) or 0)
|
| 1426 |
+
if amp_thr > 0 and t["low24"] > 0:
|
| 1427 |
+
amp = (t["high24"] - t["low24"]) / t["low24"] * 100
|
| 1428 |
+
if amp >= amp_thr:
|
| 1429 |
+
alerts.append(make_alert(
|
| 1430 |
+
label, "okx_price", f"okx_{inst}_amp", "关注",
|
| 1431 |
+
f"24h 振幅 {amp:.2f}%",
|
| 1432 |
+
f"24h 区间 {fmt_price(t['low24'])} ~ {fmt_price(t['high24'])}"
|
| 1433 |
+
f",现价 {fmt_price(price)}"))
|
| 1434 |
|
| 1435 |
+
# 3) 资金费率
|
| 1436 |
fr = self.okx.funding_rate(inst)
|
| 1437 |
if fr:
|
| 1438 |
rate = fr["rate_pct"]
|
| 1439 |
+
high = float(cfg.get("funding_high", 0.05))
|
| 1440 |
+
neg = float(cfg.get("funding_neg", -0.05))
|
| 1441 |
if rate >= high:
|
| 1442 |
alerts.append(make_alert(
|
| 1443 |
+
label, "okx_funding", f"okx_{inst}_funding_high", "关注",
|
| 1444 |
f"资金费率偏高 {rate:+.4f}%",
|
| 1445 |
f"多头需付费,资金费率 {rate:+.4f}%(阈值 {high}%),警惕多头拥挤"))
|
| 1446 |
elif rate <= neg:
|
| 1447 |
alerts.append(make_alert(
|
| 1448 |
+
label, "okx_funding", f"okx_{inst}_funding_neg", "关注",
|
| 1449 |
f"资金费率为负 {rate:+.4f}%",
|
| 1450 |
f"空头需付费,资金费率 {rate:+.4f}%(阈值 {neg}%),警惕空头拥挤"))
|
| 1451 |
|
| 1452 |
+
# 4) 持仓量变化
|
| 1453 |
oi = self.okx.open_interest(inst)
|
| 1454 |
if oi:
|
| 1455 |
cur = oi["oi"]
|
| 1456 |
if self.last_oi and self.last_oi > 0:
|
| 1457 |
pct = (cur - self.last_oi) / self.last_oi * 100
|
| 1458 |
+
if abs(pct) >= float(cfg.get("oi_change_pct", 5.0)):
|
| 1459 |
direction = "涨" if pct > 0 else "跌"
|
| 1460 |
word = "增仓" if pct > 0 else "减仓"
|
| 1461 |
alerts.append(make_alert(
|
| 1462 |
+
label, "okx_oi", f"okx_{inst}_oi", direction,
|
| 1463 |
f"持仓量{word} {pct:+.2f}%",
|
| 1464 |
f"持仓量 {fmt_usdt(oi['oi_usd'])}(较上次 {pct:+.2f}%)"))
|
| 1465 |
self.last_oi = cur
|
|
|
|
| 1490 |
|
| 1491 |
# 多交易所监控器
|
| 1492 |
self.cross_ex = CrossExchangeMonitor(self.api, self.bitget, self.okx, self.log)
|
| 1493 |
+
# OKX 合约监控器:按 inst_id 维护独立状态,运行时按需创建
|
| 1494 |
+
self.okx_contract_mons = {}
|
| 1495 |
|
| 1496 |
self.evaluators = {}
|
| 1497 |
self._rebuild_evaluators()
|
|
|
|
| 1640 |
except Exception as e:
|
| 1641 |
self.log(f"❌ {name} 评估异常: {e}")
|
| 1642 |
|
| 1643 |
+
# 多交易所监控(跨所差价 / OKX 合��:GRAM、STRC 等)
|
| 1644 |
ex_cfg = cfg.get("exchanges", {})
|
| 1645 |
+
try:
|
| 1646 |
+
total_alerts.extend(self.cross_ex.evaluate(ex_cfg))
|
| 1647 |
+
except Exception as e:
|
| 1648 |
+
self.log(f"❌ 跨所差价评估异常: {e}")
|
| 1649 |
+
|
| 1650 |
+
if ex_cfg.get("enable_okx", True):
|
| 1651 |
+
for cinfo in ex_cfg.get("okx_contracts", []):
|
| 1652 |
+
inst = cinfo.get("inst_id")
|
| 1653 |
+
if not inst or not cinfo.get("enable", True):
|
| 1654 |
+
continue
|
| 1655 |
+
mon = self.okx_contract_mons.get(inst)
|
| 1656 |
+
if mon is None:
|
| 1657 |
+
mon = OKXContractMonitor(self.okx, self.log)
|
| 1658 |
+
self.okx_contract_mons[inst] = mon
|
| 1659 |
+
try:
|
| 1660 |
+
total_alerts.extend(mon.evaluate(cinfo))
|
| 1661 |
+
except Exception as e:
|
| 1662 |
+
self.log(f"❌ OKX {inst} 评估异常: {e}")
|
| 1663 |
|
| 1664 |
if total_alerts:
|
| 1665 |
self.alert_mgr.emit(total_alerts)
|
|
|
|
| 1811 |
if parsed:
|
| 1812 |
sp["pairs"] = parsed
|
| 1813 |
|
| 1814 |
+
st.markdown("**OKX 永续合约(GRAM、STRC 等;STRC 为股票类合约,多一个休市属性)**")
|
| 1815 |
+
contracts = ex.setdefault("okx_contracts", [])
|
| 1816 |
+
for idx, c in enumerate(contracts):
|
| 1817 |
+
cname = c.get("name") or c.get("inst_id") or f"合约{idx+1}"
|
| 1818 |
+
st.markdown(f"— {cname}")
|
| 1819 |
+
wc1, wc2 = st.columns(2)
|
| 1820 |
+
with wc1:
|
| 1821 |
+
c["enable"] = st.checkbox(
|
| 1822 |
+
"启用监控", value=c.get("enable", True), key=f"okxc_{idx}_en")
|
| 1823 |
+
c["inst_id"] = st.text_input(
|
| 1824 |
+
"合约 instId", value=c.get("inst_id", ""), key=f"okxc_{idx}_inst")
|
| 1825 |
+
c["price_change_pct"] = st.number_input(
|
| 1826 |
+
"合约价短时异动 %", min_value=0.1, max_value=20.0,
|
| 1827 |
+
value=float(c.get("price_change_pct", 2.0)), step=0.1,
|
| 1828 |
+
format="%.1f", key=f"okxc_{idx}_pc")
|
| 1829 |
+
c["oi_change_pct"] = st.number_input(
|
| 1830 |
+
"持仓量变动 %", min_value=0.5, max_value=50.0,
|
| 1831 |
+
value=float(c.get("oi_change_pct", 5.0)), step=0.5,
|
| 1832 |
+
format="%.1f", key=f"okxc_{idx}_oi")
|
| 1833 |
+
with wc2:
|
| 1834 |
+
c["funding_high"] = st.number_input(
|
| 1835 |
+
"资金费率高位 %", min_value=0.001, max_value=1.0,
|
| 1836 |
+
value=float(c.get("funding_high", 0.05)), step=0.001,
|
| 1837 |
+
format="%.3f", key=f"okxc_{idx}_fh")
|
| 1838 |
+
c["funding_neg"] = st.number_input(
|
| 1839 |
+
"资金费率负位 %", min_value=-1.0, max_value=-0.001,
|
| 1840 |
+
value=float(c.get("funding_neg", -0.05)), step=0.001,
|
| 1841 |
+
format="%.3f", key=f"okxc_{idx}_fn")
|
| 1842 |
+
c["amp_24h_pct"] = st.number_input(
|
| 1843 |
+
"24h 振幅阈值 %(0=不监控)", min_value=0.0, max_value=50.0,
|
| 1844 |
+
value=float(c.get("amp_24h_pct", 0.0)), step=0.5,
|
| 1845 |
+
format="%.1f", key=f"okxc_{idx}_amp")
|
| 1846 |
+
c["market_close_aware"] = st.checkbox(
|
| 1847 |
+
"股票类合约(休市价格不变时不提醒)",
|
| 1848 |
+
value=c.get("market_close_aware", False), key=f"okxc_{idx}_mc")
|
| 1849 |
+
if c["market_close_aware"]:
|
| 1850 |
+
c["static_secs"] = st.number_input(
|
| 1851 |
+
"判定休市的静止秒数", min_value=60, max_value=7200,
|
| 1852 |
+
value=int(c.get("static_secs", 600)), step=60, key=f"okxc_{idx}_ss")
|
| 1853 |
|
| 1854 |
coin_names = list(cfg["coins"].keys())
|
| 1855 |
tabs = st.tabs(coin_names + ["➕ 新增币种"])
|