Spaces:
Runtime error
Runtime error
Create plotting_interactive.py
Browse files
utils/plotting_interactive.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import plotly.graph_objects as go
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
def plot_stock_data_with_signals_interactive(data):
|
6 |
+
# Ensure the DataFrame's index is in datetime format.
|
7 |
+
data.index = pd.to_datetime(data.index, errors='coerce')
|
8 |
+
|
9 |
+
# Create a Plotly figure
|
10 |
+
fig = go.Figure()
|
11 |
+
|
12 |
+
# Plotting stock 'Close' prices
|
13 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], name='Close Price', line=dict(color='black', width=2)))
|
14 |
+
|
15 |
+
# Check and plot EMAs if they exist
|
16 |
+
if 'EMA_20' in data.columns and 'EMA_50' in data.columns:
|
17 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['EMA_20'], name='EMA 20', line=dict(color='blue', width=1.5)))
|
18 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['EMA_50'], name='EMA 50', line=dict(color='red', width=1.5)))
|
19 |
+
|
20 |
+
# Check and plot Bollinger Bands if they exist
|
21 |
+
if 'BB_Upper' in data.columns and 'BB_Lower' in data.columns:
|
22 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['BB_Upper'], name='BB Upper', fill=None, line=dict(color='grey', width=0.5)))
|
23 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['BB_Lower'], name='BB Lower', fill='tonexty', line=dict(color='grey', width=0.5)))
|
24 |
+
|
25 |
+
# Highlight buy/sell signals if they exist
|
26 |
+
if 'Combined_Signal' in data.columns:
|
27 |
+
buy_signals = data[data['Combined_Signal'] == 'buy']
|
28 |
+
sell_signals = data[data['Combined_Signal'] == 'sell']
|
29 |
+
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')))
|
30 |
+
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')))
|
31 |
+
|
32 |
+
# Update layout for a better view
|
33 |
+
fig.update_layout(title='Stock Price with Buy/Sell Signals', xaxis_title='Date', yaxis_title='Price', xaxis_rangeslider_visible=False)
|
34 |
+
|
35 |
+
# Display the figure in Streamlit
|
36 |
+
st.plotly_chart(fig, use_container_width=True)
|