Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -91,32 +91,48 @@ for company in companies:
|
|
| 91 |
df_c['Day'] = df_c['date'].dt.date
|
| 92 |
df_c['Month'] = df_c['date'].dt.to_period('M').dt.to_timestamp()
|
| 93 |
df_c['Year'] = df_c['date'].dt.year
|
| 94 |
-
|
| 95 |
# Strategy A: Custom Sentiment
|
| 96 |
df_c['StrategyA_Cumulative'] = 0.0
|
| 97 |
for i in range(1, len(df_c)):
|
| 98 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
|
|
|
|
|
| 99 |
if df_c.loc[i, 'Sentiment'] == "UP" and df_c.loc[i,'Confidence'] > 0.8:
|
| 100 |
-
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative'] + pct
|
| 101 |
elif df_c.loc[i, 'Sentiment'] == "DOWN" and df_c.loc[i,'Confidence'] > 0.8:
|
| 102 |
-
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative'] - pct
|
| 103 |
else:
|
| 104 |
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
df_c['StrategyB_Cumulative'] = (df_c['Predicted'] * df_c['PctChangeDaily']).cumsum()
|
| 108 |
-
|
| 109 |
# Strategy C: FinBERT
|
| 110 |
df_c['StrategyC_Cumulative'] = 0.0
|
| 111 |
for i in range(1, len(df_c)):
|
| 112 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
|
|
|
|
|
| 113 |
if df_c.loc[i, 'FinBERT_Sentiment'] == "POSITIVE" and df_c.loc[i,'FinBERT_Confidence'] > 0.8:
|
| 114 |
-
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative'] + pct
|
| 115 |
elif df_c.loc[i, 'FinBERT_Sentiment'] == "NEGATIVE" and df_c.loc[i,'FinBERT_Confidence'] > 0.8:
|
| 116 |
-
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative'] - pct
|
| 117 |
else:
|
| 118 |
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative']
|
| 119 |
|
|
|
|
| 120 |
dfs_final[company] = df_c.drop(columns=["date", "date_merge"], errors="ignore")
|
| 121 |
|
| 122 |
# --- FUNZIONE PER GRADIO ---
|
|
|
|
| 91 |
df_c['Day'] = df_c['date'].dt.date
|
| 92 |
df_c['Month'] = df_c['date'].dt.to_period('M').dt.to_timestamp()
|
| 93 |
df_c['Year'] = df_c['date'].dt.year
|
| 94 |
+
|
| 95 |
# Strategy A: Custom Sentiment
|
| 96 |
df_c['StrategyA_Cumulative'] = 0.0
|
| 97 |
for i in range(1, len(df_c)):
|
| 98 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
| 99 |
+
price = df_c.loc[i-1, f'Close_{TICKERS[c]}'] # prezzo di acquisto del giorno precedente
|
| 100 |
+
|
| 101 |
if df_c.loc[i, 'Sentiment'] == "UP" and df_c.loc[i,'Confidence'] > 0.8:
|
| 102 |
+
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative'] + price * pct
|
| 103 |
elif df_c.loc[i, 'Sentiment'] == "DOWN" and df_c.loc[i,'Confidence'] > 0.8:
|
| 104 |
+
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative'] - price * pct
|
| 105 |
else:
|
| 106 |
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative']
|
| 107 |
+
# Strategy B: Regression (buy if >1, sell if <1)
|
| 108 |
+
df_c['StrategyB_Cumulative'] = 0.0
|
| 109 |
+
for i in range(1, len(df_c)):
|
| 110 |
+
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
| 111 |
+
price = df_c.loc[i-1, f'Close_{TICKERS[c]}']
|
| 112 |
+
predicted = df_c.loc[i, 'Predicted']
|
| 113 |
+
|
| 114 |
+
if predicted > 1:
|
| 115 |
+
df_c.loc[i,'StrategyB_Cumulative'] = df_c.loc[i-1,'StrategyB_Cumulative'] + price * pct
|
| 116 |
+
elif predicted < 1:
|
| 117 |
+
df_c.loc[i,'StrategyB_Cumulative'] = df_c.loc[i-1,'StrategyB_Cumulative'] - price * pct
|
| 118 |
+
else:
|
| 119 |
+
df_c.loc[i,'StrategyB_Cumulative'] = df_c.loc[i-1,'StrategyB_Cumulative']
|
| 120 |
|
| 121 |
+
|
|
|
|
|
|
|
| 122 |
# Strategy C: FinBERT
|
| 123 |
df_c['StrategyC_Cumulative'] = 0.0
|
| 124 |
for i in range(1, len(df_c)):
|
| 125 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
| 126 |
+
price = df_c.loc[i-1, f'Close_{TICKERS[c]}']
|
| 127 |
+
|
| 128 |
if df_c.loc[i, 'FinBERT_Sentiment'] == "POSITIVE" and df_c.loc[i,'FinBERT_Confidence'] > 0.8:
|
| 129 |
+
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative'] + price * pct
|
| 130 |
elif df_c.loc[i, 'FinBERT_Sentiment'] == "NEGATIVE" and df_c.loc[i,'FinBERT_Confidence'] > 0.8:
|
| 131 |
+
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative'] - price * pct
|
| 132 |
else:
|
| 133 |
df_c.loc[i,'StrategyC_Cumulative'] = df_c.loc[i-1,'StrategyC_Cumulative']
|
| 134 |
|
| 135 |
+
|
| 136 |
dfs_final[company] = df_c.drop(columns=["date", "date_merge"], errors="ignore")
|
| 137 |
|
| 138 |
# --- FUNZIONE PER GRADIO ---
|