Show trade open/close times and hold period by target exit.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-27 15:37:37 +08:00
parent 0e3a8b08ec
commit 14c6ba51a8
9 changed files with 279 additions and 30 deletions
+22 -23
View File
@@ -7,6 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException
from ..models.db import get_db
from ..sim.pnl import summarize_fills_pnl
from .auth import require_user
from .hold_timing import hold_timing
router = APIRouter(prefix="/api/trades", tags=["trades"])
@@ -15,6 +16,24 @@ def _row(r: Any) -> dict:
return dict(r)
def _enrich_group(g: dict, fills: list) -> dict:
summary = summarize_fills_pnl(fills)
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
if str(g.get("exec_mode") or "").upper() == "LIVE" and g.get("realized_pnl") is not None:
summary = dict(summary)
summary["net_pnl"] = float(g["realized_pnl"])
if g.get("funding_usdt") is not None:
summary["funding_usdt"] = float(g["funding_usdt"])
summary["pnl_source"] = "live_exchange"
g["pnl_summary"] = summary
if summary.get("net_pnl") is not None:
g["net_pnl"] = summary["net_pnl"]
elif g.get("realized_pnl") is not None:
g["net_pnl"] = float(g["realized_pnl"])
g.update(hold_timing(g, fills))
return g
@router.get("/groups")
async def list_groups(_user: Annotated[str, Depends(require_user)]) -> dict:
db = get_db()
@@ -26,20 +45,7 @@ async def list_groups(_user: Annotated[str, Depends(require_user)]) -> dict:
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC",
(g["group_id"],),
)
summary = summarize_fills_pnl(fills)
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
if str(g.get("exec_mode") or "").upper() == "LIVE" and g.get("realized_pnl") is not None:
summary = dict(summary)
summary["net_pnl"] = float(g["realized_pnl"])
if g.get("funding_usdt") is not None:
summary["funding_usdt"] = float(g["funding_usdt"])
summary["pnl_source"] = "live_exchange"
g["pnl_summary"] = summary
if summary.get("net_pnl") is not None:
g["net_pnl"] = summary["net_pnl"]
elif g.get("realized_pnl") is not None:
g["net_pnl"] = float(g["realized_pnl"])
groups.append(g)
groups.append(_enrich_group(g, fills))
return {"groups": groups}
@@ -54,16 +60,9 @@ async def group_detail(
fills = db.fetchall(
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC", (group_id,)
)
summary = summarize_fills_pnl(fills)
gr = _row(g)
if str(gr.get("exec_mode") or "").upper() == "LIVE" and gr.get("realized_pnl") is not None:
summary = dict(summary)
summary["net_pnl"] = float(gr["realized_pnl"])
if gr.get("funding_usdt") is not None:
summary["funding_usdt"] = float(gr["funding_usdt"])
summary["pnl_source"] = "live_exchange"
gr = _enrich_group(_row(g), fills)
return {
"group": gr,
"fills": [_row(x) for x in fills],
"pnl_summary": summary,
"pnl_summary": gr.get("pnl_summary"),
}