netflypsb commited on
Commit
aa4f8e5
1 Parent(s): 43e3b42

Update utils/plotting.py

Browse files
Files changed (1) hide show
  1. 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
- 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.
 
 
 
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()