Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -796,33 +796,61 @@ def update_advanced_technical_chart(indicator, selected_stock, period):
|
|
| 796 |
)
|
| 797 |
|
| 798 |
elif indicator == 'MACD':
|
|
|
|
| 799 |
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
|
| 800 |
-
vertical_spacing=0.1,
|
| 801 |
-
row_heights=[0.7, 0.3],
|
| 802 |
-
subplot_titles=('
|
| 803 |
-
|
| 804 |
-
#
|
| 805 |
-
fig.add_trace(go.Scatter(
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
colors = ['green' if x >= 0 else 'red' for x in data['MACD_Histogram']]
|
| 816 |
-
fig.add_trace(go.Bar(
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
fig.add_hline(y=0, line_dash="dash", line_color="gray", row=2, col=1)
|
| 821 |
|
|
|
|
| 822 |
fig.update_layout(
|
| 823 |
-
|
| 824 |
-
height=
|
|
|
|
|
|
|
| 825 |
)
|
|
|
|
|
|
|
| 826 |
|
| 827 |
elif indicator == 'BB':
|
| 828 |
fig = go.Figure()
|
|
|
|
| 796 |
)
|
| 797 |
|
| 798 |
elif indicator == 'MACD':
|
| 799 |
+
# 建立兩個垂直排列的子圖,並共享X軸
|
| 800 |
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
|
| 801 |
+
vertical_spacing=0.1, # 子圖間的垂直間距
|
| 802 |
+
row_heights=[0.7, 0.3], # 上方圖佔70%,下方圖佔30%
|
| 803 |
+
subplot_titles=('價格走勢', 'MACD 指標')) # 設定子圖標題
|
| 804 |
+
|
| 805 |
+
# --- 上方子圖 (row=1):只繪製價格走勢 ---
|
| 806 |
+
fig.add_trace(go.Scatter(
|
| 807 |
+
x=data.index,
|
| 808 |
+
y=data['Close'],
|
| 809 |
+
mode='lines',
|
| 810 |
+
name='收盤價',
|
| 811 |
+
line=dict(color='black', width=1.5)
|
| 812 |
+
), row=1, col=1)
|
| 813 |
+
|
| 814 |
+
# --- 下方子圖 (row=2):繪製所有MACD相關指標 ---
|
| 815 |
+
# 1. MACD 快線 (DIF)
|
| 816 |
+
fig.add_trace(go.Scatter(
|
| 817 |
+
x=data.index,
|
| 818 |
+
y=data['MACD'],
|
| 819 |
+
mode='lines',
|
| 820 |
+
name='MACD (快線)',
|
| 821 |
+
line=dict(color='blue', width=2)
|
| 822 |
+
), row=2, col=1)
|
| 823 |
+
|
| 824 |
+
# 2. Signal 慢線 (MACD)
|
| 825 |
+
fig.add_trace(go.Scatter(
|
| 826 |
+
x=data.index,
|
| 827 |
+
y=data['MACD_Signal'],
|
| 828 |
+
mode='lines',
|
| 829 |
+
name='Signal (慢線)',
|
| 830 |
+
line=dict(color='red', width=2)
|
| 831 |
+
), row=2, col=1)
|
| 832 |
+
|
| 833 |
+
# 3. Histogram 柱狀圖
|
| 834 |
colors = ['green' if x >= 0 else 'red' for x in data['MACD_Histogram']]
|
| 835 |
+
fig.add_trace(go.Bar(
|
| 836 |
+
x=data.index,
|
| 837 |
+
y=data['MACD_Histogram'],
|
| 838 |
+
name='MACD柱狀圖',
|
| 839 |
+
marker_color=colors
|
| 840 |
+
), row=2, col=1)
|
| 841 |
+
|
| 842 |
+
# 在MACD子圖中添加一條零軸水平線,方便觀察
|
| 843 |
fig.add_hline(y=0, line_dash="dash", line_color="gray", row=2, col=1)
|
| 844 |
|
| 845 |
+
# 更新整個圖表的佈局
|
| 846 |
fig.update_layout(
|
| 847 |
+
title_text=f'{stock_name} - MACD 指數平滑異同移動平均線',
|
| 848 |
+
height=550, # 可以適當增加圖表高度以容納兩個子圖
|
| 849 |
+
legend_title_text='圖例',
|
| 850 |
+
showlegend=True # 確保圖例顯示
|
| 851 |
)
|
| 852 |
+
# 隱藏柱狀圖的圖例,因為顏色已經表達了正負值
|
| 853 |
+
fig.update_traces(showlegend=False, selector=dict(type='bar'))
|
| 854 |
|
| 855 |
elif indicator == 'BB':
|
| 856 |
fig = go.Figure()
|