Fix stats dirty data: today filter, exclude synthetic closes, scrub frozen float PnL.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-21 19:28:01 +08:00
parent 391851a2b0
commit 1d2d4bbaff
3 changed files with 153 additions and 37 deletions
+20 -8
View File
@@ -782,7 +782,7 @@ def register_routes(app: Flask) -> None:
)
tid = int(conn.execute("SELECT last_insert_rowid()").fetchone()[0])
# 同一账户+单号只保留一条「持仓中」,其余重复未平仓直接关掉
# 同一账户+单号只保留一条「持仓中」,其余重复未平仓直接关掉(不计盈亏)
if ticket and close_time in (None, ""):
dup_snap = json.dumps(
{"event": "dedupe_closed", "kept_id": tid}, ensure_ascii=False
@@ -790,7 +790,7 @@ def register_routes(app: Flask) -> None:
conn.execute(
"""
UPDATE trades
SET close_time=?, snapshot_json=?
SET close_time=?, profit=NULL, snapshot_json=?
WHERE account_id=? AND ticket=? AND id!=?
AND (close_time IS NULL OR close_time='')
""",
@@ -839,16 +839,14 @@ def register_routes(app: Flask) -> None:
snap = {}
snap["event"] = "reconcile_closed"
snap["reconciled_at"] = now_ts
profit = r["profit"]
if profit is None:
profit = 0.0
# 假平仓不写入冻结浮盈,避免脏数据进统计
conn.execute(
"""
UPDATE trades
SET close_time=?, profit=?, snapshot_json=?
SET close_time=?, profit=NULL, snapshot_json=?
WHERE id=?
""",
(now_ts, profit, json.dumps(snap, ensure_ascii=False), r["id"]),
(now_ts, json.dumps(snap, ensure_ascii=False), r["id"]),
)
closed_n += 1
return jsonify({"ok": True, "closed": closed_n})
@@ -927,15 +925,29 @@ def register_routes(app: Flask) -> None:
def records_stats_page():
u = current_user()
machine_raw = (request.args.get("machine_id") or "").strip()
period = (request.args.get("period") or "today").strip().lower()
if period not in ("today", "all"):
period = "today"
day = (request.args.get("day") or "").strip() or None
account_id = None if u["role"] == "admin" else u.get("account_id")
machines, machine_id = _resolve_machine_filter(u, machine_raw)
with database.get_db() as conn:
stats = database.trade_stats(conn, account_id=account_id, machine_id=machine_id)
# 清掉历史假平仓上误写的盈亏
database.scrub_synthetic_trade_profits(conn)
stats = database.trade_stats(
conn,
account_id=account_id,
machine_id=machine_id,
day=day,
period=period,
)
return render_template(
"records_stats.html",
stats=stats,
machines=machines,
selected_machine_id=machine_id,
selected_period=period,
selected_day=stats.get("day") or day or "",
)
@app.get("/records/<int:tid>")