6547029a89
Co-authored-by: Cursor <cursoragent@cursor.com>
33 lines
969 B
Python
33 lines
969 B
Python
"""实例页:持仓未实现盈亏(实时盈亏)汇总。"""
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
from lib.hub.hub_monitor_totals_lib import position_unrealized_pnl
|
|
|
|
|
|
def _position_contracts(pos: dict[str, Any]) -> float:
|
|
try:
|
|
return abs(float(pos.get("contracts") or 0))
|
|
except (TypeError, ValueError):
|
|
return 0.0
|
|
|
|
|
|
def sum_unrealized_pnl_from_positions(positions: list[dict[str, Any]] | None) -> float:
|
|
total = 0.0
|
|
for p in positions or []:
|
|
if not isinstance(p, dict):
|
|
continue
|
|
if _position_contracts(p) <= 1e-12:
|
|
continue
|
|
total += position_unrealized_pnl(p)
|
|
return round(total, 2)
|
|
|
|
|
|
def fetch_unrealized_pnl(fetch_positions_fn: Callable[[], list[dict[str, Any]] | None]) -> float | None:
|
|
try:
|
|
return sum_unrealized_pnl_from_positions(fetch_positions_fn() or [])
|
|
except Exception:
|
|
return None
|