From 57ca5ebc8f39f5ba644d1241958ff1f12991719b Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 13 Oct 2026 11:56:47 +0800 Subject: [PATCH] =?UTF-8?q?fix=E6=A8=A1=E6=8B=9F=E7=9B=98=E6=B5=81?= =?UTF-8?q?=E6=B0=B4=E9=87=8D=E7=AE=97VWAP=E6=8C=89=E5=90=88=E7=BA=A6?= =?UTF-8?q?=E5=88=86=E6=AE=B5=EF=BC=8C=E9=81=BF=E5=85=8D=E4=B8=A4=E7=AC=94?= =?UTF-8?q?=E5=8D=96=E5=9B=9E=E6=B7=B7=E7=AE=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- ...epair_options_spot_vwap_from_sim_ledger.py | 95 +++++++++++-------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/scripts/repair_options_spot_vwap_from_sim_ledger.py b/scripts/repair_options_spot_vwap_from_sim_ledger.py index 1bcc8af..0078f1d 100644 --- a/scripts/repair_options_spot_vwap_from_sim_ledger.py +++ b/scripts/repair_options_spot_vwap_from_sim_ledger.py @@ -37,56 +37,72 @@ def _ledger_buy_sell( """在开仓前后窗口内汇总 USDT→币 与 平仓后币→USDT.""" u = (underly or "").upper() inst = str(inst_id or "").strip() - buys = conn.execute( - """ - SELECT amount, ts FROM sim_ledger_entries - WHERE ccy='USDT' AND account='trading' AND kind='convert' - AND amount < 0 AND note LIKE ? - AND ts >= datetime(?, '-14 hour') - AND ts <= datetime(?, '+14 hour') - ORDER BY id - """, - (f"{u}/USDT%", open_ts, open_ts), - ).fetchall() buy_usdt = 0.0 buy_coin = 0.0 - for row in buys: - coin_row = conn.execute( + opens = conn.execute( + """ + SELECT id, ts FROM sim_ledger_entries + WHERE kind='option_open' AND note LIKE ? + ORDER BY id + """, + (f"%{inst}%",), + ).fetchall() if inst else [] + if opens: + open_id = int(opens[0]["id"]) + # 开仓前最近一笔 USDT→币(含缓冲买币) + buy_row = conn.execute( """ - SELECT amount FROM sim_ledger_entries - WHERE ccy=? AND account='trading' AND kind='convert' AND amount > 0 - AND ts=? AND note LIKE 'from usdt%' - ORDER BY id LIMIT 1 - """, - (u, row["ts"]), - ).fetchone() - if not coin_row: - continue - buy_usdt += abs(float(row["amount"])) - buy_coin += float(coin_row["amount"]) - sell_usdt = 0.0 - sell_coin = 0.0 - last_close = None - if inst: - last_close = conn.execute( - """ - SELECT ts FROM sim_ledger_entries - WHERE kind='option_close' AND note LIKE ? + SELECT id, amount, ts FROM sim_ledger_entries + WHERE ccy='USDT' AND account='trading' AND kind='convert' + AND amount < 0 AND note LIKE ? AND id < ? ORDER BY id DESC LIMIT 1 """, - (f"%{inst}%",), + (f"{u}/USDT%", open_id), ).fetchone() - if last_close or close_ts: - close_anchor = str(last_close["ts"]) if last_close else str(close_ts) + if buy_row: + coin_row = conn.execute( + """ + SELECT amount FROM sim_ledger_entries + WHERE ccy=? AND account='trading' AND kind='convert' AND amount > 0 + AND ts=? AND note LIKE 'from usdt%' AND id < ? + ORDER BY id DESC LIMIT 1 + """, + (u, buy_row["ts"], open_id), + ).fetchone() + if coin_row: + buy_usdt = abs(float(buy_row["amount"])) + buy_coin = float(coin_row["amount"]) + sell_usdt = 0.0 + sell_coin = 0.0 + # 该合约 option_close 的 id 区间:本笔最后一笔 close → 下一笔任意 option_open/close 之前 + closes = conn.execute( + """ + SELECT id, ts FROM sim_ledger_entries + WHERE kind='option_close' AND note LIKE ? + ORDER BY id + """, + (f"%{inst}%",), + ).fetchall() if inst else [] + if closes: + start_id = int(closes[-1]["id"]) + nxt = conn.execute( + """ + SELECT id FROM sim_ledger_entries + WHERE id > ? AND kind IN ('option_open','option_close') + ORDER BY id LIMIT 1 + """, + (start_id,), + ).fetchone() + end_id = int(nxt["id"]) if nxt else 10**12 sells = conn.execute( """ - SELECT amount, ts FROM sim_ledger_entries + SELECT id, amount, ts FROM sim_ledger_entries WHERE ccy=? AND account='trading' AND kind='convert' AND amount < 0 AND note LIKE ? - AND ts >= ? + AND id > ? AND id < ? ORDER BY id """, - (u, f"{u}/USDT%", close_anchor), + (u, f"{u}/USDT%", start_id, end_id), ).fetchall() for row in sells: usdt_row = conn.execute( @@ -94,9 +110,10 @@ def _ledger_buy_sell( 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 """, - (row["ts"],), + (row["ts"], start_id, end_id), ).fetchone() if not usdt_row: continue