中控快照增加开仓权利金折U及占账户资金比例。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-02 16:13:36 +08:00
parent fcef3b3d37
commit bb6e9e9041
2 changed files with 69 additions and 8 deletions
+49 -4
View File
@@ -51,6 +51,8 @@ def _enrich_hub_protocol(snap: dict[str, Any], *, app_module: Any) -> dict[str,
positions = out.get("positions") if isinstance(out.get("positions"), list) else []
slim_pos = []
premium_total_usdt = 0.0
has_premium = False
for p in positions:
if not isinstance(p, dict):
continue
@@ -86,12 +88,26 @@ def _enrich_hub_protocol(snap: dict[str, Any], *, app_module: Any) -> dict[str,
pnl_usdt = display_pnl_usdt_from_option_row(p)
except Exception:
pnl_usdt = p.get("net_pnl_usdt")
premium_paid = p.get("premium_paid")
premium_usdt = None
try:
from lib.options.options_positions_lib import premium_paid_usdt_from_option_row
premium_usdt = premium_paid_usdt_from_option_row(p)
except Exception:
premium_usdt = None
if premium_usdt is not None:
has_premium = True
premium_total_usdt += float(premium_usdt)
slim_pos.append(
{
"inst_id": p.get("inst_id"),
"opt_type": p.get("opt_type"),
"sheets": p.get("sheets") or p.get("pos"),
"pnl_usdt": pnl_usdt,
"premium_paid": premium_paid,
"premium_paid_usdt": round(float(premium_usdt), 4) if premium_usdt is not None else None,
"premium_ccy": p.get("premium_ccy"),
"mark": p.get("mark") or p.get("mark_px"),
"underlying": p.get("underlying"),
"source_label": p.get("source_label"),
@@ -113,15 +129,44 @@ def _enrich_hub_protocol(snap: dict[str, Any], *, app_module: Any) -> dict[str,
"margin_mode": out.get("options_margin_mode") or "coin",
"underly": out.get("options_underly") or "ETH",
}
funding_usdt = out.get("funding_usdt")
trading_usdt = out.get("trading_usdt")
funding_usdc = out.get("funding_usdc")
trading_usdc = out.get("trading_usdc")
out["funds"] = {
"funding_usdt": out.get("funding_usdt"),
"trading_usdt": out.get("trading_usdt"),
"funding_usdc": out.get("funding_usdc"),
"trading_usdc": out.get("trading_usdc"),
"funding_usdt": funding_usdt,
"trading_usdt": trading_usdt,
"funding_usdc": funding_usdc,
"trading_usdc": trading_usdc,
}
def _f(v: Any) -> float | None:
if v is None or v == "":
return None
try:
return float(v)
except (TypeError, ValueError):
return None
# 账户资金口径:优先 USDT 资+交;都空则回退 USDC(≈U)
equity_parts = [_f(trading_usdt), _f(funding_usdt)]
if all(x is None for x in equity_parts):
equity_parts = [_f(trading_usdc), _f(funding_usdc)]
known = [x for x in equity_parts if x is not None]
equity_usdt = round(sum(known), 4) if known else None
premium_usdt = round(premium_total_usdt, 4) if has_premium else (0.0 if not slim_pos else None)
ratio_pct = None
if premium_usdt is not None and equity_usdt is not None and equity_usdt > 1e-9:
ratio_pct = round(float(premium_usdt) / float(equity_usdt) * 100.0, 2)
out["pnl"] = {
"unrealized_usdt": out.get("upl_total_usdc"),
}
out["premium"] = {
"paid_usdt": premium_usdt,
"equity_usdt": equity_usdt,
"ratio_pct": ratio_pct,
}
out["positions_brief"] = slim_pos
try:
from lib.options.options_time_lib import options_app_now_str