fix模拟盘流水重算卖回VWAP时同秒多笔配对去重,避免USDT重复计入。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 12:02:04 +08:00
parent 57ca5ebc8f
commit d42e1c7edf
2 changed files with 114 additions and 6 deletions
@@ -104,19 +104,37 @@ def _ledger_buy_sell(
""",
(u, f"{u}/USDT%", start_id, end_id),
).fetchall()
# 同秒多笔卖回时,按「币卖出 id → 下一笔 USDT 入账」配对,避免重复吃到同一笔 USDT
used_usdt_ids: set[int] = set()
for row in sells:
sid = int(row["id"])
usdt_row = conn.execute(
"""
SELECT amount FROM sim_ledger_entries
WHERE ccy='USDT' AND account='trading' AND kind='convert'
AND amount > 0 AND ts=? AND note LIKE 'from %'
AND id > ? AND id < ?
ORDER BY id LIMIT 1
SELECT id, amount FROM sim_ledger_entries
WHERE id = ? AND ccy='USDT' AND account='trading' AND kind='convert'
AND amount > 0 AND note LIKE 'from %'
""",
(row["ts"], start_id, end_id),
(sid + 1,),
).fetchone()
if usdt_row is None or int(usdt_row["id"]) in used_usdt_ids:
cands = conn.execute(
"""
SELECT id, amount FROM sim_ledger_entries
WHERE ccy='USDT' AND account='trading' AND kind='convert'
AND amount > 0 AND note LIKE 'from %'
AND id > ? AND id < ?
ORDER BY id
""",
(sid, end_id),
).fetchall()
usdt_row = next(
(r for r in cands if int(r["id"]) not in used_usdt_ids),
None,
)
if not usdt_row:
continue
uid = int(usdt_row["id"])
used_usdt_ids.add(uid)
sell_coin += abs(float(row["amount"]))
sell_usdt += float(usdt_row["amount"])
return (