Spaces:
Running
Running
Update utils/plotting.py
Browse files- utils/plotting.py +18 -17
utils/plotting.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
import streamlit as st
|
2 |
import matplotlib.pyplot as plt
|
3 |
import matplotlib.dates as mdates
|
4 |
import pandas as pd
|
|
|
5 |
from pandas.plotting import register_matplotlib_converters
|
6 |
|
7 |
register_matplotlib_converters()
|
@@ -10,37 +10,38 @@ def plot_stock_data_with_signals(data):
|
|
10 |
# First, ensure the DataFrame's index is in datetime format.
|
11 |
data.index = pd.to_datetime(data.index, errors='coerce')
|
12 |
|
13 |
-
#
|
14 |
-
plt.
|
15 |
|
16 |
# Plotting stock 'Close' prices
|
17 |
-
|
18 |
|
19 |
# Check and plot EMAs if they exist
|
20 |
if 'EMA_20' in data.columns and 'EMA_50' in data.columns:
|
21 |
-
|
22 |
-
|
23 |
|
24 |
# Check and plot Bollinger Bands if they exist
|
25 |
if 'BB_Upper' in data.columns and 'BB_Lower' in data.columns:
|
26 |
-
|
27 |
|
28 |
# Highlight buy/sell signals if they exist
|
29 |
if 'Combined_Signal' in data.columns:
|
30 |
buy_signals = data[data['Combined_Signal'] == 'buy']
|
31 |
sell_signals = data[data['Combined_Signal'] == 'sell']
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
# Improve the x-axis date format
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
-
st.pyplot()
|
|
|
46 |
|
|
|
|
|
1 |
import matplotlib.pyplot as plt
|
2 |
import matplotlib.dates as mdates
|
3 |
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
from pandas.plotting import register_matplotlib_converters
|
6 |
|
7 |
register_matplotlib_converters()
|
|
|
10 |
# First, ensure the DataFrame's index is in datetime format.
|
11 |
data.index = pd.to_datetime(data.index, errors='coerce')
|
12 |
|
13 |
+
# Creating a figure and axes explicitly
|
14 |
+
fig, ax = plt.subplots(figsize=(14, 7))
|
15 |
|
16 |
# Plotting stock 'Close' prices
|
17 |
+
ax.plot(data.index, data['Close'], label='Close Price', color='black', lw=2)
|
18 |
|
19 |
# Check and plot EMAs if they exist
|
20 |
if 'EMA_20' in data.columns and 'EMA_50' in data.columns:
|
21 |
+
ax.plot(data.index, data['EMA_20'], label='EMA 20', color='blue', lw=1.5)
|
22 |
+
ax.plot(data.index, data['EMA_50'], label='EMA 50', color='red', lw=1.5)
|
23 |
|
24 |
# Check and plot Bollinger Bands if they exist
|
25 |
if 'BB_Upper' in data.columns and 'BB_Lower' in data.columns:
|
26 |
+
ax.fill_between(data.index, data['BB_Lower'], data['BB_Upper'], color='grey', alpha=0.1, label='Bollinger Bands')
|
27 |
|
28 |
# Highlight buy/sell signals if they exist
|
29 |
if 'Combined_Signal' in data.columns:
|
30 |
buy_signals = data[data['Combined_Signal'] == 'buy']
|
31 |
sell_signals = data[data['Combined_Signal'] == 'sell']
|
32 |
+
ax.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1)
|
33 |
+
ax.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1)
|
34 |
|
35 |
+
ax.set_title('Stock Price with Buy/Sell Signals')
|
36 |
+
ax.set_xlabel('Date')
|
37 |
+
ax.set_ylabel('Price')
|
38 |
+
ax.legend()
|
39 |
|
40 |
# Improve the x-axis date format
|
41 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
42 |
+
ax.xaxis.set_major_locator(mdates.MonthLocator())
|
43 |
+
fig.autofmt_xdate() # Rotation
|
44 |
|
45 |
+
# Use the figure object in st.pyplot()
|
46 |
+
st.pyplot(fig)
|
47 |
|