Files
crypto_monitor/lib/options/options_hub_lib.py
T
dekun d1ab2d5172 Add options stats holding time metrics and lightweight charts.
Show average hold duration for wins and losses, open positions, and CSS ring/bar visualizations in the stats tab.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-10 11:32:45 +08:00

56 lines
2.0 KiB
Python

"""中控只读聚合:OKX 期权持仓 / 资金 / 本地统计."""
from __future__ import annotations
from typing import Any
from lib.options.options_stats_lib import compute_options_stats
def _compute_options_stats(get_db) -> dict[str, Any]:
return compute_options_stats(get_db)
def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]:
if not cfg.get("enabled"):
return {"ok": True, "enabled": False}
ex = cfg.get("exchange_options")
ready_fn = cfg.get("options_api_ready")
if not callable(ready_fn):
return {"ok": False, "enabled": True, "msg": "期权模块未就绪"}
ok, reason = ready_fn(ex)
if not ok:
return {"ok": False, "enabled": True, "msg": reason or "期权 API 未配置"}
try:
raw = cfg["fetch_option_positions"](ex)
if raw is None:
return {"ok": False, "enabled": True, "msg": "获取期权持仓失败"}
positions = [cfg["format_position_row"](p) for p in raw]
upl_total = 0.0
has_upl = False
for p in positions:
upl = p.get("upl")
if upl is None:
continue
has_upl = True
upl_total += float(upl)
bal = cfg["fetch_options_balances"](ex)
stats = _compute_options_stats(cfg["get_db"])
return {
"ok": True,
"enabled": True,
"positions": positions,
"position_count": len(positions),
"upl_total_usdc": round(upl_total, 4) if has_upl else None,
"balances": bal,
"funding_usdc": bal.get("funding_usdc"),
"funding_usdt": bal.get("funding_usdt"),
"trading_usdc": bal.get("trading_usdc"),
"trading_usdt": bal.get("trading_usdt"),
"stats": stats,
"trade_budget": cfg.get("trade_budget"),
"account_label": cfg.get("account_label") or "OKX期权",
}
except Exception as e:
return {"ok": False, "enabled": True, "msg": str(e)}