feat(paper): C-S2 分策略归因(/strategies 聚合成交/拒单/费用)

This commit is contained in:
2026-07-07 12:07:37 +08:00
parent 041dca59e2
commit 14088eac13
3 changed files with 58 additions and 0 deletions
+31
View File
@@ -59,3 +59,34 @@ def test_get_paper_404(tmp_path):
def test_unauthorized_401(tmp_path):
c, _ = _client(tmp_path)
assert c.get("/api/v1/paper/1").status_code == 401
def test_strategy_summary_aggregation(tmp_path):
"""C-S2 归因:分策略成交/拒单/费用聚合(spec §7)。"""
from sanguo_trader.persistence import init_db, save_account, save_trade
c, token = _client(tmp_path)
db = os.path.join(str(tmp_path), "p.db")
aid = c.post(
"/api/v1/paper/create",
json={"symbols": ["600000"],
"strategies": [{"name": "S", "symbol": "600000"}],
"start": "2024-01-01", "end": "2024-06-30"},
headers=_auth(token),
).json()["account_id"]
# 造假 2 笔成交 + 1 拒单(s1),1 成交(s2)
save_trade(db, aid, {"strategy_id": "s1", "symbol": "600000", "price": 10,
"volume": 100, "commission": 5, "transfer_fee": 0.02})
save_trade(db, aid, {"strategy_id": "s1", "symbol": "600000", "price": 11,
"volume": 100, "commission": 5, "stamp_duty": 0.55,
"transfer_fee": 0.02})
save_trade(db, aid, {"strategy_id": "s1", "symbol": "300750"},
rejected=True, reject_reason="limit_up_locked")
save_trade(db, aid, {"strategy_id": "s2", "symbol": "000001", "price": 15,
"volume": 100, "commission": 5})
resp = c.get(f"/api/v1/paper/{aid}/strategies", headers=_auth(token))
assert resp.status_code == 200
summary = {s["strategy_id"]: s for s in resp.json()}
assert summary["s1"]["filled"] == 2
assert summary["s1"]["rejected"] == 1
assert summary["s2"]["filled"] == 1