fix: 普适指标真实化——trades月份去重/温区总月份(原信号÷3估算使s2_panic虚高100分)
This commit is contained in:
@@ -56,6 +56,8 @@ def create_table(conn):
|
||||
positions_taken INTEGER,
|
||||
sharpe_ratio REAL,
|
||||
profit_factor REAL,
|
||||
universality_months INTEGER,
|
||||
universality_score REAL,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (strategy, market, regime, period_tag)
|
||||
)
|
||||
@@ -158,17 +160,24 @@ def process_period(conn, market, period_tag):
|
||||
cagr = round(((1 + ret / 100) ** (365 / span_days) - 1) * 100, 1)
|
||||
else:
|
||||
cagr = None
|
||||
# 普适:该温区 trades 的 entry_date 去重月份数 / 该温区总月份数(2026-08-16 修复:
|
||||
# 原 server 端信号数÷3估算 → s2_panic 2255信号估算751月=100分,实际只2个月)
|
||||
_uniq_months = len({t.get("entry_date", "")[:7] for t in reg_trades if t.get("entry_date")})
|
||||
_regime_months_all = {d[:7] for d, r in rmap.items() if r == reg}
|
||||
_regime_total_months = len(_regime_months_all)
|
||||
_univ_score = round(min(_uniq_months / max(_regime_total_months, 1) * 100, 100)) if _uniq_months else 0
|
||||
conn.execute(
|
||||
"""INSERT OR REPLACE INTO strategy_regime_perf_by_period
|
||||
(strategy, market, regime, period_tag, trades, win_rate, avg_pnl, avg_hold_days,
|
||||
total_return_pct, cagr_pct, portfolio_max_dd_pct, capital_final,
|
||||
positions_taken, sharpe_ratio, profit_factor, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
positions_taken, sharpe_ratio, profit_factor, universality_months, universality_score, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(v, market, reg, period_tag, len(reg_trades),
|
||||
extra.get("win_rate"), extra.get("avg_pnl"), extra.get("avg_hold_days"),
|
||||
ret, cagr, sim.get("portfolio_max_dd_pct"),
|
||||
sim.get("capital_final"), sim.get("positions_taken"),
|
||||
extra.get("sharpe_ratio"), extra.get("profit_factor"), now))
|
||||
extra.get("sharpe_ratio"), extra.get("profit_factor"),
|
||||
_uniq_months, _univ_score, now))
|
||||
written += 1
|
||||
return written
|
||||
|
||||
|
||||
@@ -60,7 +60,8 @@ def _compute_regime_winrates_cached(pt, _approx_univ):
|
||||
for _r in _c.execute(
|
||||
"SELECT strategy, market, regime, period_tag, trades, win_rate, avg_pnl, "
|
||||
"avg_hold_days, total_return_pct, cagr_pct, portfolio_max_dd_pct, capital_final, "
|
||||
"positions_taken, sharpe_ratio, profit_factor FROM strategy_regime_perf_by_period "
|
||||
"positions_taken, sharpe_ratio, profit_factor, universality_months, universality_score "
|
||||
"FROM strategy_regime_perf_by_period "
|
||||
"WHERE period_tag=? ORDER BY strategy, market, regime",
|
||||
(_pt_use,)).fetchall():
|
||||
_ver, _mkt, _reg = _r[0], _r[1], _r[2]
|
||||
@@ -71,6 +72,8 @@ def _compute_regime_winrates_cached(pt, _approx_univ):
|
||||
_pt_v = _r[12]
|
||||
_sh_v = _r[13]
|
||||
_pf_v = _r[14]
|
||||
_umon = _r[15]
|
||||
_uscore = _r[16]
|
||||
_regime_winrates.setdefault(_ver, {})[_reg] = {
|
||||
"trades": _r[4], "win_rate": _r[5], "avg_pnl": _r[6],
|
||||
"avg_hold_days": _r[7],
|
||||
@@ -83,7 +86,7 @@ def _compute_regime_winrates_cached(pt, _approx_univ):
|
||||
"portfolio_max_dd_pct": _dd_v, "capital_final": _cf_v,
|
||||
"positions_taken": _pt_v, "sharpe_ratio": _sh_v,
|
||||
"profit_factor": _pf_v},
|
||||
"universality": _approx_univ(_ver, _reg, _r[4]),
|
||||
"universality": _approx_univ(_ver, _reg, _r[4], _umon, _uscore),
|
||||
}
|
||||
_c.close()
|
||||
except Exception:
|
||||
@@ -625,8 +628,11 @@ def get_tracking():
|
||||
@app.route("/api/research/strategies")
|
||||
def api_research_strategies():
|
||||
"""策略版本列表(含回测结果摘要,支持 period_tag 区间过滤)"""
|
||||
def _approx_regime_universality(strategy, regime, trades):
|
||||
"""温区级普适近似:温区内信号月份≈trades/温区月均笔数,温区总月份占比(避免逐笔遍历性能问题)"""
|
||||
def _approx_regime_universality(strategy, regime, trades, umon=None, uscore=None):
|
||||
"""温区级普适:优先用预计算真实值(trades entry_date 去重月份/温区总月份),
|
||||
缺失时退回旧近似(信号数÷3估算,2026-08-16 修复——原估算对集中信号虚高)"""
|
||||
if umon is not None and uscore is not None:
|
||||
return {"months": umon, "score": uscore, "regime_total_months": 0}
|
||||
try:
|
||||
import sqlite3 as _sq6
|
||||
_c6 = _sq6.connect(str(DATA_DIR / "mofin.db"), timeout=10)
|
||||
|
||||
Reference in New Issue
Block a user