Clear trade history when resetting sim equity.

Deleting groups/fills/residuals and resetting strategy counters keeps funds reset a clean slate.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-07 14:49:51 +08:00
parent 0f552eb50e
commit 930e6c26c5
3 changed files with 95 additions and 3 deletions
+31 -1
View File
@@ -64,12 +64,42 @@ class Ledger:
pass
return equity
def clear_trade_history(self) -> None:
"""清空交易记录与持仓痕迹(组/成交/残留/账本流水),仓位置 flat。"""
now = int(time.time() * 1000)
with self.db._lock:
self.db._conn.execute("DELETE FROM fills")
self.db._conn.execute("DELETE FROM residual_options")
self.db._conn.execute("DELETE FROM groups")
self.db._conn.execute("DELETE FROM ledger_entries")
self.db._conn.execute(
"""UPDATE positions SET
group_id=NULL, perp_side=NULL, perp_qty_eth=0, perp_entry_px=NULL,
option_inst_id=NULL, option_side=NULL, option_qty_eth=0,
option_qty_contracts=0, option_entry_px=NULL, entry_index_px=NULL,
initial_premium=0, exit_target_usdt=NULL, status='flat'
WHERE id=1"""
)
self.db._conn.execute(
"""UPDATE strategy_state SET
rounds_done=0, window_key=NULL, rest_until_ms=NULL,
last_error=NULL, phase=CASE WHEN running=1 THEN phase ELSE 'idle' END,
updated_at_ms=?
WHERE id=1""",
(now,),
)
self.db._conn.execute(
"DELETE FROM settings WHERE key=?", ("risk_last_k",)
)
self.db._conn.commit()
def reset_equity(self, amount: float, *, note: str = "重置模拟资金") -> float:
"""将权益与可用资金重置为 amount(reserved 清零)。须在无持仓时调用。"""
"""将权益与可用资金重置为 amount(reserved 清零),并清空交易记录。须在无持仓时调用。"""
now = int(time.time() * 1000)
amt = float(amount)
if amt < 0:
raise ValueError("模拟资金不能为负")
self.clear_trade_history()
with self.db._lock:
self.db._conn.execute(
"UPDATE ledger_meta SET equity=?, available=?, reserved=0, updated_at_ms=? WHERE id=1",