-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinAIc.py
More file actions
302 lines (248 loc) · 12.9 KB
/
FinAIc.py
File metadata and controls
302 lines (248 loc) · 12.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
import re
import finnhub
from ollama import chat, ChatResponse
import pandas as pd
# API-Key & Parameter
API_KEY = "YOUR FINNHUB API HERE"
# Dynamically generate the system prompt
def build_system_prompt(symbol: str) -> dict:
return {
'role': 'system',
'content': (
f"You are a professional sentiment analysis assistant working for a high-stakes finance firm. "
f"Your job is to evaluate headlines and summaries related to stocks and determine the investment sentiment "
f"specifically for the company with ticker symbol '{symbol}'. "
f"Your analysis must be precise and consistent – your performance directly affects the firm's success. "
f"Always assess the sentiment strictly from the perspective of '{symbol}', even if the news mentions competitors, industry trends, or general market conditions. "
f"Respond with one numeric value only:\n"
f"- Between 0.5 and 1 for positive sentiment (buy),\n"
f"- Between -1 and -0.5 for negative sentiment (sell),\n"
f"- Between -0.5 and 0.5 for neutral sentiment (hold).\n"
f"If the headline and summary contain no meaningful or relevant information about '{symbol}', return exactly 2.\n"
f"Do not explain your reasoning. Just return the number."
)
}
# Remove hidden <think> tags
def clean(text: str) -> str:
return re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
# Sentiment Analysis with Ollama
def analyze(text: str, symbol: str) -> float:
system_prompt = build_system_prompt(symbol)
response: ChatResponse = chat(
model='gemma3:27b',
messages=[system_prompt, {'role': 'user', 'content': text}]
)
try:
return float(clean(response.message.content))
except ValueError:
print(f"⚠️ Invalid value: {response.message.content}")
return 0.0
# Evaluate Fundamentals
def evaluate_fundamentals(data: dict) -> float:
score = 0.0
weight_total = 0.0
try:
current_ratio = data["series"]["annual"]["currentRatio"][0]["v"]
score += 1.0 if current_ratio >= 1.5 else 0.5 if current_ratio >= 1.0 else -0.5
weight_total += 1
sales = data["series"]["annual"]["salesPerShare"]
if len(sales) >= 2:
growth = sales[0]["v"] - sales[1]["v"]
score += 1.0 if growth > 0 else -0.5
weight_total += 1
net_margin = data["series"]["annual"]["netMargin"][0]["v"]
score += 1.0 if net_margin >= 0.2 else 0.5 if net_margin >= 0.1 else -0.5
weight_total += 1
price_return = data["metric"]["52WeekPriceReturnDaily"]
score += 1.0 if price_return > 50 else 0.5 if price_return > 0 else -0.5
weight_total += 1
beta = data["metric"]["beta"]
score += 0.5 if beta < 1.2 else -0.5 if beta > 1.5 else 0.0
weight_total += 1
except (KeyError, IndexError, TypeError):
print("⚠️ Errors in the evaluation of fundamental data.")
return round(score / weight_total, 2) if weight_total > 0 else 0.0
# Evaluate Analyst Recommendations
def evaluate_recommendation_trends(data: list[dict]) -> float:
if not data:
return 0.0
total_score = 0.0
total_periods = 0
for entry in data:
pos = entry.get("strongBuy", 0) + entry.get("buy", 0)
neutral = entry.get("hold", 0)
neg = entry.get("sell", 0) + entry.get("strongSell", 0)
total = pos + neutral + neg
if total == 0:
continue
score = (pos - neg) / total
total_score += score
total_periods += 1
return round(total_score / total_periods, 2) if total_periods > 0 else 0.0
# Combined Recommendation
def recommend_combined(sentiment_scores_filtered: list[float], fundamental_score: float, analyst_score: float) -> str:
if not sentiment_scores_filtered:
return "⚠️ No valid sentiment values available."
sentiment_avg = sum(sentiment_scores_filtered) / len(sentiment_scores_filtered)
combined_score = (sentiment_avg * 0.5) + (fundamental_score * 0.3) + (analyst_score * 0.2)
print(f"\n🧮 Sentiment average: {sentiment_avg:.2f}")
print(f"📊 Fundamentals score: {fundamental_score:.2f}")
print(f"🗣️ Analyst score: {analyst_score:.2f}")
print(f"🔗 Combined score: {combined_score:.2f}")
if combined_score > 0.5:
return "📈 Buy"
elif combined_score < 0.3:
return "📉 Sell"
return "⏸️ Hold"
# Save Results to File
def save_results(symbol, start_date, end_date, news_items, sentiment_scores_all, sentiment_scores_filtered, f_score, analyst_score, recommendation, filename):
with open(filename, "w", encoding="utf-8") as f:
f.write(f"🔎 Analyze {symbol} from {start_date} to {end_date}\n\n")
for i, item in enumerate(news_items):
headline = item.get("headline", "")
summary = item.get("summary", "")
score = sentiment_scores_all[i]
f.write(f"📰 {headline}\n📝 {summary}\n📊 Sentiment: {score:.2f}\n{'-'*50}\n")
sentiment_avg = sum(sentiment_scores_filtered) / len(sentiment_scores_filtered) if sentiment_scores_filtered else 0
combined_score = sentiment_avg * 0.5 + f_score * 0.3 + analyst_score * 0.2
f.write(f"\n🧮 Sentiment average: {sentiment_avg:.2f}\n")
f.write(f"📊 Fundamentals score: {f_score:.2f}\n")
f.write(f"🗣️ Analyst score: {analyst_score:.2f}\n")
f.write(f"🔗Combined score: {combined_score:.2f}\n")
f.write(f"\n{recommendation}\n")
# Multi-Symbol Main Function
def main_multi(symbols: list[str], start_date: str, end_date: str):
"""
Runs the complete analysis for multiple stock symbols.
Steps:
1. Retrieves news, fundamentals, and analyst data from Finnhub.
2. Uses Ollama AI to analyze sentiment for each news item.
3. Calculates fundamentals and analyst scores.
4. Combines them into a final recommendation.
5. Saves detailed results and an overall Excel summary.
"""
client = finnhub.Client(api_key=API_KEY)
results = []
for symbol in symbols:
print(f"\n🔎 Analyzing news for {symbol} from {start_date} to {end_date}...\n")
try:
news_items = client.company_news(symbol, _from=start_date, to=end_date)
fundamentals = client.company_basic_financials(symbol, 'all')
analyst_data = client.recommendation_trends(symbol)
except Exception as e:
print(f"⚠️ Error for symbol '{symbol}': {e}")
print("⏭️ Skipping this symbol.\n" + "-"*60)
continue
analyst_score = evaluate_recommendation_trends(analyst_data)
sentiment_scores_all = []
sentiment_scores_filtered = []
for item in news_items:
headline = item.get("headline", "")
summary = item.get("summary", "")
text = f"{headline} — {summary}"
print(f"📰 {text}")
score = analyze(text, symbol)
sentiment_scores_all.append(score)
if -1 <= score <= 1:
sentiment_scores_filtered.append(score)
print(f"📊 Sentiment: {score}\n{'-'*50}")
f_score = evaluate_fundamentals(fundamentals)
recommendation = recommend_combined(sentiment_scores_filtered, f_score, analyst_score)
print(f"\n✅ {recommendation}")
filename = f"analysis_result_{symbol}_{end_date}.txt"
save_results(
symbol,
start_date,
end_date,
news_items,
sentiment_scores_all, # All scores for the file
sentiment_scores_filtered,
f_score,
analyst_score,
recommendation,
filename
)
print(f"📝 Results saved to '{filename}'.")
sentiment_avg = sum(sentiment_scores_filtered) / len(sentiment_scores_filtered) if sentiment_scores_filtered else 0.0
combined_score = sentiment_avg * 0.5 + f_score * 0.3 + analyst_score * 0.2
results.append({
"Symbol": symbol,
"Start Date": start_date,
"End Date": end_date,
"Sentiment Average": round(sentiment_avg, 2),
"Fundamentals Score": round(f_score, 2),
"Analyst Score": round(analyst_score, 2),
"Combined Score": round(combined_score, 2),
"Recommendation": recommendation
})
# Save Excel summary file
df = pd.DataFrame(results)
excel_filename = f"full_analysis_{end_date}.xlsx"
df.to_excel(excel_filename, index=False)
print(f"\n📊 Overall results saved to '{excel_filename}'")
# Run for different lists
if __name__ == "__main__":
START_DATE = "2025-07-19"
END_DATE = "2025-08-19"
NASDAQ_LIST = [
"AAPL", "MSFT", "GOOGL", "AMZN", "META", "NVDA", "TSLA", "AVGO", "NFLX",
"COST", "PLTR", "AMD", "CSCO", "ASML", "TMUS", "AZN", "LIN", "INTU", "PEP",
"BKNG", "ISRG", "TXN", "SHOP", "AMGN", "PDD", "QCOM", "ARM", "ADBE", "AMAT",
"GILD", "HON", "APP", "MELI", "LRCX", "MU", "ADP", "CMCSA", "KLAC", "SNPS",
"PANW", "ADI", "CRWD", "DASH", "CEG", "MSTR", "SBUX", "CDNS", "VRTX", "CTAS",
"REGN", "FTNT", "ROST", "MRVL", "IDXX", "TEAM", "ZS", "CHTR", "PAYX", "ODFL",
"BIIB", "EXC", "FAST", "XEL", "EA", "DXCM", "WBD", "MAR", "DLTR", "BIDU",
"NTES", "ALGN", "VRSK", "GEN", "PCAR", "ON", "KDP", "TTD", "LCID", "SIRI",
"VRSN", "SBAC", "CTSH", "CDW", "INCY", "NXPI", "MTCH", "TTWO", "VERI", "ZS",
"FISV", "OKTA", "ANSS", "META", "AEP", "EXPE", "WBA", "KHC", "MNST", "UAL"]
DOW_LIST = [
"MMM", "AXP", "AMGN", "AAPL", "BA", "CAT", "CVX", "CSCO", "KO", "DOW",
"GS", "HD", "HON", "IBM", "INTC", "JNJ", "JPM", "MCD", "MRK", "MSFT",
"NKE", "PG", "CRM", "TRV", "UNH", "VZ", "V", "WBA", "WMT", "DIS"]
SINGLE_SYMBOL = ['MSFT']
DIV_STRAT = ['MAIN', 'O', 'ABBV', 'OHI', 'VZ']
SP500 = [
"AAPL", "MSFT", "AMZN", "NVDA", "GOOGL", "GOOG", "META", "BRK.B", "TSLA", "UNH",
"LLY", "JPM", "XOM", "JNJ", "V", "PG", "AVGO", "MA", "HD", "CVX",
"MRK", "ABBV", "PEP", "COST", "ADBE", "KO", "CSCO", "WMT", "TMO", "MCD",
"PFE", "CRM", "BAC", "ACN", "CMCSA", "LIN", "NFLX", "ABT", "ORCL", "DHR",
"AMD", "WFC", "DIS", "TXN", "PM", "VZ", "INTU", "COP", "CAT", "AMGN",
"NEE", "INTC", "UNP", "LOW", "IBM", "BMY", "SPGI", "RTX", "HON", "BA",
"UPS", "GE", "QCOM", "AMAT", "NKE", "PLD", "GS", "ISRG", "MS", "NOW",
"ELV", "LMT", "MDT", "SYK", "SCHW", "TJX", "BKNG", "DE", "ADI", "BLK",
"MMC", "GILD", "MO", "AXP", "REGN", "VRTX", "C", "ZTS", "CB", "PGR",
"T", "BSX", "CI", "SLB", "CL", "SO", "BDX", "PNC", "ADP", "MU",
"USB", "EQIX", "SHW", "EOG", "DUK", "ITW", "CSX", "NSC", "FDX", "HUM",
"GM", "GD", "EMR", "WM", "ETN", "FCX", "APD", "MPC", "PSA", "ROP",
"COF", "AON", "MET", "MAR", "ORLY", "KDP", "AEP", "MCK", "FIS", "TRV",
"KHC", "MNST", "AFL", "ALL", "HCA", "MSI", "OXY", "CME", "SBUX", "SPG",
"KLAC", "CTAS", "AZO", "D", "PSX", "SNPS", "NOC", "NXPI", "CMG", "ADM",
"IDXX", "PAYX", "PH", "PRU", "HLT", "TGT", "DVN", "AIG", "WELL", "PCAR",
"BK", "ROST", "MSCI", "YUM", "CDNS", "FTNT", "CTVA", "LRCX", "F", "HPQ",
"GIS", "KR", "VLO", "ODFL", "KMB", "WMB", "PEG", "ED", "XEL", "PPG",
"MTD", "WEC", "DLR", "HSY", "FAST", "RSG", "EXC", "VRSK", "AVB", "MLM",
"LEN", "ECL", "PCG", "ANET", "WBD", "KEYS", "HES", "DHI", "CBRE", "FITB",
"AMP", "STZ", "OTIS", "ROK", "CTSH", "VICI", "AWK", "CNC", "ES", "GWW",
"DOW", "HIG", "RCL", "ZBH", "ALB", "CHTR", "FANG", "MTB", "LUV", "SWK",
"NUE", "VTR", "BLL", "EFX", "TSCO", "IR", "PPL", "FE", "CMS", "HPE",
"ABC", "CARR", "TDG", "EXR", "PAYC", "STE", "KMI", "ON", "TT", "ACGL",
"GLW", "RJF", "MKC", "CLX", "HBAN", "ETR", "ATO", "INVH", "CINF", "NVR",
"LKQ", "ULTA", "MOH", "KEY", "DRI", "VMC", "NTAP", "SYY", "OMC", "AEE",
"PFG", "IP", "HWM", "BR", "BALL", "NDAQ", "LYB", "TRGP", "BF.B", "CAG",
"CF", "MAS", "PKG", "GEN", "WRB", "JKHY", "LDOS", "HOLX", "TXT", "APA",
"MRO", "FDS", "TER", "CE", "CHD", "WAB", "IEX", "PHM", "TECH", "HAS",
"NRG", "CNP", "SWKS", "K", "A", "GPC", "BBY", "ZBRA", "RVTY", "JCI",
"NTRS", "CRL", "HRL", "PODD", "INCY", "BAX", "STT", "AKAM", "EPAM", "EMN",
"COO", "GRMN", "NWSA", "NWS", "FMC", "MGM", "DVA", "BEN", "SEE", "TPR",
"DXCM", "LW", "ALGN", "RE", "RHI", "IPG", "AIZ", "L", "PARA", "FOX",
"FOXA", "IVZ", "VTRS", "HST", "REG", "UHS", "PNR", "AOS", "CPB", "HSIC",
"NCLH", "AES", "ETSY", "FFIV", "WY", "JNPR", "CPT", "DISH", "FRT", "PNW",
"MOS", "ALLE", "OGN", "XRAY", "APA", "BIO", "BXP", "MTCH", "MKTX", "ROL",
"MHK", "GL", "DAY", "CHRW", "WHR", "LKQ", "NDSN", "VFC", "BBWI", "TPX"
]
ANALYZE_LIST_NAME = input("Enter list name (NASDAQ_LIST/DOW_LIST/SP500) to analyze: ")
ANALYZE_LIST = globals().get(ANALYZE_LIST_NAME)
if ANALYZE_LIST is None:
print("Ungültiger Listenname!")
else:
main_multi(ANALYZE_LIST, START_DATE, END_DATE)