机器对比改为模拟在上实盘在下,收益率按已结算盈亏相对初始资金计算。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 14:44:16 +08:00
parent 05661f3f30
commit 1c68493aa0
4 changed files with 85 additions and 6 deletions
+24 -3
View File
@@ -4,7 +4,11 @@ from __future__ import annotations
from typing import Any
from lib.bridge_client_lib import pull_all_enabled, pull_all_stats
from lib.cash_sample_lib import query_cash_series, record_cash_samples_from_overview
from lib.cash_sample_lib import (
first_cash_by_instance,
query_cash_series,
record_cash_samples_from_overview,
)
def _safe_float(v: Any) -> float | None:
@@ -52,6 +56,10 @@ def build_stats_summary(
pass
stats_rows = pull_all_stats(w)
stats_by_id = {r.get("id"): r for r in stats_rows}
try:
initial_fallback = first_cash_by_instance([s.get("id") for s in snaps])
except Exception:
initial_fallback = {}
machines: list[dict[str, Any]] = []
sum_cash = 0.0
@@ -92,9 +100,13 @@ def build_stats_summary(
mode = ((body.get("instance") or {}) if isinstance(body.get("instance"), dict) else {}).get(
"mode"
)
# 收益率 = 窗口已结算盈亏 / 初始资金(非当前余额)
initial = _safe_float(premium.get("initial_cash_usdt"))
if initial is None or initial <= 0:
initial = _safe_float(initial_fallback.get(iid))
return_pct = None
if online and stats.get("ok") and net is not None and cash is not None and cash > 0:
return_pct = round(float(net) / float(cash) * 100.0, 2)
if online and stats.get("ok") and net is not None and initial is not None and initial > 0:
return_pct = round(float(net) / float(initial) * 100.0, 2)
if online:
online_n += 1
if cash is not None:
@@ -127,6 +139,7 @@ def build_stats_summary(
"premium_usdt": paid,
"ratio_pct": ratio,
"unrealized_usdt": unreal,
"initial_cash_usdt": initial,
"pos_count": len(brief),
"total_closed": closed if online and stats.get("ok") else None,
"win_count": wins if online and stats.get("ok") else None,
@@ -141,6 +154,14 @@ def build_stats_summary(
}
)
def _mode_rank(m: dict[str, Any]) -> tuple[int, str]:
mode_v = str(m.get("mode") or "").strip().lower()
# 上方 sim,下方 live
rank = 0 if mode_v == "sim" else 1 if mode_v == "live" else 2
return (rank, str(m.get("name") or m.get("id") or ""))
machines.sort(key=_mode_rank)
agg_win_rate = round(sum_wins / sum_closed * 100, 2) if sum_closed else 0.0
return {
"ok": True,