-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1087 lines (934 loc) · 46.8 KB
/
main.py
File metadata and controls
1087 lines (934 loc) · 46.8 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timedelta
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from GoogleNews import GoogleNews
import finnhub
import random
from collections import Counter
from io import BytesIO
import numpy as np
import re
import time
import requests
from bs4 import BeautifulSoup
# --- 1. CONFIGURATION ---
st.set_page_config(
page_title="Sentiment Stocker | Pro Terminal",
page_icon="app_logo.png",
layout="wide",
initial_sidebar_state="expanded"
)
# --- DISCLAIMER MODAL (MUST COME BEFORE ANY OTHER UI) ---
if 'disclaimer_accepted' not in st.session_state:
st.session_state.disclaimer_accepted = False
if not st.session_state.disclaimer_accepted:
st.markdown("""
<style>
.disclaimer-box {
background: linear-gradient(135deg, #1e1e2e 0%, #2d2d44 100%);
border: 2px solid #ff4b4b;
border-radius: 15px;
padding: 40px;
margin: 50px auto;
max-width: 800px;
box-shadow: 0 10px 40px rgba(255, 75, 75, 0.3);
}
.disclaimer-title {
color: #ff4b4b;
font-size: 2rem;
font-weight: 800;
text-align: center;
margin-bottom: 20px;
text-transform: uppercase;
letter-spacing: 2px;
}
.disclaimer-subtitle {
color: #ffaa00;
font-size: 1.3rem;
font-weight: 700;
margin-top: 25px;
margin-bottom: 10px;
}
.disclaimer-text {
color: #e0e0e0;
font-size: 1rem;
line-height: 1.8;
margin-bottom: 15px;
}
.warning-icon {
font-size: 3rem;
text-align: center;
margin-bottom: 20px;
}
</style>
""", unsafe_allow_html=True)
st.markdown("""
<div class="disclaimer-box">
<div class="warning-icon">⚠️</div>
<div class="disclaimer-title">SEBI Mandatory Guidelines</div>
Educational Purpose Only
This platform does not provide any tips, recommendations, or financial advice.
All updates, posts, and discussions are purely and solely for educational and learning purposes
Consult a Professional Financial Advisor before making any investment decisions.
No Liability for Losses
Neither the Platform Admins nor the Users are responsible for any financial losses arising from decisions made based on platform content.
Admins will not be responsible for any financial losses incurred from transactions or dealings with any member providing buy/sell calls or claiming higher returns.
By clicking "I Agree", you acknowledge that you have read, understood, and accept all terms and risks mentioned above.
""", unsafe_allow_html=True)
# Center the button
col1, col2, col3 = st.columns([1, 1, 1])
with col2:
if st.button("I Agree and Accept the Risks", type="primary", use_container_width=True):
st.session_state.disclaimer_accepted = True
st.rerun()
st.stop() # Prevent rest of app from loading
# --- REST OF YOUR CODE CONTINUES BELOW ---
try:
nltk.data.find('vader_lexicon')
except LookupError:
nltk.download('vader_lexicon', quiet=True)
sia = SentimentIntensityAnalyzer()
# Initialize Finnhub Client
# Get your free API key from: https://finnhub.io/register
# RECOMMENDED: Move this to st.secrets["FINNHUB_API_KEY"] for security
# main.py
# Check if key is in secrets, otherwise fallback (or handle error)
if "FINNHUB_API_KEY" in st.secrets:
FINNHUB_API_KEY = st.secrets["FINNHUB_API_KEY"]
else:
# Fallback for local testing if you don't have secrets.toml set up
FINNHUB_API_KEY = None # Placeholder; app should not proceed without a key
st.error("FINNHUB_API_KEY not found in st.secrets. Please set it securely.")
st.stop()
finnhub_client = finnhub.Client(api_key=FINNHUB_API_KEY)
# --- 2. DATA CONSTANTS ---
COMPETITORS = {
'AAPL': ['MSFT', 'GOOGL', 'NVDA'],
'MSFT': ['AAPL', 'GOOGL', 'AMZN'],
'GOOGL': ['MSFT', 'META', 'AMZN'],
'NVDA': ['AMD', 'INTC', 'TSM'],
'TSLA': ['F', 'GM', 'TM'],
'TCS.NS': ['INFY.NS', 'WIPRO.NS', 'HCLTECH.NS'],
'INFY.NS': ['TCS.NS', 'WIPRO.NS', 'TECHM.NS'],
'RELIANCE.NS': ['ADANIENT.NS', 'ONGC.NS', 'TATASTEEL.NS'],
'HDFCBANK.NS': ['SBIN.NS', 'ICICIBANK.NS', 'AXISBANK.NS'],
'TATAMOTORS.NS': ['MARUTI.NS', 'M&M.NS', 'ASHOKLEY.NS'],
'TATASTEEL.NS': ['JSWSTEEL.NS', 'HINDALCO.NS', 'SAIL.NS']
}
# Feature 2: Map Sectors to Supply Chain Dependencies
SECTOR_MAP = {
'Technology': ['Semiconductors', 'Cloud Computing', 'AI Chips'],
'Consumer Electronics': ['Semiconductors', 'Lithium'],
'Auto': ['Steel', 'Semiconductors', 'Crude Oil'],
'Energy': ['Crude Oil', 'Natural Gas', 'OPEC'],
'Financial': ['Interest Rates', 'Housing Market'],
'Healthcare': ['Biotech', 'Insurance'],
'Utilities': ['Natural Gas', 'Coal'],
'Basic Materials': ['Iron Ore', 'Coal', 'Shipping']
}
DEPENDENCY_TICKERS = {
'Semiconductors': 'SMH', 'Cloud Computing': 'SKYY', 'AI Chips': 'SOXX',
'Lithium': 'LIT', 'Steel': 'SLX', 'Crude Oil': 'USO',
'Natural Gas': 'UNG', 'OPEC': 'XLE', 'Interest Rates': '^TNX',
'Housing Market': 'XHB', 'Biotech': 'IBB', 'Insurance': 'KIE',
'Coal': 'ARCH', 'Global Markets': '^GSPC', 'Oil': 'USO',
'Iron Ore': 'RIO', 'Shipping': 'BDRY'
}
# Feature 1: Keywords for Topic Modeling
TOPIC_KEYWORDS = {
"⚠️ Regulatory": ['sec', 'lawsuit', 'fine', 'ban', 'court', 'investigation', 'compliance', 'judge', 'antitrust'],
"💰 Earnings": ['eps', 'revenue', 'profit', 'earnings', 'quarterly', 'miss', 'beat', 'guidance', 'dividend'],
"📦 Product": ['launch', 'reveal', 'defect', 'recall', 'iphone', 'model', 'upgrade', 'delay', 'chip'],
"🌍 Macro": ['inflation', 'fed', 'rate', 'tax', 'jobs', 'recession', 'policy', 'economy', 'interest'],
"🤝 M&A": ['acquire', 'merger', 'buyout', 'deal', 'takeover', 'stake']
}
SOURCE_TIERS = {
1: ['bloomberg', 'reuters', 'wsj', 'financial times', 'cnbc', 'yahoo finance'],
2: ['marketwatch', 'benzinga', 'the motley fool', 'business insider', 'techcrunch']
}
VERIFIED_SOURCES = ['Bloomberg', 'Reuters', 'WSJ', 'CNBC', 'Financial Times', 'Yahoo Finance']
# --- 3. CUSTOM STYLING (UI FIXES) ---
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
html, body, [class*="css"] {
font-family: 'Inter', sans-serif;
}
/* Modern Card Container */
.metric-container {
background-color: #1A1C24;
border: 1px solid #2E303E;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
color: white;
text-align: center;
transition: transform 0.2s;
height: 140px; /* Fixed height for uniformity */
display: flex;
flex-direction: column;
justify-content: center;
}
.metric-container:hover {
transform: translateY(-2px);
border-color: #667eea;
}
/* News Feed Styling */
.news-card {
background-color: #1A1C24;
border: 1px solid #2E303E;
padding: 15px;
border-radius: 8px;
margin-bottom: 10px;
transition: background-color 0.3s;
min-height: 100px; /* Minimum height for uniformity */
}
.news-card:hover {
background-color: #252836;
}
.news-link {
text-decoration: none;
color: #E0E0E0;
font-weight: 600;
font-size: 1.05rem;
}
.news-meta {
font-size: 0.8rem;
color: #888;
margin-top: 5px;
}
/* Main Title */
.main-header {
font-size: 2.8rem;
font-weight: 800;
background: linear-gradient(90deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0px;
}
</style>
""", unsafe_allow_html=True)
# --- 4. CORE FUNCTIONS ---
def clean_google_link(url):
if not url: return "#"
if "news.google.com" in url or "google.com/url" in url: return url
if "&ved=" in url: return url.split("&ved=")[0]
return url
def get_currency_symbol(currency_code):
"""Map currency code to symbol"""
currency_map = {
'USD': '$', 'EUR': '€', 'GBP': '£', 'INR': '₹',
'JPY': '¥', 'CNY': '¥', 'AUD': 'A$', 'CAD': 'C$'
}
return currency_map.get(currency_code, currency_code or '$')
@st.cache_data(ttl=3600)
def fetch_stock_data(ticker, period="1mo"):
try:
stock = yf.Ticker(ticker)
# Fetch slightly more data for SMA calculations
hist_period = '1y' if period in ['1mo', '3mo'] else period
# USE yf.download() as it is more robust than stock.history() currently
df = yf.download(ticker, period=hist_period, progress=False)
# Flatten multi-index columns if present (common in new yfinance)
if isinstance(df.columns, pd.MultiIndex):
df.columns = df.columns.get_level_values(0)
if df.empty: return None, None
return df, stock.info
except Exception as e:
print(f"Error fetching stock data: {e}")
return None, None
@st.cache_data(ttl=300)
def fetch_news_data_finnhub(ticker, limit=15):
"""Fetch financial news using Finnhub API"""
news_data = []
try:
# Get date range (last 7 days)
end_date = datetime.now()
start_date = end_date - timedelta(days=7)
# Format dates for Finnhub (YYYY-MM-DD)
from_date = start_date.strftime('%Y-%m-%d')
to_date = end_date.strftime('%Y-%m-%d')
# Fetch company news
news = finnhub_client.company_news(ticker, _from=from_date, to=to_date)
for item in news[:limit]:
news_data.append({
'title': item['headline'],
'link': item['url'],
'source': item['source'],
'date': datetime.fromtimestamp(item['datetime']).strftime('%Y-%m-%d %H:%M'),
'summary': item.get('summary', ''),
'sentiment': item.get('sentiment', 0)
})
time.sleep(0.1) # Small delay to stay under rate limit
except Exception as e:
pass # Fail silently and return empty list
return news_data
@st.cache_data(ttl=1800)
def fetch_news_data(query, limit=15, allow_fallback=True):
"""Modified to accept general query strings for Topic/Sector analysis"""
news_data = []
# Strategy 1: YFinance (Best for "Credible News" if query is a ticker)
# Check if query looks like a ticker (e.g., AAPL, RELIANCE.NS, ^GSPC, BRK-B)
if not " " in query and len(query) < 15:
try:
t = yf.Ticker(query)
yf_news = t.news
if yf_news:
for n in yf_news:
# Handle nested structure (yfinance update)
title = n.get('title')
link = n.get('link')
publisher = n.get('publisher')
if not title and 'content' in n:
c = n['content']
title = c.get('title')
# Try to find link in content
if not link:
link = c.get('canonicalUrl', {}).get('url') if isinstance(c.get('canonicalUrl'), dict) else c.get('clickThroughUrl', {}).get('url')
# Try to find publisher in content
if not publisher:
publisher = c.get('provider', {}).get('displayName') if isinstance(c.get('provider'), dict) else "Yahoo Finance"
if not title: continue # Skip if no title
ts = n.get('providerPublishTime', 0)
date_str = datetime.fromtimestamp(ts).strftime('%Y-%m-%d') if ts else 'Recent'
news_data.append({
'title': title,
'link': link or f"https://finance.yahoo.com/news/{n.get('id', '')}.html",
'source': publisher or 'Yahoo Finance',
'date': date_str
})
return news_data[:limit]
except: pass
if not allow_fallback:
return news_data
# Strategy 2: Google News (Fallback & Complex Queries like "MSFT reddit")
# UPDATED: If query contains "reddit", try direct Reddit scraping text search fallback
if "reddit" in query.lower():
try:
# Extract ticker from query (e.g., "AAPL reddit" -> "AAPL")
search_term = query.replace("reddit", "").strip()
# Simple reddit search scrape (old reddit is easier to parse)
url = "https://old.reddit.com/search"
params = {'q': search_term, 'sort': 'new'}
r = requests.get(url, headers=headers, params=params, timeout=5)
r = requests.get(url, headers=headers, timeout=5)
if r.status_code == 200:
soup = BeautifulSoup(r.text, 'html.parser')
# Find search results
results = soup.find_all('div', class_='search-result-link')
for res in results[:limit]:
title_tag = res.find('a', class_='search-title')
if title_tag:
news_data.append({
'title': title_tag.text,
'link': title_tag['href'] if title_tag['href'].startswith('http') else f"https://reddit.com{title_tag['href']}",
'source': 'Reddit',
'date': 'Recent'
})
if news_data: return news_data
except Exception as e:
print(f"Reddit scrape error: {e}")
try:
time.sleep(2) # Rate limiting
googlenews = GoogleNews(lang='en', period='7d')
googlenews.search(query)
results = googlenews.results()
if results:
for item in results[:limit]:
news_data.append({
'title': item['title'],
'link': item['title'],
'link': clean_google_link(item.get('link', '#')),
'source': item.get('media', 'Google News'),
'date': item.get('date', 'Recent')
})
except Exception as e:
print(f"Google News error: {e}")
return news_data
def get_sentiment_score(news_items):
"""Calculate sentiment using Finnhub sentiment or VADER fallback"""
if not news_items:
return 0
total = 0
for item in news_items:
# Try using Finnhub sentiment first
if 'sentiment' in item and item['sentiment'] != 0:
total += item['sentiment']
else:
# Fallback to VADER on title + summary
text = f"{item.get('title', '')} {item.get('summary', '')}"
if text.strip():
total += sia.polarity_scores(text)['compound']
return total / len(news_items) if news_items else 0
def calculate_rsi(data, period=14):
"""Calculate Relative Strength Index (RSI)"""
delta = data.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi.iloc[-1] # Return latest RSI value
# --- MODIFIED: accept custom keywords dictionary ---
def categorize_topics(news_items, keywords_dict=None):
"""Feature 1: Tags news with specific topics"""
# Use default if no custom provided
if keywords_dict is None:
keywords_dict = TOPIC_KEYWORDS
tags = []
for n in news_items:
title = n['title'].lower()
found_tag = False
for category, keywords in keywords_dict.items():
if any(k in title for k in keywords):
tags.append(category)
found_tag = True
break
if not found_tag:
tags.append("📰 General News")
return Counter(tags)
# --- 5. UI COMPONENTS ---
def render_kpi(label, value, delta=None):
delta_html = f'<div class="metric-delta" style="color:{"#00E676" if "+" in delta else "#FF1744"};">{delta}</div>' if delta else ""
st.markdown(f"""
<div class="metric-container">
<div class="metric-label">{label}</div>
<div class="metric-value">{value}</div>
{delta_html}
</div>
""", unsafe_allow_html=True)
def render_news_card(title, link, source, score, date):
color = "#00E676" if score > 0.05 else ("#FF1744" if score < -0.05 else "#B0BEC5")
# Credibility Logic
source_lower = source.lower()
if any(k in source_lower for k in SOURCE_TIERS[1]):
tier_badge = '<span style="color:#00E676; font-weight:bold;">🛡️ Verified</span>'
elif any(k in source_lower for k in SOURCE_TIERS[2]):
tier_badge = '<span style="color:#B0BEC5;">ℹ️ Standard</span>'
else:
tier_badge = '<span style="color:#FF9100;">⚠️ Unverified</span>'
st.markdown(f"""
import html
# Escape potentially malicious content
escaped_title = html.escape(title)
escaped_source = html.escape(source)
escaped_date = html.escape(date)
# Basic URL validation for link
safe_link = link if link and (link.startswith('http://') or link.startswith('https://')) else '#'
st.markdown(f"""
<div class="news-card" style="border-left: 4px solid {color};">
<a href="{safe_link}" target="_blank" rel="noreferrer noopener" class="news-link">
{escaped_title}
</a>
<div class="news-meta">
<span>{escaped_source} • {tier_badge} • {escaped_date}</span> • <span style="color:{color}; font-weight:bold;">Score: {score:.2f}</span>
</div>
</div>
""", unsafe_allow_html=True)
</div>
</div>
""", unsafe_allow_html=True)
def render_ripple_effects(sector):
"""Feature 2: Supply Chain Ripple Effects Component"""
st.markdown(f"#### ⛓️ Supply Chain: {sector} Dependencies")
st.caption("Monitoring upstream sectors for early warning signals")
dependencies = SECTOR_MAP.get(sector, ['Global Markets', 'Oil'])
cols = st.columns(len(dependencies))
for i, dep in enumerate(dependencies):
with cols[i]:
# Use specific ticker if available, otherwise fallback to text search (which might fail)
query_term = DEPENDENCY_TICKERS.get(dep, dep)
dep_news = fetch_news_data(query_term, limit=5)
score = get_sentiment_score(dep_news)
color = "#00E676" if score > 0.05 else "#FF1744" if score < -0.05 else "#A0A0A0"
arrow = "⬆" if score > 0.05 else "⬇" if score < -0.05 else "➡"
st.markdown(f"""
<div style="background:#1A1C24; padding:15px; border-radius:10px; border:1px solid #2E303E; text-align:center;">
<div style="color:#888; font-size:0.8rem; text-transform:uppercase;">{dep}</div>
<div style="color:{color}; font-size:1.5rem; font-weight:bold;">{score:.2f} {arrow}</div>
</div>
""", unsafe_allow_html=True)
def plot_advanced_timeline(df, current_sentiment, period, currency_symbol='$'):
# Slice data for view
if period == '1mo': view_df = df.tail(30)
elif period == '6mo': view_df = df.tail(180)
else: view_df = df
# SMAs
df['SMA_50'] = df['Close'].rolling(window=50).mean()
df['SMA_200'] = df['Close'].rolling(window=200).mean()
plot_df = df.loc[view_df.index]
fig = go.Figure()
# Candlestick or Line? Let's go Line for cleanliness with SMAs
fig.add_trace(go.Scatter(x=plot_df.index, y=plot_df['Close'], name='Price', line=dict(color='#667eea', width=2)))
fig.add_trace(go.Scatter(x=plot_df.index, y=plot_df['SMA_50'], name='SMA 50', line=dict(color='#FFA500', width=1, dash='dot')))
fig.add_trace(go.Scatter(x=plot_df.index, y=plot_df['SMA_200'], name='SMA 200', line=dict(color='#FF4500', width=1, dash='dot')))
# Sentiment Overlay
days = len(plot_df)
noise = np.random.normal(0, 0.1, days)
trend = np.linspace(0, current_sentiment, days) + noise
fig.add_trace(go.Bar(
x=plot_df.index, y=trend, name='Sentiment Trend',
yaxis='y2', marker_color=np.where(trend>0, '#00E676', '#FF1744'),
opacity=0.15
))
fig.update_layout(
template="plotly_dark",
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
yaxis=dict(title=f"Price ({currency_symbol})", showgrid=True, gridcolor='#2E303E'),
yaxis2=dict(title="Sentiment", overlaying='y', side='right', range=[-1, 1], showgrid=False),
height=500,
margin=dict(l=0, r=0, t=30, b=0),
legend=dict(orientation="h", y=1.02, x=0)
)
return fig
# --- 6. MARKET MAP (UPDATED) ---
def render_heatmap():
st.markdown("### 🌍 Real-Time Global Market Heatmap")
# Define Sector Leaders (Expandable)
SECTOR_LEADERS = {
'Technology': ['AAPL', 'MSFT', 'NVDA', 'GOOGL', 'AMD', 'ORCL', 'CRM'],
'Financial': ['JPM', 'BAC', 'V', 'MA', 'GS', 'MS', 'WFC'],
'Healthcare': ['JNJ', 'UNH', 'LLY', 'PFE', 'MRK', 'ABBV'],
'Consumer': ['AMZN', 'TSLA', 'WMT', 'HD', 'MCD', 'NKE', 'KO'],
'Energy': ['XOM', 'CVX', 'COP', 'SLB', 'EOG'],
'Industrial': ['CAT', 'DE', 'BA', 'GE', 'HON', 'UPS']
}
# Flatten list for batch fetching
all_tickers = [ticker for sector in SECTOR_LEADERS.values() for ticker in sector]
with st.spinner("Scanning global markets..."):
try:
# Batch fetch 2 days of data to calculate % change
data = yf.download(all_tickers, period="5d", progress=False)['Close']
heatmap_data = []
for sector, tickers in SECTOR_LEADERS.items():
for t in tickers:
if t in data.columns:
# Calculate % Change
try:
prices = data[t].dropna()
if len(prices) >= 2:
pct_change = ((prices.iloc[-1] - prices.iloc[-2]) / prices.iloc[-2]) * 100
# Use volume as a proxy for size in treemap (or fetch market cap if feasible)
# Here we use a fixed size 100 or abs(change) to visualize impact
size_metric = abs(pct_change) + 1
heatmap_data.append({
'Ticker': t,
'Sector': sector,
'Change': pct_change,
'Size': size_metric,
'Label': f"{t}\n{pct_change:+.2f}%"
})
except:
continue
if not heatmap_data:
st.error("Could not fetch market data. Try again later.")
return
df_map = pd.DataFrame(heatmap_data)
# Create Treemap
fig = px.treemap(
df_map,
path=[px.Constant("Market"), 'Sector', 'Ticker'],
values='Size',
color='Change',
color_continuous_scale='RdYlGn',
range_color=[-3, 3], # Clamp colors between -3% and +3%
custom_data=['Label']
)
fig.update_traces(textposition='middle center', texttemplate='%{customdata[0]}')
fig.update_layout(height=600, margin=dict(t=30, l=10, r=10, b=10))
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"Error generating heatmap: {e}")
return
# --- 7. MAIN APP ---
def main():
# Sidebar (Control Panel + Watchlist)
with st.sidebar:
st.image("app_logo.png", width=50)
st.markdown("## ⚙️ Control Panel")
# CSS Hack: Force vertical centering for all column layouts in sidebar
st.markdown("""
<style>
[data-testid="stSidebar"] [data-testid="stHorizontalBlock"] {
align-items: center !important;
}
</style>
""", unsafe_allow_html=True)
mode = st.radio("View Mode", ["🔍 Analysis", "🗺️ Market Heatmap"], index=0)
st.markdown("---")
if mode == "🔍 Analysis":
ticker = st.text_input("Ticker Symbol", "AAPL").upper()
period = st.selectbox("Timeframe", ["1mo", "6mo", "1y", "5y"])
st.caption("Press Enter to update")
else:
ticker = None
st.markdown("---")
# --- PORTFOLIO WATCHLIST ---
st.markdown("## 📋 Watchlist")
if 'watchlist' not in st.session_state:
st.session_state.watchlist = ['AAPL', 'TSLA', 'NVDA'] # Default examples
# Input to Add Ticker
new_ticker = st.text_input("Add Ticker", placeholder="e.g. MSFT", key="watch_input").upper()
if st.button("Add +"):
if new_ticker and new_ticker not in st.session_state.watchlist:
st.session_state.watchlist.append(new_ticker)
st.rerun()
# Display Watchlist Items
if st.session_state.watchlist:
# Batch fetch data for watchlist to be fast
try:
watch_df = yf.download(st.session_state.watchlist, period="2d", progress=False)['Close']
for w_ticker in st.session_state.watchlist:
# Native vertical_alignment might fail, rely on CSS hack above
col1, col2, col3 = st.columns([2, 2, 1])
with col1:
# Fetch quick sentiment for watchlist
w_news = fetch_news_data(w_ticker, limit=3, allow_fallback=False)
w_score = get_sentiment_score(w_news)
s_color = "#00E676" if w_score > 0.05 else "#FF1744" if w_score < -0.05 else "#A0A0A0"
st.markdown(f"**{w_ticker}**<br><span style='color:{s_color}; font-size:0.8em'>{w_score:.2f}</span>", unsafe_allow_html=True)
with col2:
try:
# Handle if watch_df is DataFrame (multiple tickers) or Series (single ticker)
if isinstance(watch_df, pd.DataFrame) and w_ticker in watch_df.columns:
prices = watch_df[w_ticker].dropna()
elif isinstance(watch_df, pd.Series) and watch_df.name == w_ticker: # Only 1 ticker
prices = watch_df
elif isinstance(watch_df, pd.Series): # Fallback
prices = watch_df
else:
prices = []
if len(prices) >= 2:
last_p = prices.iloc[-1]
prev_p = prices.iloc[-2]
delta = ((last_p - prev_p) / prev_p) * 100
color = "#00E676" if delta >= 0 else "#FF1744"
st.markdown(f"<span style='color:{color}'>{last_p:.2f} ({delta:+.1f}%)</span>", unsafe_allow_html=True)
else:
st.caption("No Data")
except:
st.caption("N/A")
with col3:
if st.button("✕", key=f"del_{w_ticker}", help="Remove"):
st.session_state.watchlist.remove(w_ticker)
st.rerun()
except Exception as e:
st.error("Watchlist Error")
else:
st.caption("Your watchlist is empty.")
st.markdown("---")
if st.button("🔄 Refresh Data"):
st.cache_data.clear()
st.rerun()
# Header
c1, c2 = st.columns([3, 1])
with c1:
# Title with Logo
lc1, lc2 = st.columns([0.08, 0.92], gap="small")
with lc1:
st.image("app_logo.png", width=60)
with lc2:
st.markdown('<div class="main-header">Sentiment Stocker</div>', unsafe_allow_html=True)
st.caption("Advanced Financial Intelligence Terminal")
# MODE: MARKET HEATMAP
if mode == "🗺️ Market Heatmap":
render_heatmap()
return
# MODE: ANALYSIS
if ticker:
# Load Data
df, info = fetch_stock_data(ticker, period)
# Smart Fetch Logic (Check Session State BEFORE fetching)
is_verified_only = st.session_state.get('verified_only', False)
fetch_limit = 50 if is_verified_only else 15
# Try Finnhub first, fallback to multi-source strategy
news = fetch_news_data_finnhub(ticker, limit=fetch_limit)
if not news:
news = fetch_news_data(f"{ticker}", limit=fetch_limit) # Fallback to yfinance/google/reddit
# Apply Strict Filter if enabled
if is_verified_only and news:
original_count = len(news)
news = [n for n in news if any(vs.lower() in n['source'].lower() for vs in VERIFIED_SOURCES)]
news = news[:10] # Keep top 10 verified
if not news:
st.toast(f"⚠️ Filtered {original_count} items. No verified news found.", icon="🕵️")
if df is None:
st.error("Stock data not found. Check ticker symbol or try a different timeframe.")
return
# KPIs
curr_price = df['Close'].iloc[-1]
try:
prev = df['Close'].iloc[-2]
delta = f"{((curr_price-prev)/prev)*100:+.2f}%"
except: delta = "0%"
sent_score = get_sentiment_score(news)
# Helper: Get Currency Symbol
currency_code = info.get('currency', 'USD') if info else 'USD'
currency_symbol = get_currency_symbol(currency_code)
# Ticker Name Display
long_name = info.get('longName', ticker) if info else ticker
st.markdown(f"<h3 style='text-align: center; color: #4F8BF9; margin-bottom: 20px;'>{long_name}</h3>", unsafe_allow_html=True)
# --- TOP KPI ROW ---
k1, k2, k3, k4 = st.columns(4)
with k1: render_kpi("Current Price", f"{currency_symbol}{curr_price:.2f}", delta)
with k2: render_kpi("Sentiment", f"{sent_score:.2f}", "(-1 to +1)")
with k3: render_kpi("News Volume", str(len(news)), "Headlines")
with k4:
mcap = info.get('marketCap', 0) if info else 0
render_kpi("Market Cap", f"{currency_symbol}{mcap/1e9:.1f}B", None)
st.write("") # Spacer
# --- TABS LAYOUT (UPDATED) ---
tab1, tab2, tab3, tab4 = st.tabs(["📈 Chart & Technicals", "🧠 Sentiment Lab", "📰 News & Peers", "🔗 URL Scanner"])
# TAB 1: CHART
with tab1:
st.plotly_chart(plot_advanced_timeline(df, sent_score, period, currency_symbol), use_container_width=True)
# Fundamentals Row (Moved here from Sidebar)
st.markdown("### 🏢 Key Fundamentals")
if info:
f1, f2, f3, f4 = st.columns(4)
f1.metric("P/E Ratio", f"{info.get('trailingPE', 0):.2f}")
f2.metric("52W High", f"{currency_symbol}{info.get('fiftyTwoWeekHigh', 0):.2f}")
f3.metric("52W Low", f"{currency_symbol}{info.get('fiftyTwoWeekLow', 0):.2f}")
f4.metric("P/B Ratio", f"{info.get('priceToBook', 0):.2f}")
else:
st.warning("No fundamental data.")
# TAB 2: SENTIMENT DEEP DIVE
with tab2:
# --- NEW: PERSONALIZATION OPTION ---
with st.expander("Personalize Narrative Analysis (Add Custom Topics)"):
st.caption("Define your own topics to track specific narratives in the pie chart.")
# 1. Initialize Custom Topics in Session State
if 'user_topics' not in st.session_state:
st.session_state.user_topics = {}
# 2. Input Form
c_add1, c_add2, c_add3 = st.columns([2, 3, 1])
with c_add1:
new_topic_name = st.text_input("Topic Name", placeholder="e.g. AI Hype")
with c_add2:
new_topic_keys = st.text_input("Keywords (comma-separated)", placeholder="gpt, llm, generative, chatbot")
with c_add3:
st.write("") # Spacer
st.write("")
if st.button("Add Topic", use_container_width=True):
if new_topic_name and new_topic_keys:
# Parse keywords
keys_list = [k.strip().lower() for k in new_topic_keys.split(',')]
st.session_state.user_topics[new_topic_name] = keys_list
st.success(f"Added '{new_topic_name}'!")
st.rerun()
# 3. Display & Manage Active Custom Topics
if st.session_state.user_topics:
st.markdown("---")
st.markdown("**Your Custom Topics:**")
for t_name, t_keys in list(st.session_state.user_topics.items()):
c_view1, c_view2 = st.columns([4, 1])
with c_view1:
st.markdown(f"**{t_name}**: _{', '.join(t_keys)}_")
with c_view2:
if st.button("🗑️", key=f"del_{t_name}"):
del st.session_state.user_topics[t_name]
st.rerun()
# --- MERGE TOPICS (DEFAULT + CUSTOM) ---
effective_topics = TOPIC_KEYWORDS.copy()
if 'user_topics' in st.session_state:
effective_topics.update(st.session_state.user_topics)
# 1. Calculate Hybrid Fear & Greed Score
# Technical Component: RSI
rsi_val = 50 # Default Neutral
if df is not None and not df.empty:
try: rsi_val = calculate_rsi(df['Close'])
except: pass
# Sentiment Component: Normalized to 0-100 (Score is -1 to 1)
# -1 -> 0, 0 -> 50, +1 -> 100
sentiment_norm = (sent_score + 1) * 50
# Hybrid Score (Average of Technical & Narrative)
hybrid_score = (rsi_val + sentiment_norm) / 2
# 2. Layout - Row 1: Narrative & Supply Chain
r1c1, r1c2 = st.columns([1, 1], gap="medium")
with r1c1:
st.markdown("<h3 style='text-align: left;'>Narrative Analysis</h3>", unsafe_allow_html=True)
# UPDATED: Pass effective_topics here
topic_counts = categorize_topics(news, keywords_dict=effective_topics)
if topic_counts:
df_topics = pd.DataFrame.from_dict(topic_counts, orient='index', columns=['Count']).reset_index()
fig_topic = px.pie(df_topics, values='Count', names='index', hole=0.5,
color_discrete_sequence=px.colors.sequential.RdBu)
fig_topic.update_layout(height=300, margin=dict(t=0,b=20, l=0, r=0),
legend=dict(orientation="h", y=-0.1))
st.plotly_chart(fig_topic, use_container_width=True)
else:
st.info("Not enough data.")
with r1c2:
# Supply Chain Feature
sector = info.get('sector', 'Technology') if info else 'Technology'
render_ripple_effects(sector)
# 3. Layout - Row 2: Gauge & Explanation
r2c1, r2c2 = st.columns([1, 1], gap="medium")
with r2c1:
# fig_gauge definition
st.markdown("<h3 style='text-align: left; margin-bottom: 0px;'>Hybrid Fear & Greed</h3>", unsafe_allow_html=True)
# Manual Delta Calculation
delta_val = hybrid_score - 50
delta_color = "#00E676" if delta_val >= 0 else "#FF1744"
delta_sym = "▲" if delta_val >= 0 else "▼"
fig_gauge = go.Figure(go.Indicator(
mode = "gauge",
value = hybrid_score,
gauge = {
'axis': {'range': [0, 100], 'tickwidth': 1, 'tickcolor': "white", 'tickvals': []},
'bar': {'color': "#2E303E", 'thickness': 0.15},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "#2E303E",
'steps': [
{'range': [0, 25], 'color': '#FF1744'}, # Extreme Fear
{'range': [25, 45], 'color': '#FF9100'}, # Fear
{'range': [45, 55], 'color': '#B0BEC5'}, # Neutral
{'range': [55, 75], 'color': '#00E676'}, # Greed
{'range': [75, 100], 'color': '#00C853'} # Extreme Greed
],
}
))
# Dynamic Label
if hybrid_score < 25: state_label = "EXTREME FEAR"
elif hybrid_score < 45: state_label = "FEAR"
elif hybrid_score < 55: state_label = "NEUTRAL"
elif hybrid_score < 75: state_label = "GREED"
else: state_label = "EXTREME GREED"
fig_gauge.update_layout(
height=240,
margin=dict(t=10, b=10, l=40, r=40),
paper_bgcolor='rgba(0,0,0,0)',
font={'color': "white"}
)
# Manual Annotation for precise centering of Value & Delta
fig_gauge.add_annotation(
x=0.5, y=0.35,
text=f"<span style='font-size:40px; font-weight:bold'>{hybrid_score:.1f}</span><br><span style='font-size:18px; color:{delta_color}'>{delta_sym} {abs(delta_val):.1f}</span>",
showarrow=False,
xanchor='center',
yanchor='middle'
)
st.plotly_chart(fig_gauge, use_container_width=True, config={'displayModeBar': False})
st.markdown(f"<h3 style='text-align: center; color: white; margin-top: -30px;'>{state_label}</h3>", unsafe_allow_html=True)
with r2c2:
st.subheader("ℹ️ Calculation Logic")
st.markdown(f"""
<div style="background-color: #262730; padding: 20px; border-radius: 10px; border: 1px solid #4F4F4F;">
<h4 style="margin-top:0;">Hybrid Score = (RSI + Sentiment) / 2</h4>
<p>The <b>Market Emotion Index</b> combines two powerful signals:</p>
<ul>
<li><b>📈 Technical RSI ({rsi_val:.1f}):</b> Measures price momentum. <br><small>(Overbought > 70, Oversold < 30)</small></li>
<li><b>🗣️ News Sentiment ({sentiment_norm:.1f}):</b> AI analysis of headlines. <br><small>(Converted to 0-100 scale)</small></li>
</ul>
<hr style="margin: 10px 0;">
<p style="font-size: 0.9rem; color: #A0A0A0;">This composite metric aggregates disparate signals to gauge market mood. A divergence between low price and high sentiment is classified as 'Greed', while high valuation paired with negative news contributes to a 'Fear' score, serving as a study of market psychology</p>
</div>
""", unsafe_allow_html=True)
# TAB 3: NEWS & PEERS
with tab3:
# Strict Quality Filter Checkbox
st.checkbox("🛡️ Only Verified Sources", key="verified_only")
with st.expander("ℹ️ How is Source Credibility Calculated?"):
st.markdown("""
* **🛡️ Verified (Tier 1):** Established financial institutions and major global wire services (e.g., Bloomberg, Reuters, WSJ).
* **ℹ️ Standard (Tier 2):** Widely recognized market news platforms and dedicated financial blogs (e.g., MarketWatch, Benzinga).
* **⚠️ Social / Unverified (Tier 3):** Social media discussions, aggregators, forums (e.g., Reddit, Twitter), or unrecognized sources.
""")
c_peers, c_news = st.columns([1, 2])
with c_peers:
st.subheader("⚔️ Competitors")
peers = COMPETITORS.get(ticker, ['AAPL', 'MSFT', 'GOOGL'])[:3]
for p in peers:
# Try Finnhub first for competitors too
p_news = fetch_news_data_finnhub(p, limit=5)
if not p_news:
p_news = fetch_news_data(f"{p}", limit=5)
p_score = get_sentiment_score(p_news)
p_color = "#00E676" if p_score > 0.05 else "#FF1744"
st.markdown(f"""
<div style="background:#262730; padding:15px; border-radius:8px; margin-bottom:10px; border-left:4px solid {p_color};">
<div style="font-weight:bold; font-size:1.1rem;">{p}</div>
<div style="color:#A0A0A0; font-size:0.9rem;">Sentiment: <span style="color:{p_color}">{p_score:.2f}</span></div>
</div>
""", unsafe_allow_html=True)