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:
dekun
2026-07-08 17:06:33 +08:00
parent 5ef6affabf
commit e3e53ff7f5
15 changed files with 217 additions and 33 deletions
+28 -9
View File
@@ -205,6 +205,8 @@ def _close_roll_group(conn, cfg: dict, group: dict, *, reason: str = "下单监
def _reconcile_roll_groups(conn, cfg: dict) -> None:
from lib.hub.hub_position_metrics import contracts_qty_is_open, normalize_contracts_qty
rows = conn.execute(
"""SELECT g.*, m.status AS monitor_status
FROM roll_groups g
@@ -217,9 +219,24 @@ def _reconcile_roll_groups(conn, cfg: dict) -> None:
direction = (g.get("direction") or "long").strip().lower()
ex_sym = g.get("exchange_symbol") or cfg["normalize_exchange_symbol"](symbol)
mon_ok = (row["monitor_status"] or "").strip().lower() == "active"
pos = cfg["get_position"](ex_sym, direction)
qty = float(pos.get("contracts") or 0)
if not mon_ok or qty <= 0:
if not mon_ok:
_close_roll_group(conn, cfg, g, reason="下单监控已结案")
continue
pos = None
try:
pos = cfg["get_position"](ex_sym, direction)
except Exception:
pos = None
if pos is None:
continue
qty = normalize_contracts_qty(pos.get("contracts") or 0)
if not contracts_qty_is_open(qty):
try:
pos2 = cfg["get_position"](ex_sym, direction) or {}
qty = normalize_contracts_qty(pos2.get("contracts") or 0)
except Exception:
continue
if not contracts_qty_is_open(qty):
_close_roll_group(conn, cfg, g)
@@ -342,6 +359,8 @@ def _execute_pending_roll_leg(
direction: str,
mark: float,
) -> None:
from lib.hub.hub_position_metrics import contracts_qty_is_open, normalize_contracts_qty
leg_id = int(leg["id"])
gid = int(group["roll_group_id"]) if "roll_group_id" in leg else int(group["id"])
mon_id = group.get("order_monitor_id")
@@ -354,9 +373,9 @@ def _execute_pending_roll_leg(
return
pos = cfg["get_position"](ex_sym, direction) or {}
qty = float(pos.get("contracts") or 0)
qty = normalize_contracts_qty(pos.get("contracts") or 0)
entry = float(pos.get("entry_price") or mon.get("trigger_price") or 0)
if qty <= 0 or entry <= 0:
if not contracts_qty_is_open(qty) or entry <= 0:
_invalidate_roll_leg(conn, cfg, group, leg, mark, reason="无持仓")
return
@@ -431,17 +450,17 @@ def _execute_pending_roll_leg(
"UPDATE roll_groups SET leg_count=?, current_stop_loss=?, updated_at=? WHERE id=?",
(filled + 1, sl, _now(cfg), gid),
)
live_qty = qty + float(amount)
live_qty = normalize_contracts_qty(qty + float(amount))
try:
pos2 = cfg["get_position"](ex_sym, direction) or {}
q2 = float(pos2.get("contracts") or 0)
if q2 > 0:
q2 = normalize_contracts_qty(pos2.get("contracts") or 0)
if contracts_qty_is_open(q2):
live_qty = q2
except Exception:
pass
conn.execute(
"UPDATE order_monitors SET stop_loss=?, order_amount=? WHERE id=? AND status='active'",
(sl, float(live_qty), mon["id"]),
(sl, live_qty, mon["id"]),
)
notify = cfg.get("send_wechat")