Harden residual mid-close: IOC partial fills, exchange reconcile, atomic book.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-02 14:41:00 +08:00
parent 640ecc9530
commit 469e7a258a
8 changed files with 729 additions and 238 deletions
+12 -8
View File
@@ -28,10 +28,12 @@ class Ledger:
group_id: str | None = None,
note: str = "",
allow_negative: bool = False,
commit: bool = True,
) -> float:
"""amount>0 入账;amount<0 出账。返回余额。
LIVE 实盘成交后本地账本仅作镜像,须 allow_negative=True,避免「交易所已成交、本地拒记」导致卡仓。
commit=False:由调用方持锁并统一提交(与持仓/残留状态同事务)。
"""
now = int(time.time() * 1000)
with self.db._lock:
@@ -49,15 +51,17 @@ class Ledger:
"INSERT INTO ledger_entries(group_id, kind, amount, balance_after, note, ts_ms) VALUES (?,?,?,?,?,?)",
(group_id, kind, float(amount), equity, note, now),
)
self.db._conn.commit()
try:
from ..config import get_settings
from .funds_wallets import SimFundsWallets
if commit:
self.db._conn.commit()
if commit:
try:
from ..config import get_settings
from .funds_wallets import SimFundsWallets
if get_settings().is_sim:
SimFundsWallets(self.db).mirror_cash(float(amount), kind=kind)
except Exception:
pass
if get_settings().is_sim:
SimFundsWallets(self.db).mirror_cash(float(amount), kind=kind)
except Exception:
pass
return equity
def reset_equity(self, amount: float, *, note: str = "重置模拟资金") -> float: