Spaces:
Sleeping
Sleeping
Update chart_builder.py
Browse files- chart_builder.py +62 -79
chart_builder.py
CHANGED
|
@@ -2,97 +2,80 @@
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from plotly.subplots import make_subplots
|
| 4 |
|
| 5 |
-
def build_chart(df, indicators
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Returns Plotly HTML div
|
| 10 |
"""
|
| 11 |
fig = make_subplots(
|
| 12 |
rows=2, cols=1,
|
| 13 |
shared_xaxes=True,
|
| 14 |
row_heights=[0.7, 0.3],
|
| 15 |
-
vertical_spacing=0.
|
| 16 |
-
subplot_titles=
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
x=df.index,
|
| 25 |
-
open=df['Open'],
|
| 26 |
-
high=df['High'],
|
| 27 |
-
low=df['Low'],
|
| 28 |
-
close=df['Close'],
|
| 29 |
-
name="Price"
|
| 30 |
-
),
|
| 31 |
-
row=1, col=1
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# -------------------------------
|
| 35 |
-
# ADD MOVING AVERAGES ON MAIN CHART
|
| 36 |
-
# -------------------------------
|
| 37 |
-
if indicators:
|
| 38 |
-
for name in ['SMA20','SMA50','EMA20','EMA50']:
|
| 39 |
-
if name in indicators:
|
| 40 |
-
fig.add_trace(
|
| 41 |
-
go.Scatter(
|
| 42 |
-
x=df.index,
|
| 43 |
-
y=indicators[name],
|
| 44 |
-
mode='lines',
|
| 45 |
-
name=name
|
| 46 |
-
),
|
| 47 |
-
row=1, col=1
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# -------------------------------
|
| 51 |
-
# VOLUME BAR
|
| 52 |
-
# -------------------------------
|
| 53 |
-
fig.add_trace(
|
| 54 |
-
go.Bar(
|
| 55 |
-
x=df.index,
|
| 56 |
-
y=df['Volume'],
|
| 57 |
-
name="Volume",
|
| 58 |
-
marker_color='lightblue'
|
| 59 |
-
),
|
| 60 |
-
row=2, col=1
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
fig.add_trace(
|
| 71 |
-
go.Scatter(x=macd.index, y=macd['MACD'], name="MACD", line=dict(color='blue')),
|
| 72 |
-
row=2, col=1
|
| 73 |
-
)
|
| 74 |
-
fig.add_trace(
|
| 75 |
-
go.Scatter(x=macd.index, y=macd['Signal'], name="MACD Signal", line=dict(color='orange')),
|
| 76 |
-
row=2, col=1
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
|
|
|
| 91 |
fig.update_layout(
|
| 92 |
-
xaxis_rangeslider_visible=False,
|
| 93 |
-
template="plotly_white",
|
| 94 |
height=700,
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import plotly.graph_objects as go
|
| 3 |
from plotly.subplots import make_subplots
|
| 4 |
|
| 5 |
+
def build_chart(df, indicators, symbol="Stock"):
|
| 6 |
"""
|
| 7 |
+
Build Plotly chart with main OHLC + optional subplots for indicators.
|
| 8 |
+
Inject JS to toggle each indicator dynamically.
|
|
|
|
| 9 |
"""
|
| 10 |
fig = make_subplots(
|
| 11 |
rows=2, cols=1,
|
| 12 |
shared_xaxes=True,
|
| 13 |
row_heights=[0.7, 0.3],
|
| 14 |
+
vertical_spacing=0.05,
|
| 15 |
+
subplot_titles=(f"{symbol} Price", "Indicators")
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# --- Main chart: OHLC + MA ---
|
| 19 |
+
fig.add_trace(go.Candlestick(
|
| 20 |
+
x=df.index, open=df['Open'], high=df['High'],
|
| 21 |
+
low=df['Low'], close=df['Close'], name='OHLC'
|
| 22 |
+
), row=1, col=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# MA on main chart
|
| 25 |
+
for ma in ['SMA20', 'SMA50']:
|
| 26 |
+
if ma in indicators:
|
| 27 |
+
fig.add_trace(go.Scatter(
|
| 28 |
+
x=df.index, y=indicators[ma],
|
| 29 |
+
mode='lines', name=ma
|
| 30 |
+
), row=1, col=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# --- Subplot for MACD/RSI/SuperTrend ---
|
| 33 |
+
indicator_row = 2
|
| 34 |
+
for ind_name in ['MACD', 'RSI', 'SuperTrend']:
|
| 35 |
+
if ind_name in indicators:
|
| 36 |
+
data = indicators[ind_name]
|
| 37 |
+
if isinstance(data, pd.DataFrame):
|
| 38 |
+
for col in data.columns:
|
| 39 |
+
fig.add_trace(go.Scatter(
|
| 40 |
+
x=df.index, y=data[col],
|
| 41 |
+
mode='lines', name=f"{ind_name}-{col}",
|
| 42 |
+
visible=False # Start hidden, toggle later
|
| 43 |
+
), row=indicator_row, col=1)
|
| 44 |
+
else:
|
| 45 |
+
fig.add_trace(go.Scatter(
|
| 46 |
+
x=df.index, y=data,
|
| 47 |
+
mode='lines', name=ind_name,
|
| 48 |
+
visible=False
|
| 49 |
+
), row=indicator_row, col=1)
|
| 50 |
|
| 51 |
+
# --- Layout ---
|
| 52 |
fig.update_layout(
|
|
|
|
|
|
|
| 53 |
height=700,
|
| 54 |
+
showlegend=True,
|
| 55 |
+
title=f"{symbol} Chart with Indicators",
|
| 56 |
+
xaxis_rangeslider_visible=False
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# --- Inject JS for toggle buttons ---
|
| 60 |
+
toggle_script = """
|
| 61 |
+
<script>
|
| 62 |
+
function toggleIndicator(indName) {
|
| 63 |
+
var gd = document.querySelectorAll('.js-plotly-plot')[0];
|
| 64 |
+
var update = {visible: []};
|
| 65 |
+
gd.data.forEach(function(trace) {
|
| 66 |
+
if(trace.name.includes(indName)) {
|
| 67 |
+
trace.visible = !trace.visible;
|
| 68 |
+
}
|
| 69 |
+
});
|
| 70 |
+
Plotly.redraw(gd);
|
| 71 |
+
}
|
| 72 |
+
</script>
|
| 73 |
+
<div style="margin-top:10px;">
|
| 74 |
+
<button onclick="toggleIndicator('MACD')">Toggle MACD</button>
|
| 75 |
+
<button onclick="toggleIndicator('RSI')">Toggle RSI</button>
|
| 76 |
+
<button onclick="toggleIndicator('SuperTrend')">Toggle SuperTrend</button>
|
| 77 |
+
</div>
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
chart_html = fig.to_html(full_html=False, include_plotlyjs='cdn') + toggle_script
|
| 81 |
+
return chart_html
|