-
Notifications
You must be signed in to change notification settings - Fork 0
/
economic_sentiment_app.py
308 lines (265 loc) · 10.5 KB
/
economic_sentiment_app.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.models import load_model
import datetime
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
from datetime import datetime
import yfinance as yf
import pandas as pd
from scraping_scripts import fx_calendar, fetch_currencies, fetch_stocks, fetch_commodities, fetch_bonds, fetch_crypto, fetch_earnings
from useful_functions import prepare_timeseries, predict_forward_window, upload_data
import tensorflow as tf
import time
import tensorflow_hub as hub
# Welcome to Econ Dashboard (Beta)
title_html = """
<h1 style="color:black; font-size:36px;">Welcome to Econ Dashboard <span style="font-size:14px;">BETA</span></h1>
"""
st.sidebar.markdown(title_html, unsafe_allow_html=True)
# About Econ-Dashboard
sidebar_text_style = """
font-size: 16px;
line-height: 1.5;
text-align: justify;
"""
st.sidebar.markdown(
"""
<div style="{}">
<h3>About Econ Dashboard</h3>
<ul>
<li> A centralized dashboard for screening and downloading Stock/ETF/FX/Economic Data, as well as viewing Forex, Metal, Energy, and Crypto Calendars.</li>
</ul>
""".format(sidebar_text_style),
unsafe_allow_html=True
)
# Social Hubs
st.sidebar.markdown(
"""
<div stlye="{}">
<h3> Follow My Social Hubs For More Content </h3>
<ul>
<li><a href="https://medium.com/@bauglir">Medium</a></li>
<li><a href="https://www.kaggle.com/dfavenfre">Kaggle</a></li>
<li><a href="https://github.com/dfavenfre">GitHub</a></li>
<li><a href="https://www.linkedin.com/in/tolga-%C5%9Fakar-575b86136">LinkedIn</a></li>
</ul>
""", unsafe_allow_html=True
)
### Stock Screener
st.title("Stock Sceener")
st.write(
"""
Stock Screener Is Equipped With YahooFinanceAPI. You Can Directly Use Any Ticker/Symbol From https://finance.yahoo.com. You Can Download The Data After Running The Screener
"""
)
# Date and Interval
selected_date = st.date_input("Select A Date",
value=datetime.today().date(),
min_value=datetime(2000, 1, 1).date(),
max_value=datetime.today().date())
selected_timeframe = st.selectbox("Select A Time Frame",
["5m","15m","1h","1d"])
def convert_df(df):
return df.to_csv().encode('utf-8')
if 'ticker_df' not in st.session_state:
st.session_state.ticker_df = None
# Display Ticker/Symbol
with st.form(key="Sceener"):
name = st.text_input(label="Write Ticker/Symbol")
submit = st.form_submit_button(label="Process The Screener")
if submit:
tickerData = yf.Ticker(name)
ticker_df = tickerData.history(interval=selected_timeframe, start=selected_date)
st.session_state.ticker_df = ticker_df
if st.session_state.ticker_df is not None:
ticker_df = st.session_state.ticker_df
# display sp500 close and volume data
st.write(str(name) + " " + selected_timeframe + " Close Data")
fig_close = go.Figure(data=[go.Candlestick(x=ticker_df.index,
open=ticker_df.Open,
high=ticker_df.High,
low=ticker_df.Low,
close=ticker_df.Close)])
st.plotly_chart(fig_close)
csv = convert_df(ticker_df)
st.download_button(
label="Download Ticker Data",
data=csv,
file_name='ticker_dataframe.csv',
mime='text/csv',
)
### Economic Calendar
st.title("Data Provider")
st.write(
"""
Access a professional-grade calendar featuring daily updates on Economic Calendar, Forex, Stocks, Commodities, Bonds, and Crypto and Earnings, including comprehensive news, actual, forecast, and previous announcements.
"""
)
data_option = st.selectbox("Select Data", ["Forex Calendar","FX Market","Stock Market", "Commodities","Bonds","Crypto","Earnings"])
if data_option == "Forex Calendar":
if st.button("Get Data"):
calendar_data = fx_calendar()
st.dataframe(calendar_data, width=800)
csv = convert_df(calendar_data)
st.download_button(
label="Download Calendar data",
data=csv,
file_name='fx_calendar.csv',
mime='text/csv',
)
if data_option == "FX Market":
if st.button("Get Data"):
currency_data = fetch_currencies()
st.dataframe(currency_data, width=800)
csv = convert_df(currency_data)
st.download_button(
label="Download currency data",
data=csv,
file_name='currency.csv',
mime='text/csv',
)
if data_option == "Stock Market":
if st.button("Get Data"):
stocks_data = fetch_stocks()
st.dataframe(stocks_data, width=800)
csv = convert_df(stocks_data)
st.download_button(
label="Download stocks data",
data=csv,
file_name='stocks.csv',
mime='text/csv',
)
if data_option == "Commodities":
if st.button("Get Data"):
commodity_data = fetch_commodities()
st.dataframe(commodity_data, width=800)
csv = convert_df(commodity_data)
st.download_button(
label="Download commodity data",
data=csv,
file_name='commodities.csv',
mime='text/csv',
)
if data_option == "Bonds":
if st.button("Get Data"):
bonds_data = fetch_bonds()
st.dataframe(bonds_data, width=800)
csv = convert_df(bonds_data)
st.download_button(
label="Download bonds data",
data=csv,
file_name='bonds.csv',
mime='text/csv',
)
if data_option == "Crypto":
if st.button("Get Data"):
crypto_data = fetch_crypto()
st.dataframe(crypto_data, width=800)
csv = convert_df(crypto_data)
st.download_button(
label="Download cryptocurrency data",
data=csv,
file_name='crypto.csv',
mime='text/csv',
)
if data_option == "Earnings":
if st.button("Get Data"):
earnings_data = fetch_earnings()
st.dataframe(earnings_data, width=800)
csv = convert_df(earnings_data)
st.download_button(
label="Download earnings data",
data=csv,
file_name='earnings.csv',
mime='text/csv',
)
### Sentiment Analysis
st.title("Sentiment Analysis")
import joblib
import tensorflow as tf
import tensorflow_hub as hub
# Define the custom layer
class USEEncoderLayer(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(USEEncoderLayer, self).__init__(**kwargs)
self.use_layer = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4",
input_shape=[], # check the important notes
dtype=tf.string,
trainable=False,
name="USE_encoder")
def call(self, inputs, **kwargs):
return self.use_layer(inputs)
# Register the custom layer
custom_objects = {"USEEncoderLayer": USEEncoderLayer, "KerasLayer": hub.KerasLayer}
# Load the model with custom layer
with tf.keras.utils.custom_object_scope(custom_objects):
best_use_model = tf.keras.models.load_model(r"DOWNLOAD SENTIMENT CLASSIFIER MODEL FROM HUGGINGFACE")
import numpy as np
# Function to make prediction on new text
def predict_sentiment(text):
# Make prediction
prediction = tf.squeeze(best_use_model.predict([text]))
return prediction
def get_sentiment_label(pred):
sentiment_label = ["Negative", "Neutral", "Positive"]
max_index = np.argmax(pred.numpy())
return sentiment_label[max_index]
text_input = st.text_area("Enter the text:", value='')
submit_button = st.button("Predict")
if submit_button and text_input:
# Make prediction
prediction = predict_sentiment(text_input)
sentiment_label = get_sentiment_label(prediction)
confidence = np.max(prediction) * 100
# Output the result
output = f"{sentiment_label.capitalize()} : [Confidence: {confidence:.2f}%]"
# Display the result
st.write('Prediction Results:')
st.write(f'Sentiment Label: {sentiment_label.capitalize()}')
st.write(f'Confidence: {confidence:.2f}%')
st.write('Prediction Probabilities:')
sentiment_labels = ["Negative", "Neutral", "Positive"]
for class_idx, prob in enumerate(prediction):
st.write(f'{sentiment_labels[class_idx]}: {prob:.4f}')
# Time-series Forecast
st.title("Time-Series Forecasting")
st.write("""
Forecasting model is a fine-tuned LSTM model that is trained on 7-days look-back period to forecast 1-day forward window value. The model inputs are; last available Volume and 7 consecutive daily close data.
You have option to either upload your own data, or write the required data inputs on your own. The time-series feature is armed with two models that are specifically trained for both Large and Small Cap Stocks. You May Select The Model For Your Stock
"""
)
# Upload File
uploaded_file = st.file_uploader("Choose a file")
# Model Selection
select_cap = st.selectbox("Please Select A Market Capitalization Size", ("Large Cap", "Small Cap"))
if select_cap == "Large Cap":
large_cap_model = tf.keras.models.load_model(r"DOWNLOAD MODEL FROM MODEL FOLDER")
load_message = st.empty()
load_message.text("Model Loaded Successfuly")
time.sleep(1)
load_message.empty()
elif select_cap == "Small Cap":
low_cap_model = tf.keras.models.load_model(r"DOWNLOAD MODEL FROM MODEL FOLDER")
load_message = st.empty()
load_message.text("Model Loaded Successfuly")
time.sleep(1)
load_message.empty()
# Data Pre-Processing
submit = st.button(label="Initialize the forecast")
if submit:
if select_cap == "Large Cap":
dataframe = pd.read_csv(uploaded_file)
data = prepare_timeseries(dataframe, "Close", 6)
predict_forward_window(data, large_cap_model)
elif select_cap == "Small Cap":
dataframe = pd.read_csv(uploaded_file)
data = prepare_timeseries(dataframe, "Close", 6)
predict_forward_window(data, low_cap_model)