netflypsb commited on
Commit
af509f9
1 Parent(s): 9d82034

Update utils/plotting.py

Browse files
Files changed (1) hide show
  1. utils/plotting.py +35 -51
utils/plotting.py CHANGED
@@ -1,60 +1,44 @@
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()
 
1
  import matplotlib.pyplot as plt
2
  import matplotlib.dates as mdates
3
+ import pandas as pd
4
  from pandas.plotting import register_matplotlib_converters
5
+
6
  register_matplotlib_converters()
7
 
8
  def plot_stock_data_with_signals(data):
9
+ # First, ensure the DataFrame's index is in datetime format.
10
+ data.index = pd.to_datetime(data.index, errors='coerce')
11
+
 
 
 
12
  # Create a new figure and set the size.
13
+ plt.figure(figsize=(14, 7))
14
+
15
+ # Plotting stock 'Close' prices
16
+ plt.plot(data.index, data['Close'], label='Close Price', color='black', lw=2)
17
+
18
+ # Check and plot EMAs if they exist
19
+ if 'EMA_20' in data.columns and 'EMA_50' in data.columns:
20
+ plt.plot(data.index, data['EMA_20'], label='EMA 20', color='blue', lw=1.5)
21
+ plt.plot(data.index, data['EMA_50'], label='EMA 50', color='red', lw=1.5)
22
+
23
+ # Check and plot Bollinger Bands if they exist
24
+ if 'BB_Upper' in data.columns and 'BB_Lower' in data.columns:
25
+ plt.fill_between(data.index, data['BB_Lower'], data['BB_Upper'], color='grey', alpha=0.1, label='Bollinger Bands')
26
+
27
+ # Highlight buy/sell signals if they exist
28
+ if 'Combined_Signal' in data.columns:
29
  buy_signals = data[data['Combined_Signal'] == 'buy']
30
  sell_signals = data[data['Combined_Signal'] == 'sell']
31
+ plt.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1)
32
+ plt.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1)
33
+
34
+ plt.title('Stock Price with Buy/Sell Signals')
35
+ plt.xlabel('Date')
36
+ plt.ylabel('Price')
37
+ plt.legend()
38
+
39
+ # Improve the x-axis date format
40
+ plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
41
+ plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
42
+ plt.gcf().autofmt_xdate() # Rotation
43
+
44
+ plt.show()