feat: 自我进化模块——health_monitor+lesson_extractor+evolution_api+Dashboard+auto_iterator
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
evolution/evolution_api.py — 进化模块 API 接口
|
||||
供 dashboard 查询健康度、教训、迭代历史
|
||||
"""
|
||||
import sys, os, json, sqlite3
|
||||
|
||||
sys.path.insert(0, '/home/hmo/MoFin')
|
||||
sys.path.insert(0, '/home/hmo/MoFin/deploy/profile-scripts')
|
||||
|
||||
DB = os.environ.get('MOFIN_DB', '/home/hmo/MoFin/data/mofin.db')
|
||||
|
||||
|
||||
def get_evolution_dashboard():
|
||||
"""进化模块 Dashboard 数据"""
|
||||
conn = sqlite3.connect(DB)
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
# 最近健康度(近30天)
|
||||
health = []
|
||||
for r in conn.execute("""
|
||||
SELECT strategy_version, date, live_trades, live_wins, live_return_pct,
|
||||
backtest_wr, backtest_avg_ret, deviation, health_score
|
||||
FROM strategy_health ORDER BY date DESC LIMIT 30
|
||||
""").fetchall():
|
||||
health.append(dict(r))
|
||||
|
||||
# 最近教训(近20条)
|
||||
lessons = []
|
||||
for r in conn.execute("""
|
||||
SELECT strategy_version, lesson_type, lesson_text, confidence, applied, created_at
|
||||
FROM strategy_lessons ORDER BY id DESC LIMIT 20
|
||||
""").fetchall():
|
||||
lessons.append(dict(r))
|
||||
|
||||
# 迭代历史
|
||||
evolution = []
|
||||
for r in conn.execute("""
|
||||
SELECT parent_version, child_version, change_description, promoted, created_at
|
||||
FROM strategy_evolution ORDER BY id DESC LIMIT 20
|
||||
""").fetchall():
|
||||
evolution.append(dict(r))
|
||||
|
||||
# 当前策略基线
|
||||
baseline = {}
|
||||
for v in ['v_next4', 'v_next3', 'v8.1']:
|
||||
r = conn.execute("""
|
||||
SELECT results_json FROM strategy_research
|
||||
WHERE version=? AND period_tag='5y' ORDER BY id DESC LIMIT 1
|
||||
""", (v,)).fetchone()
|
||||
if r:
|
||||
res = json.loads(r[0])
|
||||
s = res.get('summary', {})
|
||||
pf = s.get('portfolio_full', {})
|
||||
baseline[v] = {
|
||||
'win_rate': s.get('win_rate', 0),
|
||||
'total_return': pf.get('total_return_pct', 0),
|
||||
'cagr': pf.get('cagr_pct', 0),
|
||||
'max_dd': pf.get('portfolio_max_dd_pct', 0),
|
||||
}
|
||||
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'health': health,
|
||||
'lessons': lessons,
|
||||
'evolution': evolution,
|
||||
'baseline': baseline,
|
||||
}
|
||||
|
||||
|
||||
def get_health_trend(version='v_next4', days=30):
|
||||
"""健康度趋势"""
|
||||
conn = sqlite3.connect(DB)
|
||||
conn.row_factory = sqlite3.Row
|
||||
rows = conn.execute("""
|
||||
SELECT date, health_score, deviation, live_trades
|
||||
FROM strategy_health WHERE strategy_version=? ORDER BY date DESC LIMIT ?
|
||||
""", (version, days)).fetchall()
|
||||
conn.close()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
def record_evolution(parent, child, description, backtest_result=None, promoted=0):
|
||||
"""记录一次策略迭代"""
|
||||
conn = sqlite3.connect(DB)
|
||||
conn.execute("""
|
||||
INSERT INTO strategy_evolution (parent_version, child_version, change_description, backtest_result, promoted)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""", (parent, child, description, json.dumps(backtest_result) if backtest_result else None, promoted))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
d = get_evolution_dashboard()
|
||||
print(f"健康度: {len(d['health'])}条, 教训: {len(d['lessons'])}条, 迭代: {len(d['evolution'])}条")
|
||||
print(f"基线: {list(d['baseline'].keys())}")
|
||||
Reference in New Issue
Block a user