Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import numpy as np
|
|
| 7 |
import ast
|
| 8 |
from pagination import paginator
|
| 9 |
import style as cs
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
|
|
@@ -83,16 +85,17 @@ date_limit = pd.to_datetime('2021-08-12') # Set date limit for end date picker
|
|
| 83 |
|
| 84 |
|
| 85 |
# Create Tabs
|
| 86 |
-
market_analysis, news_analysis, trade_recs, chat = st.tabs(["Market Analysis", "News Analysis", "Trading Recommendations", "Gup Shup"])
|
| 87 |
|
| 88 |
with market_analysis:
|
| 89 |
st.header("Market Analysis")
|
|
|
|
| 90 |
date_container = st.container(height = 90, border=False)
|
| 91 |
# main_container.write(f"Date: {date}")
|
| 92 |
|
| 93 |
col1, col2 = date_container.columns([0.5, 0.5], gap='medium')
|
| 94 |
start_date = col1.date_input('Start Date', value=default_start_date, min_value=data['Date'].min(), max_value=date_limit)
|
| 95 |
-
end_date = col2.date_input("
|
| 96 |
start_date = pd.to_datetime(start_date)
|
| 97 |
end_date = pd.to_datetime(end_date)
|
| 98 |
data2 = data[data['Date'].between(start_date, end_date)]
|
|
@@ -245,6 +248,7 @@ with market_analysis:
|
|
| 245 |
|
| 246 |
with news_analysis:
|
| 247 |
st.header("News Analysis")
|
|
|
|
| 248 |
|
| 249 |
# Load data
|
| 250 |
data_file_path = r"Events_SameDay.csv" # Update this with your file path
|
|
@@ -364,21 +368,29 @@ with news_analysis:
|
|
| 364 |
|
| 365 |
# news = events[events['Date'].between(start_date, end_date, inclusive='both')]
|
| 366 |
|
| 367 |
-
|
|
|
|
|
|
|
| 368 |
cols = ['Raw_Headline', 'Bold_KW', 'Feature', 'Raw_News', 'Sources', 'Urls']
|
| 369 |
for col in cols:
|
| 370 |
-
|
|
|
|
| 371 |
# Extract only the date part
|
| 372 |
-
|
|
|
|
| 373 |
# Sort DataFrame based on the 'Date' column in descending order
|
| 374 |
-
|
|
|
|
| 375 |
|
| 376 |
# Reset index to reflect the new order
|
| 377 |
-
|
|
|
|
| 378 |
st.subheader("News Events")
|
| 379 |
# st.dataframe(news, hide_index=True)
|
| 380 |
# st.table(news)
|
| 381 |
-
|
|
|
|
|
|
|
| 382 |
dates = np.sort(dates)
|
| 383 |
# Reverse the array to have the latest date at index 0
|
| 384 |
dates = dates[::-1]
|
|
@@ -388,33 +400,61 @@ with news_analysis:
|
|
| 388 |
for i, date in paginator("Select Page Number", dates, items_per_page=items_per_page, on_sidebar=False, ukey='news_pages'):
|
| 389 |
st.write(f'<span style="font-size: large;"><b>Date:</b> <u>{date}</u></span>', unsafe_allow_html=True)
|
| 390 |
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
with trade_recs:
|
| 420 |
st.header("Trading Recommendations")
|
|
@@ -554,5 +594,162 @@ with trade_recs:
|
|
| 554 |
|
| 555 |
# st.write(trade_recs)
|
| 556 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
with chat:
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import ast
|
| 8 |
from pagination import paginator
|
| 9 |
import style as cs
|
| 10 |
+
import random
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
|
| 14 |
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# Create Tabs
|
| 88 |
+
market_analysis, news_analysis, trade_recs, final_recs, chat = st.tabs(["Market Analysis", "News Analysis", "Trading Recommendations", "Final Recommendations","Gup Shup"])
|
| 89 |
|
| 90 |
with market_analysis:
|
| 91 |
st.header("Market Analysis")
|
| 92 |
+
st.write("This module provides market analysis for the following day based on the selected current date.")
|
| 93 |
date_container = st.container(height = 90, border=False)
|
| 94 |
# main_container.write(f"Date: {date}")
|
| 95 |
|
| 96 |
col1, col2 = date_container.columns([0.5, 0.5], gap='medium')
|
| 97 |
start_date = col1.date_input('Start Date', value=default_start_date, min_value=data['Date'].min(), max_value=date_limit)
|
| 98 |
+
end_date = col2.date_input("Current Date", value=date_limit, min_value=data['Date'].min(), max_value=date_limit)
|
| 99 |
start_date = pd.to_datetime(start_date)
|
| 100 |
end_date = pd.to_datetime(end_date)
|
| 101 |
data2 = data[data['Date'].between(start_date, end_date)]
|
|
|
|
| 248 |
|
| 249 |
with news_analysis:
|
| 250 |
st.header("News Analysis")
|
| 251 |
+
st.write("This module provides news impact for the following day based on the selected current date.")
|
| 252 |
|
| 253 |
# Load data
|
| 254 |
data_file_path = r"Events_SameDay.csv" # Update this with your file path
|
|
|
|
| 368 |
|
| 369 |
# news = events[events['Date'].between(start_date, end_date, inclusive='both')]
|
| 370 |
|
| 371 |
+
pos_news = plot_sub_pos[['Date', 'Raw_Headline', 'Bold_KW', 'Feature', 'Raw_News', 'Sources', 'Urls']]
|
| 372 |
+
neg_news = plot_sub_neg[['Date', 'Raw_Headline', 'Bold_KW', 'Feature', 'Raw_News', 'Sources', 'Urls']]
|
| 373 |
+
|
| 374 |
cols = ['Raw_Headline', 'Bold_KW', 'Feature', 'Raw_News', 'Sources', 'Urls']
|
| 375 |
for col in cols:
|
| 376 |
+
pos_news[col] = pos_news[col].apply(convert_str_to_list)
|
| 377 |
+
neg_news[col] = neg_news[col].apply(convert_str_to_list)
|
| 378 |
# Extract only the date part
|
| 379 |
+
neg_news['Date'] = neg_news['Date'].dt.date
|
| 380 |
+
pos_news['Date'] = pos_news['Date'].dt.date
|
| 381 |
# Sort DataFrame based on the 'Date' column in descending order
|
| 382 |
+
neg_news = neg_news.sort_values(by='Date', ascending=False)
|
| 383 |
+
pos_news = pos_news.sort_values(by='Date', ascending=False)
|
| 384 |
|
| 385 |
# Reset index to reflect the new order
|
| 386 |
+
pos_news.reset_index(drop=True, inplace=True)
|
| 387 |
+
neg_news.reset_index(drop=True, inplace=True)
|
| 388 |
st.subheader("News Events")
|
| 389 |
# st.dataframe(news, hide_index=True)
|
| 390 |
# st.table(news)
|
| 391 |
+
dates_neg = list(neg_news['Date'].unique())
|
| 392 |
+
dates_pos = list(pos_news['Date'].unique())
|
| 393 |
+
dates = dates_pos + dates_neg
|
| 394 |
dates = np.sort(dates)
|
| 395 |
# Reverse the array to have the latest date at index 0
|
| 396 |
dates = dates[::-1]
|
|
|
|
| 400 |
for i, date in paginator("Select Page Number", dates, items_per_page=items_per_page, on_sidebar=False, ukey='news_pages'):
|
| 401 |
st.write(f'<span style="font-size: large;"><b>Date:</b> <u>{date}</u></span>', unsafe_allow_html=True)
|
| 402 |
|
| 403 |
+
if date in dates_pos:
|
| 404 |
+
filtered_news = pos_news[pos_news['Date'] == date]
|
| 405 |
+
print(filtered_news.shape)
|
| 406 |
+
features = filtered_news['Feature'].sum()
|
| 407 |
+
headlines = filtered_news['Raw_Headline'].sum()
|
| 408 |
+
news_list = filtered_news['Raw_News'].sum()
|
| 409 |
+
sources = filtered_news['Sources'].sum()
|
| 410 |
+
urls = filtered_news['Urls'].sum()
|
| 411 |
+
|
| 412 |
+
main_container = st.container(height = 250, border=True)
|
| 413 |
+
# main_container.write(f"Date: {date}")
|
| 414 |
+
|
| 415 |
+
col1, col2 = main_container.columns([0.7, 0.3], gap='medium')
|
| 416 |
+
|
| 417 |
+
# Merge lists of headlines into a single list
|
| 418 |
+
for index, headline in enumerate(headlines):
|
| 419 |
+
col1.page_link(urls[index], label=f"**:blue[{headline}]**")
|
| 420 |
+
# col1.write(f"<span style='font-size: large;'><b><u>{headline}</u></b></span>", unsafe_allow_html=True)
|
| 421 |
+
col1.write(f"<span style='font-size: small;'>By {sources[index]}</span><br>", unsafe_allow_html=True)
|
| 422 |
+
with col1:
|
| 423 |
+
with st.expander("Show Full Article"):
|
| 424 |
+
st.write(news_list[index])
|
| 425 |
+
|
| 426 |
+
with col2:
|
| 427 |
+
# st.divider()
|
| 428 |
+
with st.expander("Oil Sector Features"):
|
| 429 |
+
st.write(set(features))
|
| 430 |
|
| 431 |
+
if date in dates_neg:
|
| 432 |
+
filtered_news = neg_news[neg_news['Date'] == date]
|
| 433 |
+
print(filtered_news.shape)
|
| 434 |
+
features = filtered_news['Feature'].sum()
|
| 435 |
+
headlines = filtered_news['Raw_Headline'].sum()
|
| 436 |
+
news_list = filtered_news['Raw_News'].sum()
|
| 437 |
+
sources = filtered_news['Sources'].sum()
|
| 438 |
+
urls = filtered_news['Urls'].sum()
|
| 439 |
+
|
| 440 |
+
main_container = st.container(height = 250, border=True)
|
| 441 |
+
# main_container.write(f"Date: {date}")
|
| 442 |
+
|
| 443 |
+
col1, col2 = main_container.columns([0.7, 0.3], gap='medium')
|
| 444 |
+
|
| 445 |
+
# Merge lists of headlines into a single list
|
| 446 |
+
for index, headline in enumerate(headlines):
|
| 447 |
+
col1.page_link(urls[index], label=f"**:blue[{headline}]**")
|
| 448 |
+
# col1.write(f"<span style='font-size: large;'><b><u>{headline}</u></b></span>", unsafe_allow_html=True)
|
| 449 |
+
col1.write(f"<span style='font-size: small;'>By {sources[index]}</span><br>", unsafe_allow_html=True)
|
| 450 |
+
with col1:
|
| 451 |
+
with st.expander("Show Full Article"):
|
| 452 |
+
st.write(news_list[index])
|
| 453 |
+
|
| 454 |
+
with col2:
|
| 455 |
+
# st.divider()
|
| 456 |
+
with st.expander("Oil Sector Features"):
|
| 457 |
+
st.write(set(features))
|
| 458 |
|
| 459 |
with trade_recs:
|
| 460 |
st.header("Trading Recommendations")
|
|
|
|
| 594 |
|
| 595 |
# st.write(trade_recs)
|
| 596 |
|
| 597 |
+
with final_recs:
|
| 598 |
+
st.header("GenAI Recommendations")
|
| 599 |
+
st.write("This module provides trading recommendation for the following day based on the selected current date.")
|
| 600 |
+
|
| 601 |
+
# Create the line trace for stock prices
|
| 602 |
+
line_stock = go.Scatter(x=events['Date'], y=events['Price'], mode='lines', name='OGDCL Close Price',
|
| 603 |
+
line=dict(dash='solid',color=cs.close_line_color, width=2), # color='royalblue'
|
| 604 |
+
text=events['EMA9_Signal'],
|
| 605 |
+
hovertext=events['EMA55_Signal'],
|
| 606 |
+
meta = events["RSI_Signals"],
|
| 607 |
+
customdata=events['MACD_Signals'],
|
| 608 |
+
hovertemplate='%{x}<br>Close: %{y}<br> EMA9_Signal: %{text}<br>EMA55_Signal: %{hovertext}<br> RSI_Signal: %{meta}<br>MACD_Signal: %{customdata}<br>',
|
| 609 |
+
# hoverlabel=dict(font=dict(color=events
|
| 610 |
+
# ['FeatureSentiment'].apply(lambda x: 'red' if x == 'Negative' else 'blue' if x == 'Neutral' else 'green'))), # Customize the line style, color, and width
|
| 611 |
+
)
|
| 612 |
+
# Create dummy traces for the legend
|
| 613 |
+
dummy_positive = go.Scatter(x=[None], y=[None], mode='lines', name='Positive Impacts',
|
| 614 |
+
marker=dict(color=cs.pos_impacts_color, size=15), showlegend=True,
|
| 615 |
+
# visible='legendonly'
|
| 616 |
+
)
|
| 617 |
+
dummy_negative = go.Scatter(x=[None], y=[None], mode='lines', name='Negative Impacts',
|
| 618 |
+
marker=dict(color=cs.neg_impacts_color, size=15), showlegend=True,
|
| 619 |
+
# visible='legendonly'
|
| 620 |
+
)
|
| 621 |
+
|
| 622 |
+
fontsize = 12
|
| 623 |
+
annotations = []
|
| 624 |
+
# Create annotations for the Positive points
|
| 625 |
+
for index, row in plot_sub_pos.iterrows():
|
| 626 |
+
annotation1 = dict(x=row['Date'], y=row['Price'], text = row['Positive_Impacts'],
|
| 627 |
+
showarrow=True, arrowhead=0, # arrowcolor='black',
|
| 628 |
+
ax=10, ay=-20, # Dynamic offset
|
| 629 |
+
font=dict(size=fontsize, color=cs.pos_impacts_color),
|
| 630 |
+
)
|
| 631 |
+
annotations.append(annotation1)
|
| 632 |
+
|
| 633 |
+
# Create annotations for the Negative points
|
| 634 |
+
for index, row in plot_sub_neg.iterrows():
|
| 635 |
+
annotation2 = dict(x=row['Date'], y=row['Price'], text = row['Negative_Impacts'],
|
| 636 |
+
showarrow=True, arrowhead=0, arrowcolor=cs.close_line_color,
|
| 637 |
+
ax=10, ay=-20, # Dynamic offset
|
| 638 |
+
font=dict(size=fontsize, color=cs.neg_impacts_color),
|
| 639 |
+
)
|
| 640 |
+
annotations.append(annotation2)
|
| 641 |
+
|
| 642 |
+
# Create the layout
|
| 643 |
+
title = 'OGDCL Close Price w.r.t. News Impacts'
|
| 644 |
+
layout = go.Layout(
|
| 645 |
+
title=title,
|
| 646 |
+
xaxis=dict(
|
| 647 |
+
title='Date',
|
| 648 |
+
tickformat='%b %d, %Y',
|
| 649 |
+
# gridcolor='lightgray',
|
| 650 |
+
range=[start_date, end_date],
|
| 651 |
+
# tickvals=list(range(dateA, dateB, 3)),
|
| 652 |
+
),
|
| 653 |
+
yaxis=dict(
|
| 654 |
+
title='OGDCL Close Price',
|
| 655 |
+
# gridcolor='lightgray',
|
| 656 |
+
range=[90, 120],
|
| 657 |
+
tickvals=list(range(90, 120, 5)),
|
| 658 |
+
),
|
| 659 |
+
# plot_bgcolor='rgba(245, 245, 245, 0.8)',
|
| 660 |
+
# paper_bgcolor='white',
|
| 661 |
+
# hoverlabel=dict(
|
| 662 |
+
# bgcolor='white',
|
| 663 |
+
# font=dict(color='black'),
|
| 664 |
+
# ),
|
| 665 |
+
annotations=annotations,
|
| 666 |
+
)
|
| 667 |
+
|
| 668 |
+
# Add all traces to the figure
|
| 669 |
+
figure = go.Figure(data=[line_stock, dummy_negative, dummy_positive], layout=layout)
|
| 670 |
+
# figure.update_traces(mode="markers+lines")
|
| 671 |
+
figure.update_layout(
|
| 672 |
+
title={
|
| 673 |
+
'text': title,
|
| 674 |
+
'x': 0.5,
|
| 675 |
+
'y': 0.95,
|
| 676 |
+
'xanchor': 'center',
|
| 677 |
+
'yanchor': 'top',
|
| 678 |
+
'font': dict(size=12),
|
| 679 |
+
},
|
| 680 |
+
hovermode='closest',
|
| 681 |
+
margin=dict(l=40, r=40, t=80, b=40),
|
| 682 |
+
)
|
| 683 |
+
figure.update_xaxes(
|
| 684 |
+
rangeslider_visible=True,
|
| 685 |
+
rangeselector=dict(
|
| 686 |
+
buttons=list([
|
| 687 |
+
dict(count=1, label="1m", step="month", stepmode="backward"),
|
| 688 |
+
dict(count=6, label="6m", step="month", stepmode="backward"),
|
| 689 |
+
dict(count=1, label="YTD", step="year", stepmode="todate"),
|
| 690 |
+
dict(count=1, label="1y", step="year", stepmode="backward"),
|
| 691 |
+
dict(step="all")
|
| 692 |
+
])
|
| 693 |
+
)
|
| 694 |
+
)
|
| 695 |
+
st.plotly_chart(figure)
|
| 696 |
+
if 'Trading_Recommendations\r' in events.columns:
|
| 697 |
+
events.rename(columns={'Trading_Recommendations\r': 'Trading_Recommendations'}, inplace=True)
|
| 698 |
+
|
| 699 |
+
print(events.columns)
|
| 700 |
+
trade_recs = events[['Date', 'Trading_Recommendations']]
|
| 701 |
+
# Extract date component from the datetime column
|
| 702 |
+
trade_recs['Date'] = trade_recs['Date'].dt.date
|
| 703 |
+
# Convert back to Dictionaries from strings
|
| 704 |
+
trade_recs['Trading_Recommendations'] = trade_recs['Trading_Recommendations'].apply(convert_str_to_list)
|
| 705 |
+
dates = list(trade_recs['Date'].unique())
|
| 706 |
+
dates = np.sort(dates)
|
| 707 |
+
# Reverse the array to have the latest date at index 0
|
| 708 |
+
dates = dates[::-1]
|
| 709 |
+
num_dates = len(dates)
|
| 710 |
+
genrec_container = st.container(border=False)
|
| 711 |
+
role = genrec_container.radio(
|
| 712 |
+
"Get your recommendation summary as:",
|
| 713 |
+
["Trader", "Equity Analyst"], horizontal=True)
|
| 714 |
+
|
| 715 |
+
genrec = 'Recommendation for following day will go here.'
|
| 716 |
+
genrec_container.write(genrec)
|
| 717 |
with chat:
|
| 718 |
+
# Streamed response emulator
|
| 719 |
+
def response_generator():
|
| 720 |
+
response = random.choice(
|
| 721 |
+
[
|
| 722 |
+
"Hello there! How can I assist you today?",
|
| 723 |
+
"Hi, human! Is there anything I can help you with?",
|
| 724 |
+
"Do you need help?",
|
| 725 |
+
]
|
| 726 |
+
)
|
| 727 |
+
for word in response.split():
|
| 728 |
+
yield word + " "
|
| 729 |
+
time.sleep(0.05)
|
| 730 |
+
|
| 731 |
+
|
| 732 |
+
st.title("Simple chat")
|
| 733 |
+
|
| 734 |
+
# Initialize chat history
|
| 735 |
+
if "messages" not in st.session_state:
|
| 736 |
+
st.session_state.messages = []
|
| 737 |
+
|
| 738 |
+
# Display chat messages from history on app rerun
|
| 739 |
+
for message in st.session_state.messages:
|
| 740 |
+
with st.chat_message(message["role"]):
|
| 741 |
+
st.markdown(message["content"])
|
| 742 |
+
|
| 743 |
+
# Accept user input
|
| 744 |
+
if prompt := st.chat_input("What is up?"):
|
| 745 |
+
# Add user message to chat history
|
| 746 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 747 |
+
# Display user message in chat message container
|
| 748 |
+
with st.chat_message("user"):
|
| 749 |
+
st.markdown(prompt)
|
| 750 |
+
|
| 751 |
+
# Display assistant response in chat message container
|
| 752 |
+
with st.chat_message("assistant"):
|
| 753 |
+
response = st.write_stream(response_generator())
|
| 754 |
+
# Add assistant response to chat history
|
| 755 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|