Use lightweight ticker APIs for liquidity rank to cut memory.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,6 +13,7 @@ from hub_trades_lib import trading_day_from_dt
|
||||
|
||||
TOP_N_DEFAULT = 20
|
||||
CACHE_VERSION = 3
|
||||
LIQUIDITY_RANK_CACHE_VERSION = 1
|
||||
|
||||
|
||||
def volume_rank_reset_hour() -> int:
|
||||
@@ -372,6 +373,62 @@ def _collect_scores(exchange, exchange_id: str) -> list[tuple[str, str, float]]:
|
||||
return _scores_from_markets(exchange, tickers or {}, ex_id)
|
||||
|
||||
|
||||
def build_usdt_swap_volume_ranks(
|
||||
exchange,
|
||||
ensure_markets_loaded: Callable[[], None],
|
||||
*,
|
||||
exchange_id: str | None = None,
|
||||
) -> tuple[dict[str, int], int]:
|
||||
"""
|
||||
全市场 USDT 永续 24h 成交额排名(base -> rank)。
|
||||
优先各所轻量 ticker API,避免 fetch_tickers() 拉全市场(Gate/Binance 内存优化)。
|
||||
"""
|
||||
ensure_markets_loaded()
|
||||
ex_id = str(exchange_id or getattr(exchange, "id", "") or "").lower()
|
||||
scored = _collect_scores(exchange, ex_id)
|
||||
ranks: dict[str, int] = {}
|
||||
for idx, (_sym, base, _qv) in enumerate(scored, 1):
|
||||
if base and base not in ranks:
|
||||
ranks[base] = idx
|
||||
return ranks, len(scored)
|
||||
|
||||
|
||||
def resolve_daily_volume_rank(
|
||||
target_base: str,
|
||||
cache: dict[str, Any],
|
||||
*,
|
||||
now_ts: float,
|
||||
ttl_sec: float,
|
||||
exchange,
|
||||
ensure_markets_loaded: Callable[[], None],
|
||||
exchange_id: str | None = None,
|
||||
cache_version: int = LIQUIDITY_RANK_CACHE_VERSION,
|
||||
) -> tuple[int | None, int]:
|
||||
"""关键位门控:按 base 查 24h 成交额全市场排名;cache 带 TTL。"""
|
||||
cached_ok = (
|
||||
cache.get("version") == cache_version
|
||||
and cache.get("updated_at")
|
||||
and now_ts - float(cache["updated_at"]) < ttl_sec
|
||||
)
|
||||
if not cached_ok:
|
||||
try:
|
||||
ranks, total = build_usdt_swap_volume_ranks(
|
||||
exchange,
|
||||
ensure_markets_loaded,
|
||||
exchange_id=exchange_id,
|
||||
)
|
||||
cache["ranks"] = ranks
|
||||
cache["total"] = total
|
||||
cache["version"] = cache_version
|
||||
cache["updated_at"] = now_ts
|
||||
except Exception:
|
||||
pass
|
||||
ranks = cache.get("ranks") or {}
|
||||
total = int(cache.get("total") or 0)
|
||||
base = str(target_base or "").strip().upper()
|
||||
return ranks.get(base), total
|
||||
|
||||
|
||||
def fetch_usdt_swap_volume_rank(
|
||||
exchange,
|
||||
ensure_markets_loaded: Callable[[], None],
|
||||
|
||||
Reference in New Issue
Block a user