Spaces:
Runtime error
Runtime error
import matplotlib.pyplot as plt | |
import matplotlib.dates as mdates | |
def plot_data_with_indicators_and_signals(data): | |
""" | |
Plots historical price data, SMAs, Bollinger Bands, and buy/sell signals. | |
Args: | |
data (pd.DataFrame): The DataFrame containing the historical prices, SMAs, Bollinger Bands, | |
and signals. It expects 'close', 'SMA_21', 'SMA_50', 'BB_Upper', | |
'BB_Lower', 'Buy_Signal', and 'Sell_Signal' columns. | |
""" | |
# Create a new figure and set the size | |
plt.figure(figsize=(14, 7)) | |
# Plot closing price | |
plt.plot(data['date'], data['close'], label='Close Price', alpha=0.5) | |
# Plot SMAs | |
plt.plot(data['date'], data['SMA_21'], label='21-Period SMA', alpha=0.75) | |
plt.plot(data['date'], data['SMA_50'], label='50-Period SMA', alpha=0.75) | |
# Plot Bollinger Bands | |
plt.plot(data['date'], data['BB_Upper'], label='Upper Bollinger Band', linestyle='--', alpha=0.5) | |
plt.plot(data['date'], data['BB_Lower'], label='Lower Bollinger Band', linestyle='--', alpha=0.5) | |
# Highlight buy signals | |
buy_signals = data[data['Buy_Signal']] | |
plt.scatter(buy_signals['date'], buy_signals['close'], label='Buy Signal', marker='^', color='green', alpha=1) | |
# Highlight sell signals | |
sell_signals = data[data['Sell_Signal']] | |
plt.scatter(sell_signals['date'], sell_signals['close'], label='Sell Signal', marker='v', color='red', alpha=1) | |
# Formatting the plot | |
plt.title('Cryptocurrency Analysis with Buy/Sell Signals') | |
plt.xlabel('Date') | |
plt.ylabel('Price') | |
plt.legend() | |
plt.grid(True) | |
plt.xticks(rotation=45) | |
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) | |
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5)) | |
# Show plot | |
plt.tight_layout() | |
plt.show() | |