Spaces:
Runtime error
Runtime error
Update utils/plotting.py
Browse files- utils/plotting.py +13 -16
utils/plotting.py
CHANGED
|
@@ -3,12 +3,13 @@ import matplotlib.dates as mdates
|
|
| 3 |
|
| 4 |
def plot_stock_data_with_signals(stock_data):
|
| 5 |
"""
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
Parameters:
|
| 9 |
-
- stock_data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50',
|
|
|
|
| 10 |
"""
|
| 11 |
-
# Setting up the plot
|
| 12 |
fig, ax = plt.subplots(figsize=(14, 7))
|
| 13 |
|
| 14 |
# Plotting the closing prices
|
|
@@ -22,28 +23,24 @@ def plot_stock_data_with_signals(stock_data):
|
|
| 22 |
ax.plot(stock_data.index, stock_data['BB_Upper'], label='Upper Bollinger Band', color='red', linestyle='--', alpha=0.5)
|
| 23 |
ax.plot(stock_data.index, stock_data['BB_Lower'], label='Lower Bollinger Band', color='cyan', linestyle='--', alpha=0.5)
|
| 24 |
|
| 25 |
-
# Highlighting buy signals
|
| 26 |
buy_signals = stock_data[stock_data['Buy_Signal']]
|
| 27 |
-
ax.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1, s=100)
|
| 28 |
-
|
| 29 |
-
# Highlighting sell signals
|
| 30 |
sell_signals = stock_data[stock_data['Sell_Signal']]
|
|
|
|
| 31 |
ax.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1, s=100)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
ax.set_title("Stock Price with Indicators and Signals")
|
| 35 |
ax.set_xlabel("Date")
|
| 36 |
ax.set_ylabel("Price")
|
| 37 |
-
ax.legend()
|
| 38 |
|
| 39 |
-
# Formatting the x-axis
|
| 40 |
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
|
| 41 |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
| 42 |
-
fig.autofmt_xdate()
|
| 43 |
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Generate or fetch your stock_data DataFrame with necessary columns before calling this function
|
| 49 |
-
pass
|
|
|
|
| 3 |
|
| 4 |
def plot_stock_data_with_signals(stock_data):
|
| 5 |
"""
|
| 6 |
+
Creates a plot of stock data along with SMAs, Bollinger Bands, and buy/sell signals,
|
| 7 |
+
tailored for display in a Streamlit app.
|
| 8 |
|
| 9 |
Parameters:
|
| 10 |
+
- stock_data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50',
|
| 11 |
+
'BB_Upper', 'BB_Lower', 'Buy_Signal', and 'Sell_Signal' columns.
|
| 12 |
"""
|
|
|
|
| 13 |
fig, ax = plt.subplots(figsize=(14, 7))
|
| 14 |
|
| 15 |
# Plotting the closing prices
|
|
|
|
| 23 |
ax.plot(stock_data.index, stock_data['BB_Upper'], label='Upper Bollinger Band', color='red', linestyle='--', alpha=0.5)
|
| 24 |
ax.plot(stock_data.index, stock_data['BB_Lower'], label='Lower Bollinger Band', color='cyan', linestyle='--', alpha=0.5)
|
| 25 |
|
| 26 |
+
# Highlighting buy and sell signals
|
| 27 |
buy_signals = stock_data[stock_data['Buy_Signal']]
|
|
|
|
|
|
|
|
|
|
| 28 |
sell_signals = stock_data[stock_data['Sell_Signal']]
|
| 29 |
+
ax.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1, s=100)
|
| 30 |
ax.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1, s=100)
|
| 31 |
|
| 32 |
+
# Setting title and labels
|
| 33 |
ax.set_title("Stock Price with Indicators and Signals")
|
| 34 |
ax.set_xlabel("Date")
|
| 35 |
ax.set_ylabel("Price")
|
|
|
|
| 36 |
|
| 37 |
+
# Formatting date on the x-axis
|
| 38 |
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
|
| 39 |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
| 40 |
+
fig.autofmt_xdate()
|
| 41 |
|
| 42 |
+
# Adding legend
|
| 43 |
+
ax.legend()
|
| 44 |
|
| 45 |
+
# Instead of plt.show(), just return the figure object
|
| 46 |
+
return fig
|
|
|
|
|
|