File size: 29,111 Bytes
7e96b08 49b1df6 7e96b08 49b1df6 7e96b08 b65a56b 9ea087a b65a56b 9ea087a b65a56b 7e96b08 49b1df6 b65a56b 7e96b08 9ea087a 7e96b08 74b343e 7e96b08 402d7a3 7e96b08 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 932b386 49b1df6 7e96b08 9ea087a 924efea 9ea087a 49b1df6 7e96b08 9ea087a 49b1df6 7e96b08 9ea087a 7e96b08 9ea087a 7e96b08 40c712d 7e96b08 9ea087a 7e96b08 e58ecdd 9ea087a 7e96b08 49b1df6 74b343e 7e96b08 402d7a3 7e96b08 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a bc98674 49b1df6 9ea087a 49b1df6 bc98674 49b1df6 bc98674 49b1df6 9ea087a bc98674 9ea087a bc98674 9ea087a bc98674 9ea087a 49b1df6 bc98674 49b1df6 9ea087a bc98674 9ea087a 49b1df6 9ea087a bc98674 9ea087a bc98674 9ea087a bc98674 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 bc98674 9ea087a 49b1df6 9ea087a 49b1df6 bc98674 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a bc98674 9ea087a 49b1df6 bc98674 49b1df6 bc98674 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a bc98674 9ea087a 49b1df6 bc98674 49b1df6 9ea087a 49b1df6 bc98674 49b1df6 9ea087a 49b1df6 9ea087a 9029ea8 9ea087a 9029ea8 bc98674 49b1df6 727d9cd 49b1df6 727d9cd 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a bc98674 9ea087a bc98674 9ea087a 49b1df6 9ea087a 49b1df6 9ea087a 49b1df6 bc98674 9ea087a 49b1df6 9ea087a 49b1df6 0b12971 bc98674 0b12971 49b1df6 0b12971 49b1df6 0b12971 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 49b1df6 0b12971 bc98674 49b1df6 bc98674 49b1df6 bc98674 49b1df6 bc98674 9029ea8 bc98674 49b1df6 bc98674 49b1df6 bc98674 49b1df6 9029ea8 49b1df6 9029ea8 49b1df6 9029ea8 49b1df6 bc98674 49b1df6 9029ea8 bc98674 49b1df6 bc98674 49b1df6 727d9cd 49b1df6 727d9cd 49b1df6 9029ea8 49b1df6 9029ea8 49b1df6 9029ea8 49b1df6 9029ea8 bc98674 9029ea8 49b1df6 9029ea8 49b1df6 bc98674 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | """
app.py β MC Dropout edition
P2-ETF-CNN-LSTM-ALTERNATIVE-APPROACHES
Changes from original
---------------------
1. Sidebar: added MC Dropout toggle + n_passes slider (shown only when enabled)
2. run_module(): predict_approachN calls replaced with mc_predict_approachN
when MC Dropout is on; falls back to original predict_approachN when off.
3. display_single_year_results(): show_conviction_panel replaced with
show_mc_conviction_panel when MC Dropout results are available.
4. show_all_signals_panel replaced with show_mc_all_signals_panel (MC mode).
5. Cache keys include mc_enabled + n_passes so toggling forces a fresh run.
6. trained_info now stores {"proba", "mean_proba", "uncertainty"} so both
MC and non-MC paths share the same downstream display logic.
All original behaviour is preserved when MC Dropout is disabled.
"""
import os
import streamlit as st
import pandas as pd
import numpy as np
from data.loader import (load_dataset, check_data_freshness,
get_features_and_targets, dataset_summary,
FI_ETF_COLS, EQUITY_ETF_COLS)
from utils.calendar import get_est_time, get_next_signal_date
from models.base import (build_sequences, train_val_test_split,
scale_features, returns_to_labels,
find_best_lookback, make_cache_key,
save_cache, load_cache)
from models.approach1_wavelet import train_approach1, predict_approach1
from models.approach2_regime import train_approach2, predict_approach2
from models.approach3_multiscale import train_approach3, predict_approach3
# ββ MC Dropout imports (new) ββββββββββββββββββββββββββββββββββββββββββββββββββ
from models.mc_dropout import (
mc_predict_approach1,
mc_predict_approach2,
mc_predict_approach3,
)
from signals.mc_conviction import compute_mc_conviction
from ui.mc_components import (
show_mc_conviction_panel,
show_mc_all_signals_panel,
mc_passes_selector,
)
from strategy.backtest import execute_strategy, select_winner, build_comparison_table
from signals.conviction import compute_conviction
from ui.components import (
show_freshness_status, show_signal_banner, show_conviction_panel,
show_metrics_row, show_comparison_table, show_audit_trail,
show_all_signals_panel,
)
from ui.multiyear import run_multiyear_sweep, show_multiyear_results
st.set_page_config(page_title="P2-ETF-CNN-LSTM", page_icon="π§ ", layout="wide")
HF_TOKEN = os.getenv("HF_TOKEN", "")
# ββ Initialize session state with module prefixes ββββββββββββββββββββββββββββ
def init_module_state(prefix):
defaults = {
f"{prefix}_output_ready": False,
f"{prefix}_results": None,
f"{prefix}_trained_info": None,
f"{prefix}_test_dates": None,
f"{prefix}_test_slice": None,
f"{prefix}_optimal_lookback": None,
f"{prefix}_df_for_chart": None,
f"{prefix}_target_etfs": None,
f"{prefix}_multiyear_ready": False,
f"{prefix}_multiyear_results": None,
}
for key, default in defaults.items():
if key not in st.session_state:
st.session_state[key] = default
init_module_state("fi")
init_module_state("eq")
if "tbill_rate" not in st.session_state:
st.session_state["tbill_rate"] = None
# ββ Sidebar βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with st.sidebar:
st.header("βοΈ Configuration")
st.write(f"π **EST:** {get_est_time().strftime('%H:%M:%S')}")
st.divider()
start_yr = st.slider("π
Start Year", 2010, 2024, 2016)
fee_bps = st.slider("π° Fee (bps)", 0, 50, 10)
epochs = st.number_input("π Max Epochs", 20, 150, 80, step=10)
st.divider()
split_option = st.selectbox("π Train/Val/Test Split",
["70/15/15", "80/10/10"], index=0)
train_pct, val_pct = {"70/15/15": (0.70, 0.15),
"80/10/10": (0.80, 0.10)}[split_option]
# ββ MC Dropout controls (new) βββββββββββββββββββββββββββββββββββββββββββββ
st.divider()
mc_enabled = st.toggle(
"π² MC Dropout Uncertainty",
value=True,
help=(
"Run N stochastic forward passes at inference with dropout active. "
"Produces per-ETF uncertainty estimates and an adjusted conviction score. "
"Automatically recommends CASH when uncertainty is too high."
),
)
if mc_enabled:
n_passes = mc_passes_selector(key="mc_n_passes_sidebar")
st.caption(
f"**{n_passes} passes** Β· Dropout active at inference Β· "
f"~{n_passes * 5}ms extra per approach on CPU"
)
else:
n_passes = 0
st.caption("MC Dropout off β single deterministic forward pass.")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.caption("π‘ CASH triggered automatically on 2-day drawdown β€ β15%")
st.divider()
if not HF_TOKEN:
st.error("β HF_TOKEN secret not found.")
st.stop()
# ββ Load dataset ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with st.spinner("π‘ Loading dataset from HuggingFace..."):
df_raw = load_dataset(HF_TOKEN)
if df_raw.empty:
st.stop()
freshness = check_data_freshness(df_raw)
last_date_str = str(freshness.get("last_date_in_data", "unknown"))
# ββ Dataset info sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with st.sidebar:
st.divider()
st.subheader("π¦ Dataset Info")
fi_summary = dataset_summary(df_raw, module_type="fi")
eq_summary = dataset_summary(df_raw, module_type="equity")
st.write(f"**Data Range:** {fi_summary['start_date']} β {fi_summary['end_date']}")
st.write(f"**Rows:** {fi_summary['rows']:,}")
with st.expander("π Fixed Income ETFs"):
st.write(f"Available: {', '.join(fi_summary['etfs_found'])}")
with st.expander("π Equity ETFs"):
st.write(f"Available: {', '.join(eq_summary['etfs_found'])}")
st.write(f"**Macro Signals:** {', '.join(fi_summary['macro_found'])}")
st.write(f"**T-bill col:** {'β
' if fi_summary['tbill_found'] else 'β'}")
# ββ Main Title ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.title("π§ P2-ETF-CNN-LSTM")
st.caption("Multi-Asset ETF Rotation using CNN-LSTM | Fixed Income & Equity Modules")
show_freshness_status(freshness)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MODULE RUNNER
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_module(module_type: str, df_raw: pd.DataFrame, start_yr: int, fee_bps: int,
epochs: int, train_pct: float, val_pct: float, last_date_str: str,
mc_enabled: bool = True, n_passes: int = 50):
"""Execute all 3 approaches for a given module type (fi or equity)."""
prefix = module_type
st.session_state[f"{prefix}_output_ready"] = False
df = df_raw[df_raw.index.year >= start_yr].copy()
n_rows = len(df)
if n_rows < 100:
st.error(f"β Insufficient data: only {n_rows} rows from {start_yr}.")
return False
st.write(
f"π
**Data:** {df.index[0].strftime('%Y-%m-%d')} β "
f"{df.index[-1].strftime('%Y-%m-%d')} "
f"({df.index[-1].year - df.index[0].year + 1} years, {n_rows} rows)"
)
try:
input_features, target_etfs, tbill_rate, df, col_info = get_features_and_targets(
df, module_type=module_type
)
except ValueError as e:
st.error(str(e))
return False
n_classes = len(target_etfs)
st.info(
f"π― **Targets:** {', '.join([t.replace('_Ret','') for t in target_etfs])} Β· "
f"**Features:** {len(input_features)} signals Β· "
f"**T-bill:** {tbill_rate*100:.2f}% Β· "
f"**Rows:** {len(df)}"
)
X_raw = df[input_features].values.astype(np.float32)
y_raw = np.clip(df[target_etfs].values.astype(np.float32), -0.5, 0.5)
for j in range(X_raw.shape[1]):
mask = np.isnan(X_raw[:, j])
if mask.any():
X_raw[mask, j] = np.nanmean(X_raw[:, j])
for j in range(y_raw.shape[1]):
mask = np.isnan(y_raw[:, j])
if mask.any():
y_raw[mask, j] = 0.0
# ββ Lookback auto-select (unchanged) βββββββββββββββββββββββββββββββββββββ
cache_prefix = f"{last_date_str}_{module_type}"
lb_key = make_cache_key(cache_prefix, start_yr, fee_bps, int(epochs),
split_option, False, 0)
lb_cached = load_cache(f"lb_{lb_key}")
if lb_cached is not None:
optimal_lookback = lb_cached["optimal_lookback"]
st.success(f"β‘ Lookback cache hit: **{optimal_lookback}d**")
else:
with st.spinner("π Auto-selecting optimal lookback (30/45/60d)..."):
try:
optimal_lookback = find_best_lookback(
X_raw, y_raw, train_pct, val_pct, n_classes,
candidates=[30, 45, 60],
)
except ValueError as e:
st.error(f"β Lookback selection failed: {e}")
return False
save_cache(f"lb_{lb_key}", {"optimal_lookback": optimal_lookback})
st.success(f"π Optimal lookback: **{optimal_lookback}d**")
lookback = optimal_lookback
# ββ Model cache β key includes mc flag + n_passes so toggling forces rerun
cache_key = make_cache_key(
f"{cache_prefix}_mc{int(mc_enabled)}_{n_passes}",
start_yr, fee_bps, int(epochs), split_option, False, lookback,
)
cached_data = load_cache(cache_key)
if cached_data is not None:
results = cached_data["results"]
trained_info = cached_data["trained_info"]
test_dates = pd.DatetimeIndex(cached_data["test_dates"])
test_slice = cached_data["test_slice"]
st.success("β‘ Model results loaded from cache")
else:
X_seq, y_seq = build_sequences(X_raw, y_raw, lookback)
y_labels = returns_to_labels(y_seq)
(X_train, y_train_r, X_val, y_val_r,
X_test, y_test_r) = train_val_test_split(X_seq, y_seq, train_pct, val_pct)
(_, y_train_l, _, y_val_l, _, _) = train_val_test_split(
X_seq, y_labels, train_pct, val_pct
)
for name, arr in [("Training", X_train), ("Validation", X_val), ("Test", X_test)]:
if len(arr) == 0:
st.error(f"β {name} set is empty. Try an earlier Start Year.")
return False
X_train_s, X_val_s, X_test_s, _ = scale_features(X_train, X_val, X_test)
train_size = len(X_train)
val_size = len(X_val)
test_start = lookback + train_size + val_size
test_dates = df.index[test_start: test_start + len(X_test)]
test_slice = slice(test_start, test_start + len(X_test))
results, trained_info = {}, {}
progress = st.progress(0, text="Training Approach 1...")
# ββ Approach configs ββββββββββββββββββββββββββββββββββββββββββββββββ
# Each entry: (name, train_fn, predict_fn_standard, predict_fn_mc)
# predict_fn_mc returns (preds, mean_proba, uncertainty)
# predict_fn_standard returns (preds, proba)
approach_configs = [
(
"Approach 1",
lambda: train_approach1(
X_train_s, y_train_l, X_val_s, y_val_l,
n_classes=n_classes, epochs=int(epochs),
),
lambda m: predict_approach1(m[0], X_test_s),
lambda m: mc_predict_approach1(m[0], X_test_s, n_passes=n_passes),
),
(
"Approach 2",
lambda: train_approach2(
X_train_s, y_train_l, X_val_s, y_val_l,
X_flat_all=X_raw, feature_names=input_features,
lookback=lookback, train_size=train_size,
val_size=val_size, n_classes=n_classes,
epochs=int(epochs),
),
lambda m: predict_approach2(
m[0], X_test_s, X_raw, m[3], m[2],
lookback, train_size, val_size,
),
lambda m: mc_predict_approach2(
m[0], X_test_s, X_raw, m[3], m[2],
lookback, train_size, val_size, n_passes=n_passes,
),
),
(
"Approach 3",
lambda: train_approach3(
X_train_s, y_train_l, X_val_s, y_val_l,
n_classes=n_classes, epochs=int(epochs),
),
lambda m: predict_approach3(m[0], X_test_s),
lambda m: mc_predict_approach3(m[0], X_test_s, n_passes=n_passes),
),
]
for idx, (approach, train_fn, predict_fn, mc_predict_fn) in enumerate(approach_configs):
try:
model_out = train_fn()
if mc_enabled:
# ββ MC path βββββββββββββββββββββββββββββββββββββββββββ
preds, mean_proba, uncertainty = mc_predict_fn(model_out)
trained_info[approach] = {
"proba": mean_proba, # mean_proba is the "proba" for backtest
"mean_proba": mean_proba,
"uncertainty": uncertainty,
"mc_enabled": True,
}
else:
# ββ Standard path (unchanged) βββββββββββββββββββββββββ
preds, proba = predict_fn(model_out)
trained_info[approach] = {
"proba": proba,
"mean_proba": None,
"uncertainty": None,
"mc_enabled": False,
}
results[approach] = execute_strategy(
preds, trained_info[approach]["proba"],
y_test_r, test_dates,
target_etfs, fee_bps, tbill_rate,
)
except Exception as e:
st.warning(f"β οΈ {approach} failed: {e}")
results[approach] = None
trained_info[approach] = {
"proba": None, "mean_proba": None,
"uncertainty": None, "mc_enabled": mc_enabled,
}
pct = int((idx + 1) / 3 * 100)
progress.progress(pct, text=f"{approach} complete...")
progress.empty()
save_cache(cache_key, {
"results": results,
"trained_info": trained_info,
"test_dates": list(test_dates),
"test_slice": test_slice,
})
# ββ Store in session state ββββββββββββββββββββββββββββββββββββββββββββββββ
st.session_state[f"{prefix}_results"] = results
st.session_state[f"{prefix}_trained_info"] = trained_info
st.session_state[f"{prefix}_test_dates"] = test_dates
st.session_state[f"{prefix}_test_slice"] = test_slice
st.session_state[f"{prefix}_optimal_lookback"] = optimal_lookback
st.session_state[f"{prefix}_df_for_chart"] = df
st.session_state[f"{prefix}_target_etfs"] = target_etfs
st.session_state["tbill_rate"] = tbill_rate
st.session_state[f"{prefix}_output_ready"] = True
return True
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DISPLAY SINGLE-YEAR RESULTS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def display_single_year_results(module_type: str):
prefix = module_type
if not st.session_state.get(f"{prefix}_output_ready"):
st.info("π Click **π Run Analysis** to see Single-Year results.")
return
results = st.session_state.get(f"{prefix}_results")
trained_info = st.session_state.get(f"{prefix}_trained_info")
test_dates = st.session_state.get(f"{prefix}_test_dates")
test_slice = st.session_state.get(f"{prefix}_test_slice")
optimal_lookback = st.session_state.get(f"{prefix}_optimal_lookback")
df = st.session_state.get(f"{prefix}_df_for_chart")
tbill_rate = st.session_state.get("tbill_rate")
target_etfs = st.session_state.get(f"{prefix}_target_etfs")
if not all([results, trained_info, test_dates is not None, df is not None]):
st.error("β Missing data. Please run the analysis again.")
return
winner_name = select_winner(results)
winner_res = results.get(winner_name)
if winner_res is None:
st.error("β All approaches failed.")
return
st.caption("Winner selected by highest raw annualised return on out-of-sample test set.")
next_date = get_next_signal_date()
st.divider()
show_signal_banner(winner_res["next_signal"], next_date, winner_name)
# ββ Conviction panel: MC or standard βββββββββββββββββββββββββββββββββββββ
winner_info = trained_info[winner_name]
_is_mc = winner_info.get("mc_enabled", False)
if _is_mc and winner_info.get("mean_proba") is not None:
mean_p = winner_info["mean_proba"]
unc = winner_info["uncertainty"]
mc_conv = compute_mc_conviction(mean_p[-1], unc[-1], target_etfs,
include_cash=False)
show_mc_conviction_panel(mc_conv, n_passes=n_passes)
else:
# Original path β unchanged
winner_proba = winner_info["proba"]
if winner_proba is not None:
conviction = compute_conviction(winner_proba[-1], target_etfs,
include_cash=False)
show_conviction_panel(conviction)
st.divider()
# ββ All-signals panel: MC or standard ββββββββββββββββββββββββββββββββββββ
if _is_mc:
mc_all_signals = {}
for name, res in results.items():
if res is None:
continue
info = trained_info[name]
mp = info.get("mean_proba")
uc = info.get("uncertainty")
mc_all_signals[name] = {
"signal": res["next_signal"],
"mc_conv": compute_mc_conviction(mp[-1], uc[-1], target_etfs,
include_cash=False)
if mp is not None else None,
"is_winner": name == winner_name,
}
show_mc_all_signals_panel(
mc_all_signals, target_etfs, False, next_date,
optimal_lookback, n_passes=n_passes,
)
else:
all_signals = {
name: {
"signal": res["next_signal"],
"proba": trained_info[name]["proba"][-1]
if trained_info[name]["proba"] is not None else None,
"is_winner": name == winner_name,
}
for name, res in results.items() if res is not None
}
show_all_signals_panel(all_signals, target_etfs, False,
next_date, optimal_lookback)
st.divider()
st.subheader(f"π {winner_name} β Performance Metrics")
spy_ann = None
if df is not None and "SPY_Ret" in df.columns and test_slice is not None:
spy_raw = df["SPY_Ret"].iloc[test_slice].values.copy().astype(float)
spy_raw = spy_raw[~np.isnan(spy_raw)]
spy_raw = np.clip(spy_raw, -0.5, 0.5)
if len(spy_raw) > 5:
spy_cum = np.prod(1 + spy_raw)
spy_ann = float(spy_cum ** (252 / len(spy_raw)) - 1)
show_metrics_row(winner_res, tbill_rate, spy_ann_return=spy_ann)
st.divider()
st.subheader("π Approach Comparison")
show_comparison_table(build_comparison_table(results, winner_name))
st.divider()
st.subheader(f"π Audit Trail β {winner_name} (Last 20 Trading Days)")
show_audit_trail(winner_res["audit_trail"])
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DISPLAY MULTI-YEAR SWEEP (unchanged from original)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def display_multiyear_sweep(module_type: str, last_date_str: str, fee_bps: int,
epochs: int, split_option: str, train_pct: float,
val_pct: float, df_raw: pd.DataFrame):
prefix = module_type
SWEEP_YEARS = list(range(2008, 2026))
st.subheader("π Multi-Year Consensus Sweep")
st.markdown(
"Runs **all 3 approaches** across **all years from 2008 to 2025**, picks the winner "
"per year, and aggregates signals into a weighted consensus vote."
)
st.caption(f"Sweep years: {', '.join(str(y) for y in SWEEP_YEARS)}")
col_info, col_run, col_force = st.columns([2, 1, 1])
with col_info:
st.caption(f"Data: {last_date_str}")
with col_run:
sweep_button = st.button("π Run Consensus Sweep", type="primary",
use_container_width=True,
key=f"{prefix}_sweep_run")
with col_force:
force_retrain_button = st.button("π Force Retrain All", type="secondary",
use_container_width=True,
key=f"{prefix}_sweep_force")
if force_retrain_button:
st.session_state[f"{prefix}_multiyear_ready"] = False
st.session_state[f"{prefix}_multiyear_results"] = None
with st.spinner("ποΈ Cache cleared β retraining all yearsβ¦"):
try:
sweep_results = run_multiyear_sweep(
df_raw=df_raw, sweep_years=SWEEP_YEARS, fee_bps=fee_bps,
epochs=int(epochs), split_option=split_option,
last_date_str=last_date_str, train_pct=train_pct,
val_pct=val_pct, force_retrain=True, module_type=module_type,
)
st.session_state[f"{prefix}_multiyear_results"] = sweep_results
st.session_state[f"{prefix}_multiyear_ready"] = True
st.rerun()
except Exception as e:
st.error(f"β Sweep failed: {e}")
elif sweep_button:
st.session_state[f"{prefix}_multiyear_ready"] = False
with st.spinner("Running sweep..."):
try:
sweep_results = run_multiyear_sweep(
df_raw=df_raw, sweep_years=SWEEP_YEARS, fee_bps=fee_bps,
epochs=int(epochs), split_option=split_option,
last_date_str=last_date_str, train_pct=train_pct,
val_pct=val_pct, force_retrain=False, module_type=module_type,
)
st.session_state[f"{prefix}_multiyear_results"] = sweep_results
st.session_state[f"{prefix}_multiyear_ready"] = True
st.rerun()
except Exception as e:
st.error(f"β Sweep failed: {e}")
if (st.session_state.get(f"{prefix}_multiyear_ready")
and st.session_state.get(f"{prefix}_multiyear_results")):
show_multiyear_results(st.session_state[f"{prefix}_multiyear_results"],
sweep_years=SWEEP_YEARS)
elif not st.session_state.get(f"{prefix}_multiyear_ready"):
st.info("Click **π Run Consensus Sweep** to analyse all start years at once.")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MAIN MODULE TAB BUILDER (unchanged structure, passes mc_enabled + n_passes)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def build_module_tab(module_type: str, module_name: str, etf_list: str,
last_date_str: str, fee_bps: int, epochs: int,
split_option: str, train_pct: float, val_pct: float,
df_raw: pd.DataFrame):
st.header(f"{module_name} ETF Rotation")
st.markdown(f"**ETFs:** {etf_list}")
run_button = st.button(
f"π Run {module_name} Analysis", type="primary",
use_container_width=True, key=f"{module_type}_run_button",
)
if run_button:
with st.spinner(f"Running {module_name} module..."):
success = run_module(
module_type, df_raw, start_yr, fee_bps, epochs,
train_pct, val_pct, last_date_str,
mc_enabled=mc_enabled, n_passes=n_passes,
)
if success:
st.rerun()
st.divider()
tab_single, tab_multi = st.tabs(["π Single-Year Results",
"π Multi-Year Consensus"])
with tab_single:
display_single_year_results(module_type)
with tab_multi:
display_multiyear_sweep(module_type, last_date_str, fee_bps, epochs,
split_option, train_pct, val_pct, df_raw)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MAIN TABS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
tab_fi, tab_equity = st.tabs(["ποΈ Fixed Income (FI)", "π Equity"])
with tab_fi:
build_module_tab(
module_type="fi", module_name="Fixed Income",
etf_list="TLT, VNQ, SLV, GLD, LQD, HYG, VCIT",
last_date_str=last_date_str, fee_bps=fee_bps,
epochs=epochs, split_option=split_option,
train_pct=train_pct, val_pct=val_pct, df_raw=df_raw,
)
with tab_equity:
build_module_tab(
module_type="eq", module_name="Equity",
etf_list="QQQ, XLK, XLF, XLE, XLV, XLI, XLY, XLP, XLU, XME, GDX, IWM",
last_date_str=last_date_str, fee_bps=fee_bps,
epochs=epochs, split_option=split_option,
train_pct=train_pct, val_pct=val_pct, df_raw=df_raw,
)
|