Akshayram1 commited on
Commit
37276fb
·
verified ·
1 Parent(s): e6a496c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -8
app.py CHANGED
@@ -12,12 +12,15 @@ import requests
12
 
13
  st.set_page_config(page_title="Stock News Sentiment Analyzer", layout="wide")
14
 
15
- def verify_link(url):
16
- try:
17
- response = requests.head(url, timeout=5)
18
- return response.status_code == 200
19
- except requests.RequestException:
20
- return False
 
 
 
21
 
22
  def get_news(ticker):
23
  finviz_url = 'https://finviz.com/quote.ashx?t='
@@ -83,15 +86,33 @@ def plot_daily_sentiment(parsed_and_scored_news, ticker):
83
  fig = px.bar(mean_scores, x=mean_scores.index, y='sentiment_score', title=f'{ticker} Daily Sentiment Scores')
84
  return fig
85
 
 
 
 
 
 
 
 
 
 
 
86
  st.header("Stock News Sentiment Analyzer")
87
 
88
  ticker = st.text_input('Enter Stock Ticker', '').upper()
89
 
90
  try:
91
- st.subheader(f"Hourly and Daily Sentiment of {ticker} Stock")
92
  news_table = get_news(ticker)
93
  parsed_news_df = parse_news(news_table)
94
  parsed_and_scored_news = score_news(parsed_news_df)
 
 
 
 
 
 
 
 
95
  fig_hourly = plot_hourly_sentiment(parsed_and_scored_news, ticker)
96
  fig_daily = plot_daily_sentiment(parsed_and_scored_news, ticker)
97
 
@@ -99,7 +120,7 @@ try:
99
  st.plotly_chart(fig_daily)
100
 
101
  description = f"""
102
- The above chart averages the sentiment scores of {ticker} stock hourly and daily.
103
  The table below gives each of the most recent headlines of the stock and the negative, neutral, positive and an aggregated sentiment score.
104
  The news headlines are obtained from the FinViz website.
105
  Sentiments are given by the nltk.sentiment.vader Python library.
 
12
 
13
  st.set_page_config(page_title="Stock News Sentiment Analyzer", layout="wide")
14
 
15
+ def verify_link(url, timeout=10, retries=3):
16
+ for _ in range(retries):
17
+ try:
18
+ response = requests.head(url, timeout=timeout, allow_redirects=True)
19
+ if 200 <= response.status_code < 300:
20
+ return True
21
+ except requests.RequestException:
22
+ continue
23
+ return False
24
 
25
  def get_news(ticker):
26
  finviz_url = 'https://finviz.com/quote.ashx?t='
 
86
  fig = px.bar(mean_scores, x=mean_scores.index, y='sentiment_score', title=f'{ticker} Daily Sentiment Scores')
87
  return fig
88
 
89
+ def get_recommendation(sentiment_scores):
90
+ avg_sentiment = sentiment_scores['sentiment_score'].mean()
91
+
92
+ if avg_sentiment >= 0.05:
93
+ return f"Positive sentiment (Score: {avg_sentiment:.2f}). The recent news suggests a favorable outlook for this stock. Consider buying or holding if you already own it."
94
+ elif avg_sentiment <= -0.05:
95
+ return f"Negative sentiment (Score: {avg_sentiment:.2f}). The recent news suggests caution. Consider selling or avoiding this stock for now."
96
+ else:
97
+ return f"Neutral sentiment (Score: {avg_sentiment:.2f}). The recent news doesn't show a strong bias. Consider holding if you own the stock, or watch for more definitive trends before making a decision."
98
+
99
  st.header("Stock News Sentiment Analyzer")
100
 
101
  ticker = st.text_input('Enter Stock Ticker', '').upper()
102
 
103
  try:
104
+ st.subheader(f"Sentiment Analysis and Recommendation for {ticker} Stock")
105
  news_table = get_news(ticker)
106
  parsed_news_df = parse_news(news_table)
107
  parsed_and_scored_news = score_news(parsed_news_df)
108
+
109
+ # Generate and display recommendation
110
+ recommendation = get_recommendation(parsed_and_scored_news)
111
+ st.write(recommendation)
112
+
113
+ # Display a disclaimer
114
+ st.warning("Disclaimer: This recommendation is based solely on recent news sentiment and should not be considered as financial advice. Always do your own research and consult with a qualified financial advisor before making investment decisions.")
115
+
116
  fig_hourly = plot_hourly_sentiment(parsed_and_scored_news, ticker)
117
  fig_daily = plot_daily_sentiment(parsed_and_scored_news, ticker)
118
 
 
120
  st.plotly_chart(fig_daily)
121
 
122
  description = f"""
123
+ The above charts average the sentiment scores of {ticker} stock hourly and daily.
124
  The table below gives each of the most recent headlines of the stock and the negative, neutral, positive and an aggregated sentiment score.
125
  The news headlines are obtained from the FinViz website.
126
  Sentiments are given by the nltk.sentiment.vader Python library.