Add win rate and profit-loss ratio to archive stats.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-21 09:13:37 +08:00
parent c05afbbedf
commit c0f3606ecc
5 changed files with 55 additions and 4 deletions
+13 -2
View File
@@ -1275,14 +1275,23 @@ def _empty_pnl_bucket() -> dict[str, Any]:
def _finalize_pnl_bucket(bucket: dict[str, Any]) -> None:
wins = bucket.pop("_wins", [])
losses = bucket.pop("_losses", [])
bucket["win_count"] = len(wins)
open_count = int(bucket.get("open_count") or 0)
win_count = len(wins)
bucket["win_count"] = win_count
bucket["loss_count"] = len(losses)
bucket["avg_win"] = round(sum(wins) / len(wins), 4) if wins else None
bucket["avg_loss"] = round(sum(losses) / len(losses), 4) if losses else None
avg_loss = round(sum(losses) / len(losses), 4) if losses else None
bucket["avg_loss"] = avg_loss
bucket["max_win"] = round(max(wins), 4) if wins else None
bucket["max_loss"] = round(min(losses), 4) if losses else None
bucket["pnl_total"] = round(float(bucket.get("pnl_total") or 0), 4)
bucket["pnl_ex_sick"] = round(float(bucket.get("pnl_ex_sick") or 0), 4)
bucket["win_rate"] = round(win_count / open_count * 100, 1) if open_count else None
avg_win = bucket["avg_win"]
if avg_win is not None and avg_loss is not None and avg_loss != 0:
bucket["profit_loss_ratio"] = round(avg_win / abs(avg_loss), 2)
else:
bucket["profit_loss_ratio"] = None
def _accumulate_trade_stat(bucket: dict[str, Any], *, pnl: float, is_sick: bool) -> None:
@@ -1329,6 +1338,8 @@ def _compute_period_stats(trade_rows: list[dict[str, Any]]) -> dict[str, Any]:
"avg_loss": total_bucket["avg_loss"],
"max_win": total_bucket["max_win"],
"max_loss": total_bucket["max_loss"],
"win_rate": total_bucket["win_rate"],
"profit_loss_ratio": total_bucket["profit_loss_ratio"],
"by_exchange": by_ex,
}