Fix trade log equity_after to chain from initial capital by close time.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 21:41:37 +08:00
parent 8d2d09396b
commit 0b924fca87
4 changed files with 74 additions and 13 deletions
+49 -10
View File
@@ -32,6 +32,42 @@ def calc_equity_after(capital: float, pnl_net: float) -> float | None:
return round(cap + float(pnl_net or 0), 2)
def _read_initial_capital(conn, initial_capital: float | None = None) -> float:
if initial_capital is not None and initial_capital > 0:
return float(initial_capital)
try:
row = conn.execute("SELECT value FROM settings WHERE key='live_capital'").fetchone()
return float(row[0] or 0) if row else 0.0
except (TypeError, ValueError):
return 0.0
def refresh_trade_log_equity_chain(
conn,
initial_capital: float | None = None,
) -> int:
"""按平仓时间顺序重算 trade_logs.equity_after(起始=参考资金 live_capital)。"""
base = _read_initial_capital(conn, initial_capital)
rows = [
dict(r)
for r in conn.execute(
"SELECT id, close_time, pnl_net FROM trade_logs ORDER BY close_time ASC, id ASC"
).fetchall()
]
running = float(base or 0)
updated = 0
for row in rows:
if running <= 0:
break
running = round(running + float(row.get("pnl_net") or 0), 2)
conn.execute(
"UPDATE trade_logs SET equity_after=? WHERE id=?",
(running, int(row["id"])),
)
updated += 1
return updated
def _norm_symbol(symbol: str) -> str:
return (symbol or "").split(".")[0].strip().lower()
@@ -105,23 +141,21 @@ def enrich_trades_for_records(
)
running = float(initial_capital or 0)
curve: list[dict[str, Any]] = []
equity_by_id: dict[int, float | None] = {}
for t in chrono:
_attach_symbol_meta(t)
pnl_net = float(t.get("pnl_net") or 0)
eq = t.get("equity_after")
if eq is None:
if running > 0:
eq = round(running + pnl_net, 2)
else:
eq = None
t["equity_after"] = eq
if eq is not None:
running = float(eq)
if running > 0:
running = round(running + pnl_net, 2)
eq: float | None = running
else:
eq = None
equity_by_id[int(t.get("id") or 0)] = eq
cap_before = float(eq or 0) - pnl_net if eq is not None else 0.0
if t.get("margin_pct") is None:
margin = float(t.get("margin") or 0)
cap_before = float(eq or 0) - pnl_net if eq is not None else 0.0
if margin > 0 and cap_before > 0:
t["margin_pct"] = round(margin / cap_before * 100, 2)
@@ -132,4 +166,9 @@ def enrich_trades_for_records(
"id": int(t.get("id") or 0),
})
for t in rows:
tid = int(t.get("id") or 0)
if tid in equity_by_id:
t["equity_after"] = equity_by_id[tid]
return rows, curve