Dedupe duplicate ticket closes so stats match MT5 today PnL.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+25
-12
@@ -782,20 +782,32 @@ def register_routes(app: Flask) -> None:
|
||||
)
|
||||
tid = int(conn.execute("SELECT last_insert_rowid()").fetchone()[0])
|
||||
|
||||
# 同一账户+单号只保留一条「持仓中」,其余重复未平仓直接关掉(不计盈亏)
|
||||
if ticket and close_time in (None, ""):
|
||||
# 同一账户+单号只保留一条有效记录:
|
||||
# - 上报持仓:关掉其它未平仓副本
|
||||
# - 上报平仓:其它同单号副本盈亏清空(防 history 重复插入双计)
|
||||
if ticket:
|
||||
dup_snap = json.dumps(
|
||||
{"event": "dedupe_closed", "kept_id": tid}, ensure_ascii=False
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE trades
|
||||
SET close_time=?, profit=NULL, snapshot_json=?
|
||||
WHERE account_id=? AND ticket=? AND id!=?
|
||||
AND (close_time IS NULL OR close_time='')
|
||||
""",
|
||||
(database.now(), dup_snap, act["account_id"], ticket, tid),
|
||||
)
|
||||
if close_time in (None, ""):
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE trades
|
||||
SET close_time=?, profit=NULL, snapshot_json=?
|
||||
WHERE account_id=? AND ticket=? AND id!=?
|
||||
AND (close_time IS NULL OR close_time='')
|
||||
""",
|
||||
(database.now(), dup_snap, act["account_id"], ticket, tid),
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE trades
|
||||
SET profit=NULL, snapshot_json=?
|
||||
WHERE account_id=? AND ticket=? AND id!=?
|
||||
""",
|
||||
(dup_snap, act["account_id"], ticket, tid),
|
||||
)
|
||||
return jsonify({"ok": True, "id": tid})
|
||||
|
||||
@app.post("/api/v1/trades/reconcile")
|
||||
@@ -932,8 +944,9 @@ def register_routes(app: Flask) -> 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:
|
||||
# 清掉历史假平仓上误写的盈亏
|
||||
# 清掉假平仓盈亏 + 同单号重复平仓双计
|
||||
database.scrub_synthetic_trade_profits(conn)
|
||||
database.scrub_duplicate_ticket_profits(conn)
|
||||
stats = database.trade_stats(
|
||||
conn,
|
||||
account_id=account_id,
|
||||
|
||||
Reference in New Issue
Block a user