refactor: 将共用代码迁入 lib/ 模块化目录
统一 strategy、key_monitor、trade、hub 等共用库到 lib/ 子包,并补充 lib-structure 文档,便于四所与中控维护。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+113
-113
@@ -1,113 +1,113 @@
|
||||
"""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"]) == 1
|
||||
assert all(a["monitored"] for a in out["accounts"])
|
||||
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
|
||||
"""hub_fund_history_lib:总资金、回撤与日快照。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from lib.hub.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"]) == 1
|
||||
assert all(a["monitored"] for a in out["accounts"])
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user