From 813ecdd4ac72c6b5a7856caed341d9cd3577d829 Mon Sep 17 00:00:00 2001 From: dekun Date: Sat, 18 Jul 2026 15:00:55 +0800 Subject: [PATCH] Lighten hub options snapshot by skipping history stats. Monitor board only needs positions, balances, and targets; drop the OKX positions-history pull each poll, and surface timeout errors via msg. Co-authored-by: Cursor --- lib/options/options_hub_lib.py | 14 +++----------- manual_trading_hub/hub.py | 13 ++++++++++++- tests/test_options_hub_lib.py | 15 +++++++++++---- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/options/options_hub_lib.py b/lib/options/options_hub_lib.py index 7854bb0..48227a7 100644 --- a/lib/options/options_hub_lib.py +++ b/lib/options/options_hub_lib.py @@ -1,17 +1,9 @@ -"""中控只读聚合:OKX 期权持仓 / 资金 / 本地统计.""" +"""中控只读聚合:OKX 期权持仓 / 资金(轻量,不含历史统计).""" from __future__ import annotations from typing import Any -from lib.options.options_history_lib import load_options_history -from lib.options.options_stats_lib import compute_options_stats_from_history - - -def _compute_options_stats(ex, cfg) -> dict[str, Any]: - history = load_options_history(ex, cfg) - return compute_options_stats_from_history(history) - def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]: if not cfg.get("enabled"): @@ -83,7 +75,6 @@ def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]: has_upl = True upl_total += float(net) bal = cfg["fetch_options_balances"](ex) - stats = _compute_options_stats(ex, cfg) return { "ok": True, "enabled": True, @@ -96,7 +87,8 @@ def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]: "funding_usdt": bal.get("funding_usdt"), "trading_usdc": bal.get("trading_usdc"), "trading_usdt": bal.get("trading_usdt"), - "stats": stats, + # 监控区不用历史统计;保留空对象兼容旧调用方 + "stats": {}, "trade_budget": cfg.get("trade_budget"), "account_label": cfg.get("account_label") or "OKX期权", } diff --git a/manual_trading_hub/hub.py b/manual_trading_hub/hub.py index 1c885a1..c2fc0a3 100644 --- a/manual_trading_hub/hub.py +++ b/manual_trading_hub/hub.py @@ -2462,6 +2462,17 @@ def _day_stats_from_trades_body(body: dict | None) -> dict: } +def _normalize_options_snap(options_snap: dict | None) -> dict | None: + """超时等失败常只有 error 无 msg;前端只认 msg,补齐以免落成「期权数据不可用」.""" + if not isinstance(options_snap, dict): + return options_snap + if options_snap.get("ok") is False and not (options_snap.get("msg") or "").strip(): + err = options_snap.get("error") + if err is not None and str(err).strip(): + return {**options_snap, "msg": str(err).strip()} + return options_snap + + async def _assemble_board_row( client: httpx.AsyncClient, ex: dict, agent_row: dict, *, trading_day: str ) -> dict: @@ -2495,7 +2506,7 @@ async def _assemble_board_row( "account_ok": acct_ok, "day_stats": _day_stats_from_trades_body(trades_today), "force_close": snap.get("force_close") if isinstance(snap, dict) else None, - "options": options_snap, + "options": _normalize_options_snap(options_snap if isinstance(options_snap, dict) else None), } diff --git a/tests/test_options_hub_lib.py b/tests/test_options_hub_lib.py index 7c3fb15..5c51671 100644 --- a/tests/test_options_hub_lib.py +++ b/tests/test_options_hub_lib.py @@ -10,9 +10,9 @@ class OptionsHubLibTests(TestCase): self.assertFalse(out["enabled"]) self.assertTrue(out["ok"]) - @patch("lib.options.options_hub_lib._compute_options_stats", return_value={}) + @patch("lib.options.options_history_lib.load_options_history") @patch("lib.options.options_positions_lib.build_display_option_positions") - def test_build_options_hub_snapshot_positions(self, mock_positions, _mock_stats): + def test_build_options_hub_snapshot_positions(self, mock_positions, mock_history): mock_positions.return_value = [ { "inst_id": "ETH-USD_UM-260703-1800-C", @@ -38,10 +38,17 @@ class OptionsHubLibTests(TestCase): "account_label": "OKX期权", } with patch("lib.options.options_target_lib.list_active_targets", return_value=[]): - with patch("lib.options.options_target_lib.targets_by_inst", return_value={}): - out = build_options_hub_snapshot(cfg) + with patch("lib.options.options_target_lib.list_closing_targets", return_value=[]): + with patch("lib.options.options_target_lib.targets_by_inst", return_value={}): + with patch( + "lib.hedge_plan.hedge_plan_db.active_options_targets_by_inst", + return_value={}, + ): + out = build_options_hub_snapshot(cfg) self.assertTrue(out["ok"], out.get("msg")) self.assertEqual(out["position_count"], 1) self.assertEqual(out["upl_total_usdc"], 1.5) self.assertEqual(out["trading_usdc"], 9.5) self.assertEqual(out.get("target_monitors"), []) + self.assertEqual(out.get("stats"), {}) + mock_history.assert_not_called()