-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizations.py
360 lines (278 loc) · 12.8 KB
/
visualizations.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.dates as mdates
from mplfinance.original_flavor import candlestick_ohlc
import logging
import plotly.express as px
import streamlit as st
from model import predict_future_prices
from logger import get_logger
logger = get_logger(__name__)
def plot_stock_price(data: pd.DataFrame, ticker: str, indicators: dict = None,
color='blue', line_style='-', title=None):
"""
Plot the stock price with optional indicators and customization.
"""
required_columns = ['Date', 'Close']
missing_columns = [col for col in required_columns if col not in data.columns]
if missing_columns:
logger.error(f"Missing columns in data for plot_stock_price: {', '.join(missing_columns)}")
raise KeyError(f"Missing columns in data: {', '.join(missing_columns)}")
logger.info(f"Plotting stock price for {ticker}.")
# Matplotlib Plot
plt.figure(figsize=(14, 7))
plt.plot(data['Date'], data['Close'], label='Close Price', color=color, linestyle=line_style)
if indicators:
for name, values in indicators.items():
plt.plot(data['Date'], values, label=name)
plt.title(title if title else f'{ticker} Stock Price')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
# Render the plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.line(data, x='Date', y='Close', title=title if title else f'{ticker} Stock Price')
if indicators:
for name, values in indicators.items():
fig.add_scatter(x=data['Date'], y=values, mode='lines', name=name)
# Render the interactive plot using Streamlit
st.plotly_chart(fig)
def plot_predictions(data: pd.DataFrame, predictions: pd.Series, ticker: str,
actual_color='blue', predicted_color='red', line_style_actual='-', line_style_predicted='--'):
"""
Plot actual vs predicted stock prices with customization.
"""
logger.info(f"Plotting actual vs predicted prices for {ticker}.")
# Matplotlib Plot
plt.figure(figsize=(14, 7))
plt.plot(data['Date'], data['Close'], label='Actual Prices', color=actual_color, linestyle=line_style_actual)
plt.plot(data['Date'], predictions, label='Predicted Prices', color=predicted_color, linestyle=line_style_predicted)
plt.title(f'{ticker} Actual vs Predicted Prices')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
# Render the plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.line(data, x='Date', y='Close', title=f'{ticker} Actual vs Predicted Prices')
fig.add_scatter(x=data['Date'], y=predictions, mode='lines', name='Predicted Prices', line=dict(color=predicted_color))
# Render the interactive plot using Streamlit
st.plotly_chart(fig)
def generate_predictions(model, test_data):
"""
Generate predictions using the model for the given test data.
"""
try:
# Extract relevant features for the model
features = test_data[['Open', 'SMA_50', 'EMA_50', 'RSI', 'MACD', 'MACD_Signal', 'Bollinger_High', 'Bollinger_Low', 'ATR', 'OBV']] # Adjust features based on your model
predictions = model.predict(features)
return predictions
except KeyError as e:
logger.error(f"Feature key error: {e}")
st.error(f"Feature key error: {e}")
except Exception as e:
logger.error(f"An error occurred during prediction: {e}")
st.error(f"An error occurred during prediction: {e}")
def plot_technical_indicators(data: pd.DataFrame, indicators: dict, model, days=10):
"""
Plot technical indicators along with the stock price and predictions.
"""
logger.info("Plotting stock price with technical indicators and predictions.")
# Ensure all indicators have the same length as the data
for name, values in indicators.items():
if len(values) != len(data):
logger.error(f"Indicator '{name}' length {len(values)} does not match data length {len(data)}.")
st.error(f"Indicator '{name}' length {len(values)} does not match data length {len(data)}.")
return
# Generate the last 30 days' dates
end_date = data['Date'].max()
start_date = end_date - pd.Timedelta(days=30)
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
# Filter data for the last 30 days
last_30_days_data = data[data['Date'].isin(date_range)]
# Prepare test data for predictions
test_data = last_30_days_data.copy()
# Generate future predictions
future_prices, _, _, _, _ = predict_future_prices(data, model, days)
if future_prices is not None:
# Generate future dates
future_dates = pd.date_range(start=end_date + pd.Timedelta(days=1), periods=days, freq='D')
# Create a DataFrame for future predictions
future_df = pd.DataFrame({
'Date': future_dates,
'Predicted_Close': future_prices
})
# Matplotlib Plot
plt.figure(figsize=(14, 7))
plt.plot(data['Date'], data['Close'], label='Close Price', color='blue')
plt.plot(future_df['Date'], future_df['Predicted_Close'], label='Predicted Price', color='orange', linestyle='--')
for name, values in indicators.items():
plt.plot(data['Date'], values, label=name)
plt.title('Stock Price with Technical Indicators and Predictions')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
# Render the plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.line(data, x='Date', y='Close', title='Stock Price with Technical Indicators and Predictions')
fig.add_scatter(x=future_df['Date'], y=future_df['Predicted_Close'], mode='lines', name='Predicted Price', line=dict(color='orange', dash='dash'))
for name, values in indicators.items():
fig.add_scatter(x=data['Date'], y=values, mode='lines', name=name)
# Render the interactive plot using Streamlit
st.plotly_chart(fig)
else:
st.error("No predictions available.")
def plot_risk_levels(data: pd.DataFrame, risk_levels: pd.Series, cmap='coolwarm'):
"""
Plot risk levels with stock prices and customization.
"""
logger.info("Plotting stock prices with risk levels.")
plt.figure(figsize=(14, 7))
plt.plot(data['Date'], data['Close'], label='Close Price', color='blue')
plt.scatter(data['Date'], data['Close'], c=risk_levels, cmap=cmap, label='Risk Levels', alpha=0.7)
plt.title('Stock Prices with Risk Levels')
plt.xlabel('Date')
plt.ylabel('Price')
plt.colorbar(label='Risk Level')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
# Render Matplotlib plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.scatter(data, x='Date', y='Close', color=risk_levels, color_continuous_scale=cmap,
title='Stock Prices with Risk Levels', labels={'color': 'Risk Level'})
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)
def plot_feature_importance(importances: pd.Series, feature_names: list):
"""
Plot feature importance for machine learning models.
"""
logger.info("Plotting feature importance.")
plt.figure(figsize=(10, 6))
sns.barplot(x=importances, y=feature_names, palette='viridis')
plt.title('Feature Importances')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.grid(True)
plt.tight_layout()
# Render Matplotlib plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.bar(x=importances, y=feature_names, orientation='h',
title='Feature Importances', labels={'x': 'Importance', 'y': 'Feature'})
fig.update_layout(yaxis={'categoryorder':'total ascending'})
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)
def plot_candlestick(data: pd.DataFrame, ticker: str):
"""
Plot candlestick chart for stock prices.
"""
required_columns = ['Date', 'Open', 'High', 'Low', 'Close']
# Check if all required columns are present
missing_columns = [col for col in required_columns if col not in data.columns]
if missing_columns:
logger.error(f"Missing columns in data for plot_candlestick: {', '.join(missing_columns)}")
raise KeyError(f"Missing columns in data: {', '.join(missing_columns)}")
logger.info(f"Plotting candlestick chart for {ticker}.")
data = data[required_columns]
data['Date'] = pd.to_datetime(data['Date'])
data['Date'] = mdates.date2num(data['Date'])
fig, ax = plt.subplots(figsize=(14, 7))
candlestick_ohlc(ax, data.values, width=0.6, colorup='green', colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.title(f'{ticker} Candlestick Chart')
plt.xlabel('Date')
plt.ylabel('Price')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
# Render Matplotlib plot using Streamlit
st.pyplot(fig)
# Plotly Plot (interactive)
fig = px.line(data, x='Date', y=['Open', 'High', 'Low', 'Close'],
title=f'{ticker} Candlestick Chart')
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)
def plot_volume(data: pd.DataFrame):
"""
Plot trading volume alongside stock price.
"""
logger.info("Plotting stock price and trading volume.")
plt.figure(figsize=(14, 7))
plt.subplot(2, 1, 1)
plt.plot(data['Date'], data['Close'], label='Close Price', color='blue')
plt.title('Stock Price and Trading Volume')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.subplot(2, 1, 2)
plt.bar(data['Date'], data['Volume'], color='grey', alpha=0.5)
plt.xlabel('Date')
plt.ylabel('Volume')
plt.tight_layout()
plt.xticks(rotation=45)
# Render Matplotlib plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.bar(data, x='Date', y='Volume', title='Trading Volume',
labels={'Volume': 'Volume', 'Date': 'Date'})
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)
def plot_moving_averages(data: pd.DataFrame, short_window: int = 20, long_window: int = 50):
"""
Plot moving averages along with the stock price.
"""
logger.info("Calculating and plotting moving averages.")
data['Short_MA'] = data['Close'].rolling(window=short_window).mean()
data['Long_MA'] = data['Close'].rolling(window=long_window).mean()
plt.figure(figsize=(14, 7))
plt.plot(data['Date'], data['Close'], label='Close Price', color='blue')
plt.plot(data['Date'], data['Short_MA'], label=f'Short {short_window}-day MA', color='orange')
plt.plot(data['Date'], data['Long_MA'], label=f'Long {long_window}-day MA', color='purple')
plt.title('Stock Price with Moving Averages')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
# Render Matplotlib plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.line(data, x='Date', y=['Close', 'Short_MA', 'Long_MA'],
title='Stock Price with Moving Averages')
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)
def plot_feature_correlations(data: pd.DataFrame):
"""
Plot correlation heatmap of features.
"""
logger.info("Plotting feature correlations heatmap.")
plt.figure(figsize=(12, 10))
correlation_matrix = data.corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Feature Correlations')
plt.tight_layout()
# Render Matplotlib plot using Streamlit
st.pyplot(plt)
# Plotly Plot (interactive)
fig = px.imshow(correlation_matrix, text_auto=True,
title='Feature Correlations', labels={'color': 'Correlation'})
# Render the interactive Plotly plot using Streamlit
st.plotly_chart(fig)