fix: comparable periods pick one example per market cycle
Instead of showing 5 recent days with similar scores (all from the same 2-week window), now picks one example per cycle: - pre-2016, 2016-17 Bull, 2018-19 Bear, 2020-21 Bull, 2022-23 Bear, 2024+ - Sorted by closest score match, then picks one per cycle - Shows cycle label in brackets next to each example - Much more representative of how the score performed across different eras
This commit is contained in:
+32
-8
@@ -369,15 +369,39 @@ def run_backtest():
|
||||
avg_returns[period] = round(sum(vals) / len(vals), 2)
|
||||
avg_1yr = avg_returns.get("365d")
|
||||
|
||||
# Best comparable examples (most recent 5)
|
||||
# Best comparable examples — one per market cycle for diversity
|
||||
# Cycles: pre-2016, 2016-2017 bull, 2018-2019 bear, 2020-2021 bull, 2022-2023 bear, 2024+
|
||||
cycle_bins = [
|
||||
("pre-2016", "2010-01-01", "2015-12-31"),
|
||||
("2016-17 Bull", "2016-01-01", "2017-12-31"),
|
||||
("2018-19 Bear", "2018-01-01", "2019-12-31"),
|
||||
("2020-21 Bull", "2020-01-01", "2021-12-31"),
|
||||
("2022-23 Bear", "2022-01-01", "2023-12-31"),
|
||||
("2024+", "2024-01-01", "2099-12-31"),
|
||||
]
|
||||
examples = []
|
||||
for d in comparable[-5:]:
|
||||
examples.append({
|
||||
"date": d["date"],
|
||||
"score": d["score"],
|
||||
"price": d["price"],
|
||||
"forward_returns": d["forward_returns"],
|
||||
})
|
||||
used_cycles = set()
|
||||
# Sort comparable by closest score first, then pick one per cycle
|
||||
sorted_comp = sorted(comparable, key=lambda d: abs(d["score"] - current_score))
|
||||
for d in sorted_comp:
|
||||
cycle_label = None
|
||||
for label, start, end in cycle_bins:
|
||||
if start <= d["date"] <= end:
|
||||
cycle_label = label
|
||||
break
|
||||
if cycle_label and cycle_label not in used_cycles:
|
||||
used_cycles.add(cycle_label)
|
||||
examples.append({
|
||||
"date": d["date"],
|
||||
"score": d["score"],
|
||||
"price": d["price"],
|
||||
"forward_returns": d["forward_returns"],
|
||||
"cycle": cycle_label,
|
||||
})
|
||||
if len(examples) >= 6:
|
||||
break
|
||||
# Sort examples chronologically
|
||||
examples.sort(key=lambda d: d["date"])
|
||||
|
||||
current_context = {
|
||||
"current_score": current_score,
|
||||
|
||||
Reference in New Issue
Block a user