feat: 研究Tab加近5年区间——period_tag体系+API/UI按区间切换列表数据(5年验证v1.0现原形-14.9%)
This commit is contained in:
+29
-13
@@ -680,7 +680,7 @@ def has_breakout_dna(bars, i, lookback=10):
|
||||
return False
|
||||
|
||||
|
||||
def run_backtest(strategy_version, start_date, end_date, capital=1000000, save=True, universe='all'):
|
||||
def run_backtest(strategy_version, start_date, end_date, capital=1000000, save=True, universe='all', period_tag='2y'):
|
||||
strat = get_strategy(strategy_version)
|
||||
cfg = strat['config']
|
||||
entry_cfg, exit_cfg = cfg['entry'], cfg['exit']
|
||||
@@ -1059,6 +1059,7 @@ def run_backtest(strategy_version, start_date, end_date, capital=1000000, save=T
|
||||
'strategy_name': strat['name'],
|
||||
'market': universe,
|
||||
'period': f"{start_date} ~ {end_date}",
|
||||
'period_tag': period_tag,
|
||||
'capital': capital,
|
||||
'total_stocks_screened': screened,
|
||||
'scored_events': scored_n,
|
||||
@@ -1418,6 +1419,10 @@ def init_table():
|
||||
conn.execute("ALTER TABLE strategy_research ADD COLUMN market TEXT DEFAULT 'all'")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
try:
|
||||
conn.execute("ALTER TABLE strategy_research ADD COLUMN period_tag TEXT DEFAULT '2y'")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -1427,12 +1432,12 @@ def save_result(strat, result):
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.execute("""
|
||||
INSERT INTO strategy_research (version, name, summary, hypothesis, parent,
|
||||
config_json, results_json, period, created_at, market)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)
|
||||
config_json, results_json, period, created_at, market, period_tag)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)
|
||||
""", (strat['version'], strat['name'], strat['summary'], strat['hypothesis'],
|
||||
strat.get('parent'), json.dumps(strat['config'], ensure_ascii=False),
|
||||
json.dumps(result, ensure_ascii=False), result['period'],
|
||||
datetime.now().strftime('%Y-%m-%d %H:%M:%S'), result.get('market', 'all')))
|
||||
datetime.now().strftime('%Y-%m-%d %H:%M:%S'), result.get('market', 'all'), result.get('period_tag', '2y')))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -1448,18 +1453,29 @@ def save_analysis(version, analysis):
|
||||
conn.close()
|
||||
|
||||
|
||||
def list_strategies():
|
||||
def list_strategies(period_tag=None):
|
||||
init_table()
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
# 每个 (version, market) 组合取最新一条
|
||||
rows = conn.execute("""
|
||||
SELECT sr.* FROM strategy_research sr
|
||||
INNER JOIN (SELECT version, COALESCE(market,'all') as mkt, MAX(id) as max_id
|
||||
FROM strategy_research GROUP BY version, COALESCE(market,'all')) latest
|
||||
ON sr.id = latest.max_id
|
||||
ORDER BY sr.version
|
||||
""").fetchall()
|
||||
if period_tag:
|
||||
# 指定区间:每个 (version, market) 取该区间最新一条
|
||||
rows = conn.execute("""
|
||||
SELECT sr.* FROM strategy_research sr
|
||||
INNER JOIN (SELECT version, COALESCE(market,'all') as mkt, MAX(id) as max_id
|
||||
FROM strategy_research WHERE COALESCE(period_tag,'2y')=?
|
||||
GROUP BY version, mkt) latest
|
||||
ON sr.id = latest.max_id
|
||||
ORDER BY sr.version
|
||||
""", (period_tag,)).fetchall()
|
||||
else:
|
||||
# 默认:每个 (version, market) 组合取最新一条
|
||||
rows = conn.execute("""
|
||||
SELECT sr.* FROM strategy_research sr
|
||||
INNER JOIN (SELECT version, COALESCE(market,'all') as mkt, MAX(id) as max_id
|
||||
FROM strategy_research GROUP BY version, COALESCE(market,'all')) latest
|
||||
ON sr.id = latest.max_id
|
||||
ORDER BY sr.version
|
||||
""").fetchall()
|
||||
conn.close()
|
||||
out = []
|
||||
for r in rows:
|
||||
|
||||
Reference in New Issue
Block a user