Fix QuantReport: fuzzy file matching + proper backtestId from historical data
- API: fuzzy matcher resolves files by strategy name substring - Frontend: backtestId now uses historical[name].name (the filename) - Server restarted with quant_report endpoint
This commit is contained in:
+35
-7
@@ -440,13 +440,41 @@ app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|
||||
|
||||
@app.get("/api/quant-report/{name}")
|
||||
async def get_quant_report(name: str):
|
||||
"""Compute full QF-Lib quant report from a backtest file."""
|
||||
backtest_path = os.path.join(BACKTEST_DIR, name)
|
||||
if not os.path.exists(backtest_path):
|
||||
# Try historical
|
||||
hist_path = os.path.join(HISTORICAL_DIR, name)
|
||||
if os.path.exists(hist_path):
|
||||
backtest_path = hist_path
|
||||
"""Compute full QF-Lib quant report from a backtest file.
|
||||
Accepts either an exact filename or a strategy-name prefix.
|
||||
"""
|
||||
# Build candidate paths
|
||||
candidates = []
|
||||
exact_path = os.path.join(BACKTEST_DIR, name)
|
||||
hist_exact = os.path.join(HISTORICAL_DIR, name)
|
||||
candidates.extend([exact_path, hist_exact])
|
||||
|
||||
# Try exact match first
|
||||
for path in candidates:
|
||||
if os.path.exists(path):
|
||||
backtest_path = path
|
||||
break
|
||||
else:
|
||||
# Fuzzy match: look for files containing the name
|
||||
for d in [BACKTEST_DIR, HISTORICAL_DIR]:
|
||||
if not os.path.exists(d): continue
|
||||
for f in os.listdir(d):
|
||||
# Match: name is a substring of filename (case insensitive)
|
||||
name_clean = name.lower().replace(" ", "_").replace(".json", "")
|
||||
f_clean = f.lower()
|
||||
if name_clean in f_clean or f_clean.startswith(name_clean):
|
||||
candidates.append(os.path.join(d, f))
|
||||
if not candidates:
|
||||
# Try partial match on strategy name
|
||||
for d in [BACKTEST_DIR, HISTORICAL_DIR]:
|
||||
if not os.path.exists(d): continue
|
||||
for f in os.listdir(d):
|
||||
parts = f.lower().replace(".json", "").split("_")
|
||||
name_parts = name.lower().replace(" ", "_").split("_")
|
||||
if all(p in parts for p in name_parts):
|
||||
candidates.append(os.path.join(d, f))
|
||||
if candidates:
|
||||
backtest_path = candidates[0]
|
||||
else:
|
||||
return JSONResponse({"error": f"Backtest '{name}' not found"}, status_code=404)
|
||||
try:
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user