diff --git a/server.py b/server.py index 801639c7..e6b5aac1 100644 --- a/server.py +++ b/server.py @@ -90,6 +90,45 @@ def _compute_regime_winrates_cached(pt, _approx_univ): pass return _regime_winrates +def _benchmark_annual(period_days, market='a'): + """大盘年化基准:market_regime 指数 close,近 period_days 天涨幅年化(2026-08-15 淘汰策略用)""" + try: + import sqlite3 as _sq + from datetime import datetime as _dt, timedelta as _td + _c = _sq.connect(str(DATA_DIR / "mofin.db"), timeout=5) + _cutoff = (_dt.now() - _td(days=period_days)).strftime("%Y-%m-%d") + _last = _c.execute( + "SELECT close FROM market_regime WHERE market=? AND close IS NOT NULL ORDER BY date DESC LIMIT 1", + (market,)).fetchone() + _base = _c.execute( + "SELECT close FROM market_regime WHERE market=? AND close IS NOT NULL AND date>=? ORDER BY date LIMIT 1", + (market, _cutoff)).fetchone() + _c.close() + if not _last or not _base or not _base[0]: + return None + _pct = (_last[0] / _base[0] - 1) * 100 + _yrs = period_days / 365.0 + if _pct <= -100: + return None + return round(((1 + _pct / 100) ** (1 / _yrs) - 1) * 100, 1) + except Exception: + return None + + +_BENCH_2Y = None +_BENCH_10Y = None + + +def _get_benchmarks(): + """取大盘基准(模块级缓存)""" + global _BENCH_2Y, _BENCH_10Y + if _BENCH_2Y is None: + _BENCH_2Y = _benchmark_annual(730) + if _BENCH_10Y is None: + _BENCH_10Y = _benchmark_annual(3650) + return _BENCH_2Y, _BENCH_10Y + + def _chk_http(host, port, path, timeout=3): try: url = f"http://{host}:{port}{path}"