netflypsb commited on
Commit
cd0e571
·
verified ·
1 Parent(s): 4a93688

Create utils/plotting.py

Browse files
Files changed (1) hide show
  1. utils/plotting.py +51 -0
utils/plotting.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.