Add close price column to trade records for overnight position PnL review.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 21:51:53 +08:00
parent 4552f4ef9c
commit b6b7bfb248
4 changed files with 108 additions and 17 deletions
+36
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 recalc_trade_log_pnl(
*,
symbol: str,
direction: str,
entry_price: float,
close_price: float,
lots: float,
stop_loss: float | None = None,
take_profit: float | None = None,
open_time: str = "",
close_time: str = "",
trading_mode: str = "simulation",
capital: float = 0.0,
) -> dict[str, float]:
"""按开/平仓价重算盈亏与手续费(跨日持仓可手动改价后核对)。"""
from contract_specs import calc_position_metrics
from fee_specs import calc_round_trip_fee
sym = (symbol or "").strip()
direction = (direction or "long").strip().lower()
entry = float(entry_price or close_price or 0)
close_px = float(close_price or 0)
lots_f = float(lots or 0)
sl = float(stop_loss) if stop_loss is not None else entry
tp = float(take_profit) if take_profit is not None else entry
metrics = calc_position_metrics(
direction, entry, sl, tp, lots_f, close_px, capital, sym,
)
pnl = round(float(metrics.get("float_pnl") or 0), 2)
fee = calc_round_trip_fee(
sym, entry, close_px, lots_f, open_time, close_time, trading_mode=trading_mode,
)
pnl_net = round(pnl - fee, 2)
return {"pnl": pnl, "fee": round(fee, 2), "pnl_net": pnl_net}
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)