From b0e88b1be6931f5731784497fdbae4f168d36720 Mon Sep 17 00:00:00 2001 From: Tom Dunietz Date: Mon, 22 Apr 2024 15:33:42 -0400 Subject: [PATCH 1/2] qualanalysis --- qualAnalysis.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 qualAnalysis.py diff --git a/qualAnalysis.py b/qualAnalysis.py new file mode 100644 index 0000000..6bce780 --- /dev/null +++ b/qualAnalysis.py @@ -0,0 +1,90 @@ +import nltk +from nltk.sentiment.vader import SentimentIntensityAnalyzer +import requests +import logging +import json +from functools import lru_cache + +# Initialize logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +@lru_cache(maxsize=128) +def get_sentiment_score(article): + """ + Function to get sentiment score of a news article using NLTK Vader sentiment analysis. + """ + try: + sid = SentimentIntensityAnalyzer() + sentiment_score = sid.polarity_scores(article)['compound'] + return sentiment_score + except Exception as e: + logger.error("Error getting sentiment score: %s", str(e)) + return 0 + +@lru_cache(maxsize=128) +def get_news_articles(symbol, news_sources, news_api_key): + """ + Function to get news articles related to a given symbol from News API. + """ + try: + url = f'https://newsapi.org/v2/everything?q={symbol}&sources={news_sources}&apiKey={news_api_key}' + response = requests.get(url) + data = response.json() + if 'articles' in data: + articles = [article['description'] for article in data['articles']] + return articles + else: + return [] + except Exception as e: + logger.error("Error getting news articles: %s", str(e)) + return [] + +@lru_cache(maxsize=128) +def get_analyst_recommendations(symbol, api_key): + """ + Function to get analyst recommendations for a given symbol from Alpha Vantage. + """ + try: + url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey={api_key}' + response = requests.get(url) + data = response.json() + if 'AnalystRating' in data: + return int(data['AnalystRating']) # Convert to int + else: + return 0 + except Exception as e: + logger.error("Error getting analyst recommendations: %s", str(e)) + return 0 + +def get_financial_sentiment(symbol, news_sources, news_api_key, api_key): + """ + Function to compute the overall strength value based on sentiment analysis of news articles and analyst recommendations. + """ + try: + articles = get_news_articles(symbol, news_sources, news_api_key) + if not articles: + logger.warning("No news articles found for symbol %s", symbol) + return 0 + + total_sentiment_score = sum(get_sentiment_score(article) for article in articles) / len(articles) + analyst_recommendations = get_analyst_recommendations(symbol, api_key) + overall_strength = total_sentiment_score + analyst_recommendations + return overall_strength + except Exception as e: + logger.error("Error computing overall strength: %s", str(e)) + return 0 + +if __name__ == "__main__": + try: + # Configuration + company = 'AAPL' # Specify the company symbol + news_sources = 'bbc-news, bloomberg, cnn, reuters' # Specify news sources + news_api_key = 'YOUR_NEWS_API_KEY' + api_key = 'YOUR_ALPHA_VANTAGE_API_KEY' + + # Get overall strength + strength = get_financial_sentiment(company, news_sources, news_api_key, api_key) + print("Overall Strength:", strength) + except Exception as e: + logger.error("Error in main: %s", str(e)) \ No newline at end of file From 9eda9a1d36c1f51f9101a1a12707df6c8f00c3e1 Mon Sep 17 00:00:00 2001 From: dunietzt <71984721+dunietzt@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:58:36 -0400 Subject: [PATCH 2/2] Update qualAnalysis.py --- qualAnalysis.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/qualAnalysis.py b/qualAnalysis.py index 6bce780..04459dc 100644 --- a/qualAnalysis.py +++ b/qualAnalysis.py @@ -23,12 +23,12 @@ def get_sentiment_score(article): return 0 @lru_cache(maxsize=128) -def get_news_articles(symbol, news_sources, news_api_key): +def get_news_articles(symbol, news_sources): """ Function to get news articles related to a given symbol from News API. """ try: - url = f'https://newsapi.org/v2/everything?q={symbol}&sources={news_sources}&apiKey={news_api_key}' + url = f'https://newsapi.org/v2/everything?q={symbol}&sources={news_sources}' response = requests.get(url) data = response.json() if 'articles' in data: @@ -41,12 +41,12 @@ def get_news_articles(symbol, news_sources, news_api_key): return [] @lru_cache(maxsize=128) -def get_analyst_recommendations(symbol, api_key): +def get_analyst_recommendations(symbol): """ Function to get analyst recommendations for a given symbol from Alpha Vantage. """ try: - url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey={api_key}' + url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}' response = requests.get(url) data = response.json() if 'AnalystRating' in data: @@ -57,18 +57,18 @@ def get_analyst_recommendations(symbol, api_key): logger.error("Error getting analyst recommendations: %s", str(e)) return 0 -def get_financial_sentiment(symbol, news_sources, news_api_key, api_key): +def get_financial_sentiment(symbol, news_sources): """ Function to compute the overall strength value based on sentiment analysis of news articles and analyst recommendations. """ try: - articles = get_news_articles(symbol, news_sources, news_api_key) + articles = get_news_articles(symbol, news_sources) if not articles: logger.warning("No news articles found for symbol %s", symbol) return 0 total_sentiment_score = sum(get_sentiment_score(article) for article in articles) / len(articles) - analyst_recommendations = get_analyst_recommendations(symbol, api_key) + analyst_recommendations = get_analyst_recommendations(symbol) overall_strength = total_sentiment_score + analyst_recommendations return overall_strength except Exception as e: @@ -80,11 +80,9 @@ def get_financial_sentiment(symbol, news_sources, news_api_key, api_key): # Configuration company = 'AAPL' # Specify the company symbol news_sources = 'bbc-news, bloomberg, cnn, reuters' # Specify news sources - news_api_key = 'YOUR_NEWS_API_KEY' - api_key = 'YOUR_ALPHA_VANTAGE_API_KEY' # Get overall strength - strength = get_financial_sentiment(company, news_sources, news_api_key, api_key) + strength = get_financial_sentiment(company, news_sources) print("Overall Strength:", strength) except Exception as e: - logger.error("Error in main: %s", str(e)) \ No newline at end of file + logger.error("Error in main: %s", str(e))