Spaces:
Running
Running
Update utils/plotting.py
Browse files- utils/plotting.py +44 -37
utils/plotting.py
CHANGED
@@ -1,53 +1,60 @@
|
|
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 |
-
|
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 |
-
#
|
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
|
20 |
data[['EMA_Short', 'EMA_Long']].plot(ax=ax1, lw=1.5, legend=True)
|
21 |
-
ax1.set_title('Stock Price
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
plt.tight_layout()
|
47 |
-
|
48 |
-
|
49 |
-
|
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.
|
|
|
|
|
|
|
1 |
import matplotlib.pyplot as plt
|
2 |
import matplotlib.dates as mdates
|
3 |
+
from pandas.plotting import register_matplotlib_converters
|
4 |
+
register_matplotlib_converters()
|
5 |
|
6 |
def plot_stock_data_with_signals(data):
|
7 |
"""
|
8 |
+
Enhanced plotting function to display stock data, indicators, and buy/sell signals with improvements.
|
9 |
+
|
10 |
Parameters:
|
11 |
+
- data (DataFrame): DataFrame containing stock 'Close' prices, optional indicator values, and signals.
|
12 |
"""
|
13 |
# Create a new figure and set the size.
|
14 |
plt.figure(figsize=(14, 10))
|
15 |
+
|
16 |
+
# Plotting stock prices and EMAs if they exist
|
17 |
ax1 = plt.subplot(311) # 3 rows, 1 column, 1st subplot
|
18 |
+
data['Close'].plot(ax=ax1, color='black', lw=2., legend=True, label='Close')
|
19 |
+
if 'EMA_Short' in data and 'EMA_Long' in data:
|
20 |
data[['EMA_Short', 'EMA_Long']].plot(ax=ax1, lw=1.5, legend=True)
|
21 |
+
ax1.set_title('Stock Price and Indicators')
|
22 |
+
|
23 |
+
# Plotting Bollinger Bands if they exist
|
24 |
+
if 'BB_Upper' in data and 'BB_Lower' in data:
|
25 |
+
ax1.fill_between(data.index, data['BB_Lower'], data['BB_Upper'], color='grey', alpha=0.3, label='Bollinger Bands')
|
26 |
+
|
27 |
+
# Highlight buy/sell signals if Combined_Signal column exists
|
28 |
+
if 'Combined_Signal' in data:
|
29 |
+
buy_signals = data[data['Combined_Signal'] == 'buy']
|
30 |
+
sell_signals = data[data['Combined_Signal'] == 'sell']
|
31 |
+
ax1.plot(buy_signals.index, buy_signals['Close'], '^', markersize=10, color='g', lw=0, label='Buy Signal')
|
32 |
+
ax1.plot(sell_signals.index, sell_signals['Close'], 'v', markersize=10, color='r', lw=0, label='Sell Signal')
|
33 |
|
|
|
|
|
|
|
|
|
|
|
34 |
ax1.legend()
|
35 |
|
36 |
+
# Plotting MACD and Signal Line if they exist
|
37 |
+
if 'MACD' in data and 'MACD_Signal_Line' in data:
|
38 |
+
ax2 = plt.subplot(312, sharex=ax1) # Share x-axis with ax1
|
39 |
+
data['MACD'].plot(ax=ax2, color='blue', label='MACD', legend=True)
|
40 |
+
data['MACD_Signal_Line'].plot(ax=ax2, color='red', label='Signal Line', legend=True)
|
41 |
+
ax2.set_title('MACD')
|
42 |
+
ax2.legend()
|
43 |
+
|
44 |
+
# Plotting RSI if it exists
|
45 |
+
if 'RSI' in data:
|
46 |
+
ax3 = plt.subplot(313, sharex=ax1) # Share x-axis with ax1
|
47 |
+
data['RSI'].plot(ax=ax3, color='purple', legend=True, label='RSI')
|
48 |
+
ax3.axhline(70, linestyle='--', alpha=0.5, color='red', label='Overbought')
|
49 |
+
ax3.axhline(30, linestyle='--', alpha=0.5, color='green', label='Oversold')
|
50 |
+
ax3.set_title('RSI')
|
51 |
+
ax3.legend()
|
52 |
+
|
53 |
+
# Improving layout, setting x-axis format for better date handling
|
54 |
plt.tight_layout()
|
55 |
+
for ax in [ax1, ax2, ax3]:
|
56 |
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
57 |
+
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
|
58 |
+
plt.setp(ax.xaxis.get_majorticklabels(), rotation=45)
|
59 |
+
|
60 |
plt.show()
|
|
|
|