perf: cache backtests by input signature

This commit is contained in:
Hermes Agent
2026-07-26 23:07:30 +00:00
parent 111b458ddf
commit 99f6e80ea1
2 changed files with 96 additions and 0 deletions
+42
View File
@@ -1,9 +1,11 @@
"""Historical backtest engine for Bitcoin Accumulation Zone scoring."""
import copy
import json
import logging
import os
import sys
import threading
from collections import defaultdict
from datetime import datetime, timedelta
@@ -17,6 +19,10 @@ sys.path.insert(0, BASE_DIR)
HISTORY_PATH = os.path.join(BASE_DIR, "data", "history.json")
CACHE_PATH = os.path.join(BASE_DIR, "data", "cache.json")
ML_WEIGHTS_PATH = os.path.join(BASE_DIR, "config", "ml_weights.json")
_BACKTEST_CACHE = {}
_BACKTEST_CACHE_LOCK = threading.Lock()
# Score brackets matching the dashboard assessment levels
BRACKETS = SCORE_BRACKETS
@@ -398,7 +404,43 @@ def compute_max_drawdown_forward(price_lookup, date, window=90):
return round(max_dd, 2) if max_dd > 0 else 0
def _file_signature(path):
"""Return a cheap signature that invalidates when an input file changes."""
try:
stat = os.stat(path)
return path, stat.st_mtime_ns, stat.st_size
except OSError:
return path, None, None
def clear_backtest_cache():
"""Clear memoized backtest results (primarily for explicit refreshes/tests)."""
with _BACKTEST_CACHE_LOCK:
_BACKTEST_CACHE.clear()
def run_backtest(ml_mode=False):
"""Return an isolated cached result keyed by all material input files."""
signature = (
bool(ml_mode),
_file_signature(HISTORY_PATH),
_file_signature(_THRESH_PATH),
_file_signature(ML_WEIGHTS_PATH),
_file_signature(CACHE_PATH),
)
with _BACKTEST_CACHE_LOCK:
cached = _BACKTEST_CACHE.get(signature)
if cached is not None:
return copy.deepcopy(cached)
result = _compute_backtest(ml_mode=ml_mode)
with _BACKTEST_CACHE_LOCK:
_BACKTEST_CACHE.clear()
_BACKTEST_CACHE[signature] = copy.deepcopy(result)
return copy.deepcopy(result)
def _compute_backtest(ml_mode=False):
"""Run the full backtest and return comprehensive results.
If ml_mode=True, uses ML-optimized metric weights instead of equal weights.