Benjamin Consolvo commited on
Commit
446912b
·
1 Parent(s): 91fc69d

sentiment button

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -147,7 +147,7 @@ class NewsSentiment:
147
  sort_by='publishedAt', # <-- fixed argument name
148
  page=1)
149
  compound_score = 0
150
- for article in articles['articles'][:5]: # Check first 5 articles
151
  # print(f'article= {article}')
152
  score = self.sia.polarity_scores(article['title'])['compound']
153
  compound_score += score
@@ -402,26 +402,38 @@ class TradingApp:
402
  st.header("Manual Trade")
403
  symbol = st.text_input('Enter stock symbol')
404
 
405
- # --- Sentiment Check Feature (refactored) ---
406
- sentiment_result = None
407
- article_headlines = []
 
 
 
408
  if st.button("Check Sentiment"):
409
  if symbol:
410
  sentiment_result, article_headlines = self.get_combined_sentiment_and_headlines(symbol)
 
 
 
411
  else:
412
- sentiment_result = None
413
- article_headlines = []
 
 
 
 
 
 
414
 
415
- if sentiment_result is not None:
416
  st.markdown(f"**Sentiment for {symbol.upper()}:** {sentiment_result if sentiment_result in ['Positive', 'Negative', 'Neutral'] else 'No sentiment available'}")
417
- elif sentiment_result is None and st.session_state.get("Check Sentiment"):
418
  st.markdown("**Sentiment:** No sentiment available")
419
 
420
- if article_headlines:
421
  st.markdown("**Recent Headlines:**")
422
  for headline in article_headlines:
423
  st.write(f"- {headline}")
424
- elif sentiment_result is not None and not article_headlines:
425
  st.markdown("_No headlines available._")
426
 
427
  # Fetch the current stock price dynamically using Alpaca's API
 
147
  sort_by='publishedAt', # <-- fixed argument name
148
  page=1)
149
  compound_score = 0
150
+ for article in articles['articles'][:5] # Check first 5 articles
151
  # print(f'article= {article}')
152
  score = self.sia.polarity_scores(article['title'])['compound']
153
  compound_score += score
 
402
  st.header("Manual Trade")
403
  symbol = st.text_input('Enter stock symbol')
404
 
405
+ # --- Sentiment Check Feature (fixed with session state) ---
406
+ if "sentiment_result" not in st.session_state:
407
+ st.session_state["sentiment_result"] = None
408
+ if "article_headlines" not in st.session_state:
409
+ st.session_state["article_headlines"] = []
410
+
411
  if st.button("Check Sentiment"):
412
  if symbol:
413
  sentiment_result, article_headlines = self.get_combined_sentiment_and_headlines(symbol)
414
+ st.session_state["sentiment_result"] = sentiment_result
415
+ st.session_state["article_headlines"] = article_headlines
416
+ st.session_state["sentiment_symbol"] = symbol
417
  else:
418
+ st.session_state["sentiment_result"] = None
419
+ st.session_state["article_headlines"] = []
420
+ st.session_state["sentiment_symbol"] = ""
421
+
422
+ # Always display the last checked sentiment/headlines for the current symbol
423
+ sentiment_result = st.session_state.get("sentiment_result")
424
+ article_headlines = st.session_state.get("article_headlines", [])
425
+ sentiment_symbol = st.session_state.get("sentiment_symbol", "")
426
 
427
+ if symbol and sentiment_symbol == symbol and sentiment_result is not None:
428
  st.markdown(f"**Sentiment for {symbol.upper()}:** {sentiment_result if sentiment_result in ['Positive', 'Negative', 'Neutral'] else 'No sentiment available'}")
429
+ elif symbol and sentiment_symbol == symbol and sentiment_result is None:
430
  st.markdown("**Sentiment:** No sentiment available")
431
 
432
+ if symbol and sentiment_symbol == symbol and article_headlines:
433
  st.markdown("**Recent Headlines:**")
434
  for headline in article_headlines:
435
  st.write(f"- {headline}")
436
+ elif symbol and sentiment_symbol == symbol and sentiment_result is not None and not article_headlines:
437
  st.markdown("_No headlines available._")
438
 
439
  # Fetch the current stock price dynamically using Alpaca's API