Use all-time closed groups for funds bar trade stats.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""顶栏资金摘要:总交易/胜率/盈亏比与交易日同口径。"""
|
||||
"""顶栏资金摘要:总交易/胜率/盈亏比按历史全部已平组。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -42,58 +42,18 @@ def _insert_closed(db: Database, *, group_id: str, pnl: float) -> None:
|
||||
db._conn.commit()
|
||||
|
||||
|
||||
def test_day_stats_not_mixed_with_history(tmp_path, monkeypatch) -> None:
|
||||
def test_all_time_stats_consistent(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setenv("MODE", "SIM")
|
||||
db = Database(tmp_path / "funds_sum.db")
|
||||
set_db(db)
|
||||
try:
|
||||
# 历史 3 胜 1 负 → 75%;当日仅 1 笔亏损
|
||||
# 历史 3 胜 1 负 → 75%;另有当日 1 笔亏损 → 合计 5 笔、胜率 60%
|
||||
_insert_closed(db, group_id="G-20260101-01", pnl=10.0)
|
||||
_insert_closed(db, group_id="G-20260101-02", pnl=20.0)
|
||||
_insert_closed(db, group_id="G-20260101-03", pnl=5.0)
|
||||
_insert_closed(db, group_id="G-20260101-04", pnl=-10.0)
|
||||
_insert_closed(db, group_id="G-20260802-01", pnl=-8.0)
|
||||
|
||||
class _FakeDT:
|
||||
@staticmethod
|
||||
def now(tz=None):
|
||||
from datetime import timezone
|
||||
|
||||
if tz is timezone.utc:
|
||||
return datetime(2026, 8, 2, 4, 0, tzinfo=timezone.utc)
|
||||
return datetime(2026, 8, 2, 12, 0, tzinfo=tz or ZoneInfo("Asia/Shanghai"))
|
||||
|
||||
monkeypatch.setattr(funds_api, "datetime", _FakeDT)
|
||||
|
||||
eng = SimpleNamespace(
|
||||
state=lambda: {
|
||||
"exchange": "okx",
|
||||
"position": {"status": "flat", "net_pnl": None},
|
||||
}
|
||||
)
|
||||
set_engine(eng) # type: ignore[arg-type]
|
||||
|
||||
body = asyncio.run(funds_api.funds_summary(_user="admin"))
|
||||
assert body["ok"] is True
|
||||
assert body["trading_day"] == "2026-08-02"
|
||||
assert body["total_trades"] == 1
|
||||
assert body["win_rate"] == 0.0
|
||||
assert body["profit_loss_ratio"] is None
|
||||
finally:
|
||||
set_engine(None) # type: ignore[arg-type]
|
||||
set_db(None)
|
||||
db.close()
|
||||
|
||||
|
||||
def test_day_win_rate_matches_day_trades(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setenv("MODE", "SIM")
|
||||
db = Database(tmp_path / "funds_sum2.db")
|
||||
set_db(db)
|
||||
try:
|
||||
_insert_closed(db, group_id="G-20260802-01", pnl=10.0)
|
||||
_insert_closed(db, group_id="G-20260802-02", pnl=20.0)
|
||||
_insert_closed(db, group_id="G-20260802-03", pnl=-5.0)
|
||||
|
||||
class _FakeDT:
|
||||
@staticmethod
|
||||
def now(tz=None):
|
||||
@@ -114,10 +74,12 @@ def test_day_win_rate_matches_day_trades(tmp_path, monkeypatch) -> None:
|
||||
)
|
||||
|
||||
body = asyncio.run(funds_api.funds_summary(_user="admin"))
|
||||
assert body["total_trades"] == 3
|
||||
assert abs(body["win_rate"] - (2 / 3)) < 1e-9
|
||||
assert body["ok"] is True
|
||||
assert body["trading_day"] == "2026-08-02"
|
||||
assert body["total_trades"] == 5
|
||||
assert abs(body["win_rate"] - 0.6) < 1e-9
|
||||
assert body["profit_loss_ratio"] is not None
|
||||
finally:
|
||||
set_engine(None) # type: ignore[arg-type]
|
||||
set_engine(None)
|
||||
set_db(None)
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user