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 <cursoragent@cursor.com>
This commit is contained in:
@@ -1,17 +1,9 @@
|
|||||||
"""中控只读聚合:OKX 期权持仓 / 资金 / 本地统计."""
|
"""中控只读聚合:OKX 期权持仓 / 资金(轻量,不含历史统计)."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
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]:
|
def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]:
|
||||||
if not cfg.get("enabled"):
|
if not cfg.get("enabled"):
|
||||||
@@ -83,7 +75,6 @@ def build_options_hub_snapshot(cfg: dict[str, Any]) -> dict[str, Any]:
|
|||||||
has_upl = True
|
has_upl = True
|
||||||
upl_total += float(net)
|
upl_total += float(net)
|
||||||
bal = cfg["fetch_options_balances"](ex)
|
bal = cfg["fetch_options_balances"](ex)
|
||||||
stats = _compute_options_stats(ex, cfg)
|
|
||||||
return {
|
return {
|
||||||
"ok": True,
|
"ok": True,
|
||||||
"enabled": 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"),
|
"funding_usdt": bal.get("funding_usdt"),
|
||||||
"trading_usdc": bal.get("trading_usdc"),
|
"trading_usdc": bal.get("trading_usdc"),
|
||||||
"trading_usdt": bal.get("trading_usdt"),
|
"trading_usdt": bal.get("trading_usdt"),
|
||||||
"stats": stats,
|
# 监控区不用历史统计;保留空对象兼容旧调用方
|
||||||
|
"stats": {},
|
||||||
"trade_budget": cfg.get("trade_budget"),
|
"trade_budget": cfg.get("trade_budget"),
|
||||||
"account_label": cfg.get("account_label") or "OKX期权",
|
"account_label": cfg.get("account_label") or "OKX期权",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(
|
async def _assemble_board_row(
|
||||||
client: httpx.AsyncClient, ex: dict, agent_row: dict, *, trading_day: str
|
client: httpx.AsyncClient, ex: dict, agent_row: dict, *, trading_day: str
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -2495,7 +2506,7 @@ async def _assemble_board_row(
|
|||||||
"account_ok": acct_ok,
|
"account_ok": acct_ok,
|
||||||
"day_stats": _day_stats_from_trades_body(trades_today),
|
"day_stats": _day_stats_from_trades_body(trades_today),
|
||||||
"force_close": snap.get("force_close") if isinstance(snap, dict) else None,
|
"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),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ class OptionsHubLibTests(TestCase):
|
|||||||
self.assertFalse(out["enabled"])
|
self.assertFalse(out["enabled"])
|
||||||
self.assertTrue(out["ok"])
|
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")
|
@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 = [
|
mock_positions.return_value = [
|
||||||
{
|
{
|
||||||
"inst_id": "ETH-USD_UM-260703-1800-C",
|
"inst_id": "ETH-USD_UM-260703-1800-C",
|
||||||
@@ -38,10 +38,17 @@ class OptionsHubLibTests(TestCase):
|
|||||||
"account_label": "OKX期权",
|
"account_label": "OKX期权",
|
||||||
}
|
}
|
||||||
with patch("lib.options.options_target_lib.list_active_targets", return_value=[]):
|
with patch("lib.options.options_target_lib.list_active_targets", return_value=[]):
|
||||||
|
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.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)
|
out = build_options_hub_snapshot(cfg)
|
||||||
self.assertTrue(out["ok"], out.get("msg"))
|
self.assertTrue(out["ok"], out.get("msg"))
|
||||||
self.assertEqual(out["position_count"], 1)
|
self.assertEqual(out["position_count"], 1)
|
||||||
self.assertEqual(out["upl_total_usdc"], 1.5)
|
self.assertEqual(out["upl_total_usdc"], 1.5)
|
||||||
self.assertEqual(out["trading_usdc"], 9.5)
|
self.assertEqual(out["trading_usdc"], 9.5)
|
||||||
self.assertEqual(out.get("target_monitors"), [])
|
self.assertEqual(out.get("target_monitors"), [])
|
||||||
|
self.assertEqual(out.get("stats"), {})
|
||||||
|
mock_history.assert_not_called()
|
||||||
|
|||||||
Reference in New Issue
Block a user