markytools commited on
Commit
6d4e8d7
·
1 Parent(s): 742aa68

replaced plotly with matplotlib

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. app.py +71 -110
  3. requirements.txt +2 -1
.gitignore CHANGED
@@ -1,3 +1,3 @@
1
  __pycache__/
2
  app_local.py
3
- app_localBackup.py
 
1
  __pycache__/
2
  app_local.py
3
+ app_localBackup*.py
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import streamlit as st
2
  import yfinance as yf
3
  import pandas as pd
4
- import plotly.graph_objs as go
5
- import numpy as np
6
- from plotly.subplots import make_subplots
7
  import os
8
  from datetime import date, timedelta
9
 
@@ -217,8 +217,8 @@ else:
217
  unsafe_allow_html=True,
218
  )
219
 
220
- price_tab, indicators_tab, ai_tab = st.tabs(["Price Action", "Technical Indicators", "AI Deep Dive"])
221
-
222
  with price_tab:
223
  st.subheader("Stock Price Chart")
224
  st.caption("Candlestick price action with volume on a shared timeline for quick at-a-glance context.")
@@ -227,111 +227,72 @@ else:
227
  st.write("Data sample:", df.head(3))
228
  st.write("dtypes:", df.dtypes)
229
 
230
- fig = make_subplots(specs=[[{"secondary_y": True}]])
231
- fig.add_trace(
232
- go.Candlestick(
233
- x=df['Date'],
234
- open=df['Open'],
235
- high=df['High'],
236
- low=df['Low'],
237
- close=close_series,
238
- name="Price",
239
- ),
240
- secondary_y=True,
241
- )
242
- fig.add_trace(
243
- go.Bar(
244
- x=df['Date'],
245
- y=volume_series,
246
- name="Volume",
247
- marker_color="rgba(109, 214, 255, 0.55)",
248
- ),
249
- secondary_y=False,
250
- )
251
- fig.update_layout(
252
- template="plotly_dark",
253
- plot_bgcolor="rgba(12,19,32,0.7)",
254
- paper_bgcolor="rgba(12,19,32,0.7)",
255
- legend_orientation="h",
256
- legend_yanchor="bottom",
257
- legend_y=1.02,
258
- legend_x=0,
259
- margin=dict(t=50, l=10, r=10, b=20),
260
- )
261
- fig.update_yaxes(showgrid=False, secondary_y=True)
262
- fig.update_yaxes(gridcolor="rgba(255,255,255,0.08)", secondary_y=False, title_text="Volume")
263
- fig.update_xaxes(showgrid=False)
264
- st.plotly_chart(fig, use_container_width=True)
265
-
266
- with indicators_tab:
267
- st.subheader("Moving Averages")
268
- st.caption("Compare recent closes against short and intermediate trend lines.")
269
- df['SMA_20'] = close_series.rolling(window=20).mean()
270
- df['SMA_50'] = close_series.rolling(window=50).mean()
271
- fig = go.Figure()
272
- fig.add_trace(go.Scatter(x=df['Date'], y=close_series, mode='lines', name='Close Price', line=dict(color="#7cf0c6")))
273
- fig.add_trace(go.Scatter(x=df['Date'], y=df['SMA_20'], mode='lines', name='20-Day SMA', line=dict(color="#6dd6ff")))
274
- fig.add_trace(go.Scatter(x=df['Date'], y=df['SMA_50'], mode='lines', name='50-Day SMA', line=dict(color="#b0b8ff")))
275
- fig.update_layout(
276
- title="Moving Averages",
277
- xaxis_title="Date",
278
- yaxis_title="Price (USD)",
279
- template="plotly_dark",
280
- plot_bgcolor="rgba(12,19,32,0.7)",
281
- paper_bgcolor="rgba(12,19,32,0.7)",
282
- margin=dict(t=50, l=10, r=10, b=20),
283
- )
284
- fig.update_xaxes(showgrid=False)
285
- fig.update_yaxes(gridcolor="rgba(255,255,255,0.08)")
286
- st.plotly_chart(fig, use_container_width=True)
287
-
288
- st.subheader("Relative Strength Index (RSI)")
289
- st.caption("Momentum oscillator highlighting overbought/oversold zones.")
290
- window_length = 14
291
-
292
- delta = close_series.diff()
293
- gain = delta.where(delta > 0, 0)
294
- loss = -delta.where(delta < 0, 0)
295
-
296
- avg_gain = gain.rolling(window=window_length, min_periods=1).mean()
297
- avg_loss = loss.rolling(window=window_length, min_periods=1).mean()
298
-
299
- rs = avg_gain / avg_loss
300
- df['RSI'] = 100 - (100 / (1 + rs))
301
-
302
- fig = go.Figure()
303
- fig.add_trace(go.Scatter(x=df['Date'], y=df['RSI'], mode='lines', name='RSI', line=dict(color="#6dd6ff")))
304
- fig.add_hline(y=70, line_dash="dash", line_color="#ff9b9b", annotation_text="Overbought")
305
- fig.add_hline(y=30, line_dash="dash", line_color="#7cf0c6", annotation_text="Oversold")
306
- fig.update_layout(
307
- title="RSI Indicator",
308
- xaxis_title="Date",
309
- yaxis_title="RSI",
310
- template="plotly_dark",
311
- plot_bgcolor="rgba(12,19,32,0.7)",
312
- paper_bgcolor="rgba(12,19,32,0.7)",
313
- margin=dict(t=50, l=10, r=10, b=20),
314
- )
315
- fig.update_xaxes(showgrid=False)
316
- fig.update_yaxes(gridcolor="rgba(255,255,255,0.08)")
317
- st.plotly_chart(fig, use_container_width=True)
318
-
319
- st.subheader("Volume Analysis")
320
- st.caption("Volume bars styled to match the rest of the dashboard.")
321
- fig = go.Figure()
322
- fig.add_trace(go.Bar(x=df['Date'], y=volume_series, name='Volume', marker_color="rgba(109, 214, 255, 0.55)"))
323
- fig.update_layout(
324
- title="Volume Analysis",
325
- xaxis_title="Date",
326
- yaxis_title="Volume",
327
- template="plotly_dark",
328
- plot_bgcolor="rgba(12,19,32,0.7)",
329
- paper_bgcolor="rgba(12,19,32,0.7)",
330
- margin=dict(t=50, l=10, r=10, b=20),
331
- )
332
- fig.update_xaxes(showgrid=False)
333
- fig.update_yaxes(gridcolor="rgba(255,255,255,0.08)")
334
- st.plotly_chart(fig, use_container_width=True)
335
 
336
  with ai_tab:
337
  st.subheader("In-depth Analysis")
 
1
  import streamlit as st
2
  import yfinance as yf
3
  import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ import mplfinance as mpf
6
+ import numpy as np
7
  import os
8
  from datetime import date, timedelta
9
 
 
217
  unsafe_allow_html=True,
218
  )
219
 
220
+ price_tab, indicators_tab, ai_tab = st.tabs(["Price Action", "Technical Indicators", "AI Deep Dive"])
221
+
222
  with price_tab:
223
  st.subheader("Stock Price Chart")
224
  st.caption("Candlestick price action with volume on a shared timeline for quick at-a-glance context.")
 
227
  st.write("Data sample:", df.head(3))
228
  st.write("dtypes:", df.dtypes)
229
 
230
+ df_plot = df.set_index("Date")[["Open", "High", "Low", "Close", "Volume"]]
231
+ market_colors = mpf.make_marketcolors(up="#7cf0c6", down="#ff9b9b", edge="inherit", wick="inherit", volume="in")
232
+ style = mpf.make_mpf_style(base_mpf_style="nightclouds", marketcolors=market_colors, facecolor="#0c1320", edgecolor="#0c1320", gridcolor="#1b2a45")
233
+ fig, _ = mpf.plot(
234
+ df_plot,
235
+ type="candle",
236
+ volume=True,
237
+ style=style,
238
+ returnfig=True,
239
+ figsize=(10, 6),
240
+ tight_layout=True,
241
+ update_width_config=dict(candle_linewidth=0.8, candle_width=0.6),
242
+ )
243
+ st.pyplot(fig, clear_figure=True)
244
+ plt.close(fig)
245
+
246
+ with indicators_tab:
247
+ st.subheader("Moving Averages")
248
+ st.caption("Compare recent closes against short and intermediate trend lines.")
249
+ df['SMA_20'] = close_series.rolling(window=20).mean()
250
+ df['SMA_50'] = close_series.rolling(window=50).mean()
251
+ fig, ax = plt.subplots(figsize=(10, 4))
252
+ ax.plot(df['Date'], close_series, label='Close Price', color="#7cf0c6", linewidth=1.4)
253
+ ax.plot(df['Date'], df['SMA_20'], label='20-Day SMA', color="#6dd6ff", linewidth=1.2)
254
+ ax.plot(df['Date'], df['SMA_50'], label='50-Day SMA', color="#b0b8ff", linewidth=1.2)
255
+ ax.set_ylabel("Price (USD)")
256
+ ax.grid(alpha=0.2)
257
+ ax.legend()
258
+ fig.tight_layout()
259
+ st.pyplot(fig, clear_figure=True)
260
+ plt.close(fig)
261
+
262
+ st.subheader("Relative Strength Index (RSI)")
263
+ st.caption("Momentum oscillator highlighting overbought/oversold zones.")
264
+ window_length = 14
265
+
266
+ delta = close_series.diff()
267
+ gain = delta.where(delta > 0, 0)
268
+ loss = -delta.where(delta < 0, 0)
269
+
270
+ avg_gain = gain.rolling(window=window_length, min_periods=1).mean()
271
+ avg_loss = loss.rolling(window=window_length, min_periods=1).mean()
272
+
273
+ rs = avg_gain / avg_loss
274
+ df['RSI'] = 100 - (100 / (1 + rs))
275
+
276
+ fig, ax = plt.subplots(figsize=(10, 3.5))
277
+ ax.plot(df['Date'], df['RSI'], label='RSI', color="#6dd6ff", linewidth=1.4)
278
+ ax.axhline(70, color="#ff9b9b", linestyle="--", linewidth=1, label="Overbought")
279
+ ax.axhline(30, color="#7cf0c6", linestyle="--", linewidth=1, label="Oversold")
280
+ ax.set_ylabel("RSI")
281
+ ax.grid(alpha=0.2)
282
+ ax.legend()
283
+ fig.tight_layout()
284
+ st.pyplot(fig, clear_figure=True)
285
+ plt.close(fig)
286
+
287
+ st.subheader("Volume Analysis")
288
+ st.caption("Volume bars styled to match the rest of the dashboard.")
289
+ fig, ax = plt.subplots(figsize=(10, 3.5))
290
+ ax.bar(df['Date'], volume_series, color=(109/255, 214/255, 255/255, 0.55))
291
+ ax.set_ylabel("Volume")
292
+ ax.grid(alpha=0.15)
293
+ fig.tight_layout()
294
+ st.pyplot(fig, clear_figure=True)
295
+ plt.close(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
  with ai_tab:
298
  st.subheader("In-depth Analysis")
requirements.txt CHANGED
@@ -1,6 +1,7 @@
1
  streamlit==1.52.1
2
  yfinance
3
  pandas==2.3.3
4
- plotly==6.5.0
5
  numpy==1.26.4
6
  langchain-openai==0.2.9
 
 
 
1
  streamlit==1.52.1
2
  yfinance
3
  pandas==2.3.3
 
4
  numpy==1.26.4
5
  langchain-openai==0.2.9
6
+ matplotlib
7
+ mplfinance