4994aaab16
Document and enforce: A full dual-leg close, B perp-only when deep OTM with residual archive that does not block next open, and expiry settlement when target is missed. Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Annotated, Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from ..models.db import get_db
|
|
from ..sim.pnl import summarize_fills_pnl
|
|
from .auth import require_user
|
|
|
|
router = APIRouter(prefix="/api/trades", tags=["trades"])
|
|
|
|
|
|
def _row(r: Any) -> dict:
|
|
return dict(r)
|
|
|
|
|
|
@router.get("/groups")
|
|
async def list_groups(_user: Annotated[str, Depends(require_user)]) -> dict:
|
|
db = get_db()
|
|
rows = db.fetchall("SELECT * FROM groups ORDER BY open_at_ms DESC LIMIT 200")
|
|
groups = []
|
|
for r in rows:
|
|
g = _row(r)
|
|
if g.get("status") == "closed":
|
|
fills = db.fetchall(
|
|
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC",
|
|
(g["group_id"],),
|
|
)
|
|
summary = summarize_fills_pnl(fills)
|
|
g["pnl_summary"] = summary
|
|
if summary.get("net_pnl") is not None:
|
|
g["net_pnl"] = summary["net_pnl"]
|
|
groups.append(g)
|
|
return {"groups": groups}
|
|
|
|
|
|
@router.get("/groups/{group_id}")
|
|
async def group_detail(
|
|
group_id: str, _user: Annotated[str, Depends(require_user)]
|
|
) -> dict:
|
|
db = get_db()
|
|
g = db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
|
if g is None:
|
|
raise HTTPException(status_code=404, detail="group not found")
|
|
fills = db.fetchall(
|
|
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC", (group_id,)
|
|
)
|
|
summary = summarize_fills_pnl(fills)
|
|
return {
|
|
"group": _row(g),
|
|
"fills": [_row(x) for x in fills],
|
|
"pnl_summary": summary,
|
|
}
|