Keep unflat legs on 当前持仓 after plan end.

Closed plans with open legs stay visible and closable so residual positions are not orphaned from the UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-14 16:51:33 +08:00
parent 989accdc50
commit a83b6794d8
6 changed files with 92 additions and 14 deletions
+28
View File
@@ -192,6 +192,34 @@ def list_plans(
return [dict(r) for r in rows]
def list_plans_for_live_positions(
conn: sqlite3.Connection,
*,
plan_type: str = "perp_options",
limit: int = 40,
) -> list[dict[str, Any]]:
"""当前持仓用:进行中计划 + 已结束但仍有 open 腿(人工结束未平仓残留)."""
rows = conn.execute(
"""
SELECT DISTINCT p.*
FROM hedge_plans p
LEFT JOIN hedge_plan_legs l ON l.plan_id = p.id
WHERE p.plan_type = ?
AND (
p.status IN ('opening', 'active', 'partial')
OR (
p.status = 'closed'
AND lower(coalesce(l.status, '')) = 'open'
)
)
ORDER BY p.id DESC
LIMIT ?
""",
(str(plan_type), int(limit)),
).fetchall()
return [dict(r) for r in rows]
def get_plan(conn: sqlite3.Connection, plan_id: int) -> Optional[dict[str, Any]]:
row = conn.execute("SELECT * FROM hedge_plans WHERE id=?", (plan_id,)).fetchone()
return dict(row) if row else None