Create chart_builder.py
Browse files- chart_builder.py +98 -0
chart_builder.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# chart_builder.py
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
from plotly.subplots import make_subplots
|
| 4 |
+
|
| 5 |
+
def build_chart(df, indicators=None, symbol="STOCK"):
|
| 6 |
+
"""
|
| 7 |
+
df: DataFrame with ['Open','High','Low','Close','Volume']
|
| 8 |
+
indicators: dict returned from calculate_indicators
|
| 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.02,
|
| 16 |
+
subplot_titles=[f"{symbol} Price Chart", "Volume"]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# -------------------------------
|
| 20 |
+
# CANDLESTICK ON MAIN CHART
|
| 21 |
+
# -------------------------------
|
| 22 |
+
fig.add_trace(
|
| 23 |
+
go.Candlestick(
|
| 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 |
+
# ADD SUBPLOTS FOR INDICATORS
|
| 65 |
+
# -------------------------------
|
| 66 |
+
if indicators:
|
| 67 |
+
# MACD
|
| 68 |
+
if 'MACD' in indicators:
|
| 69 |
+
macd = indicators['MACD']
|
| 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 |
+
# SuperTrend as overlay
|
| 80 |
+
if 'SuperTrend' in indicators:
|
| 81 |
+
fig.add_trace(
|
| 82 |
+
go.Scatter(
|
| 83 |
+
x=df.index,
|
| 84 |
+
y=indicators['SuperTrend'],
|
| 85 |
+
name="SuperTrend",
|
| 86 |
+
line=dict(color='green')
|
| 87 |
+
),
|
| 88 |
+
row=1, col=1
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
fig.update_layout(
|
| 92 |
+
xaxis_rangeslider_visible=False,
|
| 93 |
+
template="plotly_white",
|
| 94 |
+
height=700,
|
| 95 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return fig.to_html(include_plotlyjs='cdn')
|