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:
@@ -63,13 +63,18 @@ def infer_initial_position(
|
||||
entry_live = float(entry_live)
|
||||
except (TypeError, ValueError):
|
||||
qty_live = entry_live = 0.0
|
||||
from lib.hub.hub_position_metrics import normalize_contracts_qty
|
||||
|
||||
qty_live = normalize_contracts_qty(qty_live)
|
||||
legs = [
|
||||
lg
|
||||
for lg in filled_legs or []
|
||||
if isinstance(lg, dict) and leg_is_filled(lg) and leg_fill_price(lg) and float(lg.get("amount") or 0) > 0
|
||||
]
|
||||
add_sum = sum(float(lg.get("amount") or 0) for lg in legs)
|
||||
leg_notional = sum(float(lg.get("amount") or 0) * float(leg_fill_price(lg) or 0) for lg in legs)
|
||||
add_sum = sum(normalize_contracts_qty(lg.get("amount") or 0) for lg in legs)
|
||||
leg_notional = sum(
|
||||
normalize_contracts_qty(lg.get("amount") or 0) * float(leg_fill_price(lg) or 0) for lg in legs
|
||||
)
|
||||
q0 = qty_live - add_sum
|
||||
if q0 > 1e-12 and entry_live > 0 and qty_live > 0:
|
||||
e0 = (entry_live * qty_live - leg_notional) / q0
|
||||
@@ -131,8 +136,8 @@ def compute_roll_chain_metrics(
|
||||
return per_leg, group_out
|
||||
qty = float(q0)
|
||||
avg = float(e0)
|
||||
group_out["initial_qty"] = round(qty, 4)
|
||||
group_out["current_qty"] = round(qty, 4)
|
||||
group_out["initial_qty"] = round(qty, 2)
|
||||
group_out["current_qty"] = round(qty, 2)
|
||||
if tp > 0:
|
||||
group_out["avg_entry"] = avg
|
||||
group_out["reward_at_tp_usdt"] = reward_at_tp_usdt(
|
||||
@@ -161,12 +166,12 @@ def compute_roll_chain_metrics(
|
||||
}
|
||||
group_out["avg_entry"] = round(avg, 10)
|
||||
group_out["reward_at_tp_usdt"] = round(reward, 4) if reward is not None else None
|
||||
group_out["current_qty"] = round(qty, 4)
|
||||
group_out["current_qty"] = round(qty, 2)
|
||||
if qty_live is not None:
|
||||
try:
|
||||
live_qty = float(qty_live)
|
||||
if live_qty > 0:
|
||||
group_out["current_qty"] = round(live_qty, 4)
|
||||
group_out["current_qty"] = round(live_qty, 2)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return per_leg, group_out
|
||||
@@ -208,7 +213,9 @@ def _resolve_roll_live(cfg: dict, group: dict, monitor: dict | None) -> tuple[Op
|
||||
return None, None, cs
|
||||
try:
|
||||
pos = get_pos(ex_sym or sym, direction) or {}
|
||||
qty = float(pos.get("contracts") or 0)
|
||||
from lib.hub.hub_position_metrics import normalize_contracts_qty
|
||||
|
||||
qty = normalize_contracts_qty(pos.get("contracts") or 0)
|
||||
entry = float(pos.get("entry_price") or 0)
|
||||
if qty > 0 and entry > 0:
|
||||
return qty, entry, cs
|
||||
|
||||
Reference in New Issue
Block a user