Update chart_builder.py
Browse files- chart_builder.py +59 -39
chart_builder.py
CHANGED
|
@@ -1,60 +1,80 @@
|
|
| 1 |
# chart_builder.py
|
| 2 |
-
import
|
| 3 |
-
import plotly.graph_objs as go
|
| 4 |
from plotly.subplots import make_subplots
|
|
|
|
| 5 |
|
| 6 |
-
def build_chart(df, indicators
|
| 7 |
"""
|
| 8 |
-
df: OHLCV
|
| 9 |
-
indicators: dict
|
| 10 |
-
Returns HTML string of chart
|
| 11 |
"""
|
|
|
|
| 12 |
fig = make_subplots(
|
| 13 |
-
rows=2,
|
| 14 |
cols=1,
|
| 15 |
shared_xaxes=True,
|
| 16 |
-
row_heights=[0.7, 0.3],
|
| 17 |
vertical_spacing=0.03,
|
| 18 |
-
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Main
|
| 22 |
fig.add_trace(go.Candlestick(
|
| 23 |
-
x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'],
|
|
|
|
| 24 |
), row=1, col=1)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
fig.add_trace(go.Bar(
|
| 28 |
-
x=df.index, y=df['Volume'], name=
|
| 29 |
), row=2, col=1)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
fig.update_layout(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
html_chart = fig.to_html(full_html=False, include_plotlyjs='cdn')
|
| 60 |
-
return html_chart
|
|
|
|
| 1 |
# chart_builder.py
|
| 2 |
+
import plotly.graph_objects as go
|
|
|
|
| 3 |
from plotly.subplots import make_subplots
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
+
def build_chart(df, indicators):
|
| 7 |
"""
|
| 8 |
+
df: OHLCV DataFrame with ['Open','High','Low','Close','Volume']
|
| 9 |
+
indicators: dict from indicater.py
|
| 10 |
+
Returns: HTML string of Plotly chart with JS toggles
|
| 11 |
"""
|
| 12 |
+
# --- Create subplots ---
|
| 13 |
fig = make_subplots(
|
| 14 |
+
rows=2 + sum(1 for k in indicators if k not in ['SMA_5','SMA_10','SMA_20','SMA_50','EMA_5','EMA_10','EMA_20','EMA_50','Volume']),
|
| 15 |
cols=1,
|
| 16 |
shared_xaxes=True,
|
|
|
|
| 17 |
vertical_spacing=0.03,
|
| 18 |
+
row_heights=[0.5, 0.2] + [0.2]*sum(1 for k in indicators if k not in ['SMA_5','SMA_10','SMA_20','SMA_50','EMA_5','EMA_10','EMA_20','EMA_50','Volume'])
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- Main Candle Chart ---
|
| 22 |
fig.add_trace(go.Candlestick(
|
| 23 |
+
x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'],
|
| 24 |
+
name='Price'
|
| 25 |
), row=1, col=1)
|
| 26 |
|
| 27 |
+
# --- Add MA/EMA overlays on main chart ---
|
| 28 |
+
for key in ['SMA_5','SMA_10','SMA_20','SMA_50','EMA_5','EMA_10','EMA_20','EMA_50']:
|
| 29 |
+
if key in indicators:
|
| 30 |
+
fig.add_trace(go.Scatter(
|
| 31 |
+
x=df.index, y=indicators[key],
|
| 32 |
+
mode='lines', name=key, visible=False # initially hidden
|
| 33 |
+
), row=1, col=1)
|
| 34 |
+
|
| 35 |
+
# --- Volume subplot ---
|
| 36 |
fig.add_trace(go.Bar(
|
| 37 |
+
x=df.index, y=df['Volume'], name='Volume'
|
| 38 |
), row=2, col=1)
|
| 39 |
|
| 40 |
+
# --- Other indicators in separate subplots ---
|
| 41 |
+
row_counter = 3
|
| 42 |
+
for key, series in indicators.items():
|
| 43 |
+
if key in ['SMA_5','SMA_10','SMA_20','SMA_50','EMA_5','EMA_10','EMA_20','EMA_50','Volume']:
|
| 44 |
+
continue
|
| 45 |
+
fig.add_trace(go.Scatter(
|
| 46 |
+
x=df.index, y=series,
|
| 47 |
+
mode='lines', name=key, visible=False
|
| 48 |
+
), row=row_counter, col=1)
|
| 49 |
+
row_counter += 1
|
| 50 |
+
|
| 51 |
+
fig.update_layout(
|
| 52 |
+
height=600 + 200*(row_counter-3),
|
| 53 |
+
showlegend=True,
|
| 54 |
+
margin=dict(l=40, r=40, t=40, b=40),
|
| 55 |
+
xaxis_rangeslider_visible=False
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# --- Inject JS buttons to toggle visibility ---
|
| 59 |
+
buttons = []
|
| 60 |
+
for i, trace in enumerate(fig.data):
|
| 61 |
+
buttons.append(dict(
|
| 62 |
+
label=trace.name,
|
| 63 |
+
method='restyle',
|
| 64 |
+
args=['visible', [j==i for j in range(len(fig.data))]],
|
| 65 |
+
))
|
| 66 |
|
| 67 |
fig.update_layout(
|
| 68 |
+
updatemenus=[dict(
|
| 69 |
+
type="dropdown",
|
| 70 |
+
direction="down",
|
| 71 |
+
buttons=buttons,
|
| 72 |
+
showactive=True,
|
| 73 |
+
x=1.02,
|
| 74 |
+
xanchor="left",
|
| 75 |
+
y=1.15,
|
| 76 |
+
yanchor="top"
|
| 77 |
+
)]
|
| 78 |
)
|
| 79 |
|
| 80 |
+
return fig.to_html(full_html=False)
|
|
|
|
|
|