netflypsb commited on
Commit
8d318ce
1 Parent(s): d18aeff

Update utils/plotting.py

Browse files
Files changed (1) hide show
  1. 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 pandas as pd
5
 
6
- def plot_stock_data(data, buy_signals=None, sell_signals=None, title="Stock Data with Indicators"):
7
  """
8
- Plots stock data with SMAs, Bollinger Bands, and buy/sell signals.
9
 
10
  Parameters:
11
- - data: DataFrame containing the stock data with 'Close', 'SMA_21', 'SMA_50', 'BB_Upper', and 'BB_Lower' columns.
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
- # Create a new figure and set the size
17
- plt.figure(figsize=(14, 7))
 
 
 
18
 
19
- # Plot the closing price
20
- plt.plot(data.index, data['Close'], label='Close Price', color='skyblue', linewidth=2)
 
21
 
22
- # Plot SMAs
23
- plt.plot(data.index, data['SMA_21'], label='21-period SMA', color='orange', linewidth=1.5)
24
- plt.plot(data.index, data['SMA_50'], label='50-period SMA', color='green', linewidth=1.5)
25
 
26
- # Plot Bollinger Bands
27
- plt.plot(data.index, data['BB_Upper'], label='Upper Bollinger Band', color='grey', linestyle='--', linewidth=1)
28
- plt.plot(data.index, data['BB_Lower'], label='Lower Bollinger Band', color='grey', linestyle='--', linewidth=1)
29
 
30
- # Highlight buy signals
31
- if buy_signals is not None:
32
- plt.scatter(buy_signals.index, data.loc[buy_signals.index]['Close'], marker='^', color='green', label='Buy Signal', alpha=1)
33
 
34
- # Highlight sell signals
35
- if sell_signals is not None:
36
- plt.scatter(sell_signals.index, data.loc[sell_signals.index]['Close'], marker='v', color='red', label='Sell Signal', alpha=1)
 
 
37
 
38
- # Customize the plot
39
- plt.title(title)
40
- plt.xlabel('Date')
41
- plt.ylabel('Price')
42
- plt.legend()
43
- plt.grid(True)
44
 
45
- # Show the plot
46
  plt.show()
47
 
48
- # Example usage:
49
- # This assumes `data` DataFrame is already loaded with the required columns including 'Close', 'SMA_21', 'SMA_50', 'BB_Upper', 'BB_Lower'.
50
- # `buy_signals` and `sell_signals` DataFrames/Series should have the dates of signals.
51
- # Due to the nature of this example, actual data is not provided here. To test this function, ensure you have a DataFrame with the appropriate structure.
 
 
 
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