中控汇总:Bridge 增加窗口化期权统计接口。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-03 02:59:19 +08:00
parent 6d5b71eb3d
commit a71042c3e1
2 changed files with 128 additions and 0 deletions
+75
View File
@@ -185,6 +185,81 @@ def compute_options_stats_from_history(history: list[dict[str, Any]]) -> dict[st
}
_STATS_WINDOWS = ("today", "7d", "30d", "all")
def normalize_stats_window(raw: Any) -> str:
w = str(raw or "7d").strip().lower()
if w in ("1d", "day", "今日"):
return "today"
if w in ("7", "week"):
return "7d"
if w in ("30", "month"):
return "30d"
if w in ("0", "全部", "total"):
return "all"
return w if w in _STATS_WINDOWS else "7d"
def stats_window_bounds(window: str, *, now: datetime | None = None) -> tuple[datetime | None, datetime | None]:
"""返回闭区间起止(本地/业务时区近似用本地 now).all → (None, None)."""
w = normalize_stats_window(window)
end = now or datetime.now()
if w == "all":
return None, None
if w == "today":
start = end.replace(hour=0, minute=0, second=0, microsecond=0)
return start, end
days = 7 if w == "7d" else 30
from datetime import timedelta
start = end - timedelta(days=days)
return start, end
def filter_history_for_stats_window(
history: list[dict[str, Any]],
window: str,
*,
now: datetime | None = None,
) -> list[dict[str, Any]]:
"""窗口内已平仓 + 当前持仓(open)."""
start, end = stats_window_bounds(window, now=now)
out: list[dict[str, Any]] = []
for row in history or []:
if row.get("status") == "open":
out.append(row)
continue
if start is None:
out.append(row)
continue
ts = _parse_ts(row.get("closed_at")) or _parse_ts(row.get("created_at"))
if ts is None:
continue
if ts < start:
continue
if end is not None and ts > end:
continue
out.append(row)
return out
def compute_options_stats_for_window(
history: list[dict[str, Any]],
window: str,
*,
now: datetime | None = None,
) -> dict[str, Any]:
w = normalize_stats_window(window)
filtered = filter_history_for_stats_window(history, w, now=now)
stats = compute_options_stats_from_history(filtered)
start, end = stats_window_bounds(w, now=now)
stats["window"] = w
stats["window_start"] = start.strftime("%Y-%m-%d %H:%M:%S") if start else None
stats["window_end"] = end.strftime("%Y-%m-%d %H:%M:%S") if end else None
return stats
def compute_options_stats(get_db) -> dict[str, Any]:
conn = get_db()
try: