Spaces:
Running
Running
Update utils/plotting.py
Browse files- 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 |
-
|
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,
|
15 |
-
|
16 |
-
# Plotting stock prices
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
if
|
|
|
29 |
buy_signals = data[data['Combined_Signal'] == 'buy']
|
30 |
sell_signals = data[data['Combined_Signal'] == 'sell']
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|