-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrss_sentimentV2.py
226 lines (182 loc) · 8.1 KB
/
rss_sentimentV2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import streamlit as st
import yfinance as yf
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import feedparser
from nltk.sentiment import SentimentIntensityAnalyzer
import nltk
# Download VADER lexicon if not already done
nltk.download('vader_lexicon', quiet=True)
@st.cache_data
def load_data(ticker):
data = yf.download(ticker)
return data
def add_ema(data, periods):
for period in periods:
data[f'EMA_{period}'] = data['Close'].ewm(span=period, adjust=False).mean()
return data
def add_rsi(data, window=14):
delta = data['Close'].diff(1)
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(window=window).mean()
avg_loss = loss.rolling(window=window).mean()
rs = avg_gain / avg_loss
data['RSI'] = 100 - (100 / (1 + rs))
return data
def add_macd(data):
short_ema = data['Close'].ewm(span=12, adjust=False).mean()
long_ema = data['Close'].ewm(span=26, adjust=False).mean()
data['MACD'] = short_ema - long_ema
data['Signal Line'] = data['MACD'].ewm(span=9, adjust=False).mean()
return data
@st.cache_data
def get_fundamental_metrics(ticker):
stock = yf.Ticker(ticker)
info = stock.info
metrics = {
'P/E Ratio': info.get('trailingPE', 'N/A'),
'ROE': info.get('returnOnEquity', 'N/A'),
'ROA': info.get('returnOnAssets', 'N/A'),
'Gross Margin': info.get('grossMargins', 'N/A'),
'Profit Margin': info.get('profitMargins', 'N/A'),
'Debt to Equity': info.get('debtToEquity', 'N/A'),
'Current Ratio': info.get('currentRatio', 'N/A'),
'Price to Book': info.get('priceToBook', 'N/A'),
'Earnings Per Share': info.get('trailingEps', 'N/A'),
'Dividend Yield': info.get('dividendYield', 'N/A')
}
for key, value in metrics.items():
if isinstance(value, (int, float)):
metrics[key] = round(value, 2)
elif value == 'N/A':
metrics[key] = 'N/A'
else:
try:
metrics[key] = round(float(value), 2)
except ValueError:
metrics[key] = 'N/A'
return metrics
@st.cache_data
def fetch_rss_feed(ticker):
feed_url = f"https://finance.yahoo.com/rss/headline?s={ticker}"
feed = feedparser.parse(feed_url)
return feed
def get_vader_sentiment(text):
sia = SentimentIntensityAnalyzer()
return sia.polarity_scores(text)
st.title('Interactive Stock Chart with Technical Indicators, Fundamental Metrics, and News Sentiment')
# Sidebar for user inputs and news feed
st.sidebar.title('Stock Ticker and News')
ticker = st.sidebar.text_input('Enter Stock Ticker', 'GOOGL').upper()
# Main content
data = load_data(ticker)
periods = st.slider('Select Time Period (in days)', 30, 365, 180)
selected_emas = st.multiselect('Select EMA periods', [200, 50, 20], default=[200, 50, 20])
add_rsi_plot = st.checkbox('Add RSI Subplot')
add_macd_plot = st.checkbox('Add MACD Subplot')
st.subheader('Select Fundamental Metrics to Display')
metrics = get_fundamental_metrics(ticker)
selected_metrics = st.multiselect('Choose metrics', list(metrics.keys()), default=['P/E Ratio', 'ROE', 'Profit Margin'])
if selected_metrics:
st.subheader('Fundamental Metrics')
for i in range(0, len(selected_metrics), 3):
cols = st.columns(3)
for j in range(3):
if i + j < len(selected_metrics):
metric = selected_metrics[i + j]
cols[j].metric(label=metric, value=metrics[metric])
data = add_ema(data, selected_emas)
data_period = data[-periods:]
if add_rsi_plot:
data_period = add_rsi(data_period)
if add_macd_plot:
data_period = add_macd(data_period)
rows = 1 + add_rsi_plot + add_macd_plot
price_range = [data_period['Close'].min() * 0.95, data_period['Close'].max() * 1.05]
rsi_range = [0, 100]
macd_range = [0, 0]
if add_macd_plot:
macd_range = [
min(data_period['MACD'].min(), data_period['Signal Line'].min()) * 1.05,
max(data_period['MACD'].max(), data_period['Signal Line'].max()) * 1.05
]
fig = make_subplots(rows=rows, cols=1, shared_xaxes=True,
vertical_spacing=0.15,
row_heights=[0.5] + [0.25] * (rows - 1),
subplot_titles=('Price', 'RSI', 'MACD')[:rows])
fig.add_trace(go.Candlestick(x=data_period.index,
open=data_period['Open'],
high=data_period['High'],
low=data_period['Low'],
close=data_period['Close'],
name='Candlesticks'), row=1, col=1)
for period in selected_emas:
fig.add_trace(go.Scatter(x=data_period.index, y=data_period[f'EMA_{period}'], mode='lines', name=f'EMA_{period}'), row=1, col=1)
current_row = 2
if add_rsi_plot:
fig.add_trace(go.Scatter(x=data_period.index, y=data_period['RSI'], mode='lines', name='RSI'), row=current_row, col=1)
fig.update_yaxes(range=rsi_range, row=current_row, col=1, title='RSI')
current_row += 1
if add_macd_plot:
fig.add_trace(go.Scatter(x=data_period.index, y=data_period['MACD'], mode='lines', name='MACD'), row=current_row, col=1)
fig.add_trace(go.Scatter(x=data_period.index, y=data_period['Signal Line'], mode='lines', name='Signal Line'), row=current_row, col=1)
fig.update_yaxes(range=macd_range, row=current_row, col=1, title='MACD')
fig.update_layout(
title=f'{ticker} Stock Price and Indicators',
xaxis_title='Date',
yaxis_title='Price',
height=400 + 200 * (rows - 1),
margin=dict(l=50, r=50, t=50, b=50),
legend=dict(x=0, y=1, traceorder='normal'),
xaxis_rangeslider_visible=False,
hovermode='x unified'
)
st.plotly_chart(fig)
# RSS feed in the sidebar with sentiment analysis
st.sidebar.subheader(f"Recent News for {ticker} with Sentiment Analysis")
st.sidebar.text("Loading news...") # Simple loading message
feed = fetch_rss_feed(ticker)
if feed.entries:
news_items = []
for entry in feed.entries:
sentiment = get_vader_sentiment(entry.title)
compound_score = sentiment['compound']
# Determine sentiment category
if compound_score >= 0.05:
sentiment_category = "Positive"
color = "green"
elif compound_score <= -0.05:
sentiment_category = "Negative"
color = "red"
else:
sentiment_category = "Neutral"
color = "gray"
news_items.append({
'title': entry.title,
'link': entry.link,
'published': entry.published,
'sentiment_category': sentiment_category,
'compound_score': compound_score,
'color': color
})
# Sort news items by published date (latest first) and take the latest 10
latest_10_news = sorted(news_items, key=lambda x: x['published'], reverse=True)[:10]
# Sort the latest 10 news by sentiment score (highest to lowest)
latest_10_news.sort(key=lambda x: x['compound_score'], reverse=True)
# Calculate total sentiment score for the latest 10 news items
total_sentiment_score = sum(item['compound_score'] for item in latest_10_news)
# Display total sentiment score in red
st.sidebar.markdown(f"<h3 style='color: red;'>Total Sentiment Score: {total_sentiment_score:.2f}</h3>", unsafe_allow_html=True)
# Display sorted news items
for item in latest_10_news:
st.sidebar.markdown(f"**{item['title']}**")
st.sidebar.markdown(f"[Read more]({item['link']})")
st.sidebar.markdown(f"*Published: {item['published']}*")
st.sidebar.markdown(f"Sentiment: <span style='color:{item['color']}'>{item['sentiment_category']}</span> (Score: {item['compound_score']:.2f})", unsafe_allow_html=True)
st.sidebar.markdown("---")
else:
st.sidebar.write("No news found for the given ticker symbol.")
# Remove the loading message after fetching is complete
st.sidebar.empty()