Spaces:
Running
Running
Delete chart_builder.py
Browse files- chart_builder.py +0 -73
chart_builder.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
# chart_builder.py
|
| 2 |
-
import plotly.graph_objs as go
|
| 3 |
-
from plotly.subplots import make_subplots
|
| 4 |
-
import pandas as pd
|
| 5 |
-
|
| 6 |
-
def build_chart(data, indicators):
|
| 7 |
-
"""
|
| 8 |
-
data: pd.DataFrame with OHLCV
|
| 9 |
-
indicators: dict of series (MACD, RSI, SMA20, etc.)
|
| 10 |
-
"""
|
| 11 |
-
fig = make_subplots(
|
| 12 |
-
rows=3, cols=1,
|
| 13 |
-
row_heights=[0.5, 0.2, 0.3],
|
| 14 |
-
shared_xaxes=True,
|
| 15 |
-
vertical_spacing=0.02,
|
| 16 |
-
specs=[[{"type":"candlestick"}],
|
| 17 |
-
[{"type":"bar"}],
|
| 18 |
-
[{"type":"scatter"}]] # indicator plot
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# --- Main candlestick ---
|
| 22 |
-
fig.add_trace(go.Candlestick(
|
| 23 |
-
x=data.index,
|
| 24 |
-
open=data['Open'], high=data['High'],
|
| 25 |
-
low=data['Low'], close=data['Close'],
|
| 26 |
-
name='Candlestick'
|
| 27 |
-
), row=1, col=1)
|
| 28 |
-
|
| 29 |
-
# --- Add MA overlays on main chart ---
|
| 30 |
-
for ma_name in ['SMA20','SMA50','EMA20','EMA50']:
|
| 31 |
-
if ma_name in indicators:
|
| 32 |
-
fig.add_trace(go.Scatter(
|
| 33 |
-
x=data.index, y=indicators[ma_name],
|
| 34 |
-
mode='lines', name=ma_name,
|
| 35 |
-
visible='legendonly' # initially hidden, toggle via legend or script
|
| 36 |
-
), row=1, col=1)
|
| 37 |
-
|
| 38 |
-
# --- Volume subplot ---
|
| 39 |
-
fig.add_trace(go.Bar(
|
| 40 |
-
x=data.index, y=data['Volume'],
|
| 41 |
-
name='Volume', marker_color='blue'
|
| 42 |
-
), row=2, col=1)
|
| 43 |
-
|
| 44 |
-
# --- Single indicator subplot (default empty, user selects via checkbox) ---
|
| 45 |
-
for ind_name in ['MACD', 'RSI', 'Stochastic']:
|
| 46 |
-
if ind_name in indicators:
|
| 47 |
-
fig.add_trace(go.Scatter(
|
| 48 |
-
x=data.index, y=indicators[ind_name],
|
| 49 |
-
mode='lines', name=ind_name,
|
| 50 |
-
visible=False # initially hidden, toggle via script
|
| 51 |
-
), row=3, col=1)
|
| 52 |
-
|
| 53 |
-
fig.update_layout(height=900, showlegend=True, margin=dict(l=20,r=20,t=40,b=20))
|
| 54 |
-
|
| 55 |
-
# --- Inject script for checkbox toggle ---
|
| 56 |
-
script = """
|
| 57 |
-
<script>
|
| 58 |
-
function applyIndicators() {
|
| 59 |
-
var checkboxes = document.querySelectorAll('input.indicator-toggle');
|
| 60 |
-
var update = {visible: []};
|
| 61 |
-
var traces = document.querySelectorAll('g.cartesianlayer .main-svg > g');
|
| 62 |
-
checkboxes.forEach(function(cb, i){
|
| 63 |
-
var idx = parseInt(cb.dataset.trace);
|
| 64 |
-
update.visible[idx] = cb.checked;
|
| 65 |
-
});
|
| 66 |
-
Plotly.restyle('chart', update);
|
| 67 |
-
}
|
| 68 |
-
</script>
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
# Return full HTML
|
| 72 |
-
chart_html = fig.to_html(full_html=False, include_plotlyjs=True, div_id="chart")
|
| 73 |
-
return chart_html + script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|