Update chart_builder.py
Browse files- chart_builder.py +46 -69
chart_builder.py
CHANGED
|
@@ -1,83 +1,60 @@
|
|
| 1 |
# chart_builder.py
|
| 2 |
-
import plotly.graph_objs as go
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def build_chart(df, indicators=None):
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
indicators : dict {name: Series or DataFrame} from indicater.py
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
fig.add_trace(go.Candlestick(
|
| 19 |
-
x=df.index,
|
| 20 |
-
|
| 21 |
-
high=df['High'],
|
| 22 |
-
low=df['Low'],
|
| 23 |
-
close=df['Close'],
|
| 24 |
-
name='Price'
|
| 25 |
-
))
|
| 26 |
-
|
| 27 |
-
# --- Overlay indicators on main chart (SMA, EMA) ---
|
| 28 |
-
overlay_indicators = ['SMA5','SMA20','SMA50','SMA200','EMA5','EMA20','EMA50','EMA200']
|
| 29 |
-
for ind in overlay_indicators:
|
| 30 |
-
if ind in indicators:
|
| 31 |
-
fig.add_trace(go.Scatter(
|
| 32 |
-
x=df.index,
|
| 33 |
-
y=indicators[ind],
|
| 34 |
-
mode='lines',
|
| 35 |
-
name=ind,
|
| 36 |
-
visible='legendonly' # default off, toggle via legend
|
| 37 |
-
))
|
| 38 |
|
| 39 |
-
#
|
| 40 |
fig.add_trace(go.Bar(
|
| 41 |
-
x=df.index,
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# --- Subplot indicators (MACD, RSI, SuperTrend, etc.) ---
|
| 49 |
-
subplots = ['MACD','MACD_signal','MACD_hist','RSI','STOCH','ADX','CCI','OBV','SuperTrend']
|
| 50 |
-
for ind in subplots:
|
| 51 |
-
if ind in indicators:
|
| 52 |
-
fig.add_trace(go.Scatter(
|
| 53 |
-
x=df.index,
|
| 54 |
-
y=indicators[ind],
|
| 55 |
-
mode='lines',
|
| 56 |
-
name=ind,
|
| 57 |
-
visible='legendonly',
|
| 58 |
-
yaxis='y3'
|
| 59 |
-
))
|
| 60 |
-
|
| 61 |
-
# --- Layout ---
|
| 62 |
fig.update_layout(
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
legend=dict(orientation='h', y=-0.2),
|
| 68 |
-
margin=dict(l=50, r=50, t=50, b=100),
|
| 69 |
-
height=700,
|
| 70 |
-
template='plotly_white'
|
| 71 |
)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Add optional instructions
|
| 77 |
-
instructions = """
|
| 78 |
-
<div style="margin:10px 0;color:#555;">
|
| 79 |
-
<b>Instructions:</b> Click legend items to enable/disable indicators and overlays.
|
| 80 |
-
</div>
|
| 81 |
-
"""
|
| 82 |
-
|
| 83 |
-
return instructions + chart_html
|
|
|
|
| 1 |
# chart_builder.py
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.graph_objs as go
|
| 4 |
+
from plotly.subplots import make_subplots
|
| 5 |
|
| 6 |
def build_chart(df, indicators=None):
|
| 7 |
"""
|
| 8 |
+
df: OHLCV dataframe
|
| 9 |
+
indicators: dict of indicator_name -> DataFrame
|
| 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 |
+
subplot_titles=("Price & Indicators", "Volume / Subplots")
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
# Main OHLC
|
| 22 |
fig.add_trace(go.Candlestick(
|
| 23 |
+
x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], name="Price"
|
| 24 |
+
), row=1, col=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Default volume subplot
|
| 27 |
fig.add_trace(go.Bar(
|
| 28 |
+
x=df.index, y=df['Volume'], name="Volume", marker_color='lightblue'
|
| 29 |
+
), row=2, col=1)
|
| 30 |
+
|
| 31 |
+
# Overlay indicators
|
| 32 |
+
if indicators:
|
| 33 |
+
for name, ind_df in indicators.items():
|
| 34 |
+
if ind_df is None:
|
| 35 |
+
continue
|
| 36 |
+
if name.lower() in ['sma20','sma50','ema20','ema50','bb_upper','bb_middle','bb_lower','supertrend']:
|
| 37 |
+
# Overlay on main chart
|
| 38 |
+
for col in ind_df.columns:
|
| 39 |
+
fig.add_trace(go.Scatter(
|
| 40 |
+
x=ind_df.index, y=ind_df[col], mode='lines', name=col,
|
| 41 |
+
visible='legendonly' # hidden by default
|
| 42 |
+
), row=1, col=1)
|
| 43 |
+
else:
|
| 44 |
+
# Other indicators like MACD/RSI as subplots
|
| 45 |
+
for col in ind_df.columns:
|
| 46 |
+
fig.add_trace(go.Scatter(
|
| 47 |
+
x=ind_df.index, y=ind_df[col], mode='lines', name=col,
|
| 48 |
+
visible='legendonly'
|
| 49 |
+
), row=2, col=1)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
fig.update_layout(
|
| 52 |
+
xaxis_rangeslider_visible=False,
|
| 53 |
+
height=800,
|
| 54 |
+
template='plotly_dark',
|
| 55 |
+
legend=dict(orientation='h', y=1.02)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Inject JS for toggling indicators (all by legend click)
|
| 59 |
+
html_chart = fig.to_html(full_html=False, include_plotlyjs='cdn')
|
| 60 |
+
return html_chart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|