import pandas as pd import plotly.graph_objects as go import streamlit as st def plot_stock_data_with_signals_interactive(data): # Ensure the DataFrame's index is in datetime format. data.index = pd.to_datetime(data.index, errors='coerce') # Create a Plotly figure fig = go.Figure() # Plotting stock 'Close' prices fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black', width=2))) # Check and plot EMAs if they exist if 'EMA_20' in data.columns and 'EMA_50' in data.columns: fig.add_trace(go.Scatter(x=data.index, y=data['EMA_20'], name='EMA 20', line=dict(color='blue', width=1.5))) fig.add_trace(go.Scatter(x=data.index, y=data['EMA_50'], name='EMA 50', line=dict(color='red', width=1.5))) # Check and plot Bollinger Bands if they exist if 'BB_Upper' in data.columns and 'BB_Lower' in data.columns: fig.add_trace(go.Scatter(x=data.index, y=data['BB_Upper'], name='BB Upper', fill=None, line=dict(color='grey', width=0.5))) fig.add_trace(go.Scatter(x=data.index, y=data['BB_Lower'], name='BB Lower', fill='tonexty', line=dict(color='grey', width=0.5))) # Highlight buy/sell signals if they exist if 'Combined_Signal' in data.columns: buy_signals = data[data['Combined_Signal'] == 'buy'] sell_signals = data[data['Combined_Signal'] == 'sell'] fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals['Close'], mode='markers', name='Buy Signal', marker=dict(color='green', size=10, symbol='triangle-up'))) fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals['Close'], mode='markers', name='Sell Signal', marker=dict(color='red', size=10, symbol='triangle-down'))) # Update layout for a better view fig.update_layout(title='Stock Price with Buy/Sell Signals', xaxis_title='Date', yaxis_title='Price', xaxis_rangeslider_visible=False) # Display the figure in Streamlit st.plotly_chart(fig, use_container_width=True)