Fix roll position display: live contracts, TP profit, and 2-decimal qty precision.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -23,6 +23,24 @@ def _coerce_float(*values: Any) -> float | None:
|
||||
return None
|
||||
|
||||
|
||||
CONTRACTS_QTY_DECIMALS = 2
|
||||
|
||||
|
||||
def normalize_contracts_qty(qty: Any, *, decimals: int = CONTRACTS_QTY_DECIMALS) -> float:
|
||||
"""张数统一精度(OKX 等线性永续默认两位小数)。"""
|
||||
try:
|
||||
q = float(qty)
|
||||
except (TypeError, ValueError):
|
||||
return 0.0
|
||||
if not math.isfinite(q):
|
||||
return 0.0
|
||||
return round(abs(q), decimals)
|
||||
|
||||
|
||||
def contracts_qty_is_open(qty: Any, *, decimals: int = CONTRACTS_QTY_DECIMALS) -> bool:
|
||||
return normalize_contracts_qty(qty, decimals=decimals) > 0
|
||||
|
||||
|
||||
def position_contracts(p: dict[str, Any]) -> float:
|
||||
info = p.get("info") or {}
|
||||
if not isinstance(info, dict):
|
||||
@@ -33,7 +51,7 @@ def position_contracts(p: dict[str, Any]) -> float:
|
||||
try:
|
||||
v = float(info[k])
|
||||
if v != 0:
|
||||
return abs(v)
|
||||
return normalize_contracts_qty(v)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
raw = p.get("contracts")
|
||||
@@ -41,7 +59,7 @@ def position_contracts(p: dict[str, Any]) -> float:
|
||||
try:
|
||||
v = float(raw)
|
||||
if v != 0:
|
||||
return abs(v)
|
||||
return normalize_contracts_qty(v)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return 0.0
|
||||
|
||||
Reference in New Issue
Block a user