get_symbol_sentiment
Analyze recent news sentiment for financial symbols to assess market perception and inform trading decisions.
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)The core handler function for the 'get_symbol_sentiment' tool. It fetches the latest 10 news articles for the given symbol using yfinance, analyzes the sentiment of each title using the 'analyze_sentiment' helper, computes the average polarity score, classifies as BULLISH/BEARISH/NEUTRAL, and returns a formatted string summary.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:405-408 (registration)Registration of the 'get_symbol_sentiment' tool (along with related news tools) using the 'register_tools' helper function, which applies the @mcp.tool() decorator to make it available in the MCP server.register_tools( [get_news, analyze_sentiment, get_symbol_sentiment], "News & Sentiment" )
- server.py:19-19 (helper)Import statement that brings the 'get_symbol_sentiment' function into the server.py scope for registration.from tools.news_intelligence import get_news, analyze_sentiment, get_symbol_sentiment