get_symbol_sentiment
Analyze recent news sentiment for stock symbols to assess market perception and inform trading decisions using MonteWalk's quantitative finance tools.
Instructions
Fetches recent news for a symbol and calculates aggregate sentiment.
Args:
symbol: Ticker symbol.
Returns:
Aggregate sentiment analysis of recent news.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes |
Implementation Reference
- tools/news_intelligence.py:191-238 (handler)Core handler function that retrieves the 10 most recent news articles for the given stock symbol using yfinance, computes sentiment polarity for each article title via the analyze_sentiment helper (FinBERT model), averages the polarities, classifies overall sentiment as BULLISH, BEARISH, or NEUTRAL based on thresholds, and returns a formatted summary string.def get_symbol_sentiment(symbol: str) -> str: """ Fetches recent news for a symbol and calculates aggregate sentiment. Args: symbol: Ticker symbol. Returns: Aggregate sentiment analysis of recent news. """ try: ticker = yf.Ticker(symbol) news = ticker.news[:10] # Last 10 articles if not news: return f"No news found for {symbol}" sentiments = [] model_used = "Unknown" for item in news: title = item.get("title", "") if title: result = analyze_sentiment(title) if "polarity" in result: sentiments.append(result["polarity"]) model_used = result.get("model", "Unknown") if not sentiments: return f"No valid news titles for {symbol}" avg_polarity = sum(sentiments) / len(sentiments) if avg_polarity > 0.1: classification = "BULLISH" elif avg_polarity < -0.1: classification = "BEARISH" else: classification = "NEUTRAL" return (f"Sentiment Analysis for {symbol} ({len(sentiments)} articles):\n" f"Average Polarity: {avg_polarity:.3f}\n" f"Market Sentiment: {classification}\n" f"Model: {model_used}") except Exception as e: return f"Error analyzing sentiment for {symbol}: {str(e)}"
- server.py:406-409 (registration)Registers get_symbol_sentiment (along with related news tools) with the MCP server using the register_tools helper function, which applies the @mcp.tool() decorator to expose it as an MCP tool.[get_news, analyze_sentiment, get_symbol_sentiment], "News & Sentiment" )
- tools/news_intelligence.py:146-189 (helper)Helper function called by get_symbol_sentiment to analyze sentiment of individual news titles. Posts text to a Modal-hosted FinBERT endpoint, maps positive/negative/neutral labels and scores to a polarity value (-1 to 1), and returns a dict with polarity, classification, and model info.def analyze_sentiment(text: str) -> Dict[str, Any]: """ Analyzes the sentiment of a given text using FinBERT on Modal (via Public Endpoint). Args: text: Text to analyze (e.g., news headline, article). Returns: Dictionary with polarity, confidence, and classification. """ # Try Modal first try: # Check if URL is configured if "replace-me" in MODAL_ENDPOINT_URL: raise ValueError("Modal URL not configured") response = requests.post(MODAL_ENDPOINT_URL, json={"text": text}, timeout=15) response.raise_for_status() result = response.json() # FinBERT returns {'label': 'positive'/'negative'/'neutral', 'score': float} label = result['label'].upper() score = result['score'] # Map to polarity-like score for compatibility (-1 to 1) if label == "POSITIVE": polarity = score elif label == "NEGATIVE": polarity = -score else: polarity = 0.0 return { "text": text[:100] + "..." if len(text) > 100 else text, "polarity": round(polarity, 3), "subjectivity": 0.0, # FinBERT doesn't give subjectivity "classification": label, "model": "FinBERT (Modal Public)" } except Exception as e: logger.error(f"Modal FinBERT failed: {e}") return {"error": f"Error analyzing sentiment: {str(e)}"}