Spaces:
Runtime error
Runtime error
Update utils/plotting.py
Browse files2 unmodified lines345678910111213147 unmodified lines222324252627282930313233343536373839404142434445464748492 unmodified linesdef plot_stock_data_with_signals(stock_data):"""Plots the stock data along with SMAs, Bollinger Bands, and buy/sell signals.Parameters:- stock_data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50', 'BB_Upper', 'BB_Lower', 'Buy_Signal', and 'Sell_Signal' columns."""# Setting up the plotfig, ax = plt.subplots(figsize=(14, 7))# Plotting the closing prices7 unmodified linesax.plot(stock_data.index, stock_data['BB_Upper'], label='Upper Bollinger Band', color='red', linestyle='--', alpha=0.5)ax.plot(stock_data.index, stock_data['BB_Lower'], label='Lower Bollinger Band', color='cyan', linestyle='--', alpha=0.5)# Highlighting buy signalsbuy_signals = stock_data[stock_data['Buy_Signal']]ax.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1, s=100)# Highlighting sell signalssell_signals = stock_data[stock_data['Sell_Signal']]ax.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1, s=100)# Beautifying the plotax.set_title("Stock Price with Indicators and Signals")ax.set_xlabel("Date")ax.set_ylabel("Price")ax.legend()# Formatting the x-axis to show dates nicelyax.xaxis.set_major_locator(mdates.WeekdayLocator())ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))fig.autofmt_xdate() # Rotate date labels to prevent overlapplt.show()if __name__ == "__main__":# Example usage:# Generate or fetch your stock_data DataFrame with necessary columns before calling this functionpass2 unmodified lines34567891011121314157 unmodified lines2324252627282930313233343536373839404142434445462 unmodified linesdef plot_stock_data_with_signals(stock_data):"""Creates a plot of stock data along with SMAs, Bollinger Bands, and buy/sell signals,tailored for display in a Streamlit app.Parameters:- stock_data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50','BB_Upper', 'BB_Lower', 'Buy_Signal', and 'Sell_Signal' columns."""fig, ax = plt.subplots(figsize=(14, 7))# Plotting the closing prices7 unmodified linesax.plot(stock_data.index, stock_data['BB_Upper'], label='Upper Bollinger Band', color='red', linestyle='--', alpha=0.5)ax.plot(stock_data.index, stock_data['BB_Lower'], label='Lower Bollinger Band', color='cyan', linestyle='--', alpha=0.5)# Highlighting buy and sell signalsbuy_signals = stock_data[stock_data['Buy_Signal']]sell_signals = stock_data[stock_data['Sell_Signal']]ax.scatter(buy_signals.index, buy_signals['Close'], label='Buy Signal', marker='^', color='green', alpha=1, s=100)ax.scatter(sell_signals.index, sell_signals['Close'], label='Sell Signal', marker='v', color='red', alpha=1, s=100)# Setting title and labelsax.set_title("Stock Price with Indicators and Signals")ax.set_xlabel("Date")ax.set_ylabel("Price")# Formatting date on the x-axisax.xaxis.set_major_locator(mdates.WeekdayLocator())ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))fig.autofmt_xdate()# Adding legendax.legend()# Instead of plt.show(), just return the figure objectreturn fig
utils/plotting.py+13-16