feat: 温区级综合/普适计算——温区内信号月份分散度+温区总月份占比,温区行综合用温区portfolio算,universality显示温区级月份/温区总月份;说明列fallback

This commit is contained in:
hmo
2026-08-15 09:46:13 +08:00
parent e0bf57c3f0
commit 9ac39a9590
2 changed files with 64 additions and 4 deletions
+59 -1
View File
@@ -574,7 +574,65 @@ def api_research_strategies():
_c.close()
except Exception:
pass
# 2026-08-15 证伪标记(数据驱动):strategy_research.deprecated
# 温区级普适前先建立日期→温区映射(A股 market_regime
_regime_map = {}
try:
import sqlite3 as _sq5
_c5 = _sq5.connect(str(DATA_DIR / "mofin.db"), timeout=10)
for _r in _c5.execute("SELECT date, regime FROM market_regime WHERE market='a'"):
_regime_map[_r[0]] = _r[1]
_c5.close()
except Exception:
pass
# 2026-08-15 温区级普适:策略在适应温区内发出信号的月份分散度 + 温区总月份占比
# 温区总月份数(A股: choppy35.4%/trend_down30.3%/trend_up34.3%;按市场 regime 时长统计)
try:
import sqlite3 as _sq3
_c3 = _sq3.connect(str(DATA_DIR / "mofin.db"), timeout=10)
_total_months = _c3.execute(
"SELECT COUNT(DISTINCT substr(date,1,7)) FROM market_regime WHERE market='a'").fetchone()[0]
_regime_months = {r[0]: r[1] for r in _c3.execute(
"SELECT regime, COUNT(DISTINCT substr(date,1,7)) FROM market_regime WHERE market='a' GROUP BY regime").fetchall()}
_c3.close()
except Exception:
_total_months = 0
_regime_months = {}
_regime_universality = {} # (version, regime) -> {'months': N, 'score': 分散度}
try:
import sqlite3 as _sq4, json as _json4, collections as _coll
_c4 = _sq4.connect(str(DATA_DIR / "mofin.db"), timeout=10)
for _r in _c4.execute(
"SELECT version, results_json FROM strategy_research WHERE results_json IS NOT NULL"):
try:
_res = _json4.loads(_r[1])
_trades = _res.get("trades", [])
if not _trades:
continue
# 按温区过滤该策略 tradesregime_winrates 已有温区归类)
for _rg in ("trend_up", "choppy", "trend_down"):
_rg_months = _coll.Counter()
for _t in _trades:
_ed = _t.get("entry_date", "")
if _ed and _regime_map.get(_ed) == _rg:
_rg_months[_ed[:7]] += 1
if _rg_months:
_n = len(_rg_months)
_tot = sum(_rg_months.values())
_score = 0.0
if _n > 1:
for _c in _rg_months.values():
_p = _c / _tot
_score -= _p * (0.0 if _p == 0 else __import__('math').log(_p))
_score = _score / __import__('math').log(_n) * 100
_regime_universality.setdefault(_r[0], {})[_rg] = {
"months": _n, "score": round(_score, 0),
"regime_total_months": _regime_months.get(_rg, 0),
}
except Exception:
continue
_c4.close()
except Exception:
_regime_universality = {}
_deprecated = {}
try:
import sqlite3 as _sq2