-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
306 lines (262 loc) · 12.5 KB
/
app.py
File metadata and controls
306 lines (262 loc) · 12.5 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
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from groq import Groq
from datetime import datetime, timedelta
from duckduckgo_search import DDGS
# 1. CONFIG
st.set_page_config(page_title="NewsQuant: Multi-Asset Sentiment", page_icon="📰", layout="wide")
# Custom CSS
st.markdown("""
<style>
.stApp { background-color: #0e1117; }
.news-card {
background-color: #1f2937;
padding: 15px;
border-radius: 8px;
margin-bottom: 10px;
border-left: 4px solid #6b7280;
transition: transform 0.1s;
}
.news-card:hover { transform: scale(1.01); }
.positive { border-left-color: #22c55e; }
.negative { border-left-color: #ef4444; }
/* AI Summary Box */
.ai-summary {
background-color: #172554; /* Dark Blue */
border: 1px solid #3b82f6;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
color: #e0f2fe;
}
a { color: #60a5fa; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
""", unsafe_allow_html=True)
st.title("📰 NewsQuant: Event-Driven Sentiment Engine")
# 2. COMPREHENSIVE TICKER LIST
TICKER_UNIVERSE = {
"Mega Cap Tech": {"NVIDIA": "NVDA", "Apple": "AAPL", "Microsoft": "MSFT", "Amazon": "AMZN", "Google": "GOOGL", "Meta": "META", "Tesla": "TSLA", "AMD": "AMD"},
"Semiconductors": {"Broadcom": "AVGO", "Intel": "INTC", "Qualcomm": "QCOM", "Micron": "MU", "TSMC": "TSM", "Super Micro": "SMCI", "Arm Holdings": "ARM"},
"Software & AI": {"Palantir": "PLTR", "Snowflake": "SNOW", "Salesforce": "CRM", "Adobe": "ADBE", "CrowdStrike": "CRWD", "Oracle": "ORCL", "C3.ai": "AI"},
"Crypto & Blockchain": {"Bitcoin": "BTC-USD", "Ethereum": "ETH-USD", "Solana": "SOL-USD", "Coinbase": "COIN", "MicroStrategy": "MSTR", "Marathon Digital": "MARA"},
"Finance & Banks": {"JPMorgan": "JPM", "Goldman Sachs": "GS", "Visa": "V", "Mastercard": "MA", "BlackRock": "BLK", "PayPal": "PYPL", "Square": "SQ"},
"EV & Auto": {"Rivian": "RIVN", "Lucid": "LCID", "Ford": "F", "GM": "GM", "Toyota": "TM", "NIO": "NIO"},
"Defense & Energy": {"Lockheed Martin": "LMT", "Boeing": "BA", "Exxon Mobil": "XOM", "Chevron": "CVX", "NextEra Energy": "NEE", "Plug Power": "PLUG"},
"Indices": {"S&P 500": "^GSPC", "Nasdaq 100": "^NDX", "Dow Jones": "^DJI", "VIX": "^VIX", "Gold": "GLD"}
}
# Flatten for dropdown
flat_tickers = {}
for category, assets in TICKER_UNIVERSE.items():
for name, symbol in assets.items():
flat_tickers[f"{name} ({symbol})"] = symbol
# 3. SIDEBAR CONFIGURATION
st.sidebar.header("Configuration")
# API Key
api_key = st.secrets.get("GROQ_API_KEY")
if not api_key:
api_key = st.sidebar.text_input("🔑 Groq API Key", type="password")
# Multi-Select Tickers
selected_labels = st.sidebar.multiselect(
"Select Assets to Analyze",
options=list(flat_tickers.keys()),
default=["Tesla (TSLA)"]
)
# 4. DATA FUNCTIONS
@st.cache_data(ttl=3600)
def get_market_data(ticker_symbol):
"""Fetches Price History using yf.download (More Robust)"""
try:
df = yf.download(ticker_symbol, period="1mo", interval="1d", progress=False)
# Handle Multi-Index columns if they appear
if isinstance(df.columns, pd.MultiIndex):
try:
df = df.xs(ticker_symbol, axis=1, level=1)
except KeyError:
pass # Sometimes level is different or structure varies
return df
except Exception as e:
st.error(f"Price Data Error ({ticker_symbol}): {e}")
return pd.DataFrame()
def get_news_ddg(ticker_symbol):
"""Fetches News using DuckDuckGo"""
try:
search_term = ticker_symbol.replace("-USD", "")
results = DDGS().news(keywords=f"{search_term} stock news", max_results=8)
formatted_news = []
for item in results:
formatted_news.append({
"title": item.get('title', 'No Title'),
"link": item.get('url', '#'),
"publisher": item.get('source', 'Unknown'),
"time": item.get('date', datetime.now().isoformat())
})
# --- FIX: SORT NEWS BY TIME (DESCENDING/NEWEST FIRST) ---
# ISO strings sort correctly lexicographically
formatted_news.sort(key=lambda x: x['time'], reverse=True)
return formatted_news
except Exception as e:
st.warning(f"News fetch warning: {e}")
return []
# 5. AI FUNCTIONS
def analyze_sentiment_and_summary(ticker, news_items, api_key):
if not news_items or not api_key:
return [], "No data for analysis."
client = Groq(api_key=api_key)
headlines_block = "\n".join([f"{i+1}. {item['title']}" for i, item in enumerate(news_items)])
prompt = f"""
You are a Financial News Analyst. Analyze these headlines for {ticker}:
{headlines_block}
Perform two tasks:
1. Classify each headline as POSITIVE, NEGATIVE, or NEUTRAL. Return as a JSON list string.
2. Write a brief "Market Pulse" inference (max 50 words). Why is the stock moving?
Format output exactly like this:
SENTIMENT_LIST: ["POSITIVE", "NEGATIVE", ...]
SUMMARY: [Your summary text here]
"""
try:
completion = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
temperature=0.1
)
content = completion.choices[0].message.content
sentiments = []
summary = "Analysis failed."
import ast
if "SENTIMENT_LIST:" in content:
list_part = content.split("SENTIMENT_LIST:")[1].split("SUMMARY:")[0].strip()
try:
sentiments = ast.literal_eval(list_part)
except:
sentiments = ["NEUTRAL"] * len(news_items)
if "SUMMARY:" in content:
summary = content.split("SUMMARY:")[1].strip()
return sentiments, summary
except Exception as e:
return ["NEUTRAL"] * len(news_items), f"AI Error: {e}"
# 6. MAIN APP LOGIC
if not selected_labels:
st.info("👈 Please select at least one asset from the sidebar.")
else:
tabs = st.tabs([label.split(" (")[0] for label in selected_labels])
for i, label in enumerate(selected_labels):
ticker = flat_tickers[label]
with tabs[i]:
st.markdown(f"## 🔎 Analysis: {ticker}")
# 1. Fetch Data
col1, col2 = st.columns([2, 1])
with col1:
with st.spinner("Fetching Chart..."):
price_df = get_market_data(ticker)
with col2:
news_list = get_news_ddg(ticker)
# 2. Run AI Analysis
sentiments = []
summary = ""
if api_key and news_list:
with st.spinner("🤖 AI Reading News..."):
sentiments, summary = analyze_sentiment_and_summary(ticker, news_list, api_key)
elif not api_key:
st.warning("Enter API Key to see AI Inference.")
sentiments = ["NEUTRAL"] * len(news_list)
# 3. Merge Data
sentiment_score = 0
processed_news = []
limit = min(len(news_list), len(sentiments))
for j in range(limit):
item = news_list[j]
sent = sentiments[j]
item['sentiment'] = sent
processed_news.append(item)
if sent == "POSITIVE": sentiment_score += 1
elif sent == "NEGATIVE": sentiment_score -= 1
# --- VISUALIZATION ---
# A. AI Inference Box
if summary:
st.markdown(f"""
<div class="ai-summary">
<strong>🤖 AI Market Pulse:</strong> {summary}
</div>
""", unsafe_allow_html=True)
# B. Price Chart
if not price_df.empty:
fig = go.Figure()
# Determine correct column name (Close or Adj Close)
y_col = 'Close' if 'Close' in price_df.columns else price_df.columns[0]
fig.add_trace(go.Scatter(
x=price_df.index, y=price_df[y_col],
mode='lines', name='Price',
line=dict(color='#60a5fa', width=2),
fill='tozeroy',
fillcolor='rgba(96, 165, 250, 0.1)'
))
fig.update_layout(
height=350,
margin=dict(l=10, r=10, t=30, b=10),
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
xaxis=dict(showgrid=False, title=None),
yaxis=dict(gridcolor='#374151', title=None),
title=f"{ticker} - 1 Month Trend"
)
# FIX: Added unique key here
st.plotly_chart(fig, width='stretch', key=f"price_chart_{ticker}_{i}")
else:
st.error("Could not load price data. Ticker might be delisted or API limit reached.")
# C. Sentiment Meter
st.markdown("#### 🌡️ Media Sentiment")
m_col1, m_col2 = st.columns([1, 3])
with m_col1:
if sentiment_score > 0:
st.metric("Net Score", "BULLISH", f"+{sentiment_score}", delta_color="normal")
elif sentiment_score < 0:
st.metric("Net Score", "BEARISH", f"{sentiment_score}", delta_color="inverse")
else:
st.metric("Net Score", "NEUTRAL", "0", delta_color="off")
with m_col2:
# Sentiment Bar
if sentiments:
counts = pd.Series(sentiments).value_counts()
color_map = {"POSITIVE": "#22c55e", "NEGATIVE": "#ef4444", "NEUTRAL": "#9ca3af"}
fig_bar = go.Figure([go.Bar(
x=counts.values, y=counts.index,
orientation='h',
marker_color=[color_map.get(x, "#9ca3af") for x in counts.index],
text=counts.values, textposition='auto'
)])
fig_bar.update_layout(
height=120, margin=dict(l=0, r=0, t=0, b=0),
paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)',
xaxis=dict(showgrid=False, visible=False),
yaxis=dict(showgrid=False)
)
# FIX: Added unique key here
st.plotly_chart(fig_bar, width='stretch', key=f"sentiment_bar_{ticker}_{i}")
# D. News Feed
st.markdown("#### 🗞️ Latest Headlines")
for item in processed_news:
css = "neutral"
emoji = "⚪"
if item['sentiment'] == "POSITIVE":
css = "positive"; emoji = "🟢"
elif item['sentiment'] == "NEGATIVE":
css = "negative"; emoji = "🔴"
try:
d = datetime.fromisoformat(item['time'].replace('Z', '+00:00'))
date_display = d.strftime("%b %d, %H:%M")
except:
date_display = item['time']
st.markdown(f"""
<div class="news-card {css}">
<div style="display:flex; justify-content:space-between; margin-bottom:4px;">
<span style="color:#9ca3af; font-size:0.8rem;">{item['publisher']} • {date_display}</span>
<span style="font-weight:bold; color:{'#4ade80' if css=='positive' else '#f87171' if css=='negative' else '#9ca3af'}">{item['sentiment']}</span>
</div>
<div style="font-size:1rem;">
<a href="{item['link']}" target="_blank" style="color:#f3f4f6; text-decoration:none;">{emoji} {item['title']}</a>
</div>
</div>
""", unsafe_allow_html=True)