Karthikeyen92
commited on
Commit
•
d4a9e0f
1
Parent(s):
88e348e
Update app.py
Browse files
app.py
CHANGED
@@ -1,650 +1,650 @@
|
|
1 |
-
import sys
|
2 |
-
import json
|
3 |
-
import os
|
4 |
-
import requests
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
import streamlit as st
|
7 |
-
import plotly.graph_objects as go
|
8 |
-
import plotly.express as px
|
9 |
-
from openai import AzureOpenAI
|
10 |
-
import pandas as pd
|
11 |
-
import numpy as np
|
12 |
-
from datetime import datetime, timedelta
|
13 |
-
from dotted_dict import DottedDict
|
14 |
-
from langchain_community.vectorstores import Chroma
|
15 |
-
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
|
16 |
-
from py.data_fetch import DataFetch
|
17 |
-
from py.handle_files import *
|
18 |
-
from py.db_storage import DBStorage
|
19 |
-
from langchain.callbacks import get_openai_callback
|
20 |
-
from PyPDF2 import PdfReader
|
21 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
22 |
-
from langchain_community.vectorstores import FAISS
|
23 |
-
from langchain.chains.question_answering import load_qa_chain
|
24 |
-
from langchain.prompts import PromptTemplate
|
25 |
-
import yfinance as yf
|
26 |
-
|
27 |
-
class StockAdviserConfig:
|
28 |
-
def __init__(self):
|
29 |
-
load_dotenv()
|
30 |
-
self.azure_config = {
|
31 |
-
"base_url": os.getenv("AZURE_OPENAI_ENDPOINT"),
|
32 |
-
"embedding_base_url": os.getenv("AZURE_OPENAI_EMBEDDING_ENDPOINT"),
|
33 |
-
"model_deployment": os.getenv("AZURE_OPENAI_MODEL_DEPLOYMENT_NAME"),
|
34 |
-
"model_name": os.getenv("AZURE_OPENAI_MODEL_NAME"),
|
35 |
-
"embedding_deployment": os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME"),
|
36 |
-
"embedding_name": os.getenv("AZURE_OPENAI_EMBEDDING_NAME"),
|
37 |
-
"api-key": os.getenv("AZURE_OPENAI_API_KEY"),
|
38 |
-
"api_version": os.getenv("AZURE_OPENAI_API_VERSION")
|
39 |
-
}
|
40 |
-
self.models = DottedDict()
|
41 |
-
|
42 |
-
class StockAdviserUI:
|
43 |
-
def __init__(self):
|
44 |
-
st.set_page_config(page_title="GEN AI Stock Adviser by Karthikeyen", layout="wide",
|
45 |
-
initial_sidebar_state="expanded")
|
46 |
-
self._setup_css()
|
47 |
-
self._setup_header()
|
48 |
-
|
49 |
-
def _setup_css(self):
|
50 |
-
st.markdown("""
|
51 |
-
<style>
|
52 |
-
.main-header {
|
53 |
-
text-align: center;
|
54 |
-
padding-right: 20px;
|
55 |
-
padding-left: 20px;
|
56 |
-
color: #E9EBED;
|
57 |
-
margin-bottom: 2rem;
|
58 |
-
}
|
59 |
-
.main-header2 {
|
60 |
-
text-align: left;
|
61 |
-
color: #E9EBED;
|
62 |
-
}
|
63 |
-
.column-header {
|
64 |
-
color: #FFFF9E;
|
65 |
-
border-bottom: 2px solid #eee;
|
66 |
-
padding-bottom: 10px;
|
67 |
-
margin-bottom: 1.5rem;
|
68 |
-
}
|
69 |
-
.column-header2 {
|
70 |
-
color: #CEFFFF;
|
71 |
-
padding-top: 5px;
|
72 |
-
padding-bottom: 5px;
|
73 |
-
}
|
74 |
-
.content-section {
|
75 |
-
background-color: #f8f9fa;
|
76 |
-
padding: 15px;
|
77 |
-
border-radius: 5px;
|
78 |
-
margin-top: 10px;
|
79 |
-
}
|
80 |
-
.metric-card {
|
81 |
-
background-color: #1E1E1E;
|
82 |
-
padding: 1rem;
|
83 |
-
border-radius: 8px;
|
84 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
85 |
-
margin-bottom: 1rem;
|
86 |
-
}
|
87 |
-
.metric-title {
|
88 |
-
font-size: 0.9rem;
|
89 |
-
color: #888;
|
90 |
-
margin-bottom: 0.5rem;
|
91 |
-
}
|
92 |
-
.metric-value {
|
93 |
-
font-size: 1.5rem;
|
94 |
-
font-weight: bold;
|
95 |
-
color: #fff;
|
96 |
-
}
|
97 |
-
</style>
|
98 |
-
""", unsafe_allow_html=True)
|
99 |
-
|
100 |
-
def _setup_header(self):
|
101 |
-
st.markdown("<h1 class='main-header'>RAG Stock Analysis</h1>", unsafe_allow_html=True)
|
102 |
-
with st.expander("Available Historical Demo Companies"):
|
103 |
-
st.markdown("""
|
104 |
-
For Demo purpose, historical data is available only for the below companies:
|
105 |
-
- Reliance Industries (RELIANCE)
|
106 |
-
- HDFC Bank (HDFCBANK)
|
107 |
-
- Hindustan Unilever (HINDUNILVR)
|
108 |
-
- Bharti Airtel (BHARTIARTL)
|
109 |
-
- Asian Paints (ASIANPAINT)
|
110 |
-
- Maruti Suzuki India (MARUTI)
|
111 |
-
""", unsafe_allow_html=True)
|
112 |
-
|
113 |
-
class StockDataVisualizer:
|
114 |
-
@staticmethod
|
115 |
-
def create_price_chart(df, symbol):
|
116 |
-
fig = go.Figure()
|
117 |
-
|
118 |
-
fig.add_trace(go.Candlestick(
|
119 |
-
x=df.index,
|
120 |
-
open=df['Open'],
|
121 |
-
high=df['High'],
|
122 |
-
low=df['Low'],
|
123 |
-
close=df['Close'],
|
124 |
-
name='OHLC'
|
125 |
-
))
|
126 |
-
|
127 |
-
fig.update_layout(
|
128 |
-
title=f'{symbol} Stock Price Movement',
|
129 |
-
yaxis_title='Stock Price (INR)',
|
130 |
-
template='plotly_dark',
|
131 |
-
xaxis_rangeslider_visible=False,
|
132 |
-
height=500
|
133 |
-
)
|
134 |
-
|
135 |
-
return fig
|
136 |
-
|
137 |
-
@staticmethod
|
138 |
-
def create_volume_chart(df, symbol):
|
139 |
-
fig = go.Figure()
|
140 |
-
|
141 |
-
fig.add_trace(go.Bar(
|
142 |
-
x=df.index,
|
143 |
-
y=df['Volume'],
|
144 |
-
name='Volume',
|
145 |
-
marker_color='rgba(0, 150, 255, 0.6)'
|
146 |
-
))
|
147 |
-
|
148 |
-
fig.update_layout(
|
149 |
-
title=f'{symbol} Trading Volume',
|
150 |
-
yaxis_title='Volume',
|
151 |
-
template='plotly_dark',
|
152 |
-
height=300
|
153 |
-
)
|
154 |
-
|
155 |
-
return fig
|
156 |
-
|
157 |
-
@staticmethod
|
158 |
-
def create_sentiment_gauge(sentiment_score):
|
159 |
-
fig = go.Figure(go.Indicator(
|
160 |
-
mode="gauge+number",
|
161 |
-
value=sentiment_score,
|
162 |
-
domain={'x': [0, 1], 'y': [0, 1]},
|
163 |
-
gauge={
|
164 |
-
'axis': {'range': [-1, 1]},
|
165 |
-
'bar': {'color': "rgba(0, 150, 255, 0.6)"},
|
166 |
-
'steps': [
|
167 |
-
{'range': [-1, -0.25], 'color': "red"},
|
168 |
-
{'range': [-0.25, 0.25], 'color': "yellow"},
|
169 |
-
{'range': [0.25, 1], 'color': "green"}
|
170 |
-
]
|
171 |
-
},
|
172 |
-
title={'text': "Sentiment Score"}
|
173 |
-
))
|
174 |
-
|
175 |
-
fig.update_layout(
|
176 |
-
template='plotly_dark',
|
177 |
-
height=250
|
178 |
-
)
|
179 |
-
|
180 |
-
return fig
|
181 |
-
|
182 |
-
class StockAdviser:
|
183 |
-
def __init__(self):
|
184 |
-
self.config = StockAdviserConfig()
|
185 |
-
self.ui = StockAdviserUI()
|
186 |
-
self.visualizer = StockDataVisualizer()
|
187 |
-
self.client = AzureOpenAI(
|
188 |
-
azure_endpoint=self.config.azure_config["base_url"],
|
189 |
-
api_key=self.config.azure_config["api-key"],
|
190 |
-
api_version="2024-02-01"
|
191 |
-
)
|
192 |
-
|
193 |
-
|
194 |
-
def create_models(self):
|
195 |
-
print("creating models")
|
196 |
-
llm = AzureChatOpenAI(
|
197 |
-
temperature=0,
|
198 |
-
api_key=self.config.azure_config["api-key"],
|
199 |
-
openai_api_version=self.config.azure_config["api_version"],
|
200 |
-
azure_endpoint=self.config.azure_config["base_url"],
|
201 |
-
model=self.config.azure_config["model_deployment"],
|
202 |
-
validate_base_url=False
|
203 |
-
)
|
204 |
-
embedding_model = AzureOpenAIEmbeddings(
|
205 |
-
api_key=self.config.azure_config["api-key"],
|
206 |
-
openai_api_version=self.config.azure_config["api_version"],
|
207 |
-
azure_endpoint=self.config.azure_config["embedding_base_url"],
|
208 |
-
model=self.config.azure_config["embedding_deployment"]
|
209 |
-
)
|
210 |
-
self.config.models.llm = llm
|
211 |
-
self.config.models.embedding_model = embedding_model
|
212 |
-
return self.config.models
|
213 |
-
|
214 |
-
def get_symbol(self, user_question):
|
215 |
-
qna_system_message = """
|
216 |
-
You are an assistant to a financial services firm who finds the 'nse company symbol' (assigned to the company in the provided stock market)) of the company in the question provided.
|
217 |
-
|
218 |
-
User questions will begin with the token: ###Question.
|
219 |
-
|
220 |
-
Please find the 'nse company symbol' of the company in the question provided. In case of an invalid company, return "NOTICKER".
|
221 |
-
|
222 |
-
Response format:
|
223 |
-
{nse company symbol}
|
224 |
-
|
225 |
-
Do not mention anything about the context in your final answer. Stricktly respond only the company symbol.
|
226 |
-
"""
|
227 |
-
|
228 |
-
qna_user_message_template = """
|
229 |
-
###Question
|
230 |
-
{question}
|
231 |
-
"""
|
232 |
-
|
233 |
-
prompt = [
|
234 |
-
{'role': 'system', 'content': qna_system_message},
|
235 |
-
{'role': 'user', 'content': qna_user_message_template.format(question=user_question)}
|
236 |
-
]
|
237 |
-
|
238 |
-
try:
|
239 |
-
response = self.client.chat.completions.create(
|
240 |
-
model=self.config.azure_config["model_name"],
|
241 |
-
messages=prompt,
|
242 |
-
temperature=0
|
243 |
-
)
|
244 |
-
cmp_tkr = response.choices[0].message.content.strip()
|
245 |
-
except Exception as e:
|
246 |
-
cmp_tkr = f'Sorry, I encountered the following error: \n {e}'
|
247 |
-
st.write("Reply: ", cmp_tkr)
|
248 |
-
return
|
249 |
-
print(cmp_tkr)
|
250 |
-
return cmp_tkr
|
251 |
-
|
252 |
-
|
253 |
-
def process_historical_data(self, user_question):
|
254 |
-
cmp_tr = self.get_symbol(user_question)
|
255 |
-
|
256 |
-
# Initialize ChromaDB Database
|
257 |
-
chroma_db = DBStorage()
|
258 |
-
FAISS_DB_PATH = os.path.join(os.getcwd(), "
|
259 |
-
chroma_db.load_vectors(FAISS_DB_PATH)
|
260 |
-
context_for_query = chroma_db.get_context_for_query(cmp_tr, k=5)
|
261 |
-
|
262 |
-
sentiment_response = self._get_sentiment_analysis(context_for_query, cmp_tr)
|
263 |
-
self._display_sentiment(sentiment_response)
|
264 |
-
|
265 |
-
return cmp_tr
|
266 |
-
|
267 |
-
def display_charts(self,cmp_tr,sentiment_response):
|
268 |
-
sentiment = self._extract_between(sentiment_response, "Overall Sentiment:", "Overall Justification:").strip()
|
269 |
-
|
270 |
-
days = 365
|
271 |
-
|
272 |
-
print(f"\nFetching {days} days of stock data for {cmp_tr}...")
|
273 |
-
df, analysis = self.get_nse_stock_data(cmp_tr, days)
|
274 |
-
|
275 |
-
print(analysis)
|
276 |
-
|
277 |
-
# Create metrics cards
|
278 |
-
col1, col2, col3 = st.columns(3)
|
279 |
-
|
280 |
-
# Simulate some metric data (replace with real data in production)
|
281 |
-
with col1:
|
282 |
-
self._create_metric_card(f"52-Week High on {analysis['week_52_high_date']}",
|
283 |
-
f"₹{analysis['week_52_high']:,.2f}",
|
284 |
-
self.format_percentage(analysis['pct_from_52w_high']))
|
285 |
-
with col2:
|
286 |
-
self._create_metric_card(f"52-Week Low on {analysis['week_52_low_date']}",
|
287 |
-
f"₹{analysis['week_52_low']:,.2f}",
|
288 |
-
self.format_percentage(analysis['pct_from_52w_low']))
|
289 |
-
with col3:
|
290 |
-
self._create_metric_card("Average Volume",
|
291 |
-
f"{int(analysis['avg_volume']):,}",
|
292 |
-
f"{self.format_percentage(analysis['volume_pct_diff'])}")
|
293 |
-
|
294 |
-
# Display price chart
|
295 |
-
st.plotly_chart(self.visualizer.create_price_chart(df, cmp_tr))
|
296 |
-
|
297 |
-
# Display volume chart
|
298 |
-
st.plotly_chart(self.visualizer.create_volume_chart(df, cmp_tr))
|
299 |
-
|
300 |
-
# Display sentiment gauge (simulate sentiment score)
|
301 |
-
# Generating random score for Demo purpose
|
302 |
-
if sentiment == "Negative":
|
303 |
-
sentiment_score = np.random.uniform(-1, -0.75)
|
304 |
-
elif sentiment == "Neutral":
|
305 |
-
sentiment_score = np.random.uniform(-0.75, 0.25)
|
306 |
-
elif sentiment == "Positive":
|
307 |
-
sentiment_score = np.random.uniform(0.25, 1)
|
308 |
-
else:
|
309 |
-
sentiment_score = 0
|
310 |
-
|
311 |
-
|
312 |
-
st.plotly_chart(self.visualizer.create_sentiment_gauge(sentiment_score))
|
313 |
-
|
314 |
-
def get_nse_stock_data(self,symbol, days):
|
315 |
-
"""
|
316 |
-
Fetch stock data and perform extended analysis including 52-week highs/lows
|
317 |
-
and volume comparisons.
|
318 |
-
|
319 |
-
Args:
|
320 |
-
symbol (str): NSE stock symbol (e.g., 'RELIANCE.NS')
|
321 |
-
|
322 |
-
Returns:
|
323 |
-
tuple: (DataFrame of daily data, dict of analysis metrics)
|
324 |
-
"""
|
325 |
-
try:
|
326 |
-
# Add .NS suffix if not present
|
327 |
-
if not symbol.endswith('.NS'):
|
328 |
-
symbol = f"{symbol}.NS"
|
329 |
-
|
330 |
-
# Create Ticker object and fetch 1 year of data
|
331 |
-
ticker = yf.Ticker(symbol)
|
332 |
-
|
333 |
-
# Get last 90 days of data
|
334 |
-
end_date = datetime.now()
|
335 |
-
start_date = end_date - timedelta(days=days)
|
336 |
-
df_90d = ticker.history(start=start_date, end=end_date)
|
337 |
-
|
338 |
-
# Get 1 year of data for 52-week analysis
|
339 |
-
start_date_52w = end_date - timedelta(days=365)
|
340 |
-
df_52w = ticker.history(start=start_date_52w, end=end_date)
|
341 |
-
|
342 |
-
# Create main DataFrame with 90-day data
|
343 |
-
df = pd.DataFrame({
|
344 |
-
'Open': df_90d['Open'],
|
345 |
-
'High': df_90d['High'],
|
346 |
-
'Low': df_90d['Low'],
|
347 |
-
'Close': df_90d['Close'],
|
348 |
-
'Volume': df_90d['Volume']
|
349 |
-
}, index=df_90d.index)
|
350 |
-
|
351 |
-
# Round numerical values
|
352 |
-
df[['Open', 'High', 'Low', 'Close']] = df[['Open', 'High', 'Low', 'Close']].round(2)
|
353 |
-
df['Volume'] = df['Volume'].astype(int)
|
354 |
-
|
355 |
-
# Get current price (latest close)
|
356 |
-
current_price = df['Close'].iloc[-1]
|
357 |
-
|
358 |
-
# Calculate 52-week metrics
|
359 |
-
week_52_high = df_52w['High'].max()
|
360 |
-
week_52_low = df_52w['Low'].min()
|
361 |
-
|
362 |
-
# Calculate percentage differences
|
363 |
-
pct_from_52w_high = ((current_price - week_52_high) / week_52_high) * 100
|
364 |
-
pct_from_52w_low = ((current_price - week_52_low) / week_52_low) * 100
|
365 |
-
|
366 |
-
# Volume analysis
|
367 |
-
current_volume = df['Volume'].iloc[-1]
|
368 |
-
avg_volume = df_52w['Volume'].mean()
|
369 |
-
volume_pct_diff = ((current_volume - avg_volume) / avg_volume) * 100
|
370 |
-
|
371 |
-
# Find dates of 52-week high and low
|
372 |
-
high_date = df_52w[df_52w['High'] == week_52_high].index[0].strftime('%Y-%m-%d')
|
373 |
-
low_date = df_52w[df_52w['Low'] == week_52_low].index[0].strftime('%Y-%m-%d')
|
374 |
-
|
375 |
-
# Create analysis metrics dictionary
|
376 |
-
analysis = {
|
377 |
-
'current_price': current_price,
|
378 |
-
'week_52_high': week_52_high,
|
379 |
-
'week_52_high_date': high_date,
|
380 |
-
'week_52_low': week_52_low,
|
381 |
-
'week_52_low_date': low_date,
|
382 |
-
'pct_from_52w_high': pct_from_52w_high,
|
383 |
-
'pct_from_52w_low': pct_from_52w_low,
|
384 |
-
'current_volume': current_volume,
|
385 |
-
'avg_volume': avg_volume,
|
386 |
-
'volume_pct_diff': volume_pct_diff
|
387 |
-
}
|
388 |
-
|
389 |
-
print(analysis)
|
390 |
-
|
391 |
-
return df, analysis
|
392 |
-
|
393 |
-
except Exception as e:
|
394 |
-
print(f"Error fetching data: {str(e)}")
|
395 |
-
return None, None
|
396 |
-
|
397 |
-
def format_percentage(self, value):
|
398 |
-
"""Format percentage with + or - sign"""
|
399 |
-
return f"+{value:.2f}%" if value > 0 else f"{value:.2f}%"
|
400 |
-
|
401 |
-
|
402 |
-
def process_realtime_data(self, cmp_tr):
|
403 |
-
if cmp_tr == "NOTICKER":
|
404 |
-
st.write("No valid company in the query.")
|
405 |
-
return
|
406 |
-
|
407 |
-
data_fetch = DataFetch()
|
408 |
-
query_context = []
|
409 |
-
|
410 |
-
# Create a placeholder for the current source
|
411 |
-
source_status = st.empty()
|
412 |
-
|
413 |
-
# Collect data from various sources
|
414 |
-
data_sources = [
|
415 |
-
("Reddit", data_fetch.collect_reddit_data),
|
416 |
-
("YouTube", data_fetch.collect_youtube_data),
|
417 |
-
("Tumblr", data_fetch.collect_tumblr_data),
|
418 |
-
("Google News", data_fetch.collect_google_news),
|
419 |
-
("Financial Times", data_fetch.collect_financial_times),
|
420 |
-
("Bloomberg", data_fetch.collect_bloomberg),
|
421 |
-
("Reuters", data_fetch.collect_reuters)
|
422 |
-
]
|
423 |
-
|
424 |
-
st_status = ""
|
425 |
-
|
426 |
-
for source_name, collect_func in data_sources:
|
427 |
-
st_status = st_status.replace("Currently fetching", "Fetched") + f"📡 Currently fetching data from: {source_name} \n \n"
|
428 |
-
source_status.write(st_status, unsafe_allow_html=True)
|
429 |
-
print(f"Collecting {source_name} Data")
|
430 |
-
query_context.extend(collect_func(cmp_tr))
|
431 |
-
|
432 |
-
st_status = st_status.replace("Currently fetching", "Fetched") + "📡 Currently fetching data from: Serper - StockNews, Yahoo Finance, Insider Monkey, Investor's Business Daily, etc."
|
433 |
-
source_status.write(st_status, unsafe_allow_html=True)
|
434 |
-
print("Collecting Serper Data")
|
435 |
-
query_context.extend(data_fetch.search_news(cmp_tr, 100))
|
436 |
-
|
437 |
-
# Process collected data
|
438 |
-
db_store = DBStorage()
|
439 |
-
FAISS_DB_PATH = os.path.join(os.getcwd(), "
|
440 |
-
db_store.embed_vectors(to_documents(query_context), FAISS_DB_PATH)
|
441 |
-
|
442 |
-
db_store.load_vectors(FAISS_DB_PATH)
|
443 |
-
context_for_query = db_store.get_context_for_query(cmp_tr, k=5)
|
444 |
-
|
445 |
-
sentiment_response = self._get_sentiment_analysis(context_for_query, cmp_tr, is_realtime=True)
|
446 |
-
self._display_sentiment(sentiment_response)
|
447 |
-
|
448 |
-
# Clear the status message after all sources are processed
|
449 |
-
source_status.empty()
|
450 |
-
|
451 |
-
return sentiment_response
|
452 |
-
|
453 |
-
|
454 |
-
def _create_metric_card(self, title, value, change):
|
455 |
-
st.markdown(f"""
|
456 |
-
<div class="metric-card">
|
457 |
-
<div class="metric-title">{title}</div>
|
458 |
-
<div class="metric-value">{value}</div>
|
459 |
-
<div style="color: {'green' if float(change.strip('%')) > 0 else 'red'}">
|
460 |
-
{change}
|
461 |
-
</div>
|
462 |
-
</div>
|
463 |
-
""", unsafe_allow_html=True)
|
464 |
-
|
465 |
-
def _get_sentiment_analysis(self, context, cmp_tr, is_realtime=False):
|
466 |
-
system_message = self._get_system_prompt(is_realtime)
|
467 |
-
user_message = f"""
|
468 |
-
###Context
|
469 |
-
Here are some documents that are relevant to the question mentioned below.
|
470 |
-
{context}
|
471 |
-
|
472 |
-
###Question
|
473 |
-
{cmp_tr}
|
474 |
-
"""
|
475 |
-
|
476 |
-
try:
|
477 |
-
response = self.client.chat.completions.create(
|
478 |
-
model=self.config.azure_config["model_name"],
|
479 |
-
messages=[
|
480 |
-
{'role': 'system', 'content': system_message},
|
481 |
-
{'role': 'user', 'content': user_message}
|
482 |
-
],
|
483 |
-
temperature=0
|
484 |
-
)
|
485 |
-
return response.choices[0].message.content.strip()
|
486 |
-
except Exception as e:
|
487 |
-
return f'Sorry, I encountered the following error: \n {e}'
|
488 |
-
|
489 |
-
def _display_sentiment(self, prediction):
|
490 |
-
sentiment = self._extract_between(prediction, "Overall Sentiment:", "Overall Justification:").strip()
|
491 |
-
print("Sentiment: "+ sentiment)
|
492 |
-
print(prediction)
|
493 |
-
if sentiment == "Positive":
|
494 |
-
st.success("Positive : Go Ahead...!")
|
495 |
-
elif sentiment == "Negative":
|
496 |
-
st.warning("Negative : Don't...!")
|
497 |
-
elif sentiment == "Neutral":
|
498 |
-
st.info("Neutral : Need to Analyse further")
|
499 |
-
st.write(prediction, unsafe_allow_html=True)
|
500 |
-
|
501 |
-
@staticmethod
|
502 |
-
def _extract_between(text: str, start: str, end: str) -> str:
|
503 |
-
try:
|
504 |
-
start_pos = text.find(start)
|
505 |
-
if start_pos == -1:
|
506 |
-
return ""
|
507 |
-
start_pos += len(start)
|
508 |
-
end_pos = text.find(end, start_pos)
|
509 |
-
if end_pos == -1:
|
510 |
-
return ""
|
511 |
-
return text[start_pos:end_pos]
|
512 |
-
except (AttributeError, TypeError):
|
513 |
-
return ""
|
514 |
-
|
515 |
-
@staticmethod
|
516 |
-
def _get_system_prompt(is_realtime):
|
517 |
-
"""
|
518 |
-
Returns the appropriate system prompt based on whether it's realtime or historical data analysis.
|
519 |
-
|
520 |
-
Args:
|
521 |
-
is_realtime (bool): Flag indicating if this is for realtime data analysis
|
522 |
-
|
523 |
-
Returns:
|
524 |
-
str: The complete system prompt for the sentiment analysis
|
525 |
-
"""
|
526 |
-
base_prompt = """
|
527 |
-
You are an assistant to a financial services firm who answers user queries on Stock Investments.
|
528 |
-
User input will have the context required by you to answer user questions.
|
529 |
-
This context will begin with the token: ###Context.
|
530 |
-
The context contains references to specific portions of a document relevant to the user query.
|
531 |
-
|
532 |
-
User questions will begin with the token: ###Question.
|
533 |
-
|
534 |
-
First, find the 'nse company symbol' of the related company in the question provided.
|
535 |
-
Your task is to perform sentiment analysis on the content part of each documents provided in the Context, which discuss a company identified by its 'nse company symbol'. The goal is to determine the overall sentiment expressed across all documents and provide an overall justification. Based on the sentiment analysis, give a recommendation on whether the company's stock should be purchased.
|
536 |
-
|
537 |
-
Step-by-Step Instructions:
|
538 |
-
1. See if the question is "NOTICKER". If so, give response and don't proceed.
|
539 |
-
2. If the company in question is not found in the context, give the corresponding response and don't proceed.
|
540 |
-
3. Read the Context: Carefully read the content parts of each document provided in the list of Documents.
|
541 |
-
4. Determine Overall Sentiment: Analyze the sentiment across all documents and categorize the overall sentiment as Positive, Negative, or Neutral.
|
542 |
-
5. Provide Overall Justification: Summarize the key points from all documents to justify the overall sentiment.
|
543 |
-
6. Stock Advice: Based on the overall sentiment and justification, provide a recommendation on whether the company's stock should be purchased.
|
544 |
-
|
545 |
-
Example Analysis:
|
546 |
-
Context:
|
547 |
-
[Document(metadata={'platform': 'Moneycontrol', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 134}, page_content="{'title': 'Asian Paints launches Neo Bharat Latex Paint to tap on booming demand', 'content': 'The company, which is the leading player in India, touts the new segment to being affordable, offering over 1000 shades for consumers.'}"), Document(metadata={'platform': 'MarketsMojo', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 128}, page_content="{'title': 'Asian Paints Ltd. Stock Performance Shows Positive Trend, Outperforms Sector by 0.9%', 'content': 'Asian Paints Ltd., a leading player in the paints industry, has seen a positive trend in its stock performance on July 10, 2024.'}"), Document(metadata={'platform': 'Business Standard', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 138}, page_content="{'title': 'Asian Paints, Indigo Paints, Kansai gain up to 5% on falling oil prices', 'content': 'Shares of paint companies were trading higher on Wednesday, rising up to 5 per cent on the BSE, on the back of a fall in crude oil prices.'}")]
|
548 |
-
"""
|
549 |
-
|
550 |
-
if is_realtime:
|
551 |
-
response_format = """
|
552 |
-
Response Formats:
|
553 |
-
Only If the Question is 'NOTICKER':
|
554 |
-
No valid company in the query.
|
555 |
-
|
556 |
-
Else, If the context does not have relevent data for the company:
|
557 |
-
Respond "Company {Company name} {nse company symbol}({symbol}) details not found in the RealTime Data".
|
558 |
-
"""
|
559 |
-
else:
|
560 |
-
response_format = """
|
561 |
-
Response Formats:
|
562 |
-
If the Question value is "NOTICKER":
|
563 |
-
No valid company in the query.
|
564 |
-
|
565 |
-
If the context does not have relevent data for the company (Question value):
|
566 |
-
Respond "Company {Company name} {nse company symbol}({symbol}) details not found in the Historical Data".
|
567 |
-
"""
|
568 |
-
|
569 |
-
common_format = """
|
570 |
-
else, If the content parts of context has relevent data:
|
571 |
-
Overall Sentiment: [Positive/Negative/Neutral] <line break>
|
572 |
-
Overall Justification: [Detailed analysis of why the sentiment was chosen, summarizing key points from the documents] <line break>
|
573 |
-
Stock Advice: [Clear recommendation on whether to purchase the stock, based on the sentiment analysis and justification]
|
574 |
-
|
575 |
-
Please follow the steps to analyze the sentiment of each document's content; and strictly follow exact structure illustrated in above example response to provide an overall sentiment, justification and give stock purchase advice. Provide only Overall response, don't provide documentwise response or any note. Decorate the response with html/css tags.
|
576 |
-
"""
|
577 |
-
|
578 |
-
return base_prompt + response_format + common_format
|
579 |
-
|
580 |
-
|
581 |
-
def main():
|
582 |
-
adviser = StockAdviser()
|
583 |
-
|
584 |
-
|
585 |
-
# Create sidebar for filters and settings
|
586 |
-
st.logo(
|
587 |
-
"https://cdn.shopify.com/s/files/1/0153/8513/3156/files/info_omac.png?v=1595717396",
|
588 |
-
size="large"
|
589 |
-
)
|
590 |
-
|
591 |
-
with st.sidebar:
|
592 |
-
# About the Application
|
593 |
-
st.markdown("""
|
594 |
-
<div style="background-color: #2d2d2d; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(255, 255, 255, 0.1);">
|
595 |
-
<h2 style="color: #e6e6e6; text-align: Left;">About the Application</h2>
|
596 |
-
<p style="font-size: 16px; color: #cccccc; line-height: 1.6; text-align: justify;">
|
597 |
-
This application provides investment managers with daily insights into social media and news sentiment surrounding specific stocks and companies.
|
598 |
-
By analyzing posts and articles across major platforms such as <strong>Reddit</strong>, <strong>YouTube</strong>, <strong>Tumblr</strong>, <strong>Google News</strong>,
|
599 |
-
<strong>Financial Times</strong>, <strong>Bloomberg</strong>, <strong>Reuters</strong>, and <strong>Wall Street Journal</strong> (WSJ), it detects shifts
|
600 |
-
in public and media opinion that may impact stock performance.
|
601 |
-
</p>
|
602 |
-
<p style="font-size: 16px; color: #cccccc; line-height: 1.6; text-align: justify;">
|
603 |
-
Additionally, sources like <strong>Serper</strong> provide data from <strong>StockNews</strong>, <strong>Yahoo Finance</strong>, <strong>Insider Monkey</strong>,
|
604 |
-
<strong>Investor's Business Daily</strong>, and others. Using advanced AI techniques, the application generates a sentiment report that serves as a leading indicator,
|
605 |
-
helping managers make informed, timely adjustments to their positions. With daily updates and historical trend analysis, it empowers users to stay ahead in a fast-paced,
|
606 |
-
sentiment-driven market.
|
607 |
-
</p>
|
608 |
-
</div>
|
609 |
-
|
610 |
-
""", unsafe_allow_html=True)
|
611 |
-
# Sidebar Footer (Floating Footer)
|
612 |
-
st.sidebar.markdown("""
|
613 |
-
<div style="position: fixed; bottom: 5px; padding: 5px; background-color: #1f1f1f; border-radius: 5px; text-align: left;">
|
614 |
-
<p style="color: #cccccc; font-size: 14px;">
|
615 |
-
Developed by: <a href="https://www.linkedin.com/in/karthikeyen92/" target="_blank" style="color: #4DA8DA; text-decoration: none;">Karthikeyen Packirisamy</a>
|
616 |
-
</p>
|
617 |
-
</div>
|
618 |
-
""", unsafe_allow_html=True)
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
# Main content
|
623 |
-
cmp_tr = "NOTICKER"
|
624 |
-
st.header("Ask a question")
|
625 |
-
user_question = st.text_input("Ask a stock advice related question", key="user_question")
|
626 |
-
|
627 |
-
col1, col2 = st.columns(2)
|
628 |
-
|
629 |
-
with col1:
|
630 |
-
st.markdown("<h2 class='column-header'>Historical Analysis</h2>", unsafe_allow_html=True)
|
631 |
-
with st.container():
|
632 |
-
if user_question:
|
633 |
-
cmp_tr = adviser.process_historical_data(user_question)
|
634 |
-
|
635 |
-
with col2:
|
636 |
-
st.markdown("<h2 class='column-header'>Real-Time Analysis</h2>", unsafe_allow_html=True)
|
637 |
-
with st.container():
|
638 |
-
if user_question:
|
639 |
-
sentiment_response = adviser.process_realtime_data(cmp_tr)
|
640 |
-
|
641 |
-
if (str(cmp_tr) is not "NOTICKER"):
|
642 |
-
with st.container():
|
643 |
-
if user_question:
|
644 |
-
adviser.display_charts(cmp_tr,sentiment_response)
|
645 |
-
|
646 |
-
st.markdown("---")
|
647 |
-
st.markdown("<p style='text-align: center; color: #666;'>© 2024 EY</p>", unsafe_allow_html=True)
|
648 |
-
|
649 |
-
if __name__ == "__main__":
|
650 |
main()
|
|
|
1 |
+
import sys
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import streamlit as st
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
import plotly.express as px
|
9 |
+
from openai import AzureOpenAI
|
10 |
+
import pandas as pd
|
11 |
+
import numpy as np
|
12 |
+
from datetime import datetime, timedelta
|
13 |
+
from dotted_dict import DottedDict
|
14 |
+
from langchain_community.vectorstores import Chroma
|
15 |
+
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
|
16 |
+
from py.data_fetch import DataFetch
|
17 |
+
from py.handle_files import *
|
18 |
+
from py.db_storage import DBStorage
|
19 |
+
from langchain.callbacks import get_openai_callback
|
20 |
+
from PyPDF2 import PdfReader
|
21 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
22 |
+
from langchain_community.vectorstores import FAISS
|
23 |
+
from langchain.chains.question_answering import load_qa_chain
|
24 |
+
from langchain.prompts import PromptTemplate
|
25 |
+
import yfinance as yf
|
26 |
+
|
27 |
+
class StockAdviserConfig:
|
28 |
+
def __init__(self):
|
29 |
+
load_dotenv()
|
30 |
+
self.azure_config = {
|
31 |
+
"base_url": os.getenv("AZURE_OPENAI_ENDPOINT"),
|
32 |
+
"embedding_base_url": os.getenv("AZURE_OPENAI_EMBEDDING_ENDPOINT"),
|
33 |
+
"model_deployment": os.getenv("AZURE_OPENAI_MODEL_DEPLOYMENT_NAME"),
|
34 |
+
"model_name": os.getenv("AZURE_OPENAI_MODEL_NAME"),
|
35 |
+
"embedding_deployment": os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME"),
|
36 |
+
"embedding_name": os.getenv("AZURE_OPENAI_EMBEDDING_NAME"),
|
37 |
+
"api-key": os.getenv("AZURE_OPENAI_API_KEY"),
|
38 |
+
"api_version": os.getenv("AZURE_OPENAI_API_VERSION")
|
39 |
+
}
|
40 |
+
self.models = DottedDict()
|
41 |
+
|
42 |
+
class StockAdviserUI:
|
43 |
+
def __init__(self):
|
44 |
+
st.set_page_config(page_title="GEN AI Stock Adviser by Karthikeyen", layout="wide",
|
45 |
+
initial_sidebar_state="expanded")
|
46 |
+
self._setup_css()
|
47 |
+
self._setup_header()
|
48 |
+
|
49 |
+
def _setup_css(self):
|
50 |
+
st.markdown("""
|
51 |
+
<style>
|
52 |
+
.main-header {
|
53 |
+
text-align: center;
|
54 |
+
padding-right: 20px;
|
55 |
+
padding-left: 20px;
|
56 |
+
color: #E9EBED;
|
57 |
+
margin-bottom: 2rem;
|
58 |
+
}
|
59 |
+
.main-header2 {
|
60 |
+
text-align: left;
|
61 |
+
color: #E9EBED;
|
62 |
+
}
|
63 |
+
.column-header {
|
64 |
+
color: #FFFF9E;
|
65 |
+
border-bottom: 2px solid #eee;
|
66 |
+
padding-bottom: 10px;
|
67 |
+
margin-bottom: 1.5rem;
|
68 |
+
}
|
69 |
+
.column-header2 {
|
70 |
+
color: #CEFFFF;
|
71 |
+
padding-top: 5px;
|
72 |
+
padding-bottom: 5px;
|
73 |
+
}
|
74 |
+
.content-section {
|
75 |
+
background-color: #f8f9fa;
|
76 |
+
padding: 15px;
|
77 |
+
border-radius: 5px;
|
78 |
+
margin-top: 10px;
|
79 |
+
}
|
80 |
+
.metric-card {
|
81 |
+
background-color: #1E1E1E;
|
82 |
+
padding: 1rem;
|
83 |
+
border-radius: 8px;
|
84 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
85 |
+
margin-bottom: 1rem;
|
86 |
+
}
|
87 |
+
.metric-title {
|
88 |
+
font-size: 0.9rem;
|
89 |
+
color: #888;
|
90 |
+
margin-bottom: 0.5rem;
|
91 |
+
}
|
92 |
+
.metric-value {
|
93 |
+
font-size: 1.5rem;
|
94 |
+
font-weight: bold;
|
95 |
+
color: #fff;
|
96 |
+
}
|
97 |
+
</style>
|
98 |
+
""", unsafe_allow_html=True)
|
99 |
+
|
100 |
+
def _setup_header(self):
|
101 |
+
st.markdown("<h1 class='main-header'>RAG Stock Analysis</h1>", unsafe_allow_html=True)
|
102 |
+
with st.expander("Available Historical Demo Companies"):
|
103 |
+
st.markdown("""
|
104 |
+
For Demo purpose, historical data is available only for the below companies:
|
105 |
+
- Reliance Industries (RELIANCE)
|
106 |
+
- HDFC Bank (HDFCBANK)
|
107 |
+
- Hindustan Unilever (HINDUNILVR)
|
108 |
+
- Bharti Airtel (BHARTIARTL)
|
109 |
+
- Asian Paints (ASIANPAINT)
|
110 |
+
- Maruti Suzuki India (MARUTI)
|
111 |
+
""", unsafe_allow_html=True)
|
112 |
+
|
113 |
+
class StockDataVisualizer:
|
114 |
+
@staticmethod
|
115 |
+
def create_price_chart(df, symbol):
|
116 |
+
fig = go.Figure()
|
117 |
+
|
118 |
+
fig.add_trace(go.Candlestick(
|
119 |
+
x=df.index,
|
120 |
+
open=df['Open'],
|
121 |
+
high=df['High'],
|
122 |
+
low=df['Low'],
|
123 |
+
close=df['Close'],
|
124 |
+
name='OHLC'
|
125 |
+
))
|
126 |
+
|
127 |
+
fig.update_layout(
|
128 |
+
title=f'{symbol} Stock Price Movement',
|
129 |
+
yaxis_title='Stock Price (INR)',
|
130 |
+
template='plotly_dark',
|
131 |
+
xaxis_rangeslider_visible=False,
|
132 |
+
height=500
|
133 |
+
)
|
134 |
+
|
135 |
+
return fig
|
136 |
+
|
137 |
+
@staticmethod
|
138 |
+
def create_volume_chart(df, symbol):
|
139 |
+
fig = go.Figure()
|
140 |
+
|
141 |
+
fig.add_trace(go.Bar(
|
142 |
+
x=df.index,
|
143 |
+
y=df['Volume'],
|
144 |
+
name='Volume',
|
145 |
+
marker_color='rgba(0, 150, 255, 0.6)'
|
146 |
+
))
|
147 |
+
|
148 |
+
fig.update_layout(
|
149 |
+
title=f'{symbol} Trading Volume',
|
150 |
+
yaxis_title='Volume',
|
151 |
+
template='plotly_dark',
|
152 |
+
height=300
|
153 |
+
)
|
154 |
+
|
155 |
+
return fig
|
156 |
+
|
157 |
+
@staticmethod
|
158 |
+
def create_sentiment_gauge(sentiment_score):
|
159 |
+
fig = go.Figure(go.Indicator(
|
160 |
+
mode="gauge+number",
|
161 |
+
value=sentiment_score,
|
162 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
163 |
+
gauge={
|
164 |
+
'axis': {'range': [-1, 1]},
|
165 |
+
'bar': {'color': "rgba(0, 150, 255, 0.6)"},
|
166 |
+
'steps': [
|
167 |
+
{'range': [-1, -0.25], 'color': "red"},
|
168 |
+
{'range': [-0.25, 0.25], 'color': "yellow"},
|
169 |
+
{'range': [0.25, 1], 'color': "green"}
|
170 |
+
]
|
171 |
+
},
|
172 |
+
title={'text': "Sentiment Score"}
|
173 |
+
))
|
174 |
+
|
175 |
+
fig.update_layout(
|
176 |
+
template='plotly_dark',
|
177 |
+
height=250
|
178 |
+
)
|
179 |
+
|
180 |
+
return fig
|
181 |
+
|
182 |
+
class StockAdviser:
|
183 |
+
def __init__(self):
|
184 |
+
self.config = StockAdviserConfig()
|
185 |
+
self.ui = StockAdviserUI()
|
186 |
+
self.visualizer = StockDataVisualizer()
|
187 |
+
self.client = AzureOpenAI(
|
188 |
+
azure_endpoint=self.config.azure_config["base_url"],
|
189 |
+
api_key=self.config.azure_config["api-key"],
|
190 |
+
api_version="2024-02-01"
|
191 |
+
)
|
192 |
+
|
193 |
+
|
194 |
+
def create_models(self):
|
195 |
+
print("creating models")
|
196 |
+
llm = AzureChatOpenAI(
|
197 |
+
temperature=0,
|
198 |
+
api_key=self.config.azure_config["api-key"],
|
199 |
+
openai_api_version=self.config.azure_config["api_version"],
|
200 |
+
azure_endpoint=self.config.azure_config["base_url"],
|
201 |
+
model=self.config.azure_config["model_deployment"],
|
202 |
+
validate_base_url=False
|
203 |
+
)
|
204 |
+
embedding_model = AzureOpenAIEmbeddings(
|
205 |
+
api_key=self.config.azure_config["api-key"],
|
206 |
+
openai_api_version=self.config.azure_config["api_version"],
|
207 |
+
azure_endpoint=self.config.azure_config["embedding_base_url"],
|
208 |
+
model=self.config.azure_config["embedding_deployment"]
|
209 |
+
)
|
210 |
+
self.config.models.llm = llm
|
211 |
+
self.config.models.embedding_model = embedding_model
|
212 |
+
return self.config.models
|
213 |
+
|
214 |
+
def get_symbol(self, user_question):
|
215 |
+
qna_system_message = """
|
216 |
+
You are an assistant to a financial services firm who finds the 'nse company symbol' (assigned to the company in the provided stock market)) of the company in the question provided.
|
217 |
+
|
218 |
+
User questions will begin with the token: ###Question.
|
219 |
+
|
220 |
+
Please find the 'nse company symbol' of the company in the question provided. In case of an invalid company, return "NOTICKER".
|
221 |
+
|
222 |
+
Response format:
|
223 |
+
{nse company symbol}
|
224 |
+
|
225 |
+
Do not mention anything about the context in your final answer. Stricktly respond only the company symbol.
|
226 |
+
"""
|
227 |
+
|
228 |
+
qna_user_message_template = """
|
229 |
+
###Question
|
230 |
+
{question}
|
231 |
+
"""
|
232 |
+
|
233 |
+
prompt = [
|
234 |
+
{'role': 'system', 'content': qna_system_message},
|
235 |
+
{'role': 'user', 'content': qna_user_message_template.format(question=user_question)}
|
236 |
+
]
|
237 |
+
|
238 |
+
try:
|
239 |
+
response = self.client.chat.completions.create(
|
240 |
+
model=self.config.azure_config["model_name"],
|
241 |
+
messages=prompt,
|
242 |
+
temperature=0
|
243 |
+
)
|
244 |
+
cmp_tkr = response.choices[0].message.content.strip()
|
245 |
+
except Exception as e:
|
246 |
+
cmp_tkr = f'Sorry, I encountered the following error: \n {e}'
|
247 |
+
st.write("Reply: ", cmp_tkr)
|
248 |
+
return
|
249 |
+
print(cmp_tkr)
|
250 |
+
return cmp_tkr
|
251 |
+
|
252 |
+
|
253 |
+
def process_historical_data(self, user_question):
|
254 |
+
cmp_tr = self.get_symbol(user_question)
|
255 |
+
|
256 |
+
# Initialize ChromaDB Database
|
257 |
+
chroma_db = DBStorage()
|
258 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_HD")
|
259 |
+
chroma_db.load_vectors(FAISS_DB_PATH)
|
260 |
+
context_for_query = chroma_db.get_context_for_query(cmp_tr, k=5)
|
261 |
+
|
262 |
+
sentiment_response = self._get_sentiment_analysis(context_for_query, cmp_tr)
|
263 |
+
self._display_sentiment(sentiment_response)
|
264 |
+
|
265 |
+
return cmp_tr
|
266 |
+
|
267 |
+
def display_charts(self,cmp_tr,sentiment_response):
|
268 |
+
sentiment = self._extract_between(sentiment_response, "Overall Sentiment:", "Overall Justification:").strip()
|
269 |
+
|
270 |
+
days = 365
|
271 |
+
|
272 |
+
print(f"\nFetching {days} days of stock data for {cmp_tr}...")
|
273 |
+
df, analysis = self.get_nse_stock_data(cmp_tr, days)
|
274 |
+
|
275 |
+
print(analysis)
|
276 |
+
|
277 |
+
# Create metrics cards
|
278 |
+
col1, col2, col3 = st.columns(3)
|
279 |
+
|
280 |
+
# Simulate some metric data (replace with real data in production)
|
281 |
+
with col1:
|
282 |
+
self._create_metric_card(f"52-Week High on {analysis['week_52_high_date']}",
|
283 |
+
f"₹{analysis['week_52_high']:,.2f}",
|
284 |
+
self.format_percentage(analysis['pct_from_52w_high']))
|
285 |
+
with col2:
|
286 |
+
self._create_metric_card(f"52-Week Low on {analysis['week_52_low_date']}",
|
287 |
+
f"₹{analysis['week_52_low']:,.2f}",
|
288 |
+
self.format_percentage(analysis['pct_from_52w_low']))
|
289 |
+
with col3:
|
290 |
+
self._create_metric_card("Average Volume",
|
291 |
+
f"{int(analysis['avg_volume']):,}",
|
292 |
+
f"{self.format_percentage(analysis['volume_pct_diff'])}")
|
293 |
+
|
294 |
+
# Display price chart
|
295 |
+
st.plotly_chart(self.visualizer.create_price_chart(df, cmp_tr))
|
296 |
+
|
297 |
+
# Display volume chart
|
298 |
+
st.plotly_chart(self.visualizer.create_volume_chart(df, cmp_tr))
|
299 |
+
|
300 |
+
# Display sentiment gauge (simulate sentiment score)
|
301 |
+
# Generating random score for Demo purpose
|
302 |
+
if sentiment == "Negative":
|
303 |
+
sentiment_score = np.random.uniform(-1, -0.75)
|
304 |
+
elif sentiment == "Neutral":
|
305 |
+
sentiment_score = np.random.uniform(-0.75, 0.25)
|
306 |
+
elif sentiment == "Positive":
|
307 |
+
sentiment_score = np.random.uniform(0.25, 1)
|
308 |
+
else:
|
309 |
+
sentiment_score = 0
|
310 |
+
|
311 |
+
|
312 |
+
st.plotly_chart(self.visualizer.create_sentiment_gauge(sentiment_score))
|
313 |
+
|
314 |
+
def get_nse_stock_data(self,symbol, days):
|
315 |
+
"""
|
316 |
+
Fetch stock data and perform extended analysis including 52-week highs/lows
|
317 |
+
and volume comparisons.
|
318 |
+
|
319 |
+
Args:
|
320 |
+
symbol (str): NSE stock symbol (e.g., 'RELIANCE.NS')
|
321 |
+
|
322 |
+
Returns:
|
323 |
+
tuple: (DataFrame of daily data, dict of analysis metrics)
|
324 |
+
"""
|
325 |
+
try:
|
326 |
+
# Add .NS suffix if not present
|
327 |
+
if not symbol.endswith('.NS'):
|
328 |
+
symbol = f"{symbol}.NS"
|
329 |
+
|
330 |
+
# Create Ticker object and fetch 1 year of data
|
331 |
+
ticker = yf.Ticker(symbol)
|
332 |
+
|
333 |
+
# Get last 90 days of data
|
334 |
+
end_date = datetime.now()
|
335 |
+
start_date = end_date - timedelta(days=days)
|
336 |
+
df_90d = ticker.history(start=start_date, end=end_date)
|
337 |
+
|
338 |
+
# Get 1 year of data for 52-week analysis
|
339 |
+
start_date_52w = end_date - timedelta(days=365)
|
340 |
+
df_52w = ticker.history(start=start_date_52w, end=end_date)
|
341 |
+
|
342 |
+
# Create main DataFrame with 90-day data
|
343 |
+
df = pd.DataFrame({
|
344 |
+
'Open': df_90d['Open'],
|
345 |
+
'High': df_90d['High'],
|
346 |
+
'Low': df_90d['Low'],
|
347 |
+
'Close': df_90d['Close'],
|
348 |
+
'Volume': df_90d['Volume']
|
349 |
+
}, index=df_90d.index)
|
350 |
+
|
351 |
+
# Round numerical values
|
352 |
+
df[['Open', 'High', 'Low', 'Close']] = df[['Open', 'High', 'Low', 'Close']].round(2)
|
353 |
+
df['Volume'] = df['Volume'].astype(int)
|
354 |
+
|
355 |
+
# Get current price (latest close)
|
356 |
+
current_price = df['Close'].iloc[-1]
|
357 |
+
|
358 |
+
# Calculate 52-week metrics
|
359 |
+
week_52_high = df_52w['High'].max()
|
360 |
+
week_52_low = df_52w['Low'].min()
|
361 |
+
|
362 |
+
# Calculate percentage differences
|
363 |
+
pct_from_52w_high = ((current_price - week_52_high) / week_52_high) * 100
|
364 |
+
pct_from_52w_low = ((current_price - week_52_low) / week_52_low) * 100
|
365 |
+
|
366 |
+
# Volume analysis
|
367 |
+
current_volume = df['Volume'].iloc[-1]
|
368 |
+
avg_volume = df_52w['Volume'].mean()
|
369 |
+
volume_pct_diff = ((current_volume - avg_volume) / avg_volume) * 100
|
370 |
+
|
371 |
+
# Find dates of 52-week high and low
|
372 |
+
high_date = df_52w[df_52w['High'] == week_52_high].index[0].strftime('%Y-%m-%d')
|
373 |
+
low_date = df_52w[df_52w['Low'] == week_52_low].index[0].strftime('%Y-%m-%d')
|
374 |
+
|
375 |
+
# Create analysis metrics dictionary
|
376 |
+
analysis = {
|
377 |
+
'current_price': current_price,
|
378 |
+
'week_52_high': week_52_high,
|
379 |
+
'week_52_high_date': high_date,
|
380 |
+
'week_52_low': week_52_low,
|
381 |
+
'week_52_low_date': low_date,
|
382 |
+
'pct_from_52w_high': pct_from_52w_high,
|
383 |
+
'pct_from_52w_low': pct_from_52w_low,
|
384 |
+
'current_volume': current_volume,
|
385 |
+
'avg_volume': avg_volume,
|
386 |
+
'volume_pct_diff': volume_pct_diff
|
387 |
+
}
|
388 |
+
|
389 |
+
print(analysis)
|
390 |
+
|
391 |
+
return df, analysis
|
392 |
+
|
393 |
+
except Exception as e:
|
394 |
+
print(f"Error fetching data: {str(e)}")
|
395 |
+
return None, None
|
396 |
+
|
397 |
+
def format_percentage(self, value):
|
398 |
+
"""Format percentage with + or - sign"""
|
399 |
+
return f"+{value:.2f}%" if value > 0 else f"{value:.2f}%"
|
400 |
+
|
401 |
+
|
402 |
+
def process_realtime_data(self, cmp_tr):
|
403 |
+
if cmp_tr == "NOTICKER":
|
404 |
+
st.write("No valid company in the query.")
|
405 |
+
return
|
406 |
+
|
407 |
+
data_fetch = DataFetch()
|
408 |
+
query_context = []
|
409 |
+
|
410 |
+
# Create a placeholder for the current source
|
411 |
+
source_status = st.empty()
|
412 |
+
|
413 |
+
# Collect data from various sources
|
414 |
+
data_sources = [
|
415 |
+
("Reddit", data_fetch.collect_reddit_data),
|
416 |
+
("YouTube", data_fetch.collect_youtube_data),
|
417 |
+
("Tumblr", data_fetch.collect_tumblr_data),
|
418 |
+
("Google News", data_fetch.collect_google_news),
|
419 |
+
("Financial Times", data_fetch.collect_financial_times),
|
420 |
+
("Bloomberg", data_fetch.collect_bloomberg),
|
421 |
+
("Reuters", data_fetch.collect_reuters)
|
422 |
+
]
|
423 |
+
|
424 |
+
st_status = ""
|
425 |
+
|
426 |
+
for source_name, collect_func in data_sources:
|
427 |
+
st_status = st_status.replace("Currently fetching", "Fetched") + f"📡 Currently fetching data from: {source_name} \n \n"
|
428 |
+
source_status.write(st_status, unsafe_allow_html=True)
|
429 |
+
print(f"Collecting {source_name} Data")
|
430 |
+
query_context.extend(collect_func(cmp_tr))
|
431 |
+
|
432 |
+
st_status = st_status.replace("Currently fetching", "Fetched") + "📡 Currently fetching data from: Serper - StockNews, Yahoo Finance, Insider Monkey, Investor's Business Daily, etc."
|
433 |
+
source_status.write(st_status, unsafe_allow_html=True)
|
434 |
+
print("Collecting Serper Data")
|
435 |
+
query_context.extend(data_fetch.search_news(cmp_tr, 100))
|
436 |
+
|
437 |
+
# Process collected data
|
438 |
+
db_store = DBStorage()
|
439 |
+
FAISS_DB_PATH = os.path.join(os.getcwd(), "faiss_RD")
|
440 |
+
db_store.embed_vectors(to_documents(query_context), FAISS_DB_PATH)
|
441 |
+
|
442 |
+
db_store.load_vectors(FAISS_DB_PATH)
|
443 |
+
context_for_query = db_store.get_context_for_query(cmp_tr, k=5)
|
444 |
+
|
445 |
+
sentiment_response = self._get_sentiment_analysis(context_for_query, cmp_tr, is_realtime=True)
|
446 |
+
self._display_sentiment(sentiment_response)
|
447 |
+
|
448 |
+
# Clear the status message after all sources are processed
|
449 |
+
source_status.empty()
|
450 |
+
|
451 |
+
return sentiment_response
|
452 |
+
|
453 |
+
|
454 |
+
def _create_metric_card(self, title, value, change):
|
455 |
+
st.markdown(f"""
|
456 |
+
<div class="metric-card">
|
457 |
+
<div class="metric-title">{title}</div>
|
458 |
+
<div class="metric-value">{value}</div>
|
459 |
+
<div style="color: {'green' if float(change.strip('%')) > 0 else 'red'}">
|
460 |
+
{change}
|
461 |
+
</div>
|
462 |
+
</div>
|
463 |
+
""", unsafe_allow_html=True)
|
464 |
+
|
465 |
+
def _get_sentiment_analysis(self, context, cmp_tr, is_realtime=False):
|
466 |
+
system_message = self._get_system_prompt(is_realtime)
|
467 |
+
user_message = f"""
|
468 |
+
###Context
|
469 |
+
Here are some documents that are relevant to the question mentioned below.
|
470 |
+
{context}
|
471 |
+
|
472 |
+
###Question
|
473 |
+
{cmp_tr}
|
474 |
+
"""
|
475 |
+
|
476 |
+
try:
|
477 |
+
response = self.client.chat.completions.create(
|
478 |
+
model=self.config.azure_config["model_name"],
|
479 |
+
messages=[
|
480 |
+
{'role': 'system', 'content': system_message},
|
481 |
+
{'role': 'user', 'content': user_message}
|
482 |
+
],
|
483 |
+
temperature=0
|
484 |
+
)
|
485 |
+
return response.choices[0].message.content.strip()
|
486 |
+
except Exception as e:
|
487 |
+
return f'Sorry, I encountered the following error: \n {e}'
|
488 |
+
|
489 |
+
def _display_sentiment(self, prediction):
|
490 |
+
sentiment = self._extract_between(prediction, "Overall Sentiment:", "Overall Justification:").strip()
|
491 |
+
print("Sentiment: "+ sentiment)
|
492 |
+
print(prediction)
|
493 |
+
if sentiment == "Positive":
|
494 |
+
st.success("Positive : Go Ahead...!")
|
495 |
+
elif sentiment == "Negative":
|
496 |
+
st.warning("Negative : Don't...!")
|
497 |
+
elif sentiment == "Neutral":
|
498 |
+
st.info("Neutral : Need to Analyse further")
|
499 |
+
st.write(prediction, unsafe_allow_html=True)
|
500 |
+
|
501 |
+
@staticmethod
|
502 |
+
def _extract_between(text: str, start: str, end: str) -> str:
|
503 |
+
try:
|
504 |
+
start_pos = text.find(start)
|
505 |
+
if start_pos == -1:
|
506 |
+
return ""
|
507 |
+
start_pos += len(start)
|
508 |
+
end_pos = text.find(end, start_pos)
|
509 |
+
if end_pos == -1:
|
510 |
+
return ""
|
511 |
+
return text[start_pos:end_pos]
|
512 |
+
except (AttributeError, TypeError):
|
513 |
+
return ""
|
514 |
+
|
515 |
+
@staticmethod
|
516 |
+
def _get_system_prompt(is_realtime):
|
517 |
+
"""
|
518 |
+
Returns the appropriate system prompt based on whether it's realtime or historical data analysis.
|
519 |
+
|
520 |
+
Args:
|
521 |
+
is_realtime (bool): Flag indicating if this is for realtime data analysis
|
522 |
+
|
523 |
+
Returns:
|
524 |
+
str: The complete system prompt for the sentiment analysis
|
525 |
+
"""
|
526 |
+
base_prompt = """
|
527 |
+
You are an assistant to a financial services firm who answers user queries on Stock Investments.
|
528 |
+
User input will have the context required by you to answer user questions.
|
529 |
+
This context will begin with the token: ###Context.
|
530 |
+
The context contains references to specific portions of a document relevant to the user query.
|
531 |
+
|
532 |
+
User questions will begin with the token: ###Question.
|
533 |
+
|
534 |
+
First, find the 'nse company symbol' of the related company in the question provided.
|
535 |
+
Your task is to perform sentiment analysis on the content part of each documents provided in the Context, which discuss a company identified by its 'nse company symbol'. The goal is to determine the overall sentiment expressed across all documents and provide an overall justification. Based on the sentiment analysis, give a recommendation on whether the company's stock should be purchased.
|
536 |
+
|
537 |
+
Step-by-Step Instructions:
|
538 |
+
1. See if the question is "NOTICKER". If so, give response and don't proceed.
|
539 |
+
2. If the company in question is not found in the context, give the corresponding response and don't proceed.
|
540 |
+
3. Read the Context: Carefully read the content parts of each document provided in the list of Documents.
|
541 |
+
4. Determine Overall Sentiment: Analyze the sentiment across all documents and categorize the overall sentiment as Positive, Negative, or Neutral.
|
542 |
+
5. Provide Overall Justification: Summarize the key points from all documents to justify the overall sentiment.
|
543 |
+
6. Stock Advice: Based on the overall sentiment and justification, provide a recommendation on whether the company's stock should be purchased.
|
544 |
+
|
545 |
+
Example Analysis:
|
546 |
+
Context:
|
547 |
+
[Document(metadata={'platform': 'Moneycontrol', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 134}, page_content="{'title': 'Asian Paints launches Neo Bharat Latex Paint to tap on booming demand', 'content': 'The company, which is the leading player in India, touts the new segment to being affordable, offering over 1000 shades for consumers.'}"), Document(metadata={'platform': 'MarketsMojo', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 128}, page_content="{'title': 'Asian Paints Ltd. Stock Performance Shows Positive Trend, Outperforms Sector by 0.9%', 'content': 'Asian Paints Ltd., a leading player in the paints industry, has seen a positive trend in its stock performance on July 10, 2024.'}"), Document(metadata={'platform': 'Business Standard', 'company': 'ASIANPAINT', 'ingestion_timestamp': '2024-10-25T17:13:42.970099', 'word_count': 138}, page_content="{'title': 'Asian Paints, Indigo Paints, Kansai gain up to 5% on falling oil prices', 'content': 'Shares of paint companies were trading higher on Wednesday, rising up to 5 per cent on the BSE, on the back of a fall in crude oil prices.'}")]
|
548 |
+
"""
|
549 |
+
|
550 |
+
if is_realtime:
|
551 |
+
response_format = """
|
552 |
+
Response Formats:
|
553 |
+
Only If the Question is 'NOTICKER':
|
554 |
+
No valid company in the query.
|
555 |
+
|
556 |
+
Else, If the context does not have relevent data for the company:
|
557 |
+
Respond "Company {Company name} {nse company symbol}({symbol}) details not found in the RealTime Data".
|
558 |
+
"""
|
559 |
+
else:
|
560 |
+
response_format = """
|
561 |
+
Response Formats:
|
562 |
+
If the Question value is "NOTICKER":
|
563 |
+
No valid company in the query.
|
564 |
+
|
565 |
+
If the context does not have relevent data for the company (Question value):
|
566 |
+
Respond "Company {Company name} {nse company symbol}({symbol}) details not found in the Historical Data".
|
567 |
+
"""
|
568 |
+
|
569 |
+
common_format = """
|
570 |
+
else, If the content parts of context has relevent data:
|
571 |
+
Overall Sentiment: [Positive/Negative/Neutral] <line break>
|
572 |
+
Overall Justification: [Detailed analysis of why the sentiment was chosen, summarizing key points from the documents] <line break>
|
573 |
+
Stock Advice: [Clear recommendation on whether to purchase the stock, based on the sentiment analysis and justification]
|
574 |
+
|
575 |
+
Please follow the steps to analyze the sentiment of each document's content; and strictly follow exact structure illustrated in above example response to provide an overall sentiment, justification and give stock purchase advice. Provide only Overall response, don't provide documentwise response or any note. Decorate the response with html/css tags.
|
576 |
+
"""
|
577 |
+
|
578 |
+
return base_prompt + response_format + common_format
|
579 |
+
|
580 |
+
|
581 |
+
def main():
|
582 |
+
adviser = StockAdviser()
|
583 |
+
|
584 |
+
|
585 |
+
# Create sidebar for filters and settings
|
586 |
+
st.logo(
|
587 |
+
"https://cdn.shopify.com/s/files/1/0153/8513/3156/files/info_omac.png?v=1595717396",
|
588 |
+
size="large"
|
589 |
+
)
|
590 |
+
|
591 |
+
with st.sidebar:
|
592 |
+
# About the Application
|
593 |
+
st.markdown("""
|
594 |
+
<div style="background-color: #2d2d2d; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(255, 255, 255, 0.1);">
|
595 |
+
<h2 style="color: #e6e6e6; text-align: Left;">About the Application</h2>
|
596 |
+
<p style="font-size: 16px; color: #cccccc; line-height: 1.6; text-align: justify;">
|
597 |
+
This application provides investment managers with daily insights into social media and news sentiment surrounding specific stocks and companies.
|
598 |
+
By analyzing posts and articles across major platforms such as <strong>Reddit</strong>, <strong>YouTube</strong>, <strong>Tumblr</strong>, <strong>Google News</strong>,
|
599 |
+
<strong>Financial Times</strong>, <strong>Bloomberg</strong>, <strong>Reuters</strong>, and <strong>Wall Street Journal</strong> (WSJ), it detects shifts
|
600 |
+
in public and media opinion that may impact stock performance.
|
601 |
+
</p>
|
602 |
+
<p style="font-size: 16px; color: #cccccc; line-height: 1.6; text-align: justify;">
|
603 |
+
Additionally, sources like <strong>Serper</strong> provide data from <strong>StockNews</strong>, <strong>Yahoo Finance</strong>, <strong>Insider Monkey</strong>,
|
604 |
+
<strong>Investor's Business Daily</strong>, and others. Using advanced AI techniques, the application generates a sentiment report that serves as a leading indicator,
|
605 |
+
helping managers make informed, timely adjustments to their positions. With daily updates and historical trend analysis, it empowers users to stay ahead in a fast-paced,
|
606 |
+
sentiment-driven market.
|
607 |
+
</p>
|
608 |
+
</div>
|
609 |
+
|
610 |
+
""", unsafe_allow_html=True)
|
611 |
+
# Sidebar Footer (Floating Footer)
|
612 |
+
st.sidebar.markdown("""
|
613 |
+
<div style="position: fixed; bottom: 5px; padding: 5px; background-color: #1f1f1f; border-radius: 5px; text-align: left;">
|
614 |
+
<p style="color: #cccccc; font-size: 14px;">
|
615 |
+
Developed by: <a href="https://www.linkedin.com/in/karthikeyen92/" target="_blank" style="color: #4DA8DA; text-decoration: none;">Karthikeyen Packirisamy</a>
|
616 |
+
</p>
|
617 |
+
</div>
|
618 |
+
""", unsafe_allow_html=True)
|
619 |
+
|
620 |
+
|
621 |
+
|
622 |
+
# Main content
|
623 |
+
cmp_tr = "NOTICKER"
|
624 |
+
st.header("Ask a question")
|
625 |
+
user_question = st.text_input("Ask a stock advice related question", key="user_question")
|
626 |
+
|
627 |
+
col1, col2 = st.columns(2)
|
628 |
+
|
629 |
+
with col1:
|
630 |
+
st.markdown("<h2 class='column-header'>Historical Analysis</h2>", unsafe_allow_html=True)
|
631 |
+
with st.container():
|
632 |
+
if user_question:
|
633 |
+
cmp_tr = adviser.process_historical_data(user_question)
|
634 |
+
|
635 |
+
with col2:
|
636 |
+
st.markdown("<h2 class='column-header'>Real-Time Analysis</h2>", unsafe_allow_html=True)
|
637 |
+
with st.container():
|
638 |
+
if user_question:
|
639 |
+
sentiment_response = adviser.process_realtime_data(cmp_tr)
|
640 |
+
|
641 |
+
if (str(cmp_tr) is not "NOTICKER"):
|
642 |
+
with st.container():
|
643 |
+
if user_question:
|
644 |
+
adviser.display_charts(cmp_tr,sentiment_response)
|
645 |
+
|
646 |
+
st.markdown("---")
|
647 |
+
st.markdown("<p style='text-align: center; color: #666;'>© 2024 EY</p>", unsafe_allow_html=True)
|
648 |
+
|
649 |
+
if __name__ == "__main__":
|
650 |
main()
|