-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
478 lines (418 loc) · 21.9 KB
/
Copy pathapp.py
File metadata and controls
478 lines (418 loc) · 21.9 KB
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
import os
import json
import time
from pathlib import Path
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from streamlit_autorefresh import st_autorefresh
import yfinance as yf
import mlflow
from mlflow.tracking import MlflowClient
from mlflow.exceptions import MlflowException
# =============================================================================
# IMPORTS DES MODULES UTILITAIRES ET DATA
# =============================================================================
from src.utils.market_utils import get_live_ticker_data, discover_markets, get_ticker_currency
from src.utils.ui_utils import display_kpi_card, load_css
from src.utils.metrics import calculate_metrics, calculate_period_return
from src.utils.math_utils import trim_flat_start
from src.utils.mlflow_utils import get_champion_metrics
from src.extract.data_loader import load_all_data
from src.utils.config_loader import get_ticker_names, apply_ticker_names
# =============================================================================
# CONFIGURATION & STYLE
# =============================================================================
st.set_page_config(
page_title="AlphaEdge Dashboard",
layout="wide",
initial_sidebar_state="expanded"
)
st_autorefresh(interval=900000, key="datarefresh")
BASE_DIR = Path(__file__).resolve().parent
HF_REPO_ID = os.getenv("HF_REPO_ID", "soradata/alphaedge-data")
MLFLOW_TRACKING_URI = "https://soradata-alphaedge-registry.hf.space"
HF_TOKEN = os.getenv("HF_TOKEN")
MLFLOW_ENABLED = bool(HF_TOKEN)
if MLFLOW_ENABLED:
os.environ["MLFLOW_TRACKING_USERNAME"] = "SORADATA"
os.environ["MLFLOW_TRACKING_PASSWORD"] = HF_TOKEN
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
MODEL_DIR = BASE_DIR / "models"
# Chargement du style CSS personnalisé
load_css()
def _load_fallback_markets(base_dir: Path) -> list:
cfg_path = base_dir / "config" / "markets.json"
if cfg_path.exists():
with open(cfg_path) as f:
return json.load(f)
return []
MARKET_OPTIONS = discover_markets(
repo_id=HF_REPO_ID,
token=HF_TOKEN,
local_dir=BASE_DIR / "data" / "processed",
fallback=_load_fallback_markets(BASE_DIR),
)
if not MARKET_OPTIONS:
st.error("Aucun marché disponible : vérifiez HF_REPO_ID, HF_TOKEN, data/processed, ou config/markets.json.")
st.stop()
# =============================================================================
# SIDEBAR
# =============================================================================
st.sidebar.title("AlphaEdge")
st.sidebar.caption("Quantitative Asset Allocation")
selected_market = st.sidebar.selectbox("Marché", MARKET_OPTIONS, index=0)
with st.spinner(f"Loading {selected_market} data..."):
df_hist, df_signals, df_rebalance, load_errors = load_all_data(selected_market, HF_REPO_ID)
if st.sidebar.button("Force Sync Pipeline"):
st.cache_data.clear()
st.rerun()
st.sidebar.markdown(
"[](https://github.com/SORADATA/CAC40-Quantitative-Analysis-Predictive-Asset-Allocation)"
)
st.sidebar.markdown("---")
page = st.sidebar.radio("Navigation", [
"Dashboard",
"Daily Signals",
"Data Explorer",
"Model Details",
"Rebalance History"
])
st.sidebar.markdown("---")
if not df_hist.empty:
last_dt = df_hist.index[-1]
days_old = (datetime.now() - last_dt).days
if days_old <= 1:
status_icon, status_text = "🟢", "System Online"
elif days_old <= 3:
status_icon, status_text = "🟡", "Data Slightly Old"
else:
status_icon, status_text = "🔴", "Data Outdated"
st.sidebar.info(f"Last Update: {last_dt.date()}")
st.sidebar.markdown(f"{status_icon} {status_text}")
else:
st.sidebar.error("No Data Available")
st.sidebar.markdown("---")
ticker_val_path = BASE_DIR / "config" / selected_market / "ticker_validation.json"
if not ticker_val_path.exists():
ticker_val_path = BASE_DIR / "ticker_validation.json"
if ticker_val_path.exists():
with st.sidebar.expander("Ticker Health", expanded=False):
try:
with open(ticker_val_path, "r") as f:
validation = json.load(f)
alerts = validation.get("alerts", {})
if alerts.get("delisted"):
st.error(f"{len(alerts['delisted'])} delistés")
if alerts.get("stale"):
st.warning(f"{len(alerts['stale'])} obsolètes")
if alerts.get("warnings"):
st.info(f"{len(alerts['warnings'])} warnings")
st.metric("Valid Tickers", validation.get("valid_tickers", 0))
except Exception:
st.caption("Validation data unavailable")
st.sidebar.markdown("---")
if load_errors:
with st.sidebar.expander("Data Issues", expanded=False):
for err in load_errors:
st.warning(err, icon="⚠️")
if not MLFLOW_ENABLED:
st.sidebar.caption("HF_TOKEN absent : métriques modèle en mode local uniquement.")
st.sidebar.caption("Disclaimer: Not financial advice.")
# =============================================================================
# PAGE 1 : DASHBOARD
# =============================================================================
if page == "Dashboard":
st.title(f"Portfolio Overview - {selected_market}")
if not df_hist.empty:
tot_ret, alpha, sharpe, max_dd, recovery_days = calculate_metrics(df_hist)
c1, c2, c3, c4, c5 = st.columns(5)
with c1:
display_kpi_card("Total Return", tot_ret, color_code=True)
with c2:
display_kpi_card("Alpha vs Bench", alpha, color_code=True)
with c3:
display_kpi_card("Sharpe Ratio", sharpe, is_percent=False)
with c4:
display_kpi_card("Max Drawdown", max_dd, color_code=True)
with c5:
display_kpi_card("Recovery Time", recovery_days, is_percent=False, suffix=" j", minimal=False)
st.markdown("<br>", unsafe_allow_html=True)
st.subheader("Period Performance")
k1, k2, k3, k4, k5 = st.columns(5)
with k1:
display_kpi_card("YTD", calculate_period_return(df_hist, ytd=True), color_code=True, minimal=True)
with k2:
display_kpi_card("6 Months", calculate_period_return(df_hist, days=180), color_code=True, minimal=True)
with k3:
display_kpi_card("3 Months", calculate_period_return(df_hist, days=90), color_code=True, minimal=True)
with k4:
display_kpi_card("1 Month", calculate_period_return(df_hist, days=30), color_code=True, minimal=True)
with k5:
display_kpi_card("Daily Return", calculate_period_return(df_hist, daily=True), color_code=True, minimal=True)
st.markdown("---")
col_title, col_filter = st.columns([2, 1])
with col_title:
st.subheader("Strategy vs Benchmark")
with col_filter:
p_sel = st.radio("Zoom:", ["1M", "3M", "6M", "YTD", "1Y", "ALL"], index=5, horizontal=True, label_visibility="collapsed")
df_c = trim_flat_start(df_hist)
end = df_c.index[-1]
zoom_map = {"1M": 30, "3M": 90, "6M": 180, "1Y": 365}
if p_sel in zoom_map:
start = end - timedelta(days=zoom_map[p_sel])
elif p_sel == "YTD":
start = datetime(end.year, 1, 1)
else:
start = df_c.index[0]
start = max(start, df_c.index[0])
df_c = df_c[df_c.index >= pd.Timestamp(start)]
df_base = df_c.apply(lambda x: x / x.iloc[0] * 100)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df_base.index, y=df_base["Benchmark"], mode="lines", name="Benchmark", line=dict(color="#8b92a5", width=1.3, dash="dot"), hovertemplate="Benchmark: %{y:.1f}<extra></extra>"))
fig.add_trace(go.Scatter(x=df_base.index, y=df_base["Strategy"], mode="lines", name="Strategy", line=dict(color="#2ED9A0", width=2), hovertemplate="Strategy: %{y:.1f}<extra></extra>"))
fig.update_layout(template="plotly_white", plot_bgcolor="#11151c", paper_bgcolor="#11151c", font=dict(color="#c9ced6", size=12), margin=dict(l=0, r=0, t=30, b=0), height=400, hovermode="x unified", legend=dict(orientation="h", y=1.12, x=1, xanchor="right", bgcolor="rgba(0,0,0,0)", title=None, font=dict(size=12)), xaxis=dict(showgrid=False, showline=True, linecolor="#2a2f3a", ticks="outside", tickcolor="#2a2f3a"), yaxis=dict(title="Indexed Value (Base 100)", title_font=dict(size=11, color="#8b92a5"), showgrid=True, gridcolor="rgba(255,255,255,0.06)", zeroline=False, showline=False))
st.plotly_chart(fig, use_container_width=True)
st.markdown("---")
c_risk, c_pie = st.columns([3, 2])
with c_risk:
st.subheader("Risk Analysis")
s_ret = df_c["Strategy"].pct_change().dropna()
cum = (1 + s_ret).cumprod()
dd = (cum - cum.cummax()) / cum.cummax()
fig_dd = go.Figure()
fig_dd.add_trace(go.Scatter(x=dd.index, y=dd, fill="tozeroy", mode="lines", line=dict(color="#EF553B", width=1.5), name="Drawdown", fillcolor="rgba(239, 85, 59, 0.3)"))
fig_dd.update_layout(template="plotly_dark", margin=dict(l=0, r=0, t=10, b=0), height=320, yaxis_tickformat=".1%", yaxis_title="Drawdown")
st.plotly_chart(fig_dd, use_container_width=True)
with c_pie:
st.subheader("Current Allocation")
if not df_signals.empty and "Allocation" in df_signals.columns:
ticker_names = get_ticker_names(selected_market, BASE_DIR)
active = df_signals[df_signals["Allocation"] > 0.001].copy()
active = apply_ticker_names(active, ticker_names)
cash = max(0, 1.0 - active["Allocation"].sum())
if cash > 0.001:
final = pd.concat([active, pd.DataFrame([{"Ticker": "CASH", "Name": "CASH", "Allocation": cash}])], ignore_index=True)
else:
final = active
fig_p = px.pie(final, values="Allocation", names="Name", hole=0.5, color_discrete_sequence=px.colors.qualitative.Prism)
fig_p.update_traces(textposition="outside", textinfo="percent+label")
fig_p.update_layout(template="plotly_dark", margin=dict(l=20, r=20, t=0, b=0), showlegend=False, height=370)
st.plotly_chart(fig_p, use_container_width=True)
else:
st.info("Waiting for signals...")
# =============================================================================
# PAGE 2 : DAILY SIGNALS
# =============================================================================
elif page == "Daily Signals":
st.title(f"Daily Trading Signals - {selected_market}")
if not df_signals.empty:
ticker_names = get_ticker_names(selected_market, BASE_DIR)
d = apply_ticker_names(df_signals, ticker_names)
if "Allocation" in d.columns:
d = d.sort_values("Allocation", ascending=False)
col_filter1, col_filter2 = st.columns([1, 3])
with col_filter1:
filter_opt = st.selectbox("Filter", ["All Signals", "BUY Only", "NEUTRAL Only"])
if filter_opt == "BUY Only":
d = d[d["Signal"] == "BUY"]
elif filter_opt == "NEUTRAL Only":
d = d[d["Signal"] == "NEUTRAL"]
st.dataframe(
d, use_container_width=True, height=600, hide_index=True,
column_config={
"Allocation": st.column_config.ProgressColumn("Weight", format="%.2f", min_value=0, max_value=1),
"Proba_Hausse": st.column_config.NumberColumn("Probability Up", format="%.1f%%")
}
)
st.markdown("---")
col_s1, col_s2, col_s3 = st.columns(3)
with col_s1:
st.metric("Total Tickers", len(df_signals))
with col_s2:
n_buy = len(df_signals[df_signals["Signal"] == "BUY"]) if "Signal" in df_signals.columns else 0
st.metric("BUY Signals", n_buy)
with col_s3:
alloc_total = df_signals["Allocation"].sum() if "Allocation" in df_signals.columns else 0
st.metric("Total Allocated", f"{alloc_total:.1%}")
else:
st.info(f"No signals available for {selected_market}.")
# =============================================================================
# PAGE 3 : DATA EXPLORER
# =============================================================================
elif page == "Data Explorer":
st.title("Market Data Explorer")
default_tickers = ["AI.PA", "AIR.PA", "BNP.PA", "MC.PA", "OR.PA", "TTE.PA"]
tickers = df_signals["Ticker"].unique().tolist() if not df_signals.empty and "Ticker" in df_signals.columns else default_tickers
ticker_names = get_ticker_names(selected_market, BASE_DIR)
def format_ticker(t):
name = ticker_names.get(t)
return f"{t} — {name}" if name else t
col_sel1, col_sel2 = st.columns([1, 3])
with col_sel1:
selected_ticker = st.selectbox("Select Asset", tickers, index=0, format_func=format_ticker)
currency_code, currency_symbol = get_ticker_currency(selected_ticker)
st.caption(f"Devise : {currency_code}")
with col_sel2:
period_exp = st.selectbox("Timeframe", ["1 Month", "3 Months", "6 Months", "1 Year", "5 Years"], index=2)
yf_period_map = {"1 Month": "1mo", "3 Months": "3mo", "6 Months": "6mo", "1 Year": "1y", "5 Years": "5y"}
with st.spinner(f"Downloading {selected_ticker}..."):
df_asset = get_live_ticker_data(selected_ticker, period=yf_period_map[period_exp])
if not df_asset.empty and len(df_asset) > 1:
try:
last_close = df_asset["adj close"].iloc[-1]
prev_close = df_asset["adj close"].iloc[-2]
daily_var = (last_close / prev_close) - 1
total_ret_period = (last_close / df_asset["adj close"].iloc[0]) - 1
volatility = df_asset["adj close"].pct_change().dropna().std() * np.sqrt(252)
except Exception:
last_close = daily_var = total_ret_period = volatility = 0
m1, m2, m3, m4 = st.columns(4)
with m1:
display_kpi_card("Last Price", last_close, is_percent=False, prefix=f"{currency_symbol} ")
with m2:
display_kpi_card("Daily Change", daily_var, color_code=True)
with m3:
display_kpi_card(f"Return ({period_exp})", total_ret_period, color_code=True)
with m4:
display_kpi_card("Ann. Volatility", volatility, is_percent=True)
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.05, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(
x=df_asset.index, open=df_asset["open"], high=df_asset["high"],
low=df_asset["low"], close=df_asset["close"], name="OHLC"
), row=1, col=1)
colors = ["#00CC96" if r >= 0 else "#EF553B" for r in df_asset["adj close"].pct_change().fillna(0)]
fig.add_trace(go.Bar(x=df_asset.index, y=df_asset["volume"], name="Volume", marker_color=colors), row=2, col=1)
fig.update_layout(template="plotly_dark", xaxis_rangeslider_visible=False, height=550, margin=dict(l=0, r=0, t=30, b=0), showlegend=False)
st.plotly_chart(fig, use_container_width=True)
else:
st.warning(f"No data for {selected_ticker}")
# =============================================================================
# PAGE 4 : MODEL DETAILS
# =============================================================================
elif page == "Model Details":
st.title(f"Model Configuration - {selected_market}")
tab1, tab2 = st.tabs(["Performance", "Clusters"])
with tab1:
st.markdown("""
### Hybrid Strategy Components
**1. XGBoost + LightGBM + Ridge -> LogisticRegression**: Predicts 1-month upside probability
**2. K-Means**: Market regime detection (RSI-based)
**3. Markowitz**: Portfolio optimization (Max Sharpe)
""")
st.markdown("---")
champ = get_champion_metrics(
selected_market,
model_dir=MODEL_DIR,
mlflow_enabled=MLFLOW_ENABLED,
)
if champ["source"] == "mlflow":
st.markdown(
'<span class="mlflow-badge badge-champion">MLflow - Champion v'
f'{champ["version"]}</span>', unsafe_allow_html=True
)
elif champ["source"] == "local":
status = "promu champion" if champ.get("promoted") else "dernier run (non promu - seuils non atteints)"
st.markdown(
f'<span class="mlflow-badge badge-fallback">Fallback local - {status}</span>',
unsafe_allow_html=True
)
else:
st.info("Aucune métrique disponible pour ce marché (ni MLflow, ni model_card.json local).")
if champ["metrics"]:
st.markdown("### Model Performance")
metric_items = list(champ["metrics"].items())
cols = st.columns(4)
for i, (name, val) in enumerate(metric_items):
try:
formatted = f"{val:.4f}"
except (TypeError, ValueError):
formatted = str(val)
cols[i % 4].metric(name.replace("_", " ").title(), formatted)
if champ["error"]:
with st.expander("Détails techniques (debug)"):
st.caption(champ["error"])
st.markdown("---")
st.markdown("### Feature Importance")
st.caption("Explicabilité du modèle XGBoost — variables les plus influentes sur la prédiction.")
feat_imp = None
if isinstance(champ.get("metrics"), dict):
feat_imp = champ["metrics"].get("feature_importance")
if feat_imp:
fi_df = pd.DataFrame(list(feat_imp.items()), columns=["Feature", "Importance"]).sort_values("Importance", ascending=True)
else:
demo_features = ["momentum_3m", "rsi_14", "volume_lag1", "pe_ratio", "book_to_market",
"volatility_60d", "ff_smb", "ff_hml", "earnings_yield", "beta_1y"]
rng = np.random.default_rng(42)
demo_vals = np.sort(rng.uniform(0.02, 0.22, size=len(demo_features)))
fi_df = pd.DataFrame({"Feature": demo_features, "Importance": demo_vals})
st.caption("Données factices affichées à titre d'exemple — en attente de l'extraction réelle depuis le modèle XGBoost.")
fig_fi = px.bar(
fi_df, x="Importance", y="Feature", orientation="h",
color="Importance", color_continuous_scale=px.colors.sequential.Tealgrn
)
fig_fi.update_layout(template="plotly_dark", height=400, margin=dict(l=0, r=0, t=10, b=0), showlegend=False, coloraxis_showscale=False)
st.plotly_chart(fig_fi, use_container_width=True)
with tab2:
st.subheader("Cluster Analysis")
st.markdown("Segmentation based on RSI to identify momentum vs reversal regimes.")
if not df_signals.empty and "RSI" in df_signals.columns and "Return_3M" in df_signals.columns:
fig = px.scatter(
df_signals, x="RSI", y="Return_3M", color="Cluster", hover_name="Ticker",
color_continuous_scale=px.colors.sequential.Viridis,
labels={"RSI": "RSI (20)", "Return_3M": "3-Month Momentum"}
)
fig.update_layout(template="plotly_dark", height=500)
st.plotly_chart(fig, use_container_width=True)
else:
st.warning("No cluster data available")
# =============================================================================
# PAGE 5 : REBALANCE HISTORY
# =============================================================================
elif page == "Rebalance History":
st.title(f"Monthly Rebalancing History - {selected_market}")
if not df_rebalance.empty:
st.markdown("""
This page shows the **monthly rebalancing decisions** made by the strategy.
Each month, the model selects new assets and recalculates optimal weights.
""")
col_r1, col_r2, col_r3 = st.columns(3)
with col_r1:
st.metric("Total Rebalances", len(df_rebalance))
with col_r2:
avg_stocks = df_rebalance["N_Stocks"].mean() if "N_Stocks" in df_rebalance.columns else 0
st.metric("Avg. Stocks/Month", f"{avg_stocks:.1f}")
with col_r3:
last_rebal = df_rebalance.index[0].date() if len(df_rebalance) > 0 else "N/A"
st.metric("Last Rebalance", str(last_rebal))
st.markdown("---")
if "N_Stocks" in df_rebalance.columns:
st.subheader("Portfolio Size Evolution")
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_rebalance.index, y=df_rebalance["N_Stocks"],
mode="lines+markers", name="N Stocks",
line=dict(color="#00CC96", width=2), marker=dict(size=6)
))
fig.update_layout(template="plotly_dark", height=350, yaxis_title="Number of Stocks", xaxis_title="Date")
st.plotly_chart(fig, use_container_width=True)
st.subheader("Detailed Rebalancing Log")
st.dataframe(df_rebalance, use_container_width=True, height=400)
else:
st.info(f"No rebalance history available for {selected_market}.")
# =============================================================================
# DISCLAIMER FOOTER
# =============================================================================
st.markdown("---")
st.markdown("""
<div class="disclaimer-box">
<div class="disclaimer-title">AVIS DE NON-RESPONSABILITÉ</div>
<p>Les informations présentées sur ce tableau de bord sont fournies <strong>à titre informatif et éducatif uniquement</strong>. Elles ne constituent en aucun cas un conseil en investissement.</p>
<p><strong>Risques :</strong> Tout investissement comporte des risques. Les performances passées ne garantissent pas les résultats futurs.</p>
<p><strong>Responsabilité :</strong> Consultez un conseiller financier agréé avant toute décision d'investissement.</p>
</div>
""", unsafe_allow_html=True)