Update app.py
Browse files
app.py
CHANGED
|
@@ -123,11 +123,12 @@ for company in companies:
|
|
| 123 |
# --- FUNZIONE PER GRADIO ---
|
| 124 |
def show_company_data(selected_companies, aggregation="Day"):
|
| 125 |
if not selected_companies:
|
| 126 |
-
return "Seleziona almeno un'azienda", None
|
| 127 |
|
| 128 |
-
fig = go.Figure()
|
| 129 |
agg_col = {"Day": "Day", "Month": "Month", "Year": "Year"}.get(aggregation, "Day")
|
| 130 |
|
|
|
|
|
|
|
| 131 |
for c in selected_companies:
|
| 132 |
if c not in dfs_final:
|
| 133 |
continue
|
|
@@ -136,47 +137,73 @@ def show_company_data(selected_companies, aggregation="Day"):
|
|
| 136 |
df_grouped = df_c.groupby(agg_col).agg({
|
| 137 |
'StrategyA_Cumulative':'last',
|
| 138 |
'StrategyB_Cumulative':'last',
|
| 139 |
-
'StrategyC_Cumulative':'last'
|
| 140 |
-
f'Close_{TICKERS[c]}':'last'
|
| 141 |
}).reset_index()
|
| 142 |
|
| 143 |
-
# Strategie
|
| 144 |
for strat in ['StrategyA_Cumulative','StrategyB_Cumulative','StrategyC_Cumulative']:
|
| 145 |
-
|
| 146 |
x=df_grouped[agg_col], y=df_grouped[strat],
|
| 147 |
mode="lines", name=f"{c} {strat}"
|
| 148 |
))
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
x=df_grouped[agg_col], y=df_grouped[f'Close_{TICKERS[c]}'],
|
| 153 |
-
mode="lines", name=f"{c} Price"
|
| 154 |
))
|
| 155 |
|
| 156 |
-
|
| 157 |
-
title="
|
| 158 |
xaxis_title=aggregation,
|
| 159 |
-
yaxis_title="
|
| 160 |
-
yaxis2=dict(title="Stock Price", overlaying="y", side="right"),
|
| 161 |
-
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 162 |
template="plotly_dark",
|
| 163 |
hovermode="x unified"
|
| 164 |
)
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
# --- INTERFACCIA GRADIO ---
|
| 168 |
demo = gr.Interface(
|
| 169 |
fn=show_company_data,
|
| 170 |
inputs=[
|
| 171 |
-
gr.Dropdown(
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
],
|
| 174 |
outputs=[
|
| 175 |
-
gr.Dataframe(label="Preview", type="pandas"),
|
| 176 |
-
gr.Plot(label="
|
|
|
|
| 177 |
],
|
| 178 |
title="Portfolio Evolution",
|
| 179 |
-
description="
|
| 180 |
)
|
| 181 |
|
| 182 |
demo.launch()
|
|
|
|
|
|
| 123 |
# --- FUNZIONE PER GRADIO ---
|
| 124 |
def show_company_data(selected_companies, aggregation="Day"):
|
| 125 |
if not selected_companies:
|
| 126 |
+
return "Seleziona almeno un'azienda", None, None
|
| 127 |
|
|
|
|
| 128 |
agg_col = {"Day": "Day", "Month": "Month", "Year": "Year"}.get(aggregation, "Day")
|
| 129 |
|
| 130 |
+
# --- Grafico Strategie ---
|
| 131 |
+
fig_strat = go.Figure()
|
| 132 |
for c in selected_companies:
|
| 133 |
if c not in dfs_final:
|
| 134 |
continue
|
|
|
|
| 137 |
df_grouped = df_c.groupby(agg_col).agg({
|
| 138 |
'StrategyA_Cumulative':'last',
|
| 139 |
'StrategyB_Cumulative':'last',
|
| 140 |
+
'StrategyC_Cumulative':'last'
|
|
|
|
| 141 |
}).reset_index()
|
| 142 |
|
|
|
|
| 143 |
for strat in ['StrategyA_Cumulative','StrategyB_Cumulative','StrategyC_Cumulative']:
|
| 144 |
+
fig_strat.add_trace(go.Scatter(
|
| 145 |
x=df_grouped[agg_col], y=df_grouped[strat],
|
| 146 |
mode="lines", name=f"{c} {strat}"
|
| 147 |
))
|
| 148 |
|
| 149 |
+
fig_strat.update_layout(
|
| 150 |
+
title="Confronto Strategie (Sentiment, Regression, FinBERT)",
|
| 151 |
+
xaxis_title=aggregation,
|
| 152 |
+
yaxis_title="Cumulative",
|
| 153 |
+
template="plotly_dark",
|
| 154 |
+
hovermode="x unified"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# --- Grafico Prezzi ---
|
| 158 |
+
fig_price = go.Figure()
|
| 159 |
+
for c in selected_companies:
|
| 160 |
+
if c not in dfs_final:
|
| 161 |
+
continue
|
| 162 |
+
df_c = dfs_final[c]
|
| 163 |
+
|
| 164 |
+
df_grouped = df_c.groupby(agg_col).agg({
|
| 165 |
+
f'Close_{TICKERS[c]}':'last'
|
| 166 |
+
}).reset_index()
|
| 167 |
+
|
| 168 |
+
fig_price.add_trace(go.Scatter(
|
| 169 |
x=df_grouped[agg_col], y=df_grouped[f'Close_{TICKERS[c]}'],
|
| 170 |
+
mode="lines", name=f"{c} Price"
|
| 171 |
))
|
| 172 |
|
| 173 |
+
fig_price.update_layout(
|
| 174 |
+
title="Andamento Prezzi",
|
| 175 |
xaxis_title=aggregation,
|
| 176 |
+
yaxis_title="Stock Price",
|
|
|
|
|
|
|
| 177 |
template="plotly_dark",
|
| 178 |
hovermode="x unified"
|
| 179 |
)
|
| 180 |
+
|
| 181 |
+
return None, fig_strat, fig_price
|
| 182 |
|
| 183 |
# --- INTERFACCIA GRADIO ---
|
| 184 |
demo = gr.Interface(
|
| 185 |
fn=show_company_data,
|
| 186 |
inputs=[
|
| 187 |
+
gr.Dropdown(
|
| 188 |
+
choices=companies,
|
| 189 |
+
value=["Microsoft", "Tesla"],
|
| 190 |
+
multiselect=True,
|
| 191 |
+
label="Select Companies"
|
| 192 |
+
),
|
| 193 |
+
gr.Radio(
|
| 194 |
+
choices=["Day", "Month", "Year"],
|
| 195 |
+
value="Day",
|
| 196 |
+
label="Aggregation Level"
|
| 197 |
+
)
|
| 198 |
],
|
| 199 |
outputs=[
|
| 200 |
+
gr.Dataframe(label="Data Preview", type="pandas"),
|
| 201 |
+
gr.Plot(label="Strategies"),
|
| 202 |
+
gr.Plot(label="Prices")
|
| 203 |
],
|
| 204 |
title="Portfolio Evolution",
|
| 205 |
+
description="Compare Custom Sentiment, Regression, and FinBERT strategies alongside stock prices."
|
| 206 |
)
|
| 207 |
|
| 208 |
demo.launch()
|
| 209 |
+
|