File size: 22,178 Bytes
e4b2bca
 
 
 
 
 
 
 
 
 
 
afd4a3d
e4b2bca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import requests
import numpy as np
from datetime import datetime, date, timedelta
import os

st.set_page_config(layout="wide")

API_KEY = os.getenv("FMP_API_KEY")

# -----------------------------
# Data Fetching
# -----------------------------
@st.cache_data
def fetch_analyst_ratings(ticker):
    """
    Fetches analyst consensus ratings for a given ticker.
    """
    url = f'https://financialmodelingprep.com/api/v4/upgrades-downgrades-consensus?symbol={ticker}&apikey={API_KEY}'
    try:
        r = requests.get(url)
        r.raise_for_status()
        data = r.json()
        return data[0] if data else None
    except Exception:
        return None

@st.cache_data
def fetch_detailed_ratings(ticker):
    """
    Fetches detailed analyst ratings for a given ticker.
    """
    url = f'https://financialmodelingprep.com/api/v4/upgrades-downgrades?symbol={ticker}&apikey={API_KEY}'
    try:
        r = requests.get(url)
        r.raise_for_status()
        data = r.json()
        df = pd.DataFrame(data) if data else pd.DataFrame()
        # Reorder columns if target columns exist
        if not df.empty and any(col in df.columns for col in ['newsBaseURL', 'newsPublisher', 'newsURL', 'newsTitle']):
            target_cols = ['newsBaseURL', 'newsPublisher', 'newsURL', 'newsTitle']
            other_cols = [c for c in df.columns if c not in target_cols]
            new_order = other_cols + target_cols
            df = df[new_order]
        return df
    except Exception:
        return pd.DataFrame()

@st.cache_data
def fetch_upgrades_downgrades_rss_feed(api_key, num_pages=5):
    """
    Fetches up to 'num_pages' pages of upgrades/downgrades data
    from the FMP RSS feed API.
    """
    all_data = []
    for page in range(num_pages):
        url = f"https://financialmodelingprep.com/api/v4/upgrades-downgrades-rss-feed?page={page}&apikey={api_key}"
        response = requests.get(url)
        if response.status_code == 200:
            data = response.json()
            all_data.extend(data)
        else:
            print(f"Error fetching page {page}: {response.status_code}")
    df = pd.DataFrame(all_data)
    if not df.empty and any(col in df.columns for col in ['newsBaseURL', 'newsPublisher', 'newsURL', 'newsTitle']):
        target_cols = ['newsBaseURL', 'newsPublisher', 'newsURL', 'newsTitle']
        other_cols = [c for c in df.columns if c not in target_cols]
        new_order = other_cols + target_cols
        df = df[new_order]
    return df

# -----------------------------
# Plotting
# -----------------------------
def plot_analyst_ratings(data):
    """
    Plots a horizontal bar chart of analyst consensus ratings.
    """
    df = pd.DataFrame({
        "rating": ["Strong Buy", "Buy", "Hold", "Sell", "Strong Sell"],
        "count": [
            data["strongBuy"], data["buy"], data["hold"], data["sell"], data["strongSell"]
        ]
    })
    total_ratings = df["count"].sum()
    df["percentage"] = (df["count"] / total_ratings * 100).round(0).astype(int)
    df["text"] = df["count"].astype(str) + " (" + df["percentage"].astype(str) + "%)"

    color_map = {
        "Strong Buy": "#145A32",
        "Buy": "#27ae60",
        "Hold": "#f1c40f",
        "Sell": "#e74c3c",
        "Strong Sell": "#641E16"
    }
    custom_colors = [color_map[r] for r in df["rating"]]

    fig = px.bar(
        df,
        x='count',
        y='rating',
        orientation='h',
        color='rating',
        color_discrete_sequence=custom_colors,
        text='text'
    )
    fig.update_traces(
        textposition='outside',
        textfont=dict(size=16, color="white"),
        textangle=0
    )
    fig.update_layout(
        template='plotly_dark',
        paper_bgcolor='#0e1117',
        plot_bgcolor='#0e1117',
        showlegend=False,
        xaxis=dict(showgrid=False, visible=False),
        yaxis=dict(title='', showgrid=False, tickfont=dict(size=18, color="white")),
        margin=dict(l=120, r=10, t=80, b=30),
        height=350,
        title=dict(
            text=(
                f"Analyst Consensus: {data['consensus']}<br>"
                f"<span style='font-size:0.8em;color:white'>Aggregate opinion from {total_ratings} analysts</span>"
            ),
            x=0.015,
            xanchor='left',
            y=0.9,
            yanchor='top',
            font=dict(size=25, color="white")
        ),
        width=None
    )
    return fig

def plot_sentiment_over_time(df):
    """
    Plots net sentiment changes over time.
    """
    df['publishedDate'] = pd.to_datetime(df['publishedDate'], errors='coerce')
    grade_map = {
        'Strong Buy': 2, 'Buy': 2, 'Overweight': 2, 'Outperform': 2,
        'Hold': 0, 'Neutral': 0,
        'Underperform': -2, 'Reduce': -2, 'Sell': -2
    }
    df['newGrade_score'] = df['newGrade'].map(grade_map)
    df['previousGrade_score'] = df['previousGrade'].map(grade_map)
    df['sentiment_change'] = df['newGrade_score'] - df['previousGrade_score']
    df['year_month'] = df['publishedDate'].dt.to_period('M')

    monthly_sentiment = df.groupby('year_month')['sentiment_change'].sum().reset_index()
    monthly_sentiment['year_month'] = monthly_sentiment['year_month'].astype(str)

    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=monthly_sentiment['year_month'],
        y=monthly_sentiment['sentiment_change'],
        mode='lines+markers',
        name='Net Sentiment',
        line=dict(color='blue', width=6),
        marker=dict(color='blue', size=8),
        hovertemplate='Month: %{x}<br>Sentiment: %{y}<extra></extra>'
    ))
    fig.update_layout(
        template='plotly_dark',
        paper_bgcolor='#0e1117',
        plot_bgcolor='#0e1117',
        #title="Analyst Sentiment Over Time",
        xaxis=dict(
            title=dict(text='Month', font=dict(color='green', size=20)),
            tickangle=45,
            tickfont=dict(color='white'),
            showgrid=True,
            gridcolor='white'
        ),
        yaxis=dict(
            title=dict(text='Sum of Sentiment Changes', font=dict(color='green', size=20)),
            tickfont=dict(color='white'),
            showgrid=True,
            gridcolor='white'
        ),
        font=dict(color='white'),
        margin=dict(l=40, r=40, t=80, b=80),
        width=None
    )
    return fig

def plot_actions_over_time(df):
    """
    Plots a stacked bar chart of analyst actions over time.
    """
    df['publishedDate'] = pd.to_datetime(df['publishedDate'])
    df['year_month'] = df['publishedDate'].dt.to_period('M')
    monthly_counts = df.groupby(['year_month','action'])['symbol'].count().reset_index(name='count')
    monthly_counts['year_month'] = monthly_counts['year_month'].astype(str)

    pivot_df = monthly_counts.pivot(
        index='year_month',
        columns='action',
        values='count'
    ).fillna(0)

    action_colors = {
        'upgrade': 'green',
        'downgrade': 'red',
        'hold': 'gray',
        'initiate': 'orange'
    }
    fig = go.Figure()
    for action in sorted(pivot_df.columns):
        fig.add_trace(go.Bar(
            x=pivot_df.index,
            y=pivot_df[action],
            name=action.capitalize(),
            marker_color=action_colors.get(action, 'blue'),
            hovertemplate=(
                "<b>%{x}</b><br>Action: " + action.capitalize() + "<br>Count: %{y}<extra></extra>"
            )
        ))
    fig.update_layout(
        barmode='stack',
        template='plotly_dark',
        paper_bgcolor='#0e1117',
        plot_bgcolor='#0e1117',
        #title="Upgrades vs. Downgrades vs. Holds Over Time",
        xaxis=dict(
            title=dict(text='Month', font=dict(color='green', size=20)),
            tickangle=45,
            tickfont=dict(color='white'),
            showgrid=True,
            gridcolor='white'
        ),
        yaxis=dict(
            title=dict(text='Count of Actions', font=dict(color='green', size=20)),
            tickfont=dict(color='white'),
            showgrid=True,
            gridcolor='white'
        ),
        legend=dict(font=dict(color='white')),
        margin=dict(l=40, r=40, t=80, b=80),
        width=None
    )
    return fig

def plot_animated_transition_heatmap(df):
    """
    Plots an animated heatmap of grade transitions over time.
    """
    df['publishedDate'] = pd.to_datetime(df['publishedDate'], errors='coerce')
    df['year_month'] = df['publishedDate'].dt.to_period('M')
    all_months = sorted(df['year_month'].dropna().unique())

    all_prev_grades = sorted(df['previousGrade'].dropna().unique())
    all_new_grades = sorted(df['newGrade'].dropna().unique())

    frames = []
    initial_data = None

    for idx, month in enumerate(all_months):
        monthly_df = df[df['year_month'] == month]
        transition_counts = monthly_df.groupby(
            ['previousGrade','newGrade']
        ).size().reset_index(name='count')

        z_data = np.zeros((len(all_prev_grades), len(all_new_grades)), dtype=int)
        for _, row in transition_counts.iterrows():
            r_idx = all_prev_grades.index(row['previousGrade'])
            c_idx = all_new_grades.index(row['newGrade'])
            z_data[r_idx, c_idx] = row['count']

        z_data_list = z_data.tolist()
        frames.append(
            go.Frame(
                data=[go.Heatmap(
                    z=z_data_list,
                    coloraxis="coloraxis",
                    hovertemplate="Prev: %{y}<br>New: %{x}<br>Count: %{z}<extra></extra>",
                    text=z_data_list,
                    texttemplate="%{text}",
                    textfont=dict(color="white"),
                    showscale=True
                )],
                name=str(month)
            )
        )
        if idx == 0:
            initial_data = z_data_list

    if initial_data is None:
        raise ValueError("No valid data found to plot.")

    fig = go.Figure(
        data=[go.Heatmap(
            z=initial_data,
            coloraxis="coloraxis",
            hovertemplate="Prev: %{y}<br>New: %{x}<br>Count: %{z}<extra></extra>",
            text=initial_data,
            texttemplate="%{text}",
            textfont=dict(color="white"),
            showscale=True
        )],
        layout=go.Layout(
            # title="Animated Transition Heatmap (Monthly)",
            xaxis=dict(
                title=dict(text="New Grade", font=dict(color="green", size=20), standoff=75),
                tickvals=list(range(len(all_new_grades))),
                ticktext=all_new_grades,
                tickfont=dict(color='white')
            ),
            yaxis=dict(
                title=dict(text="Previous Grade", font=dict(color="green", size=20)),
                tickvals=list(range(len(all_prev_grades))),
                ticktext=all_prev_grades,
                tickfont=dict(color='white'),
                autorange='reversed'
            ),
            template="plotly_dark",
            paper_bgcolor='#0e1117',
            plot_bgcolor='#0e1117'
        ),
        frames=frames
    )

    fig.update_layout(
        coloraxis=dict(colorscale="Blues"),
        updatemenus=[
            dict(
                type="buttons",
                showactive=False,
                x=0,            # Position from left
                y=1.2,          # Position from top
                xanchor="left", # Anchor the button's left side to x=0
                yanchor="top",  # Anchor the button's top to y=1
                buttons=[
                    dict(
                        label="Play Animation",
                        method="animate",
                        args=[
                            None,
                            {"frame": {"duration": 1000, "redraw": True},
                             "transition": {"duration": 500}}
                        ]
                    )
                ]
            )
        ],
        sliders=[
            dict(
                steps=[
                    dict(
                        method="animate",
                        args=[
                            [frame.name],
                            {
                                "mode": "immediate",
                                "frame": {"duration": 500, "redraw": True},
                                "transition": {"duration": 300}
                            }
                        ],
                        label=frame.name
                    )
                    for frame in frames
                ],
                x=0,
                y=-0.1,
                xanchor="left",
                yanchor="top",
                pad=dict(b=50, t=20),
                currentvalue=dict(prefix="Month: ", font=dict(color='white')),
                len=0.9
            )
        ],
        width=None
    )
    return fig

def plot_extreme_sentiment_symbols(
    df,
    start_date=None,
    end_date=None,
    top_n=10,
    title= ""
):
    """
    Identifies most bullish and bearish symbols based on sentiment changes.
    """
    df['publishedDate'] = pd.to_datetime(df['publishedDate'], errors='coerce')
    df['publishedDate'] = df['publishedDate'].dt.tz_localize(None)

    if start_date is not None:
        df = df[df['publishedDate'] >= pd.to_datetime(start_date)]
    if end_date is not None:
        df = df[df['publishedDate'] <= pd.to_datetime(end_date)]

    grade_map = {
        'Strong Buy': 2, 'Buy': 2, 'Overweight': 2, 'Outperform': 2,
        'Hold': 0, 'Neutral': 0,
        'Underperform': -2, 'Reduce': -2, 'Sell': -2
    }
    df['newGrade_score'] = df['newGrade'].map(grade_map)
    df['previousGrade_score'] = df['previousGrade'].map(grade_map)
    df['sentiment_change'] = df['newGrade_score'] - df['previousGrade_score']

    symbol_sentiment = df.groupby('symbol')['sentiment_change'].sum().reset_index()
    symbol_sentiment.sort_values('sentiment_change', ascending=False, inplace=True)

    top_bullish = symbol_sentiment.head(top_n)
    top_bearish = symbol_sentiment.tail(top_n)

    top_bullish['category'] = 'Bullish'
    top_bearish['category'] = 'Bearish'
    combined = pd.concat([top_bullish, top_bearish], ignore_index=True)

    fig = px.bar(
        combined,
        x='sentiment_change',
        y='symbol',
        color='category',
        orientation='h',
        title=title,
        text='sentiment_change',
        color_discrete_map={'Bullish': 'green', 'Bearish': 'red'}
    )
    n_bars = len(combined)
    fig.update_layout(
        height=50 + (40 * n_bars),
        template='plotly_dark',
        paper_bgcolor='#0e1117',
        plot_bgcolor='#0e1117',
        xaxis=dict(
            title=dict(text='Net Sentiment (Sum of Upgrades/Downgrades)', font=dict(color='green', size=20)),
            tickfont=dict(color='white')
        ),
        yaxis=dict(
            title=dict(text='Symbol', font=dict(color='green', size=20)),
            dtick=1,
            tickfont=dict(color='white')
        ),
        width=None
    )
    fig.update_traces(textposition='auto')
    return fig

# -----------------------------
# Sidebar and Pages
# -----------------------------
with st.sidebar:
    st.header("Parameters")
    with st.expander("Select Page", expanded=True):
        page = st.radio("Analyses", ["Actions by Ticker", "Actions Live Feed"], index=0)

    if page == "Actions by Ticker":
        with st.expander("Ticker Input", expanded=True):
            ticker = st.text_input(
                "Enter Ticker Symbol",
                value="AAPL",
                help="Enter a valid stock ticker symbol (e.g., AAPL)."
            )
        run_button = st.button("Run Analysis")

    else:
        with st.expander("Date Range", expanded=True):
            # In the sidebar for live feed:
            start_date = st.date_input(
                "Start Date",
                value=date.today() - timedelta(days=45),
                help="Select the start date."
            )
            end_date = st.date_input(
                "End Date",
                value=date.today(),
                help="Select the end date."
            )
        with st.expander("Display Options", expanded=True):
            top_n = st.slider(
                "Number of Symbols",
                min_value=5,
                max_value=20,
                value=10,
                help="How many bullish and bearish symbols to display."
            )
        run_button = st.button("Run Analysis")

st.title("Stock Downgrades and Upgrades")
st.write("This tool provides real-time updates on analyst ratings, shows sentiment trends over time, and displays recent analyst actions.")

if 'results' not in st.session_state:
    st.session_state.results = {}

# -----------------------------
# Page 1: Actions by Ticker
# -----------------------------
if page == "Actions by Ticker":
    if run_button:
        if not ticker.strip():
            st.error("Please enter a valid ticker symbol.")
        else:
            with st.spinner("Fetching data..."):
                consensus_data = fetch_analyst_ratings(ticker.upper())
                detailed_data = fetch_detailed_ratings(ticker.upper())
                st.session_state.results["consensus"] = consensus_data
                st.session_state.results["detailed"] = detailed_data

    if "consensus" in st.session_state.results and "detailed" in st.session_state.results:
        consensus_data = st.session_state.results["consensus"]
        detailed_data = st.session_state.results["detailed"]

        st.subheader(f"Analyst Consensus for {ticker.upper()}")
        st.write("This chart displays the overall analyst consensus rating for the stock. It shows the distribution of ratings such as Strong Buy, Buy, Hold, Sell, and Strong Sell.")
        if consensus_data:
            st.plotly_chart(plot_analyst_ratings(consensus_data), use_container_width=True)
        else:
            st.warning("No consensus data available.")

        st.subheader(f"Sentiment Trend Over Time for {ticker.upper()}")
        st.write("This line chart tracks changes in analyst sentiment over time. Net sentiment is computed by subtracting the previous rating score from the new rating score using a mapping (Strong Buy, Buy, Overweight, Outperform = 2; Hold, Neutral = 0; Underperform, Reduce, Sell = -2), which indicates the direction of rating adjustments each month.")
        if not detailed_data.empty:
            st.plotly_chart(plot_sentiment_over_time(detailed_data), use_container_width=True)
        else:
            st.warning("No detailed data available for sentiment analysis.")

        st.subheader(f"Analyst Actions Over Time for {ticker.upper()}")
        st.write("This stacked bar chart shows monthly counts of analyst actions. It shows the number of upgrades, downgrades, and holds over time.")
        if not detailed_data.empty:
            st.plotly_chart(plot_actions_over_time(detailed_data), use_container_width=True)
        else:
            st.warning("No detailed data available for action analysis.")

        st.subheader(f"Rating Transition Heatmap for {ticker.upper()}")
        st.write("This animated heatmap visualizes transitions between previous and new analyst ratings each month. It shows how frequently ratings change from one category to another. Use the slider or play button to observe changes for specific dates.")
        if not detailed_data.empty:
            try:
                st.plotly_chart(plot_animated_transition_heatmap(detailed_data), use_container_width=True)
            except ValueError:
                st.warning("Insufficient data for transition heatmap.")
        else:
            st.warning("No detailed data available for transitions.")

        st.subheader(f"Detailed Analyst Action Data for {ticker.upper()}")
        st.write("The table below provides the detailed information on each analyst action and the associated news data.")
        if not detailed_data.empty:
            with st.expander("View Detailed Results", expanded=False):
                st.dataframe(detailed_data)
        else:
            st.warning("No detailed data available.")

# -----------------------------
# Page 2: Actions Live Feed
# -----------------------------
else:
    if run_button:
        if start_date > end_date:
            st.error("Start date must be before end date.")
        else:
            with st.spinner("Fetching live feed data..."):
                rss_feed_df = fetch_upgrades_downgrades_rss_feed(API_KEY, num_pages=5)
                st.session_state.results["live_data"] = rss_feed_df

    if "live_data" in st.session_state.results:
        live_data = st.session_state.results["live_data"]
        if not live_data.empty:
            st.subheader("Top Bullish and Bearish Stocks")
            st.write("This bar chart displays stocks with the highest net sentiment. Net sentiment is computed by subtracting previous rating scores from new rating scores using a defined mapping (e.g., Strong Buy, Buy, Overweight, Outperform = +2; Hold, Neutral = 0; Underperform, Reduce, Sell = -2). This helps identify stocks viewed most positively and negatively by analysts.")
            st.plotly_chart(
                plot_extreme_sentiment_symbols(
                    live_data,
                    start_date=start_date,
                    end_date=end_date,
                    top_n=top_n
                ),
                use_container_width=True
            )

            st.subheader("Detailed Live Feed Data")
            st.write("This table provides a detailed view of the latest analyst upgrades, downgrades, and related news.")
            with st.expander("View Detailed Results", expanded=False):
                st.dataframe(live_data)
        else:
            st.warning("No live data returned from the feed.")
    else:
        st.info("Please run the analysis to fetch live data.")
        
        

# Hide default Streamlit style
st.markdown(
    """
    <style>
    #MainMenu {visibility: hidden;}
    footer {visibility: hidden;}
    </style>
    """,
    unsafe_allow_html=True
)