ba629ea0ee
Add HUB_FUND_HISTORY_START_DAY so curves and drawdown exclude snapshots before the baseline trading day. Co-authored-by: Cursor <cursoragent@cursor.com>
116 lines
3.2 KiB
Python
116 lines
3.2 KiB
Python
"""hub_fund_history_lib:总资金、回撤与日快照。"""
|
|
from __future__ import annotations
|
|
|
|
from hub_fund_history_lib import (
|
|
account_total_usdt,
|
|
build_fund_overview,
|
|
compute_drawdown,
|
|
get_fund_history,
|
|
record_fund_snapshot,
|
|
)
|
|
|
|
|
|
def test_account_total_requires_both_sides():
|
|
assert account_total_usdt(10, 20) == 30.0
|
|
assert account_total_usdt(10, None) is None
|
|
assert account_total_usdt(None, 5) is None
|
|
|
|
|
|
def test_compute_drawdown():
|
|
dd = compute_drawdown([100, 120, 90, 110])
|
|
assert dd["peak_usdt"] == 120.0
|
|
assert dd["max_drawdown_u"] == 30.0
|
|
assert dd["max_drawdown_pct"] == 25.0
|
|
|
|
|
|
def test_build_fund_overview_skips_unmonitored(tmp_path, monkeypatch):
|
|
hist_path = tmp_path / "hub_fund_history.json"
|
|
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_PATH", hist_path)
|
|
record_fund_snapshot(
|
|
"2026-06-01",
|
|
[
|
|
{
|
|
"key": "binance",
|
|
"name": "Binance",
|
|
"funding_usdt": 10,
|
|
"trading_usdt": 20,
|
|
"monitored": True,
|
|
}
|
|
],
|
|
keep_days=180,
|
|
)
|
|
record_fund_snapshot(
|
|
"2026-06-02",
|
|
[
|
|
{
|
|
"key": "binance",
|
|
"name": "Binance",
|
|
"funding_usdt": 12,
|
|
"trading_usdt": 18,
|
|
"monitored": True,
|
|
}
|
|
],
|
|
keep_days=180,
|
|
)
|
|
exchanges = [
|
|
{"id": "0", "key": "binance", "name": "Binance", "enabled": True},
|
|
{"id": "3", "key": "gate_bot", "name": "Gate Bot", "enabled": False},
|
|
]
|
|
board_rows = [
|
|
{
|
|
"key": "binance",
|
|
"name": "Binance",
|
|
"account_ok": True,
|
|
"funding_usdt": 15,
|
|
"trading_usdt": 25,
|
|
}
|
|
]
|
|
out = build_fund_overview(
|
|
exchanges,
|
|
board_rows=board_rows,
|
|
trading_day="2026-06-02",
|
|
keep_days=180,
|
|
)
|
|
assert out["totals"]["total_usdt"] == 40.0
|
|
assert out["totals"]["monitored_count"] == 1
|
|
assert len(out["accounts"]) == 2
|
|
off = next(a for a in out["accounts"] if a["key"] == "gate_bot")
|
|
assert off["monitored"] is False
|
|
assert off["total_usdt"] is None
|
|
assert out["totals"]["drawdown"]["max_drawdown_u"] == 0.0
|
|
|
|
|
|
def test_history_start_day_filters_older(tmp_path, monkeypatch):
|
|
hist_path = tmp_path / "hub_fund_history.json"
|
|
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_PATH", hist_path)
|
|
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_START_DAY", "2026-06-09")
|
|
record_fund_snapshot(
|
|
"2026-06-01",
|
|
[
|
|
{
|
|
"key": "binance",
|
|
"name": "Binance",
|
|
"funding_usdt": 1,
|
|
"trading_usdt": 1,
|
|
"monitored": True,
|
|
}
|
|
],
|
|
keep_days=180,
|
|
)
|
|
record_fund_snapshot(
|
|
"2026-06-09",
|
|
[
|
|
{
|
|
"key": "binance",
|
|
"name": "Binance",
|
|
"funding_usdt": 10,
|
|
"trading_usdt": 20,
|
|
"monitored": True,
|
|
}
|
|
],
|
|
keep_days=180,
|
|
)
|
|
hist = get_fund_history(anchor_day="2026-06-10", keep_days=180)
|
|
assert "2026-06-01" not in hist
|
|
assert "2026-06-09" in hist
|