Spaces:
Runtime error
Runtime error
Update utils/plotting.py
Browse files- utils/plotting.py +34 -36
utils/plotting.py
CHANGED
|
@@ -1,51 +1,49 @@
|
|
| 1 |
-
# utils/plotting.py
|
| 2 |
-
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
Plots stock data with SMAs, Bollinger Bands, and buy/sell signals.
|
| 9 |
|
| 10 |
Parameters:
|
| 11 |
-
-
|
| 12 |
-
- buy_signals: DataFrame or Series with buy signals. Must contain a 'Date' or similar index for plotting.
|
| 13 |
-
- sell_signals: DataFrame or Series with sell signals. Must contain a 'Date' or similar index for plotting.
|
| 14 |
-
- title: Title of the plot.
|
| 15 |
"""
|
| 16 |
-
#
|
| 17 |
-
plt.
|
|
|
|
|
|
|
|
|
|
| 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 |
-
plt.legend()
|
| 43 |
-
plt.grid(True)
|
| 44 |
|
| 45 |
-
# Show the plot
|
| 46 |
plt.show()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
+
import matplotlib.dates as mdates
|
| 3 |
|
| 4 |
+
def plot_stock_data_with_signals(stock_data):
|
| 5 |
"""
|
| 6 |
+
Plots the stock data along with SMAs, Bollinger Bands, and buy/sell signals.
|
| 7 |
|
| 8 |
Parameters:
|
| 9 |
+
- stock_data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50', 'BB_Upper', 'BB_Lower', 'Buy_Signal', and 'Sell_Signal' columns.
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
+
# Setting up the plot
|
| 12 |
+
fig, ax = plt.subplots(figsize=(14, 7))
|
| 13 |
+
|
| 14 |
+
# Plotting the closing prices
|
| 15 |
+
ax.plot(stock_data.index, stock_data['Close'], label='Close Price', color='blue', alpha=0.5)
|
| 16 |
|
| 17 |
+
# Plotting the SMAs
|
| 18 |
+
ax.plot(stock_data.index, stock_data['SMA_21'], label='21-Period SMA', color='orange', alpha=0.75)
|
| 19 |
+
ax.plot(stock_data.index, stock_data['SMA_50'], label='50-Period SMA', color='green', alpha=0.75)
|
| 20 |
|
| 21 |
+
# Plotting the Bollinger Bands
|
| 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 |
+
# Beautifying the plot
|
| 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 to show dates nicely
|
| 40 |
+
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
|
| 41 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
| 42 |
+
fig.autofmt_xdate() # Rotate date labels to prevent overlap
|
|
|
|
|
|
|
| 43 |
|
|
|
|
| 44 |
plt.show()
|
| 45 |
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
# Example usage:
|
| 48 |
+
# Generate or fetch your stock_data DataFrame with necessary columns before calling this function
|
| 49 |
+
pass
|