Spaces:
Running
Running
Create utils/plotting.py
Browse files- utils/plotting.py +53 -0
utils/plotting.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# utils/plotting.py
|
2 |
+
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import matplotlib.dates as mdates
|
5 |
+
|
6 |
+
def plot_stock_data_with_signals(data):
|
7 |
+
"""
|
8 |
+
Plots stock data, indicators, and buy/sell signals.
|
9 |
+
|
10 |
+
Parameters:
|
11 |
+
- data (DataFrame): DataFrame containing stock 'Close' prices, indicator values, and signals.
|
12 |
+
"""
|
13 |
+
# Create a new figure and set the size.
|
14 |
+
plt.figure(figsize=(14, 10))
|
15 |
+
|
16 |
+
# Plot closing prices and EMAs
|
17 |
+
ax1 = plt.subplot(311) # 3 rows, 1 column, 1st subplot
|
18 |
+
data['Close'].plot(ax=ax1, color='black', lw=2., legend=True)
|
19 |
+
if 'EMA_Short' in data.columns and 'EMA_Long' in data.columns:
|
20 |
+
data[['EMA_Short', 'EMA_Long']].plot(ax=ax1, lw=1.5, legend=True)
|
21 |
+
ax1.set_title('Stock Price, EMAs, and Bollinger Bands')
|
22 |
+
ax1.fill_between(data.index, data['BB_Lower'], data['BB_Upper'], color='grey', alpha=0.3)
|
23 |
+
|
24 |
+
# Highlight buy/sell signals
|
25 |
+
buy_signals = data[data['Combined_Signal'] == 'buy']
|
26 |
+
sell_signals = data[data['Combined_Signal'] == 'sell']
|
27 |
+
ax1.plot(buy_signals.index, data.loc[buy_signals.index]['Close'], '^', markersize=10, color='g', lw=0, label='Buy Signal')
|
28 |
+
ax1.plot(sell_signals.index, data.loc[sell_signals.index]['Close'], 'v', markersize=10, color='r', lw=0, label='Sell Signal')
|
29 |
+
ax1.legend()
|
30 |
+
|
31 |
+
# Plot MACD and Signal Line
|
32 |
+
ax2 = plt.subplot(312, sharex=ax1) # Share x-axis with ax1
|
33 |
+
data['MACD'].plot(ax=ax2, color='blue', label='MACD', legend=True)
|
34 |
+
data['MACD_Signal_Line'].plot(ax=ax2, color='red', label='Signal Line', legend=True)
|
35 |
+
ax2.fill_between(data.index, data['MACD'] - data['MACD_Signal_Line'], color='grey', alpha=0.3)
|
36 |
+
ax2.set_title('MACD')
|
37 |
+
|
38 |
+
# Plot RSI
|
39 |
+
ax3 = plt.subplot(313, sharex=ax1) # Share x-axis with ax1
|
40 |
+
data['RSI'].plot(ax=ax3, color='purple', legend=True)
|
41 |
+
ax3.axhline(70, linestyle='--', alpha=0.5, color='red')
|
42 |
+
ax3.axhline(30, linestyle='--', alpha=0.5, color='green')
|
43 |
+
ax3.set_title('RSI')
|
44 |
+
|
45 |
+
# Improve layout and x-axis date format
|
46 |
+
plt.tight_layout()
|
47 |
+
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
48 |
+
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
|
49 |
+
plt.xticks(rotation=45)
|
50 |
+
|
51 |
+
plt.show()
|
52 |
+
|
53 |
+
# Note: This is a basic example for visualization. You may need to adjust it based on your actual 'data' DataFrame structure and the specific indicators you are plotting.
|