Files
MoFin/deploy/profile-scripts/ab_research_daily.py
T

93 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""ab_research_daily.py — AB路线每日LLM主导研究(老莫2026-08-18
职责:
1. 读进化中心快照 evolution_center.json + strategy_regime_perf_by_period
2. 分析找出薄弱温区/薄弱策略(覆盖最少)
3. 记录到 strategy_research_log 表(每日一行:日期/发现/尝试/结论/产出策略)
4. 产出新策略(若验证通过)→ 注册为未激活状态(等老莫审查)
"""
import sys, os, json, sqlite3
from datetime import datetime
DB = "/home/hmo/MoFin/data/mofin.db"
CENTER = "/home/hmo/MoFin/data/evolution_center.json"
def ensure_table(conn):
conn.execute("""
CREATE TABLE IF NOT EXISTS strategy_research_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
log_date TEXT NOT NULL,
market TEXT,
weak_regime TEXT,
finding TEXT,
experiment TEXT,
result TEXT,
produced_strategy TEXT,
produced_verified INTEGER DEFAULT 0,
llm_model TEXT,
created_at TEXT
)
""")
conn.commit()
def load_center():
if not os.path.exists(CENTER):
return {}
try:
return json.load(open(CENTER))
except:
return {}
def analyze(conn):
"""结构化分析:温区覆盖薄弱点"""
weak = []
findings = []
try:
rows = conn.execute("""
SELECT market, regime, COUNT(DISTINCT strategy) as cnt
FROM strategy_regime_perf_by_period
WHERE period_tag='2y' AND trades >= 30
GROUP BY market, regime
""").fetchall()
coverage = [{"market": r[0], "regime": r[1], "count": r[2]} for r in rows]
if coverage:
coverage.sort(key=lambda x: x["count"])
weak = coverage[:2]
findings.append("策略覆盖最少的温区: " + "; ".join(
f"{c['market']}/{c['regime']}({c['count']}个策略)" for c in weak))
# 补充:qual_overview 里合格策略少的
center = load_center()
qo = center.get("qual_overview") or []
if qo:
findings.append(f"合格策略总览: {len(qo)}条")
except Exception as e:
findings.append(f"分析异常: {e}")
return findings, weak
def main():
conn = sqlite3.connect(DB, timeout=30)
conn.execute("PRAGMA busy_timeout=30000")
ensure_table(conn)
today = datetime.now().strftime("%Y-%m-%d")
exists = conn.execute("SELECT COUNT(*) FROM strategy_research_log WHERE log_date=?", (today,)).fetchone()[0]
if exists:
print(f"[AB研究] {today} 已有记录,跳过")
conn.close()
return
findings, weak = analyze(conn)
finding_text = "; ".join(findings) if findings else "无明显薄弱点(常规观察)"
weak_rg = (weak[0]["regime"] if weak else "")
weak_mkt = (weak[0]["market"] if weak else "a")
conn.execute(
"INSERT INTO strategy_research_log (log_date, market, weak_regime, finding, experiment, result, produced_strategy, created_at) "
"VALUES (?,?,?,?,?,?,?,?)",
(today, weak_mkt, weak_rg, finding_text,
"自动扫描温区覆盖+B组候选", "记录待LLM深度分析", "",
datetime.now().isoformat()))
conn.commit()
print(f"[AB研究] {today} 已记录: {finding_text}")
conn.close()
if __name__ == "__main__":
main()