Dmitry Beresnev commited on
Commit ·
a584bff
1
Parent(s): f6443c4
add prediction markets, sectoral news, market events, economic calendar
Browse filesFiles changed (7)
- app/components/news.py+315
- app/pages/05_Dashboard.py+257-8
- app/services/economic_calendar.pyAdded+377
- app/services/market_events.pyAdded+391
- app/services/prediction_markets.pyAdded+411
- app/services/sectoral_news.pyAdded+426
- app/utils/news_cache.py+25-1
424 unmodified lines425426427424 unmodified lines</div>"""st.markdown(banner_html, unsafe_allow_html=True)424 unmodified lines425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742424 unmodified lines</div>"""st.markdown(banner_html, unsafe_allow_html=True)def display_prediction_card(prediction_item: dict):"""Display a single prediction market card with probability visualization."""# Escape HTML in texttitle = html_module.escape(prediction_item.get('title', '').strip())source = html_module.escape(prediction_item['source'])url = html_module.escape(prediction_item['url'])# Get probabilitiesyes_prob = prediction_item.get('yes_probability', 50.0)no_prob = prediction_item.get('no_probability', 50.0)# Determine bar color based on probabilitiesif yes_prob > 60:bar_color = '#089981' # Green - likely YESsentiment_text = 'YES LIKELY'elif no_prob > 60:bar_color = '#F23645' # Red - likely NOsentiment_text = 'NO LIKELY'else:bar_color = '#FF9800' # Orange - balancedsentiment_text = 'BALANCED'# Format end date if availableend_date = prediction_item.get('end_date')if end_date:if isinstance(end_date, str):end_date_display = end_dateelse:days_until = (end_date - datetime.now()).daysend_date_display = f"Closes in {days_until}d" if days_until > 0 else "Closed"else:end_date_display = ""# Volume displayvolume = prediction_item.get('volume', 0)if volume > 1000000:volume_display = f"${volume/1000000:.1f}M volume"elif volume > 1000:volume_display = f"${volume/1000:.1f}K volume"elif volume > 0:volume_display = f"${volume:.0f} volume"else:volume_display = ""# Prediction card HTMLcard_html = f"""<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%);border: 1px solid #2A2E39;border-radius: 8px;padding: 16px;margin-bottom: 12px;transition: all 0.2s ease;cursor: pointer;" onmouseover="this.style.borderColor='#3861FB'; this.style.transform='translateY(-2px)';"onmouseout="this.style.borderColor='#2A2E39'; this.style.transform='translateY(0)';"><!-- Header --><div style="margin-bottom: 12px;"><div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px;"><span style="color: #3861FB; font-weight: 600; font-size: 13px;">{source}</span><span style="background: {bar_color};color: white;padding: 2px 8px;border-radius: 4px;font-size: 10px;font-weight: 700;">{sentiment_text}</span></div><div style="color: #D1D4DC; font-size: 14px; font-weight: 500; line-height: 1.4; margin-bottom: 8px;">{title}</div></div><!-- Probability Visualization --><div style="margin-bottom: 10px;"><div style="display: flex; justify-content: space-between; margin-bottom: 4px;"><span style="color: #089981; font-size: 12px; font-weight: 600;">YES {yes_prob:.1f}%</span><span style="color: #F23645; font-size: 12px; font-weight: 600;">NO {no_prob:.1f}%</span></div><!-- Horizontal probability bar --><div style="display: flex;height: 8px;border-radius: 4px;overflow: hidden;background: #2A2E39;"><div style="width: {yes_prob}%;background: #089981;transition: width 0.3s ease;"></div><div style="width: {no_prob}%;background: #F23645;transition: width 0.3s ease;"></div></div></div><!-- Footer info --><div style="display: flex; justify-content: space-between; align-items: center;"><div style="color: #787B86; font-size: 11px;">{end_date_display}{" • " + volume_display if volume_display and end_date_display else volume_display}</div><a href="{url}" target="_blank" style="color: #3861FB;font-size: 11px;font-weight: 600;text-decoration: none;">View Market →</a></div></div>"""st.markdown(card_html, unsafe_allow_html=True)def display_economic_event_card(event_item: dict):"""Display a single economic event card with forecast/actual comparison."""# Escape HTMLtitle = html_module.escape(event_item.get('event_name', event_item.get('title', '')).strip())country = html_module.escape(event_item.get('country', 'US'))url = html_module.escape(event_item.get('url', ''))# Get valuesforecast = event_item.get('forecast')previous = event_item.get('previous')actual = event_item.get('actual')importance = event_item.get('importance', 'medium')# Importance badge colorimportance_colors = {'high': '#F23645','medium': '#FF9800','low': '#787B86'}importance_color = importance_colors.get(importance, '#787B86')# Time to eventtime_to_event = event_item.get('time_to_event', '')# Format values with unit detectiondef format_value(val):if val is None:return '-'if isinstance(val, (int, float)):# Check if it looks like a percentageif abs(val) < 100:return f"{val:.1f}%"else:return f"{val:.1f}"return str(val)forecast_display = format_value(forecast)previous_display = format_value(previous)actual_display = format_value(actual)# Determine if beat/missbeat_miss_html = ""if actual is not None and forecast is not None:if actual > forecast:beat_miss_html = '<span style="color: #089981; font-weight: 700;">[BEAT]</span>'elif actual < forecast:beat_miss_html = '<span style="color: #F23645; font-weight: 700;">[MISS]</span>'# Country flag emojiscountry_flags = {'US': '🇺🇸','EU': '🇪🇺','UK': '🇬🇧','JP': '🇯🇵','CN': '🇨🇳','CA': '🇨🇦','AU': '🇦🇺'}flag = country_flags.get(country, '🌍')# Event card HTMLcard_html = f"""<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%);border: 1px solid #2A2E39;border-radius: 8px;padding: 16px;margin-bottom: 12px;transition: all 0.2s ease;" onmouseover="this.style.borderColor='#3861FB'; this.style.transform='translateY(-2px)';"onmouseout="this.style.borderColor='#2A2E39'; this.style.transform='translateY(0)';"><!-- Header --><div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px;"><div style="flex: 1;"><div style="display: flex; align-items: center; gap: 8px; margin-bottom: 6px;"><span style="font-size: 20px;">{flag}</span><span style="background: {importance_color};color: white;padding: 2px 8px;border-radius: 4px;font-size: 10px;font-weight: 700;">{importance.upper()}</span></div><div style="color: #D1D4DC; font-size: 14px; font-weight: 500; line-height: 1.4;">{title}</div></div>{f'<div style="color: #3861FB; font-size: 12px; font-weight: 600; white-space: nowrap; margin-left: 12px;">{time_to_event}</div>' if time_to_event else ''}</div><!-- Values comparison --><div style="background: #0D0E13; border-radius: 6px; padding: 10px; margin-bottom: 8px;"><div style="display: flex; justify-content: space-between; margin-bottom: 6px;"><span style="color: #787B86; font-size: 11px;">Forecast:</span><span style="color: #D1D4DC; font-size: 12px; font-weight: 600;">{forecast_display}</span></div><div style="display: flex; justify-content: space-between; margin-bottom: 6px;"><span style="color: #787B86; font-size: 11px;">Previous:</span><span style="color: #D1D4DC; font-size: 12px; font-weight: 600;">{previous_display}</span></div>{f'<div style="display: flex; justify-content: space-between;"><span style="color: #787B86; font-size: 11px;">Actual:</span><span style="color: #D1D4DC; font-size: 12px; font-weight: 600;">{actual_display} {beat_miss_html}</span></div>' if actual is not None else ''}</div></div>"""st.markdown(card_html, unsafe_allow_html=True)def display_economic_calendar_widget(events_df: pd.DataFrame):"""Display economic calendar widget showing upcoming events."""if events_df.empty:st.info("📅 No upcoming economic events in the next 7 days")return# Widget containerwidget_html = """<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%);border: 1px solid #2A2E39;border-radius: 12px;padding: 20px;margin-bottom: 20px;"><div style="margin-bottom: 16px;"><h3 style="color: #D1D4DC; font-size: 18px; font-weight: 600; margin: 0;">📅 Economic Calendar</h3><p style="color: #787B86; font-size: 13px; margin: 4px 0 0 0;">Upcoming high-impact events</p></div>"""# Show top 10 eventsfor idx, event in events_df.head(10).iterrows():# Get event detailsevent_name = html_module.escape(event.get('event_name', event.get('title', '')))country = html_module.escape(event.get('country', 'US'))importance = event.get('importance', 'medium')time_to_event = event.get('time_to_event', '')forecast = event.get('forecast')# Country flagscountry_flags = {'US': '🇺🇸','EU': '🇪🇺','UK': '🇬🇧','JP': '🇯🇵','CN': '🇨🇳'}flag = country_flags.get(country, '🌍')# Importance starsstars = '⭐' * ({'high': 3, 'medium': 2, 'low': 1}.get(importance, 1))# Format forecastforecast_display = f"{forecast:.1f}" if forecast is not None else "N/A"event_html = f"""<div style="background: #0D0E13;border-left: 3px solid {'#F23645' if importance == 'high' else '#FF9800' if importance == 'medium' else '#787B86'};border-radius: 6px;padding: 12px;margin-bottom: 10px;"><div style="display: flex; justify-content: space-between; align-items: center;"><div style="flex: 1;"><div style="color: #D1D4DC; font-size: 13px; font-weight: 500; margin-bottom: 4px;">{flag} {event_name}</div><div style="color: #787B86; font-size: 11px;">{stars} Forecast: {forecast_display}</div></div><div style="color: #3861FB; font-size: 12px; font-weight: 600; white-space: nowrap; margin-left: 12px;">{time_to_event}</div></div></div>"""widget_html += event_htmlwidget_html += "</div>"st.markdown(widget_html, unsafe_allow_html=True)
14 unmodified lines1516171819202122 unmodified lines44454647484919 unmodified lines6970717273747576777886 unmodified lines16516616716816917017120 unmodified lines19219319419519619713 unmodified lines21121221321421521677 unmodified lines29429529629729829930030130230330430530630730830931031131210 unmodified lines32332432532632732832914 unmodified lines3443453463473483493 unmodified lines3533543553563573583593 unmodified lines36336436536636736836960 unmodified lines430431432433434435145 unmodified lines58158258358458558614 unmodified linesdisplay_news_statistics,display_category_breakdown,display_breaking_news_banner,display_scrollable_news_section)from utils.breaking_news_scorer import get_breaking_news_scorer22 unmodified linesexcept ImportError:AI_TECH_AVAILABLE = False# ---- Page Configuration ----st.set_page_config(19 unmodified linesif 'ai_tech_monitor' not in st.session_state and AI_TECH_AVAILABLE:st.session_state.ai_tech_monitor = AITechNewsScraper()rss_monitor = st.session_state.get('rss_monitor')twitter_monitor = st.session_state.get('twitter_monitor')reddit_monitor = st.session_state.get('reddit_monitor')ai_tech_monitor = st.session_state.get('ai_tech_monitor')# Initialize unified cache managerif 'news_cache_manager' not in st.session_state:86 unmodified linesreddit_sources = len(reddit_monitor.SUBREDDITS) if reddit_monitor else 0rss_sources = len(rss_monitor.SOURCES) if rss_monitor else 0ai_tech_sources = len(ai_tech_monitor.SOURCES) if ai_tech_monitor else 0total_sources = twitter_sources + reddit_sources + rss_sources + ai_tech_sourcesst.markdown(f"""<div style='font-size: 11px; line-height: 1.6;'>20 unmodified lines• TechCrunch • The Verge • VentureBeat• MIT Tech Review • Wired • Ars Technica**Total: {total_sources} Premium Sources**</div>""", unsafe_allow_html=True)13 unmodified linesrss_all_df = pd.DataFrame()rss_main_df = pd.DataFrame()ai_tech_df = pd.DataFrame()def fetch_twitter_news():"""Fetch Twitter/X news via cache manager"""77 unmodified linesreturn pd.DataFrame(), f"AI/Tech news unavailable: {e}"return pd.DataFrame(), None# Progressive loading: Display results as they arrive# Create a status placeholder to show progressstatus_placeholder = st.empty()# Execute all news fetching operations in parallel using ThreadPoolExecutorwith st.spinner("Loading news from 4 sources..."):with ThreadPoolExecutor(max_workers=4) as executor:# Submit all tasks with source name attachedfutures_map = {executor.submit(fetch_twitter_news): 'twitter',executor.submit(fetch_reddit_news): 'reddit',executor.submit(fetch_rss_news): 'rss',executor.submit(fetch_ai_tech_news): 'ai_tech'}# Track errors and completion10 unmodified lines# Update statuscompleted_sources.append(source_name)status_placeholder.info(f"🔍 Loaded {len(completed_sources)}/4 sources ({', '.join(completed_sources)})")if source_name == 'twitter':twitter_df = result_df14 unmodified linesai_tech_df = result_dfif error:fetch_errors.append(error)except Exception as e:fetch_errors.append(f"Error fetching {source_name} news: {e}")3 unmodified linesexcept TimeoutError:# Handle timeout gracefully - continue with whatever results we havefetch_errors.append("⏱️ Some sources timed out after 90 seconds - displaying available results")status_placeholder.warning(f"⚠️ {len(completed_sources)}/4 sources loaded (some timed out)")# Mark incomplete sourcesall_sources = set(futures_map.values())3 unmodified linescompleted_sources.append(f"{source} (timeout)")# Clear the status message after all sources completestatus_placeholder.success(f"✅ Loaded {len(completed_sources)}/4 sources successfully")# Debug output (remove in production)if st.session_state.get('debug_mode', False):60 unmodified linesst.markdown("---")# ---- FOUR-COLUMN SCROLLABLE NEWS LAYOUT (TradingView Style) ----col1, col2, col3, col4 = st.columns(4)145 unmodified lines</style>""", unsafe_allow_html=True)# Display fetch errors in expander (less intrusive)if 'fetch_errors' in locals() and fetch_errors:with st.expander("⚠️ Source Fetch Warnings", expanded=False):14 unmodified lines1516171819202122232422 unmodified lines47484950515253545556575859606162636465666768697071727374757619 unmodified lines9697989910010110210310410510610710810911011111211311411511611711811912012186 unmodified lines20820921021121221321421521621720 unmodified lines23823924024124224324424524624724824925025125225325413 unmodified lines26826927027127227327427527627777 unmodified lines35535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545610 unmodified lines46746846947047147247314 unmodified lines4884894904914924934944954964974984995005015025035045055065075085093 unmodified lines5135145155165175185193 unmodified lines52352452552652752852960 unmodified lines590591592593594595596597598599600145 unmodified lines74674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483514 unmodified linesdisplay_news_statistics,display_category_breakdown,display_breaking_news_banner,display_scrollable_news_section,display_prediction_card,display_economic_event_card,display_economic_calendar_widget)from utils.breaking_news_scorer import get_breaking_news_scorer22 unmodified linesexcept ImportError:AI_TECH_AVAILABLE = Falsetry:from services.prediction_markets import PredictionMarketsScraperPREDICTIONS_AVAILABLE = Trueexcept ImportError:PREDICTIONS_AVAILABLE = Falsetry:from services.sectoral_news import SectoralNewsScraperSECTORAL_AVAILABLE = Trueexcept ImportError:SECTORAL_AVAILABLE = Falsetry:from services.market_events import MarketEventsScraperEVENTS_AVAILABLE = Trueexcept ImportError:EVENTS_AVAILABLE = Falsetry:from services.economic_calendar import EconomicCalendarServiceCALENDAR_AVAILABLE = Trueexcept ImportError:CALENDAR_AVAILABLE = False# ---- Page Configuration ----st.set_page_config(19 unmodified linesif 'ai_tech_monitor' not in st.session_state and AI_TECH_AVAILABLE:st.session_state.ai_tech_monitor = AITechNewsScraper()if 'prediction_markets_monitor' not in st.session_state and PREDICTIONS_AVAILABLE:st.session_state.prediction_markets_monitor = PredictionMarketsScraper()if 'sectoral_news_monitor' not in st.session_state and SECTORAL_AVAILABLE:st.session_state.sectoral_news_monitor = SectoralNewsScraper()if 'market_events_monitor' not in st.session_state and EVENTS_AVAILABLE:st.session_state.market_events_monitor = MarketEventsScraper()if 'economic_calendar_service' not in st.session_state and CALENDAR_AVAILABLE:st.session_state.economic_calendar_service = EconomicCalendarService()rss_monitor = st.session_state.get('rss_monitor')twitter_monitor = st.session_state.get('twitter_monitor')reddit_monitor = st.session_state.get('reddit_monitor')ai_tech_monitor = st.session_state.get('ai_tech_monitor')prediction_markets_monitor = st.session_state.get('prediction_markets_monitor')sectoral_news_monitor = st.session_state.get('sectoral_news_monitor')market_events_monitor = st.session_state.get('market_events_monitor')economic_calendar_service = st.session_state.get('economic_calendar_service')# Initialize unified cache managerif 'news_cache_manager' not in st.session_state:86 unmodified linesreddit_sources = len(reddit_monitor.SUBREDDITS) if reddit_monitor else 0rss_sources = len(rss_monitor.SOURCES) if rss_monitor else 0ai_tech_sources = len(ai_tech_monitor.SOURCES) if ai_tech_monitor else 0prediction_sources = 3 # Polymarket, Metaculus, CME FedWatchsectoral_sources = 7 # 7 sectorsevents_sources = 3 # Earnings, indicators, central bankstotal_sources = twitter_sources + reddit_sources + rss_sources + ai_tech_sources + prediction_sources + sectoral_sources + events_sourcesst.markdown(f"""<div style='font-size: 11px; line-height: 1.6;'>20 unmodified lines• TechCrunch • The Verge • VentureBeat• MIT Tech Review • Wired • Ars Technica**Prediction Markets ({prediction_sources})**• Polymarket • Metaculus • CME FedWatch**Sectoral Coverage ({sectoral_sources})**• Finance • Tech • Energy • Healthcare• Consumer • Industrials • Real Estate**Market Events ({events_sources})**• Earnings Calendar • Economic Indicators• Central Bank Events (Fed, ECB, BoE, BoJ)**Total: {total_sources} Premium Sources**</div>""", unsafe_allow_html=True)13 unmodified linesrss_all_df = pd.DataFrame()rss_main_df = pd.DataFrame()ai_tech_df = pd.DataFrame()predictions_df = pd.DataFrame()sectoral_news_df = pd.DataFrame()market_events_df = pd.DataFrame()economic_calendar_df = pd.DataFrame()def fetch_twitter_news():"""Fetch Twitter/X news via cache manager"""77 unmodified linesreturn pd.DataFrame(), f"AI/Tech news unavailable: {e}"return pd.DataFrame(), Nonedef fetch_prediction_markets():"""Fetch prediction market data via cache manager"""try:if prediction_markets_monitor:predictions = cache_manager.get_news(source='predictions',fetcher_func=prediction_markets_monitor.scrape_predictions,force_refresh=force_refresh,max_items=50)if predictions:df = pd.DataFrame(predictions)if not df.empty:df['timestamp'] = pd.to_datetime(df['timestamp'])return df, Noneexcept Exception as e:return pd.DataFrame(), f"Prediction markets unavailable: {e}"return pd.DataFrame(), Nonedef fetch_sectoral_news():"""Fetch sectoral news via cache manager"""try:if sectoral_news_monitor:sectoral_news = cache_manager.get_news(source='sectoral_news',fetcher_func=sectoral_news_monitor.scrape_sectoral_news,force_refresh=force_refresh,max_items=50,hours=24)if sectoral_news:df = pd.DataFrame(sectoral_news)if not df.empty:df['timestamp'] = pd.to_datetime(df['timestamp'])return df, Noneexcept Exception as e:return pd.DataFrame(), f"Sectoral news unavailable: {e}"return pd.DataFrame(), Nonedef fetch_market_events():"""Fetch market events via cache manager"""try:if market_events_monitor:events = cache_manager.get_news(source='market_events',fetcher_func=market_events_monitor.scrape_market_events,force_refresh=force_refresh,max_items=50,days_ahead=14)if events:df = pd.DataFrame(events)if not df.empty:df['timestamp'] = pd.to_datetime(df['timestamp'])return df, Noneexcept Exception as e:return pd.DataFrame(), f"Market events unavailable: {e}"return pd.DataFrame(), Nonedef fetch_economic_calendar():"""Fetch economic calendar via cache manager"""try:if economic_calendar_service:calendar_events = cache_manager.get_news(source='economic_calendar',fetcher_func=economic_calendar_service.get_upcoming_events,force_refresh=force_refresh,days_ahead=7,min_importance='medium')if calendar_events:df = pd.DataFrame(calendar_events)if not df.empty:df['timestamp'] = pd.to_datetime(df['timestamp'])return df, Noneexcept Exception as e:return pd.DataFrame(), f"Economic calendar unavailable: {e}"return pd.DataFrame(), None# Progressive loading: Display results as they arrive# Create a status placeholder to show progressstatus_placeholder = st.empty()# Execute all news fetching operations in parallel using ThreadPoolExecutorwith st.spinner("Loading news from 8 sources..."):with ThreadPoolExecutor(max_workers=8) as executor:# Submit all tasks with source name attachedfutures_map = {executor.submit(fetch_twitter_news): 'twitter',executor.submit(fetch_reddit_news): 'reddit',executor.submit(fetch_rss_news): 'rss',executor.submit(fetch_ai_tech_news): 'ai_tech',executor.submit(fetch_prediction_markets): 'predictions',executor.submit(fetch_sectoral_news): 'sectoral_news',executor.submit(fetch_market_events): 'market_events',executor.submit(fetch_economic_calendar): 'economic_calendar'}# Track errors and completion10 unmodified lines# Update statuscompleted_sources.append(source_name)status_placeholder.info(f"🔍 Loaded {len(completed_sources)}/8 sources ({', '.join(completed_sources)})")if source_name == 'twitter':twitter_df = result_df14 unmodified linesai_tech_df = result_dfif error:fetch_errors.append(error)elif source_name == 'predictions':predictions_df = result_dfif error:fetch_errors.append(error)elif source_name == 'sectoral_news':sectoral_news_df = result_dfif error:fetch_errors.append(error)elif source_name == 'market_events':market_events_df = result_dfif error:fetch_errors.append(error)elif source_name == 'economic_calendar':economic_calendar_df = result_dfif error:fetch_errors.append(error)except Exception as e:fetch_errors.append(f"Error fetching {source_name} news: {e}")3 unmodified linesexcept TimeoutError:# Handle timeout gracefully - continue with whatever results we havefetch_errors.append("⏱️ Some sources timed out after 90 seconds - displaying available results")status_placeholder.warning(f"⚠️ {len(completed_sources)}/8 sources loaded (some timed out)")# Mark incomplete sourcesall_sources = set(futures_map.values())3 unmodified linescompleted_sources.append(f"{source} (timeout)")# Clear the status message after all sources completestatus_placeholder.success(f"✅ Loaded {len(completed_sources)}/8 sources successfully")# Debug output (remove in production)if st.session_state.get('debug_mode', False):60 unmodified linesst.markdown("---")# ---- ECONOMIC CALENDAR WIDGET ----if not economic_calendar_df.empty:display_economic_calendar_widget(economic_calendar_df)st.markdown("---")# ---- FOUR-COLUMN SCROLLABLE NEWS LAYOUT (TradingView Style) ----col1, col2, col3, col4 = st.columns(4)145 unmodified lines</style>""", unsafe_allow_html=True)# ---- SECOND ROW: MARKET INTELLIGENCE (3 COLUMNS) ----st.markdown("---")st.markdown("## 📊 Market Intelligence - Predictions, Sectors & Events")col5, col6, col7 = st.columns(3)with col5:# Prediction Markets Columnif not predictions_df.empty:display_scrollable_news_section(predictions_df,section_title="Prediction Markets",section_icon="🎲",section_subtitle="Polymarket, Metaculus & CME FedWatch",max_items=50,height="600px")else:st.markdown("""<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%); border: 1px solid #2A2E39; border-radius: 8px; padding: 30px; text-align: center;"><div style="font-size: 48px; margin-bottom: 16px; animation: pulse 2s ease-in-out infinite;">⏳</div><div style="color: #D1D4DC; font-size: 16px; font-weight: 600; margin-bottom: 8px;">Loading Prediction Markets</div><div style="color: #787B86; font-size: 13px;">Fetching market forecasts...</div></div><style>@keyframes pulse {0%, 100% { opacity: 1; transform: scale(1); }50% { opacity: 0.6; transform: scale(1.1); }}</style>""", unsafe_allow_html=True)with col6:# Sectoral News Columnif not sectoral_news_df.empty:display_scrollable_news_section(sectoral_news_df,section_title="Sectoral News",section_icon="🏭",section_subtitle="7 sectors: Finance, Tech, Energy & more",max_items=50,height="600px")else:st.markdown("""<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%); border: 1px solid #2A2E39; border-radius: 8px; padding: 30px; text-align: center;"><div style="font-size: 48px; margin-bottom: 16px; animation: pulse 2s ease-in-out infinite;">⏳</div><div style="color: #D1D4DC; font-size: 16px; font-weight: 600; margin-bottom: 8px;">Loading Sectoral News</div><div style="color: #787B86; font-size: 13px;">Aggregating sector-specific news...</div></div><style>@keyframes pulse {0%, 100% { opacity: 1; transform: scale(1); }50% { opacity: 0.6; transform: scale(1.1); }}</style>""", unsafe_allow_html=True)with col7:# Market Events Columnif not market_events_df.empty:display_scrollable_news_section(market_events_df,section_title="Market Events",section_icon="📈",section_subtitle="Earnings, indicators & central banks",max_items=50,height="600px")else:st.markdown("""<div style="background: linear-gradient(135deg, #1E222D 0%, #131722 100%); border: 1px solid #2A2E39; border-radius: 8px; padding: 30px; text-align: center;"><div style="font-size: 48px; margin-bottom: 16px; animation: pulse 2s ease-in-out infinite;">⏳</div><div style="color: #D1D4DC; font-size: 16px; font-weight: 600; margin-bottom: 8px;">Loading Market Events</div><div style="color: #787B86; font-size: 13px;">Fetching earnings & economic indicators...</div></div><style>@keyframes pulse {0%, 100% { opacity: 1; transform: scale(1); }50% { opacity: 0.6; transform: scale(1.1); }}</style>""", unsafe_allow_html=True)# Display fetch errors in expander (less intrusive)if 'fetch_errors' in locals() and fetch_errors:with st.expander("⚠️ Source Fetch Warnings", expanded=False):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377"""Economic Calendar Scraper - Investing.comScrapes upcoming economic events, indicators, and releasesNo API key required - web scraping approach"""
from datetime import datetime, timedeltafrom typing import List, Dict, Optionalimport loggingimport re
import requestsfrom bs4 import BeautifulSoup
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
class EconomicCalendarService: """ Scrapes economic calendar data from Investing.com Focus: High and medium importance events """
def __init__(self): """Initialize scraper with session""" self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.9', 'Referer': 'https://www.investing.com/', })
def get_upcoming_events(self, days_ahead: int = 7, min_importance: str = 'medium') -> List[Dict]: """ Get upcoming economic events Returns list of events in standardized format """ try: # Try to scrape from Investing.com events = self._scrape_investing_com(days_ahead, min_importance)
if events: logger.info(f"Scraped {len(events)} economic events from Investing.com") return events else: logger.warning("No events scraped - using mock data") return self._get_mock_events()
except Exception as e: logger.error(f"Error fetching economic calendar: {e}") return self._get_mock_events()
def _scrape_investing_com(self, days_ahead: int, min_importance: str) -> List[Dict]: """ Scrape economic calendar from Investing.com Note: This may be fragile and break if they change their HTML structure """ try: url = 'https://www.investing.com/economic-calendar/' response = self.session.get(url, timeout=10) response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser') events = []
# Investing.com uses a table structure for the calendar # Look for table rows with event data calendar_table = soup.find('table', {'id': 'economicCalendarData'})
if not calendar_table: logger.warning("Could not find economic calendar table on Investing.com") return []
rows = calendar_table.find_all('tr', {'class': 'js-event-item'})
for row in rows[:50]: # Limit to 50 events try: # Extract event data from row event_data = self._parse_event_row(row)
if event_data and self._should_include_event(event_data, days_ahead, min_importance): events.append(event_data)
except Exception as e: logger.debug(f"Error parsing event row: {e}") continue
return events
except Exception as e: logger.error(f"Error scraping Investing.com: {e}") return []
def _parse_event_row(self, row) -> Optional[Dict]: """Parse a single event row from Investing.com table""" try: # Extract timestamp timestamp_elem = row.find('td', {'class': 'first left time'}) time_str = timestamp_elem.get_text(strip=True) if timestamp_elem else ''
# Extract country country_elem = row.find('td', {'class': 'flagCur'}) country = country_elem.get('title', 'US') if country_elem else 'US'
# Extract importance (bull icons) importance_elem = row.find('td', {'class': 'sentiment'}) importance = self._parse_importance(importance_elem) if importance_elem else 'low'
# Extract event name event_elem = row.find('td', {'class': 'left event'}) event_name = event_elem.get_text(strip=True) if event_elem else ''
# Extract actual, forecast, previous values actual_elem = row.find('td', {'id': re.compile('eventActual_')}) forecast_elem = row.find('td', {'id': re.compile('eventForecast_')}) previous_elem = row.find('td', {'id': re.compile('eventPrevious_')})
actual = self._parse_value(actual_elem.get_text(strip=True) if actual_elem else '') forecast = self._parse_value(forecast_elem.get_text(strip=True) if forecast_elem else '') previous = self._parse_value(previous_elem.get_text(strip=True) if previous_elem else '')
# Create event dictionary event_date = self._parse_event_time(time_str) time_to_event = self._calculate_time_to_event(event_date)
return { 'id': hash(f"{event_name}_{event_date}_{country}"), 'title': f"{country} - {event_name}", 'event_name': event_name, 'event_date': event_date, 'country': country, 'category': self._categorize_event(event_name), 'importance': importance, 'forecast': forecast, 'previous': previous, 'actual': actual, 'time_to_event': time_to_event, 'timestamp': datetime.now(), 'source': 'Investing.com', 'url': 'https://www.investing.com/economic-calendar/', 'impact': importance, # Map importance to impact 'sentiment': self._determine_sentiment(actual, forecast, previous) }
except Exception as e: logger.debug(f"Error parsing event row: {e}") return None
def _parse_importance(self, importance_elem) -> str: """Parse importance from bull icons""" if not importance_elem: return 'low'
# Investing.com uses bull icons (1-3 bulls) bulls = importance_elem.find_all('i', {'class': 'grayFullBullishIcon'}) num_bulls = len(bulls)
if num_bulls >= 3: return 'high' elif num_bulls == 2: return 'medium' else: return 'low'
def _parse_value(self, value_str: str) -> Optional[float]: """Parse numeric value from string""" if not value_str or value_str == '' or value_str == '-': return None
try: # Remove % sign, K, M, B suffixes value_str = value_str.replace('%', '').replace('K', '').replace('M', '').replace('B', '') value_str = value_str.replace(',', '') return float(value_str) except: return None
def _parse_event_time(self, time_str: str) -> datetime: """Parse event time string to datetime""" try: # Investing.com uses formats like "10:00" or "All Day" if 'All Day' in time_str or not time_str: # Default to noon today return datetime.now().replace(hour=12, minute=0, second=0, microsecond=0)
# Parse time (assumes today for now - real implementation would need date context) time_parts = time_str.split(':') hour = int(time_parts[0]) minute = int(time_parts[1]) if len(time_parts) > 1 else 0
event_time = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
# If time has passed today, assume it's tomorrow if event_time < datetime.now(): event_time += timedelta(days=1)
return event_time
except Exception as e: logger.debug(f"Error parsing time: {e}") return datetime.now() + timedelta(hours=2)
def _calculate_time_to_event(self, event_date: datetime) -> str: """Calculate human-readable time until event""" delta = event_date - datetime.now()
if delta.total_seconds() < 0: return "In progress"
days = delta.days hours = delta.seconds // 3600 minutes = (delta.seconds % 3600) // 60
if days > 0: return f"in {days}d {hours}h" elif hours > 0: return f"in {hours}h {minutes}m" else: return f"in {minutes}m"
def _categorize_event(self, event_name: str) -> str: """Categorize economic event""" event_lower = event_name.lower()
if any(kw in event_lower for kw in ['cpi', 'inflation', 'pce', 'price']): return 'inflation' elif any(kw in event_lower for kw in ['employment', 'jobs', 'unemployment', 'nfp', 'payroll']): return 'employment' elif any(kw in event_lower for kw in ['gdp', 'growth']): return 'gdp' elif any(kw in event_lower for kw in ['fed', 'fomc', 'ecb', 'rate', 'boe', 'boj']): return 'central_bank' elif any(kw in event_lower for kw in ['pmi', 'manufacturing', 'services']): return 'pmi' else: return 'other'
def _determine_sentiment(self, actual: Optional[float], forecast: Optional[float], previous: Optional[float]) -> str: """Determine sentiment based on actual vs forecast""" if actual is None or forecast is None: return 'neutral'
if actual > forecast: return 'positive' # Beat forecast elif actual < forecast: return 'negative' # Missed forecast else: return 'neutral'
def _should_include_event(self, event: Dict, days_ahead: int, min_importance: str) -> bool: """Determine if event should be included""" # Filter by importance importance_levels = ['low', 'medium', 'high'] min_level = importance_levels.index(min_importance) event_level = importance_levels.index(event['importance'])
if event_level < min_level: return False
# Filter by date range days_until = (event['event_date'] - datetime.now()).days if days_until > days_ahead: return False
return True
def _get_mock_events(self) -> List[Dict]: """Mock economic events for development/testing""" now = datetime.now()
return [ { 'id': 1, 'title': 'US - Consumer Price Index (CPI)', 'event_name': 'Consumer Price Index', 'event_date': now + timedelta(hours=2), 'country': 'US', 'category': 'inflation', 'importance': 'high', 'forecast': 2.5, 'previous': 2.3, 'actual': None, 'time_to_event': 'in 2h 0m', 'timestamp': now, 'source': 'Economic Calendar', 'url': 'https://www.investing.com/economic-calendar/', 'impact': 'high', 'sentiment': 'neutral' }, { 'id': 2, 'title': 'US - Non-Farm Payrolls (NFP)', 'event_name': 'Non-Farm Payrolls', 'event_date': now + timedelta(days=2, hours=8, minutes=30), 'country': 'US', 'category': 'employment', 'importance': 'high', 'forecast': 180.0, 'previous': 175.0, 'actual': None, 'time_to_event': 'in 2d 8h', 'timestamp': now, 'source': 'Economic Calendar', 'url': 'https://www.investing.com/economic-calendar/', 'impact': 'high', 'sentiment': 'neutral' }, { 'id': 3, 'title': 'EU - ECB Interest Rate Decision', 'event_name': 'ECB Interest Rate Decision', 'event_date': now + timedelta(days=3, hours=12), 'country': 'EU', 'category': 'central_bank', 'importance': 'high', 'forecast': 3.75, 'previous': 4.00, 'actual': None, 'time_to_event': 'in 3d 12h', 'timestamp': now, 'source': 'Economic Calendar', 'url': 'https://www.investing.com/economic-calendar/', 'impact': 'high', 'sentiment': 'neutral' }, { 'id': 4, 'title': 'US - GDP Growth Rate', 'event_name': 'GDP Growth Rate', 'event_date': now + timedelta(days=5, hours=8, minutes=30), 'country': 'US', 'category': 'gdp', 'importance': 'high', 'forecast': 2.8, 'previous': 2.5, 'actual': None, 'time_to_event': 'in 5d 8h', 'timestamp': now, 'source': 'Economic Calendar', 'url': 'https://www.investing.com/economic-calendar/', 'impact': 'high', 'sentiment': 'neutral' }, { 'id': 5, 'title': 'US - Manufacturing PMI', 'event_name': 'Manufacturing PMI', 'event_date': now + timedelta(days=1, hours=10), 'country': 'US', 'category': 'pmi', 'importance': 'medium', 'forecast': 51.5, 'previous': 50.8, 'actual': None, 'time_to_event': 'in 1d 10h', 'timestamp': now, 'source': 'Economic Calendar', 'url': 'https://www.investing.com/economic-calendar/', 'impact': 'medium', 'sentiment': 'neutral' } ]
def get_todays_events(self) -> List[Dict]: """Get events happening today""" all_events = self.get_upcoming_events(days_ahead=1) today = datetime.now().date()
todays_events = [ event for event in all_events if event['event_date'].date() == today ]
return todays_events123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391"""Market Events Scraper - Earnings, Economic Indicators & Central Bank EventsAggregates upcoming and recent market-moving eventsWeb scraping approach - no API keys required"""
from datetime import datetime, timedeltafrom typing import List, Dict, Optionalimport loggingimport refrom concurrent.futures import ThreadPoolExecutor
import requestsimport feedparserfrom bs4 import BeautifulSoup
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
class MarketEventsScraper: """ Scrapes market events from multiple sources Focus: Earnings, economic indicators, central bank announcements """
# Central bank RSS feeds (already in use for news) CENTRAL_BANKS = { 'fed': { 'name': 'Federal Reserve', 'rss': 'https://www.federalreserve.gov/feeds/press_all.xml', 'weight': 2.0 }, 'ecb': { 'name': 'European Central Bank', 'rss': 'https://www.ecb.europa.eu/rss/press.xml', 'weight': 2.0 } }
def __init__(self): """Initialize scraper""" self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.9', })
def scrape_market_events(self, max_items: int = 50, days_ahead: int = 14) -> List[Dict]: """ Scrape market events from all sources Returns unified list sorted by date and impact """ all_events = [] seen_urls = set()
# Parallel fetching with ThreadPoolExecutor(max_workers=3) as executor: futures = []
# Submit tasks futures.append((executor.submit(self._fetch_earnings), 'earnings')) futures.append((executor.submit(self._fetch_economic_indicators), 'indicators')) futures.append((executor.submit(self._fetch_central_bank_events), 'central_banks'))
for future, source_type in futures: try: events = future.result(timeout=35)
# Deduplicate by URL for event in events: if event['url'] not in seen_urls: seen_urls.add(event['url']) all_events.append(event)
logger.info(f"Fetched {len(events)} events from {source_type}")
except Exception as e: logger.error(f"Error fetching {source_type}: {e}")
# If no events fetched, use mock data if not all_events: logger.warning("No market events fetched - using mock data") return self._get_mock_events()
# Sort by event date and impact all_events.sort( key=lambda x: (x.get('event_date', x['timestamp']), x['impact'] != 'high'), )
return all_events[:max_items]
def _fetch_earnings(self) -> List[Dict]: """ Fetch earnings calendar from Yahoo Finance Web scraping approach """ try: url = 'https://finance.yahoo.com/calendar/earnings' response = self.session.get(url, timeout=10) response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser') events = []
# Yahoo Finance uses a table for earnings table = soup.find('table', {'class': re.compile('earnings')})
if not table: logger.warning("Could not find earnings table on Yahoo Finance") return self._get_mock_earnings()
rows = table.find_all('tr')[1:20] # Skip header, limit to 20
for row in rows: try: cells = row.find_all('td') if len(cells) < 4: continue
# Parse cells ticker = cells[0].get_text(strip=True) company = cells[1].get_text(strip=True) if len(cells) > 1 else ticker eps_estimate = cells[2].get_text(strip=True) if len(cells) > 2 else 'N/A' reported_eps = cells[3].get_text(strip=True) if len(cells) > 3 else None event_time = cells[4].get_text(strip=True) if len(cells) > 4 else 'N/A'
# Create event event_date = self._parse_earnings_date(event_time)
events.append({ 'id': hash(f"earnings_{ticker}_{event_date}"), 'title': f"{company} ({ticker}) Earnings Report", 'summary': f"Expected EPS: {eps_estimate}" + (f", Reported: {reported_eps}" if reported_eps and reported_eps != 'N/A' else ''), 'source': 'Yahoo Finance', 'category': 'earnings', 'timestamp': datetime.now(), 'event_date': event_date, 'url': f"https://finance.yahoo.com/quote/{ticker}", 'event_type': 'earnings', 'ticker': ticker, 'expected_value': self._parse_float(eps_estimate), 'actual_value': self._parse_float(reported_eps) if reported_eps else None, 'previous_value': None, 'impact': 'medium', # Earnings are generally medium impact 'sentiment': self._determine_earnings_sentiment(eps_estimate, reported_eps), 'is_breaking': False, 'source_weight': 1.3, 'likes': 0, 'retweets': 0 })
except Exception as e: logger.debug(f"Error parsing earnings row: {e}") continue
return events if events else self._get_mock_earnings()
except Exception as e: logger.error(f"Error fetching earnings: {e}") return self._get_mock_earnings()
def _fetch_economic_indicators(self) -> List[Dict]: """ Fetch economic indicators from FRED and other sources Uses RSS feeds """ try: events = []
# FRED Economic Data releases (via RSS - if available) # For now, use mock data as FRED RSS is primarily historical data # Real implementation would scrape FRED release calendar
events.extend(self._get_mock_indicators())
return events
except Exception as e: logger.error(f"Error fetching economic indicators: {e}") return self._get_mock_indicators()
def _fetch_central_bank_events(self) -> List[Dict]: """ Fetch central bank announcements from RSS feeds """ events = []
for bank_id, bank_info in self.CENTRAL_BANKS.items(): try: feed = feedparser.parse(bank_info['rss'])
for entry in feed.entries[:10]: try: # Parse timestamp if hasattr(entry, 'published_parsed') and entry.published_parsed: timestamp = datetime(*entry.published_parsed[:6]) else: timestamp = datetime.now()
# Skip old events (>7 days) if (datetime.now() - timestamp).days > 7: continue
title = entry.get('title', '') summary = entry.get('summary', '') or title url = entry.get('link', '')
# Clean HTML from summary if summary: summary = BeautifulSoup(summary, 'html.parser').get_text() summary = summary[:200] + '...' if len(summary) > 200 else summary
events.append({ 'id': hash(url), 'title': f"{bank_info['name']}: {title}", 'summary': summary, 'source': bank_info['name'], 'category': 'central_bank', 'timestamp': timestamp, 'event_date': timestamp, 'url': url, 'event_type': 'central_bank_announcement', 'ticker': None, 'expected_value': None, 'actual_value': None, 'previous_value': None, 'impact': 'high', # Central bank events are high impact 'sentiment': 'neutral', 'is_breaking': (datetime.now() - timestamp).days < 1, 'source_weight': bank_info['weight'], 'likes': 0, 'retweets': 0 })
except Exception as e: logger.debug(f"Error parsing {bank_id} entry: {e}") continue
except Exception as e: logger.error(f"Error fetching {bank_id} RSS: {e}")
return events
def _parse_earnings_date(self, time_str: str) -> datetime: """Parse earnings report time""" # Yahoo Finance uses "Before Market Open", "After Market Close", or specific dates now = datetime.now()
if 'Before Market' in time_str or 'BMO' in time_str: return now.replace(hour=7, minute=0, second=0, microsecond=0) elif 'After Market' in time_str or 'AMC' in time_str: return now.replace(hour=16, minute=0, second=0, microsecond=0) else: # Default to tomorrow morning return (now + timedelta(days=1)).replace(hour=7, minute=0, second=0, microsecond=0)
def _parse_float(self, value_str: str) -> Optional[float]: """Parse float from string""" if not value_str or value_str == 'N/A' or value_str == '-': return None
try: # Remove $ and other non-numeric characters except . and - cleaned = re.sub(r'[^\d.-]', '', value_str) return float(cleaned) except: return None
def _determine_earnings_sentiment(self, expected: str, actual: Optional[str]) -> str: """Determine sentiment based on earnings beat/miss""" if not actual or actual == 'N/A': return 'neutral'
exp_val = self._parse_float(expected) act_val = self._parse_float(actual)
if exp_val is None or act_val is None: return 'neutral'
if act_val > exp_val: return 'positive' # Beat elif act_val < exp_val: return 'negative' # Miss else: return 'neutral' # In-line
def _get_mock_earnings(self) -> List[Dict]: """Mock earnings data""" now = datetime.now()
return [ { 'id': 1, 'title': 'Apple Inc. (AAPL) Earnings Report', 'summary': 'Expected EPS: $2.10', 'source': 'Yahoo Finance', 'category': 'earnings', 'timestamp': now, 'event_date': now + timedelta(days=2, hours=16), 'url': 'https://finance.yahoo.com/quote/AAPL', 'event_type': 'earnings', 'ticker': 'AAPL', 'expected_value': 2.10, 'actual_value': None, 'previous_value': 1.95, 'impact': 'high', 'sentiment': 'neutral', 'is_breaking': False, 'source_weight': 1.5, 'likes': 0, 'retweets': 0 }, { 'id': 2, 'title': 'Microsoft Corporation (MSFT) Earnings Report', 'summary': 'Expected EPS: $2.75', 'source': 'Yahoo Finance', 'category': 'earnings', 'timestamp': now, 'event_date': now + timedelta(days=3, hours=16), 'url': 'https://finance.yahoo.com/quote/MSFT', 'event_type': 'earnings', 'ticker': 'MSFT', 'expected_value': 2.75, 'actual_value': None, 'previous_value': 2.50, 'impact': 'high', 'sentiment': 'neutral', 'is_breaking': False, 'source_weight': 1.5, 'likes': 0, 'retweets': 0 } ]
def _get_mock_indicators(self) -> List[Dict]: """Mock economic indicator data""" now = datetime.now()
return [ { 'id': 3, 'title': 'US Retail Sales Data Release', 'summary': 'Monthly retail sales figures', 'source': 'US Census Bureau', 'category': 'economic_indicator', 'timestamp': now, 'event_date': now + timedelta(days=1, hours=8, minutes=30), 'url': 'https://www.census.gov/retail/', 'event_type': 'retail_sales', 'ticker': None, 'expected_value': 0.5, 'actual_value': None, 'previous_value': 0.3, 'impact': 'medium', 'sentiment': 'neutral', 'is_breaking': False, 'source_weight': 1.6, 'likes': 0, 'retweets': 0 } ]
def _get_mock_events(self) -> List[Dict]: """Combined mock data""" return self._get_mock_earnings() + self._get_mock_indicators() + [ { 'id': 4, 'title': 'Federal Reserve: FOMC Meeting Minutes Released', 'summary': 'Minutes from the latest Federal Open Market Committee meeting', 'source': 'Federal Reserve', 'category': 'central_bank', 'timestamp': datetime.now() - timedelta(hours=2), 'event_date': datetime.now() - timedelta(hours=2), 'url': 'https://www.federalreserve.gov/', 'event_type': 'central_bank_announcement', 'ticker': None, 'expected_value': None, 'actual_value': None, 'previous_value': None, 'impact': 'high', 'sentiment': 'neutral', 'is_breaking': True, 'source_weight': 2.0, 'likes': 0, 'retweets': 0 } ]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411"""Prediction Markets Scraper - Polymarket, Metaculus & CME FedWatchAggregates market predictions for financial, political, and geopolitical eventsNo authentication required - all free/public APIs"""
from datetime import datetime, timedeltafrom typing import List, Dict, Optionalimport loggingimport refrom concurrent.futures import ThreadPoolExecutor
import requestsimport pandas as pdfrom bs4 import BeautifulSoup
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
class PredictionMarketsScraper: """ Scrapes prediction market data from multiple sources Focus: Economics, geopolitics, markets """
# Source configuration SOURCES = { 'polymarket': { 'name': 'Polymarket', 'base_url': 'https://clob.polymarket.com', 'weight': 1.8, 'enabled': True }, 'metaculus': { 'name': 'Metaculus', 'base_url': 'https://www.metaculus.com/api', 'weight': 1.6, 'enabled': True }, 'cme_fedwatch': { 'name': 'CME FedWatch', 'url': 'https://www.cmegroup.com/markets/interest-rates/cme-fedwatch-tool.html', 'weight': 2.0, 'enabled': True } }
# Category keywords MACRO_KEYWORDS = ['Fed', 'ECB', 'inflation', 'CPI', 'GDP', 'rate', 'economy'] MARKETS_KEYWORDS = ['stock', 'market', 'S&P', 'Dow', 'price', 'Bitcoin', 'crypto'] GEOPOLITICAL_KEYWORDS = ['election', 'war', 'Trump', 'Biden', 'China', 'Russia', 'Ukraine']
def __init__(self): """Initialize scraper with session""" self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36', 'Accept': 'application/json', 'Accept-Language': 'en-US,en;q=0.9', })
def scrape_predictions(self, max_items: int = 50) -> List[Dict]: """ Scrape predictions from all enabled sources Returns unified list of prediction markets """ all_predictions = [] seen_titles = set()
# Parallel fetching with ThreadPoolExecutor(max_workers=3) as executor: futures = []
if self.SOURCES['polymarket']['enabled']: futures.append((executor.submit(self._fetch_polymarket), 'polymarket'))
if self.SOURCES['metaculus']['enabled']: futures.append((executor.submit(self._fetch_metaculus), 'metaculus'))
if self.SOURCES['cme_fedwatch']['enabled']: futures.append((executor.submit(self._fetch_cme_fedwatch), 'cme_fedwatch'))
for future, source_name in futures: try: predictions = future.result(timeout=35)
# Deduplicate by title similarity for pred in predictions: title_norm = pred['title'].lower().strip() if title_norm not in seen_titles: seen_titles.add(title_norm) all_predictions.append(pred)
logger.info(f"Fetched {len(predictions)} predictions from {source_name}")
except Exception as e: logger.error(f"Error fetching {source_name}: {e}")
# If no predictions fetched, use mock data if not all_predictions: logger.warning("No predictions fetched - using mock data") return self._get_mock_predictions()
# Sort by volume (if available) and impact all_predictions.sort( key=lambda x: (x['impact'] == 'high', x.get('volume', 0)), reverse=True )
return all_predictions[:max_items]
def _fetch_polymarket(self) -> List[Dict]: """Fetch predictions from Polymarket API""" try: # Polymarket CLOB API - get active markets url = f"{self.SOURCES['polymarket']['base_url']}/markets"
response = self.session.get(url, timeout=10) response.raise_for_status()
markets = response.json() predictions = []
for market in markets[:30]: # Limit to 30 most recent try: # Parse market data title = market.get('question', '') if not title or len(title) < 10: continue
# Get probabilities (0-1 range, convert to 0-100) yes_prob = float(market.get('outcome_prices', ['0.5', '0.5'])[0]) * 100 no_prob = 100 - yes_prob
# Calculate volume volume = float(market.get('volume', 0))
# Category classification category = self._categorize_prediction(title)
# Impact based on volume impact = self._assess_impact(volume, category)
# Sentiment from probability sentiment = 'positive' if yes_prob > 60 else ('negative' if yes_prob < 40 else 'neutral')
# End date end_date_str = market.get('end_date_iso', '') try: end_date = datetime.fromisoformat(end_date_str.replace('Z', '+00:00')) except: end_date = datetime.now() + timedelta(days=30)
predictions.append({ 'id': hash(market.get('condition_id', title)), 'title': title, 'summary': f"Market probability: {yes_prob:.1f}% YES, {no_prob:.1f}% NO", 'source': 'Polymarket', 'category': category, 'timestamp': datetime.now(), 'url': f"https://polymarket.com/event/{market.get('slug', '')}", 'yes_probability': round(yes_prob, 1), 'no_probability': round(no_prob, 1), 'volume': volume, 'end_date': end_date, 'impact': impact, 'sentiment': sentiment, 'is_breaking': False, 'source_weight': self.SOURCES['polymarket']['weight'], 'likes': int(volume / 1000), # Approximate engagement from volume 'retweets': 0 })
except Exception as e: logger.debug(f"Error parsing Polymarket market: {e}") continue
return predictions
except Exception as e: logger.error(f"Error fetching Polymarket: {e}") return []
def _fetch_metaculus(self) -> List[Dict]: """Fetch predictions from Metaculus API""" try: # Metaculus API - get open questions url = f"{self.SOURCES['metaculus']['base_url']}/questions/" params = { 'status': 'open', 'type': 'forecast', 'order_by': '-activity', 'limit': 30 }
response = self.session.get(url, params=params, timeout=10) response.raise_for_status()
data = response.json() questions = data.get('results', []) predictions = []
for q in questions: try: title = q.get('title', '') if not title or len(title) < 10: continue
# Get community prediction community_prediction = q.get('community_prediction', {}) if not community_prediction: continue
# For binary questions if q.get('possibilities', {}).get('type') == 'binary': yes_prob = float(community_prediction.get('q2', 0.5)) * 100 no_prob = 100 - yes_prob else: # Skip non-binary for now continue
# Category classification category = self._categorize_prediction(title)
# Impact based on number of forecasters num_forecasters = q.get('number_of_forecasters', 0) impact = 'high' if num_forecasters > 100 else ('medium' if num_forecasters > 20 else 'low')
# Sentiment sentiment = 'positive' if yes_prob > 60 else ('negative' if yes_prob < 40 else 'neutral')
# Close date close_time_str = q.get('close_time', '') try: close_time = datetime.fromisoformat(close_time_str.replace('Z', '+00:00')) except: close_time = datetime.now() + timedelta(days=30)
predictions.append({ 'id': q.get('id', hash(title)), 'title': title, 'summary': f"Community forecast: {yes_prob:.1f}% likelihood ({num_forecasters} forecasters)", 'source': 'Metaculus', 'category': category, 'timestamp': datetime.now(), 'url': q.get('url', f"https://www.metaculus.com/questions/{q.get('id')}"), 'yes_probability': round(yes_prob, 1), 'no_probability': round(no_prob, 1), 'volume': 0, # Metaculus doesn't have trading volume 'end_date': close_time, 'impact': impact, 'sentiment': sentiment, 'is_breaking': False, 'source_weight': self.SOURCES['metaculus']['weight'], 'likes': num_forecasters, 'retweets': 0 })
except Exception as e: logger.debug(f"Error parsing Metaculus question: {e}") continue
return predictions
except Exception as e: logger.error(f"Error fetching Metaculus: {e}") return []
def _fetch_cme_fedwatch(self) -> List[Dict]: """ Fetch Fed rate probabilities from CME FedWatch Tool Note: This is web scraping and may be fragile """ try: url = self.SOURCES['cme_fedwatch']['url'] response = self.session.get(url, timeout=10) response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# CME FedWatch has a data table with meeting dates and probabilities # This is a simplified version - actual implementation may need adjustment # based on current page structure
predictions = []
# Try to find probability data in script tags (CME often embeds data in JSON) scripts = soup.find_all('script') for script in scripts: if script.string and 'probability' in script.string.lower(): # This would need custom parsing based on CME's data format # For now, create mock Fed predictions logger.warning("CME FedWatch scraping not fully implemented - using mock Fed data") break
# Fallback: Create mock Fed rate prediction next_fomc = datetime.now() + timedelta(days=45) # Approximate next FOMC predictions.append({ 'id': hash('fed_rate_' + next_fomc.strftime('%Y%m%d')), 'title': f'Fed Rate Decision - {next_fomc.strftime("%B %Y")} FOMC', 'summary': 'Market-implied probability of rate changes based on fed funds futures', 'source': 'CME FedWatch', 'category': 'macro', 'timestamp': datetime.now(), 'url': url, 'yes_probability': 65.0, # Probability of rate cut 'no_probability': 35.0, # Probability of no change 'volume': 0, 'end_date': next_fomc, 'impact': 'high', 'sentiment': 'neutral', 'is_breaking': False, 'source_weight': self.SOURCES['cme_fedwatch']['weight'], 'likes': 0, 'retweets': 0 })
return predictions
except Exception as e: logger.error(f"Error fetching CME FedWatch: {e}") return []
def _categorize_prediction(self, text: str) -> str: """Categorize prediction market by keywords""" text_lower = text.lower()
macro_score = sum(1 for kw in self.MACRO_KEYWORDS if kw.lower() in text_lower) market_score = sum(1 for kw in self.MARKETS_KEYWORDS if kw.lower() in text_lower) geo_score = sum(1 for kw in self.GEOPOLITICAL_KEYWORDS if kw.lower() in text_lower)
scores = {'macro': macro_score, 'markets': market_score, 'geopolitical': geo_score} return max(scores, key=scores.get) if max(scores.values()) > 0 else 'markets'
def _assess_impact(self, volume: float, category: str) -> str: """Assess market impact based on volume and category""" # Macro predictions are inherently high impact if category == 'macro': return 'high'
# Volume-based assessment if volume > 1000000: # $1M+ volume return 'high' elif volume > 100000: # $100K+ volume return 'medium' else: return 'low'
def _get_mock_predictions(self) -> List[Dict]: """Mock prediction data for development/testing""" return [ { 'id': 1, 'title': 'Will the Fed cut interest rates by March 2025?', 'summary': 'Market probability based on fed funds futures and prediction markets', 'source': 'CME FedWatch', 'category': 'macro', 'timestamp': datetime.now(), 'url': 'https://www.cmegroup.com/markets/interest-rates/cme-fedwatch-tool.html', 'yes_probability': 72.5, 'no_probability': 27.5, 'volume': 0, 'end_date': datetime.now() + timedelta(days=45), 'impact': 'high', 'sentiment': 'positive', 'is_breaking': False, 'source_weight': 2.0, 'likes': 0, 'retweets': 0 }, { 'id': 2, 'title': 'Will Bitcoin reach $100,000 in 2025?', 'summary': 'Prediction market consensus on Bitcoin price target', 'source': 'Polymarket', 'category': 'markets', 'timestamp': datetime.now(), 'url': 'https://polymarket.com', 'yes_probability': 45.0, 'no_probability': 55.0, 'volume': 2500000, 'end_date': datetime.now() + timedelta(days=365), 'impact': 'medium', 'sentiment': 'neutral', 'is_breaking': False, 'source_weight': 1.8, 'likes': 2500, 'retweets': 0 }, { 'id': 3, 'title': 'Will there be a US recession in 2025?', 'summary': 'Expert consensus forecast on economic downturn', 'source': 'Metaculus', 'category': 'macro', 'timestamp': datetime.now(), 'url': 'https://www.metaculus.com', 'yes_probability': 35.0, 'no_probability': 65.0, 'volume': 0, 'end_date': datetime.now() + timedelta(days=365), 'impact': 'high', 'sentiment': 'negative', 'is_breaking': False, 'source_weight': 1.6, 'likes': 450, 'retweets': 0 } ]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426"""Sectoral News Scraper - 7 Major Market SectorsFilters and aggregates news by sector: Finance, Tech, Energy, Healthcare, Consumer, Industrials, Real EstateLeverages existing RSS infrastructure with sector-specific classification"""
from datetime import datetime, timedeltafrom typing import List, Dict, Optionalimport loggingimport refrom concurrent.futures import ThreadPoolExecutor
import requestsimport pandas as pdimport feedparserfrom bs4 import BeautifulSoup
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
class SectoralNewsScraper: """ Aggregates news by market sector Uses RSS feeds + keyword classification """
# 7 Sector configuration with keywords and RSS feeds SECTORS = { 'finance': { 'name': 'Finance', 'keywords': [ 'bank', 'JPMorgan', 'Goldman Sachs', 'Morgan Stanley', 'Wells Fargo', 'Citigroup', 'Bank of America', 'fintech', 'lending', 'credit', 'financial sector', 'banking', 'insurance', 'asset management' ], 'rss_sources': [ 'https://www.cnbc.com/id/10000664/device/rss/rss.html', # CNBC Banking 'https://feeds.bloomberg.com/markets/news.rss' ], 'weight': 1.5 }, 'tech': { 'name': 'Technology', 'keywords': [ 'Apple', 'Microsoft', 'Google', 'Alphabet', 'Amazon', 'Meta', 'Facebook', 'NVIDIA', 'AMD', 'Intel', 'semiconductor', 'chip', 'software', 'cloud', 'AI', 'artificial intelligence', 'tech sector', 'Silicon Valley', 'Tesla' ], 'rss_sources': [ 'https://www.cnbc.com/id/19854910/device/rss/rss.html', # CNBC Technology 'https://techcrunch.com/feed/' ], 'weight': 1.5 }, 'energy': { 'name': 'Energy', 'keywords': [ 'oil', 'gas', 'crude', 'petroleum', 'OPEC', 'Exxon', 'ExxonMobil', 'Chevron', 'ConocoPhillips', 'renewable', 'solar', 'wind', 'energy sector', 'pipeline', 'natural gas', 'LNG', 'fracking', 'drilling' ], 'rss_sources': [ 'https://www.cnbc.com/id/19832390/device/rss/rss.html', # CNBC Energy ], 'weight': 1.6 }, 'healthcare': { 'name': 'Healthcare', 'keywords': [ 'pharma', 'pharmaceutical', 'biotech', 'FDA', 'drug', 'vaccine', 'clinical trial', 'Pfizer', 'Johnson & Johnson', 'Merck', 'AbbVie', 'Bristol Myers', 'healthcare', 'hospital', 'medical device', 'therapeutics' ], 'rss_sources': [ 'https://www.cnbc.com/id/10000108/device/rss/rss.html', # CNBC Health ], 'weight': 1.5 }, 'consumer': { 'name': 'Consumer & Retail', 'keywords': [ 'retail', 'Amazon', 'Walmart', 'Target', 'Costco', 'Home Depot', 'e-commerce', 'consumer', 'shopping', 'Black Friday', 'sales', 'Nike', 'Starbucks', 'McDonald\'s', 'consumer goods', 'discretionary' ], 'rss_sources': [ 'https://www.cnbc.com/id/10001009/device/rss/rss.html', # CNBC Retail ], 'weight': 1.3 }, 'industrials': { 'name': 'Industrials', 'keywords': [ 'Boeing', 'Airbus', 'Caterpillar', 'Deere', '3M', 'GE', 'General Electric', 'Honeywell', 'Lockheed Martin', 'manufacturing', 'industrial', 'aerospace', 'defense', 'machinery', 'equipment', 'logistics', 'freight' ], 'rss_sources': [ 'https://www.reuters.com/rss/businessNews', # Reuters Business ], 'weight': 1.4 }, 'real_estate': { 'name': 'Real Estate', 'keywords': [ 'housing', 'mortgage', 'REIT', 'real estate', 'property', 'home sales', 'construction', 'residential', 'commercial real estate', 'housing market', 'home prices', 'rent', 'rental', 'builder', 'homebuilder' ], 'rss_sources': [], # Will rely on keyword filtering from general news 'weight': 1.3 } }
def __init__(self): """Initialize scraper""" self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.9', })
def scrape_sectoral_news(self, max_items: int = 50, hours: int = 24) -> List[Dict]: """ Scrape and classify news by sector Returns aggregated list sorted by sector and timestamp """ all_news = [] seen_urls = set()
# Parallel fetch from all sector RSS feeds with ThreadPoolExecutor(max_workers=7) as executor: futures = []
for sector_id, sector_info in self.SECTORS.items(): # Submit RSS fetching task for each sector futures.append(( executor.submit(self._fetch_sector_news, sector_id, sector_info, hours), sector_id ))
for future, sector_id in futures: try: sector_news = future.result(timeout=35)
# Deduplicate by URL for item in sector_news: if item['url'] not in seen_urls: seen_urls.add(item['url']) all_news.append(item)
logger.info(f"Fetched {len(sector_news)} items for {sector_id}")
except Exception as e: logger.error(f"Error fetching {sector_id} news: {e}")
# If no news fetched, use mock data if not all_news: logger.warning("No sectoral news fetched - using mock data") return self._get_mock_sectoral_news()
# Sort by sector priority and timestamp all_news.sort( key=lambda x: (x['sector'] != 'tech', x['sector'] != 'finance', -x['timestamp'].timestamp()), )
return all_news[:max_items]
def _fetch_sector_news(self, sector_id: str, sector_info: Dict, hours: int) -> List[Dict]: """Fetch news for a specific sector""" sector_news = []
# Fetch from sector-specific RSS feeds for rss_url in sector_info['rss_sources']: try: feed_news = self._fetch_rss_feed(rss_url, sector_id, sector_info, hours) sector_news.extend(feed_news) except Exception as e: logger.debug(f"Error fetching RSS {rss_url}: {e}")
# If no RSS news, could also filter general news sources by keywords # (This would require access to FinanceNewsScraper - skipping for now)
return sector_news
def _fetch_rss_feed(self, rss_url: str, sector_id: str, sector_info: Dict, hours: int) -> List[Dict]: """Fetch and parse RSS feed for sector""" try: feed = feedparser.parse(rss_url)
if not feed.entries: return []
news_items = [] cutoff_time = datetime.now() - timedelta(hours=hours)
for entry in feed.entries[:15]: # Limit to 15 per feed try: # Parse timestamp if hasattr(entry, 'published_parsed') and entry.published_parsed: timestamp = datetime(*entry.published_parsed[:6]) elif hasattr(entry, 'updated_parsed') and entry.updated_parsed: timestamp = datetime(*entry.updated_parsed[:6]) else: timestamp = datetime.now()
# Skip old news if timestamp < cutoff_time: continue
# Extract title and summary title = entry.get('title', '') summary = entry.get('summary', '') or entry.get('description', '')
# Clean HTML from summary if summary: summary = BeautifulSoup(summary, 'html.parser').get_text() summary = summary[:200] + '...' if len(summary) > 200 else summary
url = entry.get('link', '')
# Verify sector relevance by keywords text = f"{title} {summary}".lower() keyword_matches = sum(1 for kw in sector_info['keywords'] if kw.lower() in text)
# Skip if not relevant enough (unless from sector-specific feed) if keyword_matches == 0 and len(sector_info['rss_sources']) > 3: continue
# Categorize and analyze category = self._categorize_news(text) sentiment = self._analyze_sentiment(text) impact = self._assess_impact(sector_info['weight'], keyword_matches)
news_items.append({ 'id': hash(url), 'title': title, 'summary': summary or title[:200], 'source': sector_info['name'], 'sector': sector_id, # Add sector field 'category': category, 'timestamp': timestamp, 'sentiment': sentiment, 'impact': impact, 'url': url, 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': sector_info['weight'], 'from_web': False })
except Exception as e: logger.debug(f"Error parsing RSS entry: {e}") continue
return news_items
except Exception as e: logger.error(f"Error fetching RSS feed {rss_url}: {e}") return []
def _categorize_news(self, text: str) -> str: """Categorize news (macro, markets, geopolitical)""" macro_keywords = ['Fed', 'ECB', 'inflation', 'rate', 'GDP', 'economy', 'recession'] markets_keywords = ['stock', 'earnings', 'revenue', 'profit', 'IPO', 'merger', 'acquisition'] geo_keywords = ['China', 'tariff', 'trade war', 'sanctions', 'regulation']
macro_score = sum(1 for kw in macro_keywords if kw.lower() in text) markets_score = sum(1 for kw in markets_keywords if kw.lower() in text) geo_score = sum(1 for kw in geo_keywords if kw.lower() in text)
scores = {'macro': macro_score, 'markets': markets_score, 'geopolitical': geo_score} return max(scores, key=scores.get) if max(scores.values()) > 0 else 'markets'
def _analyze_sentiment(self, text: str) -> str: """Analyze sentiment based on keywords""" positive = ['surge', 'soar', 'rally', 'beat', 'upgrade', 'gain', 'rise', 'bullish', 'positive'] negative = ['plunge', 'crash', 'fall', 'miss', 'downgrade', 'loss', 'drop', 'bearish', 'negative']
pos_count = sum(1 for word in positive if word in text) neg_count = sum(1 for word in negative if word in text)
if pos_count > neg_count: return 'positive' elif neg_count > pos_count: return 'negative' return 'neutral'
def _assess_impact(self, sector_weight: float, keyword_matches: int) -> str: """Assess impact based on sector weight and keyword relevance""" if sector_weight >= 1.5 and keyword_matches >= 3: return 'high' elif keyword_matches >= 2: return 'medium' else: return 'low'
def _get_mock_sectoral_news(self) -> List[Dict]: """Mock sectoral news for development""" now = datetime.now()
return [ { 'id': 1, 'title': 'Apple announces new iPhone with advanced AI capabilities', 'summary': 'Apple unveils next-generation iPhone featuring on-device AI processing', 'source': 'Technology', 'sector': 'tech', 'category': 'markets', 'timestamp': now - timedelta(minutes=30), 'sentiment': 'positive', 'impact': 'high', 'url': 'https://techcrunch.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.5, 'from_web': False }, { 'id': 2, 'title': 'JPMorgan reports strong Q4 earnings beat analyst expectations', 'summary': 'Major investment bank posts record profits amid trading surge', 'source': 'Finance', 'sector': 'finance', 'category': 'markets', 'timestamp': now - timedelta(hours=1), 'sentiment': 'positive', 'impact': 'high', 'url': 'https://cnbc.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.5, 'from_web': False }, { 'id': 3, 'title': 'OPEC+ extends oil production cuts through Q2', 'summary': 'Major oil producers agree to maintain supply restrictions', 'source': 'Energy', 'sector': 'energy', 'category': 'geopolitical', 'timestamp': now - timedelta(hours=2), 'sentiment': 'neutral', 'impact': 'high', 'url': 'https://reuters.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.6, 'from_web': False }, { 'id': 4, 'title': 'Pfizer receives FDA approval for new cancer treatment', 'summary': 'Breakthrough therapy approved for late-stage lung cancer', 'source': 'Healthcare', 'sector': 'healthcare', 'category': 'markets', 'timestamp': now - timedelta(hours=3), 'sentiment': 'positive', 'impact': 'medium', 'url': 'https://cnbc.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.5, 'from_web': False }, { 'id': 5, 'title': 'Amazon expands same-day delivery to 50 new cities', 'summary': 'E-commerce giant accelerates logistics network expansion', 'source': 'Consumer & Retail', 'sector': 'consumer', 'category': 'markets', 'timestamp': now - timedelta(hours=4), 'sentiment': 'positive', 'impact': 'medium', 'url': 'https://techcrunch.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.3, 'from_web': False }, { 'id': 6, 'title': 'Boeing wins $10B contract for new military aircraft', 'summary': 'Defense contractor secures major government order', 'source': 'Industrials', 'sector': 'industrials', 'category': 'markets', 'timestamp': now - timedelta(hours=5), 'sentiment': 'positive', 'impact': 'medium', 'url': 'https://reuters.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.4, 'from_web': False }, { 'id': 7, 'title': 'US housing starts surge 15% in December', 'summary': 'Construction activity rebounds amid lower mortgage rates', 'source': 'Real Estate', 'sector': 'real_estate', 'category': 'macro', 'timestamp': now - timedelta(hours=6), 'sentiment': 'positive', 'impact': 'medium', 'url': 'https://cnbc.com', 'likes': 0, 'retweets': 0, 'is_breaking': False, 'source_weight': 1.3, 'from_web': False } ]33 unmodified lines343536373839272 unmodified lines31231331431531631731827 unmodified lines34634734834935035133 unmodified lines'reddit': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'rss': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'ai_tech': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'dedup_index': {}, # Global deduplication index'filtered_cache': {} # Cached filtered results}272 unmodified linesself._clear_source_from_dedup(source)logger.info(f"🗑️ Cleared cache for {source}")else:for src in ['twitter', 'reddit', 'rss', 'ai_tech']:self.cache[src] = {'raw_news': [], 'last_fetch': None, 'ttl': 180}self.cache['dedup_index'] = {}self.cache['filtered_cache'] = {}27 unmodified lines'age_seconds': self._get_cache_age('ai_tech'),'is_valid': self._is_cache_valid('ai_tech')},'dedup_index_size': len(self.cache['dedup_index']),'filtered_cache_size': len(self.cache['filtered_cache'])}33 unmodified lines34353637383940414243272 unmodified lines31631731831932032132227 unmodified lines35035135235335435535635735835936036136236336436536636736836937037137237337437533 unmodified lines'reddit': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'rss': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'ai_tech': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'predictions': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'sectoral_news': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'market_events': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'economic_calendar': {'raw_news': [], 'last_fetch': None, 'ttl': default_ttl},'dedup_index': {}, # Global deduplication index'filtered_cache': {} # Cached filtered results}272 unmodified linesself._clear_source_from_dedup(source)logger.info(f"🗑️ Cleared cache for {source}")else:for src in ['twitter', 'reddit', 'rss', 'ai_tech', 'predictions', 'sectoral_news', 'market_events', 'economic_calendar']:self.cache[src] = {'raw_news': [], 'last_fetch': None, 'ttl': 180}self.cache['dedup_index'] = {}self.cache['filtered_cache'] = {}27 unmodified lines'age_seconds': self._get_cache_age('ai_tech'),'is_valid': self._is_cache_valid('ai_tech')},'predictions': {'items': len(self.cache['predictions']['raw_news']),'age_seconds': self._get_cache_age('predictions'),'is_valid': self._is_cache_valid('predictions')},'sectoral_news': {'items': len(self.cache['sectoral_news']['raw_news']),'age_seconds': self._get_cache_age('sectoral_news'),'is_valid': self._is_cache_valid('sectoral_news')},'market_events': {'items': len(self.cache['market_events']['raw_news']),'age_seconds': self._get_cache_age('market_events'),'is_valid': self._is_cache_valid('market_events')},'economic_calendar': {'items': len(self.cache['economic_calendar']['raw_news']),'age_seconds': self._get_cache_age('economic_calendar'),'is_valid': self._is_cache_valid('economic_calendar')},'dedup_index_size': len(self.cache['dedup_index']),'filtered_cache_size': len(self.cache['filtered_cache'])}
app/components/news.py+315
app/pages/05_Dashboard.py+257-8
app/services/economic_calendar.pyAdded+377
app/services/market_events.pyAdded+391
app/services/prediction_markets.pyAdded+411
app/services/sectoral_news.pyAdded+426
app/utils/news_cache.py+25-1